2016-05-19 21:43:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) by BlackMark 2015-2016
|
2016-05-20 15:32:42 +02:00
|
|
|
* Date 20/05/2016
|
|
|
|
* Version 2.4
|
2016-05-19 21:43:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef USART_H
|
|
|
|
#define USART_H
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ctype.h>
|
2016-05-20 15:51:01 +02:00
|
|
|
#include "clock.h"
|
2016-05-20 15:46:20 +02:00
|
|
|
#include "string/string.h"
|
|
|
|
#include "vector/vector.h"
|
2016-05-19 21:43:10 +02:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2016-05-19 22:13:06 +02:00
|
|
|
#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
|
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
class USART0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class Mode
|
|
|
|
{
|
|
|
|
ASYNCHRONOUS = 0,
|
|
|
|
SYNCHRONOUS = 1,
|
|
|
|
#ifdef USART_SPI
|
|
|
|
MASTERSPI = 2
|
|
|
|
#endif
|
|
|
|
};
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
enum class Parity
|
|
|
|
{
|
|
|
|
DISABLED = 0,
|
|
|
|
ODD = 1,
|
|
|
|
EVEN = 2
|
|
|
|
};
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
enum class StopBit
|
|
|
|
{
|
|
|
|
ONE = 1,
|
|
|
|
TWO = 2
|
|
|
|
};
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
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:
|
2016-05-19 22:59:22 +02:00
|
|
|
char m_szConvertBuffer[64];
|
2016-05-19 21:43:10 +02:00
|
|
|
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();
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 22:59:22 +02:00
|
|
|
void init( uint32_t ui32BaudRate = 9600, uint8_t ui8DataBits = 8, Parity enmParity = Parity::DISABLED, StopBit enmStopBits = StopBit::ONE, Mode enmMode = Mode::ASYNCHRONOUS );
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
bool receiveByte( unsigned char &chData, uint32_t ui32DelayMS );
|
|
|
|
unsigned char receiveByte();
|
|
|
|
string receiveLine( string strLineTerminator = "\r\n", size_t sizeMaxSize = 1024 );
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
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 );
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
void transmitByte( unsigned char byteData );
|
|
|
|
void transmit( string strData );
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
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 );
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename T>
|
|
|
|
USART0& operator<<( const T &RHS )
|
|
|
|
{
|
|
|
|
transmit( RHS );
|
|
|
|
return *this;
|
|
|
|
}
|
2016-05-20 15:51:01 +02:00
|
|
|
|
2016-05-19 21:43:10 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
template<typename T>
|
|
|
|
USART0& operator>>( T &RHS )
|
|
|
|
{
|
|
|
|
receive( RHS );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef SECOND_USART
|
|
|
|
|
|
|
|
class USART1 : public USART0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
USART1();
|
|
|
|
~USART1();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-05-20 15:51:01 +02:00
|
|
|
#endif
|