размер буфера на прием задается теперь, только если ранее не был задан

This commit is contained in:
KLASSENTS 2025-09-09 11:34:22 +07:00
parent 00e8d1b1af
commit da0fea8a33
2 changed files with 14 additions and 8 deletions

View File

@ -121,18 +121,16 @@ int HardwareSerial::availableForWrite(void)
void HardwareSerial::rx_complete_irq(void) void HardwareSerial::rx_complete_irq(void)
{ {
// which UART to use // which UART to use
UART_TypeDef* uart = UART_0; UART_TypeDef* uart = (uartNum == 1) ? UART_1 : UART_0;
if (uartNum == 1) rx_buffer_index_t i;
uart = UART_1;
// find next index in buffer with upper limit
uint8_t i = (uint8_t)(_rx_buffer_head + 1)%SERIAL_RX_BUFFER_SIZE;
unsigned char c; unsigned char c;
// while there is something to receive, put the data into the buffer // while there is something to receive, put the data into the buffer
// and edit the buffer index // and edit the buffer index
while(!UART_IS_RX_FIFO_EMPTY(uart)) while(!UART_IS_RX_FIFO_EMPTY(uart))
{ {
c = UART_READ_BYTE(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) if (i != _rx_buffer_tail)
{ {
// write if there is space in the buffer // write if there is space in the buffer

View File

@ -41,7 +41,15 @@
// often work, but occasionally a race condition can occur that makes // often work, but occasionally a race condition can occur that makes
// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405 // Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
#if !defined(SERIAL_RX_BUFFER_SIZE)
#define SERIAL_RX_BUFFER_SIZE 64 #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); // Define config for Serial.begin(baud, config);
// parity: 7...4 bits = 0 (no), 2 (even), 3 (odd) // parity: 7...4 bits = 0 (no), 2 (even), 3 (odd)
@ -68,8 +76,8 @@
class HardwareSerial : public Stream class HardwareSerial : public Stream
{ {
protected: protected:
volatile uint8_t _rx_buffer_head; volatile rx_buffer_index_t _rx_buffer_head;
volatile uint8_t _rx_buffer_tail; volatile rx_buffer_index_t _rx_buffer_tail;
unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]; unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];
private: private: