From 95feb0874b60b35e77f01d1dffce39b580fda271 Mon Sep 17 00:00:00 2001 From: Jacob Abel Date: Thu, 27 Aug 2015 08:47:15 -0700 Subject: [PATCH] Add isFlushed() method to serial classes Add isFlushed() to HardwareSerial class for AVR and to UARTClass class for SAM. This provides a non-blocking way to check if the transmit buffer is flushed. --- build/shared/lib/keywords.txt | 1 + hardware/arduino/avr/cores/arduino/HardwareSerial.cpp | 6 ++++++ hardware/arduino/avr/cores/arduino/HardwareSerial.h | 1 + hardware/arduino/sam/cores/arduino/UARTClass.cpp | 5 +++++ hardware/arduino/sam/cores/arduino/UARTClass.h | 1 + 5 files changed, 14 insertions(+) diff --git a/build/shared/lib/keywords.txt b/build/shared/lib/keywords.txt index a7bc003d2d9..9c714673f6f 100644 --- a/build/shared/lib/keywords.txt +++ b/build/shared/lib/keywords.txt @@ -152,6 +152,7 @@ println KEYWORD2 Serial_Println available KEYWORD2 Serial_Available availableForWrite KEYWORD2 flush KEYWORD2 Serial_Flush +isFlushed KEYWORD2 setTimeout KEYWORD2 find KEYWORD2 findUntil KEYWORD2 diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp b/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp index 5cd89e5e664..70afd520aa7 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp @@ -210,6 +210,12 @@ void HardwareSerial::flush() // the hardware finished tranmission (TXC is set). } +bool HardwareSerial::isFlushed(void) +{ + if (!_written) return true; + return (_tx_buffer_head == _tx_buffer_tail) && bit_is_clear(*_ucsrb, UDRIE0) && bit_is_set(*_ucsra, TXC0); +} + size_t HardwareSerial::write(uint8_t c) { _written = true; diff --git a/hardware/arduino/avr/cores/arduino/HardwareSerial.h b/hardware/arduino/avr/cores/arduino/HardwareSerial.h index 8a5bf95e660..37db334960d 100644 --- a/hardware/arduino/avr/cores/arduino/HardwareSerial.h +++ b/hardware/arduino/avr/cores/arduino/HardwareSerial.h @@ -126,6 +126,7 @@ class HardwareSerial : public Stream virtual int read(void); int availableForWrite(void); virtual void flush(void); + virtual bool isFlushed(void); virtual size_t write(uint8_t); inline size_t write(unsigned long n) { return write((uint8_t)n); } inline size_t write(long n) { return write((uint8_t)n); } diff --git a/hardware/arduino/sam/cores/arduino/UARTClass.cpp b/hardware/arduino/sam/cores/arduino/UARTClass.cpp index ed1cb26870b..4411d838aa5 100644 --- a/hardware/arduino/sam/cores/arduino/UARTClass.cpp +++ b/hardware/arduino/sam/cores/arduino/UARTClass.cpp @@ -142,6 +142,11 @@ void UARTClass::flush( void ) ; } +bool UARTClass::isFlushed( void ) +{ + return ((_pUart->UART_SR & UART_SR_TXRDY) == UART_SR_TXRDY) && (_tx_buffer->_iTail == _tx_buffer->_iHead); +} + size_t UARTClass::write( const uint8_t uc_data ) { // Is the hardware currently busy? diff --git a/hardware/arduino/sam/cores/arduino/UARTClass.h b/hardware/arduino/sam/cores/arduino/UARTClass.h index 3747d8beb10..4205200112c 100644 --- a/hardware/arduino/sam/cores/arduino/UARTClass.h +++ b/hardware/arduino/sam/cores/arduino/UARTClass.h @@ -52,6 +52,7 @@ class UARTClass : public HardwareSerial int peek(void); int read(void); void flush(void); + bool isFlushed(void); size_t write(const uint8_t c); using Print::write; // pull in write(str) and write(buf, size) from Print