From 7d78986f133f802b4e40090b41d3c4a134b5eb37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Fri, 12 Jan 2018 11:31:58 +0100 Subject: [PATCH] uart: make receiving bytes more reliable --- lib/uart.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/uart.c b/lib/uart.c index f888711..5049510 100644 --- a/lib/uart.c +++ b/lib/uart.c @@ -43,10 +43,10 @@ #define UART_BAUDRATE 921600 /**< serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */ /* input and output ring buffer, indexes, and available memory */ -static uint8_t rx_buffer[UART_BUFFER] = {0}; /**< ring buffer for received data */ +static volatile uint8_t rx_buffer[UART_BUFFER] = {0}; /**< ring buffer for received data */ static volatile uint8_t rx_i = 0; /**< current position of read received data */ static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */ -static uint8_t tx_buffer[UART_BUFFER] = {0}; /**< ring buffer for data to transmit */ +static volatile uint8_t tx_buffer[UART_BUFFER] = {0}; /**< ring buffer for data to transmit */ static volatile uint8_t tx_i = 0; /**< current position of transmitted data */ static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */ @@ -101,8 +101,8 @@ char uart_getchar(void) while (!rx_used) { // idle until data is available __WFI(); // sleep until interrupt } - char to_return = rx_buffer[rx_i]; // get the next available character usart_disable_rx_interrupt(USART(UART_ID)); // disable receive interrupt to prevent index corruption + volatile char to_return = rx_buffer[rx_i]; // get the next available character rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer rx_used--; // update used buffer uart_received = (rx_used!=0); // update available data