-
Notifications
You must be signed in to change notification settings - Fork 0
/
usart_lib.c
547 lines (543 loc) · 16.1 KB
/
usart_lib.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* usart_lib.c
*
* Created on: 08 mar 2016
* Author: Piotr Rudzki ryba.lodz@gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "usart_lib.h"
#if defined (USE_USART0_INTERRUPT)
uint8_t us0rxBuff[USART0_RX_BUFFER_LENGTH]; // receiver data buffer
volatile fifo_T usart0rxBuffer = { .data = us0rxBuff, }; // receiver circular buffer
uint8_t us0txBuff[USART0_TX_BUFFER_LENGTH]; // transmitter data buffer
fifo_T us0txStruct = { .data = us0txBuff, }; // transmitter circular buffer
volatile usartTxBuffer_T usart0txBuffer = { .buffer = &us0txStruct, .status =
STOPPED, }; // transmitter structure
#endif /* defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1_INTERRUPT)
uint8_t us1rxBuff[USART1_RX_BUFFER_LENGTH]; // receiver data buffer
volatile fifo_T usart1rxBuffer = { .data = us1rxBuff, }; // receiver circular buffer
uint8_t us1txBuff[USART1_TX_BUFFER_LENGTH]; // transmitter data buffer
fifo_T us1txStruct = { .data = us1txBuff, }; // transmitter circular buffer
volatile usartTxBuffer_T usart1txBuffer = { .buffer = &us1txStruct, .status =
STOPPED, };
#endif /* defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2_INTERRUPT)
uint8_t us2rxBuff[USART2_RX_BUFFER_LENGTH]; // receiver data buffer
volatile fifo_T usart2rxBuffer = { .data = us2rxBuff, }; // receiver circular buffer
uint8_t us2txBuff[USART2_TX_BUFFER_LENGTH]; // transmitter data buffer
fifo_T us2txStruct = { .data = us2txBuff, }; // transmitter circular buffer
volatile usartTxBuffer_T usart2txBuffer = { .buffer = &us2txStruct, .status =
STOPPED, };
#endif /* defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3_INTERRUPT)
uint8_t us3rxBuff[USART3_RX_BUFFER_LENGTH]; // receiver data buffer
volatile fifo_T usart3rxBuffer = { .data = us3rxBuff, }; // receiver circular buffer
uint8_t us3txBuff[USART3_TX_BUFFER_LENGTH]; // transmitter data buffer
fifo_T us3txStruct = { .data = us3txBuff, }; // transmitter circular buffer
volatile usartTxBuffer_T usart3txBuffer = { .buffer = &us3txStruct, .status =
STOPPED, };
#endif /* defined (USE_USART3_INTERRUPT) */
// USART initialization
void usartInit(usartNumber_T const usartNumber, uint16_t const ubrrValue) {
switch (usartNumber) {
#if defined (USE_USART0) || defined (USE_USART0_INTERRUPT)
case USART0:
// set baud rate
USART0_SET_UBRRH(ubrrValue);
USART0_SET_UBRRL(ubrrValue);
if (ubrrValue & 0x8000) {
USART0_SET_U2X;
} else {
USART0_CLR_U2X;
}
// enable receiver and transmitter
USART0_ENABLE_RXTX;
// 8N1 (8 bit data, no parity, one stop)
USART0_8N1_FRAME_FORMAT;
break;
#endif /* defined (USE_USART0) || defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1) || defined (USE_USART1_INTERRUPT)
case USART1:
// set baud rate
USART1_SET_UBRRH(ubrrValue);
USART1_SET_UBRRL(ubrrValue);
if ((ubrrValue & 0x10000)) {
USART1_SET_U2X;
} else {
USART1_CLR_U2X;
}
// enable receiver and transmitter
USART1_ENABLE_RXTX;
// 8N1 (8 bit data, no parity, one stop)
USART1_8N1_FRAME_FORMAT;
break;
#endif /* defined (USE_USART1) || defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2) || defined (USE_USART2_INTERRUPT)
case USART2:
// set baud rate
USART2_SET_UBRRH(ubrrValue);
USART2_SET_UBRRL(ubrrValue);
if (ubrrValue & 0x10000) {
USART2_SET_U2X;
} else {
USART2_CLR_U2X;
}
// enable receiver and transmitter
USART2_ENABLE_RXTX;
// 8N1 (8 bit data, no parity, one stop)
USART2_8N1_FRAME_FORMAT;
break;
#endif /* defined (USE_USART2) || defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3) || defined (USE_USART3_INTERRUPT)
case USART3:
// set baud rate
USART3_SET_UBRRH(ubrrValue);
USART3_SET_UBRRL(ubrrValue);
if (ubrrValue & 0x10000) {
USART3_SET_U2X;
} else {
USART3_CLR_U2X;
}
// enable receiver and transmitter
USART3_ENABLE_RXTX;
// 8N1 (8 bit data, no parity, one stop)
USART3_8N1_FRAME_FORMAT;
break;
#endif /* defined (USE_USART3) || defined (USE_USART3_INTERRUPT) */
default:
break;
}
}
// Normal mode functions
// Receiver
uint8_t usartDataReceived(usartNumber_T const usartNumber) {
uint8_t tmp = 0;
switch (usartNumber) {
#if defined (USE_USART0)
case USART0:
tmp = USART0_TEST_RXC;
break;
#endif /* defined (USE_USART0) */
#if defined (USE_USART1)
case USART1:
tmp = USART1_TEST_RXC;
break;
#endif /* defined (USE_USART1) */
#if defined (USE_USART2)
case USART2:
tmp = USART2_TEST_RXC;
break;
#endif /* defined (USE_USART2) */
#if defined (USE_USART3)
case USART3:
tmp = USART3_TEST_RXC;
break;
#endif /* defined (USE_USART3) */
default:
break;
}
return tmp;
}
uint8_t usartImGetByte(usartNumber_T const usartNumber) {
uint8_t tmp = 0;
switch (usartNumber) {
#if defined (USE_USART0)
case USART0:
tmp = USART0_DATA_REG;
break;
#endif /* defined (USE_USART0) */
#if defined (USE_USART1)
case USART1:
tmp = USART1_DATA_REG;
break;
#endif /* defined (USE_USART1) */
#if defined (USE_USART2)
case USART2:
tmp = USART2_DATA_REG;
break;
#endif /* defined (USE_USART2) */
#if defined (USE_USART3)
case USART3:
tmp = USART3_DATA_REG;
break;
#endif /* defined (USE_USART3) */
default:
break;
}
return tmp;
}
uint8_t usartGetByte(usartNumber_T const usartNumber) {
while (!(usartDataReceived(usartNumber)))
;
return usartImGetByte(usartNumber);
}
// Transmitter
uint8_t usartDataTransferred(usartNumber_T const usartNumber) {
uint8_t tmp = 0;
switch (usartNumber) {
#if defined (USE_USART0)
case USART0:
tmp = USART0_TEST_TXC;
break;
#endif /* defined (USE_USART0) */
#if defined (USE_USART1)
case USART1:
tmp = USART1_TEST_TXC;
break;
#endif /* defined (USE_USART1) */
#if defined (USE_USART2)
case USART2:
tmp = USART2_TEST_TXC;
break;
#endif /* defined (USE_USART2) */
#if defined (USE_USART3)
case USART3:
tmp = USART3_TEST_TXC;
break;
#endif /* defined (USE_USART3) */
default:
break;
}
return tmp;
}
void usartImPutByte(usartNumber_T const usartNumber, uint8_t const data) {
switch (usartNumber) {
#if defined (USE_USART0) || defined (USE_USART0_INTERRUPT)
case USART0:
USART0_DATA_REG = data;
break;
#endif /* defined (USE_USART0) || defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1) || defined (USE_USART1_INTERRUPT)
case USART1:
USART1_DATA_REG = data;
break;
#endif /* defined (USE_USART1) || defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2) || defined (USE_USART2_INTERRUPT)
case USART2:
USART2_DATA_REG = data;
break;
#endif /* defined (USE_USART2) || defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3) || defined (USE_USART3_INTERRUPT)
case USART3:
USART3_DATA_REG = data;
break;
#endif /* defined (USE_USART3) || defined (USE_USART3_INTERRUPT) */
default:
break;
}
}
void usartPutByte(usartNumber_T const usartNumber, uint8_t const data) {
while (!(usartDataTransferred(usartNumber)))
;
usartImPutByte(usartNumber, data);
}
// Interrupt mode
#if defined (USE_USART0_INTERRUPT) || defined (USE_USART1_INTERRUPT)\
|| defined (USE_USART2_INTERRUPT) || defined (USE_USART3_INTERRUPT)
// Get byte from buffer. Returns data or -1 if buffer empty. Internal function.
int16_t __usartGetByteFromBuffer(volatile fifo_T * const buffer,
uint8_t const bufferMask) {
uint8_t tmp = 0;
if (buffer->tail != buffer->head) { // if buffer not empty
tmp = buffer->data[buffer->tail]; // get data from buffer
buffer->tail = (buffer->tail + 1) & bufferMask; // move buffer tail
return tmp; // return data
}
return -1; // return buffer empty
}
// Put byte in buffer. Returns -1 if buffer full. Internal function.
int8_t __usartPutByteToBuffer(uint8_t const data, volatile fifo_T * const buffer,
uint8_t const bufferMask) {
uint8_t next = (buffer->head + 1) & bufferMask; // get next byte index in buffer
if (next != buffer->tail) { // if there is room in buffer
buffer->data[buffer->head] = data; // put data in buffer
buffer->head = next; // move head
return 0;
}
return -1;
}
// Receiver
int16_t usartGetByteFromReceiveBuffer(usartNumber_T const usartNumber) {
volatile fifo_T * buffer = 0;
uint8_t bufferMask = 0;
switch (usartNumber) {
#if defined (USE_USART0_INTERRUPT)
case USART0:
buffer = &usart0rxBuffer;
bufferMask = USART0_RX_BUFFER_MASK;
break;
#endif /* defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1_INTERRUPT)
case USART1:
buffer = &usart1rxBuffer;
bufferMask = USART1_RX_BUFFER_MASK;
break;
#endif /* defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2_INTERRUPT)
case USART2:
buffer = &usart2rxBuffer;
bufferMask = USART2_RX_BUFFER_MASK;
break;
#endif /* defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3_INTERRUPT)
case USART3:
buffer = &usart3rxBuffer;
bufferMask = USART3_RX_BUFFER_MASK;
break;
#endif /* defined (USE_USART3_INTERRUPT) */
default:
break;
}
return (__usartGetByteFromBuffer(buffer, bufferMask));
}
// Callback function
_usartFctPtr_T _rxDataReadyCallback;
// Callback function
_usartFctPtr_T _rxBufferFullCallback;
void registerRxDataReadyCallback(_usartFctPtr_T callback) {
_rxDataReadyCallback = callback;
}
void registerRxBufferFullCallback(_usartFctPtr_T callback) {
_rxBufferFullCallback = callback;
}
// start interrupt based receiver
void usartRxStart(usartNumber_T usartNumber) {
switch (usartNumber) {
#if defined (USE_USART0_INTERRUPT)
case USART0:
USART0_SET_RXCIE;
break;
#endif /* defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1_INTERRUPT)
case USART1:
USART1_SET_RXCIE;
break;
#endif /* defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2_INTERRUPT)
case USART2:
USART2_SET_RXCIE;
break;
#endif /* defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3_INTERRUPT)
case USART3:
USART3_SET_RXCIE;
break;
#endif /* defined (USE_USART3_INTERRUPT) */
default:
break;
}
}
// Transmitter
int8_t usartPutByteToTransmitBuffer(usartNumber_T const usartNumber, uint8_t const data) {
volatile fifo_T * buffer = 0;
uint8_t bufferMask = 0;
switch (usartNumber) {
#if defined (USE_USART0_INTERRUPT)
case USART0:
buffer = usart0txBuffer.buffer;
bufferMask = USART0_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1_INTERRUPT)
case USART1:
buffer = usart1txBuffer.buffer;
bufferMask = USART1_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2_INTERRUPT)
case USART2:
buffer = usart2txBuffer.buffer;
bufferMask = USART2_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3_INTERRUPT)
case USART3:
buffer = usart3txBuffer.buffer;
bufferMask = USART3_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART3_INTERRUPT) */
default:
break;
}
return (__usartPutByteToBuffer(data, buffer, bufferMask));
}
// Callback function
void (*_txCompleteCallback)(usartNumber_T const usartNumber);
void registerTxCompleteCallback(_usartFctPtr_T callback) {
_txCompleteCallback = callback;
}
// Start sending data from buffer.
void usartTxStart(usartNumber_T const usartNumber) {
volatile usartTxBuffer_T * tmpPtr = 0;
uint8_t bufferMask = 0;
int16_t tmp = 0;
switch (usartNumber) {
#if defined (USE_USART0_INTERRUPT)
case USART0:
USART0_SET_TXCIE;
tmpPtr = &usart0txBuffer;
bufferMask = USART0_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1_INTERRUPT)
case USART1:
USART1_SET_TXCIE;
tmpPtr = &usart1txBuffer;
bufferMask = USART1_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2_INTERRUPT)
case USART2:
USART2_SET_TXCIE;
tmpPtr = &usart2txBuffer;
bufferMask = USART2_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3_INTERRUPT)
case USART3:
USART3_SET_TXCIE;
tmpPtr = &usart3txBuffer;
bufferMask = USART3_TX_BUFFER_MASK;
break;
#endif /* defined (USE_USART3_INTERRUPT) */
default:
break;
}
if (tmpPtr->status == STOPPED) { // if transmitter not working
tmp = __usartGetByteFromBuffer(tmpPtr->buffer, bufferMask); // get data from buffer
if (tmp > -1) { // if buffer not empty
tmpPtr->status = STARTED; // set transmitter working flag
usartImPutByte(usartNumber, tmp); // send first byte
}
}
}
#endif /* defined (USE_USART0_INTERRUPT) || defined (USE_USART1_INTERRUPT) || defined (USE_USART2_INTERRUPT) || defined (USE_USART3_INTERRUPT) */
#if defined (USE_USART0_INTERRUPT)
ISR(USART0_RX_ISR) {
// if there is room for next byte in buffer
if (__usartPutByteToBuffer(USART0_DATA_REG, &usart0rxBuffer, USART0_RX_BUFFER_MASK)
> -1) {
if (_rxDataReadyCallback) {
(*_rxDataReadyCallback)(USART0); // new data ready
}
} else {
if (_rxBufferFullCallback) {
// next received byte generate buffer overrun
(*_rxBufferFullCallback)(USART0);
}
}
}
ISR(USART0_TX_ISR) {
int16_t tmp = __usartGetByteFromBuffer(usart0txBuffer.buffer,
USART0_TX_BUFFER_MASK); // get data from buffer
if (tmp > -1) { // if buffer was not empty
USART0_DATA_REG = tmp; // send
} else {
usart0txBuffer.status = STOPPED; // set transmitter stop flag
USART0_CLR_TXCIE; // disable USART TX interrupt
if (_txCompleteCallback) {
(*_txCompleteCallback)(USART0); // transmit complete
}
}
}
#endif /* defined (USE_USART0_INTERRUPT) */
#if defined (USE_USART1_INTERRUPT)
ISR(USART1_RX_ISR) {
// if there is room for next byte in buffer
if (__usartPutByteToBuffer(USART1_DATA_REG, &usart1rxBuffer, USART1_RX_BUFFER_MASK)
> -1) {
if (_rxDataReadyCallback) {
(*_rxDataReadyCallback)(USART1); // new data ready
}
} else {
if (_rxBufferFullCallback) {
// next received byte generate buffer overrun
(*_rxBufferFullCallback)(USART1);
}
}
}
ISR(USART1_TX_ISR) {
int16_t tmp = __usartGetByteFromBuffer(usart1txBuffer.buffer,
USART1_TX_BUFFER_MASK); // get data from buffer
if (tmp > -1) { // if buffer was not empty
USART1_DATA_REG = tmp; // send
} else {
usart1txBuffer.status = STOPPED; // set transmitter stop flag
USART1_CLR_TXCIE; // disable USART TX interrupt
if (_txCompleteCallback) {
(*_txCompleteCallback)(USART1); // transmit complete
}
}
}
#endif /* defined (USE_USART1_INTERRUPT) */
#if defined (USE_USART2_INTERRUPT)
ISR(USART2_RX_ISR) {
// if there is room for next byte in buffer
if (__usartPutByteToBuffer(USART2_DATA_REG, &usart2rxBuffer, USART2_RX_BUFFER_MASK)
> -1) {
if (_rxDataReadyCallback) {
(*_rxDataReadyCallback)(USART2); // new data ready
}
} else {
if (_rxBufferFullCallback) {
// next received byte generate buffer overrun
(*_rxBufferFullCallback)(USART2);
}
}
}
ISR(USART2_TX_ISR) {
int16_t tmp = __usartGetByteFromBuffer(usart2txBuffer.buffer,
USART2_TX_BUFFER_MASK); // get data from buffer
if (tmp > -1) { // if buffer was not empty
USART2_DATA_REG = tmp; // send
} else {
usart2txBuffer.status = STOPPED; // set transmitter stop flag
USART2_CLR_TXCIE; // disable USART TX interrupt
if (_txCompleteCallback) {
(*_txCompleteCallback)(USART2); // transmit complete
}
}
}
#endif /* defined (USE_USART2_INTERRUPT) */
#if defined (USE_USART3_INTERRUPT)
ISR(USART3_RX_ISR) {
// if there is room for next byte in buffer
if (__usartPutByteToBuffer(USART3_DATA_REG, &usart3rxBuffer, USART3_RX_BUFFER_MASK)
> -1) {
if (_rxDataReadyCallback) {
(*_rxDataReadyCallback)(USART3); // new data ready
}
} else {
if (_rxBufferFullCallback) {
// next received byte generate buffer overrun
(*_rxBufferFullCallback)(USART3);
}
}
}
ISR(USART3_TX_ISR) {
int16_t tmp = __usartGetByteFromBuffer(usart3txBuffer.buffer,
USART3_TX_BUFFER_MASK); // get data from buffer
if (tmp > -1) { // if buffer was not empty
USART3_DATA_REG = tmp; // send
} else {
usart3txBuffer.status = STOPPED; // set transmitter stop flag
USART3_CLR_TXCIE; // disable USART TX interrupt
if (_txCompleteCallback) {
(*_txCompleteCallback)(USART3); // transmit complete
}
}
}
#endif /* defined (USE_USART3_INTERRUPT) */