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. 8d851289bSMatthias Ringwald- Make sure your checkout is newer than 4654278b1bd6b7f7f55013f7edad76109f7ee944 from Aug 25th, 2017 9a5481ee3SMatthias Ringwald- In port/esp32/template, configure the serial port for firmware upload as described in the ESP-IDF setup guides. 10edbc01eeSMatthias Ringwald 11edbc01eeSMatthias Ringwald## Usage 12edbc01eeSMatthias Ringwald 13a681a241SMatthias RingwaldIn port/esp32, run 14a681a241SMatthias Ringwald 159c7c30eeSMatthias Ringwald ./integrate_btstack.py 16a681a241SMatthias Ringwald 179c7c30eeSMatthias 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. 189c7c30eeSMatthias Ringwald 199c7c30eeSMatthias RingwaldEach example project folder, e.g. port/esp32/examples/spp_and_le_counter, contains a Makefile. Please run the command again after updating the BTstack tree (e.g. by git pull) to also update the copy in the ESP-IDF. 205d426cd2SMatthias Ringwald 21*59200c1cSMatthias 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: 22*59200c1cSMatthias Ringwald1. set target: 23*59200c1cSMatthias Ringwald 24*59200c1cSMatthias Ringwald `idf.py set-target esp32c3` 25*59200c1cSMatthias Ringwald 26*59200c1cSMatthias Ringwald or 27*59200c1cSMatthias Ringwald 28*59200c1cSMatthias Ringwald `idf.py set-target esp32s3` 29*59200c1cSMatthias Ringwald 30*59200c1cSMatthias Ringwald2. re-enable Bluetooth Controller via menuconfig 31*59200c1cSMatthias Ringwald 32*59200c1cSMatthias Ringwald 1. `idf.py menuconfig` 33*59200c1cSMatthias Ringwald 2. select `Component Config` 34*59200c1cSMatthias Ringwald 3. select `Bluetooth` and enable 35*59200c1cSMatthias Ringwald 4. select `Bluetooth Host` 36*59200c1cSMatthias Ringwald 5. select `Controller Only` 37*59200c1cSMatthias Ringwald 6. exit and save config 38*59200c1cSMatthias Ringwald 394e60647aSMatthias RingwaldTo compile an example, run: 40edbc01eeSMatthias Ringwald 41*59200c1cSMatthias Ringwald idf.py 42edbc01eeSMatthias Ringwald 43edbc01eeSMatthias Ringwald 445d426cd2SMatthias RingwaldTo upload the binary to your device, run: 45edbc01eeSMatthias Ringwald 46*59200c1cSMatthias Ringwald idf.py -p PATH-TO-DEVICE flash 47edbc01eeSMatthias Ringwald 48edbc01eeSMatthias Ringwald 49*59200c1cSMatthias RingwaldTo get debug output, run: 502d4acfc3SMatthias Ringwald 51*59200c1cSMatthias 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 6752a720aeSMatthias RingwaldAudio playback is implemented by `btstack_audio_esp32.c` and supports generic I2S codecs as well as the ES8388 on the [ESP32 LyraT v4.3](https://docs.espressif.com/projects/esp-adf/en/latest/design-guide/board-esp32-lyrat-v4.3.html) devkit. 6852a720aeSMatthias Ringwald 6952a720aeSMatthias RingwaldIt uses the first I2S interface with the following pin out: 70a057b0dbSMatthias Ringwald 71a057b0dbSMatthias RingwaldESP32 pin | I2S Pin 72a057b0dbSMatthias Ringwald----------|--------- 7352a720aeSMatthias RingwaldGPIO0 | MCK 7452a720aeSMatthias RingwaldGPIO5 | BCLK 75a057b0dbSMatthias RingwaldGPIO25 | LRCK 7652a720aeSMatthias RingwaldGPIO26 | DOUT 7752a720aeSMatthias RingwaldGPIO35 | DIN 78a057b0dbSMatthias Ringwald 7952a720aeSMatthias RingwaldIf support for the LyraT v4.3 is enabled, e.g. 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. 80a057b0dbSMatthias Ringwald 8152a720aeSMatthias RingwaldWe've also used the MAX98357A on the [Adafruit breakout board](https://www.adafruit.com/product/3006). The simplest example is the mod_player, which plays back an 8 kB sound file and the a2dp_sink_demo that implements a basic Bluetooth loudspeaker. 8252a720aeSMatthias Ringwald 83a057b0dbSMatthias Ringwald 8400948339SMatthias Ringwald### Multi-Threading 852d4acfc3SMatthias Ringwald 865f209a26SMatthias 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. 875d426cd2SMatthias RingwaldTo call a function from the BTstack thread, there are currently two options: 882d4acfc3SMatthias Ringwald 895f209a26SMatthias Ringwald- *btstack_run_loop_freertos_execute_code_on_main_thread* allows to directly schedule a function callback, i.e. 'do BTstack tasks' function, from the BTstack thread. 905d426cd2SMatthias Ringwald- Setup a BTstack Data Source (btstack_data_source_t): 915f209a26SMatthias Ringwald Set 'do BTstack tasks' function as its process function and enable its polling callback (DATA_SOURCE_CALLBACK_POLL). The process function will be called in every iteration of the BTstack Run Loop. To trigger a run loop iteration, you can call *btstack_run_loop_freertos_trigger*. 922d4acfc3SMatthias Ringwald 935d426cd2SMatthias RingwaldWith both options, the called function should check if there are any pending BTstack tasks and execute them. 945d426cd2SMatthias Ringwald 955d426cd2SMatthias RingwaldThe 'run on main thread' method is only provided by a few ports and requires a queue to store the calls. This should be used with care, since calling it multiple times could cause the queue to overflow. 965f209a26SMatthias Ringwald 972d4acfc3SMatthias RingwaldWe're considering different options to make BTstack thread-safe, but for now, please use one of the suggested options. 982d4acfc3SMatthias Ringwald 992d1ab7bfSMatthias Ringwald### Acknowledgments 100edbc01eeSMatthias Ringwald 1016d9d5ca8SMatthias RingwaldFirst HCI Reset was sent to Bluetooth chipset by [@mattkelly](https://github.com/mattkelly) 102