Skip to content

Commit 3198a85

Browse files
committed
pbio/drv/bluetooth_stm32_cc2640: Use non-blocking debug driver.
This lets us investigate the order of Bluetooth operations to find out where issues occur.
1 parent dfbcfa8 commit 3198a85

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,18 @@
4545

4646
PROCESS(pbdrv_bluetooth_spi_process, "Bluetooth SPI");
4747

48+
#define DEBUG 0
49+
50+
#if DEBUG
51+
#include <pbdrv/../../drv/uart/uart_debug_first_port.h>
52+
// You can selectively enable these as relevant for the debug session.
53+
// Remember to set PBDRV_CONFIG_UART_DEBUG_FIRST_PORT in pbdrvconfig.
54+
#define DBG pbdrv_uart_debug_printf
55+
#define DEBUG_PRINT pbdrv_uart_debug_printf
56+
#else
4857
#define DBG(...)
4958
#define DEBUG_PRINT(...)
50-
#define DEBUG_PRINT_PT(...)
51-
59+
#endif
5260
// hub name goes in special section so that it can be modified when flashing firmware
5361
__attribute__((section(".name")))
5462
char pbdrv_bluetooth_hub_name[16] = "Pybricks Hub";
@@ -506,7 +514,7 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
506514
// Optionally, disconnect from host (usually Pybricks Code).
507515
if (conn_handle != NO_CONNECTION &&
508516
(peri->options & PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_DISCONNECT_HOST)) {
509-
DEBUG_PRINT_PT(pt, "Disconnect from Pybricks code (%d).\n", conn_handle);
517+
DEBUG_PRINT("Disconnect from Pybricks code (%d).\n", conn_handle);
510518
// Guard used in pbdrv_bluetooth_is_connected so higher level processes
511519
// won't try to send anything while we are disconnecting.
512520
busy_disconnecting = true;
@@ -523,7 +531,7 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
523531
PROCESS_CONTEXT_END(&pbdrv_bluetooth_spi_process);
524532

525533
// start scanning
526-
DEBUG_PRINT_PT(pt, "Start scanning.\n");
534+
DEBUG_PRINT("Start scanning.\n");
527535
PT_WAIT_WHILE(pt, write_xfer_size);
528536
GAP_DeviceDiscoveryRequest(GAP_DEVICE_DISCOVERY_MODE_ALL, 1, GAP_FILTER_POLICY_SCAN_ANY_CONNECT_ANY);
529537
PT_WAIT_UNTIL(pt, hci_command_status);
@@ -555,7 +563,7 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
555563
GAP_DeviceDiscoveryCancel();
556564
PT_WAIT_UNTIL(pt, hci_command_status);
557565
PT_WAIT_UNTIL(pt, device_discovery_done);
558-
DEBUG_PRINT_PT(pt, "Sub-scan interval timed out.\n");
566+
DEBUG_PRINT("Sub-scan interval timed out.\n");
559567
goto restart_scan;
560568
}
561569

@@ -597,7 +605,7 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
597605
GAP_DeviceDiscoveryCancel();
598606
PT_WAIT_UNTIL(pt, hci_command_status);
599607
PT_WAIT_UNTIL(pt, device_discovery_done);
600-
DEBUG_PRINT_PT(pt, "Scan response timed out.\n");
608+
DEBUG_PRINT("Scan response timed out.\n");
601609
goto restart_scan;
602610
}
603611

@@ -630,7 +638,7 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
630638
assert(peri->con_handle == NO_CONNECTION);
631639
bond_auth_err = NO_AUTH;
632640
connection_error = PBIO_SUCCESS;
633-
DEBUG_PRINT_PT(pt, "Found %s. Going to connect.\n", peri->name);
641+
DEBUG_PRINT("Found %s. Going to connect.\n", peri->name);
634642

635643
// Configure to initiate pairing right after connect if bonding required.
636644
// NB: We must unset "initiate" before we allow a new connection to
@@ -658,7 +666,7 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
658666
peri->con_handle != NO_CONNECTION;
659667
}));
660668

