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 214e60647aSMatthias RingwaldTo compile an example, run: 22edbc01eeSMatthias Ringwald 23edbc01eeSMatthias Ringwald make 24edbc01eeSMatthias Ringwald 25edbc01eeSMatthias Ringwald 265d426cd2SMatthias RingwaldTo upload the binary to your device, run: 27edbc01eeSMatthias Ringwald 28edbc01eeSMatthias Ringwald make flash 29edbc01eeSMatthias Ringwald 30edbc01eeSMatthias Ringwald 315d426cd2SMatthias RingwaldTo get the debug output, run: 322d4acfc3SMatthias Ringwald 332d4acfc3SMatthias Ringwald make monitor 342d4acfc3SMatthias Ringwald 355d426cd2SMatthias RingwaldYou can quit the monitor with CTRL-]. 362d4acfc3SMatthias Ringwald 374e60647aSMatthias Ringwald## Old Make Versions 384e60647aSMatthias Ringwald 39aba3b91aSMatthias RingwaldCompilation fails with older versions of the make tool, e.g. make 3.8.1 (from 2006) provided by the current Xcode 9 on macOS or Ubuntu 14.04.1 LTS. 40aba3b91aSMatthias Ringwald 414e60647aSMatthias RingwaldInterestingly, if you run make a second time, it completes the compilation. 424e60647aSMatthias Ringwald 43eb6282bcSMatthias Ringwald## Configuration 44eb6282bcSMatthias Ringwald 45eb6282bcSMatthias RingwaldThe sdkconfig of the example template disables the original Bluedroid stack by disabling the CONFIG_BLUEDROID_ENABLED kconfig option. 46eb6282bcSMatthias Ringwald 472d4acfc3SMatthias Ringwald## Limitations 482d4acfc3SMatthias Ringwald 49aee3eb85SMatthias Ringwald### Issues with the Bluetooth Controller Implementation 50d851289bSMatthias Ringwald 51aee3eb85SMatthias 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 522d4acfc3SMatthias Ringwald 53*a057b0dbSMatthias Ringwald### Audio playback 54*a057b0dbSMatthias Ringwald 55*a057b0dbSMatthias RingwaldAudio playback is implemented by `btstack_audio_esp32.c`. It assumes an I2S Codec connected as follows: 56*a057b0dbSMatthias Ringwald 57*a057b0dbSMatthias RingwaldESP32 pin | I2S Pin 58*a057b0dbSMatthias Ringwald----------|--------- 59*a057b0dbSMatthias RingwaldGPIO25 | LRCK 60*a057b0dbSMatthias RingwaldGPIO26 | BCLK 61*a057b0dbSMatthias RingwaldGPIO22 | DATA 62*a057b0dbSMatthias Ringwald 63*a057b0dbSMatthias RingwaldWe've 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 implements a basic Bluetooth loudspeaker. 64*a057b0dbSMatthias Ringwald 65*a057b0dbSMatthias RingwaldAudio input via I2S or ADC is not supported yet. We might have a look at it when [HFP/SCO via VHCI](https://github.com/espressif/esp-idf/issues/1118) is working on the ESP32. 66*a057b0dbSMatthias Ringwald 6700948339SMatthias Ringwald### Multi-Threading 682d4acfc3SMatthias Ringwald 695f209a26SMatthias 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. 705d426cd2SMatthias RingwaldTo call a function from the BTstack thread, there are currently two options: 712d4acfc3SMatthias Ringwald 725f209a26SMatthias 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. 735d426cd2SMatthias Ringwald- Setup a BTstack Data Source (btstack_data_source_t): 745f209a26SMatthias 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*. 752d4acfc3SMatthias Ringwald 765d426cd2SMatthias RingwaldWith both options, the called function should check if there are any pending BTstack tasks and execute them. 775d426cd2SMatthias Ringwald 785d426cd2SMatthias 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. 795f209a26SMatthias Ringwald 802d4acfc3SMatthias RingwaldWe're considering different options to make BTstack thread-safe, but for now, please use one of the suggested options. 812d4acfc3SMatthias Ringwald 822d1ab7bfSMatthias Ringwald### Acknowledgments 83edbc01eeSMatthias Ringwald 846d9d5ca8SMatthias RingwaldFirst HCI Reset was sent to Bluetooth chipset by [@mattkelly](https://github.com/mattkelly) 85