uart: make receiving bytes more reliable

This commit is contained in:
King Kévin 2018-01-12 11:31:58 +01:00
parent 5f5f7c9558
commit 7d78986f13
1 changed files with 3 additions and 3 deletions

View File

@ -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