diff --git a/usart/usart.cpp b/usart/usart.cpp index 1f4c025..e201c74 100644 --- a/usart/usart.cpp +++ b/usart/usart.cpp @@ -235,6 +235,20 @@ void USART0::setMode( Mode enmMode ) setUCSRC( ui8UCSRC ); } +////////////////////////////////////////////////////////////////////////// +USART0::~USART0() +{ + flushTransmit(); + + setBaudRate( 0 ); + + setRXState( false ); + setTXState( false ); + + setRXInterrupt( false ); + setUDREInterrupt( false ); +} + ////////////////////////////////////////////////////////////////////////// USART0& USART0::inst() { @@ -278,7 +292,7 @@ bool USART0::receiveByte( uint8_t &ui8Data ) } ////////////////////////////////////////////////////////////////////////// -bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS ) +bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS ) { uint16_t ui16DelayCounter = 0; @@ -286,7 +300,7 @@ bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS ) { _delay_ms( 1 ); - if( ui16DelayCounter++ >= ui16DelayMS ) + if( ui16DelayCounter++ >= ui16TimeoutMS ) { return false; } @@ -317,6 +331,14 @@ bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *s return false; } +////////////////////////////////////////////////////////////////////////// +void USART0::flushReceive( uint16_t ui16TimeoutMS ) +{ + uint8_t ui8Received; + + while( receiveByte( ui8Received, ui16TimeoutMS ) ); +} + ////////////////////////////////////////////////////////////////////////// void USART0::transmitByte( uint8_t ui8Data ) { @@ -359,6 +381,12 @@ void USART0::transmitString( const char *szString ) } } +////////////////////////////////////////////////////////////////////////// +void USART0::flushTransmit() +{ + while( m_vsizeTXBufferHead != m_vsizeTXBufferTail && !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) ); +} + ////////////////////////////////////////////////////////////////////////// void USART0::receiveInterruptHandler() { diff --git a/usart/usart.h b/usart/usart.h index c282bfa..a4d2850 100644 --- a/usart/usart.h +++ b/usart/usart.h @@ -1,7 +1,7 @@ /* * Copyright (c) by BlackMark 2015-2016 -* Date 21/05/2016 -* Version 2.7 +* Date 22/05/2016 +* Version 2.8 */ #ifndef USART_H @@ -128,6 +128,8 @@ private: void setStopBits( StopBit enmStopBits ); void setMode( Mode enmMode ); + ~USART0(); + public: static USART0& inst(); USART0( const USART0& ) = delete; @@ -136,11 +138,13 @@ public: void init( uint32_t ui32BaudRate = 9600, uint8_t ui8DataBits = 8, Parity enmParity = Parity::DISABLED, StopBit enmStopBits = StopBit::ONE, Mode enmMode = Mode::ASYNCHRONOUS ); bool receiveByte( uint8_t &ui8Data ); - bool receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS ); + bool receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS ); bool receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator = "\r\n" ); + void flushReceive( uint16_t ui16TimeoutMS ); void transmitByte( uint8_t ui8Data ); void transmitString( const char *szString ); + void flushTransmit(); void receiveInterruptHandler(); void transmitInterruptHandler();