From 5c11f4f1add85999ba89873926792fec3abc7be9 Mon Sep 17 00:00:00 2001 From: Harry Date: Sat, 8 Oct 2011 21:25:11 -0300 Subject: [PATCH 1/3] If the serial buffer is full when write is called, HardwareSerial now decides whether to wait for the ISR to open up some space, or quits immediately to avoid hanging the processor. (If this situation was caused from within another ISR). Added HardwareSerial::waitForBufferSpace(uint8_t v) which takes TRUE or FALSE so the user can make that choice. --- hardware/arduino/cores/arduino/HardwareSerial.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/hardware/arduino/cores/arduino/HardwareSerial.cpp b/hardware/arduino/cores/arduino/HardwareSerial.cpp index b5992ad6fe3..cd765e92a97 100644 --- a/hardware/arduino/cores/arduino/HardwareSerial.cpp +++ b/hardware/arduino/cores/arduino/HardwareSerial.cpp @@ -289,6 +289,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, _rxcie = rxcie; _udrie = udrie; _u2x = u2x; + _serial_buffer_wait = 0x1; //by default, wait for space. } // Public Methods ////////////////////////////////////////////////////////////// @@ -384,11 +385,14 @@ size_t HardwareSerial::write(uint8_t c) { int i = (_tx_buffer->head + 1) % SERIAL_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 - // ???: return 0 here instead? - while (i == _tx_buffer->tail) + // If the output buffer is full, there's two options. Either quit or wait for interrupt handler to make space. User decides. + if(_serial_buffer_wait == 1) { + while (i == _tx_buffer->tail) ; + } else { + if(i == _tx_buffer->tail) { return 0; } + } + _tx_buffer->buffer[_tx_buffer->head] = c; _tx_buffer->head = i; @@ -398,6 +402,9 @@ size_t HardwareSerial::write(uint8_t c) return 1; } +void HardwareSerial::waitForBufferSpace(uint8_t v) { + _serial_buffer_wait = v; +} // Preinstantiate Objects ////////////////////////////////////////////////////// #if defined(UBRRH) && defined(UBRRL) From f788720f69effe0bb95fd0314026614f241019f9 Mon Sep 17 00:00:00 2001 From: Harry Date: Sat, 8 Oct 2011 21:26:19 -0300 Subject: [PATCH 2/3] Added the waitForBufferSpace(uint8_t) function and its associated variable to the .h file. --- hardware/arduino/cores/arduino/HardwareSerial.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hardware/arduino/cores/arduino/HardwareSerial.h b/hardware/arduino/cores/arduino/HardwareSerial.h index 176abe1e20b..6a3728fc4e2 100644 --- a/hardware/arduino/cores/arduino/HardwareSerial.h +++ b/hardware/arduino/cores/arduino/HardwareSerial.h @@ -43,6 +43,7 @@ class HardwareSerial : public Stream uint8_t _rxcie; uint8_t _udrie; uint8_t _u2x; + uint8_t _serial_buffer_wait; public: HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, @@ -57,6 +58,7 @@ class HardwareSerial : public Stream virtual void flush(void); virtual size_t write(uint8_t); using Print::write; // pull in write(str) and write(buf, size) from Print + virtual void waitForBufferSpace(uint8_t); }; #if defined(UBRRH) || defined(UBRR0H) From 3eac689b3b73bcc2d0bc39a2a761c8ea1e41906d Mon Sep 17 00:00:00 2001 From: Harry Date: Wed, 12 Oct 2011 12:06:43 -0300 Subject: [PATCH 3/3] Added the keyword for the Serial.waitForBufferSpace function. --- build/shared/lib/keywords.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/build/shared/lib/keywords.txt b/build/shared/lib/keywords.txt index 730de4e7575..cc7ceb499f6 100644 --- a/build/shared/lib/keywords.txt +++ b/build/shared/lib/keywords.txt @@ -179,6 +179,7 @@ parseInt KEYWORD2 parseFloat KEYWORD2 readBytes KEYWORD2 readBytesUntil KEYWORD2 +waitForBufferSpace KEYWORD2 setup KEYWORD3 Setup loop KEYWORD3 Loop