-
Notifications
You must be signed in to change notification settings - Fork 0
/
saba_usart.h
123 lines (99 loc) · 2.87 KB
/
saba_usart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* saba_usart.h
*
* Saarbastler AVR C++ 11 Library
*
* Created: 12.10.2017
* Author: Joerg
*
* Uart templates
*/
#include <avr/io.h>
#include <saba_avr.h>
#include "Device.h"
#ifndef SABA_USART_H_
#define SABA_USART_H_
namespace SABA
{
// \brief Usart controlling class
/**
The Usart template class is used to control an AVR Usart. There are predefined typedefs USARTx in saba_controller.h
@tparam _UDR the UDR SFR address
@tparam _UCSRA the UCSRA address
@tparam _UCSRB the UCSRB address
@tparam _UCSRC the UCSRC address
@tparam _UBRRL the UBRRL address
@tparam _UBRRH the UBRRH address
Usage:
~~~{.c}
SABA::USART0 usart(19200); // use one of the defined typedefs
~~~
*/
template <SFRA _UDR,SFRA _UCSRA,SFRA _UCSRB,SFRA _UCSRC,SFRA _UBRRL,SFRA _UBRRH
,uint8_t _RXEN,uint8_t _TXEN,uint8_t _UCSZ0,uint8_t _UCSZ1,uint8_t _TXC,uint8_t _RXC,uint8_t _UDRE>
class Usart
{
public:
Usart(uint32_t baudrate) //! initializes the Usart using the desired baudrate
{
uint16_t ubrr= (uint16_t)(.5 + F_CPU / (baudrate * 16))-1;
SFREG<_UCSRA> ucsra;
SFREG<_UCSRB> ucsrb;
SFREG<_UCSRC> ucsrc;
SFREG<_UBRRL> ubrrl;
SFREG<_UBRRH> ubrrh;
ubrrl= (uint8_t)ubrr & (uint8_t)0xff;
ubrrh= ubrr >> 8;
ucsra= 0;
ucsrb= BIT(_RXEN)|BIT(_TXEN);
// Atmega 8
#ifdef URSEL
ucsrc= BIT(URSEL) | BIT(UCSZ0) | BIT(UCSZ1);
#else
ucsrc= BIT(_UCSZ0) | BIT(_UCSZ1);
#endif
}
bool transmitComplete() //! return true, if the last transmit was completed
{
SFRBIT<_UCSRA,_TXC> txc;
return txc();
}
bool readyToSend() //! return true, if the data register is empty, the next char can be written
{
SFRBIT<_UCSRA,_UDRE> udre;
return udre();
}
void putch( uint8_t ch ) //! waits until data register is empty and transmits the byte
{
while(! readyToSend())
;
SFREG<_UDR> udr;
udr= ch;
}
bool receiverComplete() //! return true, if data has been received complete, the data register can be read
{
SFRBIT<_UCSRA,_RXC> rxc;
return rxc();
}
uint8_t getch() //! wait until a receive was completed, then return the received char
{
while(!receiverComplete())
;
SFREG<_UDR> udr;
return udr();
}
};
#ifdef UDR
//! Atmega 8
typedef Usart<(SFRA)&UDR,(SFRA)&UCSRA,(SFRA)&UCSRB,(SFRA)&UCSRC,(SFRA)&UBRRL,(SFRA)&UBRRH,RXEN,TXEN,UCSZ0,UCSZ1,TXC,RXC,UDRE> USART0;
#endif
#ifdef UDR0
//! Atmega xx8
typedef Usart<(SFRA)&UDR0,(SFRA)&UCSR0A,(SFRA)&UCSR0B,(SFRA)&UCSR0C,(SFRA)&UBRR0L,(SFRA)&UBRR0H,RXEN0,TXEN0,UCSZ00,UCSZ01,TXC0,RXC0,UDRE0> USART0;
#endif
#ifdef UDR1
//! Atmega xx8
typedef Usart<(SFRA)&UDR1,(SFRA)&UCSR1A,(SFRA)&UCSR1B,(SFRA)&UCSR1C,(SFRA)&UBRR1L,(SFRA)&UBRR1H,RXEN1,TXEN1,UCSZ10,UCSZ11,TXC1,RXC1,UDRE1> USART1;
#endif
}
#endif // SABA_USART_H_