Skip to content

Commit f87e115

Browse files
committed
Added support for different size of TX and RX buffer sizes.
Added support for buffer sizes bigger than 256 bytes. Added possibility to overrule the default size. Added support for different size of TX and RX buffer sizes. The default values remain the same. You can however specify a different value for TX and RX buffer Added possibility to overrule the default size. 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. Because of the possibility to change the size of the buffer sizes longer than 256 must be supported. 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 699e9c0 commit f87e115

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Diff for: cores/arduino/HardwareSerial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -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 = (BUFPOINTER)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
174+
_rx_buffer_tail = (rx_buffer_index_t)(_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-
BUFPOINTER i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE;
210+
tx_buffer_index_t 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

+13-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
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))
35+
#if !(defined(SERIAL_TX_BUFFER_SIZE) && defined(SERIAL_RX_BUFFER_SIZE))
3636
#if (RAMEND < 1000)
3737
#define SERIAL_TX_BUFFER_SIZE 16
3838
#define SERIAL_RX_BUFFER_SIZE 16
@@ -41,10 +41,15 @@
4141
#define SERIAL_RX_BUFFER_SIZE 64
4242
#endif
4343
#endif
44-
#if (SERIAL_TX_BUFFER_SIZE>255) || (SERIAL_RX_BUFFER_SIZE>255)
45-
#define BUFPOINTER uint16_t
44+
#if (SERIAL_TX_BUFFER_SIZE>256)
45+
typedef uint16_t tx_buffer_index_t;
4646
#else
47-
#define BUFPOINTER uint8_t
47+
typedef uint8_t tx_buffer_index_t;
48+
#endif
49+
#if (SERIAL_RX_BUFFER_SIZE>256)
50+
typedef uint16_t rx_buffer_index_t;
51+
#else
52+
typedef uint8_t rx_buffer_index_t;
4853
#endif
4954

5055
// Define config for Serial.begin(baud, config);
@@ -85,10 +90,10 @@ class HardwareSerial : public Stream
8590
// Has any byte been written to the UART since begin()
8691
bool _written;
8792

88-
volatile BUFPOINTER _rx_buffer_head;
89-
volatile BUFPOINTER _rx_buffer_tail;
90-
volatile BUFPOINTER _tx_buffer_head;
91-
volatile BUFPOINTER _tx_buffer_tail;
93+
volatile rx_buffer_index_t _rx_buffer_head;
94+
volatile rx_buffer_index_t _rx_buffer_tail;
95+
volatile tx_buffer_index_t _tx_buffer_head;
96+
volatile tx_buffer_index_t _tx_buffer_tail;
9297

9398
// Don't put any members after these buffers, since only the first
9499
// 32 bytes of this struct can be accessed quickly using the ldd

0 commit comments

Comments
 (0)