uart/usart/usart.cpp

642 lines
14 KiB
C++
Raw Normal View History

2016-05-20 15:32:42 +02:00
#include "usart.h"
2016-05-19 21:43:10 +02:00
2016-05-20 22:27:57 +02:00
//////////////////////////////////////////////////////////////////////////
USART0 USART0::sm_cInstance;
//////////////////////////////////////////////////////////////////////////
USART0::USART0()
{
#ifdef USART_SHAREDIO
m_vui8pUCSRA = &UCSRA;
m_vui8pUCSRB = &UCSRB;
m_vui8pUCSRC = &UCSRC;
m_vui8pUBRRH = &UBRRH;
m_vui8pUBRRL = &UBRRL;
m_vui8pUDR = &UDR;
#endif
#ifndef USART_SHAREDIO
m_vui8pUCSRA = &UCSR0A;
m_vui8pUCSRB = &UCSR0B;
m_vui8pUCSRC = &UCSR0C;
m_vui8pUBRRH = &UBRR0H;
m_vui8pUBRRL = &UBRR0L;
m_vui8pUDR = &UDR0;
#endif
m_vsizeRXBufferHead = 0;
m_vsizeRXBufferTail = 0;
m_vsizeTXBufferHead = 0;
m_vsizeTXBufferTail = 0;
}
2016-05-19 21:43:10 +02:00
//////////////////////////////////////////////////////////////////////////
uint8_t USART0::readUCSRC()
{
uint8_t ui8UCSRC;
#ifdef USART_SHAREDIO
ui8UCSRC = UBRRH;
ui8UCSRC = UCSRC;
#else
ui8UCSRC = *m_vui8pUCSRC;
#endif
return ui8UCSRC;
}
//////////////////////////////////////////////////////////////////////////
void USART0::setUCSRC( uint8_t ui8UCSRC )
{
#ifdef USART_SHAREDIO
*m_vui8pUCSRC = ( 1 << URSEL ) | ui8UCSRC;
#else
*m_vui8pUCSRC = ui8UCSRC;
#endif
}
//////////////////////////////////////////////////////////////////////////
void USART0::setRXState( bool bEnable )
{
if( bEnable )
{
2016-05-19 22:13:06 +02:00
*m_vui8pUCSRB |= ( 1 << RXEN_D );
2016-05-19 21:43:10 +02:00
}
else
{
2016-05-19 22:13:06 +02:00
*m_vui8pUCSRB &= ~( 1 << RXEN_D );
2016-05-19 21:43:10 +02:00
}
}
//////////////////////////////////////////////////////////////////////////
void USART0::setTXState( bool bEnable )
{
if( bEnable )
{
2016-05-19 22:13:06 +02:00
*m_vui8pUCSRB |= ( 1 << TXEN_D );
2016-05-19 21:43:10 +02:00
}
else
{
2016-05-19 22:13:06 +02:00
*m_vui8pUCSRB &= ~( 1 << TXEN_D );
2016-05-19 21:43:10 +02:00
}
}
2016-05-20 22:27:57 +02:00
//////////////////////////////////////////////////////////////////////////
void USART0::setRXInterrupt( bool bEnable )
{
if( bEnable )
{
*m_vui8pUCSRB |= ( 1 << RXCIE_D );
}
else
{
*m_vui8pUCSRB &= ~( 1 << RXCIE_D );
}
}
//////////////////////////////////////////////////////////////////////////
void USART0::setUDREInterrupt( bool bEnable )
{
if( bEnable )
{
*m_vui8pUCSRB |= ( 1 << UDRIE_D );
}
else
{
*m_vui8pUCSRB &= ~( 1 << UDRIE_D );
}
}
2016-05-19 21:43:10 +02:00
//////////////////////////////////////////////////////////////////////////
void USART0::setBaudRate( uint32_t ui32BaudRate )
{
uint16_t ui16UBRR = ( ( F_CPU / ( 16 * ui32BaudRate ) ) - 1 );
*m_vui8pUBRRH = static_cast<uint8_t>( ui16UBRR >> 8 );
*m_vui8pUBRRL = static_cast<uint8_t>( ui16UBRR );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setDataBits( uint8_t ui8DataBits )
{
uint8_t ui8UCSRC = readUCSRC();
if( ui8DataBits < 5 )
{
ui8DataBits = 5;
}
else if( ui8DataBits > 9 )
{
ui8DataBits = 9;
}
if( ui8DataBits <= 8 )
{
bool bZeroBit = ( ui8DataBits - 5 ) & 1;
bool bOneBit = ( ( ui8DataBits - 5 ) >> 1 ) & 1;
if( bZeroBit )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC |= ( 1 << UCSZ0_D );
2016-05-19 21:43:10 +02:00
}
else
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( 1 << UCSZ0_D );
2016-05-19 21:43:10 +02:00
}
if( bOneBit )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC |= ( 1 << UCSZ1_D );
2016-05-19 21:43:10 +02:00
}
else
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( 1 << UCSZ1_D );
2016-05-19 21:43:10 +02:00
}
2016-05-19 22:13:06 +02:00
*m_vui8pUCSRB &= ~( 1 << UCSZ2_D );
2016-05-19 21:43:10 +02:00
}
else
{
2016-05-19 22:13:06 +02:00
ui8UCSRC |= ( 1 << UCSZ1_D ) | ( 1 << UCSZ0_D );
*m_vui8pUCSRB |= ( 1 << UCSZ2_D );
2016-05-19 21:43:10 +02:00
}
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setParity( Parity enmParity )
{
uint8_t ui8UCSRC = readUCSRC();
if( enmParity == Parity::DISABLED )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( ( 1 << UPM1_D ) | ( 1 << UPM0_D ) );
2016-05-19 21:43:10 +02:00
}
else if( enmParity == Parity::ODD )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC |= ( ( 1 << UPM1_D ) | ( 1 << UPM0_D ) );
2016-05-19 21:43:10 +02:00
}
else if( enmParity == Parity::EVEN )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( ( 1 << UPM0_D ) );
ui8UCSRC |= ( ( 1 << UPM1_D ) );
2016-05-19 21:43:10 +02:00
}
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setStopBits( StopBit enmStopBits )
{
uint8_t ui8UCSRC = readUCSRC();
if( enmStopBits == StopBit::ONE )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( 1 << USBS_D );
2016-05-19 21:43:10 +02:00
}
else if( enmStopBits == StopBit::TWO )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC |= ( 1 << USBS_D );
2016-05-19 21:43:10 +02:00
}
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setMode( Mode enmMode )
{
uint8_t ui8UCSRC = readUCSRC();
#ifdef USART_SPI
if( enmMode == Mode::ASYNCHRONOUS )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( ( 1 << UMSEL1_D ) | ( 1 << UMSEL0_D ) );
2016-05-19 21:43:10 +02:00
}
else if( enmMode == Mode::SYNCHRONOUS )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( 1 << UMSEL1_D );
ui8UCSRC |= ( 1 << UMSEL0_D );
2016-05-19 21:43:10 +02:00
}
else if( enmMode == Mode::MASTERSPI )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC |= ( ( 1 << UMSEL1_D ) | ( 1 << UMSEL0_D ) );
2016-05-19 21:43:10 +02:00
}
#else
if( enmMode == Mode::ASYNCHRONOUS )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC &= ~( 1 << UMSEL_D );
2016-05-19 21:43:10 +02:00
}
else if( enmMode == Mode::SYNCHRONOUS )
{
2016-05-19 22:13:06 +02:00
ui8UCSRC |= ( 1 << UMSEL_D );
2016-05-19 21:43:10 +02:00
}
#endif
setUCSRC( ui8UCSRC );
}
2016-10-29 17:22:34 +02:00
//////////////////////////////////////////////////////////////////////////
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;
}
2016-05-24 20:59:46 +02:00
//////////////////////////////////////////////////////////////////////////
USART0::~USART0()
{
flushTransmit();
setBaudRate( 0 );
setRXState( false );
setTXState( false );
setRXInterrupt( false );
setUDREInterrupt( false );
}
2016-05-19 21:43:10 +02:00
//////////////////////////////////////////////////////////////////////////
2016-05-20 22:27:57 +02:00
USART0& USART0::inst()
2016-05-19 21:43:10 +02:00
{
2016-05-20 22:27:57 +02:00
return sm_cInstance;
2016-05-19 21:43:10 +02:00
}
//////////////////////////////////////////////////////////////////////////
void USART0::init( uint32_t ui32BaudRate /* = 9600 */, uint8_t ui8DataBits /* = 8 */, Parity enmParity /* = Parity::DISABLED */, StopBit enmStopBits /* = StopBit::ONE */, Mode enmMode /* = Mode::ASYNCHRONOUS */ )
2016-05-19 21:43:10 +02:00
{
setBaudRate( ui32BaudRate );
setDataBits( ui8DataBits );
setParity( enmParity );
setStopBits( enmStopBits );
setMode( enmMode );
2016-05-19 21:43:10 +02:00
setRXState( true );
setTXState( true );
2016-05-20 22:27:57 +02:00
setRXInterrupt( true );
setUDREInterrupt( false );
2016-05-19 21:43:10 +02:00
}
//////////////////////////////////////////////////////////////////////////
2016-05-20 22:27:57 +02:00
bool USART0::receiveByte( uint8_t &ui8Data )
2016-05-19 21:43:10 +02:00
{
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 )
2016-05-19 21:43:10 +02:00
{
2016-05-20 22:27:57 +02:00
return false;
}
ui8Data = m_vui8aRXBuffer[m_vsizeRXBufferTail];
m_vsizeRXBufferTail = ( m_vsizeRXBufferTail + 1 ) % sm_sizeRXBUFFER_SIZE;
return true;
2016-05-19 21:43:10 +02:00
}
//////////////////////////////////////////////////////////////////////////
2016-05-24 20:59:46 +02:00
bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS )
2016-05-19 21:43:10 +02:00
{
2016-05-20 22:27:57 +02:00
uint16_t ui16DelayCounter = 0;
while( !receiveByte( ui8Data ) )
{
_delay_ms( 1 );
2016-10-29 17:22:34 +02:00
if( ui16DelayCounter++ > ui16TimeoutMS )
2016-05-20 22:27:57 +02:00
{
return false;
}
}
return true;
2016-05-19 21:43:10 +02:00
}
2016-10-29 17:22:34 +02:00
//////////////////////////////////////////////////////////////////////////
uint8_t USART0::receiveByteBlocked()
{
uint8_t ui8Received;
while( !receiveByte( ui8Received ) );
return ui8Received;
}
//////////////////////////////////////////////////////////////////////////
uint8_t USART0::receivePeek()
{
uint8_t ui8Received;
if( !( SREG & ( 1 << SREG_I ) && m_vsizeRXBufferHead == m_vsizeRXBufferTail ) )
{
while( !( *m_vui8pUCSRA & ( 1 << RXC_D ) ) );
ui8Received = *m_vui8pUDR;
size_t sizeIndex = ( m_vsizeRXBufferHead + 1 ) % sm_sizeRXBUFFER_SIZE;
if( sizeIndex != m_vsizeRXBufferTail )
{
m_vui8aRXBuffer[m_vsizeRXBufferHead] = ui8Received;
m_vsizeRXBufferHead = sizeIndex;
}
return ui8Received;
}
while( m_vsizeRXBufferHead == m_vsizeRXBufferTail );
return m_vui8aRXBuffer[m_vsizeRXBufferTail];
}
2016-05-19 21:43:10 +02:00
//////////////////////////////////////////////////////////////////////////
bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator /* = "\r\n" */ )
2016-05-19 21:43:10 +02:00
{
size_t sizeReceived = 0;
while( sizeReceived < sizeBufferLength - 1 )
2016-05-19 21:43:10 +02:00
{
2016-05-20 22:27:57 +02:00
uint8_t ui8ReceiveByte;
while( !receiveByte( ui8ReceiveByte ) );
szBuffer[sizeReceived++] = ui8ReceiveByte;
szBuffer[sizeReceived] = '\0';
2016-05-19 21:43:10 +02:00
if( strstr( szBuffer, szLineTerminator ) )
2016-05-19 21:43:10 +02:00
{
2016-10-29 17:22:34 +02:00
szBuffer[sizeReceived - strlen( szLineTerminator )] = '\0';
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////////////
bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, uint16_t ui16TimeoutMS, const char *szLineTerminator /* = "\r\n" */ )
{
size_t sizeReceived = 0;
while( sizeReceived < sizeBufferLength - 1 )
{
uint8_t ui8ReceiveByte;
uint16_t ui16DelayCounter = 0;
while( !receiveByte( ui8ReceiveByte ) )
{
_delay_ms( 1 );
if( ui16DelayCounter++ > ui16TimeoutMS )
{
szBuffer[sizeReceived] = '\0';
return false;
}
}
szBuffer[sizeReceived++] = ui8ReceiveByte;
szBuffer[sizeReceived] = '\0';
if( strstr( szBuffer, szLineTerminator ) )
{
szBuffer[sizeReceived - strlen( szLineTerminator )] = '\0';
return true;
2016-05-19 21:43:10 +02:00
}
}
return false;
2016-05-19 21:43:10 +02:00
}
2016-05-24 20:59:46 +02:00
//////////////////////////////////////////////////////////////////////////
void USART0::flushReceive()
2016-05-24 20:59:46 +02:00
{
2016-10-29 17:22:34 +02:00
uint32_t ui32BaudRate = getBaudRate();
uint8_t ui8BitsPerSymbol = 1;
2016-05-24 20:59:46 +02:00
2016-10-29 17:22:34 +02:00
ui8BitsPerSymbol += getDataBits();
if( getParity() != Parity::DISABLED )
{
2016-10-29 17:22:34 +02:00
ui8BitsPerSymbol += 1;
}
2016-10-29 17:22:34 +02:00
if( getStopBits() == StopBit::ONE )
{
2016-10-29 17:22:34 +02:00
ui8BitsPerSymbol += 1;
}
else
{
ui8BitsPerSymbol += 2;
}
uint16_t ui16BaudDelayMS = static_cast<uint16_t>( ( 1000.0 * ui8BitsPerSymbol ) / ui32BaudRate ) + 1;
uint8_t ui8Received;
if( !( SREG & ( 1 << SREG_I ) ) )
{
while( true )
{
for( uint16_t i = 0; i < ui16BaudDelayMS; ++i )
{
_delay_ms( 1 );
}
if( m_vsizeRXBufferHead != m_vsizeRXBufferTail )
{
receiveByte( ui8Received );
continue;
}
if( ( *m_vui8pUCSRA & ( 1 << RXC_D ) ) )
{
ui8Received = *m_vui8pUDR;
continue;
}
break;
}
}
else
{
while( receiveByte( ui8Received, ui16BaudDelayMS ) );
}
2016-05-24 20:59:46 +02:00
}
2016-05-19 21:43:10 +02:00
//////////////////////////////////////////////////////////////////////////
void USART0::transmitByte( uint8_t ui8Data )
2016-05-19 21:43:10 +02:00
{
2016-05-20 22:27:57 +02:00
if( m_vsizeTXBufferHead == m_vsizeTXBufferTail && *m_vui8pUCSRA & ( 1 << UDRE_D ) )
{
*m_vui8pUDR = ui8Data;
return;
}
size_t sizeIndex = ( m_vsizeTXBufferHead + 1 ) % sm_sizeTXBUFFER_SIZE;
while( sizeIndex == m_vsizeTXBufferTail )
{
if( !( SREG & ( 1 << SREG_I ) ) && *m_vui8pUCSRA & ( 1 << UDRE_D ) )
{
transmitInterruptHandler();
}
}
2016-05-20 22:27:57 +02:00
m_vui8aTXBuffer[m_vsizeTXBufferHead] = ui8Data;
m_vsizeTXBufferHead = sizeIndex;
if( !( SREG & ( 1 << SREG_I ) ) )
{
while( !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) );
transmitInterruptHandler();
}
else
{
setUDREInterrupt( true );
}
2016-05-19 21:43:10 +02:00
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmitString( const char *szString )
2016-05-19 21:43:10 +02:00
{
2016-05-20 22:27:57 +02:00
while( *szString )
2016-05-19 21:43:10 +02:00
{
transmitByte( *szString++ );
2016-05-19 21:43:10 +02:00
}
}
2016-05-24 20:59:46 +02:00
//////////////////////////////////////////////////////////////////////////
void USART0::flushTransmit()
{
while( m_vsizeTXBufferHead != m_vsizeTXBufferTail && !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) );
}
2016-05-20 22:27:57 +02:00
//////////////////////////////////////////////////////////////////////////
void USART0::receiveInterruptHandler()
{
uint8_t ui8ReceivedByte = *m_vui8pUDR;
size_t sizeIndex = ( m_vsizeRXBufferHead + 1 ) % sm_sizeRXBUFFER_SIZE;
if( sizeIndex != m_vsizeRXBufferTail )
{
m_vui8aRXBuffer[m_vsizeRXBufferHead] = ui8ReceivedByte;
m_vsizeRXBufferHead = sizeIndex;
}
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmitInterruptHandler()
{
uint8_t ui8TransmitByte = m_vui8aTXBuffer[m_vsizeTXBufferTail];
m_vsizeTXBufferTail = ( m_vsizeTXBufferTail + 1 ) % sm_sizeTXBUFFER_SIZE;
*m_vui8pUDR = ui8TransmitByte;
if( m_vsizeTXBufferHead == m_vsizeTXBufferTail )
{
setUDREInterrupt( false );
}
}
//////////////////////////////////////////////////////////////////////////
ISR( USART0_RX_vect_D )
{
USART0::inst().receiveInterruptHandler();
}
//////////////////////////////////////////////////////////////////////////
ISR( USART0_UDRE_vect_D )
{
USART0::inst().transmitInterruptHandler();
}
2016-05-20 22:33:47 +02:00
/************************************************************************/
/************************************************************************/
2016-05-19 21:43:10 +02:00
#ifdef SECOND_USART
2016-05-20 22:33:47 +02:00
//////////////////////////////////////////////////////////////////////////
USART1 USART1::sm_cInstance;
//////////////////////////////////////////////////////////////////////////
USART1& USART1::inst()
{
return sm_cInstance;
}
2016-05-19 21:43:10 +02:00
//////////////////////////////////////////////////////////////////////////
USART1::USART1()
2016-05-19 21:43:10 +02:00
{
m_vui8pUCSRA = &UCSR1A;
m_vui8pUCSRB = &UCSR1B;
m_vui8pUCSRC = &UCSR1C;
m_vui8pUBRRH = &UBRR1H;
m_vui8pUBRRL = &UBRR1L;
m_vui8pUDR = &UDR1;
}
2016-05-20 22:33:47 +02:00
//////////////////////////////////////////////////////////////////////////
ISR( USART1_RX_vect_D )
{
USART1::inst().receiveInterruptHandler();
}
//////////////////////////////////////////////////////////////////////////
ISR( USART1_UDRE_vect_D )
{
USART1::inst().transmitInterruptHandler();
}
2016-05-19 21:43:10 +02:00
#endif