From c261d7d3096b7378ce931893d896cebf5b524eaa Mon Sep 17 00:00:00 2001 From: BlackMark Date: Wed, 25 May 2016 21:22:16 +0200 Subject: [PATCH] Added getter functions for usart settings and fixed receive flush to work for all usart settings --- usart.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++----- usart.h | 7 ++++- 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/usart.cpp b/usart.cpp index 82d72cf..23f347d 100644 --- a/usart.cpp +++ b/usart.cpp @@ -235,6 +235,66 @@ void USART0::setMode( Mode enmMode ) setUCSRC( ui8UCSRC ); } +////////////////////////////////////////////////////////////////////////// +uint32_t USART0::getBaudRate() +{ + uint16_t ui16UBRR; + ui16UBRR = static_cast( *m_vui8pUBRRH ) << 8; + ui16UBRR |= *m_vui8pUBRRL; + + return F_CPU / ( static_cast( 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( ( 1000.0 * ui8BitsPerSymbol ) / ui32BaudRate ) + 1; + uint8_t ui8Received; - uint16_t ui16UBRR; - ui16UBRR = static_cast( *m_vui8pUBRRH ) << 8; - ui16UBRR |= *m_vui8pUBRRL; - - uint8_t ui8BitsPerSymbol = 10; - - uint16_t ui16BaudDelayMS = static_cast( ui8BitsPerSymbol * ( 16 * ( ui16UBRR + 1.0 ) ) / ( F_CPU / 1000.0 ) ) + 1; - if( !( SREG & ( 1 << SREG_I ) ) ) { while( true ) diff --git a/usart.h b/usart.h index 1d7ba01..a80d174 100644 --- a/usart.h +++ b/usart.h @@ -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: