Skip to content

Commit 0309019

Browse files
committed
pbio/drv/ioport_debug_uart: Follow uart updates.
This can be used to debug non-blocking protothreads like the Bluetooth driver. We need to explicitly call back to the relevant process since the uart drivers are no longer broadcasting to every process.
1 parent 158cf0e commit 0309019

File tree

4 files changed

+41
-101
lines changed

4 files changed

+41
-101
lines changed

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
#endif
4141

4242
#if 0
43-
#include <pbdrv/../../drv/ioport/ioport_debug_uart.h>
44-
#define DEBUG_PRINT pbdrv_ioport_debug_uart_printf
43+
#define DEBUG_PRINT(...)
4544
#else
4645
#define DEBUG_PRINT(...)
4746
#endif

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,22 @@
4343

4444
#include "./bluetooth_stm32_cc2640.h"
4545

46-
#define DEBUG_LL (0x01)
47-
#define DEBUG_PT (0x02)
46+
PROCESS(pbdrv_bluetooth_spi_process, "Bluetooth SPI");
4847

49-
// Choose either/or DEBUG_LL | DEBUG_PT
50-
#define DEBUG (0)
48+
#define DEBUG_ON_LAST_UART_PORT (0)
5149

52-
#if DEBUG
50+
#if DEBUG_ON_LAST_UART_PORT
5351
#include <pbdrv/../../drv/ioport/ioport_debug_uart.h>
54-
#endif
55-
#if (DEBUG & DEBUG_LL)
56-
#define DBG pbdrv_ioport_debug_uart_printf
57-
#else
5852
#define DBG(...)
59-
#endif
60-
#if (DEBUG & DEBUG_PT)
61-
#define DEBUG_PRINT pbdrv_ioport_debug_uart_printf
53+
#define DEBUG_PRINT(...)
6254
#define DEBUG_PRINT_PT PBDRV_IOPORT_DEBUG_UART_PT_PRINTF
55+
static void uart_poll_callback(pbdrv_uart_dev_t *uart) {
56+
process_poll(&pbdrv_bluetooth_spi_process);
57+
}
6358
#else
64-
#define DEBUG_PRINT_PT(...)
59+
#define DBG(...)
6560
#define DEBUG_PRINT(...)
61+
#define DEBUG_PRINT_PT(...)
6662
#endif
6763

6864
// hub name goes in special section so that it can be modified when flashing firmware
@@ -156,8 +152,6 @@ static uint16_t uart_service_handle, uart_service_end_handle, uart_rx_char_handl
156152
// Nordic UART tx notifications enabled
157153
static bool uart_tx_notify_en;
158154

