Make rx tail/head atomic

2.0.x
AnHardt 9 years ago
parent 9f8e6dce20
commit dc0f41868e

@ -33,7 +33,7 @@
#endif #endif
FORCE_INLINE void store_char(unsigned char c) { FORCE_INLINE void store_char(unsigned char c) {
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE; uint8_t i = (uint8_t)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
// if we should be storing the received character into the location // if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the // just before the tail (meaning that the head would advance to the
@ -116,7 +116,7 @@ int MarlinSerial::read(void) {
} }
else { else {
unsigned char c = rx_buffer.buffer[rx_buffer.tail]; unsigned char c = rx_buffer.buffer[rx_buffer.tail];
rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE; rx_buffer.tail = (uint8_t)(rx_buffer.tail + 1) % RX_BUFFER_SIZE;
return c; return c;
} }
} }

@ -69,13 +69,14 @@
// using a ring buffer (I think), in which rx_buffer_head is the index of the // using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail // location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read. // is the index of the location from which to read.
// 256 is the max limit due to uint8_t head and tail. Thats needed to make them atomic.
#define RX_BUFFER_SIZE 128 #define RX_BUFFER_SIZE 128
struct ring_buffer { struct ring_buffer {
unsigned char buffer[RX_BUFFER_SIZE]; unsigned char buffer[RX_BUFFER_SIZE];
int head; volatile uint8_t head;
int tail; volatile uint8_t tail;
}; };
#if UART_PRESENT(SERIAL_PORT) #if UART_PRESENT(SERIAL_PORT)
@ -92,8 +93,8 @@ class MarlinSerial { //: public Stream
int read(void); int read(void);
void flush(void); void flush(void);
FORCE_INLINE int available(void) { FORCE_INLINE uint8_t available(void) {
return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE; return (uint8_t)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
} }
FORCE_INLINE void write(uint8_t c) { FORCE_INLINE void write(uint8_t c) {
@ -105,7 +106,7 @@ class MarlinSerial { //: public Stream
FORCE_INLINE void checkRx(void) { FORCE_INLINE void checkRx(void) {
if (TEST(M_UCSRxA, M_RXCx)) { if (TEST(M_UCSRxA, M_RXCx)) {
unsigned char c = M_UDRx; unsigned char c = M_UDRx;
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE; uint8_t i = (uint8_t)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
// if we should be storing the received character into the location // if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the // just before the tail (meaning that the head would advance to the

Loading…
Cancel
Save