Improved code so that it automatically adjusts to whether interrupts are enabled or not and also removed useless max size paramter from receive string function

This commit is contained in:
BlackMark 2016-05-21 21:49:56 +02:00
parent 02d5d34e72
commit 30c0269fca
3 changed files with 28 additions and 8 deletions

View File

@ -11,6 +11,7 @@
int main()
{
sei();
USART0 &cUSART = USART0::inst();
cUSART.init();

View File

@ -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 );
}
}
//////////////////////////////////////////////////////////////////////////

View File

@ -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 );