aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/HardwareSerial.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cores/arduino/HardwareSerial.cpp')
-rw-r--r--cores/arduino/HardwareSerial.cpp30
1 files changed, 22 insertions, 8 deletions
diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp
index a270ace..a2029a8 100644
--- a/cores/arduino/HardwareSerial.cpp
+++ b/cores/arduino/HardwareSerial.cpp
@@ -72,7 +72,7 @@ void serialEventRun(void)
if (Serial2_available && serialEvent2 && Serial2_available()) serialEvent2();
#endif
#if defined(HAVE_HWSERIAL3)
- if (Serial3_available && serialEvent2 && Serial3_available()) serialEvent3();
+ if (Serial3_available && serialEvent3 && Serial3_available()) serialEvent3();
#endif
}
@@ -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;
@@ -117,7 +117,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
- // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
+ // assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register)
*_ubrrh = baud_setting >> 8;
*_ubrrl = baud_setting;
@@ -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,11 +171,26 @@ 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 = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
return c;
}
}
+int HardwareSerial::availableForWrite(void)
+{
+#if (SERIAL_TX_BUFFER_SIZE>256)
+ uint8_t oldSREG = SREG;
+ cli();
+#endif
+ tx_buffer_index_t head = _tx_buffer_head;
+ tx_buffer_index_t tail = _tx_buffer_tail;
+#if (SERIAL_TX_BUFFER_SIZE>256)
+ SREG = oldSREG;
+#endif
+ if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail;
+ return tail - head - 1;
+}
+
void HardwareSerial::flush()
{
// If we have never written a byte, no need to flush. This special
@@ -198,6 +213,7 @@ void HardwareSerial::flush()
size_t HardwareSerial::write(uint8_t c)
{
+ _written = true;
// If the buffer and the data register is empty, just write the byte
// to the data register and be done. This shortcut helps
// significantly improve the effective datarate at high (>
@@ -207,7 +223,7 @@ size_t HardwareSerial::write(uint8_t c)
sbi(*_ucsra, TXC0);
return 1;
}
- uint8_t i = (_tx_buffer_head + 1) % SERIAL_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
@@ -228,10 +244,8 @@ size_t HardwareSerial::write(uint8_t c)
_tx_buffer_head = i;
sbi(*_ucsrb, UDRIE0);
- _written = true;
return 1;
}
-
#endif // whole file