Zephyr ESP32 蓝牙试用

Creative Commons
本作品采用知识共享署名

本文简要说明如何通过配置启用zephyr上esp32蓝牙并运行一个测试例程。

Zephyr-ESP32-蓝牙驱动简析一文中简要分析了esp32 蓝牙如何集成进Zephyr,本文接着说明如果在Zephyr内使用ESP32蓝牙。

在Zepher中使用ESP32蓝牙比WIFI还要简单只要基于esp32 board编译的APP配置了CONFIG_BT=y就会默认打开ESP32蓝牙的配置CONFIG_BT_ESP32,详细可见boards\xtensa\esp32\Kconfig.defconfig.
之后就直接使用zephyr提供的蓝牙API进行应用编程即可。
本文不说明如何进行蓝牙编程,试跑一个Zephyr的蓝牙例程eddystone,这是Google BLE Beacon,用于蓝牙定位。
例程的配置文件如下

1
2
3
4
CONFIG_BT=y
CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="Zephyr Eddystone"

应用主程序如下, 实际的各种流程都在bt_ready中展开

1
2
3
4
5
6
7
8
9
10
11
12
13
void main(void)
{
int err;

bt_conn_cb_register(&conn_callbacks);
k_work_init_delayable(&idle_work, idle_timeout);

/* Initialize the Bluetooth Subsystem */
err = bt_enable(bt_ready);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
}
}

编译并下载

1
2
west build -p -b esp32 zephyrproject/zephyr/samples/bluetooth/eddystone
west flash --esp-device /dev/ttyS11

运行log如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
▒▒*** Booting Zephyr OS build v2.6.0-rc1-300-g6ce0f2ee6606  ***
phy_version: 4500, 0cd6843, Sep 17 2020, 15:37:07, 0, 2
Coex register schm btdm cb faild
Bluetooth initialized
Initial advertising as C4:DD:57:5B:F8:DE (public)
Configuration mode: waiting connections...
[00:00:00.911,000] <dbg> bt_hci_driver_esp32.hci_esp_host_rcv_pkt: Calling bt_recv(0x3fff4c28)
--- 98 messages dropped ---
[00:00:00.911,000] <inf> bt_hci_core: Identity: C4:DD:57:5B:F8:DE (public)
[00:00:00.911,000] <inf> bt_hci_core: HCI: version 4.2 (0x08) revision 0x030e, manufacturer 0x0060
[00:00:00.911,000] <inf> bt_hci_core: LMP: version 4.2 (0x08) subver 0x030e
[00:00:00.911,000] <dbg> bt_hci_driver_esp32.bt_esp32_send: buf 0x3fff4c28 type 0 len 18
[00:00:00.911,000] <dbg> bt_hci_driver_esp32: Final HCI buffer:
01 06 20 0f a0 00 f0 00 00 00 00 00 00 00 00 00 |.. ..... ........
00 07 00 |...

在手机上通过nrf connect可以看到”Zephyr Eddystone”


从上面可以看到zephyr上面使用ESP32蓝牙完全可以不用关心驱动,是单纯应用层的编程。

参考

https://docs.zephyrproject.org/latest/samples/bluetooth/eddystone/README.html