From a3e75061cac23b324ab8cc6d80d8ab26dc770616 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 15 Jan 2026 15:41:28 -0800 Subject: [PATCH] console: disable interrupts around msg count and char buffer Ensure consistency between console_service() in task context and CONSOLE_USART_ISR() by disabling interrupts when the task is accessing counters/indices maniuplated by both. --- src/console.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/console.c b/src/console.c index ae125b9..7c7987f 100644 --- a/src/console.c +++ b/src/console.c @@ -84,6 +84,7 @@ const char *menu_str[] = {"\r\n", }; #define MENU_LEN (sizeof(menu_str)/sizeof(*menu_str)) +// number of UART_MSG_TERMINATOR in UARTQUEUE static uint8_t _msgCount; static uint8_t _fpgaEnable; @@ -719,11 +720,23 @@ void CONSOLE_USART_ISR(void) { return; } +// call only from ISR void console_pend_msg(void) { _msgCount++; return; } +static bool console_dequeue_msg(void) { + bool ret = false; + INTERRUPTS_DISABLE(); + if(_msgCount) { + _msgCount--; + ret = true; // caller must console_shift_msg() + } + INTERRUPTS_ENABLE(); + return ret; +} + /* * int console_service(void); * Service the console system. @@ -732,9 +745,8 @@ void console_pend_msg(void) { int console_service(void) { uint8_t msg[CONSOLE_MAX_MESSAGE_LENGTH]; int len; - if (_msgCount) { + if (console_dequeue_msg()) { len = console_shift_msg(msg); - _msgCount--; if (len) { return console_handle_msg((char *)msg, len); } @@ -767,8 +779,11 @@ static int console_shift_all(uint8_t *pData) { * Returns the number of bytes shifted out. */ static int console_shift_msg(uint8_t *pData) { - //UARTQUEUE_ShiftOut(pData, CONSOLE_MAX_MESSAGE_LENGTH); - return UARTQUEUE_ShiftUntil(pData, UART_MSG_TERMINATOR, CONSOLE_MAX_MESSAGE_LENGTH); + int ret; + INTERRUPTS_DISABLE(); + ret = UARTQUEUE_ShiftUntil(pData, UART_MSG_TERMINATOR, CONSOLE_MAX_MESSAGE_LENGTH); + INTERRUPTS_ENABLE(); + return ret; } static int xatoi(char c) {