Added getter functions for usart settings and fixed receive flush to work for all usart settings

This commit is contained in:
BlackMark 2016-05-25 21:22:16 +02:00
parent e008cc8da1
commit c261d7d309
2 changed files with 87 additions and 9 deletions

View File

@ -235,6 +235,66 @@ void USART0::setMode( Mode enmMode )
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
uint32_t USART0::getBaudRate()
{
uint16_t ui16UBRR;
ui16UBRR = static_cast<uint16_t>( *m_vui8pUBRRH ) << 8;
ui16UBRR |= *m_vui8pUBRRL;
return F_CPU / ( static_cast<uint32_t>( 16 ) * ( ui16UBRR + 1 ) );
}
//////////////////////////////////////////////////////////////////////////
uint8_t USART0::getDataBits()
{
if( *m_vui8pUCSRB & ( 1 << UCSZ2_D ) )
{
return 9;
}
uint8_t ui8UCSRC = readUCSRC();
bool bZeroBit = ui8UCSRC & ( 1 << UCSZ0_D );
bool bOneBit = ui8UCSRC & ( 1 << UCSZ1_D );
return 5 + ( bOneBit << 1 | bZeroBit );
}
//////////////////////////////////////////////////////////////////////////
USART0::Parity USART0::getParity()
{
uint8_t ui8UCSRC = readUCSRC();
bool bZeroBit = ui8UCSRC & ( 1 << UPM0_D );
bool bOneBit = ui8UCSRC & ( 1 << UPM1_D );
if( bOneBit && !bZeroBit )
{
return Parity::EVEN;
}
if( bOneBit && bZeroBit )
{
return Parity::ODD;
}
return Parity::DISABLED;
}
//////////////////////////////////////////////////////////////////////////
USART0::StopBit USART0::getStopBits()
{
uint8_t ui8UCSRC = readUCSRC();
if( ui8UCSRC & ( 1 << USBS_D ) )
{
return StopBit::TWO;
}
return StopBit::ONE;
}
//////////////////////////////////////////////////////////////////////////
USART0::~USART0()
{
@ -334,16 +394,29 @@ bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *s
//////////////////////////////////////////////////////////////////////////
void USART0::flushReceive()
{
uint32_t ui32BaudRate = getBaudRate();
uint8_t ui8BitsPerSymbol = 1;
ui8BitsPerSymbol += getDataBits();
if( getParity() != Parity::DISABLED )
{
ui8BitsPerSymbol += 1;
}
if( getStopBits() == StopBit::ONE )
{
ui8BitsPerSymbol += 1;
}
else
{
ui8BitsPerSymbol += 2;
}
uint16_t ui16BaudDelayMS = static_cast<uint16_t>( ( 1000.0 * ui8BitsPerSymbol ) / ui32BaudRate ) + 1;
uint8_t ui8Received;
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 ) ) )
{
while( true )

View File

@ -1,7 +1,7 @@
/*
* Copyright (c) by BlackMark 2015-2016
* Date 25/05/2016
* Version 3.0
* Version 3.1
*/
#ifndef USART_H
@ -128,6 +128,11 @@ private:
void setStopBits( StopBit enmStopBits );
void setMode( Mode enmMode );
uint32_t getBaudRate();
uint8_t getDataBits();
Parity getParity();
StopBit getStopBits();
~USART0();
public: