1edbc01eeSMatthias Ringwald# BTstack Port for the Espressif ESP32 Platform 2edbc01eeSMatthias Ringwald 3a5481ee3SMatthias RingwaldStatus: Basic port incl. all examples. BTstack runs on dedicated FreeRTOS thread. Multi threading (calling BTstack functions from a different thread) is not supported. 4edbc01eeSMatthias Ringwald 5edbc01eeSMatthias Ringwald## Setup 6edbc01eeSMatthias Ringwald 75d426cd2SMatthias Ringwald- Follow [Espressif IoT Development Framework (ESP-IDF) setup](https://github.com/espressif/esp-idf) to install XTensa toolchain and the ESP-IDF. 8b696904aSMatthias Ringwald- Currently used for testing: ESP-IDF v4.4 or v5.0 9edbc01eeSMatthias Ringwald 10edbc01eeSMatthias Ringwald## Usage 11edbc01eeSMatthias Ringwald 12a681a241SMatthias RingwaldIn port/esp32, run 13a681a241SMatthias Ringwald 149c7c30eeSMatthias Ringwald ./integrate_btstack.py 15a681a241SMatthias Ringwald 169c7c30eeSMatthias RingwaldThe script will copy parts of the BTstack tree into the ESP-IDF as $IDF_PATH/components/btstack and then create project folders for all examples. 179c7c30eeSMatthias Ringwald 18b696904aSMatthias RingwaldEach example project folder, e.g. port/esp32/examples/spp_and_le_counter, contains a CMake project file. Please run the `integrate_btstack.py` 19b696904aSMatthias Ringwaldcommand again after updating the BTstack tree (e.g. by git pull) to also update the copy in the ESP-IDF. 205d426cd2SMatthias Ringwald 2159200c1cSMatthias RingwaldThe examples are configure for the original ESP32. IF you want to use the newer ESP32-C3 or ESP32-S3 - both only support Bluetooth LE - you need to: 2259200c1cSMatthias Ringwald1. set target: 2359200c1cSMatthias Ringwald 2459200c1cSMatthias Ringwald `idf.py set-target esp32c3` 2559200c1cSMatthias Ringwald 2659200c1cSMatthias Ringwald or 2759200c1cSMatthias Ringwald 2859200c1cSMatthias Ringwald `idf.py set-target esp32s3` 2959200c1cSMatthias Ringwald 3059200c1cSMatthias Ringwald2. re-enable Bluetooth Controller via menuconfig 3159200c1cSMatthias Ringwald 3259200c1cSMatthias Ringwald 1. `idf.py menuconfig` 3359200c1cSMatthias Ringwald 2. select `Component Config` 3459200c1cSMatthias Ringwald 3. select `Bluetooth` and enable 3559200c1cSMatthias Ringwald 4. select `Bluetooth Host` 3659200c1cSMatthias Ringwald 5. select `Controller Only` 3759200c1cSMatthias Ringwald 6. exit and save config 3859200c1cSMatthias Ringwald 394e60647aSMatthias RingwaldTo compile an example, run: 40edbc01eeSMatthias Ringwald 4159200c1cSMatthias Ringwald idf.py 42edbc01eeSMatthias Ringwald 43edbc01eeSMatthias Ringwald 445d426cd2SMatthias RingwaldTo upload the binary to your device, run: 45edbc01eeSMatthias Ringwald 4659200c1cSMatthias Ringwald idf.py -p PATH-TO-DEVICE flash 47edbc01eeSMatthias Ringwald 48edbc01eeSMatthias Ringwald 4959200c1cSMatthias RingwaldTo get debug output, run: 502d4acfc3SMatthias Ringwald 5159200c1cSMatthias Ringwald idf.py monitor 522d4acfc3SMatthias Ringwald 535d426cd2SMatthias RingwaldYou can quit the monitor with CTRL-]. 542d4acfc3SMatthias Ringwald 55eb6282bcSMatthias Ringwald## Configuration 56eb6282bcSMatthias Ringwald 57eb6282bcSMatthias RingwaldThe sdkconfig of the example template disables the original Bluedroid stack by disabling the CONFIG_BLUEDROID_ENABLED kconfig option. 58eb6282bcSMatthias Ringwald 592d4acfc3SMatthias Ringwald## Limitations 602d4acfc3SMatthias Ringwald 61aee3eb85SMatthias Ringwald### Issues with the Bluetooth Controller Implementation 62d851289bSMatthias Ringwald 63aee3eb85SMatthias RingwaldThere are different issues in the Bluetooth Controller of the ESP32 that is provided in binary. We've submitted appropriate issues on the GitHub Issues page here: https://github.com/espressif/esp-idf/issues/created_by/mringwal 642d4acfc3SMatthias Ringwald 65a057b0dbSMatthias Ringwald### Audio playback 66a057b0dbSMatthias Ringwald 67*6857b998SMatthias RingwaldAudio playback is implemented by `btstack_audio_esp32_v4.c` resp. `btstack_audio_esp32_v5.c` and supports generic I2S codecs as well as the ES8388 on the 68b696904aSMatthias Ringwald[ESP32 LyraT v4.3](https://docs.espressif.com/projects/esp-adf/en/latest/design-guide/board-esp32-lyrat-v4.3.html) devkit. 6952a720aeSMatthias Ringwald 70*6857b998SMatthias RingwaldDue to the various ESP32 variants, please double check the I2S Configuration in the mentioned audio file and verify that the correct menuconfig options are set. 71*6857b998SMatthias Ringwald 72*6857b998SMatthias RingwaldFor a generic ESP32, the first I2S interface is used with with the following pin out: 73a057b0dbSMatthias Ringwald 74a057b0dbSMatthias RingwaldESP32 pin | I2S Pin 75a057b0dbSMatthias Ringwald----------|--------- 7652a720aeSMatthias RingwaldGPIO0 | MCK 7752a720aeSMatthias RingwaldGPIO5 | BCLK 78a057b0dbSMatthias RingwaldGPIO25 | LRCK 7952a720aeSMatthias RingwaldGPIO26 | DOUT 8052a720aeSMatthias RingwaldGPIO35 | DIN 81a057b0dbSMatthias Ringwald 82b696904aSMatthias RingwaldIf support for the LyraT v4.3 is enabled via menuconfig - Example Board Configuration --> ESP32 board --> ESP32-LyraT V4.3, CONFIG_ESP_LYRAT_V4_3_BOARD gets defined and the ES8388 will be configured as well. 83a057b0dbSMatthias Ringwald 84b696904aSMatthias RingwaldWe've also used the MAX98357A on the [Adafruit breakout board](https://www.adafruit.com/product/3006). 85*6857b998SMatthias RingwaldThe simplest audio example is the `mod_player`, which plays back an 8 kB sound file and the a2dp_sink_demo that implements a basic Bluetooth loudspeaker. 86*6857b998SMatthias RingwaldYou can test the audio input with the `audio_duplex` example. 8752a720aeSMatthias Ringwald 88b696904aSMatthias Ringwald## ESP32 printf/log 89b696904aSMatthias Ringwald 90b696904aSMatthias RingwaldThe BTstack port setups a buffered output for printf/ESP log macros. However, if that gets full, the main thread will get blocked. If you're playing audio, e.g. a2dp_sink_demo, and a lot of text 91b696904aSMatthias Ringwaldis sent over the UART, this will cause audio glitches as the I2S DMA driver isn't re-filled on time. If this happens, you can increase the UART baudrate, reduce the debug output or increase the output 92b696904aSMatthias Ringwaldbuffer size - or any combination of these. :) 93a057b0dbSMatthias Ringwald 9400948339SMatthias Ringwald### Multi-Threading 952d4acfc3SMatthias Ringwald 965f209a26SMatthias RingwaldBTstack is not thread-safe, but you're using a multi-threading OS. Any function that is called from BTstack, e.g. packet handlers, can directly call into BTstack without issues. For other situations, you need to provide some general 'do BTstack tasks' function and trigger BTstack to execute it on its own thread. 97b696904aSMatthias RingwaldTo call a function from the BTstack thread, you can use *btstack_run_loop_execute_on_main_thread* allows to directly schedule a function callback, 98b696904aSMatthias Ringwaldi.e. 'do BTstack tasks' function, from the BTstack thread. The called function should check if there are any pending BTstack tasks and execute them. 995f209a26SMatthias Ringwald 1002d4acfc3SMatthias RingwaldWe're considering different options to make BTstack thread-safe, but for now, please use one of the suggested options. 1012d4acfc3SMatthias Ringwald 1022d1ab7bfSMatthias Ringwald### Acknowledgments 103edbc01eeSMatthias Ringwald 1046d9d5ca8SMatthias RingwaldFirst HCI Reset was sent to Bluetooth chipset by [@mattkelly](https://github.com/mattkelly) 105