661-
DEBUG_PRINT_PT(pt, "Connected.\n");
669+
DEBUG_PRINT("Connected.\n");
662670

663671
if (peri->options & PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_PAIR) {
664672
PT_WAIT_UNTIL(pt, ({
@@ -668,7 +676,7 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
668676
}
669677
bond_auth_err != NO_AUTH;
670678
}));
671-
DEBUG_PRINT_PT(pt, "Auth complete: 0x%02x\n", bond_auth_err);
679+
DEBUG_PRINT("Auth complete: 0x%02x\n", bond_auth_err);
672680

673681
if (bond_auth_err != 0) {
674682
if (bond_auth_err == bleInvalidEventId) {
@@ -694,14 +702,14 @@ static PT_THREAD(peripheral_scan_and_connect_task(struct pt *pt, pbio_task_t *ta
694702

695703
cancel_auth_then_disconnect:
696704

697-
DEBUG_PRINT_PT(pt, "Cancel auth.\n");
705+
DEBUG_PRINT("Cancel auth.\n");
698706
PT_WAIT_WHILE(pt, write_xfer_size);
699707
GAP_TerminateAuth(peri->con_handle, 0x13);
700708
PT_WAIT_UNTIL(pt, hci_command_status);
701709

702710
disconnect:
703711

704-
DEBUG_PRINT_PT(pt, "Disconnect due to %s.\n", task->cancel ? "cancel" : "error");
712+
DEBUG_PRINT("Disconnect due to %s.\n", task->cancel ? "cancel" : "error");
705713
PT_WAIT_WHILE(pt, write_xfer_size);
706714
GAP_TerminateLinkReq(peri->con_handle, 0x13);
707715
PT_WAIT_UNTIL(pt, hci_command_status);
@@ -893,7 +901,7 @@ static PT_THREAD(periperal_read_characteristic_task(struct pt *pt, pbio_task_t *
893901
static uint8_t status;
894902

895903
retry:
896-
DEBUG_PRINT_PT(pt, "going to read %04x:\n", peri->char_now->handle);
904+
DEBUG_PRINT("going to read %04x:\n", peri->char_now->handle);
897905
PT_WAIT_WHILE(pt, write_xfer_size);
898906
{
899907
attReadReq_t req = {
@@ -943,7 +951,7 @@ static PT_THREAD(periperal_read_characteristic_task(struct pt *pt, pbio_task_t *
943951
});
944952
}));
945953

946-
DEBUG_PRINT_PT(pt, "Read %04x with status %d\n", peri->char_now->handle, status);
954+
DEBUG_PRINT("Read %04x with status %d\n", peri->char_now->handle, status);
947955

948956
exit:
949957
task->status = ble_error_to_pbio_error(status);
@@ -1854,7 +1862,7 @@ static PT_THREAD(gap_init(struct pt *pt)) {
18541862
// random address used by Pybricks. Bonding takes only a few seconds, so we
18551863
// just always clear the bond information if there is any, and start over.
18561864
if (read_buf[12] > 0) {
1857-
DEBUG_PRINT_PT(pt, "Old bond count: %d\n", read_buf[12]);
1865+
DEBUG_PRINT("Old bond count: %d\n", read_buf[12]);
18581866

18591867
// Erase all bonds stored on Bluetooth chip. We can also erase local
18601868
// info, but this does not appear to be necessary.
@@ -1866,7 +1874,7 @@ static PT_THREAD(gap_init(struct pt *pt)) {
18661874
PT_WAIT_WHILE(pt, write_xfer_size);
18671875
GAP_BondMgrGetParameter(GAPBOND_BOND_COUNT);
18681876
PT_WAIT_UNTIL(pt, hci_command_status);
1869-
DEBUG_PRINT_PT(pt, "New bond count: %d\n", read_buf[12]);
1877+
DEBUG_PRINT("New bond count: %d\n", read_buf[12]);
18701878
}
18711879

18721880
PT_WAIT_WHILE(pt, write_xfer_size);

0 commit comments

Comments
 (0)