Skip to content

Commit 5c11f4f

Browse files
committed
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.
1 parent 965480f commit 5c11f4f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

hardware/arduino/cores/arduino/HardwareSerial.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
289289
_rxcie = rxcie;
290290
_udrie = udrie;
291291
_u2x = u2x;
292+
_serial_buffer_wait = 0x1; //by default, wait for space.
292293
}
293294

294295
// Public Methods //////////////////////////////////////////////////////////////
@@ -384,11 +385,14 @@ size_t HardwareSerial::write(uint8_t c)
384385
{
385386
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
386387

387-
// If the output buffer is full, there's nothing for it other than to
388-
// wait for the interrupt handler to empty it a bit
389-
// ???: return 0 here instead?
390-
while (i == _tx_buffer->tail)
388+
// If the output buffer is full, there's two options. Either quit or wait for interrupt handler to make space. User decides.
389+
if(_serial_buffer_wait == 1) {
390+
while (i == _tx_buffer->tail)
391391
;
392+
} else {
393+
if(i == _tx_buffer->tail) { return 0; }
394+
}
395+
392396

393397
_tx_buffer->buffer[_tx_buffer->head] = c;
394398
_tx_buffer->head = i;
@@ -398,6 +402,9 @@ size_t HardwareSerial::write(uint8_t c)
398402
return 1;
399403
}
400404

405+
void HardwareSerial::waitForBufferSpace(uint8_t v) {
406+
_serial_buffer_wait = v;
407+
}
401408
// Preinstantiate Objects //////////////////////////////////////////////////////
402409

403410
#if defined(UBRRH) && defined(UBRRL)

0 commit comments

Comments
 (0)