Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/pbio/drv/uart/uart_debug_first_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ static lwrb_t ring_buffer;

static pbdrv_uart_dev_t *debug_uart = NULL;

int pbdrv_uart_debug_next_char(void) {
if (!lwrb_is_ready(&ring_buffer)) {
return -1;
}
uint8_t c;
if (lwrb_read(&ring_buffer, &c, 1) == 1) {
return c;
} else {
return -1;
}
}

/**
* Formats and stores a string in the UART debug ring buffer.
*
Expand Down
12 changes: 12 additions & 0 deletions lib/pbio/drv/uart/uart_debug_first_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ bool pbdrv_uart_debug_is_done(void);

void pbdrv_uart_debug_init(void);

// Returns the next character that should be sent to the UART, or -1 if there
// are not pending log messages. Used only by the panic handler to flush unsent
// data before reboot.
int pbdrv_uart_debug_next_char(void);

#else // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT

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

#define pbdrv_uart_debug_init()

static inline int pbdrv_uart_debug_next_char(void) {
return -1;
}

#endif // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT

// Convenient shorthand for pbdrv_uart_debug_printf.
#define pbdrv_dbg(...) pbdrv_uart_debug_printf(__VA_ARGS__)

#endif // _INTERNAL_PBDRV_UART_DEBUG_FIRST_PORT_H_
7 changes: 7 additions & 0 deletions lib/pbio/platform/ev3/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#include "../../drv/pwm/pwm_ev3.h"
#include "../../drv/reset/reset_ev3.h"
#include "../../drv/uart/uart_ev3.h"
#include "../../drv/uart/uart_debug_first_port.h"

enum {
LED_DEV_0_STATUS,
Expand Down Expand Up @@ -443,6 +444,12 @@ void ev3_panic_handler(int except_type, ev3_panic_ctx *except_data) {
UARTConfigSetExpClk(SOC_UART_1_REGS, SOC_UART_1_MODULE_FREQ, 115200, UART_WORDL_8BITS, UART_OVER_SAMP_RATE_13);
UARTFIFOEnable(SOC_UART_1_REGS);

// Drain whatever is still left in the UART debug FIFO.
int c;
while ((c = pbdrv_uart_debug_next_char()) != -1) {
UARTCharPut(SOC_UART_1_REGS, (uint8_t)c);
}

panic_puts("********************************************************************************\r\n");
panic_puts("* Pybricks on EV3 Panic *\r\n");
panic_puts("********************************************************************************\r\n");
Expand Down