From 5430bdc0c024bd4ef03f147ca4d7ee64e485a7b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 28 Mar 2016 17:27:02 +0200 Subject: [PATCH] fix index corruption race condition --- lib/usart.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/usart.c b/lib/usart.c index 93df25d..ce3adae 100644 --- a/lib/usart.c +++ b/lib/usart.c @@ -105,9 +105,11 @@ char usart_getchar(void) __WFI(); // sleep until interrupt; } char to_return = rx_buffer[rx_i]; // get the next available character + usart_disable_rx_interrupt(USART); // disable receive interrupt to prevent index corruption rx_i = (rx_i+1)%sizeof(rx_buffer); // update used buffer rx_used--; // update used buffer usart_received = rx_used; // update available data + usart_enable_rx_interrupt(USART); // enable receive interrupt return to_return; } @@ -117,6 +119,7 @@ void usart_putchar_nonblocking(char c) usart_enable_tx_interrupt(USART); // enable transmit interrupt __WFI(); // sleep until something happened } + usart_disable_tx_interrupt(USART); // disable transmit interrupt to prevent index corruption tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // put character in buffer tx_used++; // update used buffer usart_enable_tx_interrupt(USART); // enable transmit interrupt