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:
parent
02d5d34e72
commit
30c0269fca
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
sei();
|
||||||
USART0 &cUSART = USART0::inst();
|
USART0 &cUSART = USART0::inst();
|
||||||
cUSART.init();
|
cUSART.init();
|
||||||
|
|
||||||
|
@ -252,16 +252,21 @@ void USART0::init( uint32_t ui32BaudRate /* = 9600 */, uint8_t ui8DataBits /* =
|
|||||||
|
|
||||||
setRXState( true );
|
setRXState( true );
|
||||||
setTXState( true );
|
setTXState( true );
|
||||||
|
|
||||||
setRXInterrupt( true );
|
setRXInterrupt( true );
|
||||||
setUDREInterrupt( false );
|
setUDREInterrupt( false );
|
||||||
|
|
||||||
sei();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
bool USART0::receiveByte( uint8_t &ui8Data )
|
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;
|
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;
|
size_t sizeReceived = 0;
|
||||||
|
|
||||||
while( sizeReceived < sizeMaxSize - 1 && sizeReceived < sizeBufferLength - 1 )
|
while( sizeReceived < sizeBufferLength - 1 )
|
||||||
{
|
{
|
||||||
uint8_t ui8ReceiveByte;
|
uint8_t ui8ReceiveByte;
|
||||||
|
|
||||||
@ -323,12 +328,26 @@ void USART0::transmitByte( uint8_t ui8Data )
|
|||||||
|
|
||||||
size_t sizeIndex = ( m_vsizeTXBufferHead + 1 ) % sm_sizeTXBUFFER_SIZE;
|
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_vui8aTXBuffer[m_vsizeTXBufferHead] = ui8Data;
|
||||||
m_vsizeTXBufferHead = sizeIndex;
|
m_vsizeTXBufferHead = sizeIndex;
|
||||||
|
|
||||||
setUDREInterrupt( true );
|
if( !( SREG & ( 1 << SREG_I ) ) )
|
||||||
|
{
|
||||||
|
while( !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) );
|
||||||
|
transmitInterruptHandler();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
setUDREInterrupt( true );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -137,7 +137,7 @@ public:
|
|||||||
|
|
||||||
bool receiveByte( uint8_t &ui8Data );
|
bool receiveByte( uint8_t &ui8Data );
|
||||||
bool receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS );
|
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 transmitByte( uint8_t ui8Data );
|
||||||
void transmitString( const char *szString );
|
void transmitString( const char *szString );
|
||||||
|
Loading…
Reference in New Issue
Block a user