From fd5f4791c4e45553616c634435c9ce2e6897f706 Mon Sep 17 00:00:00 2001 From: jantje Date: Sun, 23 Mar 2014 23:12:00 +0100 Subject: [PATCH 1/4] 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 --- .../avr/cores/arduino/HardwareSerial.cpp | 8 +++--- .../avr/cores/arduino/HardwareSerial.h | 25 +++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp b/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp index a270ace18..a634b2892 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp @@ -83,7 +83,7 @@ void HardwareSerial::_tx_udr_empty_irq(void) // If interrupts are enabled, there must be more data in the output // buffer. Send the next byte unsigned char c = _tx_buffer[_tx_buffer_tail]; - _tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_BUFFER_SIZE; + _tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_TX_BUFFER_SIZE; *_udr = c; @@ -152,7 +152,7 @@ void HardwareSerial::end() int HardwareSerial::available(void) { - return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_BUFFER_SIZE; + return (unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_RX_BUFFER_SIZE; } int HardwareSerial::peek(void) @@ -171,7 +171,7 @@ int HardwareSerial::read(void) return -1; } else { unsigned char c = _rx_buffer[_rx_buffer_tail]; - _rx_buffer_tail = (uint8_t)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE; + _rx_buffer_tail = (BUFPOINTER)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE; return c; } } @@ -207,7 +207,7 @@ size_t HardwareSerial::write(uint8_t c) sbi(*_ucsra, TXC0); return 1; } - uint8_t i = (_tx_buffer_head + 1) % SERIAL_BUFFER_SIZE; + BUFPOINTER i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE; // If the output buffer is full, there's nothing for it other than to // wait for the interrupt handler to empty it a bit diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial.h b/hardware/arduino/avr/cores/arduino/HardwareSerial.h index 226bf57a7..e21e55ba7 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial.h +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial.h @@ -32,10 +32,19 @@ // using a ring buffer (I think), in which head is the index of the location // to which to write the next incoming character and tail is the index of the // location from which to read. +#if !(defined(SERIAL_TX_BUFFER_SIZE)&&defined(SERIAL_RX_BUFFER_SIZE)) #if (RAMEND < 1000) - #define SERIAL_BUFFER_SIZE 16 +#define SERIAL_TX_BUFFER_SIZE 16 +#define SERIAL_RX_BUFFER_SIZE 16 #else - #define SERIAL_BUFFER_SIZE 64 +#define SERIAL_TX_BUFFER_SIZE 64 +#define SERIAL_RX_BUFFER_SIZE 64 +#endif +#endif +#if (SERIAL_TX_BUFFER_SIZE>255) || (SERIAL_RX_BUFFER_SIZE>255) +#define BUFPOINTER uint16_t +#else +#define BUFPOINTER uint8_t #endif // Define config for Serial.begin(baud, config); @@ -76,16 +85,16 @@ class HardwareSerial : public Stream // Has any byte been written to the UART since begin() bool _written; - volatile uint8_t _rx_buffer_head; - volatile uint8_t _rx_buffer_tail; - volatile uint8_t _tx_buffer_head; - volatile uint8_t _tx_buffer_tail; + volatile BUFPOINTER _rx_buffer_head; + volatile BUFPOINTER _rx_buffer_tail; + volatile BUFPOINTER _tx_buffer_head; + volatile BUFPOINTER _tx_buffer_tail; // Don't put any members after these buffers, since only the first // 32 bytes of this struct can be accessed quickly using the ldd // instruction. - unsigned char _rx_buffer[SERIAL_BUFFER_SIZE]; - unsigned char _tx_buffer[SERIAL_BUFFER_SIZE]; + unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]; + unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]; public: inline HardwareSerial( From a51e1c40256618fe8101604013b6e28619867c09 Mon Sep 17 00:00:00 2001 From: jantje Date: Mon, 24 Mar 2014 21:40:12 +0100 Subject: [PATCH 2/4] 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 --- .../avr/cores/arduino/HardwareSerial.cpp | 4 ++-- .../avr/cores/arduino/HardwareSerial.h | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp b/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp index a634b2892..b9dce07cd 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp @@ -171,7 +171,7 @@ int HardwareSerial::read(void) return -1; } else { unsigned char c = _rx_buffer[_rx_buffer_tail]; - _rx_buffer_tail = (BUFPOINTER)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE; + _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE; return c; } } @@ -207,7 +207,7 @@ size_t HardwareSerial::write(uint8_t c) sbi(*_ucsra, TXC0); return 1; } - BUFPOINTER i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE; + tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE; // If the output buffer is full, there's nothing for it other than to // wait for the interrupt handler to empty it a bit diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial.h b/hardware/arduino/avr/cores/arduino/HardwareSerial.h index e21e55ba7..b96e5d058 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial.h +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial.h @@ -32,7 +32,7 @@ // using a ring buffer (I think), in which head is the index of the location // to which to write the next incoming character and tail is the index of the // location from which to read. -#if !(defined(SERIAL_TX_BUFFER_SIZE)&&defined(SERIAL_RX_BUFFER_SIZE)) +#if !(defined(SERIAL_TX_BUFFER_SIZE) && defined(SERIAL_RX_BUFFER_SIZE)) #if (RAMEND < 1000) #define SERIAL_TX_BUFFER_SIZE 16 #define SERIAL_RX_BUFFER_SIZE 16 @@ -41,10 +41,15 @@ #define SERIAL_RX_BUFFER_SIZE 64 #endif #endif -#if (SERIAL_TX_BUFFER_SIZE>255) || (SERIAL_RX_BUFFER_SIZE>255) -#define BUFPOINTER uint16_t +#if (SERIAL_TX_BUFFER_SIZE>256) +typedef uint16_t tx_buffer_index_t; #else -#define BUFPOINTER uint8_t +typedef uint8_t tx_buffer_index_t; +#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); @@ -85,10 +90,10 @@ class HardwareSerial : public Stream // Has any byte been written to the UART since begin() bool _written; - volatile BUFPOINTER _rx_buffer_head; - volatile BUFPOINTER _rx_buffer_tail; - volatile BUFPOINTER _tx_buffer_head; - volatile BUFPOINTER _tx_buffer_tail; + volatile rx_buffer_index_t _rx_buffer_head; + volatile rx_buffer_index_t _rx_buffer_tail; + volatile tx_buffer_index_t _tx_buffer_head; + volatile tx_buffer_index_t _tx_buffer_tail; // Don't put any members after these buffers, since only the first // 32 bytes of this struct can be accessed quickly using the ldd From 77187ad4e452dd6fb0b20a288e8ceaf47cf4bba2 Mon Sep 17 00:00:00 2001 From: jantje Date: Tue, 1 Apr 2014 16:14:16 +0200 Subject: [PATCH 3/4] I forgot a file --- hardware/arduino/avr/cores/arduino/HardwareSerial_private.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h b/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h index fa20e553b..9e98da6a1 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h @@ -99,7 +99,7 @@ void HardwareSerial::_rx_complete_irq(void) // No Parity error, read byte and store it in the buffer if there is // room unsigned char c = *_udr; - uint8_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_BUFFER_SIZE; + uint8_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE; // if we should be storing the received character into the location // just before the tail (meaning that the head would advance to the From 1ad74ce09b3e7ee364d7f58b42a1b94485f84ea3 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 1 Apr 2014 17:18:02 +0200 Subject: [PATCH 4/4] Use correct type for index calculation in HardwareSerial --- hardware/arduino/avr/cores/arduino/HardwareSerial_private.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h b/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h index 9e98da6a1..ea41028b1 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h @@ -99,7 +99,7 @@ void HardwareSerial::_rx_complete_irq(void) // No Parity error, read byte and store it in the buffer if there is // room unsigned char c = *_udr; - uint8_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE; + rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE; // if we should be storing the received character into the location // just before the tail (meaning that the head would advance to the