Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ESP32 as Bluetooth controller #28872

Closed
Cimex97 opened this issue Oct 2, 2020 · 45 comments
Closed

Support ESP32 as Bluetooth controller #28872

Cimex97 opened this issue Oct 2, 2020 · 45 comments
Labels
area: Bluetooth Feature Request A request for a new feature priority: low Low impact/importance bug

Comments

@Cimex97
Copy link

Cimex97 commented Oct 2, 2020

Describe the bug
I want to build the samples/bluetooth/peripheral_hr as a host only with an extern esp32 as a controller.
For this I use the nucleo_f411re board.

*** Booting Zephyr OS build zephyr-v2.4.0  ***
ASSERTION FAIL [err == 0] @ WEST_TOPDIR/zephyr/subsys/bluetooth/host/hci_core.c:375
        k_sem_take failed with err -11
[00:00:10.014,000] <err> os: r0/a1:  0x00000003  r1/a2:  0x0000000a  r2/a3:  0xffffffbf
[00:00:10.014,000] <err> os: r3/a4:  0x08001761 r12/ip:  0x00000000 r14/lr:  0x080037d9
[00:00:10.014,000] <err> os:  xpsr:  0x61000000
[00:00:10.014,000] <err> os: Faulting instruction address (r15/pc): 0x080037e4
[00:00:10.014,000] <err> os: >>> ZEPHYR FATAL ERROR 3: Kernel oops on CPU 0
[00:00:10.014,000] <err> os: Current thread: 0x20000da8 (unknown)
[00:00:10.059,000] <err> os: Halting system

To Reproduce
I add hw-flow-control to the dts, the PINMUX functions for RTS/CTS pins of the UART_1 and set the configuration BT_UART_ON_DEV_NAME="UART_1".

