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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
|
/*******************************************************************************
* (c) Copyright 2011-2013 Microsemi SoC Products Group. All rights reserved.
*
* SmartFusion2 Microcontroller Subsystem MMUART bare metal software driver
* implementation.
*
* SVN $Revision: 5610 $
* SVN $Date: 2013-04-05 18:49:30 +0530 (Fri, 05 Apr 2013) $
*/
#include "mss_uart.h"
#include "mss_uart_regs.h"
#include "../../CMSIS/mss_assert.h"
#include "../../CMSIS/hw_reg_io.h"
#include "../../CMSIS/system_m2sxxx.h"
#ifdef __cplusplus
extern "C" {
#endif
/*******************************************************************************
* Defines
*/
#define TX_COMPLETE 0u
#define TX_FIFO_SIZE 16u
#define FCR_TRIG_LEVEL_MASK 0xC0u
#define IIRF_MASK 0x0Fu
#define INVALID_INTERRUPT 0u
#define INVALID_IRQ_HANDLER ((mss_uart_irq_handler_t) 0)
#define NULL_HANDLER ((mss_uart_irq_handler_t) 0)
#define MSS_UART_DATA_READY ((uint8_t) 0x01)
#define SYNC_ASYNC_MODE_MASK (0x7u)
/*******************************************************************************
* Possible values for Interrupt Identification Register Field.
*/
#define IIRF_MODEM_STATUS 0x00u
#define IIRF_THRE 0x02u
#define IIRF_MMI 0x03u
#define IIRF_RX_DATA 0x04u
#define IIRF_RX_LINE_STATUS 0x06u
#define IIRF_DATA_TIMEOUT 0x0Cu
/*******************************************************************************
* Receiver error status mask.
*/
#define STATUS_ERROR_MASK ( MSS_UART_OVERUN_ERROR | MSS_UART_PARITY_ERROR | \
MSS_UART_FRAMING_ERROR | MSS_UART_BREAK_ERROR | \
MSS_UART_FIFO_ERROR)
/*******************************************************************************
* Cortex-M3 interrupt handler functions implemented as part of the MSS UART
* driver.
*/
#if defined(__GNUC__)
__attribute__((__interrupt__)) void UART0_IRQHandler(void);
#else
void UART0_IRQHandler(void);
#endif
#if defined(__GNUC__)
__attribute__((__interrupt__)) void UART1_IRQHandler(void);
#else
void UART1_IRQHandler(void);
#endif
/*******************************************************************************
* Local functions.
*/
static void global_init(mss_uart_instance_t * this_uart, uint32_t baud_rate,
uint8_t line_config);
static void MSS_UART_isr(mss_uart_instance_t * this_uart);
static void default_tx_handler(mss_uart_instance_t * this_uart);
static void config_baud_divisors
(
mss_uart_instance_t * this_uart,
uint32_t baudrate
);
/*******************************************************************************
* Instance definitions
*/
mss_uart_instance_t g_mss_uart0;
mss_uart_instance_t g_mss_uart1;
/*******************************************************************************
* Public Functions
*******************************************************************************/
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_init
(
mss_uart_instance_t* this_uart,
uint32_t baud_rate,
uint8_t line_config
)
{
/* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
* mss_uart_instance_t instances used to identify UART0 and UART1. */
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
/* Perform generic initialization */
global_init(this_uart, baud_rate, line_config);
/* Disable LIN mode */
clear_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
/* Disable IrDA mode */
clear_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
/* Disable SmartCard Mode */
clear_bit_reg8(&this_uart->hw_reg->MM2, EERR);
/* set default tx handler for automated TX using interrupt in USART mode */
this_uart->tx_handler = default_tx_handler;
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void MSS_UART_lin_init
(
mss_uart_instance_t* this_uart,
uint32_t baud_rate,
uint8_t line_config
)
{
/* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
* mss_uart_instance_t instances used to identify UART0 and UART1. */
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
/* Perform generic initialization */
global_init(this_uart, baud_rate, line_config);
/* Enable LIN mode */
set_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
/* Disable IrDA mode */
clear_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
/* Disable SmartCard Mode */
clear_bit_reg8(&this_uart->hw_reg->MM2, EERR);
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_irda_init
(
mss_uart_instance_t* this_uart,
uint32_t baud_rate,
uint8_t line_config,
mss_uart_rzi_polarity_t rxpol,
mss_uart_rzi_polarity_t txpol,
mss_uart_rzi_pulsewidth_t pw
)
{
/* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
* mss_uart_instance_t instances used to identify UART0 and UART1. */
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
/* Perform generic initialization */
global_init(this_uart, baud_rate, line_config);
/* Enable LIN mode */
clear_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
/* Disable IrDA mode */
set_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
((rxpol == MSS_UART_ACTIVE_LOW) ? clear_bit_reg8(&this_uart->hw_reg->MM1,EIRX) :
set_bit_reg8(&this_uart->hw_reg->MM1,EIRX));
((txpol == MSS_UART_ACTIVE_LOW) ? clear_bit_reg8(&this_uart->hw_reg->MM1,EITX) :
set_bit_reg8(&this_uart->hw_reg->MM1,EITX));
((pw == MSS_UART_3_BY_16) ? clear_bit_reg8(&this_uart->hw_reg->MM1,EITP) :
set_bit_reg8(&this_uart->hw_reg->MM1,EITP));
/* Disable SmartCard Mode */
clear_bit_reg8(&this_uart->hw_reg->MM2, EERR);
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_smartcard_init
(
mss_uart_instance_t* this_uart,
uint32_t baud_rate,
uint8_t line_config
)
{
/* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
* mss_uart_instance_t instances used to identify UART0 and UART1. */
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
/* Perform generic initialization */
global_init(this_uart, baud_rate, line_config);
/* Disable LIN mode */
clear_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
/* Disable IrDA mode */
clear_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
/* Enable SmartCard Mode : Only when data is 8-bit and 2 stop bits*/
if( ( MSS_UART_DATA_8_BITS | MSS_UART_TWO_STOP_BITS) ==
(line_config & (MSS_UART_DATA_8_BITS | MSS_UART_TWO_STOP_BITS)))
{
set_bit_reg8(&this_uart->hw_reg->MM2, EERR);
/* Enable single wire half-duplex mode */
set_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_polled_tx
(
mss_uart_instance_t * this_uart,
const uint8_t * pbuff,
uint32_t tx_size
)
{
uint32_t char_idx = 0u;
uint32_t size_sent;
uint8_t status;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(pbuff != ( (uint8_t *)0));
ASSERT(tx_size > 0u);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(pbuff != ((uint8_t *)0)) && (tx_size > 0u))
{
/* Remain in this loop until the entire input buffer
* has been transferred to the UART.
*/
do {
/* Read the Line Status Register and update the sticky record */
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
/* Check if TX FIFO is empty. */
if(status & MSS_UART_THRE)
{
uint32_t fill_size = TX_FIFO_SIZE;
/* Calculate the number of bytes to transmit. */
if(tx_size < TX_FIFO_SIZE)
{
fill_size = tx_size;
}
/* Fill the TX FIFO with the calculated the number of bytes. */
for(size_sent = 0u; size_sent < fill_size; ++size_sent)
{
/* Send next character in the buffer. */
this_uart->hw_reg->THR = pbuff[char_idx];
char_idx++;
}
/* Calculate the number of untransmitted bytes remaining. */
tx_size -= size_sent;
}
} while(tx_size);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_polled_tx_string
(
mss_uart_instance_t * this_uart,
const uint8_t * p_sz_string
)
{
uint32_t char_idx = 0u;
uint32_t fill_size;
uint8_t data_byte;
uint8_t status;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(p_sz_string != ((uint8_t *)0));
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(p_sz_string != ((uint8_t *)0)))
{
/* Get the first data byte from the input buffer */
data_byte = p_sz_string[char_idx];
/* First check for the NULL terminator byte.
* Then remain in this loop until the entire string in the input buffer
* has been transferred to the UART.
*/
while(0u != data_byte)
{
/* Wait until TX FIFO is empty. */
do {
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
} while (0u == (status & MSS_UART_THRE));
/* Send bytes from the input buffer until the TX FIFO is full
* or we reach the NULL terminator byte.
*/
fill_size = 0u;
while((0u != data_byte) && (fill_size < TX_FIFO_SIZE))
{
/* Send the data byte */
this_uart->hw_reg->THR = data_byte;
++fill_size;
char_idx++;
/* Get the next data byte from the input buffer */
data_byte = p_sz_string[char_idx];
}
}
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_irq_tx
(
mss_uart_instance_t * this_uart,
const uint8_t * pbuff,
uint32_t tx_size
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(pbuff != ((uint8_t *)0));
ASSERT(tx_size > 0u);
if((tx_size > 0u) && ( pbuff != ((uint8_t *)0)) &&
((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)))
{
/*Initialise the transmit info for the UART instance with the arguments.*/
this_uart->tx_buffer = pbuff;
this_uart->tx_buff_size = tx_size;
this_uart->tx_idx = (uint16_t)0;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ(this_uart->irqn);
/* assign default handler for data transfer */
this_uart->tx_handler = default_tx_handler;
/* enables TX interrupt */
set_bit_reg8(&this_uart->hw_reg->IER,ETBEI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
int8_t
MSS_UART_tx_complete
(
mss_uart_instance_t * this_uart
)
{
int8_t ret_value = 0;
uint8_t status = 0u;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Read the Line Status Register and update the sticky record. */
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
if((TX_COMPLETE == this_uart->tx_buff_size) &&
((status & MSS_UART_TEMT) != 0u))
{
ret_value = (int8_t)1;
}
}
return ret_value;
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
size_t
MSS_UART_get_rx
(
mss_uart_instance_t * this_uart,
uint8_t * rx_buff,
size_t buff_size
)
{
size_t rx_size = 0u;
uint8_t status = 0u;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(rx_buff != ((uint8_t *)0));
ASSERT(buff_size > 0u);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(rx_buff != ((uint8_t *)0)) && (buff_size > 0u))
{
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
while(((status & MSS_UART_DATA_READY) != 0u) &&
(rx_size < buff_size))
{
rx_buff[rx_size] = this_uart->hw_reg->RBR;
++rx_size;
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
}
}
return rx_size;
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_enable_irq
(
mss_uart_instance_t * this_uart,
mss_uart_irq_t irq_mask
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(MSS_UART_INVALID_IRQ > irq_mask);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(MSS_UART_INVALID_IRQ > irq_mask))
{
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ(this_uart->irqn);
/* irq_mask encoding: 1- enable
* bit 0 - Receive Data Available Interrupt
* bit 1 - Transmitter Holding Register Empty Interrupt
* bit 2 - Receiver Line Status Interrupt
* bit 3 - Modem Status Interrupt
*/
this_uart->hw_reg->IER |= (uint8_t)irq_mask & IIRF_MASK;
/*
* bit 4 - Receiver time-out interrupt
* bit 5 - NACK / ERR signal interrupt
* bit 6 - PID parity error interrupt
* bit 7 - LIN break detection interrupt
* bit 8 - LIN Sync detection interrupt
*/
this_uart->hw_reg->IEM |= (uint8_t)(((uint32_t)irq_mask & ~((uint32_t)IIRF_MASK)) >> 4u);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_disable_irq
(
mss_uart_instance_t * this_uart,
mss_uart_irq_t irq_mask
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* irq_mask encoding: 1 - disable
* bit 0 - Receive Data Available Interrupt
* bit 1 - Transmitter Holding Register Empty Interrupt
* bit 2 - Receiver Line Status Interrupt
* bit 3 - Modem Status Interrupt
*/
this_uart->hw_reg->IER &= ((uint8_t)(~((uint32_t)irq_mask & (uint32_t)IIRF_MASK)));
/*
* bit 4 - Receiver time-out interrupt
* bit 5 - NACK / ERR signal interrupt
* bit 6 - PID parity error interrupt
* bit 7 - LIN break detection interrupt
* bit 8 - LIN Sync detection interrupt
*/
this_uart->hw_reg->IEM |= (uint8_t)(~(((uint32_t)irq_mask & ~((uint32_t)IIRF_MASK)) >> 8u));
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ(this_uart->irqn);
if(irq_mask == IIRF_MASK)
{
/* Disable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_DisableIRQ(this_uart->irqn);
}
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_rx_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler,
mss_uart_rx_trig_level_t trigger_level
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER );
ASSERT(trigger_level < MSS_UART_FIFO_INVALID_TRIG_LEVEL);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER) &&
(trigger_level < MSS_UART_FIFO_INVALID_TRIG_LEVEL))
{
this_uart->rx_handler = handler;
/* Set the receive interrupt trigger level. */
this_uart->hw_reg->FCR = (this_uart->hw_reg->FCR &
(uint8_t)(~((uint8_t)FCR_TRIG_LEVEL_MASK))) |
(uint8_t)trigger_level;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ(this_uart->irqn);
/* Enable receive interrupt. */
set_bit_reg8(&this_uart->hw_reg->IER,ERBFI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_loopback
(
mss_uart_instance_t * this_uart,
mss_uart_loopback_t loopback
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(MSS_UART_INVALID_LOOPBACK > loopback);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) ||
(MSS_UART_INVALID_LOOPBACK > loopback))
{
switch(loopback)
{
case MSS_UART_LOCAL_LOOPBACK_OFF:
/* Disable local loopback */
clear_bit_reg8(&this_uart->hw_reg->MCR,LOOP);
break;
case MSS_UART_LOCAL_LOOPBACK_ON:
/* Enable local loopback */
set_bit_reg8(&this_uart->hw_reg->MCR,LOOP);
break;
case MSS_UART_REMOTE_LOOPBACK_OFF:
case MSS_UART_AUTO_ECHO_OFF:
/* Disable remote loopback & automatic echo*/
this_uart->hw_reg->MCR &= ~RLOOP_MASK;
break;
case MSS_UART_REMOTE_LOOPBACK_ON:
/* Enable remote loopback */
this_uart->hw_reg->MCR |= (1u << RLOOP);
break;
case MSS_UART_AUTO_ECHO_ON:
/* Enable automatic echo */
this_uart->hw_reg->MCR |= (1u << ECHO);
break;
case MSS_UART_INVALID_LOOPBACK:
/* Fall through to default. */
default:
ASSERT(0);
break;
}
}
}
/***************************************************************************//**
* UART0 interrupt service routine.
* UART0_IRQHandler is included within the Cortex-M3 vector table as part of the
* Fusion 2 CMSIS.
*/
#if defined(__GNUC__)
__attribute__((__interrupt__)) void UART0_IRQHandler(void)
#else
void UART0_IRQHandler(void)
#endif
{
MSS_UART_isr(&g_mss_uart0);
}
/***************************************************************************//**
* UART1 interrupt service routine.
* UART2_IRQHandler is included within the Cortex-M3 vector table as part of the
* Fusion 2 CMSIS.
*/
#if defined(__GNUC__)
__attribute__((__interrupt__)) void UART1_IRQHandler(void)
#else
void UART1_IRQHandler(void)
#endif
{
MSS_UART_isr(&g_mss_uart1);
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_rxstatus_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->linests_handler = handler;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ(this_uart->irqn);
/* Enable receiver line status interrupt. */
set_bit_reg8(&this_uart->hw_reg->IER,ELSI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_tx_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->tx_handler = handler;
/* Make TX buffer info invalid */
this_uart->tx_buffer = (const uint8_t *)0;
this_uart->tx_buff_size = 0u;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ(this_uart->irqn);
/* Enable transmitter holding register Empty interrupt. */
set_bit_reg8(&this_uart->hw_reg->IER,ETBEI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_modemstatus_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->modemsts_handler = handler;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ(this_uart->irqn);
/* Enable modem status interrupt. */
set_bit_reg8(&this_uart->hw_reg->IER,EDSSI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
size_t
MSS_UART_fill_tx_fifo
(
mss_uart_instance_t * this_uart,
const uint8_t * tx_buffer,
size_t tx_size
)
{
uint8_t status = 0u;
size_t size_sent = 0u;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(tx_buffer != ( (uint8_t *)0));
ASSERT(tx_size > 0);
/* Fill the UART's Tx FIFO until the FIFO is full or the complete input
* buffer has been written. */
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(tx_buffer != ((uint8_t *)0)) &&
(tx_size > 0u))
{
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
if(status & MSS_UART_THRE)
{
uint32_t fill_size = TX_FIFO_SIZE;
if(tx_size < TX_FIFO_SIZE)
{
fill_size = tx_size;
}
/* Fill up FIFO */
for(size_sent = 0u; size_sent < fill_size; ++size_sent)
{
/* Send next character in the buffer. */
this_uart->hw_reg->THR = tx_buffer[size_sent];
}
}
}
return size_sent;
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
uint8_t
MSS_UART_get_rx_status
(
mss_uart_instance_t * this_uart
)
{
uint8_t status = MSS_UART_INVALID_PARAM;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/*
* Extract UART receive error status.
* Bit 1 - Overflow error status
* Bit 2 - Parity error status
* Bit 3 - Frame error status
* Bit 4 - Break interrupt indicator
* Bit 7 - FIFO data error status
*/
this_uart->status |= (this_uart->hw_reg->LSR);
status = (this_uart->status & STATUS_ERROR_MASK);
/* Clear the sticky status after reading */
this_uart->status = 0u;
}
return status;
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
uint8_t
MSS_UART_get_modem_status
(
mss_uart_instance_t * this_uart
)
{
uint8_t status = MSS_UART_INVALID_PARAM;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/*
* Extract UART modem status and place in lower bits of "status".
* Bit 0 - Delta Clear to Send Indicator
* Bit 1 - Delta Clear to Receive Indicator
* Bit 2 - Trailing edge of Ring Indicator detector
* Bit 3 - Delta Data Carrier Detect indicator
* Bit 4 - Clear To Send
* Bit 5 - Data Set Ready
* Bit 6 - Ring Indicator
* Bit 7 - Data Carrier Detect
*/
status = this_uart->hw_reg->MSR;
}
return status;
}
/***************************************************************************//**
* MSS_UART_get_tx_status.
* See mss_uart.h for details of how to use this function.
*/
uint8_t
MSS_UART_get_tx_status
(
mss_uart_instance_t * this_uart
)
{
uint8_t status = MSS_UART_TX_BUSY;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Read the Line Status Register and update the sticky record. */
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
/*
* Extract the transmit status bits from the UART's Line Status Register.
* Bit 5 - Transmitter Holding Register/FIFO Empty (THRE) status. (If = 1, TX FIFO is empty)
* Bit 6 - Transmitter Empty (TEMT) status. (If = 1, both TX FIFO and shift register are empty)
*/
status &= (MSS_UART_THRE | MSS_UART_TEMT);
}
return status;
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_break
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* set break charecter on Tx line */
set_bit_reg8(&this_uart->hw_reg->LCR,SB);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_clear_break
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* remove break charecter from Tx line */
clear_bit_reg8(&this_uart->hw_reg->LCR,SB);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_pidpei_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->pid_pei_handler = handler;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ( this_uart->irqn );
/* Enable PID parity error interrupt. */
set_bit_reg8(&this_uart->hw_reg->IEM,EPID_PEI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_linbreak_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->break_handler = handler;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ( this_uart->irqn );
/* Enable LIN break detection interrupt. */
set_bit_reg8(&this_uart->hw_reg->IEM,ELINBI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_linsync_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->sync_handler = handler;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ( this_uart->irqn );
/* Enable LIN sync detection interrupt. */
set_bit_reg8(&this_uart->hw_reg->IEM,ELINSI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_nack_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->nack_handler = handler;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ( this_uart->irqn );
/* Enable LIN sync detection interrupt. */
set_bit_reg8(&this_uart->hw_reg->IEM,ENACKI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_rx_timeout_handler
(
mss_uart_instance_t * this_uart,
mss_uart_irq_handler_t handler
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(handler != INVALID_IRQ_HANDLER);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(handler != INVALID_IRQ_HANDLER))
{
this_uart->rto_handler = handler;
/* Clear any previously pended interrupts */
NVIC_ClearPendingIRQ( this_uart->irqn );
/* Enable receiver timeout interrupt. */
set_bit_reg8(&this_uart->hw_reg->IEM,ERTOI);
/* Enable UART instance interrupt in Cortex-M3 NVIC. */
NVIC_EnableIRQ(this_uart->irqn);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_enable_half_duplex
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* enable single wire half-duplex mode */
set_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_disable_half_duplex
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* enable single wire half-duplex mode */
clear_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_rx_endian
(
mss_uart_instance_t * this_uart,
mss_uart_endian_t endian
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(MSS_UART_INVALID_ENDIAN > endian);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(MSS_UART_INVALID_ENDIAN > endian))
{
/* Configure MSB first / LSB first for receiver */
((MSS_UART_LITTLEEND == endian) ? (clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_RX)) :
(set_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_RX)));
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_tx_endian
(
mss_uart_instance_t * this_uart,
mss_uart_endian_t endian
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(MSS_UART_INVALID_ENDIAN > endian);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(MSS_UART_INVALID_ENDIAN > endian))
{
/* Configure MSB first / LSB first for transmitter */
((MSS_UART_LITTLEEND == endian) ? (clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_TX)) :
(set_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_TX)) ) ;
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_filter_length
(
mss_uart_instance_t * this_uart,
mss_uart_filter_length_t length
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(MSS_UART_INVALID_FILTER_LENGTH > length);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(MSS_UART_INVALID_FILTER_LENGTH > length))
{
/* Configure glitch filter length */
this_uart->hw_reg->GFR = (uint8_t)length;
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_enable_afm
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Disable RX FIFO till address flag with correct address is received */
set_bit_reg8(&this_uart->hw_reg->MM2,EAFM);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_disable_afm
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Enable RX FIFO irrespective of address flag and
correct address is received */
clear_bit_reg8(&this_uart->hw_reg->MM2,EAFM);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_enable_afclear
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Enable address flag clearing */
/* Disable RX FIFO till another address flag with
correct address is received */
set_bit_reg8(&this_uart->hw_reg->MM2,EAFC);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_disable_afclear
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Disable address flag clearing */
clear_bit_reg8(&this_uart->hw_reg->MM2,EAFC);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_enable_rx_timeout
(
mss_uart_instance_t * this_uart,
uint8_t timeout
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Load the receive timeout value */
this_uart->hw_reg->RTO = timeout;
/*Enable receiver time-out */
set_bit_reg8(&this_uart->hw_reg->MM0,ERTO);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_disable_rx_timeout
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/*Disable receiver time-out */
clear_bit_reg8(&this_uart->hw_reg->MM0,ERTO);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_enable_tx_time_guard
(
mss_uart_instance_t * this_uart,
uint8_t timeguard
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/* Load the transmitter time guard value */
this_uart->hw_reg->TTG = timeguard;
/*Enable transmitter time guard */
set_bit_reg8(&this_uart->hw_reg->MM0,ETTG);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_disable_tx_time_guard
(
mss_uart_instance_t * this_uart
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
/*Disable transmitter time guard */
clear_bit_reg8(&this_uart->hw_reg->MM0,ETTG);
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_address
(
mss_uart_instance_t * this_uart,
uint8_t address
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
this_uart->hw_reg->ADR = address;
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_ready_mode
(
mss_uart_instance_t * this_uart,
mss_uart_ready_mode_t mode
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(MSS_UART_INVALID_READY_MODE > mode);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(MSS_UART_INVALID_READY_MODE > mode ) )
{
/* Configure mode 0 or mode 1 for TXRDY and RXRDY */
((MSS_UART_READY_MODE0 == mode) ? clear_bit_reg8(&this_uart->hw_reg->FCR,RDYMODE) :
set_bit_reg8(&this_uart->hw_reg->FCR,RDYMODE) );
}
}
/***************************************************************************//**
* Configure baud divisors using fractional baud rate if possible.
*/
static void
config_baud_divisors
(
mss_uart_instance_t * this_uart,
uint32_t baudrate
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
uint32_t baud_value;
uint32_t baud_value_by_64;
uint32_t baud_value_by_128;
uint32_t fractional_baud_value;
uint32_t pclk_freq;
this_uart->baudrate = baudrate;
/* Force the value of the CMSIS global variables holding the various system
* clock frequencies to be updated. */
SystemCoreClockUpdate();
if(this_uart == &g_mss_uart0)
{
pclk_freq = g_FrequencyPCLK0;
}
else
{
pclk_freq = g_FrequencyPCLK1;
}
/*
* Compute baud value based on requested baud rate and PCLK frequency.
* The baud value is computed using the following equation:
* baud_value = PCLK_Frequency / (baud_rate * 16)
*/
baud_value_by_128 = (8u * pclk_freq) / baudrate;
baud_value_by_64 = baud_value_by_128 / 2u;
baud_value = baud_value_by_64 / 64u;
fractional_baud_value = baud_value_by_64 - (baud_value * 64u);
fractional_baud_value += (baud_value_by_128 - (baud_value * 128u)) - (fractional_baud_value * 2u);
/* Assert if integer baud value fits in 16-bit. */
ASSERT(baud_value <= UINT16_MAX);
if(baud_value <= (uint32_t)UINT16_MAX)
{
if(baud_value > 1u)
{
/*
* Use Frational baud rate divisors
*/
/* set divisor latch */
set_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
/* msb of baud value */
this_uart->hw_reg->DMR = (uint8_t)(baud_value >> 8);
/* lsb of baud value */
this_uart->hw_reg->DLR = (uint8_t)baud_value;
/* reset divisor latch */
clear_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
/* Enable Fractional baud rate */
set_bit_reg8(&this_uart->hw_reg->MM0,EFBR);
/* Load the fractional baud rate register */
ASSERT(fractional_baud_value <= (uint32_t)UINT8_MAX);
this_uart->hw_reg->DFR = (uint8_t)fractional_baud_value;
}
else
{
/*
* Do NOT use Frational baud rate divisors.
*/
/* set divisor latch */
set_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
/* msb of baud value */
this_uart->hw_reg->DMR = (uint8_t)(baud_value >> 8u);
/* lsb of baud value */
this_uart->hw_reg->DLR = (uint8_t)baud_value;
/* reset divisor latch */
clear_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
/* Disable Fractional baud rate */
clear_bit_reg8(&this_uart->hw_reg->MM0,EFBR);
}
}
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
void
MSS_UART_set_usart_mode
(
mss_uart_instance_t * this_uart,
mss_uart_usart_mode_t mode
)
{
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(MSS_UART_INVALID_SYNC_MODE > mode);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(MSS_UART_INVALID_SYNC_MODE > mode))
{
/* Nothing to do for the baudrate: operates at PCLK / 2 + glitch filter length */
/* Clear the ESYN bits 2:0 */
this_uart->hw_reg->MM0 &= ~SYNC_ASYNC_MODE_MASK;
this_uart->hw_reg->MM0 |= (uint8_t)mode;
}
}
/*******************************************************************************
* Local Functions
*******************************************************************************/
/*******************************************************************************
* Global initialization for all modes
*/
static void global_init
(
mss_uart_instance_t * this_uart,
uint32_t baud_rate,
uint8_t line_config
)
{
/* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
* mss_uart_instance_t instances used to identify UART0 and UART1. */
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if(this_uart == &g_mss_uart0)
{
this_uart->hw_reg = UART0;
this_uart->irqn = UART0_IRQn;
/* reset UART0 */
SYSREG->SOFT_RST_CR |= SYSREG_MMUART0_SOFTRESET_MASK;
/* Clear any previously pended UART0 interrupt */
NVIC_ClearPendingIRQ(UART0_IRQn);
/* Take UART0 out of reset. */
SYSREG->SOFT_RST_CR &= ~SYSREG_MMUART0_SOFTRESET_MASK;
}
else
{
this_uart->hw_reg = UART1;
this_uart->irqn = UART1_IRQn;
/* Reset UART1 */
SYSREG->SOFT_RST_CR |= SYSREG_MMUART1_SOFTRESET_MASK;
/* Clear any previously pended UART1 interrupt */
NVIC_ClearPendingIRQ(UART1_IRQn);
/* Take UART1 out of reset. */
SYSREG->SOFT_RST_CR &= ~SYSREG_MMUART1_SOFTRESET_MASK;
}
/* disable interrupts */
this_uart->hw_reg->IER = 0u;
/* FIFO configuration */
this_uart->hw_reg->FCR = (uint8_t)MSS_UART_FIFO_SINGLE_BYTE;
/* clear receiver FIFO */
set_bit_reg8(&this_uart->hw_reg->FCR,CLEAR_RX_FIFO);
/* clear transmitter FIFO */
set_bit_reg8(&this_uart->hw_reg->FCR,CLEAR_TX_FIFO);
/* set default READY mode : Mode 0*/
/* enable RXRDYN and TXRDYN pins. The earlier FCR write to set the TX FIFO
* trigger level inadvertently disabled the FCR_RXRDY_TXRDYN_EN bit. */
set_bit_reg8(&this_uart->hw_reg->FCR,RXRDY_TXRDYN_EN);
/* disable loopback : local * remote */
clear_bit_reg8(&this_uart->hw_reg->MCR,LOOP);
clear_bit_reg8(&this_uart->hw_reg->MCR,RLOOP);
/* set default TX endian */
clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_TX);
/* set default RX endian */
clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_RX);
/* default AFM : disabled */
clear_bit_reg8(&this_uart->hw_reg->MM2,EAFM);
/* disable TX time gaurd */
clear_bit_reg8(&this_uart->hw_reg->MM0,ETTG);
/* set default RX timeout */
clear_bit_reg8(&this_uart->hw_reg->MM0,ERTO);
/* disable fractional baud-rate */
clear_bit_reg8(&this_uart->hw_reg->MM0,EFBR);
/* disable single wire mode */
clear_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
/* set filter to minimum value */
this_uart->hw_reg->GFR = 0u;
/* set default TX time gaurd */
this_uart->hw_reg->TTG = 0u;
/* set default RX timeout */
this_uart->hw_reg->RTO = 0u;
/*
* Configure baud rate divisors. This uses the frational baud rate divisor
* where possible to provide the most accurate baud rat possible.
*/
config_baud_divisors(this_uart, baud_rate);
/* set the line control register (bit length, stop bits, parity) */
this_uart->hw_reg->LCR = line_config;
/* Instance setup */
this_uart->baudrate = baud_rate;
this_uart->lineconfig = line_config;
this_uart->tx_buff_size = TX_COMPLETE;
this_uart->tx_buffer = (const uint8_t *)0;
this_uart->tx_idx = 0u;
/* Default handlers for MSS UART interrupts */
this_uart->rx_handler = NULL_HANDLER;
this_uart->tx_handler = NULL_HANDLER;
this_uart->linests_handler = NULL_HANDLER;
this_uart->modemsts_handler = NULL_HANDLER;
this_uart->rto_handler = NULL_HANDLER;
this_uart->nack_handler = NULL_HANDLER;
this_uart->pid_pei_handler = NULL_HANDLER;
this_uart->break_handler = NULL_HANDLER;
this_uart->sync_handler = NULL_HANDLER;
/* Initialize the sticky status */
this_uart->status = 0u;
}
/***************************************************************************//**
* Interrupt service routine triggered by any MSS UART interrupt. This routine
* will call the handler function appropriate to the interrupt from the
* handlers previously registered with the driver through calls to the
* MSS_UART_set_*_handler() functions, or it will call the default_tx_handler()
* function in response to transmit interrupts if MSS_UART_irq_tx() is used to
* transmit data.
*/
static void
MSS_UART_isr
(
mss_uart_instance_t * this_uart
)
{
uint8_t iirf;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
{
iirf = this_uart->hw_reg->IIR & IIRF_MASK;
switch (iirf)
{
case IIRF_MODEM_STATUS: /* Modem status interrupt */
{
ASSERT(NULL_HANDLER != this_uart->modemsts_handler);
if(NULL_HANDLER != this_uart->modemsts_handler)
{
(*(this_uart->modemsts_handler))(this_uart);
}
}
break;
case IIRF_THRE: /* Transmitter Holding Register Empty */
{
ASSERT(NULL_HANDLER != this_uart->tx_handler);
if(NULL_HANDLER != this_uart->tx_handler)
{
(*(this_uart->tx_handler))(this_uart);
}
}
break;
case IIRF_RX_DATA: /* Received Data Available */
case IIRF_DATA_TIMEOUT: /* Received Data Timed-out */
{
ASSERT(NULL_HANDLER != this_uart->rx_handler);
if(NULL_HANDLER != this_uart->rx_handler)
{
(*(this_uart->rx_handler))(this_uart);
}
}
break;
case IIRF_RX_LINE_STATUS: /* Line Status Interrupt */
{
ASSERT(NULL_HANDLER != this_uart->linests_handler);
if(NULL_HANDLER != this_uart->linests_handler)
{
(*(this_uart->linests_handler))(this_uart);
}
}
break;
case IIRF_MMI:
{
/* Identify multimode interrupts and handle */
/* Receiver time-out interrupt */
if(read_bit_reg8(&this_uart->hw_reg->IIM,ERTOI))
{
ASSERT(NULL_HANDLER != this_uart->rto_handler);
if(NULL_HANDLER != this_uart->rto_handler)
{
(*(this_uart->rto_handler))(this_uart);
}
}
/* NACK interrupt */
if(read_bit_reg8(&this_uart->hw_reg->IIM,ENACKI))
{
ASSERT(NULL_HANDLER != this_uart->nack_handler);
if(NULL_HANDLER != this_uart->nack_handler)
{
(*(this_uart->nack_handler))(this_uart);
}
}
/* PID parity error interrupt */
if(read_bit_reg8(&this_uart->hw_reg->IIM,EPID_PEI))
{
ASSERT(NULL_HANDLER != this_uart->pid_pei_handler);
if(NULL_HANDLER != this_uart->pid_pei_handler)
{
(*(this_uart->pid_pei_handler))(this_uart);
}
}
/* LIN break detection interrupt */
if(read_bit_reg8(&this_uart->hw_reg->IIM,ELINBI))
{
ASSERT(NULL_HANDLER != this_uart->break_handler);
if(NULL_HANDLER != this_uart->break_handler)
{
(*(this_uart->break_handler))(this_uart);
}
}
/* LIN Sync detection interrupt */
if(read_bit_reg8(&this_uart->hw_reg->IIM,ELINSI))
{
ASSERT(NULL_HANDLER != this_uart->sync_handler);
if(NULL_HANDLER != this_uart->sync_handler)
{
(*(this_uart->sync_handler))(this_uart);
}
}
break;
}
default:
{
ASSERT(INVALID_INTERRUPT);
}
break;
}
}
}
/***************************************************************************//**
* See mss_uart.h for details of how to use this function.
*/
static void
default_tx_handler
(
mss_uart_instance_t * this_uart
)
{
uint8_t status;
ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
ASSERT(( (uint8_t *)0 ) != this_uart->tx_buffer);
ASSERT(0u < this_uart->tx_buff_size);
if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
(((uint8_t *)0 ) != this_uart->tx_buffer) &&
(0u < this_uart->tx_buff_size))
{
/* Read the Line Status Register and update the sticky record. */
status = this_uart->hw_reg->LSR;
this_uart->status |= status;
/*
* This function should only be called as a result of a THRE interrupt.
* Verify that this is true before proceeding to transmit data.
*/
if(status & MSS_UART_THRE)
{
uint32_t i;
uint32_t fill_size = TX_FIFO_SIZE;
uint32_t tx_remain = this_uart->tx_buff_size - this_uart->tx_idx;
/* Calculate the number of bytes to transmit. */
if(tx_remain < TX_FIFO_SIZE)
{
fill_size = tx_remain;
}
/* Fill the TX FIFO with the calculated the number of bytes. */
for(i = 0u; i < fill_size; ++i)
{
/* Send next character in the buffer. */
this_uart->hw_reg->THR = this_uart->tx_buffer[this_uart->tx_idx];
++this_uart->tx_idx;
}
}
/* Flag Tx as complete if all data has been pushed into the Tx FIFO. */
if(this_uart->tx_idx == this_uart->tx_buff_size)
{
this_uart->tx_buff_size = TX_COMPLETE;
/* disables TX interrupt */
clear_bit_reg8(&this_uart->hw_reg->IER,ETBEI);
}
}
}
#ifdef __cplusplus
}
#endif
|