Skip to content

Commit 33efceb

Browse files
committed
Tidy up bt logging
Fixes #2253
1 parent 67f89e4 commit 33efceb

File tree

3 files changed

+83
-64
lines changed

3 files changed

+83
-64
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2025 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef CYBT_LOGGING_H
8+
#define CYBT_LOGGING_H
9+
10+
// Error messages only enabled in debug by default
11+
#ifndef NDEBUG
12+
#define CYBT_ERROR_ENABLED 1
13+
#else
14+
#define CYBT_ERROR_ENABLED 0
15+
#endif
16+
17+
// Info messages only enabled in debug by default
18+
#ifndef NDEBUG
19+
#define CYBT_INFO_ENABLED 1
20+
#else
21+
#define CYBT_INFO_ENABLED 0
22+
#endif
23+
24+
// Debug messages disabled by default
25+
#define CYBT_DEBUG_ENABLED 0
26+
27+
#if CYBT_DEBUG_ENABLED || CYBT_ERROR_ENABLED
28+
#include <stdio.h>
29+
#endif
30+
31+
#if CYBT_ERROR_ENABLED
32+
#define cybt_error CYW43_WARN
33+
#else
34+
#define cybt_error(...)
35+
#endif
36+
37+
#if CYBT_INFO_ENABLED
38+
#define cybt_info CYW43_PRINTF
39+
#else
40+
#define cybt_info(...)
41+
#endif
42+
43+
#if CYBT_DEBUG_ENABLED
44+
#define cybt_debug CYW43_PRINTF
45+
#else
46+
#define cybt_debug(...)
47+
#endif
48+
49+
#endif // CYBT_LOGGING_H

src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus.c

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "cybt_shared_bus_driver.h"
1616

1717
#include "cyw43_btfw_43439.h"
18+
#include "cybt_logging.h"
1819

1920
#if CYW43_USE_HEX_BTFW
2021
extern const char brcm_patch_version[];
@@ -31,20 +32,6 @@ extern const int brcm_patch_ram_length;
3132
#define BTSDIO_FWBUF_OPER_DELAY_US (250)
3233
#define BTFW_WAIT_TIME_MS (150)
3334

