Skip to content

A fix for issue 672: http://code.google.com/p/arduino/issues/detail?id=672 #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/shared/lib/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ parseInt KEYWORD2
parseFloat KEYWORD2
readBytes KEYWORD2
readBytesUntil KEYWORD2
waitForBufferSpace KEYWORD2

setup KEYWORD3 Setup
loop KEYWORD3 Loop
15 changes: 11 additions & 4 deletions hardware/arduino/cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 //////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions hardware/arduino/cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down