uart/USART/USART.h
2016-05-19 21:43:10 +02:00

163 lines
3.8 KiB
C++

/*
* Copyright (c) by BlackMark 2015-2016
* Date 05/01/2016
* Version 2.2
*/
#ifndef USART_H
#define USART_H
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include "Clock.h"
#include "string.h"
#include "vector.h"
#if defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__)
#define USART_SPI
#endif
#if defined (__AVR_ATmega32A__) || (__AVR_ATmega8__)
#define USART_SHAREDIO
#endif
#if defined (__AVR_ATmega1284P__)
#define SECOND_USART
#endif
class USART0
{
public:
enum class Mode
{
ASYNCHRONOUS = 0,
SYNCHRONOUS = 1,
#ifdef USART_SPI
MASTERSPI = 2
#endif
};
enum class Parity
{
DISABLED = 0,
ODD = 1,
EVEN = 2
};
enum class StopBit
{
ONE = 1,
TWO = 2
};
protected:
volatile uint8_t *m_vui8pUCSRA;
volatile uint8_t *m_vui8pUCSRB;
volatile uint8_t *m_vui8pUCSRC;
volatile uint8_t *m_vui8pUBRRH;
volatile uint8_t *m_vui8pUBRRL;
volatile uint8_t *m_vui8pUDR;
uint8_t m_ui8RXEN;
uint8_t m_ui8TXEN;
uint8_t m_ui8UCSZ0;
uint8_t m_ui8UCSZ1;
uint8_t m_ui8UCSZ2;
uint8_t m_ui8UPM0;
uint8_t m_ui8UPM1;
uint8_t m_ui8USBS;
uint8_t m_ui8RXC;
uint8_t m_ui8UDRE;
#ifdef USART_SPI
uint8_t m_ui8UMSEL0;
uint8_t m_ui8UMSEL1;
#else
uint8_t m_ui8UMSEL;
#endif
private:
uint8_t readUCSRC();
void setUCSRC( uint8_t ui8UCSRC );
void setRXState( bool bEnable );
void setTXState( bool bEnable );
void setBaudRate( uint32_t ui32BaudRate );
void setDataBits( uint8_t ui8DataBits );
void setParity( Parity enmParity );
void setStopBits( StopBit enmStopBits );
void setMode( Mode enmMode );
public:
USART0();
USART0( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode = Mode::ASYNCHRONOUS );
~USART0();
void init( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode = Mode::ASYNCHRONOUS );
bool receiveByte( unsigned char &chData, uint32_t ui32DelayMS );
unsigned char receiveByte();
string receiveLine( string strLineTerminator = "\r\n", size_t sizeMaxSize = 1024 );
void receive( char &chReceived );
void receive( unsigned char &uchReceived );
void receive( short int &shiReceived );
void receive( unsigned short int &ushiReceived );
void receive( int &iReceived );
void receive( unsigned int &uiReceived );
void receive( long int &liReceived );
void receive( unsigned long int &uliReceived );
void receive( long long int &lliReceived );
void receive( unsigned long long int &ulliReceived );
void receive( float &fReceived );
void receive( double &dReceived );
void receive( long double &ldReceived );
void transmitByte( unsigned char byteData );
void transmit( string strData );
void transmit( char chData );
void transmit( unsigned char uchData );
void transmit( short int shiData );
void transmit( unsigned short int ushiData );
void transmit( int iData );
void transmit( unsigned int uiData );
void transmit( long int liData );
void transmit( unsigned long int uliData );
void transmit( long long int lliData );
void transmit( unsigned long long int ulliData );
void transmit( float fData );
void transmit( double dData );
void transmit( long double ldData );
//////////////////////////////////////////////////////////////////////////
template<typename T>
USART0& operator<<( const T &RHS )
{
transmit( RHS );
return *this;
}
//////////////////////////////////////////////////////////////////////////
template<typename T>
USART0& operator>>( T &RHS )
{
receive( RHS );
return *this;
}
};
#ifdef SECOND_USART
class USART1 : public USART0
{
public:
USART1();
USART1( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode = Mode::ASYNCHRONOUS );
~USART1();
};
#endif
#endif