drop old data if receive buffer full

This commit is contained in:
King Kévin 2016-09-04 19:08:57 +02:00
parent 868283a8e7
commit 833d40fe10
1 changed files with 10 additions and 10 deletions

View File

@ -101,7 +101,7 @@ char usart_getchar(void)
}
char to_return = rx_buffer[rx_i]; // get the next available character
usart_disable_rx_interrupt(USART(USART_ID)); // disable receive interrupt to prevent index corruption
rx_i = (rx_i+1)%sizeof(rx_buffer); // update used buffer
rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
rx_used--; // update used buffer
usart_received = (rx_used!=0); // update available data
usart_enable_rx_interrupt(USART(USART_ID)); // enable receive interrupt
@ -110,12 +110,12 @@ char usart_getchar(void)
void usart_putchar_nonblocking(char c)
{
while (tx_used>=sizeof(tx_buffer)) { // idle until buffer has some space
while (tx_used>=LENGTH(tx_buffer)) { // idle until buffer has some space
usart_enable_tx_interrupt(USART(USART_ID)); // enable transmit interrupt
__WFI(); // sleep until something happened
}
usart_disable_tx_interrupt(USART(USART_ID)); // disable transmit interrupt to prevent index corruption
tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // put character in buffer
tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
tx_used++; // update used buffer
usart_enable_tx_interrupt(USART(USART_ID)); // enable transmit interrupt
}
@ -128,18 +128,18 @@ void USART_ISR(USART_ID)(void)
usart_disable_tx_interrupt(USART(USART_ID)); // disable transmit interrupt
} else {
usart_send(USART(USART_ID),tx_buffer[tx_i]); // put data in transmit register
tx_i = (tx_i+1)%sizeof(rx_buffer); // update location on buffer
tx_i = (tx_i+1)%LENGTH(rx_buffer); // update location on buffer
tx_used--; // update used size
}
}
if (usart_get_interrupt_source(USART(USART_ID), USART_SR_RXNE)) { // data has been received
// only save data if there is space in the buffer
if (rx_used>=sizeof(rx_buffer)) {
usart_recv(USART(USART_ID)); // read to clear interrupt
} else {
rx_buffer[(rx_i+rx_used)%sizeof(rx_buffer)] = usart_recv(USART(USART_ID)); // put character in buffer
rx_used++; // update used buffer
usart_received = (rx_used!=0); // update available data
while (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
rx_i = (rx_i+1)%LENGTH(rx_buffer); // drop oldest data
rx_used--; // update used buffer information
}
rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usart_recv(USART(USART_ID)); // put character in buffer
rx_used++; // update used buffer
usart_received = (rx_used!=0); // update available data
}
}