Commit d50e5978 authored by John Holman's avatar John Holman Committed by Martino Facchin

Create macro to guard critical sections for large transmit buffers

New macro TX_BUFFER_ATOMIC makes the following code block atomic
only if the transmit buffer is larger than 256 bytes. SREG is restored
on completion.
The macro is then used to simplify code for availableForWrite()
parent f1c4cc63
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <util/atomic.h>
#include "Arduino.h" #include "Arduino.h"
#include "HardwareSerial.h" #include "HardwareSerial.h"
...@@ -76,6 +77,13 @@ void serialEventRun(void) ...@@ -76,6 +77,13 @@ void serialEventRun(void)
#endif #endif
} }
// macro to guard critical sections when needed for large TX buffer sizes
#if (SERIAL_TX_BUFFER_SIZE>256)
#define TX_BUFFER_ATOMIC ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
#else
#define TX_BUFFER_ATOMIC
#endif
// Actual interrupt handlers ////////////////////////////////////////////////////////////// // Actual interrupt handlers //////////////////////////////////////////////////////////////
void HardwareSerial::_tx_udr_empty_irq(void) void HardwareSerial::_tx_udr_empty_irq(void)
...@@ -177,15 +185,13 @@ int HardwareSerial::read(void) ...@@ -177,15 +185,13 @@ int HardwareSerial::read(void)
int HardwareSerial::availableForWrite(void) int HardwareSerial::availableForWrite(void)
{ {
#if (SERIAL_TX_BUFFER_SIZE>256) tx_buffer_index_t head;
uint8_t oldSREG = SREG; tx_buffer_index_t tail;
cli();
#endif TX_BUFFER_ATOMIC {
tx_buffer_index_t head = _tx_buffer_head; head = _tx_buffer_head;
tx_buffer_index_t tail = _tx_buffer_tail; tail = _tx_buffer_tail;
#if (SERIAL_TX_BUFFER_SIZE>256) }
SREG = oldSREG;
#endif
if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail; if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail;
return tail - head - 1; return tail - head - 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment