uart/usart/usart.h

175 lines
3.8 KiB
C++

/*
* Copyright (c) by BlackMark 2015-2016
* Date 20/05/2016
* Version 2.4
*/
#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/string.h"
#include "vector/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
#ifdef USART_SHAREDIO
#define RXEN_D RXEN
#define TXEN_D TXEN
#define UCSZ0_D UCSZ0
#define UCSZ1_D UCSZ1
#define UCSZ2_D UCSZ2
#define UPM0_D UPM0
#define UPM1_D UPM1
#define USBS_D USBS
#define RXC_D RXC
#define UDRE_D UDRE
#else
#define RXEN_D RXEN0
#define TXEN_D TXEN0
#define UCSZ0_D UCSZ00
#define UCSZ1_D UCSZ01
#define UCSZ2_D UCSZ02
#define UPM0_D UPM00
#define UPM1_D UPM01
#define USBS_D USBS0
#define RXC_D RXC0
#define UDRE_D UDRE0
#endif
#ifdef USART_SPI
#define UMSEL0_D UMSEL00
#define UMSEL1_D UMSEL01
#else
#define UMSEL_D UMSEL
#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;
private:
char m_szConvertBuffer[64];
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();
void init( uint32_t ui32BaudRate = 9600, uint8_t ui8DataBits = 8, Parity enmParity = Parity::DISABLED, StopBit enmStopBits = StopBit::ONE, 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();
};
#endif
#endif