-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUSART.hpp
118 lines (88 loc) · 2.76 KB
/
USART.hpp
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
#pragma once
/*
* File: USART.h
* Author: cameron
*
* Created on March 17, 2011, 11:00 AM
*/
#include "basicTypes.hpp"
#include "undefAVR.hpp"
#include <avr/io.h>
#include <stddef.h>
#define UCSRA _MMIO_BYTE(A + 0)
#define UCSRB _MMIO_BYTE(A + 1)
#define UCSRC _MMIO_BYTE(A + 2)
#define UBRR _MMIO_WORD(A + 4)
#define UBRRL _MMIO_BYTE(A + 4)
#define UBRRH _MMIO_BYTE(A + 5)
#define UDR _MMIO_BYTE(A + 6)
namespace AVR {
using namespace Basic;
template <size_t A>
class USART {
public:
inline static void setBRR(u2 const BBR) { UBRR = BBR; }
inline static u2 getBRR() { return UBRR; }
inline static void set2X() { UCSRA |= 0b10; }
inline static void clr2X() { UCSRA &= ~0b10; }
inline static void setDataRegister(u1 const byte) { UDR = byte; }
inline static u1 getDataRegister() { return UDR; }
inline static bool dataRegisterEmpty() { return UCSRA & 0b00100000; }
inline static bool isTxComplete() { return UCSRA & 0b01000000; }
inline static bool isRxComplete() { return UCSRA & 0b10000000; }
inline static void clearTxCompleteFlag() { UCSRA = UCSRA; }
inline static void disableReInt() { UCSRB &= ~0b00100000; }
inline static void enableReInt() { UCSRB |= 0b00100000; }
inline static void disableTxInt() { UCSRB &= ~0b01000000; }
inline static void enableTxInt() { UCSRB |= 0b01000000; }
inline static void disableRxInt() { UCSRB &= ~0b10000000; }
inline static void enableRxInt() { UCSRB |= 0b10000000; }
inline static void enableTx() { UCSRB |= 0b00001000; }
inline static void disableTx() { UCSRB &= ~0b00001000; }
inline static void enableRx() { UCSRB |= 0b00010000; }
inline static void disableRx() { UCSRB &= ~0b00010000; }
static void send(const u1 byte);
static u1 get();
static void skip(u1 num);
static void getBlock(u1 *array, u2 len);
USART &operator<<(const char byte) {
send(byte);
return *this;
}
USART &operator<<(USART &(*callback)(USART &)) { return callback(*this); }
bool operator==(const char byte) { return get() == byte; }
bool operator!=(const char byte) { return get() != byte; }
USART &operator>>(u1 &byte) {
byte = get();
return *this;
}
USART &operator>>(u2 &word);
void getLittleEndian(u2 &word);
USART &operator<<(const u1 byte);
USART &operator<<(const u2 byte);
private:
};
#undef UCSRA
#undef UCSRB
#undef UCSRC
#undef UBRR
#undef UBRRL
#undef UBRRH
#undef UDR
#ifdef UCSRA
extern USART<(size_t)&UCSRA> usart;
#elif defined(UCSR0A)
extern USART<(size_t)&UCSR0A> usart;
#elif defined(UCSR1A)
extern USART<0xC8> usart;
#endif
#if defined(UCSR1A) && (defined(UCSRA) || defined(UCSR0A))
extern USART<(size_t)&UCSR1A> usart1;
#endif
#ifdef UCSR2A
extern USART<(size_t)&UCSR2A> usart2;
#endif
#ifdef UCSR3A
extern USART<(size_t)&UCSR3A> usart3;
#endif
}; // namespace AVR