-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserial.cpp
More file actions
227 lines (178 loc) · 3.91 KB
/
serial.cpp
File metadata and controls
227 lines (178 loc) · 3.91 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <avr/io.h>
#include <stdlib.h>
#include "serial.h"
uart::uart(USART_t* usart, const uint32_t baud) : reg(usart)
{
//Make usart's tx pin output (supports xmega a4 series)
if (usart == &USARTC0)
{
PORTC.OUTSET = PIN3_bm;
PORTC.DIRSET = PIN3_bm;
}
else if (usart == &USARTC1)
{
PORTC.OUTSET = PIN7_bm;
PORTC.DIRSET = PIN7_bm;
}
else if (usart == &USARTD0)
{
PORTD.OUTSET = PIN3_bm;
PORTD.DIRSET = PIN3_bm;
}
else if (usart == &USARTD1)
{
PORTD.OUTSET = PIN7_bm;
PORTD.DIRSET = PIN7_bm;
}
else if (usart == &USARTE0)
{
PORTE.OUTSET = PIN3_bm;
PORTE.DIRSET = PIN3_bm;
}
setBaudRate(baud);
// Set USART Format to 8bit, no parity, 1 stop bit
reg->CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;
// Enable both RX and TX.
reg->CTRLB = USART_RXEN_bm | USART_TXEN_bm;
// Eanble rx interrupt and disable tx, becouse we don't send data yet
reg->CTRLA = USART_RXCINTLVL_HI_gc | USART_DREINTLVL_OFF_gc;
}
void uart::flush()
{
while(!txBuffer.isEmpty());
}
void uart::sendChar(const char c)
{
while(txBuffer.isFull());
txBuffer.write(c);
//Enable interrupt for sending data
reg->CTRLA = USART_RXCINTLVL_HI_gc | USART_DREINTLVL_HI_gc;
}
void uart::sendString(const char *str)
{
while (*str)
sendChar(*str++);
}
void uart::sendStringPgm(const char *str)
{
while (pgm_read_byte(str))
sendChar(pgm_read_byte(str++));
}
uint8_t uart::sendStringNonBlocking(const char *str)
{
uint8_t charsSent = 0;
while (*str)
{
if(txBuffer.isFull()) return charsSent;
txBuffer.write(*str++);
charsSent++;
reg->CTRLA = USART_RXCINTLVL_HI_gc | USART_DREINTLVL_HI_gc;
}
return charsSent;
}
uint8_t uart::sendStringPgmNonBlocking(const char *str)
{
uint8_t charsSent = 0;
while (pgm_read_byte(str))
{
if(txBuffer.isFull()) return charsSent;
txBuffer.write(pgm_read_byte(str++));
charsSent++;
reg->CTRLA = USART_RXCINTLVL_HI_gc | USART_DREINTLVL_HI_gc;
}
return charsSent;
}
void uart::sendInt(const int32_t i)
{
char itoa_tmp[12];
ltoa (i, itoa_tmp, 10);
sendString(itoa_tmp);
}
void uart::sendInt(const int16_t i)
{
char itoa_tmp[7];
itoa (i, itoa_tmp, 10);
sendString(itoa_tmp);
}
void uart::sendInt(const int8_t i)
{
char itoa_tmp[5];
itoa (i, itoa_tmp, 10);
sendString(itoa_tmp);
}
void uart::sendInt(const uint32_t i)
{
char itoa_tmp[11];
ltoa (i, itoa_tmp, 10);
sendString(itoa_tmp);
}
void uart::sendInt(const uint16_t i)
{
char itoa_tmp[6];
itoa (i, itoa_tmp, 10);
sendString(itoa_tmp);
}
void uart::sendInt(const uint8_t i)
{
char itoa_tmp[4];
itoa (i, itoa_tmp, 10);
sendString(itoa_tmp);
}
void uart::sendHex(const uint32_t i)
{
sendHex((uint16_t)(i >> 16));
sendHex((uint16_t) i);
}
void uart::sendHex(const uint16_t i)
{
sendHex((uint8_t)(i >> 8));
sendHex((uint8_t) i);
}
void uart::sendHex(const uint8_t i)
{
char h = (i >> 4);
if(h > 9) h += 55;
else h += 48;
sendChar(h);
char l = (i & 0x0F);
if(l > 9) l += 55;
else l += 48;
sendChar(l);
}
void uart::sendHex(const int32_t i)
{
sendHex((uint32_t) i);
}
void uart::sendHex(const int16_t i)
{
sendHex((uint16_t) i);
}
void uart::sendHex(const int8_t i)
{
sendHex((uint8_t) i);
}
uint8_t uart::dataAvailable()
{
return rxBuffer.size();
}
char uart::getChar()
{
return rxBuffer.read();
}
uint8_t uart::setBaudRate(const uint32_t baud)
{
uint32_t div1k;
uint8_t bscale = 0;
uint16_t bsel;
if (baud > (F_CPU/16)) return 0;
div1k = ((F_CPU*64) / baud) - 1024;
while ((div1k < 2096640) && (bscale < 7))
{
bscale++;
div1k <<= 1;
}
bsel = div1k >> 10;
reg->BAUDCTRLA = bsel&0xff;
reg->BAUDCTRLB = ((16-bscale) << 4 ) | (bsel >> 8);
return 1;
}