34-
#define CYBT_DEBUG 0
35-
#define CYBT_VDEBUG 0
36-
37-
#if CYBT_DEBUG
38-
#define cybt_debug(format,args...) printf("%d.%d: " format, (int)cyw43_hal_ticks_ms() / 1000, (int)cyw43_hal_ticks_ms() % 1000, ## args)
39-
#else
40-
#define cybt_debug(format, ...) ((void)0)
41-
#endif
42-
#ifndef NDEBUG
43-
#define cybt_printf(format, args...) printf("%d.%d: " format, (int)cyw43_hal_ticks_ms() / 1000, (int)cyw43_hal_ticks_ms() % 1000, ## args)
44-
#else
45-
#define cybt_printf(...)
46-
#endif
47-
4835
#define ROUNDUP(x, a) ((((x) + ((a) - 1)) / (a)) * (a))
4936
#define ROUNDDN(x, a) ((x) & ~((a) - 1))
5037
#define ISALIGNED(a, x) (((uint32_t)(a) & ((x) - 1)) == 0)
@@ -123,17 +110,17 @@ int cyw43_btbus_init(cyw43_ll_t *self) {
123110

124111
ret = cybt_fw_download_prepare(&p_write_buf, &p_hex_buf);
125112
if (CYBT_SUCCESS != ret) {
126-
cybt_printf("Could not allocate memory\n");
113+
cybt_error("Could not allocate memory\n");
127114
return ret;
128115
}
129116

130117
cybt_debug("cybt_fw_download\n");
131118
const uint8_t *fw_data_buf;
132119
uint32_t fw_data_len;
133120
#if CYW43_USE_HEX_BTFW
134-
cybt_printf("CYW43_USE_HEX_BTFW is true\n");
121+
cybt_error("CYW43_USE_HEX_BTFW is true\n");
135122
#ifndef NDEBUG
136-
cybt_printf("BT FW download, version = %s\n", brcm_patch_version);
123+
cybt_info("BT FW download, version = %s\n", brcm_patch_version);
137124
#endif
138125
fw_data_len = brcm_patch_ram_length;
139126
fw_data_buf = brcm_patchram_buf;
@@ -151,7 +138,7 @@ int cyw43_btbus_init(cyw43_ll_t *self) {
151138
cybt_fw_download_finish(p_write_buf, p_hex_buf);
152139

153140
if (CYBT_SUCCESS != ret) {
154-
cybt_printf("hci_open(): FW download failed (0x%x)\n", ret);
141+
cybt_error("hci_open(): FW download failed (0x%x)\n", ret);
155142
return CYBT_ERR_HCI_INIT_FAILED;
156143
}
157144

@@ -161,7 +148,7 @@ int cyw43_btbus_init(cyw43_ll_t *self) {
161148
if (CYBT_SUCCESS == ret) {
162149
cybt_debug("hci_open(): FW download successfully\n");
163150
} else {
164-
cybt_printf("hci_open(): Failed to download FW\n");
151+
cybt_error("hci_open(): Failed to download FW\n");
165152
return CYBT_ERR_HCI_INIT_FAILED;
166153
}
167154

@@ -182,7 +169,7 @@ int cyw43_btbus_init(cyw43_ll_t *self) {
182169
return CYBT_SUCCESS;
183170
}
184171

185-
#if CYBT_VDEBUG
172+
#if 0
186173
static void dump_bytes(const uint8_t *bptr, uint32_t len) {
187174
unsigned int i = 0;
188175

@@ -198,6 +185,9 @@ static void dump_bytes(const uint8_t *bptr, uint32_t len) {
198185
}
199186
printf("\n");
200187
}
188+
#define DUMP_BYTES dump_bytes
189+
#else
190+
#define DUMP_BYTES(...)
201191
#endif
202192

203193
static cybt_result_t cybt_hci_write_buf(const uint8_t *p_data, uint32_t length) {
@@ -206,7 +196,7 @@ static cybt_result_t cybt_hci_write_buf(const uint8_t *p_data, uint32_t length)
206196

207197
assert(ISALIGNED(p_data, 4));
208198
if (!ISALIGNED(p_data, 4)) {
209-
cybt_printf("cybt_hci_write_hdr: buffer not aligned\n");
199+
cybt_error("cybt_hci_write_hdr: buffer not aligned\n");
210200
return CYBT_ERR_BADARG;
211201
}
212202

@@ -260,7 +250,7 @@ static cybt_result_t cybt_hci_read(uint8_t *p_data, uint32_t *p_length) {
260250
assert(ISALIGNED(p_data, 4));
261251
if (!ISALIGNED(p_data, 4)) {
262252
assert(false);
263-
cybt_printf("cybt_hci_read: buffer not aligned\n");
253+
cybt_error("cybt_hci_read: buffer not aligned\n");
264254
return CYBT_ERR_BADARG;
265255
}
266256

@@ -272,9 +262,9 @@ static cybt_result_t cybt_hci_read(uint8_t *p_data, uint32_t *p_length) {
272262
cybt_debug("cybt_hci_read: bt2host_in_val=%lu bt2host_out_val=%lu fw_b2h_buf_count=%ld\n",
273263
fw_membuf_info.bt2host_in_val, fw_membuf_info.bt2host_out_val, fw_b2h_buf_count);
274264
if (fw_b2h_buf_count < available) {
275-
cybt_printf("error: cybt_hci_read buffer overflow fw_b2h_buf_count=%ld available=%lu\n", fw_b2h_buf_count,
265+
cybt_error("error: cybt_hci_read buffer overflow fw_b2h_buf_count=%ld available=%lu\n", fw_b2h_buf_count,
276266
available);
277-
cybt_printf("error: cybt_hci_read bt2host_in_val=%lu bt2host_out_val=%lu\n", fw_membuf_info.bt2host_in_val,
267+
cybt_error("error: cybt_hci_read bt2host_in_val=%lu bt2host_out_val=%lu\n", fw_membuf_info.bt2host_in_val,
278268
fw_membuf_info.bt2host_out_val);
279269
panic("cyw43 buffer overflow");
280270
}
@@ -355,9 +345,7 @@ int cyw43_btbus_write(uint8_t *buf, uint32_t size) {
355345
cybt_bus_request();
356346

357347
cybt_debug("cyw43_btbus_write: %d\n", cmd_len);
358-
#if CYBT_VDEBUG
359-
dump_bytes(buf, size); // dump header and data
360-
#endif
348+
DUMP_BYTES(buf, size); // dump header and data
361349

362350
cybt_hci_write_buf(buf, size);
363351
cybt_bus_release();
@@ -376,7 +364,7 @@ static bool cybt_hci_read_packet(uint8_t *buf, uint32_t max_buf_size, uint32_t *
376364

377365
if (bt_result != CYBT_SUCCESS) {
378366
*size = 0;
379-
cybt_printf("cybt_hci_read_packet: error %d", bt_result);
367+
cybt_error("cybt_hci_read_packet: error %d", bt_result);
380368
return true;
381369
}
382370

@@ -390,7 +378,7 @@ static bool cybt_hci_read_packet(uint8_t *buf, uint32_t max_buf_size, uint32_t *
390378
uint32_t hci_read_len = ((buf[2] << 16) & 0xFFFF00) | ((buf[1] << 8) & 0xFF00) | (buf[0] & 0xFF);
391379
if (hci_read_len > max_buf_size - 4) {
392380
*size = 0;
393-
cybt_printf("cybt_hci_read_packet: too much data len %" PRId32"\n", hci_read_len);
381+
cybt_error("cybt_hci_read_packet: too much data len %" PRId32"\n", hci_read_len);
394382
assert(false);
395383
return false;
396384
}
@@ -401,7 +389,7 @@ static bool cybt_hci_read_packet(uint8_t *buf, uint32_t max_buf_size, uint32_t *
401389
bt_result = cybt_hci_read(buf + 4, &total_read_len);
402390
if (bt_result != CYBT_SUCCESS) {
403391
*size = 0;
404-
cybt_printf("cybt_hci_read_packet: read failed\n");
392+
cybt_error("cybt_hci_read_packet: read failed\n");
405393
assert(false);
406394
return false;
407395
}
@@ -413,15 +401,13 @@ static bool cybt_hci_read_packet(uint8_t *buf, uint32_t max_buf_size, uint32_t *
413401
} else {
414402
assert(total_read_len > 0);
415403
*size = total_read_len + 4;
416-
cybt_printf("cybt_hci_read_packet: failed to read all data %lu < %lu\n", total_read_len, hci_read_len);
404+
cybt_error("cybt_hci_read_packet: failed to read all data %lu < %lu\n", total_read_len, hci_read_len);
417405
//assert(false);
418406
return true;
419407
}
420408

421409
cybt_debug("cybt_hci_read_packet: %ld\n", *size);
422-
#if CYBT_VDEBUG
423-
dump_bytes(buf, *size);
424-
#endif
410+
DUMP_BYTES(buf, *size);
425411

426412
return true;
427413
}

src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus_driver.c

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "cyw43_ll.h"
1313
#include "cybt_shared_bus_driver.h"
14+
#include "cybt_logging.h"
1415

1516
// Bluetooth register corruption occurs if both wifi and bluetooth are fully utilised.
1617
#define CYBT_CORRUPTION_TEST 1
@@ -23,23 +24,6 @@ static uint32_t last_bt_ctrl_reg;
2324

2425
#endif
2526

26-
#ifndef NDEBUG
27-
#define cybt_printf(format, args ...) printf(format,##args)
28-
#else
29-
#define cybt_printf(...)
30-
#endif
31-
32-
#ifndef CYBT_DEBUG
33-
#define CYBT_DEBUG 0
34-
#endif
35-
36-
#if CYBT_DEBUG
37-
#include <stdio.h>
38-
#define cybt_debug(format, args ...) printf(format,##args)
39-
#else
40-
#define cybt_debug(format, ...) ((void)0)
41-
#endif
42-
4327

4428
/******************************************************************************
4529
* Constants
@@ -319,7 +303,7 @@ cybt_result_t cybt_fw_download(const uint8_t *p_bt_firmware,
319303
uint8_t version_len = *p_bt_firmware;
320304
assert(*(p_bt_firmware + version_len) == 0);
321305
#ifndef NDEBUG
322-
cybt_printf("BT FW download, version = %s\n", p_bt_firmware + 1);
306+
cybt_info("BT FW download, version = %s\n", p_bt_firmware + 1);
323307
#endif
324308
p_bt_firmware += version_len + 1; // skip over version
325309
p_bt_firmware += 1; // skip over record count
@@ -409,7 +393,7 @@ cybt_result_t cybt_toggle_bt_intr(void) {
409393
cybt_reg_read(HOST_CTRL_REG_ADDR, &reg_val);
410394
#if CYBT_CORRUPTION_TEST
411395
if ((reg_val & ~(BTSDIO_REG_SW_RDY_BITMASK | BTSDIO_REG_WAKE_BT_BITMASK | BTSDIO_REG_DATA_VALID_BITMASK)) != 0) {
412-
cybt_printf("cybt_toggle_bt_intr read HOST_CTRL_REG_ADDR as 0x%08lx\n", reg_val);
396+
cybt_error("cybt_toggle_bt_intr read HOST_CTRL_REG_ADDR as 0x%08lx\n", reg_val);
413397
cybt_debug_dump();
414398
panic("cyw43 btsdio register corruption");
415399
}
@@ -485,32 +469,32 @@ void cybt_debug_dump(void) {
485469
uint32_t reg_val = 0;
486470
cybt_fw_membuf_index_t buf_index;
487471

488-
cybt_printf("WLAN_RAM_BASE_ADDR: 0x%08lx\n", WLAN_RAM_BASE_ADDR);
489-
cybt_printf("H2B_BUF_ADDR: 0x%08lx\n", H2B_BUF_ADDR);
490-
cybt_printf("B2H_BUF_ADDR: 0x%08lx\n", B2H_BUF_ADDR);
472+
cybt_debug("WLAN_RAM_BASE_ADDR: 0x%08lx\n", WLAN_RAM_BASE_ADDR);
473+
cybt_debug("H2B_BUF_ADDR: 0x%08lx\n", H2B_BUF_ADDR);
474+
cybt_debug("B2H_BUF_ADDR: 0x%08lx\n", B2H_BUF_ADDR);
491475

492476
cybt_reg_read(H2B_BUF_IN_ADDR, &buf_index.host2bt_in_val);
493-
cybt_printf("H2B_BUF_IN_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", H2B_BUF_IN_ADDR, buf_index.host2bt_in_val,
477+
cybt_debug("H2B_BUF_IN_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", H2B_BUF_IN_ADDR, buf_index.host2bt_in_val,
494478
last_buf_index.host2bt_in_val);
495479

496480
cybt_reg_read(H2B_BUF_OUT_ADDR, &buf_index.host2bt_out_val);
497-
cybt_printf("H2B_BUF_OUT_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", H2B_BUF_OUT_ADDR, buf_index.host2bt_out_val,
481+
cybt_debug("H2B_BUF_OUT_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", H2B_BUF_OUT_ADDR, buf_index.host2bt_out_val,
498482
last_buf_index.host2bt_out_val);
499483

500484
cybt_reg_read(B2H_BUF_IN_ADDR, &buf_index.bt2host_in_val);
501-
cybt_printf("B2H_BUF_IN_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", B2H_BUF_IN_ADDR, buf_index.bt2host_in_val,
485+
cybt_debug("B2H_BUF_IN_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", B2H_BUF_IN_ADDR, buf_index.bt2host_in_val,
502486
last_buf_index.bt2host_in_val);
503487

504488
cybt_reg_read(B2H_BUF_OUT_ADDR, &buf_index.bt2host_out_val);
505-
cybt_printf("B2H_BUF_OUT_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", B2H_BUF_OUT_ADDR, buf_index.bt2host_out_val,
489+
cybt_debug("B2H_BUF_OUT_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", B2H_BUF_OUT_ADDR, buf_index.bt2host_out_val,
506490
last_buf_index.bt2host_out_val);
507491

508492
cybt_reg_read(HOST_CTRL_REG_ADDR, &reg_val);
509-
cybt_printf("HOST_CTRL_REG_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", HOST_CTRL_REG_ADDR, reg_val,
493+
cybt_debug("HOST_CTRL_REG_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", HOST_CTRL_REG_ADDR, reg_val,
510494
last_host_ctrl_reg);
511495

512496
cybt_reg_read(BT_CTRL_REG_ADDR, &reg_val);
513-
cybt_printf("BT_CTRL_REG_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", BT_CTRL_REG_ADDR, reg_val, last_bt_ctrl_reg);
497+
cybt_debug("BT_CTRL_REG_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", BT_CTRL_REG_ADDR, reg_val, last_bt_ctrl_reg);
514498
#endif
515499
}
516500

@@ -533,7 +517,7 @@ cybt_result_t cybt_get_bt_buf_index(cybt_fw_membuf_index_t *p_buf_index) {
533517
#if CYBT_CORRUPTION_TEST
534518
if (p_buf_index->host2bt_in_val >= BTSDIO_FWBUF_SIZE || p_buf_index->host2bt_out_val >= BTSDIO_FWBUF_SIZE ||
535519
p_buf_index->bt2host_in_val >= BTSDIO_FWBUF_SIZE || p_buf_index->bt2host_out_val >= BTSDIO_FWBUF_SIZE) {
536-
cybt_printf("cybt_get_bt_buf_index invalid buffer value\n");
520+
cybt_error("cybt_get_bt_buf_index invalid buffer value\n");
537521
cybt_debug_dump();
538522
} else {
539523
memcpy((uint8_t *) &last_buf_index, (uint8_t *) p_buf_index, sizeof(cybt_fw_membuf_index_t));
@@ -567,7 +551,7 @@ static cybt_result_t cybt_reg_read(uint32_t reg_addr, uint32_t *p_value) {
567551
return CYBT_SUCCESS;
568552
}
569553

570-
#if CYBT_DEBUG
554+
#if 0
571555
static void dump_bytes(const uint8_t *bptr, uint32_t len) {
572556
unsigned int i = 0;
573557

0 commit comments

Comments
 (0)