From c9822bc75bf4df6fff7430d2816763bdd5fa34fe Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 10 Dec 2024 14:21:42 +0100 Subject: [PATCH] esp32: use synchronous VHCI API with newer ESP32 variants (esp32,-c3, s3 use asynchronous API) --- CHANGELOG.md | 3 +- .../components/btstack/btstack_port_esp32.c | 51 +++++++++++-------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d66a130da..c1506bcbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,7 +47,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - HID Host: omit Report ID in Set/Get Report and send report for report id == HID_REPORT_ID_UNDEFINED - POSIX: clear run loop exit flag - btstack_util: skip whitespace in btstack_atoi - +- esp32: use synchronous VHCI API with newer ESP32 variants (esp32, -c3, and -s3 use asynchronous API) + ### Changed - GAP: return command disallowed if disconnect already requested - GAP: improve handling of incorrectly resolved addresses in HCI_SUBEVENT_LE_CONNECTION_COMPLETE diff --git a/port/esp32/components/btstack/btstack_port_esp32.c b/port/esp32/components/btstack/btstack_port_esp32.c index 5d17046eb..ad72c5152 100644 --- a/port/esp32/components/btstack/btstack_port_esp32.c +++ b/port/esp32/components/btstack/btstack_port_esp32.c @@ -77,6 +77,15 @@ uint32_t hal_time_ms(void) { #ifdef CONFIG_BT_ENABLED +// ESP32 and next generation ESP32-C3 + ESP32-S3 provide an asynchronous VHCI interface with +// a callback when the packet was accepted by the Controller. +// Newer Controller only rely on the regular Host to Controller Flow Control and can be +// considered synchronous + +#if defined(CONFIG_IDF_TARGET_ESP32) || defined (CONFIG_IDF_TARGET_ESP32C3) || defined (CONFIG_IDF_TARGET_ESP32S3) +#define ENABLE_ESP32_VHCI_ASYNCHRONOUS +#endif + // assert pre-buffer for packet type is available #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < 1) #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined or smaller than 1. Please update hci.h @@ -211,7 +220,11 @@ static int bt_controller_initialized; static int transport_open(void){ esp_err_t ret; - log_info("transport_open"); +#ifdef ENABLE_ESP32_VHCI_ASYNCHRONOUS + log_info("transport_open: using asynchronous VHCI"); +#else + log_info("transport_open: using synchronous VHCI"); +#endif btstack_ring_buffer_init(&hci_ringbuffer, hci_ringbuffer_storage, sizeof(hci_ringbuffer_storage)); @@ -284,9 +297,11 @@ static void transport_register_packet_handler(void (*handler)(uint8_t packet_typ transport_packet_handler = handler; } +#ifdef ENABLE_ESP32_VHCI_ASYNCHRONOUS static int transport_can_send_packet_now(uint8_t packet_type) { return esp_vhci_host_check_send_available(); } +#endif static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){ // store packet type before actual data and increase size @@ -300,16 +315,18 @@ static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size) } static const hci_transport_t transport = { - "esp32-vhci", - &transport_init, - &transport_open, - &transport_close, - &transport_register_packet_handler, - &transport_can_send_packet_now, - &transport_send_packet, - NULL, // set baud rate - NULL, // reset link - NULL, // set SCO config + .name = "esp32-vhci", + .init = &transport_init, + .open = &transport_open, + .close = &transport_close, + .register_packet_handler = &transport_register_packet_handler, +#ifdef ENABLE_ESP32_VHCI_ASYNCHRONOUS + // asynchronous + .can_send_packet_now = &transport_can_send_packet_now, +#else + // synchronous <=> can_send_packet_now == NULL +#endif + .send_packet = &transport_send_packet, }; #else @@ -327,16 +344,8 @@ static void transport_init(const void *transport_config){ } static const hci_transport_t transport = { - "none", - &transport_init, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, // set baud rate - NULL, // reset link - NULL, // set SCO config + .name = "none", + .init = &transport_init, }; #endif