&usart1 {
	current-speed = <115200>;
	status = "okay";
	hw-flow-control;
};
static const struct pin_config pinconf[] = {
#if DT_NODE_HAS_STATUS(DT_NODELABEL(usart1), okay) && CONFIG_SERIAL
	{STM32_PIN_PB6, STM32F4_PINMUX_FUNC_PB6_USART1_TX},
	{STM32_PIN_PB7, STM32F4_PINMUX_FUNC_PB7_USART1_RX},
	{STM32_PIN_PA11, STM32F4_PINMUX_FUNC_PA11_USART1_CTS},
	{STM32_PIN_PA12, STM32F4_PINMUX_FUNC_PA12_USART1_RTS},
#endif

Steps to reproduce the behavior:

  1. cd samples/bluetooth/peripheral_hr
  2. west build -b nucle_f411re
  3. west flash
  4. See error

Expected behavior
I expected that I can use Bluetooth specific APIs in my application if I connect an external Bluetooth controller, like the esp32.

Impact
I can't implement my application, because I want to use Zephyr, instead of the esp-idf for the hardware independent.

Logs and console output
Here is logic analyzer trace:
Screen Capture_select-area_20201002155806
I see that there is a communication, but I don't know why the RTS/CTS pins don't change the state.

Environment (please complete the following information):

  • OS: linux
  • zephyr 2.4.0
  • arm-none-eabi-gcc v10.2
  • board: nucleo_f411re
  • bluetooth controller: esp32 with a hci_uart interface

Additional context
On the ESP32 run this sample which make a uart_hci interface available. This works fine with a raspberry pi 2b+.
I used here the BlueZ and attach the device via: btattach -B /dev/ttyAMA0 -P h4 -S 115200

@Cimex97 Cimex97 added the bug The issue is a bug, or the PR is fixing a bug label Oct 2, 2020
@erwango erwango added the platform: STM32 ST Micro STM32 label Oct 5, 2020
@erwango
Copy link
Member

erwango commented Oct 5, 2020

@Cimex97 Hard to reproduce w/o the matching HW. Can you enable CONFIG_BT_DEBUG_HCI_DRIVER ?

@Cimex97
Copy link
Author

Cimex97 commented Oct 5, 2020

Here is the output with CONFIG_BT_DEBUG_HCI_DRIVER=y

*** Booting Zephyr OS build zephyr-v2.4.0  ***
[00:00:00.004,000] <dbg> bt_driver.h4_open: 
[00:00:00.004,000] <dbg> bt_driver.rx_thread: started
[00:00:00.004,000] <dbg> bt_driver.rx_thread: rx.buf 0x00000000
[00:00:00.004,000] <dbg> bt_driver.h4_send: buf 0x20003370 type 0 len 3
ASSERTION FAIL [err == 0] @ WEST_TOPDIR/zephyr/subsys/bluetooth/host/hci_core.c:375
        k_sem_take failed with err -11
[00:00:10.014,000] <err> os: r0/a1:  0x00000003  r1/a2:  0x0000000a  r2/a3:  0xffffffbf
[00:00:10.014,000] <err> os: r3/a4:  0x08001761 r12/ip:  0x00000000 r14/lr:  0x080039d1
[00:00:10.014,000] <err> os:  xpsr:  0x61000000
[00:00:10.014,000] <err> os: Faulting instruction address (r15/pc): 0x080039dc
[00:00:10.014,000] <err> os: >>> ZEPHYR FATAL ERROR 3: Kernel oops on CPU 0
[00:00:10.014,000] <err> os: Current thread: 0x20000da8 (unknown)
[00:00:10.059,000] <err> os: Halting system

@nashif nashif added the priority: low Low impact/importance bug label Oct 13, 2020
@erwango
Copy link
Member

erwango commented Nov 4, 2020

@Cimex97 genuine question: ESP32 is supported on Zephyr as a WIFI controller, but is that supposed to work in BLE mode ?

@erwango
Copy link
Member

erwango commented Nov 4, 2020

@Cimex97 genuine question: ESP32 is supported on Zephyr as a WIFI controller, but is that supposed to work in BLE mode ?

@jhedberg maybe you can answer this question?

@Cimex97
Copy link
Author

Cimex97 commented Nov 5, 2020

Zephyr don't support the ESP32 with BLE mode. This wrote @carlescufi in this issue.

But in my case, zephyr doesn't run on the ESP32.

@erwango
Copy link
Member

erwango commented Nov 5, 2020

@Cimex97 ok, so we can close this issue then, right ?

@Cimex97
Copy link
Author

Cimex97 commented Nov 5, 2020

Maybe not. I want to use a nucleof411re board as a Bluetooth host and the ESP32 as a Bluetooth controller.

The ESP32 is connected over UART to the nucleof411re and I want to use the hci_uart interface to have Bluetooth support on this board.

@erwango erwango removed platform: STM32 ST Micro STM32 bug The issue is a bug, or the PR is fixing a bug labels Nov 5, 2020
@erwango
Copy link
Member

erwango commented Nov 5, 2020

I want to use a nucleof411re board as a Bluetooth host and the ESP32 as a Bluetooth controller.

So maybe it could work, but in that case, since there is no official support for that, this is not a bug but a feature request and might require some development.

@erwango erwango removed their assignment Nov 5, 2020
@erwango erwango added Feature Request A request for a new feature area: Bluetooth labels Nov 5, 2020
@erwango erwango changed the title Bluetooth Host only does not work with nucleo_f411re Support ESP32 as Bluetooth controller Nov 5, 2020
@carlescufi
Copy link
Member

Zephyr don't support the ESP32 with BLE mode. This wrote @carlescufi in this issue.

But in my case, zephyr doesn't run on the ESP32.

Is there a build that acts as a controller over UART for the ESP32? Or can you make one? If you can come up with one, then using Zephyr as the Host is trivial and requires only configuration.

@Cimex97
Copy link
Author

Cimex97 commented Nov 16, 2020

I have running this sample on the ESP32 and can it use as a Bluetoothcontroler. This is come from the ESP-IDF.

This works fine when I build a Zephyr sample for the native_posix_64 board. But this doesn't work if I build it for the nucleo_f411re.

@erwango
Copy link
Member

erwango commented Nov 20, 2020

@Cimex97, would you mind providing mode information on the sample configurations you're using (native_posix_64 vs nucleo_f411re).

Right now I don't have ESP32, so I can't test and see what does (/not) happen.
Though, I'm comparing uart drivers, and IIC, native_posix_64 doesn't handle "hw-flow-control".
Are you sure hw-flow-control is expected on ESP32 side ? Have you tried running the sample w/o this option ?

I'm sorry for asking these questions, but w/o available HW to reproduce, my ability to support is limited.

@Cimex97
Copy link
Author

Cimex97 commented Nov 23, 2020

I'm sure that the ESP32 need hw-flow-control. This is required for the ESP32 as a UART HCI controller.

I tested it with the samples/bluetooth/peripheral_hr (same as above). I only deactivate BT_HCI_ACL_FLOW_CONTROL=n. Maybe this isn't supported by the ESP32 HCI stack. If it is enabled I get this warning: [00:00:00.300,000] <wrn> bt_hci_core: opcode 0x0c33 status 0x11 and this error Bluetooth init failed (err -5).

This is the configuration of the sample:

CONFIG_BT=y
CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_SMP=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DIS=y
CONFIG_BT_DIS_PNP=n
CONFIG_BT_BAS=y
CONFIG_BT_HRS=y
CONFIG_BT_DEVICE_NAME="Zephyr Heartrate Sensor"
CONFIG_BT_DEVICE_APPEARANCE=833

BT_HCI_ACL_FLOW_CONTROL=n

I build and flash it onto the nucleo_f411re and I got the error message above. If I build it for native_posix_64, then I can start the application with sudo build/zephyr/zephyr.exe --bt-dev=hci0. Here the ESP32 is with a USB <-> UART converter attached to my Linux system.

@erwango
Copy link
Member

erwango commented Nov 23, 2020

@Cimex97, please note that BT_HCI_ACL_FLOW_CONTROL, is HCI level flow control, which is different from the RTS/CTS uart level flow control.

What I'm proposing is to have a try w/o the UART level flow control (&usart1 {hw-flow-control;};), as these pins don"t seem to be handled by native_posix driver. This being said, this is a naive try, as native_posix is not a basic uart driver.
Anyway, I should hopefully be able to get an ESP32 this week.

@Cimex97
Copy link
Author

Cimex97 commented Nov 23, 2020

Ok I tried to disable the hw-flow-controll.
This works for the natvie_posix_64. I attached the device with btattach -B /dev/ttyUSB1 -S 115200 -P h4 -N, where -N means no hw-flow-contoll.

On the nucle_f411re I get now a lot of this error messages:
[00:00:00.009,000] <err> bt_driver: TX interrupt but no pending buffer!

Note: I have to connect the CTS pin from the ESP32 to the RTS-pin. I don't know why.

@erwango
Copy link
Member

erwango commented Nov 23, 2020

I have to connect the CTS pin from the ESP32 to the RTS-pin

That's how CTS/RTS works.
EDIT: Do you mean CTS pin from the ESP32 to the RTS-pin from the ESP32?

On the nucle_f411re I get now a lot of this error messages:
[00:00:00.009,000] <err> bt_driver: TX interrupt but no pending buffer!

Can you try to increase BT_RX_STACK_SIZE, say CONFIG_BT_RX_STACK_SIZE=2048 ?

@Cimex97
Copy link
Author

Cimex97 commented Nov 23, 2020

Than I get this message again:

*** Booting Zephyr OS build zephyr-v2.4.0-586-g4d0750920b52  ***
ASSERTION FAIL [err == 0] @ WEST_TOPDIR/zephyr/subsys/bluetooth/host/hci_core.c:376
        k_sem_take failed with err -11
[00:00:10.015,000] <err> os: r0/a1:  0x00000003  r1/a2:  0x0000000a  r2/a3:  0xffffffbf
[00:00:10.015,000] <err> os: r3/a4:  0x080016c5 r12/ip:  0x00000000 r14/lr:  0x08003839
[00:00:10.015,000] <err> os:  xpsr:  0x61000000
[00:00:10.015,000] <err> os: Faulting instruction address (r15/pc): 0x08003844
[00:00:10.015,000] <err> os: >>> ZEPHYR FATAL ERROR 3: Kernel oops on CPU 0
[00:00:10.015,000] <err> os: Current thread: 0x20000d80 (unknown)
[00:00:10.060,000] <err> os: Halting system

@erwango
Copy link
Member

erwango commented Nov 23, 2020

ok, txs.
I'll really need to get a way to test this myself. I'll get an ESP32 and get back to you.

@Cimex97
Copy link
Author

Cimex97 commented Dec 4, 2020

Hey @erwango,
do you have some new information about this issue?
If I could help you to just ask me :).

