Fixed flushing receive buffer to work independent of baud rate and with and without interrupts
This commit is contained in:
parent
99d09b0f0f
commit
e008cc8da1
35
usart.cpp
35
usart.cpp
@ -300,7 +300,7 @@ bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS )
|
|||||||
{
|
{
|
||||||
_delay_ms( 1 );
|
_delay_ms( 1 );
|
||||||
|
|
||||||
if( ui16DelayCounter++ >= ui16TimeoutMS )
|
if( ui16DelayCounter++ > ui16TimeoutMS )
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -336,14 +336,41 @@ void USART0::flushReceive()
|
|||||||
{
|
{
|
||||||
uint8_t ui8Received;
|
uint8_t ui8Received;
|
||||||
|
|
||||||
while( m_vsizeRXBufferHead != m_vsizeRXBufferTail )
|
uint16_t ui16UBRR;
|
||||||
|
ui16UBRR = static_cast<uint16_t>( *m_vui8pUBRRH ) << 8;
|
||||||
|
ui16UBRR |= *m_vui8pUBRRL;
|
||||||
|
|
||||||
|
uint8_t ui8BitsPerSymbol = 10;
|
||||||
|
|
||||||
|
uint16_t ui16BaudDelayMS = static_cast<uint16_t>( ui8BitsPerSymbol * ( 16 * ( ui16UBRR + 1.0 ) ) / ( F_CPU / 1000.0 ) ) + 1;
|
||||||
|
|
||||||
|
if( !( SREG & ( 1 << SREG_I ) ) )
|
||||||
{
|
{
|
||||||
receiveByte( ui8Received );
|
while( true )
|
||||||
|
{
|
||||||
|
for( uint16_t i = 0; i < ui16BaudDelayMS; ++i )
|
||||||
|
{
|
||||||
|
_delay_ms( 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !( SREG & ( 1 << SREG_I ) ) && ( *m_vui8pUCSRA & ( 1 << RXC_D ) ) )
|
if( m_vsizeRXBufferHead != m_vsizeRXBufferTail )
|
||||||
|
{
|
||||||
|
receiveByte( ui8Received );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( ( *m_vui8pUCSRA & ( 1 << RXC_D ) ) )
|
||||||
{
|
{
|
||||||
ui8Received = *m_vui8pUDR;
|
ui8Received = *m_vui8pUDR;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while( receiveByte( ui8Received, ui16BaudDelayMS ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user