fix index corruption race condition

This commit is contained in:
King Kévin 2016-03-28 17:27:02 +02:00
parent 456ca4def2
commit 5430bdc0c0
1 changed files with 3 additions and 0 deletions

View File

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