@erwango
Copy link
Member

erwango commented Dec 4, 2020

Hi @Cimex97, sorry finally I haven't got a chance to find a ESP32 (was a ESP8266, so no bt...). I need to order one.
So I'm actually sorry, but w/o way to reproduce for now, it will be difficult to help more, at least for my part.

@Cimex97
Copy link
Author

Cimex97 commented Dec 4, 2020

Okay, thank you for your answer.

@erwango
Copy link
Member

erwango commented Jan 8, 2021

@Cimex97 not sure it could help, but since it could have an impact on the API behavior, maybe you could have a try to #31192

@carlescufi
Copy link
Member

@Cimex97 would you be able to try this again with the latest master now that #31192 is merged?

@Cimex97
Copy link
Author

Cimex97 commented Jan 14, 2021

Hey @carlescufi,
I will do it in a few weeks.

@Cimex97
Copy link
Author

Cimex97 commented Feb 1, 2021

Hey, I update now to master. I got the same error.

@carlescufi
Copy link
Member

@Cimex97 that failing k_sem_take you are getting is due to the controller not replying to a command. As to why it is not responding, the most likely reason is a UART driver problem. Could you post a full logic analyzer trace of the whole exchange, and, if possible, a transcription of the trace as bytes going to the controller and coming from the controller?

