diff --git a/usart/main.cpp b/usart/main.cpp index 520ef4b..4b47263 100644 --- a/usart/main.cpp +++ b/usart/main.cpp @@ -11,6 +11,7 @@ int main() { + sei(); USART0 &cUSART = USART0::inst(); cUSART.init(); diff --git a/usart/usart.cpp b/usart/usart.cpp index 52a0a5a..1f4c025 100644 --- a/usart/usart.cpp +++ b/usart/usart.cpp @@ -252,16 +252,21 @@ void USART0::init( uint32_t ui32BaudRate /* = 9600 */, uint8_t ui8DataBits /* = setRXState( true ); setTXState( true ); + setRXInterrupt( true ); setUDREInterrupt( false ); - - sei(); } ////////////////////////////////////////////////////////////////////////// bool USART0::receiveByte( uint8_t &ui8Data ) { - if( m_vsizeRXBufferHead == m_vsizeRXBufferTail ) + if( m_vsizeRXBufferHead == m_vsizeRXBufferTail && !( SREG & ( 1 << SREG_I ) ) ) + { + while( !( *m_vui8pUCSRA & ( 1 << RXC_D ) ) ); + ui8Data = *m_vui8pUDR; + return true; + } + else if( m_vsizeRXBufferHead == m_vsizeRXBufferTail ) { return false; } @@ -291,11 +296,11 @@ bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS ) } ////////////////////////////////////////////////////////////////////////// -bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator /* = "\r\n" */, size_t sizeMaxSize /* = 512 */ ) +bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator /* = "\r\n" */ ) { size_t sizeReceived = 0; - while( sizeReceived < sizeMaxSize - 1 && sizeReceived < sizeBufferLength - 1 ) + while( sizeReceived < sizeBufferLength - 1 ) { uint8_t ui8ReceiveByte; @@ -323,12 +328,26 @@ void USART0::transmitByte( uint8_t ui8Data ) size_t sizeIndex = ( m_vsizeTXBufferHead + 1 ) % sm_sizeTXBUFFER_SIZE; - while( sizeIndex == m_vsizeTXBufferTail ); + while( sizeIndex == m_vsizeTXBufferTail ) + { + if( !( SREG & ( 1 << SREG_I ) ) && *m_vui8pUCSRA & ( 1 << UDRE_D ) ) + { + transmitInterruptHandler(); + } + } m_vui8aTXBuffer[m_vsizeTXBufferHead] = ui8Data; m_vsizeTXBufferHead = sizeIndex; - setUDREInterrupt( true ); + if( !( SREG & ( 1 << SREG_I ) ) ) + { + while( !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) ); + transmitInterruptHandler(); + } + else + { + setUDREInterrupt( true ); + } } ////////////////////////////////////////////////////////////////////////// diff --git a/usart/usart.h b/usart/usart.h index 8cd3d88..c282bfa 100644 --- a/usart/usart.h +++ b/usart/usart.h @@ -137,7 +137,7 @@ public: bool receiveByte( uint8_t &ui8Data ); bool receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS ); - bool receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator = "\r\n", size_t sizeMaxSize = 512 ); + bool receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator = "\r\n" ); void transmitByte( uint8_t ui8Data ); void transmitString( const char *szString );