Skip to content

Commit 25069f5

Browse files
authored
btstack: configure incoming pre-buffer for cyw43 driver (#2165)
1 parent d1a1c9f commit 25069f5

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@
66

77
#include "pico.h"
88
#include "cyw43.h"
9-
#include "hci_transport.h"
9+
#include "btstack_config.h"
1010
#include "hci.h"
11+
#include "hci_transport.h"
1112
#include "pico/btstack_hci_transport_cyw43.h"
1213
#include "pico/btstack_chipset_cyw43.h"
1314

15+
// cyw43_bluetooth_hci_write and cyw43_bluetooth_hci_read require a custom 4-byte packet header in front of the actual HCI packet
16+
// the HCI packet type is stored in the fourth byte of the packet header
17+
#define CYW43_PACKET_HEADER_SIZE 4
18+
1419
// assert outgoing pre-buffer for cyw43 header is available
15-
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < 4)
16-
#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined or smaller than 4. Please update btstack_config.h
20+
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < CYW43_PACKET_HEADER_SIZE)
21+
#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined or smaller than 4 (CYW43_PACKET_HEADER_SIZE) bytes. Please update btstack_config.h
1722
#endif
1823

1924
// assert outgoing packet fragments are word aligned
2025
#if !defined(HCI_ACL_CHUNK_SIZE_ALIGNMENT) || ((HCI_ACL_CHUNK_SIZE_ALIGNMENT & 3) != 0)
21-
#error HCI_ACL_CHUNK_SIZE_ALIGNMENT not defined or not a multiply of 4. Please update btstack_config.h
26+
#error HCI_ACL_CHUNK_SIZE_ALIGNMENT not defined or not a multiple of 4. Please update btstack_config.h
2227
#endif
2328

29+
// ensure incoming pre-buffer for cyw43 header is available (defaults from btstack/src/hci.h)
30+
#if HCI_INCOMING_PRE_BUFFER_SIZE < CYW43_PACKET_HEADER_SIZE
31+
#undef HCI_INCOMING_PRE_BUFFER_SIZE
32+
#define HCI_INCOMING_PRE_BUFFER_SIZE CYW43_PACKET_HEADER_SIZE
33+
#endif
34+
35+
// ensure buffer for cyw43_bluetooth_hci_read starts word aligned (word align pre buffer)
36+
#define HCI_INCOMING_PRE_BUFFER_SIZE_ALIGNED ((HCI_INCOMING_PRE_BUFFER_SIZE + 3) & ~3)
37+
2438
#define BT_DEBUG_ENABLED 0
2539
#if BT_DEBUG_ENABLED
2640
#define BT_DEBUG(...) CYW43_PRINTF(__VA_ARGS__)
@@ -31,9 +45,12 @@
3145
// Callback when we have data
3246
static void (*hci_transport_cyw43_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = NULL;
3347

34-
// Incoming packet buffer - cyw43 packet header (incl packet type) + incoming pre buffer + max(acl header + acl payload, event header + event data)
48+
// The incoming packet buffer consist of a pre-buffer and the actual HCI packet
49+
// For the call to cyw43_bluetooth_hci_read, the last 4 bytes (CY43_PACKET_HEADER_SIZE) of the pre-buffer is used for the CYW43 packet header
50+
// After that, only the actual HCI packet is forwarded to BTstack, which expects HCI_INCOMING_PACKET_BUFFER_SIZE of pre-buffer bytes for its own use.
3551
__attribute__((aligned(4)))
36-
static uint8_t hci_packet_with_pre_buffer[4 + HCI_INCOMING_PRE_BUFFER_SIZE + HCI_INCOMING_PACKET_BUFFER_SIZE ];
52+
static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE_ALIGNED + HCI_INCOMING_PACKET_BUFFER_SIZE ];
53+
static uint8_t * cyw43_receive_buffer = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE_ALIGNED - CYW43_PACKET_HEADER_SIZE];
3754

3855
static btstack_data_source_t transport_data_source;
3956
static bool hci_transport_ready;
@@ -97,8 +114,8 @@ static int hci_transport_cyw43_can_send_now(uint8_t packet_type) {
97114
static int hci_transport_cyw43_send_packet(uint8_t packet_type, uint8_t *packet, int size) {
98115
// store packet type before actual data and increase size
99116
// This relies on HCI_OUTGOING_PRE_BUFFER_SIZE being set
100-
uint8_t *buffer = &packet[-4];
101-
uint32_t buffer_size = size + 4;
117+
uint8_t *buffer = &packet[-CYW43_PACKET_HEADER_SIZE];
118+
uint32_t buffer_size = size + CYW43_PACKET_HEADER_SIZE;
102119
buffer[3] = packet_type;
103120

104121
CYW43_THREAD_ENTER
@@ -143,10 +160,10 @@ static void hci_transport_cyw43_process(void) {
143160
uint32_t loop_count = 0;
144161
#endif
145162
do {
146-
int err = cyw43_bluetooth_hci_read(hci_packet_with_pre_buffer, sizeof(hci_packet_with_pre_buffer), &len);
163+
int err = cyw43_bluetooth_hci_read(cyw43_receive_buffer, CYW43_PACKET_HEADER_SIZE + HCI_INCOMING_PACKET_BUFFER_SIZE , &len);
147164
BT_DEBUG("bt in len=%lu err=%d\n", len, err);
148165
if (err == 0 && len > 0) {
149-
hci_transport_cyw43_packet_handler(hci_packet_with_pre_buffer[3], hci_packet_with_pre_buffer + 4, len - 4);
166+
hci_transport_cyw43_packet_handler(cyw43_receive_buffer[3], &cyw43_receive_buffer[CYW43_PACKET_HEADER_SIZE], len - CYW43_PACKET_HEADER_SIZE);
150167
has_work = true;
151168
} else {
152169
has_work = false;

0 commit comments

Comments
 (0)