1edbc01eeSMatthias Ringwald# BTstack Port for the Espressif ESP32 Platform 2edbc01eeSMatthias Ringwald 32d4acfc3SMatthias RingwaldStatus: Basic port incl. all examples. BTstack runs on dedicated FreeRTOS thread. Multi threading 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. 85d426cd2SMatthias Ringwald- In port/esp32, configure the serial port for firmware upload as described in the ESP-IDF setup guides. 9edbc01eeSMatthias Ringwald 10edbc01eeSMatthias Ringwald## Usage 11edbc01eeSMatthias Ringwald 12a681a241SMatthias RingwaldIn port/esp32, run 13a681a241SMatthias Ringwald 142d4acfc3SMatthias Ringwald ./create_examples.py 15a681a241SMatthias Ringwald 165d426cd2SMatthias RingwaldThe script will create project folders for all examples. Each example project folder, e.g. port/esp32/spp_and_le_counter, contains a Makefile. 175d426cd2SMatthias Ringwald 185d426cd2SMatthias RingwaldTo compile the example, run: 19edbc01eeSMatthias Ringwald 20edbc01eeSMatthias Ringwald make 21edbc01eeSMatthias Ringwald 22edbc01eeSMatthias Ringwald 235d426cd2SMatthias RingwaldTo upload the binary to your device, run: 24edbc01eeSMatthias Ringwald 25edbc01eeSMatthias Ringwald make flash 26edbc01eeSMatthias Ringwald 27edbc01eeSMatthias Ringwald 285d426cd2SMatthias RingwaldTo get the debug output, run: 292d4acfc3SMatthias Ringwald 302d4acfc3SMatthias Ringwald make monitor 312d4acfc3SMatthias Ringwald 325d426cd2SMatthias RingwaldYou can quit the monitor with CTRL-]. 332d4acfc3SMatthias Ringwald 34*eb6282bcSMatthias Ringwald## Configuration 35*eb6282bcSMatthias Ringwald 36*eb6282bcSMatthias RingwaldThe sdkconfig of the example template disables the original Bluedroid stack by disabling the CONFIG_BLUEDROID_ENABLED kconfig option. 37*eb6282bcSMatthias Ringwald 382d4acfc3SMatthias Ringwald## Limitations 392d4acfc3SMatthias Ringwald 4000948339SMatthias Ringwald### Bug in ESP32 VHCI Implementation 415d426cd2SMatthias RingwaldThe Host Controller to Host Flow Control of the Virtual HCI (VHCI) on the ESP32 does not work currently. Without it, it is not possible to slow down incoming data. For most applications, this won't be an issue, but please keep it in mind. See https://github.com/espressif/esp-idf/issues/480 422d4acfc3SMatthias Ringwald 4300948339SMatthias Ringwald### Multi-Threading 442d4acfc3SMatthias Ringwald 455f209a26SMatthias 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. 465d426cd2SMatthias RingwaldTo call a function from the BTstack thread, there are currently two options: 472d4acfc3SMatthias Ringwald 485f209a26SMatthias 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. 495d426cd2SMatthias Ringwald- Setup a BTstack Data Source (btstack_data_source_t): 505f209a26SMatthias 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*. 512d4acfc3SMatthias Ringwald 525d426cd2SMatthias RingwaldWith both options, the called function should check if there are any pending BTstack tasks and execute them. 535d426cd2SMatthias Ringwald 545d426cd2SMatthias 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. 555f209a26SMatthias Ringwald 562d4acfc3SMatthias RingwaldWe're considering different options to make BTstack thread-safe, but for now, please use one of the suggested options. 572d4acfc3SMatthias Ringwald 582d1ab7bfSMatthias Ringwald### Acknowledgments 59edbc01eeSMatthias Ringwald 606d9d5ca8SMatthias RingwaldFirst HCI Reset was sent to Bluetooth chipset by [@mattkelly](https://github.com/mattkelly) 61