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. 8*d851289bSMatthias 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 152d4acfc3SMatthias Ringwald ./create_examples.py 16a681a241SMatthias Ringwald 175d426cd2SMatthias RingwaldThe script will create project folders for all examples. Each example project folder, e.g. port/esp32/spp_and_le_counter, contains a Makefile. 185d426cd2SMatthias Ringwald 195d426cd2SMatthias RingwaldTo compile the example, run: 20edbc01eeSMatthias Ringwald 21edbc01eeSMatthias Ringwald make 22edbc01eeSMatthias Ringwald 23edbc01eeSMatthias Ringwald 245d426cd2SMatthias RingwaldTo upload the binary to your device, run: 25edbc01eeSMatthias Ringwald 26edbc01eeSMatthias Ringwald make flash 27edbc01eeSMatthias Ringwald 28edbc01eeSMatthias Ringwald 295d426cd2SMatthias RingwaldTo get the debug output, run: 302d4acfc3SMatthias Ringwald 312d4acfc3SMatthias Ringwald make monitor 322d4acfc3SMatthias Ringwald 335d426cd2SMatthias RingwaldYou can quit the monitor with CTRL-]. 342d4acfc3SMatthias Ringwald 35eb6282bcSMatthias Ringwald## Configuration 36eb6282bcSMatthias Ringwald 37eb6282bcSMatthias RingwaldThe sdkconfig of the example template disables the original Bluedroid stack by disabling the CONFIG_BLUEDROID_ENABLED kconfig option. 38eb6282bcSMatthias Ringwald 392d4acfc3SMatthias Ringwald## Limitations 402d4acfc3SMatthias Ringwald 41*d851289bSMatthias Ringwald### Bug in Host Controller to Host Flow Control implementation 42*d851289bSMatthias RingwaldThe Link Layer in the ESP32 does not reset the supervision timeout while the Host does not accept new packets (due to slow processing etc..), which will triggger a disconnect based on Connection Timeout (reason 8). See https://github.com/espressif/esp-idf/issues/644#issuecomment-325251315 43*d851289bSMatthias Ringwald 44*d851289bSMatthias RingwaldFor most applications, this won't be an issue, but please keep it in mind. 452d4acfc3SMatthias Ringwald 4600948339SMatthias Ringwald### Multi-Threading 472d4acfc3SMatthias Ringwald 485f209a26SMatthias 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. 495d426cd2SMatthias RingwaldTo call a function from the BTstack thread, there are currently two options: 502d4acfc3SMatthias Ringwald 515f209a26SMatthias 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. 525d426cd2SMatthias Ringwald- Setup a BTstack Data Source (btstack_data_source_t): 535f209a26SMatthias 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*. 542d4acfc3SMatthias Ringwald 555d426cd2SMatthias RingwaldWith both options, the called function should check if there are any pending BTstack tasks and execute them. 565d426cd2SMatthias Ringwald 575d426cd2SMatthias 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. 585f209a26SMatthias Ringwald 592d4acfc3SMatthias RingwaldWe're considering different options to make BTstack thread-safe, but for now, please use one of the suggested options. 602d4acfc3SMatthias Ringwald 612d1ab7bfSMatthias Ringwald### Acknowledgments 62edbc01eeSMatthias Ringwald 636d9d5ca8SMatthias RingwaldFirst HCI Reset was sent to Bluetooth chipset by [@mattkelly](https://github.com/mattkelly) 64