Skip to content

Commit 9f46367

Browse files
committed
pbdrv/uart_debug_first_port: Log on panic.
It's helpful when you crash to see what happens right before you crash. For example, if you add a log message "about to dereference sketchy pointer" and then you get a segfault, you'd love to know whether that message occurred. This change makes it so that at least on EV3, we will. This also adds "pb_log" as an alias for pbdrv_uart_debug_printf. Usually, platforms make it easy to log debug messages, and we should be no different. C.f. `ESP_LOGI`, Zephyr's `LOG_ERR`, glog's `LOG(WARNING)` etc. Tested by adding the following code to pbdrv_init. ``` pb_log("test\n"); lwrb_t* rb = (lwrb_t*)0x1281441490; pb_log("%p\n", lwrb_get_free(rb)); ``` Before this, the "test" would not appear before the panic. After, it does. If a panic occurs while some messages are pending in the uart send state, some bytes may be duplicated on the log.
1 parent 350db6e commit 9f46367

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

lib/pbio/drv/uart/uart_debug_first_port.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ static lwrb_t ring_buffer;
2222

2323
static pbdrv_uart_dev_t *debug_uart = NULL;
2424

25+
int pbdrv_uart_debug_next_char(void) {
26+
if (!lwrb_is_ready(&ring_buffer)) {
27+
return -1;
28+
}
29+
uint8_t c;
30+
if (lwrb_read(&ring_buffer, &c, 1) == 1) {
31+
return c;
32+
} else {
33+
return -1;
34+
}
35+
}
36+
2537
/**
2638
* Formats and stores a string in the UART debug ring buffer.
2739
*

lib/pbio/drv/uart/uart_debug_first_port.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ bool pbdrv_uart_debug_is_done(void);
1919

2020
void pbdrv_uart_debug_init(void);
2121

22+
// Returns the next character that should be sent to the UART, or -1 if there
23+
// are not pending log messages. Used only by the panic handler to flush unsent
24+
// data before reboot.
25+
int pbdrv_uart_debug_next_char(void);
26+
2227
#else // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
2328

2429
#define pbdrv_uart_debug_printf(...)
@@ -28,6 +33,13 @@ void pbdrv_uart_debug_init(void);
2833

2934
#define pbdrv_uart_debug_init()
3035

36+
static inline int pbdrv_uart_debug_next_char(void) {
37+
return -1;
38+
}
39+
3140
#endif // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
3241

42+
// Convenient shorthand for pbdrv_uart_debug_printf.
43+
#define pbdrv_dbg(...) pbdrv_uart_debug_printf(__VA_ARGS__)
44+
3345
#endif // _INTERNAL_PBDRV_UART_DEBUG_FIRST_PORT_H_

lib/pbio/platform/ev3/platform.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include "../../drv/pwm/pwm_ev3.h"
8282
#include "../../drv/reset/reset_ev3.h"
8383
#include "../../drv/uart/uart_ev3.h"
84+
#include "../../drv/uart/uart_debug_first_port.h"
8485

8586
enum {
8687
LED_DEV_0_STATUS,
@@ -443,6 +444,12 @@ void ev3_panic_handler(int except_type, ev3_panic_ctx *except_data) {
443444
UARTConfigSetExpClk(SOC_UART_1_REGS, SOC_UART_1_MODULE_FREQ, 115200, UART_WORDL_8BITS, UART_OVER_SAMP_RATE_13);
444445
UARTFIFOEnable(SOC_UART_1_REGS);
445446

447+
// Drain whatever is still left in the UART debug FIFO.
448+
int c;
449+
while ((c = pbdrv_uart_debug_next_char()) != -1) {
450+
UARTCharPut(SOC_UART_1_REGS, (uint8_t)c);
451+
}
452+
446453
panic_puts("********************************************************************************\r\n");
447454
panic_puts("* Pybricks on EV3 Panic *\r\n");
448455
panic_puts("********************************************************************************\r\n");

0 commit comments

Comments
 (0)