@Cimex97
Copy link
Author

Cimex97 commented Feb 3, 2021

I traced it with the logic analyzer and realized that the ESP32 does not answer. After I enable the hw-flow-control and set the CTS/RTS pins in the device tree it just works. Many thanks for your support.

pinctrl-0 = <&usart1_tx_pb6 &usart1_rx_pb7 &usart1_rts_pa12 &usart1_cts_pa11>;

Note: I have to disable BT_HCI_ACL_FLOW_CONTROL. Maybe the ESP32 does not support that.

@erwango
Copy link
Member

erwango commented Feb 3, 2021

@Cimex97 Thanks for this news! Let me know if I can close the issue.

@erwango erwango closed this as completed Feb 3, 2021
@dogtopus
Copy link

dogtopus commented Aug 8, 2021

@Cimex97 I think flow control is supported by ESP32. However there are (at least) 2 issues that prevent it from working:

  1. The ACL MTU (without header) must be >= 1017 (i.e. ACL packet length must be >= 1021). This is a known quirk as noted by IDF devs here and easily worked around by just increase the L2CAP MTU in Zephyr.
  2. The SCO MTU and count probably need to be non-zero as well. I wasn't able to verify this because for some very weird reason my ESP32 fails to even respond to Reset command (returns error code 0x80) when I set dummy values for hbs->sco_mtu (I chose 0x80) and hbs->sco_pkts (I chose 0x1) in set_flow_control in Zephyr (remind you that I run Zephyr on a separate processor and all requests are flowing through UART so a piece of code that didn't even get executed shouldn't affect the ESP32 HCI in any way 😕 ).

With these 2 solved we probably could get the flow control working but I still have no idea what's wrong with case 2.

@danergo
Copy link

danergo commented Oct 22, 2021

Hi, @Cimex97, @dogtopus, are you confirming that ESP32 can work together with Zephyr, providing only Bt-controller features which can be utilized by BlueZ apps, like hci/btattach?

@dogtopus
Copy link

dogtopus commented Oct 22, 2021

Hi, @Cimex97, @dogtopus, are you confirming that ESP32 can work together with Zephyr, providing only Bt-controller features which can be utilized by BlueZ apps, like hci/btattach?

btattach with ESP32 in HCI mode always works fine for me. Zephyr's HCI driver also works with HCI mode ESP32, but ACL flow control is currently broken.

@danergo
Copy link