159-
PROCESS(pbdrv_bluetooth_spi_process, "Bluetooth SPI");
160-
161155
LIST(task_queue);
162156
static bool bluetooth_ready;
163157
static pbdrv_bluetooth_on_event_t bluetooth_on_event;
@@ -2277,6 +2271,14 @@ PROCESS_THREAD(pbdrv_bluetooth_spi_process, ev, data) {
22772271

22782272
PROCESS_BEGIN();
22792273

2274+
#if DEBUG_ON_LAST_UART_PORT
2275+
// Wait for the UART to be ready for debugging.
2276+
while (pbdrv_ioport_debug_uart_init(uart_poll_callback) != PBIO_SUCCESS) {
2277+
etimer_set(&timer, 100);
2278+
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
2279+
}
2280+
#endif // DEBUG_ON_LAST_UART_PORT
2281+
22802282
start:
22812283
// take Bluetooth chip out of reset
22822284
bluetooth_reset(RESET_STATE_OUT_HIGH);

lib/pbio/drv/ioport/ioport_debug_uart.c

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#if PBDRV_CONFIG_IOPORT_DEBUG_UART
77

8-
#error "This driver can currently not be used."
9-
108
#include <stdio.h>
119
#include <string.h>
1210
#include <stdarg.h>
@@ -22,16 +20,13 @@
2220
#error "PBDRV_CONFIG_IOPORT_DEBUG_UART requires exactly one port"
2321
#endif
2422

25-
static char buffer[64];
26-
static pbdrv_uart_dev_t *debug_uart;
27-
28-
#define UART_TIMEOUT (250)
23+
// Use globals for simplified debug macro inside protothreads.
24+
struct pt debug_printf_thread;
25+
char debug_buffer[64];
26+
pbdrv_uart_dev_t *debug_uart;
27+
pbio_error_t debug_err;
2928

30-
static pbio_error_t pbdrv_ioport_init(void) {
31-
// UART already initialized.
32-
if (debug_uart) {
33-
return PBIO_SUCCESS;
34-
}
29+
pbio_error_t pbdrv_ioport_debug_uart_init(pbdrv_uart_poll_callback_t callback) {
3530

3631
// Get the debug uart from last port.
3732
const pbdrv_ioport_pup_port_platform_data_t *port_data = &pbdrv_ioport_pup_platform_data.ports[PBDRV_CONFIG_IOPORT_NUM_DEV - 1];
@@ -41,6 +36,8 @@ static pbio_error_t pbdrv_ioport_init(void) {
4136
return err;
4237
}
4338

39+
pbdrv_uart_set_poll_callback(debug_uart, callback);
40+
4441
// Enable and configure uart.
4542
const pbdrv_ioport_pup_pins_t *pins = &port_data->pins;
4643
pbdrv_gpio_alt(&pins->uart_rx, pins->uart_alt);
@@ -52,60 +49,11 @@ static pbio_error_t pbdrv_ioport_init(void) {
5249
return PBIO_SUCCESS;
5350
}
5451

55-
pbio_error_t pbdrv_ioport_debug_uart_printf(const char *format, ...) {
56-
57-
// Get the debug uart.
58-
pbio_error_t err = pbdrv_ioport_init();
59-
if (err != PBIO_SUCCESS) {
60-
return err;
61-
}
62-
63-
// If still writing, just ignore this debug message.
64-
err = pbdrv_uart_write_end(debug_uart);
65-
if (err != PBIO_SUCCESS) {
66-
return err;
67-
}
68-
69-
// Buffer result.
70-
va_list args;
71-
va_start(args, format);
72-
vsnprintf(buffer, sizeof(buffer), format, args);
73-
va_end(args);
74-
75-
// Write to uart.
76-
return pbdrv_uart_write_begin(debug_uart, (uint8_t *)buffer, strlen(buffer), UART_TIMEOUT);
77-
}
78-
79-
// Protothread set up here for simplified debug macro inside protothreads.
80-
struct pt printf_thread;
81-
82-
PT_THREAD(pbdrv_ioport_debug_uart_printf_thread(struct pt *pt, const char *format, ...)) {
83-
static pbio_error_t err;
84-
85-
PT_BEGIN(pt);
86-
87-
// Only print if port initialized.
88-
err = pbdrv_ioport_init();
89-
if (err != PBIO_SUCCESS) {
90-
PT_EXIT(pt);
91-
}
92-
93-
// Buffer result.
52+
void pbdrv_ioport_debug_uart_printf_buffer(const char *format, ...) {
9453
va_list args;
9554
va_start(args, format);
96-
vsnprintf(buffer, sizeof(buffer), format, args);
55+
vsnprintf(debug_buffer, sizeof(debug_buffer), format, args);
9756
va_end(args);
98-
99-
// Wait for uart to be ready then write.
100-
PT_WAIT_UNTIL((pt), (err = pbdrv_uart_write_begin(debug_uart, (uint8_t *)buffer, strlen(buffer), UART_TIMEOUT)) != PBIO_ERROR_AGAIN);
101-
if (err != PBIO_SUCCESS) {
102-
PT_EXIT(pt);
103-
}
104-
105-
// Wait for uart to finish.
106-
PT_WAIT_UNTIL((pt), (err = pbdrv_uart_write_end(debug_uart)) != PBIO_ERROR_AGAIN);
107-
108-
PT_END(pt);
10957
}
11058

11159
#endif // PBDRV_CONFIG_IOPORT_DEBUG_UART

lib/pbio/drv/ioport/ioport_debug_uart.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,18 @@
99
#include <pbio/error.h>
1010

1111
#include <pbdrv/config.h>
12+
#include <pbdrv/uart.h>
1213

1314
#if PBDRV_CONFIG_IOPORT_DEBUG_UART
1415

15-
extern struct pt printf_thread;
16+
extern char debug_buffer[];
17+
extern struct pt debug_printf_thread;
18+
extern pbdrv_uart_dev_t *debug_uart;
19+
extern pbio_error_t debug_err;
1620

17-
/**
18-
* Prints debug information on the last ioport.
19-
*
20-
* Will not print anything if the debug uart is already in use by another
21-
* debug message, to avoid blocking.
22-
*
23-
* Useful outside of a protothread or when timing is more critical while
24-
* complete information is not.
25-
*
26-
* @param [in] format Format string.
27-
* @param [in] ... Arguments.
28-
*
29-
* @return Error code.
30-
*/
31-
pbio_error_t pbdrv_ioport_debug_uart_printf(const char *format, ...);
21+
PT_THREAD(pbdrv_ioport_debug_uart_debug_printf_thread(struct pt *pt, const char *format, ...));
3222

33-
PT_THREAD(pbdrv_ioport_debug_uart_printf_thread(struct pt *pt, const char *format, ...));
23+
void pbdrv_ioport_debug_uart_printf_buffer(const char *format, ...);
3424

3525
/**
3626
* Spawns a task to print debug information on the last ioport.
@@ -42,13 +32,14 @@ PT_THREAD(pbdrv_ioport_debug_uart_printf_thread(struct pt *pt, const char *forma
4232
* @param [in] ... Format string and arguments.
4333
*/
4434
#define PBDRV_IOPORT_DEBUG_UART_PT_PRINTF(pt, ...) \
45-
PT_SPAWN(pt, &printf_thread, pbdrv_ioport_debug_uart_printf_thread(&printf_thread, __VA_ARGS__))
35+
pbdrv_ioport_debug_uart_printf_buffer(__VA_ARGS__); \
36+
if (debug_uart) { \
37+
PT_SPAWN(pt, &debug_printf_thread, pbdrv_uart_write(&debug_printf_thread, debug_uart, (uint8_t *)debug_buffer, strlen(debug_buffer), 250, &debug_err)); \
38+
} \
4639

47-
#else // PBDRV_CONFIG_IOPORT_DEBUG_UART
40+
pbio_error_t pbdrv_ioport_debug_uart_init(pbdrv_uart_poll_callback_t callback);
4841

49-
static inline pbio_error_t pbdrv_ioport_debug_uart_printf(const char *format, ...) {
50-
return PBIO_ERROR_NOT_SUPPORTED;
51-
}
42+
#else // PBDRV_CONFIG_IOPORT_DEBUG_UART
5243

5344
#define PBDRV_IOPORT_DEBUG_UART_PT_PRINTF(pt, ...)
5445

0 commit comments

Comments
 (0)