This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiring_serial.c
294 lines (245 loc) · 7.03 KB
/
wiring_serial.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright (c) 2005-2006 David A. Mellis
* Modified for Waspmote by Libelium, 2016
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.0
*/
#include "wiring_private.h"
#ifndef __WASPCONSTANTS_H__
#include "WaspConstants.h"
#endif
// Define constants and variables for buffering incoming serial data. We're
// using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read.
#define RX_BUFFER_SIZE_0 512
#define RX_BUFFER_SIZE_1 512
unsigned char rx_buffer0[RX_BUFFER_SIZE_0];
unsigned char rx_buffer1[RX_BUFFER_SIZE_1];
int rx_buffer_head0 = 0;
int rx_buffer_tail0 = 0;
int rx_buffer_head1 = 0;
int rx_buffer_tail1 = 0;
// connects the internal peripheral in the processor and configures it
void beginSerial(long baud, uint8_t portNum)
{
if (portNum == 0) {
setIPF_(IPUSART0);
UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1);
// enable rx and tx
sbi(UCSR0B, RXEN0);
sbi(UCSR0B, TXEN0);
// enable interrupt on complete reception of a byte
sbi(UCSR0B, RXCIE0);
} else {
setIPF_(IPUSART1);
UBRR1H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
UBRR1L = ((F_CPU / 16 + baud / 2) / baud - 1);
// enable rx and tx
sbi(UCSR1B, RXEN1);
sbi(UCSR1B, TXEN1);
// enable interrupt on complete reception of a byte
sbi(UCSR1B, RXCIE1);
}
// defaults to 8-bit, no parity, 1 stop bit
}
// disconnects the internal peripheral in the processor
void closeSerial(uint8_t portNum)
{
if (portNum == 0) {
// turn off the internal peripheral, but also the interface
// resetIPF is just turning off the clock, what is not helping
// to save power, you gotta get rid of all the pull-ups in the sytem
resetIPF_(IPUSART0);
cbi(UCSR0B, RXEN0);
cbi(UCSR0B, TXEN0);
} else {
// turn off the internal peripheral, but also the interface
// resetIPF is just turning off the clock, what is not helping
// to save power, you gotta get rid of all the pull-ups in the sytem
resetIPF_(IPUSART1);
cbi(UCSR1B, RXEN1);
cbi(UCSR1B, TXEN1);
}
}
void serialWrite(unsigned char c, uint8_t portNum)
{
if (portNum == 0) {
while (!(UCSR0A & (1 << UDRE0)))
;
UDR0 = c;
} else {
while (!(UCSR1A & (1 << UDRE1)))
;
UDR1 = c;
}
}
int serialAvailable(uint8_t portNum)
{
if (portNum == 0)
return (RX_BUFFER_SIZE_0 + rx_buffer_head0 - rx_buffer_tail0) % RX_BUFFER_SIZE_0;
else
return (RX_BUFFER_SIZE_1 + rx_buffer_head1 - rx_buffer_tail1) % RX_BUFFER_SIZE_1;
}
int serialRead(uint8_t portNum)
{
if (portNum == 0) {
// if the head isn't ahead of the tail, we don't have any characters
if (rx_buffer_head0 == rx_buffer_tail0) {
return -1;
} else {
unsigned char c = rx_buffer0[rx_buffer_tail0];
rx_buffer_tail0 = (rx_buffer_tail0 + 1) % RX_BUFFER_SIZE_0;
return c;
}
}
else {
// if the head isn't ahead of the tail, we don't have any characters
if (rx_buffer_head1 == rx_buffer_tail1) {
return -1;
} else {
unsigned char c = rx_buffer1[rx_buffer_tail1];
rx_buffer_tail1 = (rx_buffer_tail1 + 1) % RX_BUFFER_SIZE_1;
return c;
}
}
}
void serialFlush(uint8_t portNum)
{
// don't reverse this or there may be problems if the RX interrupt
// occurs after reading the value of rx_buffer_head but before writing
// the value to rx_buffer_tail; the previous value of rx_buffer_head
// may be written to rx_buffer_tail, making it appear as if the buffer
// were full, not empty.
if (portNum == 0){
rx_buffer_tail0=0;
rx_buffer_head0 = rx_buffer_tail0;
}
else{
rx_buffer_tail1=0;
rx_buffer_head1 = rx_buffer_tail1;
}
}
SIGNAL(USART0_RX_vect)
{
unsigned char c = UDR0;
int i = (rx_buffer_head0 + 1) % RX_BUFFER_SIZE_0;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != rx_buffer_tail0) {
rx_buffer0[rx_buffer_head0] = c;
rx_buffer_head0 = i;
}
}
SIGNAL(USART1_RX_vect)
{
unsigned char c = UDR1;
int i = (rx_buffer_head1 + 1) % RX_BUFFER_SIZE_1;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != rx_buffer_tail1) {
rx_buffer1[rx_buffer_head1] = c;
rx_buffer_head1 = i;
}
}
void printMode(int mode, uint8_t portNum)
{
// do nothing, we only support serial printing, not lcd.
}
void printByte(unsigned char c, uint8_t portNum)
{
serialWrite(c, portNum);
}
void printNewline(uint8_t portNum)
{
printByte('\r', portNum);
printByte('\n', portNum);
}
void printString(const char *s, uint8_t portNum)
{
while (*s)
printByte(*s++, portNum);
}
void printIntegerInBase(unsigned long n, unsigned long base, uint8_t portNum)
{
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long i = 0;
if (n == 0) {
printByte('0', portNum);
return;
}
while (n > 0) {
buf[i++] = n % base;
n /= base;
}
for (; i > 0; i--)
printByte(buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10, portNum);
}
void puthex(char ch, uint8_t portNum) {
char ah,al;
ah = (ch & 0xf0) >> 4;
if(ah >= 0x0a) {
ah = ah - 0x0a + 'A';
} else {
ah += '0';
}
al = (ch & 0x0f);
if(al >= 0x0a) {
al = al - 0x0a + 'A';
} else {
al += '0';
}
printByte(ah, portNum);
printByte(al, portNum);
}
void printInteger(long n, uint8_t portNum)
{
if (n < 0) {
printByte('-', portNum);
n = -n;
}
printIntegerInBase(n, 10, portNum);
}
void printHex(unsigned long n, uint8_t portNum)
{
printIntegerInBase(n, 16, portNum);
}
void printOctal(unsigned long n, uint8_t portNum)
{
printIntegerInBase(n, 8, portNum);
}
void printBinary(unsigned long n, uint8_t portNum)
{
printIntegerInBase(n, 2, portNum);
}
/* Including print() adds approximately 1500 bytes to the binary size,
* so we replace it with the smaller and less-confusing printString(),
* printInteger(), etc.
void print(const char *format, ...)
{
char buf[256];
va_list ap;
va_start(ap, format);
vsnprintf(buf, 256, format, ap);
va_end(ap);
printString(buf);
}
*/