|
6 | 6 |
|
7 | 7 | #include "pico.h"
|
8 | 8 | #include "cyw43.h"
|
9 |
| -#include "hci_transport.h" |
| 9 | +#include "btstack_config.h" |
10 | 10 | #include "hci.h"
|
| 11 | +#include "hci_transport.h" |
11 | 12 | #include "pico/btstack_hci_transport_cyw43.h"
|
12 | 13 | #include "pico/btstack_chipset_cyw43.h"
|
13 | 14 |
|
| 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 | + |
14 | 19 | // 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 |
17 | 22 | #endif
|
18 | 23 |
|
19 | 24 | // assert outgoing packet fragments are word aligned
|
20 | 25 | #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 |
22 | 27 | #endif
|
23 | 28 |
|
| 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 | + |
24 | 38 | #define BT_DEBUG_ENABLED 0
|
25 | 39 | #if BT_DEBUG_ENABLED
|
26 | 40 | #define BT_DEBUG(...) CYW43_PRINTF(__VA_ARGS__)
|
|
31 | 45 | // Callback when we have data
|
32 | 46 | static void (*hci_transport_cyw43_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = NULL;
|
33 | 47 |
|
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. |
35 | 51 | __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]; |
37 | 54 |
|
38 | 55 | static btstack_data_source_t transport_data_source;
|
39 | 56 | static bool hci_transport_ready;
|
@@ -97,8 +114,8 @@ static int hci_transport_cyw43_can_send_now(uint8_t packet_type) {
|
97 | 114 | static int hci_transport_cyw43_send_packet(uint8_t packet_type, uint8_t *packet, int size) {
|
98 | 115 | // store packet type before actual data and increase size
|
99 | 116 | // 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; |
102 | 119 | buffer[3] = packet_type;
|
103 | 120 |
|
104 | 121 | CYW43_THREAD_ENTER
|
@@ -143,10 +160,10 @@ static void hci_transport_cyw43_process(void) {
|
143 | 160 | uint32_t loop_count = 0;
|
144 | 161 | #endif
|
145 | 162 | 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); |
147 | 164 | BT_DEBUG("bt in len=%lu err=%d\n", len, err);
|
148 | 165 | 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); |
150 | 167 | has_work = true;
|
151 | 168 | } else {
|
152 | 169 | has_work = false;
|
|
0 commit comments