esp32: use synchronous VHCI API with newer ESP32 variants (esp32,-c3, s3 use asynchronous API)

This commit is contained in:
Matthias Ringwald 2024-12-10 14:21:42 +01:00
parent 94fd3c039b
commit c9822bc75b
2 changed files with 32 additions and 22 deletions

View File

@ -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 - 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 - POSIX: clear run loop exit flag
- btstack_util: skip whitespace in btstack_atoi - btstack_util: skip whitespace in btstack_atoi
- esp32: use synchronous VHCI API with newer ESP32 variants (esp32, -c3, and -s3 use asynchronous API)
### Changed ### Changed
- GAP: return command disallowed if disconnect already requested - GAP: return command disallowed if disconnect already requested
- GAP: improve handling of incorrectly resolved addresses in HCI_SUBEVENT_LE_CONNECTION_COMPLETE - GAP: improve handling of incorrectly resolved addresses in HCI_SUBEVENT_LE_CONNECTION_COMPLETE

View File

@ -77,6 +77,15 @@ uint32_t hal_time_ms(void) {
#ifdef CONFIG_BT_ENABLED #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 // assert pre-buffer for packet type is available
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < 1) #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 #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){ static int transport_open(void){
esp_err_t ret; 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)); 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; transport_packet_handler = handler;
} }
#ifdef ENABLE_ESP32_VHCI_ASYNCHRONOUS
static int transport_can_send_packet_now(uint8_t packet_type) { static int transport_can_send_packet_now(uint8_t packet_type) {
return esp_vhci_host_check_send_available(); return esp_vhci_host_check_send_available();
} }
#endif
static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){ static int transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){
// store packet type before actual data and increase 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 = { static const hci_transport_t transport = {
"esp32-vhci", .name = "esp32-vhci",
&transport_init, .init = &transport_init,
&transport_open, .open = &transport_open,
&transport_close, .close = &transport_close,
&transport_register_packet_handler, .register_packet_handler = &transport_register_packet_handler,
&transport_can_send_packet_now, #ifdef ENABLE_ESP32_VHCI_ASYNCHRONOUS
&transport_send_packet, // asynchronous
NULL, // set baud rate .can_send_packet_now = &transport_can_send_packet_now,
NULL, // reset link #else
NULL, // set SCO config // synchronous <=> can_send_packet_now == NULL
#endif
.send_packet = &transport_send_packet,
}; };
#else #else
@ -327,16 +344,8 @@ static void transport_init(const void *transport_config){
} }
static const hci_transport_t transport = { static const hci_transport_t transport = {
"none", .name = "none",
&transport_init, .init = &transport_init,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL, // set baud rate
NULL, // reset link
NULL, // set SCO config
}; };
#endif #endif