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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
/*
ChibiOS - Copyright (C) 2016 Andrew Wygle aka awygle
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @file MSP430X/hal_serial_lld.c
* @brief MSP430X serial subsystem low level driver source.
*
* @addtogroup SERIAL
* @{
*/
#include "hal.h"
#if (HAL_USE_SERIAL == TRUE) || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/** @brief USART0 serial driver identifier.*/
#if (MSP430X_SERIAL_USE_USART0 == TRUE) || defined(__DOXYGEN__)
#ifndef __MSP430_HAS_EUSCI_A0__
#error "Cannot find USCI module to use for SD0"
#endif
#ifdef MSP430X_USCI_A0_USED
#error "USCI module A0 already in use - USART0 not available"
#endif
SerialDriver SD0;
#define MSP430X_USCI_A0_USED
#endif
/** @brief USART1 serial driver identifier.*/
#if (MSP430X_SERIAL_USE_USART1 == TRUE) || defined(__DOXYGEN__)
#ifndef __MSP430_HAS_EUSCI_A1__
#error "Cannot find USCI module to use for SD1"
#endif
#ifdef MSP430X_USCI_A1_USED
#error "USCI module A1 already in use - USART1 not available"
#endif
SerialDriver SD1;
#define MSP430X_USCI_A1_USED
#endif
/** @brief USART2 serial driver identifier.*/
#if (MSP430X_SERIAL_USE_USART2 == TRUE) || defined(__DOXYGEN__)
#ifndef __MSP430_HAS_EUSCI_A2__
#error "Cannot find USCI module to use for SD2"
#endif
#ifdef MSP430X_USCI_A2_USED
#error "USCI module A2 already in use - USART2 not available"
#endif
SerialDriver SD2;
#define MSP430X_USCI_A2_USED
#endif
/** @brief USART3 serial driver identifier.*/
#if (MSP430X_SERIAL_USE_USART3 == TRUE) || defined(__DOXYGEN__)
#ifndef __MSP430_HAS_EUSCI_A3__
#error "Cannot find USCI module to use for SD3"
#endif
#ifdef MSP430X_USCI_A3_USED
#error "USCI module A3 already in use - USART3 not available"
#endif
SerialDriver SD3;
#define MSP430X_USCI_A3_USED
#endif
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/**
* @brief Driver default configuration.
*/
static const SerialConfig default_config = { SERIAL_DEFAULT_BITRATE };
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/**
* @brief UCBRS calculation.
* @details This function calculates the UCBRS value for oversampled baud
* rates.
*
* @param[in] frac Fractional part of baud rate division, times 10000.
*/
static uint8_t UCBRS(uint16_t frac) {
/* TODO there must be a better way */
if (frac < 529)
return 0x00;
else if (frac < 715)
return 0x01;
else if (frac < 835)
return 0x02;
else if (frac < 1001)
return 0x04;
else if (frac < 1252)
return 0x08;
else if (frac < 1430)
return 0x10;
else if (frac < 1670)
return 0x20;
else if (frac < 2147)
return 0x11;
else if (frac < 2224)
return 0x21;
else if (frac < 2503)
return 0x22;
else if (frac < 3000)
return 0x44;
else if (frac < 3335)
return 0x25;
else if (frac < 3575)
return 0x49;
else if (frac < 3753)
return 0x4A;
else if (frac < 4003)
return 0x52;
else if (frac < 4286)
return 0x92;
else if (frac < 4378)
return 0x53;
else if (frac < 5002)
return 0x55;
else if (frac < 5715)
return 0xAA;
else if (frac < 6003)
return 0x6B;
else if (frac < 6254)
return 0xAD;
else if (frac < 6432)
return 0xB5;
else if (frac < 6667)
return 0xB6;
else if (frac < 7001)
return 0xD6;
else if (frac < 7147)
return 0xB7;
else if (frac < 7503)
return 0xBB;
else if (frac < 7861)
return 0xDD;
else if (frac < 8004)
return 0xED;
else if (frac < 8333)
return 0xEE;
else if (frac < 8464)
return 0xBF;
else if (frac < 8572)
return 0xDF;
else if (frac < 8751)
return 0xEF;
else if (frac < 9004)
return 0xF7;
else if (frac < 9170)
return 0xFB;
else if (frac < 9288)
return 0xFD;
else
return 0xFE;
}
/**
* @brief Modulation control word calculator.
* @details This function calculates the modulation control word from the
* input clock frequency and the requested baud rate.
*
* @param[in] baud Requested baud rate
* @param[in] freq Frequency of the clock driving the USCI module
*/
static uint16_t UCAxMCTLW(uint32_t baud, uint32_t freq) {
uint16_t n = freq / baud;
/*uint16_t frac = (freq * 10000 / baud) - ((freq / baud) * 10000);*/
uint16_t frac = (freq - (n * baud)) * 10000 / baud;
if (n > 16) {
while (n > 16) {
n -= 16;
}
return (UCBRS(frac) << 8) | (n << 4) | UCOS16;
}
return UCBRS(frac) << 8;
}
/**
* @brief UCBRW calculation.
* @details This function calculates the UCBRW value for all baud
* rates.
*
* @param[in] baud Requested baud rate
* @param[in] freq Frequency of the clock driving the USCI module
*/
static uint16_t UCAxBRW(uint32_t baud, uint32_t freq) {
uint16_t n = freq / baud;
if (n > 16) {
return n >> 4;
}
return n;
}
#if (MSP430X_SERIAL_USE_USART0 == TRUE) || defined(__DOXYGEN__)
static void usart0_init(const SerialConfig * config) {
UCA0BRW = UCAxBRW(config->sc_bitrate, MSP430X_USART0_CLK_FREQ);
UCA0MCTLW = UCAxMCTLW(config->sc_bitrate, MSP430X_USART0_CLK_FREQ);
UCA0STATW = 0;
UCA0ABCTL = 0;
UCA0IRCTL = 0;
UCA0CTLW0 = (MSP430X_USART0_PARITY << 14) | (MSP430X_USART0_ORDER << 13) |
(MSP430X_USART0_SIZE << 12) | (MSP430X_USART0_STOP << 11) |
(MSP430X_USART0_UCSSEL);
UCA0IE = UCRXIE;
}
#endif
#if (MSP430X_SERIAL_USE_USART1 == TRUE) || defined(__DOXYGEN__)
static void usart1_init(const SerialConfig * config) {
UCA1BRW = UCAxBRW(config->sc_bitrate, MSP430X_USART1_CLK_FREQ);
UCA1MCTLW = UCAxMCTLW(config->sc_bitrate, MSP430X_USART1_CLK_FREQ);
UCA1STATW = 0;
UCA1ABCTL = 0;
UCA1IRCTL = 0;
UCA1CTLW0 = (MSP430X_USART1_PARITY << 14) | (MSP430X_USART1_ORDER << 13) |
(MSP430X_USART1_SIZE << 12) | (MSP430X_USART1_STOP << 11) |
(MSP430X_USART1_UCSSEL);
UCA1IE = UCRXIE;
}
#endif
#if (MSP430X_SERIAL_USE_USART2 == TRUE) || defined(__DOXYGEN__)
static void usart2_init(const SerialConfig * config) {
UCA2BRW = UCAxBRW(config->sc_bitrate, MSP430X_USART2_CLK_FREQ);
UCA2MCTLW = UCAxMCTLW(config->sc_bitrate);
UCA2STATW = 0;
UCA2ABCTL = 0;
UCA2IRCTL = 0;
UCA2CTLW0 = (MSP430X_USART2_PARITY << 14) | (MSP430X_USART2_ORDER << 13) |
(MSP430X_USART2_SIZE << 12) | (MSP430X_USART2_STOP << 11) |
(MSP430X_USART2_UCSSEL);
UCA2IE = UCRXIE;
}
#endif
#if (MSP430X_SERIAL_USE_USART3 == TRUE) || defined(__DOXYGEN__)
static void usart3_init(const SerialConfig * config) {
UCA3BRW = UCAxBRW(config->sc_bitrate, MSP430X_USART3_CLK_FREQ);
UCA3MCTLW = UCAxMCTLW(config->sc_bitrate, MSP430X_USART3_CLK_FREQ);
UCA3STATW = 0;
UCA3ABCTL = 0;
UCA3IRCTL = 0;
UCA3CTLW0 = (MSP430X_USART3_PARITY << 14) | (MSP430X_USART3_ORDER << 13) |
(MSP430X_USART3_SIZE << 12) | (MSP430X_USART3_STOP << 11) |
(MSP430X_USART3_UCSSEL);
UCA3IE = UCRXIE;
}
#endif
#if (MSP430X_SERIAL_USE_USART0 == TRUE) || defined(__DOXYGEN__)
static void notify0(io_queue_t * qp) {
(void)qp;
UCA0IE |= UCTXIE;
}
#endif
#if (MSP430X_SERIAL_USE_USART1 == TRUE) || defined(__DOXYGEN__)
static void notify1(io_queue_t * qp) {
(void)qp;
UCA1IE |= UCTXIE;
}
#endif
#if (MSP430X_SERIAL_USE_USART2 == TRUE) || defined(__DOXYGEN__)
static void notify2(io_queue_t * qp) {
(void)qp;
UCA2IE |= UCTXIE;
}
#endif
#if (MSP430X_SERIAL_USE_USART3 == TRUE) || defined(__DOXYGEN__)
static void notify3(io_queue_t * qp) {
(void)qp;
UCA3IE |= UCTXIE;
}
#endif
/**
* @brief Error handling routine.
*
* @param[in] sra USCI status register containing errors
* @param[in] sdp pointer to a @p SerialDriver object
*/
static void set_error(uint16_t sra, SerialDriver * sdp) {
eventflags_t sts = 0;
if (sra & UCOE)
sts |= SD_OVERRUN_ERROR;
if (sra & UCPE)
sts |= SD_PARITY_ERROR;
if (sra & UCFE)
sts |= SD_FRAMING_ERROR;
osalSysLockFromISR();
chnAddFlagsI(sdp, sts);
osalSysUnlockFromISR();
}
/*===========================================================================*/
/* Driver interrupt handlers. */
/*===========================================================================*/
#if MSP430X_SERIAL_USE_USART0 || defined(__DOXYGEN__)
/**
* @brief USART0 interrupt handler.
*
* @isr
*/
PORT_IRQ_HANDLER(USCI_A0_VECTOR) {
msg_t b;
OSAL_IRQ_PROLOGUE();
switch (__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG)) {
case USCI_UART_UCRXIFG: /* RX interrupt */
/* Detect errors */
if (UCA0STATW & UCRXERR)
set_error(UCA0STATW, &SD0);
/* Data available */
osalSysLockFromISR();
sdIncomingDataI(&SD0, UCA0RXBUF);
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXIFG: /* TX interrupt */
/* Transmission buffer empty */
osalSysLockFromISR();
b = sdRequestDataI(&SD0);
if (b < Q_OK) {
chnAddFlagsI(&SD0, CHN_OUTPUT_EMPTY);
UCA0IE = (UCA0IE & ~UCTXIE) | UCTXCPTIE;
UCA0IFG |= UCTXIFG; /* If we don't write to TXBUF, IFG won't get set */
}
else
UCA0TXBUF = b;
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXCPTIFG: /* TX complete interrupt */
/* Physical transmission end */
osalSysLockFromISR();
if (oqIsEmptyI(&SD0.oqueue))
chnAddFlagsI(&SD0, CHN_TRANSMISSION_END);
UCA0IE &= ~UCTXCPTIE;
osalSysUnlockFromISR();
break;
default: /* other interrupts */
osalDbgAssert(false, "unhandled serial interrupt");
break;
}
OSAL_IRQ_EPILOGUE();
}
#endif
#if MSP430X_SERIAL_USE_USART1 || defined(__DOXYGEN__)
/**
* @brief USART1 interrupt handler.
*
* @isr
*/
PORT_IRQ_HANDLER(USCI_A1_VECTOR) {
msg_t b;
OSAL_IRQ_PROLOGUE();
switch (__even_in_range(UCA1IV, USCI_UART_UCTXCPTIFG)) {
case USCI_UART_UCRXIFG: /* RX interrupt */
/* Detect errors */
if (UCA1STATW & UCRXERR)
set_error(UCA1STATW, &SD1);
/* Data available */
osalSysLockFromISR();
sdIncomingDataI(&SD1, UCA1RXBUF);
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXIFG: /* TX interrupt */
/* Transmission buffer empty */
osalSysLockFromISR();
b = sdRequestDataI(&SD1);
if (b < Q_OK) {
chnAddFlagsI(&SD1, CHN_OUTPUT_EMPTY);
UCA1IE = (UCA1IE & ~UCTXIE) | UCTXCPTIE;
UCA1IFG |= UCTXIFG; /* If we don't write to TXBUF, IFG won't get set */
}
else
UCA1TXBUF = b;
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXCPTIFG: /* TX complete interrupt */
/* Physical transmission end */
osalSysLockFromISR();
if (oqIsEmptyI(&SD1.oqueue))
chnAddFlagsI(&SD1, CHN_TRANSMISSION_END);
UCA1IE &= ~UCTXCPTIE;
osalSysUnlockFromISR();
break;
default: /* other interrupts */
osalDbgAssert(false, "unhandled serial interrupt");
break;
}
OSAL_IRQ_EPILOGUE();
}
#endif
#if MSP430X_SERIAL_USE_USART2 || defined(__DOXYGEN__)
/**
* @brief USART2 interrupt handler.
*
* @isr
*/
PORT_IRQ_HANDLER(USCI_A2_VECTOR) {
msg_t b;
OSAL_IRQ_PROLOGUE();
switch (__even_in_range(UCA2IV, USCI_UART_UCTXCPTIFG)) {
case USCI_UART_UCRXIFG: /* RX interrupt */
/* Detect errors */
if (UCA2STATW & UCRXERR)
set_error(UCA2STATW, &SD2);
/* Data available */
osalSysLockFromISR();
sdIncomingDataI(&SD2, UCA2RXBUF);
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXIFG: /* TX interrupt */
/* Transmission buffer empty */
osalSysLockFromISR();
b = sdRequestDataI(&SD2);
if (b < Q_OK) {
chnAddFlagsI(&SD2, CHN_OUTPUT_EMPTY);
UCA2IE = (UCA2IE & ~UCTXIE) | UCTXCPTIE;
UCA2IFG |= UCTXIFG; /* If we don't write to TXBUF, IFG won't get set */
}
else
UCA2TXBUF = b;
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXCPTIFG: /* TX complete interrupt */
/* Physical transmission end */
osalSysLockFromISR();
if (oqIsEmptyI(&SD2.oqueue))
chnAddFlagsI(&SD2, CHN_TRANSMISSION_END);
UCA2IE &= ~UCTXCPTIE;
osalSysUnlockFromISR();
break;
default: /* other interrupts */
osalDbgAssert(false, "unhandled serial interrupt");
break;
}
OSAL_IRQ_EPILOGUE();
}
#endif
#if MSP430X_SERIAL_USE_USART3 || defined(__DOXYGEN__)
/**
* @brief USART3 interrupt handler.
*
* @isr
*/
PORT_IRQ_HANDLER(USCI_A3_VECTOR) {
msg_t b;
OSAL_IRQ_PROLOGUE();
switch (__even_in_range(UCA3IV, USCI_UART_UCTXCPTIFG)) {
case USCI_UART_UCRXIFG: /* RX interrupt */
/* Detect errors */
if (UCA3STATW & UCRXERR)
set_error(UCA3STATW, &SD3);
/* Data available */
osalSysLockFromISR();
sdIncomingDataI(&SD3, UCA3RXBUF);
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXIFG: /* TX interrupt */
/* Transmission buffer empty */
osalSysLockFromISR();
b = sdRequestDataI(&SD3);
if (b < Q_OK) {
chnAddFlagsI(&SD3, CHN_OUTPUT_EMPTY);
UCA3IE = (UCA3IE & ~UCTXIE) | UCTXCPTIE;
UCA3IFG |= UCTXIFG; /* If we don't write to TXBUF, IFG won't get set */
}
else
UCA3TXBUF = b;
osalSysUnlockFromISR();
break;
case USCI_UART_UCTXCPTIFG: /* TX complete interrupt */
/* Physical transmission end */
osalSysLockFromISR();
if (oqIsEmptyI(&SD3.oqueue))
chnAddFlagsI(&SD3, CHN_TRANSMISSION_END);
UCA3IE &= ~UCTXCPTIE;
osalSysUnlockFromISR();
break;
default: /* other interrupts */
osalDbgAssert(false, "unhandled serial interrupt");
break;
}
OSAL_IRQ_EPILOGUE();
}
#endif
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Low level serial driver initialization.
*
* @notapi
*/
void sd_lld_init(void) {
#if MSP430X_SERIAL_USE_USART0 == TRUE
sdObjectInit(&SD0, NULL, notify0);
#endif
#if MSP430X_SERIAL_USE_USART1 == TRUE
sdObjectInit(&SD1, NULL, notify1);
#endif
#if MSP430X_SERIAL_USE_USART2 == TRUE
sdObjectInit(&SD2, NULL, notify2);
#endif
#if MSP430X_SERIAL_USE_USART3 == TRUE
sdObjectInit(&SD3, NULL, notify3);
#endif
}
/**
* @brief Low level serial driver configuration and (re)start.
*
* @param[in] sdp pointer to a @p SerialDriver object
* @param[in] config the architecture-dependent serial driver configuration.
* If this parameter is set to @p NULL then a default
* configuration is used.
*
* @notapi
*/
void sd_lld_start(SerialDriver * sdp, const SerialConfig * config) {
if (config == NULL) {
config = &default_config;
}
if (sdp->state == SD_STOP) {
#if MSP430X_SERIAL_USE_USART0 == TRUE
if (&SD0 == sdp) {
usart0_init(config);
}
#endif
#if MSP430X_SERIAL_USE_USART1 == TRUE
if (&SD1 == sdp) {
usart1_init(config);
}
#endif
#if MSP430X_SERIAL_USE_USART2 == TRUE
if (&SD2 == sdp) {
usart2_init(config);
}
#endif
#if MSP430X_SERIAL_USE_USART3 == TRUE
if (&SD3 == sdp) {
usart3_init(config);
}
#endif
}
}
/**
* @brief Low level serial driver stop.
* @details De-initializes the USART, stops the associated clock, resets the
* interrupt vector.
*
* @param[in] sdp pointer to a @p SerialDriver object
*
* @notapi
*/
void sd_lld_stop(SerialDriver * sdp) {
if (sdp->state == SD_READY) {
#if MSP430X_SERIAL_USE_USART0 == TRUE
if (&SD0 == sdp) {
UCA0CTLW0 = UCSWRST;
}
#endif
#if MSP430X_SERIAL_USE_USART1 == TRUE
if (&SD1 == sdp) {
UCA1CTLW0 = UCSWRST;
}
#endif
#if MSP430X_SERIAL_USE_USART2 == TRUE
if (&SD2 == sdp) {
UCA2CTLW0 = UCSWRST;
}
#endif
#if MSP430X_SERIAL_USE_USART3 == TRUE
if (&SD3 == sdp) {
UCA3CTLW0 = UCSWRST;
}
#endif
}
}
#endif /* HAL_USE_SERIAL == TRUE */
/** @} */
|