1# BTstack Port for the Espressif ESP32 Platform 2 3Status: Basic port incl. all examples. BTstack runs on dedicated FreeRTOS thread. Multi threading not supported. 4 5## Setup 6 7- Follow [Espressif IoT Development Framework (ESP-IDF) setup](https://github.com/espressif/esp-idf) to install XTensa toolchain and the ESP-IDF. 8- In port/esp32, configure the serial port for firmware upload as described in the ESP-IDF setup guides. 9 10## Usage 11 12In port/esp32, run 13 14 ./create_examples.py 15 16The script will create project folders for all examples. Each example project folder, e.g. port/esp32/spp_and_le_counter, contains a Makefile. 17 18To compile the example, run: 19 20 make 21 22 23To upload the binary to your device, run: 24 25 make flash 26 27 28To get the debug output, run: 29 30 make monitor 31 32You can quit the monitor with CTRL-]. 33 34## Configuration 35 36The sdkconfig of the example template disables the original Bluedroid stack by disabling the CONFIG_BLUEDROID_ENABLED kconfig option. 37 38## Limitations 39 40### Bug in ESP32 VHCI Implementation 41The 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 42 43### Multi-Threading 44 45BTstack 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. 46To call a function from the BTstack thread, there are currently two options: 47 48- *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. 49- Setup a BTstack Data Source (btstack_data_source_t): 50 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*. 51 52With both options, the called function should check if there are any pending BTstack tasks and execute them. 53 54The '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. 55 56We're considering different options to make BTstack thread-safe, but for now, please use one of the suggested options. 57 58### Acknowledgments 59 60First HCI Reset was sent to Bluetooth chipset by [@mattkelly](https://github.com/mattkelly) 61