danergo commented Oct 23, 2021

Thanks for confirming. Zephyr's HCI driver over UART or USB? We are trying to make it work over UART, but it seems communication stucks at one end:

BlueZ is trying to initiate the connection, but Zephyr's driver doesn't passes it correctly to HAL, therefore no response arrives back from HAL. (assumption)

@Cimex97
Copy link
Author

Cimex97 commented Oct 23, 2021

UART and USB should work. Which firmware do you flashed onto the ESP32?
Do you disable this configuration: CONFIG_BT_HCI_ACL_FLOW_CONTROL ?

@danergo
Copy link

danergo commented Oct 23, 2021

I have disabled, yes, but it doesn't work, hci0 is not coming up.
Firmware is in the samples directory "hci_uart" one with some minor adjustments for UART pins (RX,TX,RTS,CTS).

@Cimex97
Copy link
Author

Cimex97 commented Oct 23, 2021

Do you check are the RX, TX, CTS, RTS pins are connected correctly?
How do you power the ESP32 module? Try to connect it also with USB.

@danergo
Copy link

danergo commented Oct 23, 2021

Yes, of course. Also tried esp-idf's sample called "controller_hci_uart" and it works almost perfectly (only massive stresstest of hcitool lescan can cause some issues).

So wiring is surely OK.

Unfortunately USB is not an option, as I don't have a free USB on the host :/

@Cimex97
Copy link
Author

Cimex97 commented Oct 23, 2021

I had some trouble using the ESP32 as a controller without connected to USB. The ESP32 restarted permanently. I think this caused due to bad power supply. So can you try it to be sure that isn't the error?

@Cimex97
Copy link
Author

Cimex97 commented Oct 23, 2021

Yes, of course. Also tried esp-idf's sample called "controller_hci_uart" and it works almost perfectly (only massive stresstest of hcitool lescan can cause some issues).

So wiring is surely OK.

Unfortunately USB is not an option, as I don't have a free USB on the host :/

I'm not sure, but you can't use Zephyr as your Bluetooth controller firmware because of that the ESP32 isn't fully supported by Zephyr

@dogtopus
Copy link

Zephyr's HCI driver over UART or USB?

UART. OG ESP32 doesn't even have USB hardware.

@danergo
Copy link

danergo commented Oct 25, 2021

@Cimex97: I'm kind of lost now :)

UART and USB should work.

then:

you can't use Zephyr as your Bluetooth controller firmware because of that the ESP32 isn't fully supported by Zephyr.

So now which one is true? :)

@Cimex97
Copy link
Author

Cimex97 commented Oct 25, 2021

Sorry for the mistake.
You must flash the HCI controller from the esp-idf(controller_hci_uart). Then you get a HCI interface over UART which you can use with an different controller or btattach.
I'm not sure if you can use only the esp.

@danergo
Copy link

danergo commented Oct 25, 2021

Ah, I see. Thank you. ESP's implementation has a bug which bothers me, so I'm gonna search further.
I thought Zephyr can also work as a HCI controller firmware :)

@mirko
Copy link

mirko commented Dec 11, 2022

Ah, I see. Thank you. ESP's implementation has a bug which bothers me, so I'm gonna search further. I thought Zephyr can also work as a HCI controller firmware :)

Can it (by now)? I'm sorta lost about whether I can use an ESP32 on linux using basic HCI or not by today.

@danergo
Copy link

danergo commented Dec 11, 2022 via email

@mirko
Copy link

mirko commented Dec 11, 2022

You don't need zephyr, you can just use esp's hci_uart_sample (or similar, its in their repo). I could fine-tune it and proved to work for quite a long time already.

Interesting, thanks! Didn't realise they implemented the HCI stack complete enough to be usable with blueZ.
Does it work with a normal cheap node-mcu pcd and their CH9102X UART<->USB bridges? Asking, because I read that RTS/CTS is a necessity for attaching it as UART driven BLE-HCI peripheral.

@danergo
Copy link

danergo commented Dec 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Bluetooth Feature Request A request for a new feature priority: low Low impact/importance bug
Projects
None yet
Development

No branches or pull requests

7 participants