Fixed flushing receive buffer to work independent of baud rate and with and without interrupts

This commit is contained in:
BlackMark 2016-05-25 01:40:53 +02:00
parent 99d09b0f0f
commit e008cc8da1
2 changed files with 36 additions and 9 deletions

View File

@ -300,7 +300,7 @@ bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS )
{
_delay_ms( 1 );
if( ui16DelayCounter++ >= ui16TimeoutMS )
if( ui16DelayCounter++ > ui16TimeoutMS )
{
return false;
}
@ -336,14 +336,41 @@ void USART0::flushReceive()
{
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;
continue;
}
break;
}
}
else
{
while( receiveByte( ui8Received, ui16BaudDelayMS ) );
}
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (c) by BlackMark 2015-2016
* Date 24/05/2016
* Version 2.9
* Date 25/05/2016
* Version 3.0
*/
#ifndef USART_H