Skip to content

Commit 699e9c0

Browse files
committed
This commit contains 2 changes:
Added support for different size of TX and RX buffer sizes. Added support for buffer sizes bigger than 256 bytes. Added support for different size of TX and RX buffer sizes. The default values remain the same. If you want to have different values define SERIAL_TX_BUFFER_SIZE and SERIAL_RX_BUFFER_SIZE on the command line Added support for buffer sizes bigger than 256 bytes. The type of the indexes is decided upon the size of the buffers. So there is no increase in program/data size when the buffers are smaller than 257
1 parent b59826c commit 699e9c0

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Diff for: cores/arduino/HardwareSerial.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void HardwareSerial::_tx_udr_empty_irq(void)
8383
// If interrupts are enabled, there must be more data in the output
8484
// buffer. Send the next byte
8585
unsigned char c = _tx_buffer[_tx_buffer_tail];
86-
_tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
86+
_tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_TX_BUFFER_SIZE;
8787

8888
*_udr = c;
8989

@@ -152,7 +152,7 @@ void HardwareSerial::end()
152152

153153
int HardwareSerial::available(void)
154154
{
155-
return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_BUFFER_SIZE;
155+
return (unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_RX_BUFFER_SIZE;
156156
}
157157

158158
int HardwareSerial::peek(void)
@@ -171,7 +171,7 @@ int HardwareSerial::read(void)
171171
return -1;
172172
} else {
173173
unsigned char c = _rx_buffer[_rx_buffer_tail];
174-
_rx_buffer_tail = (uint8_t)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
174+
_rx_buffer_tail = (BUFPOINTER)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
175175
return c;
176176
}
177177
}
@@ -207,7 +207,7 @@ size_t HardwareSerial::write(uint8_t c)
207207
sbi(*_ucsra, TXC0);
208208
return 1;
209209
}
210-
uint8_t i = (_tx_buffer_head + 1) % SERIAL_BUFFER_SIZE;
210+
BUFPOINTER i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE;
211211

212212
// If the output buffer is full, there's nothing for it other than to
213213
// wait for the interrupt handler to empty it a bit

Diff for: cores/arduino/HardwareSerial.h

+17-8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@
3232
// using a ring buffer (I think), in which head is the index of the location
3333
// to which to write the next incoming character and tail is the index of the
3434
// location from which to read.
35+
#if !(defined(SERIAL_TX_BUFFER_SIZE)&&defined(SERIAL_RX_BUFFER_SIZE))
3536
#if (RAMEND < 1000)
36-
#define SERIAL_BUFFER_SIZE 16
37+
#define SERIAL_TX_BUFFER_SIZE 16
38+
#define SERIAL_RX_BUFFER_SIZE 16
3739
#else
38-
#define SERIAL_BUFFER_SIZE 64
40+
#define SERIAL_TX_BUFFER_SIZE 64
41+
#define SERIAL_RX_BUFFER_SIZE 64
42+
#endif
43+
#endif
44+
#if (SERIAL_TX_BUFFER_SIZE>255) || (SERIAL_RX_BUFFER_SIZE>255)
45+
#define BUFPOINTER uint16_t
46+
#else
47+
#define BUFPOINTER uint8_t
3948
#endif
4049

4150
// Define config for Serial.begin(baud, config);
@@ -76,16 +85,16 @@ class HardwareSerial : public Stream
7685
// Has any byte been written to the UART since begin()
7786
bool _written;
7887

79-
volatile uint8_t _rx_buffer_head;
80-
volatile uint8_t _rx_buffer_tail;
81-
volatile uint8_t _tx_buffer_head;
82-
volatile uint8_t _tx_buffer_tail;
88+
volatile BUFPOINTER _rx_buffer_head;
89+
volatile BUFPOINTER _rx_buffer_tail;
90+
volatile BUFPOINTER _tx_buffer_head;
91+
volatile BUFPOINTER _tx_buffer_tail;
8392

8493
// Don't put any members after these buffers, since only the first
8594
// 32 bytes of this struct can be accessed quickly using the ldd
8695
// instruction.
87-
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
88-
unsigned char _tx_buffer[SERIAL_BUFFER_SIZE];
96+
unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];
97+
unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];
8998

9099
public:
91100
inline HardwareSerial(

0 commit comments

Comments
 (0)