diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index a0a6642..6eb0bf9 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -121,18 +121,16 @@ int HardwareSerial::availableForWrite(void) void HardwareSerial::rx_complete_irq(void) { // which UART to use - UART_TypeDef* uart = UART_0; - if (uartNum == 1) - uart = UART_1; - - // find next index in buffer with upper limit - uint8_t i = (uint8_t)(_rx_buffer_head + 1)%SERIAL_RX_BUFFER_SIZE; + UART_TypeDef* uart = (uartNum == 1) ? UART_1 : UART_0; + rx_buffer_index_t i; unsigned char c; // while there is something to receive, put the data into the buffer // and edit the buffer index while(!UART_IS_RX_FIFO_EMPTY(uart)) { c = UART_READ_BYTE(uart); + // find next index in buffer with upper limit + i = (unsigned int)(_rx_buffer_head + 1)%SERIAL_RX_BUFFER_SIZE; if (i != _rx_buffer_tail) { // write if there is space in the buffer diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 792bf13..5791740 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -41,7 +41,15 @@ // often work, but occasionally a race condition can occur that makes // Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405 +#if !defined(SERIAL_RX_BUFFER_SIZE) #define SERIAL_RX_BUFFER_SIZE 64 +#endif + +#if (SERIAL_RX_BUFFER_SIZE>256) +typedef uint16_t rx_buffer_index_t; +#else +typedef uint8_t rx_buffer_index_t; +#endif // Define config for Serial.begin(baud, config); // parity: 7...4 bits = 0 (no), 2 (even), 3 (odd) @@ -68,8 +76,8 @@ class HardwareSerial : public Stream { protected: - volatile uint8_t _rx_buffer_head; - volatile uint8_t _rx_buffer_tail; + volatile rx_buffer_index_t _rx_buffer_head; + volatile rx_buffer_index_t _rx_buffer_tail; unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]; private: