summaryrefslogtreecommitdiff
path: root/nuttx/arch/arm/src/tiva/tiva_adc.c
blob: 26f3b6890fa34846eb2aeca44e64c2dd27d8f229 (plain) (blame)
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
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
/****************************************************************************
 * arch/arm/src/tiva/tiva_adc.c
 *
 *   Copyright (C) 2015 TRD2 Inc. All rights reserved.
 *   Author: Calvin Maguranis <calvin.maguranis@trd2inc.com>
 *
 * References:
 *
 *   TM4C123GH6PM Series Data Sheet
 *   TI Tivaware driverlib ADC sample code.
 *
 * The Tivaware sample code has a BSD compatible license that requires this
 * copyright notice:
 *
 * Copyright (c) 2005-2014 Texas Instruments Incorporated.  All rights reserved.
 * Software License Agreement
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *   Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 *   Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 *
 *   Neither the name of Texas Instruments Incorporated nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This is part of revision 2.1.0.12573 of the Tiva Peripheral Driver Library.
 *****************************************************************************/

/* Keep in mind that for every step there should be another entry in the
 * CONFIG_ADC_FIFOSIZE value.
 * e.g. if there are 12 steps in use; CONFIG_ADC_FIFOSIZE = 12+1 = 13
 *      if there are  3 steps in use; CONFIG_ADC_FIFOSIZE = 3+1 = 4
 */

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>
#include <sys/ioctl.h>

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <errno.h>
#include <debug.h>
#include <assert.h>

#include <arch/board/board.h>

#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
#include <nuttx/analog/adc.h>

#include "up_internal.h"
#include "up_arch.h"

#include "chip.h"

#include "tiva_gpio.h"
#include "tiva_adc.h"
#include "chip/tiva_pinmap.h"
#include "chip/tiva_syscontrol.h"
#include "chip/tiva_adc.h"

#if defined (CONFIG_TIVA_ADC0) || defined (CONFIG_TIVA_ADC1)

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* Configuration ************************************************************/

#ifndef CONFIG_TIVA_ADC_CLOCK
#  define CONFIG_TIVA_ADC_CLOCK TIVA_ADC_CLOCK_MIN
#endif

#ifdef CONFIG_TIVA_ADC_VREF
#  ifndef CONFIG_ARCH_CHIP_TM4C129
#    error Voltage reference selection only supported in TM4C129 parts
#  endif
#endif

#ifdef CONFIG_TIVA_ADC_ALT_CLK
#  warning CONFIG_TIVA_ADC_ALT_CLK unsupported.
#endif

/* Are we using interrupt-based triggering (opposed to SW triggering)? Then work
 * queues are required.
 */

#if (CONFIG_TIVA_ADC0_SSE0_TRIGGER > 0) || (CONFIG_TIVA_ADC0_SSE1_TRIGGER > 0) || \
    (CONFIG_TIVA_ADC0_SSE2_TRIGGER > 0) || (CONFIG_TIVA_ADC0_SSE3_TRIGGER > 0) || \
    (CONFIG_TIVA_ADC0_SSE0_TRIGGER > 0) || (CONFIG_TIVA_ADC0_SSE1_TRIGGER > 0) || \
    (CONFIG_TIVA_ADC0_SSE2_TRIGGER > 0) || (CONFIG_TIVA_ADC0_SSE3_TRIGGER > 0)
#  define TIVA_ADC_HAVE_INTERRUPTS 1
#endif

#ifdef TIVA_ADC_HAVE_INTERRUPTS
#  ifndef CONFIG_SCHED_WORKQUEUE
#    error Work queue support is required (CONFIG_SCHED_WORKQUEUE) for ADC interrupts
#  endif
#  ifndef CONFIG_SCHED_HPWORK
#    error High priority worker threads is required (CONFIG_SCHED_HPWORK) for ADC interrupts
#  endif
#endif


#ifndef CONFIG_DEBUG
#  undef CONFIG_TIVA_ADC_REGDEBUG
#endif


/* Misc utility defines *****************************************************/

#define TIVA_ADC_ENABLE  true
#define TIVA_ADC_DISABLE false

#define TIVA_ADC_RESOLUTION 4095

#ifdef CONFIG_ARCH_CHIP_TM4C123
#  define TIVA_ADC_CLOCK_MAX (16000000)
#  define TIVA_ADC_CLOCK_MIN (16000000)
#elif CONFIG_ARCH_CHIP_TM4C129
#  define TIVA_ADC_CLOCK_MAX (32000000)
#  define TIVA_ADC_CLOCK_MIN (16000000)
#else
#  error TIVA_ADC_CLOCK: unsupported architecture
#endif

/* Allow the same function call to be used for sample rate */

#ifdef CONFIG_ARCH_CHIP_TM4C123
#  define TIVA_ADC_SAMPLE_RATE_SLOWEST  (ADC_PC_SR_125K)
#  define TIVA_ADC_SAMPLE_RATE_SLOW     (ADC_PC_SR_250K)
#  define TIVA_ADC_SAMPLE_RATE_FAST     (ADC_PC_SR_500K)
#  define TIVA_ADC_SAMPLE_RATE_FASTEST  (ADC_PC_SR_1M)
#elif CONFIG_ARCH_CHIP_TM4C129
#  define TIVA_ADC_SAMPLE_RATE_SLOWEST  (ADC_PC_MCR_1_8)
#  define TIVA_ADC_SAMPLE_RATE_SLOW     (ADC_PC_MCR_1_4)
#  define TIVA_ADC_SAMPLE_RATE_FAST     (ADC_PC_MCR_1_2)
#  define TIVA_ADC_SAMPLE_RATE_FASTEST  (ADC_PC_MCR_FULL)
#else
#  error TIVA_ADC_SAMPLE_RATE: unsupported architecture
#endif

/* Utility macros ***********************************************************/

/* PWM trigger support definitions ******************************************/

/* Decodes the PWM generator and module from trigger and converts
 * to the TSSEL_PS register
 */

#define ADC_TRIG_PWM_CFG(t) \
    (1<<(ADC_TSSEL_PS_SHIFT(ADC_TRIG_gen(t))))

/* ADC support definitions **************************************************/

#define ADC_CHN_AIN(n)   GPIO_ADC_AIN##n
#define TIVA_ADC_PIN(n)  ADC_CHN_AIN(n)

#define SSE_PROC_TRIG(n)  (1 << (n))
#define SSE_PROC_TRIG_ALL (0xF)

#define SSE_IDX(a,s)      (((a)*SSE_PER_BASE) + (s))

#define MAX_NORMAL_CHN 15
#define BASE_PER_ADC   2
#define SSE_PER_BASE   4
#define SSE_MAX_STEP   8
#define NUM_SSE(n)     (sizeof(n)/sizeof(n[0]))

#define GET_AIN(a,s,c) (uint8_t)((getreg32( \
    TIVA_ADC_BASE(a)+TIVA_ADC_SSMUX(s)) & ADC_SSMUX_MUX_MASK(c)) >> ADC_SSMUX_MUX_SHIFT(c))

#define ADC_SSE_STEP_NULL 0xFF

#define CLOCK_CONFIG(div, src) \
    ( ((((div) << ADC_CC_CLKDIV_SHIFT) & ADC_CC_CLKDIV_MASK) | \
    ((src) & ADC_CC_CS_MASK)) & (ADC_CC_CLKDIV_MASK + ADC_CC_CS_MASK) )

#define SEM_PROCESS_PRIVATE 0
#define SEM_PROCESS_SHARED  1

/* Debug ********************************************************************/

#ifndef CONFIG_DEBUG
#  undef CONFIG_TIVA_ADC_REGDEBUG
#endif

/* ADC event trace logic.  NOTE:  trace uses the internal, non-standard, low-level
 * debug interface syslog() but does not require that any other debug
 * is enabled.
 */

#ifndef CONFIG_ADC_TRACE
#  define tiva_adc_tracereset(p)
#  define tiva_adc_tracenew(p,s)
#  define tiva_adc_traceevent(p,e,a)
#  define tiva_adc_tracedump(p)
#endif

#ifndef CONFIG_ADC_NTRACE
#  define CONFIG_ADC_NTRACE 32
#endif

/****************************************************************************
 * Public Functions
 * **************************************************************************/

/* Upper level ADC driver ***************************************************/

static void tiva_adc_reset(struct adc_dev_s *dev);
static int tiva_adc_setup(struct adc_dev_s *dev);
static void tiva_adc_shutdown(struct adc_dev_s *dev);
static void tiva_adc_rxint(struct adc_dev_s *dev, bool enable);
static int tiva_adc_ioctl(struct adc_dev_s *dev, int cmd, unsigned long arg);

/****************************************************************************
 * Public Data
 ****************************************************************************/

/* ADC lower half device operations */

static const struct adc_ops_s g_adcops =
{
  .ao_reset = tiva_adc_reset,
  .ao_setup = tiva_adc_setup,
  .ao_shutdown = tiva_adc_shutdown,
  .ao_rxint = tiva_adc_rxint,
  .ao_ioctl = tiva_adc_ioctl,
};

/****************************************************************************
 * Private Types
 ****************************************************************************/

/* tracks overall ADC peripherals one-time initialization state */
struct tiva_adc_state_s
{
  bool init[BASE_PER_ADC];
  bool sse[BASE_PER_ADC * SSE_PER_BASE];
};

struct tiva_adc_s
{
  struct adc_dev_s *dev;
  bool ena;                   /* Operation state */
  uint8_t devno;              /* ADC device number */
  struct tiva_adc_sse_s *sse[SSE_PER_BASE];   /* Sample sequencer operation
                                               * state */
#ifdef CONFIG_TIVA_ADC_REGDEBUG
  /* Debug stuff */

  bool wrlast;                /* Last was a write */
  uintptr_t addrlast;         /* Last address */
  uint32_t vallast;           /* Last value */
  int ntimes;                 /* Number of times */
#endif                        /* CONFIG_TIVA_ADC_REGDEBUG */
};

struct tiva_adc_sse_s
{
  struct tiva_adc_s *adc;     /* Parent peripheral */
#ifdef TIVA_ADC_HAVE_INTERRUPTS
  sem_t exclsem;              /* Mutual exclusion semaphore */
  struct work_s work;         /* Supports the interrupt handling "bottom half" */
#endif
  bool ena;                   /* Sample sequencer operation state */
  uint32_t irq;               /* SSE interrupt vectors */
  uint8_t num;                /* SSE number */
};

/****************************************************************************
 * Private Function Definitions
 ****************************************************************************/

/* Debug ADC functions **********************************************/

#if defined(CONFIG_TIVA_ADC_REGDEBUG) && defined(CONFIG_DEBUG)
static bool tiva_adc_checkreg(struct tiva_adc_s *priv, bool wr,
                              uint32_t regval, uintptr_t address);
#endif

#ifdef CONFIG_TIVA_ADC_REGDEBUG
static void tiva_adc_modifyreg(struct tiva_adc_s *priv, unsigned int addr,
                               uint32_t clearbits, uint32_t setbits);
#else
#  define tiva_adc_modifyreg(priv,addr,clearbits,setbits) modifyreg32(addr,clearbits,setbits)
#endif

/* TM4C-specific ADC functions **********************************************/

/* Common peripheral level */

static int adc_state(struct tiva_adc_s *adc, bool state);
static void adc_clock(uint32_t freq);
#ifdef CONFIG_ARCH_CHIP_TM4C129
static void adc_vref(uint8_t vref);
#endif
#ifdef CONFIG_TIVA_ADC_INTERRUPTS
static void tiva_adc_read(void *arg);
#endif

/* Peripheral (base) level */

static void adc_sample_rate(uint8_t rate);
static void adc_proc_trig(struct tiva_adc_s *adc, uint8_t sse_mask);
static uint32_t adc_int_status(struct tiva_adc_s *adc);

/* Sample Sequencer (SSE) level */

static void sse_state(struct tiva_adc_s *adc, uint8_t sse, bool state);
static void sse_trigger(struct tiva_adc_s *adc, uint8_t sse, uint32_t trigger);
#ifdef CONFIG_EXPERIMENTAL
static void sse_pwm_trig_ioctl(struct tiva_adc_s *adc, uint8_t sse, uint32_t cfg);
#endif
static int sse_data(struct tiva_adc_s *adc, uint8_t sse);
static void sse_priority(struct tiva_adc_s *adc, uint8_t sse, uint8_t priority);

static void sse_int_state(struct tiva_adc_s *adc, uint8_t sse, bool state);
static bool sse_int_status(struct tiva_adc_s *adc, uint8_t sse);
static void sse_clear_int(struct tiva_adc_s *adc, uint8_t sse);

static void sse_register_chn(struct tiva_adc_s *adc, uint8_t sse, uint8_t chn,
                             uint32_t ain);
static void sse_differential(struct tiva_adc_s *adc, uint8_t sse, uint8_t chn,
                             uint32_t diff);
#ifdef CONFIG_EXPERIMENTAL
static void sse_sample_hold_time(struct tiva_adc_s *adc, uint8_t sse,
                                 uint8_t chn, uint32_t shold);
#endif
static void sse_step_cfg(struct tiva_adc_s *adc, uint8_t sse, uint8_t chn,
                         uint8_t cfg);

/* Helper functions *********************************************************/

#ifdef CONFIG_TIVA_ADC0
static void tiva_adc0_sse_init(void);
static void tiva_adc0_assign_channels(void);
#  ifdef TIVA_ADC_HAVE_INTERRUPTS
static void tiva_adc0_assign_interrupts(void);
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE0
static void adc0_sse0_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc0_sse0_interrupt(int irq, void *context);
#    endif
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE1
static void adc0_sse1_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc0_sse1_interrupt(int irq, void *context);
#    endif
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE2
static void adc0_sse2_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc0_sse2_interrupt(int irq, void *context);
#    endif
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE3
static void adc0_sse3_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc0_sse3_interrupt(int irq, void *context);
#    endif
#  endif
#endif

#ifdef CONFIG_TIVA_ADC1
static void tiva_adc1_sse_init(void);
static void tiva_adc1_assign_channels(void);
#  ifdef TIVA_ADC_HAVE_INTERRUPTS
static void tiva_adc1_assign_interrupts(void);
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE0
static void adc1_sse0_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc1_sse0_interrupt(int irq, void *context);
#    endif
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE1
static void adc1_sse1_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc1_sse1_interrupt(int irq, void *context);
#    endif
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE2
static void adc1_sse2_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc1_sse2_interrupt(int irq, void *context);
#    endif
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE3
static void adc1_sse3_chn_cfg(void);
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
static void adc1_sse3_interrupt(int irq, void *context);
#    endif
#  endif
#endif

/****************************************************************************
 * Private Data
 ****************************************************************************/

/* Tracks overall peripheral state */

static struct tiva_adc_state_s adc_common =
{
  .init =
  {
    false,
    false
  },
  .sse =
  {
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false
  },
};

#ifdef CONFIG_TIVA_ADC0

/* ADC device instance 0 */

static struct adc_dev_s g_adcdev0;
static struct tiva_adc_s adc0;
#  ifdef CONFIG_TIVA_ADC0_SSE0
static struct tiva_adc_sse_s sse00;
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE1
static struct tiva_adc_sse_s sse01;
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE2
static struct tiva_adc_sse_s sse02;
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE3
static struct tiva_adc_sse_s sse03;
#  endif
#endif

#ifdef CONFIG_TIVA_ADC1

/* ADC device instance 1 */

static struct adc_dev_s g_adcdev1;
static struct tiva_adc_s adc1;
#  ifdef CONFIG_TIVA_ADC1_SSE0
static struct tiva_adc_sse_s sse10;
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE1
static struct tiva_adc_sse_s sse11;
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE2
static struct tiva_adc_sse_s sse12;
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE3
static struct tiva_adc_sse_s sse13;
#  endif
#endif

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: tiva_adc_reset
 *
 * Description:
 *   Reset the ADC device. Called early to initialize the hardware. This is
 *   called before tiva_adc_setup() and on error conditions.
 *
 ****************************************************************************/

static void tiva_adc_reset(struct adc_dev_s *dev)
{
  struct tiva_adc_s *priv = (struct tiva_adc_s *)dev->ad_priv;

  avdbg("Resetting...\n");

  /* Only if ADCs are active do we run the reset routine: - disable ADC
   * interrupts - clear interrupt bits - disable all active sequences
   * Otherwise, if the peripheral is inactive, perform no operations since
   * register access to a peripheral that is not active will result in a
   * segmentation fault.
   */

  if (priv->ena)
    {
      tiva_adc_rxint(dev, TIVA_ADC_DISABLE);
      uint8_t s;
      for (s = 0; s < SSE_PER_BASE; ++s)
        {
          if (adc_common.sse[SSE_IDX(priv->devno, s)])
            {
              sse_state(priv, s, TIVA_ADC_DISABLE);
            }
        }
    }
}

/****************************************************************************
 * Name: tiva_adc_setup
 *
 * Description:
 *   Configure the ADC. This method is called the first time that the ADC
 *   device is opened. This will occur when the port is first opened.
 *   this setup includes configuring and attaching ADC interrupts. Interrupts
 *   are all disabled upon return.
 *
 * Returned Value:
 *   Non negative value on success; negative value on failure.
 *
 ****************************************************************************/

static int tiva_adc_setup(struct adc_dev_s *dev)
{
  avdbg("Setup\n");

  /* Only if ADCs are active do we run the reset routine: - enable ADC
   * interrupts - clear interrupt bits - enable all active sequences - register
   * triggers and respective interrupt handlers Otherwise, if the peripheral is
   * inactive, perform no operations since register access to a peripheral that
   * is not active will result in a segmentation fault.
   */

  struct tiva_adc_s *priv = (struct tiva_adc_s *)dev->ad_priv;
  uint8_t s = 0;
  for (s = 0; s < SSE_PER_BASE; ++s)
    {
      if (adc_common.sse[SSE_IDX(priv->devno, s)])
        {
          sse_state(priv, s, true);
        }
    }

  tiva_adc_rxint(dev, false);
  return OK;
}

/****************************************************************************
 * Name: tiva_adc_shutdown
 *
 * Description:
 *   Disable the ADC. This method is called when the ADC device is closed.
 *   This method reverses the operation the setup method.
 *
 ****************************************************************************/

static void tiva_adc_shutdown(struct adc_dev_s *dev)
{
  struct tiva_adc_s *priv = (struct tiva_adc_s *)dev->ad_priv;

  avdbg("Shutdown\n");

  /* Reset the ADC peripheral */

  tiva_adc_reset(dev);

  uint8_t s = 0;
  for (s = 0; s < SSE_PER_BASE; ++s)
    {
      if (adc_common.sse[SSE_IDX(priv->devno, s)])
        {
          /* Disable ADC interrupts at the level of the AIC */

          up_disable_irq(priv->sse[s]->irq);

          /* Then detach the ADC interrupt handler. */

          irq_detach(priv->sse[s]->irq);
        }
    }
}

/****************************************************************************
 * Name: tiva_adc_rxint
 *
 * Description:
 *   Call to enable or disable RX interrupts
 *
 * Input Parameters:
 *   enable - the enable state of interrupts for this device
 *
 ****************************************************************************/

static void tiva_adc_rxint(struct adc_dev_s *dev, bool enable)
{
  struct tiva_adc_s *priv = (struct tiva_adc_s *)dev->ad_priv;

  avdbg("rx enable=%d\n", enable);

  uint8_t s = 0;
  for (s = 0; s < SSE_PER_BASE; ++s)
    {
      uint32_t trigger =
        (tiva_adc_getreg(priv, TIVA_ADC_EMUX(priv->devno)) >> s) & 0xF;
      if (adc_common.sse[SSE_IDX(priv->devno, s)] && (trigger > 0))
        {
          sse_int_state(priv, s, enable);
        }
    }
}

/****************************************************************************
 * Name: tiva_adc_ioctl
 *
 * Description:
 *   All ioctl calls will be routed through this method.
 *
 * Input Parameters:
 *   cmd - ADC ioctl command
 *   arg - argument for the ioctl command
 *
 * Returned Value:
 *   Non negative value on success; negative value on failure.
 *
 ****************************************************************************/

static int tiva_adc_ioctl(struct adc_dev_s *dev, int cmd, unsigned long arg)
{
  struct tiva_adc_s *priv = (struct tiva_adc_s *)dev->ad_priv;
  int ret = OK;
  uint32_t regval = 0;
  uint8_t sse = 0;

  avdbg("cmd=%d arg=%ld\n", cmd, arg);

  switch (cmd)
    {
      /* Software trigger */

      case ANIOC_TRIGGER:

        sse = (uint8_t) arg;

        /* start conversion and read to buffer */

        adc_proc_trig(priv, (uint8_t) SSE_PROC_TRIG(sse));
        regval = adc_int_status(priv) & (1 << sse);
        while (!regval)
          {
            regval = adc_int_status(priv) & (1 << sse);
          }

        sse_clear_int(priv, sse);
        sse_data(priv, sse);
        break;

      /* PWM triggering */

#warning Missing Logic

      /* TODO: Needs to be tested */

#ifdef CONFIG_EXPERIMENTAL

      case TIVA_ADC_PWM_TRIG_IOCTL:

        /* Verify input SSE trigger is a PWM trigger */

        sse = (uint8_t)(arg & 0x2);
        regval = (tiva_adc_getreg(priv, (TIVA_ADC_EMUX(adc->devno))) >>
            ADC_EMUX_SHIFT(sse)) & ADC_EMUX_MASK(sse);

        if ((regval == ADC_EMUX_PWM0) ||
            (regval == ADC_EMUX_PWM1) ||
            (regval == ADC_EMUX_PWM2) ||
            (regval == ADC_EMUX_PWM3))
          {
            sse_pwm_trig_ioctl(priv, sse, (uint32_t)(arg&0xFFFFFFFC));
          }

        break;

#endif

#warning Missing Logic

      /* Unsupported or invalid command */

      default:
        ret = -ENOTTY;
        break;
    }

  return ret;
}

/****************************************************************************
 * Name: tiva_adc_read
 *
 * Description:
 *   This function executes on the worker thread.  It is scheduled by
 *   sam_adc_interrupt whenever any enabled event occurs. All interrupts
 *   are disabled when this function runs. tiva_adc_read will
 *   re-enable interrupts when it completes processing all pending events.
 *
 * Input Parameters
 *   arg - The ADC SSE data structure cast to (void *)
 *
 * Returned Value:
 *   None
 *
 ****************************************************************************/

static void tiva_adc_read(void *arg)
{
  struct tiva_adc_sse_s *sse = (struct tiva_adc_sse_s *)arg;
  uint32_t data = 0;
  uint8_t fifo_count = 0;

  /* Get exclusive access to the driver data structure */

  tiva_adc_lock(sse->adc, sse->num);

  /* Get sampled data */

  while (!(tiva_adc_getreg(&adc0, TIVA_ADC_BASE(0) + TIVA_ADC_SSFSTAT(0)) &
          ADC_SSFSTAT_EMPTY) && fifo_count < SSE_MAX_STEP)
    {
      data = tiva_adc_getreg(&adc0, TIVA_ADC_BASE(0) + TIVA_ADC_SSFIFO(0));
      (void)adc_receive(adc0.dev, GET_AIN(0, 0, fifo_count), data);
      ++fifo_count;
    }

  /* Exit, re-enabling ADC interrupts */

  sse_int_state(&adc0, 0, TIVA_ADC_ENABLE);

  /* Release our lock on the ADC structure */

  tiva_adc_unlock(sse->adc, sse->num);
}

/****************************************************************************
 * Register Operations
 ****************************************************************************/

#ifdef CONFIG_TIVA_ADC_REGDEBUG

/****************************************************************************
 * Name: tiva_adc_checkreg
 *
 * Description:
 *   Check if the current register access is a duplicate of the preceding.
 *
 * Input Parameters:
 *   regval  - The value to be written
 *   address - The address of the register to write to
 *
 * Returned Value:
 *   true:  This is the first register access of this type.
 *   flase: This is the same as the preceding register access.
 *
 ****************************************************************************/

static bool tiva_adc_checkreg(struct tiva_adc_s *priv, bool wr,
                              uint32_t regval, uintptr_t address)
{
  if (wr == priv->wrlast &&             /* Same kind of access? */
      regval == priv->vallast &&        /* Same value? */
      address == priv->addrlast)        /* Same address? */
    {
      /* Yes, then just keep a count of the number of times we did this. */

      priv->ntimes++;
      return false;
    }
  else
    {
      /* Did we do the previous operation more than once? */

      if (priv->ntimes > 0)
        {
          /* Yes... show how many times we did it */

          lldbg("...[Repeats %d times]...\n", priv->ntimes);
        }

      /* Save information about the new access */

      priv->wrlast = wr;
      priv->vallast = regval;
      priv->addrlast = address;
      priv->ntimes = 0;
    }

  /* Return true if this is the first time that we have done this operation */

  return true;
}
#endif /* CONFIG_TIVA_ADC_REGDEBUG */

/****************************************************************************
 * Name: tiva_adc_modifyreg
 *
 * Description:
 *   Atomically modify the specified bits in a memory mapped register
 *
 * Input Parameters:
 *   addr - The address of the register to write to
 *
 ****************************************************************************/

#ifdef CONFIG_TIVA_ADC_REGDEBUG
static void tiva_adc_modifyreg(struct tiva_adc_s *priv, unsigned int addr,
                               uint32_t clearbits, uint32_t setbits)
{
  uint32_t regval = 0;
  irqstate_t flags = irqsave();
  regval = tiva_adc_getreg(priv, addr);
  regval &= ~clearbits;
  regval |= setbits;
  tiva_adc_putreg(priv, addr, regval);
  irqrestore(flags);
}
#endif /* CONFIG_TIVA_ADC_REGDEBUG */

/* TM4C-specific ADC functions **********************************************/

/* Peripheral (base) level **************************************************/

/****************************************************************************
 * Name: adc_state
 *
 * Description:
 *  Toggles the operational state of the ADC peripheral
 *
 * Input Parameters:
 *  state - operation state
 *
 ****************************************************************************/

static int adc_state(struct tiva_adc_s *adc, bool state)
{
  if (state == TIVA_ADC_ENABLE)
    {
      /* Enable clocking to the ADC peripheral */

#ifdef TIVA_SYSCON_RCGCADC
      modifyreg32(TIVA_SYSCON_RCGCADC, 0, 1 << adc->devno);
#else
      modifyreg32(TIVA_SYSCON_RCGC0, 0, SYSCON_RCGC0_ADC0);
#endif
      return OK;
    }
  else if (state == TIVA_ADC_DISABLE)
    {
      /* Disable clocking to the ADC peripheral */

#ifdef TIVA_SYSCON_RCGCADC
      modifyreg32(TIVA_SYSCON_RCGCADC, 1 << adc->devno, 0);
#else
      modifyreg32(TIVA_SYSCON_RCGC0, SYSCON_RCGC0_ADC0, 0);
#endif
      return OK;
    }

  /* ERROR! */

  return -1;
}

/****************************************************************************
 * Name: adc_clock
 *
 * Description:
 *  Sets the ADC peripherals clock to the desired frequency.
 *
 * Input Parameters:
 *  freq - ADC clock value; dependent on platform:
 *
 *  TM4C123 - Select either MOSC or PIOSC. Both result in 16 MHz operation,
 *  however the PIOSC allows the ADC to operate in deep sleep mode.
 *
 *  TM4C129 - For the 129, there is still a selection between various internal
 *  clocks, however the output frequency is variable (16 MHz - 32 MHz); so it
 *  is much more intuitive to allow the clock variable be a frequency value.
 *
 ****************************************************************************/

static void adc_clock(uint32_t freq)
{
#if defined(CONFIG_ARCH_CHIP_TM4C123)
  /* For the TM4C123, the ADC clock source does not affect the frequency, it
   * runs at 16 MHz regardless. You end up selecting between the MOSC (default)
   * or the PIOSC. The PIOSC allows the ADC to operate even in deep sleep mode.
   * Since this is the case, the clock value for
   */

  uintptr_t ccreg = (TIVA_ADC0_BASE + TIVA_ADC_CC_OFFSET);
  modifyreg32(ccreg, 0, (freq & ADC_CC_CS_MASK));

#elif defined (CONFIG_ARCH_CHIP_TM4C129)
  /* check clock bounds and specific match cases */

  uint32_t clk_src = 0;
  uint32_t div = 0;
  if (clock > TIVA_ADC_CLOCK_MAX)
    {
      clk_src = ADC_CC_CS_SYSPLL;
      div = (BOARD_FVCO_FREQUENCY / TIVA_ADC_CLOCK_MAX);
    }
  else if (clock < TIVA_ADC_CLOCK_MIN)
    {
      clk_src = ADC_CC_CS_PIOSC;
      div = 1;
    }
  else if (clock == XTAL_FREQUENCY)
    {
      clk_src = ADC_CC_CS_MOSC;
      div = 1;
    }
  else
    {
      clk_src = ADC_CC_CS_SYSPLL;
      div = (BOARD_FVCO_FREQUENCY / freq);
    }

  uintptr_t ccreg = (TIVA_ADC0_BASE + TIVA_ADC_CC_OFFSET);
  modifyreg32(ccreg, 0, CLOCK_CONFIG(div, clk_src));
#else
#  error Unsupported architecture reported
#endif
}

/****************************************************************************
 * Name: adc_vref
 *
 * Description:
 *  Sets the ADC peripherals clock to the desired frequency.
 *
 * Input Parameters:
 *  vref - ADC clock voltage reference source
 *
 ****************************************************************************/

#ifdef CONFIG_ARCH_CHIP_TM4C129
static void adc_vref(uint8_t vref)
{
  uintptr_t ctlreg = (TIVA_ADC0_BASE + TIVA_ADC_CTL_OFFSET);
  if (vref == 0)
    {
      modifyreg32(ctlreg, vref, 0);
    }
  else
    {
      modifyreg32(ctlreg, 0, vref);
    }
}
#endif

/****************************************************************************
 * Name: adc_sample_rate
 *
 * Description:
 *  Sets the ADC sample rate as follows for each processor.
 *  TM4C123 - by maximum samples: 125 ksps, 250 ksps, 500 ksps or 1 Msps
 *  TM4C129 - by a divisor either being full, half, quarter or
 *  an eighth.
 *
 * Input Parameters:
 *  rate - ADC sample rate divisor
 *
 ****************************************************************************/

static void adc_sample_rate(uint8_t rate)
{
  uintptr_t pcreg = (TIVA_ADC0_BASE + TIVA_ADC_PC_OFFSET);

  /* NOTE: ADC_PC_SR_MASK is intended for use with the TM4C123, the
   * alternative is ADC_PC_MCR_MASK for the TM4C129. However both masks
   * mask off the first 4 bits (0xF) so there is no need to distinguish
   * between the two.
   */

  modifyreg32(pcreg, 0, (rate & ADC_PC_SR_MASK));
}

/****************************************************************************
 * Name: adc_proc_trig
 *
 * Description:
 *   Triggers the sample sequence to start it's conversion(s) and store them
 *   to the FIFO. This is only required when the trigger source is set to the
 *   processor.
 *
 * Input parameters:
 *   adc - which ADC peripherals' sample sequencers to trigger
 *   sse_mask - sample sequencer bitmask, each sse is 1 shifted by the sse
 *              number. e.g.
 *              SSE0 = 1 << 0
 *              SSE1 = 1 << 1
 *              SSE2 = 1 << 2
 *              SSE3 = 1 << 3
 *
 ****************************************************************************/

static void adc_proc_trig(struct tiva_adc_s *adc, uint8_t sse_mask)
{
  uintptr_t pssireg = TIVA_ADC_PSSI(adc->devno);
  tiva_adc_modifyreg(adc, pssireg, 0, sse_mask);
#ifdef CONFIG_TIVA_ADC_SYNC
#  warning CONFIG_TIVA_ADC_SYNC unsupported at this time.
#endif
}

/****************************************************************************
 * Name: adc_int_status
 *
 * Description:
 *   Returns raw interrupt status for the input ADC
 *
 * Input parameters:
 *   adc - which ADC peripherals' interrupt status to retrieve
 *
 ****************************************************************************/

static uint32_t adc_int_status(struct tiva_adc_s *adc)
{
  return tiva_adc_getreg(adc, TIVA_ADC_RIS(adc->devno));
}

/* Sample sequencer (SSE) functions *****************************************/

/****************************************************************************
 * Name: sse_state
 *
 * Description:
 *   Sets the operation state of an ADC's sample sequencer (SSE). SSEs must
 *   be configured before being enabled.
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   state - sample sequencer enable/disable state
 *
 ****************************************************************************/

static void sse_state(struct tiva_adc_s *adc, uint8_t sse, bool state)
{
  uintptr_t actssreg = TIVA_ADC_ACTSS(adc->devno);
  if (state == TIVA_ADC_ENABLE)
    {
      tiva_adc_modifyreg(adc, actssreg, 0, (1 << sse));
    }
  else
    {
      tiva_adc_modifyreg(adc, actssreg, (1 << sse), 0);
    }

  adc->sse[sse]->ena = state;
}

/****************************************************************************
 * Name: sse_trigger
 *
 * Description:
 *   Sets the trigger configuration for an ADC's sample sequencer (SSE).
 *   Possible triggers are the following:
 *      - Processor
 *      - PWMs, requires that one of the PWMnn_TRIG_CFG defines be OR'd
 *        into the trigger value.
 *      - Timers
 *      - GPIO (which GPIO is platform specific, consult the datasheet)
 *      - Always
 *      - !!UNSUPPORTED: Comparators
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   trigger - interrupt trigger
 *
 ****************************************************************************/

static void sse_trigger(struct tiva_adc_s *adc, uint8_t sse, uint32_t trigger)
{
  uintptr_t emuxreg = (TIVA_ADC_EMUX(adc->devno));
  uint32_t trig = 0;
  if ((trigger & ADC_EMUX_MASK(0)) == 0)
    {
      /* The 0 value is a special case since using modifyregn() results in an
       * ORing of the register value; we need to unset those bits if it's a 0.
       */

      tiva_adc_modifyreg(adc, emuxreg, (0xF << sse), 0);
    }
  else
    {
      trig = ((trigger << ADC_EMUX_SHIFT(sse)) & ADC_EMUX_MASK(sse));
      tiva_adc_modifyreg(adc, emuxreg, 0, trig);
    }

  /* NOTE: PWM triggering needs an additional register to be set (ADC_TSSEL)
   * A platform specific IOCTL command is provided to configure the triggering.
   */
}

/****************************************************************************
 * Name: sse_pwm_trig_ioctl
 *
 * Description:
 *   Additional triggering configuration for PWM. Sets which PWM and which
 *   generator.
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   cfg - which PWM modulator and generator to use, use TIVA_ADC_PWM_TRIG
 *         to encode the value correctly
 *
 ****************************************************************************/

#ifdef CONFIG_EXPERIMENTAL
static void sse_pwm_trig_ioctl(struct tiva_adc_s *adc, uint8_t sse, uint32_t cfg)
{
  /* PWM triggering needs an additional register to be set (ADC_TSSEL) */

  uintptr_t tsselreg = TIVA_ADC_TSSEL(adc->devno);

  if ((cfg & ADC_EMUX_MASK(0)) == 1)
    {
      tiva_adc_modifyreg(adc, tsselreg, 0, cfg);
    }
  else
    {
      tiva_adc_modifyreg(adc, tsselreg, cfg, 0);
    }
}
#endif

/****************************************************************************
 * Name: sse_int
 *
 * Description:
 *   Sets the interrupt state of an ADC's sample sequencer (SSE). SSEs must
 *   be enabled before setting interrupt state.
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   state - sample sequencer enable/disable interrupt state
 *
 ****************************************************************************/

static void sse_int_state(struct tiva_adc_s *adc, uint8_t sse, bool state)
{
  uintptr_t imreg = TIVA_ADC_IM(adc->devno);
  if (adc->sse[sse])
    {
      sse_clear_int(adc, sse);
    }
  if (state == TIVA_ADC_ENABLE)
    {
      tiva_adc_modifyreg(adc, imreg, 0, (1 << sse));
    }
  else
    {
      tiva_adc_modifyreg(adc, imreg, (1 << sse), 0);
    }
}

/****************************************************************************
 * Name: sse_int_status
 *
 * Description:
 *   Returns interrupt status for the specificed SSE
 *
 * Input parameters:
 *   adc - which ADC peripherals' interrupt status to retrieve
 *   sse - which SSE interrupt status to retrieve
 *
 ****************************************************************************/

static bool sse_int_status(struct tiva_adc_s *adc, uint8_t sse)
{
  return (adc_int_status(adc) & (1 << sse)) > 0 ? true : false;
}

/****************************************************************************
 * Name: sse_clear_int
 *
 * Description:
 *   Clears the interrupt bit for the SSE.
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   state - sample sequencer
 *
 ****************************************************************************/

static void sse_clear_int(struct tiva_adc_s *adc, uint8_t sse)
{
  uintptr_t iscreg = TIVA_ADC_ISC(adc->devno);
  tiva_adc_modifyreg(adc, iscreg, 0, (1 << sse));
}

/****************************************************************************
 * Name: sse_data
 *
 * Description:
 *   Retrieves data from the FIFOs for all steps in the given sample sequencer.
 *   The input data buffer MUST be as large or larger than the sample sequencer.
 *   otherwise
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *
 * Return value:
 *   number of steps read from FIFO.
 *
 ****************************************************************************/

static int sse_data(struct tiva_adc_s *adc, uint8_t sse)
{
  uint32_t ssfstatreg =
    tiva_adc_getreg(adc, TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSFSTAT(sse));
  int32_t data = 0;
  uint8_t fifo_count = 0;

  /* Read samples from the FIFO until it is empty */

  while (!(ssfstatreg & ADC_SSFSTAT_EMPTY) && fifo_count < SSE_MAX_STEP)
    {
      /* Read the FIFO and copy it to the destination */

      data =
        tiva_adc_getreg(adc, TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSFIFO(sse));
      (void)adc_receive(adc->dev, GET_AIN(adc->devno, sse, fifo_count), data);
      fifo_count++;

      /* refresh fifo status register state */

      ssfstatreg =
        tiva_adc_getreg(adc, TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSFSTAT(sse));
    }

  return fifo_count;
}

/****************************************************************************
 * Name: sse_priority
 *
 * Description:
 *   Sets the priority configuration for an ADC's sample sequencer (SSE). The
 *   priority value ranges from 0 to 3, 0 being the highest priority, 3 being
 *   the lowest. There can be no duplicate values.
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   priority - conversion priority
 *
 ****************************************************************************/

static void sse_priority(struct tiva_adc_s *adc, uint8_t sse, uint8_t priority)
{
  uintptr_t ssprireg = TIVA_ADC_SSPRI(adc->devno);
  uint32_t sspri = 0;

  if (priority == 0)
    {
      /* The 0 value is a special case since using modifyregn() results in an
       * ORing of the register value; we need to unset those bits if it's a 0.
       */

      sspri = (ADC_SSPRI_MASK(sse) & (0x3 << ADC_SSPRI_SHIFT(sse)));
      tiva_adc_modifyreg(adc, ssprireg, sspri, 0);
    }
  else
    {
      sspri = (ADC_SSPRI_MASK(sse) & (priority << ADC_SSPRI_SHIFT(sse)));
      tiva_adc_modifyreg(adc, ssprireg, 0, sspri);
    }
}

/****************************************************************************
 * Name: sse_register_chn
 *
 * Description:
 *   Registers an input channel to an SSE. Channels are registered according
 *   to the step and channel values stored in the channel struct. If the SSE
 *   already has a channel registered, it is overwritten by the new channel.
 *
 *   *SSEMUX only supported on TM4C129 devices
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   chn - sample sequencer step
 *   ain - analog input pin
 *
 ****************************************************************************/

static void sse_register_chn(struct tiva_adc_s *adc, uint8_t sse, uint8_t chn,
                             uint32_t ain)
{
  /* Configure SSE mux (SSMUX) with step number */

  uintptr_t ssmuxreg = (TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSMUX(sse));
  uint32_t step = 0;

  if (ain > 0)
    {
      step = ((ain << ADC_SSMUX_MUX_SHIFT(chn)) & ADC_SSMUX_MUX_MASK(chn));
      tiva_adc_modifyreg(adc, ssmuxreg, 0, step);
    }
  else
    {
      step = ((0xF << ADC_SSMUX_MUX_SHIFT(chn)) & ADC_SSMUX_MUX_MASK(chn));
      tiva_adc_modifyreg(adc, ssmuxreg, step, 0);
    }

#ifdef CONFIG_ARCH_CHIP_TM4C129
  /* Configure SSE extended mux (SSEMUX) with step number and configuration */

  ssmuxreg = (TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSMUX(sse));
  step = ((1 << ADC_SSEMUX_MUX_SHIFT(chn)) & ADC_SSEMUX_MUX_MASK(chn));
  if (chn > MAX_NORMAL_CHN)
    {
      tiva_adc_modifyreg(adc, ssmuxreg, 0, step);
    }
  else
    {
      tiva_adc_modifyreg(adc, ssmuxreg, step, 0);
    }
#endif
}

/****************************************************************************
 * Name: sse_differential
 *
 * Description:
 *   Sets the differential capability for a SSE. !! UNSUPPORTED
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   chn - sample sequencer channel
 *   diff - differential configuration
 *
 ****************************************************************************/

static void sse_differential(struct tiva_adc_s *adc, uint8_t sse, uint8_t chn,
                             uint32_t diff)
{
#ifdef CONFIG_TIVA_ADC_DIFFERENTIAL
#  error CONFIG_TIVA_ADC_DIFFERENTIAL unsupported!!
#else

  /* for now, ensure the FIFO is used and differential sampling is disabled */

  uintptr_t ssopreg = (TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSOP(sse));
  uint32_t sdcopcfg = (1 << chn);
  tiva_adc_modifyreg(adc, ssopreg, sdcopcfg, 0);
#endif
}

/****************************************************************************
 * Name: sse_sample_hold_time
 *
 * Description:
 *  Set the sample and hold time for this step.
 *
 *  This is not available on all devices, however on devices that do not
 *  support this feature these reserved bits are ignored on write access.
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   chn - sample sequencer channel
 *   shold - sample and hold time
 *
 ****************************************************************************/

#ifdef CONFIG_EXPERIMENTAL
static void sse_sample_hold_time(struct tiva_adc_s *adc, uint8_t sse,
                                 uint8_t chn, uint32_t shold)
{
  uintptr_t sstshreg = (TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSTSH(sse));
  if (shold > 0)
    {
      tiva_adc_modifyreg(adc, sstshreg, 0, (shold << ADC_SSTSH_SHIFT(sse)));
    }
  else
    {
      tiva_adc_modifyreg(adc, sstshreg, ADC_SSTSH_MASK(sse), 0);
    }
}
#endif

/****************************************************************************
 * Name: sse_step_cfg
 *
 * Description:
 *   Configures the given SSE step to one of the following options:
 *      -Temperature sensor select: this step is muxed to the internal
 *       temperature sensor.
 *      -Interrupt enabled select: this step causes the interrupt bit to
 *       be set and, if the MASK0 bit in ADC_IM register is set, the
 *       interrupt is promoted to the interrupt controller.
 *      -Sequence end select: This step is the last sequence to be sampled.
 *       This MUST be set somewhere in the SSE.
 *      -*Comparator/Differential select: The analog input is differentially
 *       sampled. The corresponding ADCSSMUXn nibble must be set to the pair
 *       number "i", where the paired inputs are "2i and 2i+1". Because the
 *       temperature sensor does not have a differential option, this bit must
 *       not be set when the TS3 bit is set.
 *
 *  *Comparator/Differential functionality is unsupported and ignored.
 *
 * Input parameters:
 *   adc - peripheral state
 *   sse - sample sequencer
 *   chn - sample sequencer channel
 *   cfg - step configuration
 *
 ****************************************************************************/

static void sse_step_cfg(struct tiva_adc_s *adc, uint8_t sse, uint8_t chn,
                         uint8_t cfg)
{
  uintptr_t ssctlreg = (TIVA_ADC_BASE(adc->devno) + TIVA_ADC_SSCTL(sse));
  uint32_t ctlcfg = cfg << ADC_SSCTL_SHIFT(chn);
  tiva_adc_modifyreg(adc, ssctlreg, 0, ctlcfg);
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: tiva_adc_initialize
 *
 * Description:
 *   Initialize the ADC
 *
 * Returned Value:
 *   Valid can device structure reference on success; a NULL on failure
 *
 ****************************************************************************/

struct adc_dev_s *tiva_adc_initialize(int adc_num)
{
  avdbg("tiva_adc_initialize\n");

  /* Initialize the private ADC device data structure */

  struct tiva_adc_s *adc;
  uint8_t s;
#ifdef CONFIG_TIVA_ADC0
  if (adc_num == 0)
    {
      adc0.ena = false;
      adc0.devno = 0;

      /* Debug stuff */

#  ifdef CONFIG_TIVA_ADC_REGDEBUG
      adc0.wrlast = false;
      adc0.addrlast = 0x0;
      adc0.vallast = 0x0;
      adc0.ntimes = 0;
#  endif                             /* CONFIG_TIVA_ADC_REGDEBUG */

      /* Initialize SSEs */

#  ifdef CONFIG_TIVA_ADC0_SSE0
      sse00.ena = false;
      sse00.irq = TIVA_IRQ_ADC0;
      sse00.num = 0;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse00.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif
      adc0.sse[0] = &sse00;
#  endif                             /* CONFIG_TIVA_ADC0_SSE0 */

#  ifdef CONFIG_TIVA_ADC0_SSE1
      sse01.ena = false;
      sse01.irq = TIVA_IRQ_ADC1;
      sse01.num = 1;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse01.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif
      adc0.sse[1] = &sse01;
#  endif                             /* CONFIG_TIVA_ADC0_SSE1 */

#  ifdef CONFIG_TIVA_ADC0_SSE2
      sse02.ena = false;
      sse02.irq = TIVA_IRQ_ADC2;
      sse02.num = 2;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse02.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif
      adc0.sse[2] = &sse02;
#  endif                             /* CONFIG_TIVA_ADC0_SSE2 */

#  ifdef CONFIG_TIVA_ADC0_SSE3
      sse03.ena = false;
      sse03.irq = TIVA_IRQ_ADC3;
      sse03.num = 3;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse03.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif
      adc0.sse[3] = &sse03;
#  endif                             /* CONFIG_TIVA_ADC0_SSE3 */

      adc0.dev = &g_adcdev0;
      adc = &adc0;

      /* Initialize the public ADC device data structure */

      g_adcdev0.ad_ops = &g_adcops;
      g_adcdev0.ad_priv = &adc0;
    }
#endif                               /* CONFIG_TIVA_ADC0 */

#ifdef CONFIG_TIVA_ADC1
  if (adc_num == 1)
    {
      adc1.ena = false;
      adc1.devno = 0;

      /* Debug stuff */

#  ifdef CONFIG_TIVA_ADC_REGDEBUG
      adc1.wrlast = false;
      adc1.addrlast = 0x0;
      adc1.vallast = 0x0;
      adc1.ntimes = 0;
#  endif                             /* CONFIG_TIVA_ADC_REGDEBUG */

      /* Initialize SSEs */

#  ifdef CONFIG_TIVA_ADC1_SSE0
      sse10.ena = false;
      sse10.irq = TIVA_IRQ_ADC1_0;
      sse10.num = 0;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse10.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif
      adc1.sse[0] = &sse10;
#  endif                             /* CONFIG_TIVA_ADC1_SSE0 */

#  ifdef CONFIG_TIVA_ADC1_SSE1
      sse11.ena = false;
      sse11.irq = TIVA_IRQ_ADC1_1;
      sse11.num = 1;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse11.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif

      adc1.sse[1] = &sse11;
#  endif                             /* CONFIG_TIVA_ADC1_SSE1 */

#  ifdef CONFIG_TIVA_ADC1_SSE2
      sse12.ena = false;
      sse12.irq = TIVA_IRQ_ADC1_2;
      sse12.num = 2;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse12.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif
      adc1.sse[2] = &sse12;
#  endif                             /* CONFIG_TIVA_ADC1_SSE2 */

#  ifdef CONFIG_TIVA_ADC1_SSE3
      sse13.ena = false;
      sse13.irq = TIVA_IRQ_ADC1_3;
      sse13.num = 3;
#    ifdef TIVA_ADC_HAVE_INTERRUPTS
      sem_init(&sse13.exclsem, SEM_PROCESS_PRIVATE, 1);
#    endif
      adc1.sse[3] = &sse13;
#  endif                             /* CONFIG_TIVA_ADC1_SSE3 */

      adc1.dev = &g_adcdev1;
      adc = &adc1;

      /* Initialize the public ADC device data structure */

      g_adcdev1.ad_ops = &g_adcops;
      g_adcdev1.ad_priv = &adc1;
    }
#endif                               /* CONFIG_TIVA_ADC1 */

  if (adc_num > 1)
    {
      adbg("ERROR: Invalid ADV devno given, must be 0 or 1! ADC Devno: %d\n",
           adc_num);
      return NULL;
    }

  /* Have the common peripheral properties already been initialized? If yes,
   * continue.
   */

  if (adc_common.init[adc->devno] == false)
    {
      /* turn on peripheral */

      if (adc_state(adc, TIVA_ADC_ENABLE) < 0)
        {
          adbg("ERROR: failure to power ADC peripheral (devno=%d)\n",
               adc_num);
          return NULL;
        }

      /* set clock */

      adc_clock(CONFIG_TIVA_ADC_CLOCK);

      /* set sampling rate */

      adc_sample_rate(TIVA_ADC_SAMPLE_RATE_FASTEST);

#ifdef CONFIG_ARCH_CHIP_TM4C129
      /* voltage reference */

      adc_vref();
#endif                               /* CONFIG_ARCH_CHIP_TM4C129 */
      adc_common.init[adc->devno] = true;
    }

  /* Initialize peripheral */

  /* Have we already been initialized? If yes, than just hand out the interface
   * one more time.
   */

  if (adc->ena == false)
    {
#if CONFIG_TIVA_ADC0
      if (adc_num == 0)
        {
          /* Configure sample sequencers */

          tiva_adc0_sse_init();

          /* Configure channels & register interrupts */

#  if TIVA_ADC_HAVE_INTERRUPTS
          tiva_adc0_assign_interrupts();
#  endif
          tiva_adc0_assign_channels();
        }
#endif

#if CONFIG_TIVA_ADC1
      if (adc_num == 1)
        {
          /* Configure sample sequencers */

          tiva_adc1_sse_init();

          /* Configure channels & register interrupts */

#  if TIVA_ADC_HAVE_INTERRUPTS
          tiva_adc1_assign_interrupts();
#  endif
          tiva_adc1_assign_channels();
        }
#endif

      /* Enable SSEs */

      for (s = 0; s < SSE_PER_BASE; ++s)
        {
          if (adc_common.sse[SSE_IDX(adc_num, s)])
            {
              sse_state(adc, s, TIVA_ADC_ENABLE);
              sse_clear_int(adc, s);
            }
        }

      /* Now we are initialized */

      adc->ena = true;
    }

  /* Return a pointer to the device structure */

  avdbg("Returning %x\n", adc->dev);
  return adc->dev;
}

/****************************************************************************
 * Name: tiva_adc_lock
 *
 * Description:
 *   Get exclusive access to the ADC interface
 *
 ****************************************************************************/

void tiva_adc_lock(FAR struct tiva_adc_s *priv, int sse)
{
#if TIVA_ADC_HAVE_INTERRUPTS
  int ret;

  avdbg("Locking\n");

  do
    {
      ret = sem_wait(&priv->sse[sse]->exclsem);

      /* This should only fail if the wait was canceled by an signal (and the
       * worker thread will receive a lot of signals).
       */

      DEBUGASSERT(ret == OK || errno == EINTR);
    }
  while (ret < 0);
#endif
}

/****************************************************************************
 * Name: tiva_adc_unlock
 *
 * Description:
 *   Relinquish the lock on the ADC interface
 *
 ****************************************************************************/

void tiva_adc_unlock(FAR struct tiva_adc_s *priv, int sse)
{
#if TIVA_ADC_HAVE_INTERRUPTS
  avdbg("Unlocking\n");
  sem_post(&priv->sse[sse]->exclsem);
#endif
}

/****************************************************************************
 * Name: tiva_adc_getreg
 *
 * Description:
 *  Read any 32-bit register using an absolute address.
 *
 ****************************************************************************/

#ifdef CONFIG_TIVA_ADC_REGDEBUG
uint32_t tiva_adc_getreg(struct tiva_adc_s *priv, uintptr_t address)
{
  uint32_t regval = getreg32(address);

  if (tiva_adc_checkreg(priv, false, regval, address))
    {
      lldbg("%08x->%08x\n", address, regval);
    }

  return regval;
}

/****************************************************************************
 * Name: tiva_adc_putreg
 *
 * Description:
 *  Write to any 32-bit register using an absolute address.
 *
 ****************************************************************************/

void tiva_adc_putreg(struct tiva_adc_s *priv, uintptr_t address,
                     uint32_t regval)
{
  if (tiva_adc_checkreg(priv, true, regval, address))
    {
      lldbg("%08x<-%08x\n", address, regval);
    }

  putreg32(regval, address);
}
#endif                               /* CONFIG_TIVA_ADC_REGDEBUG */

/****************************************************************************
 * Name: Verbose, generated code
 *
 * Description:
 *  Generated with a python script, the following code is used to deal with
 *  the defines generated from the Kconfig menu. Read at your own risk.
 *
 ****************************************************************************/

/* Sample sequencer initialization ******************************************/

#ifdef CONFIG_TIVA_ADC0
static void tiva_adc0_sse_init(void)
{
#  ifdef CONFIG_TIVA_ADC0_SSE0
  sse_state(&adc0, 0, TIVA_ADC_DISABLE);
  sse_priority(&adc0, 0, CONFIG_TIVA_ADC0_SSE0_PRIORITY);
  sse_trigger(&adc0, 0, CONFIG_TIVA_ADC0_SSE0_TRIGGER);
  adc_common.sse[SSE_IDX(0, 0)] = true;
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE1
  sse_state(&adc0, 1, TIVA_ADC_DISABLE);
  sse_priority(&adc0, 1, CONFIG_TIVA_ADC0_SSE1_PRIORITY);
  sse_trigger(&adc0, 1, CONFIG_TIVA_ADC0_SSE1_TRIGGER);
  adc_common.sse[SSE_IDX(0, 1)] = true;
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE2
  sse_state(&adc0, 2, TIVA_ADC_DISABLE);
  sse_priority(&adc0, 2, CONFIG_TIVA_ADC0_SSE2_PRIORITY);
  sse_trigger(&adc0, 2, CONFIG_TIVA_ADC0_SSE2_TRIGGER);
  adc_common.sse[SSE_IDX(0, 2)] = true;
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE3
  sse_state(&adc0, 3, TIVA_ADC_DISABLE);
  sse_priority(&adc0, 3, CONFIG_TIVA_ADC0_SSE3_PRIORITY);
  sse_trigger(&adc0, 3, CONFIG_TIVA_ADC0_SSE3_TRIGGER);
  adc_common.sse[SSE_IDX(0, 3)] = true;
#  endif
}
#endif                               /* CONFIG_TIVA_ADC0 */

#ifdef CONFIG_TIVA_ADC1
static void tiva_adc1_sse_init(void)
{
#  ifdef CONFIG_TIVA_ADC1_SSE0
  sse_state(&adc1, 0, TIVA_ADC_DISABLE);
  sse_priority(&adc1, 0, CONFIG_TIVA_ADC1_SSE0_PRIORITY);
  sse_trigger(&adc1, 0, CONFIG_TIVA_ADC1_SSE0_TRIGGER);
  adc_common.sse[SSE_IDX(1, 0)] = true;
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE1
  sse_state(&adc1, 1, TIVA_ADC_DISABLE);
  sse_priority(&adc1, 1, CONFIG_TIVA_ADC1_SSE1_PRIORITY);
  sse_trigger(&adc1, 1, CONFIG_TIVA_ADC1_SSE1_TRIGGER);
  adc_common.sse[SSE_IDX(1, 1)] = true;
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE2
  sse_state(&adc1, 2, TIVA_ADC_DISABLE);
  sse_priority(&adc1, 2, CONFIG_TIVA_ADC1_SSE2_PRIORITY);
  sse_trigger(&adc1, 2, CONFIG_TIVA_ADC1_SSE2_TRIGGER);
  adc_common.sse[SSE_IDX(1, 2)] = true;
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE3
  sse_state(&adc1, 3, TIVA_ADC_DISABLE);
  sse_priority(&adc1, 3, CONFIG_TIVA_ADC1_SSE3_PRIORITY);
  sse_trigger(&adc1, 3, CONFIG_TIVA_ADC1_SSE3_TRIGGER);
  adc_common.sse[SSE_IDX(1, 3)] = true;
#  endif
}
#endif                               /* CONFIG_TIVA_ADC1 */

/* Sample sequencer interrupt initialization ********************************/

#ifdef TIVA_ADC_HAVE_INTERRUPTS
static void tiva_adc0_assign_interrupts(void)
{
#  ifdef CONFIG_TIVA_ADC0
  uint32_t ret = 0;
#    ifdef CONFIG_TIVA_ADC0_SSE0
  ret = irq_attach(sse00.irq, (xcpt_t)adc0_sse0_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse00.irq, ret);
      return;
    }
  up_enable_irq(sse00.irq);
#    endif
#    ifdef CONFIG_TIVA_ADC0_SSE1
  ret = irq_attach(sse01.irq, (xcpt_t)adc0_sse1_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse01.irq, ret);
      return;
    }
  up_enable_irq(sse01.irq);
#    endif
#    ifdef CONFIG_TIVA_ADC0_SSE2
  ret = irq_attach(sse02.irq, (xcpt_t)adc0_sse2_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse02.irq, ret);
      return;
    }
  up_enable_irq(sse02.irq);
#    endif
#    ifdef CONFIG_TIVA_ADC0_SSE3
  ret = irq_attach(sse03.irq, (xcpt_t)adc0_sse3_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse03.irq, ret);
      return;
    }
  up_enable_irq(sse03.irq);
#    endif
#  endif
};

static void tiva_adc1_assign_interrupts(void)
{
#  ifdef CONFIG_TIVA_ADC1
  uint32_t ret = 0;
#    ifdef CONFIG_TIVA_ADC1_SSE0
  ret = irq_attach(sse10.irq, (xcpt_t)adc1_sse0_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse10.irq, ret);
      return;
    }
  up_enable_irq(sse10.irq);
#    endif
#    ifdef CONFIG_TIVA_ADC1_SSE1
  ret = irq_attach(sse11.irq, (xcpt_t)adc1_sse1_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse11.irq, ret);
      return;
    }
  up_enable_irq(sse11.irq);
#    endif
#    ifdef CONFIG_TIVA_ADC1_SSE2
  ret = irq_attach(sse12.irq, (xcpt_t)adc1_sse2_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse12.irq, ret);
      return;
    }
  up_enable_irq(sse12.irq);
#    endif
#    ifdef CONFIG_TIVA_ADC1_SSE3
  ret = irq_attach(sse13.irq, (xcpt_t)adc1_sse3_interrupt);
  if (ret < 0)
    {
      adbg("ERROR: Failed to attach IRQ %d: %d\n", sse13.irq, ret);
      return;
    }
  up_enable_irq(sse13.irq);
#    endif
#  endif
};
#endif /* TIVA_ADC_HAVE_INTERRUPTS */

/* Sample sequencer interrupt declaration ********************************/

#ifdef TIVA_ADC_HAVE_INTERRUPTS
#  ifdef CONFIG_TIVA_ADC0
#    ifdef CONFIG_TIVA_ADC0_SSE0
static void adc0_sse0_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse00.ena == true) &&
              (adc_common.sse[SSE_IDX(0, 0)] == true));

  /* disable further  interrupts. Interrupts will be re-enabled
   * after the worker thread executes.
   */

  sse_int_state(&adc0, 0, TIVA_ADC_DISABLE);

  /* Clear interrupt status */

  sse_clear_int(&adc0, 0);

  /* Transfer processing to the worker thread.  Since interrupts are
   * disabled while the work is pending, no special action should be
   * required to protected the work queue.
   */

  DEBUGASSERT(sse00.work.worker == NULL);
  ret = work_queue(HPWORK, &sse00.work, tiva_adc_read, &sse00, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d\n", ret);
    }
}
#    endif
#    ifdef CONFIG_TIVA_ADC0_SSE1
static void adc0_sse1_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse01.ena == true) &&
              (adc_common.sse[SSE_IDX(0, 1)] == true));

  sse_int_state(&adc0, 1, TIVA_ADC_DISABLE);
  sse_clear_int(&adc0, 1);
  DEBUGASSERT(sse01.work.worker == NULL);
  ret = work_queue(HPWORK, &sse01.work, tiva_adc_read, &sse01, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d ADC.SSE: %d.%d\n",
           ret, adc0.devno, sse01.num);
    }
}
#    endif
#    ifdef CONFIG_TIVA_ADC0_SSE2
static void adc0_sse2_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse02.ena == true) &&
              (adc_common.sse[SSE_IDX(0, 2)] == true));

  sse_int_state(&adc0, 2, TIVA_ADC_DISABLE);
  sse_clear_int(&adc0, 2);
  DEBUGASSERT(sse02.work.worker == NULL);
  ret = work_queue(HPWORK, &sse02.work, tiva_adc_read, &sse02, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d ADC.SSE: %d.%d\n",
           ret, adc0.devno, sse02.num);
    }
}
#    endif
#    ifdef CONFIG_TIVA_ADC0_SSE3
static void adc0_sse3_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse03.ena == true) &&
              (adc_common.sse[SSE_IDX(0, 3)] == true));

  sse_int_state(&adc0, 3, TIVA_ADC_DISABLE);
  sse_clear_int(&adc0, 3);
  DEBUGASSERT(sse03.work.worker == NULL);
  ret = work_queue(HPWORK, &sse03.work, tiva_adc_read, &sse03, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d ADC.SSE: %d.%d\n",
           ret, adc0.devno, sse03.num);
    }
}
#    endif
#  endif /* CONFIG_TIVA_ADC0 */

#  ifdef CONFIG_TIVA_ADC1
#    ifdef CONFIG_TIVA_ADC1_SSE0
static void adc1_sse0_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse10.ena == true) &&
              (adc_common.sse[SSE_IDX(1, 0)] == true));

  sse_int_state(&adc1, 0, TIVA_ADC_DISABLE);
  sse_clear_int(&adc1, 0);
  DEBUGASSERT(sse10.work.worker == NULL);
  ret = work_queue(HPWORK, &sse10.work, tiva_adc_read, &sse10, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d ADC.SSE: %d.%d\n",
           ret, adc1.devno, sse10.num);
    }
}
#    endif
#    ifdef CONFIG_TIVA_ADC1_SSE1
static void adc1_sse1_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse11.ena == true) &&
              (adc_common.sse[SSE_IDX(1, 1)] == true));

  sse_int_state(&adc1, 1, TIVA_ADC_DISABLE);
  sse_clear_int(&adc1, 1);
  DEBUGASSERT(sse11.work.worker == NULL);
  ret = work_queue(HPWORK, &sse11.work, tiva_adc_read, &sse11, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d ADC.SSE: %d.%d\n",
           ret, adc1.devno, sse11.num);
    }
}
#    endif
#    ifdef CONFIG_TIVA_ADC1_SSE2
static void adc1_sse2_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse12.ena == true) &&
              (adc_common.sse[SSE_IDX(1, 2)] == true));

  sse_int_state(&adc1, 2, TIVA_ADC_DISABLE);
  sse_clear_int(&adc1, 2);
  DEBUGASSERT(sse12.work.worker == NULL);
  ret = work_queue(HPWORK, &sse12.work, tiva_adc_read, &sse12, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d ADC.SSE: %d.%d\n",
           ret, adc1.devno, sse12.num);
    }
}
#    endif
#    ifdef CONFIG_TIVA_ADC1_SSE3
static void adc1_sse3_interrupt(int irq, void *context)
{
  int ret;

  DEBUGASSERT((sse13.ena == true) &&
              (adc_common.sse[SSE_IDX(1, 3)] == true));

  sse_int_state(&adc1, 3, TIVA_ADC_DISABLE);
  sse_clear_int(&adc1, 3);
  DEBUGASSERT(sse13.work.worker == NULL);
  ret = work_queue(HPWORK, &sse13.work, tiva_adc_read, &sse13, 0);
  if (ret != 0)
    {
      adbg("ERROR: Failed to queue work: %d ADC.SSE: %d.%d\n",
           ret, adc1.devno, sse13.num);
    }
}
#    endif
#  endif /* CONFIG_TIVA_ADC1 */
#endif /* TIVA_ADC_HAVE_INTERRUPTS */

/* Channel assignment *******************************************************/

#ifdef CONFIG_TIVA_ADC0
static void tiva_adc0_assign_channels(void)
{
#  ifdef CONFIG_TIVA_ADC0_SSE0
  adc0_sse0_chn_cfg();
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE1
  adc0_sse1_chn_cfg();
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE2
  adc0_sse2_chn_cfg();
#  endif
#  ifdef CONFIG_TIVA_ADC0_SSE3
  adc0_sse3_chn_cfg();
#  endif
}
#endif

#ifdef CONFIG_TIVA_ADC1
static void tiva_adc1_assign_channels(void)
{
#  ifdef CONFIG_TIVA_ADC1_SSE0
  adc1_sse0_chn_cfg();
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE1
  adc1_sse1_chn_cfg();
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE2
  adc1_sse2_chn_cfg();
#  endif
#  ifdef CONFIG_TIVA_ADC1_SSE3
  adc1_sse3_chn_cfg();
#  endif
}
#endif

/* Channel config ***********************************************************/

#ifdef CONFIG_TIVA_ADC0
#  ifdef CONFIG_TIVA_ADC0_SSE0
static void adc0_sse0_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC0_SSE0_STEP0
#      ifdef CONFIG_TIVA_ADC0_SSE0_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP0_AIN));
  sse_register_chn(&adc0, 0, 0, CONFIG_TIVA_ADC0_SSE0_STEP0_AIN);
  sse_differential(&adc0, 0, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 0, ADC_SSTH_SHOLD_16);
#        endif
#        ifndef CONFIG_TIVA_ADC0_SSE0_STEP1
  sse_step_cfg(&adc0, 0, 0, chncfg | ADC_SSCTL_END);
#        else
  sse_step_cfg(&adc0, 0, 0, chncfg);
#        endif
#      ifdef CONFIG_TIVA_ADC0_SSE0_STEP1
#        ifdef CONFIG_TIVA_ADC0_SSE0_STEP1_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#        else
  chncfg = ADC_SSCTL_IE;
#        endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP1_AIN));
  sse_register_chn(&adc0, 0, 1, CONFIG_TIVA_ADC0_SSE0_STEP1_AIN);
  sse_differential(&adc0, 0, 1, 0);
#          if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 1, ADC_SSTH_SHOLD_16);
#          endif
#          ifndef CONFIG_TIVA_ADC0_SSE0_STEP2
  sse_step_cfg(&adc0, 0, 1, chncfg | ADC_SSCTL_END);
#          else
  sse_step_cfg(&adc0, 0, 1, chncfg);
#          endif
#        ifdef CONFIG_TIVA_ADC0_SSE0_STEP2
#          ifdef CONFIG_TIVA_ADC0_SSE0_STEP2_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#          else
  chncfg = ADC_SSCTL_IE;
#          endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP2_AIN));
  sse_register_chn(&adc0, 0, 2, CONFIG_TIVA_ADC0_SSE0_STEP2_AIN);
  sse_differential(&adc0, 0, 2, 0);
#            if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 2, ADC_SSTH_SHOLD_16);
#            endif
#            ifndef CONFIG_TIVA_ADC0_SSE0_STEP3
  sse_step_cfg(&adc0, 0, 2, chncfg | ADC_SSCTL_END);
#            else
  sse_step_cfg(&adc0, 0, 2, chncfg);
#            endif
#          ifdef CONFIG_TIVA_ADC0_SSE0_STEP3
#            ifdef CONFIG_TIVA_ADC0_SSE0_STEP3_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#            else
  chncfg = ADC_SSCTL_IE;
#            endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP3_AIN));
  sse_register_chn(&adc0, 0, 3, CONFIG_TIVA_ADC0_SSE0_STEP3_AIN);
  sse_differential(&adc0, 0, 3, 0);
#              if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 3, ADC_SSTH_SHOLD_16);
#              endif
#              ifndef CONFIG_TIVA_ADC0_SSE0_STEP4
  sse_step_cfg(&adc0, 0, 3, chncfg | ADC_SSCTL_END);
#              else
  sse_step_cfg(&adc0, 0, 3, chncfg);
#              endif
#            ifdef CONFIG_TIVA_ADC0_SSE0_STEP4
#              ifdef CONFIG_TIVA_ADC0_SSE0_STEP4_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#              else
  chncfg = ADC_SSCTL_IE;
#              endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP4_AIN));
  sse_register_chn(&adc0, 0, 4, CONFIG_TIVA_ADC0_SSE0_STEP4_AIN);
  sse_differential(&adc0, 0, 4, 0);
#                if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 4, ADC_SSTH_SHOLD_16);
#                endif
#                ifndef CONFIG_TIVA_ADC0_SSE0_STEP5
  sse_step_cfg(&adc0, 0, 4, chncfg | ADC_SSCTL_END);
#                else
  sse_step_cfg(&adc0, 0, 4, chncfg);
#                endif
#              ifdef CONFIG_TIVA_ADC0_SSE0_STEP5
#                ifdef CONFIG_TIVA_ADC0_SSE0_STEP5_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#                else
  chncfg = ADC_SSCTL_IE;
#                endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP5_AIN));
  sse_register_chn(&adc0, 0, 5, CONFIG_TIVA_ADC0_SSE0_STEP5_AIN);
  sse_differential(&adc0, 0, 5, 0);
#                  if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 5, ADC_SSTH_SHOLD_16);
#                  endif
#                  ifndef CONFIG_TIVA_ADC0_SSE0_STEP6
  sse_step_cfg(&adc0, 0, 5, chncfg | ADC_SSCTL_END);
#                  else
  sse_step_cfg(&adc0, 0, 5, chncfg);
#                  endif
#                ifdef CONFIG_TIVA_ADC0_SSE0_STEP6
#                  ifdef CONFIG_TIVA_ADC0_SSE0_STEP6_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#                  else
  chncfg = ADC_SSCTL_IE;
#                  endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP6_AIN));
  sse_register_chn(&adc0, 0, 6, CONFIG_TIVA_ADC0_SSE0_STEP6_AIN);
  sse_differential(&adc0, 0, 6, 0);
#                    if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 6, ADC_SSTH_SHOLD_16);
#                    endif
#                    ifndef CONFIG_TIVA_ADC0_SSE0_STEP7
  sse_step_cfg(&adc0, 0, 6, chncfg | ADC_SSCTL_END);
#                    else
  sse_step_cfg(&adc0, 0, 6, chncfg);
#                    endif
#                  ifdef CONFIG_TIVA_ADC0_SSE0_STEP7
#                    ifdef CONFIG_TIVA_ADC0_SSE0_STEP7_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#                    else
  chncfg = ADC_SSCTL_IE;
#                    endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE0_STEP7_AIN));
  sse_register_chn(&adc0, 0, 7, CONFIG_TIVA_ADC0_SSE0_STEP7_AIN);
  sse_differential(&adc0, 0, 7, 0);
#                      if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 0, 7, ADC_SSTH_SHOLD_16);
#                      endif
  sse_step_cfg(&adc0, 0, 7, chncfg | ADC_SSCTL_END);
#                  endif /* CONFIG_TIVA_ADC0_SSE0_STEP7 */
#                endif /* CONFIG_TIVA_ADC0_SSE0_STEP6 */
#              endif /* CONFIG_TIVA_ADC0_SSE0_STEP5 */
#            endif /* CONFIG_TIVA_ADC0_SSE0_STEP4 */
#          endif /* CONFIG_TIVA_ADC0_SSE0_STEP3 */
#        endif /* CONFIG_TIVA_ADC0_SSE0_STEP2 */
#      endif /* CONFIG_TIVA_ADC0_SSE0_STEP1 */
#    endif /* CONFIG_TIVA_ADC0_SSE0_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC0_SSE0 */

#  ifdef CONFIG_TIVA_ADC0_SSE1
static void adc0_sse1_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC0_SSE1_STEP0
#      ifdef CONFIG_TIVA_ADC0_SSE1_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE1_STEP0_AIN));
  sse_register_chn(&adc0, 1, 0, CONFIG_TIVA_ADC0_SSE1_STEP0_AIN);
  sse_differential(&adc0, 1, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 1, 0, ADC_SSTH_SHOLD_16);
#        endif
#        ifndef CONFIG_TIVA_ADC0_SSE1_STEP1
  sse_step_cfg(&adc0, 1, 0, chncfg | ADC_SSCTL_END);
#        else
  sse_step_cfg(&adc0, 1, 0, chncfg);
#        endif
#      ifdef CONFIG_TIVA_ADC0_SSE1_STEP1
#        ifdef CONFIG_TIVA_ADC0_SSE1_STEP1_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#        else
  chncfg = ADC_SSCTL_IE;
#        endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE1_STEP1_AIN));
  sse_register_chn(&adc0, 1, 1, CONFIG_TIVA_ADC0_SSE1_STEP1_AIN);
  sse_differential(&adc0, 1, 1, 0);
#          if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 1, 1, ADC_SSTH_SHOLD_16);
#          endif
#          ifndef CONFIG_TIVA_ADC0_SSE1_STEP2
  sse_step_cfg(&adc0, 1, 1, chncfg | ADC_SSCTL_END);
#          else
  sse_step_cfg(&adc0, 1, 1, chncfg);
#          endif
#        ifdef CONFIG_TIVA_ADC0_SSE1_STEP2
#          ifdef CONFIG_TIVA_ADC0_SSE1_STEP2_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#          else
  chncfg = ADC_SSCTL_IE;
#          endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE1_STEP2_AIN));
  sse_register_chn(&adc0, 1, 2, CONFIG_TIVA_ADC0_SSE1_STEP2_AIN);
  sse_differential(&adc0, 1, 2, 0);
#            if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 1, 2, ADC_SSTH_SHOLD_16);
#            endif
#            ifndef CONFIG_TIVA_ADC0_SSE1_STEP3
  sse_step_cfg(&adc0, 1, 2, chncfg | ADC_SSCTL_END);
#            else
  sse_step_cfg(&adc0, 1, 2, chncfg);
#            endif
#          ifdef CONFIG_TIVA_ADC0_SSE1_STEP3
#            ifdef CONFIG_TIVA_ADC0_SSE1_STEP3_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#            else
  chncfg = ADC_SSCTL_IE;
#            endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE1_STEP3_AIN));
  sse_register_chn(&adc0, 1, 3, CONFIG_TIVA_ADC0_SSE1_STEP3_AIN);
  sse_differential(&adc0, 1, 3, 0);
#              if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 1, 3, ADC_SSTH_SHOLD_16);
#              endif
  sse_step_cfg(&adc0, 1, 3, chncfg | ADC_SSCTL_END);
#          endif /* CONFIG_TIVA_ADC0_SSE1_STEP3 */
#        endif /* CONFIG_TIVA_ADC0_SSE1_STEP2 */
#      endif /* CONFIG_TIVA_ADC0_SSE1_STEP1 */
#    endif /* CONFIG_TIVA_ADC0_SSE1_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC0_SSE1 */

#  ifdef CONFIG_TIVA_ADC0_SSE2
static void adc0_sse2_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC0_SSE2_STEP0
#      ifdef CONFIG_TIVA_ADC0_SSE2_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE2_STEP0_AIN));
  sse_register_chn(&adc0, 2, 0, CONFIG_TIVA_ADC0_SSE2_STEP0_AIN);
  sse_differential(&adc0, 2, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 2, 0, ADC_SSTH_SHOLD_16);
#        endif
#        ifndef CONFIG_TIVA_ADC0_SSE2_STEP1
  sse_step_cfg(&adc0, 2, 0, chncfg | ADC_SSCTL_END);
#        else
  sse_step_cfg(&adc0, 2, 0, chncfg);
#        endif
#      ifdef CONFIG_TIVA_ADC0_SSE2_STEP1
#        ifdef CONFIG_TIVA_ADC0_SSE2_STEP1_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#        else
  chncfg = ADC_SSCTL_IE;
#        endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE2_STEP1_AIN));
  sse_register_chn(&adc0, 2, 1, CONFIG_TIVA_ADC0_SSE2_STEP1_AIN);
  sse_differential(&adc0, 2, 1, 0);
#          if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 2, 1, ADC_SSTH_SHOLD_16);
#          endif
#          ifndef CONFIG_TIVA_ADC0_SSE2_STEP2
  sse_step_cfg(&adc0, 2, 1, chncfg | ADC_SSCTL_END);
#          else
  sse_step_cfg(&adc0, 2, 1, chncfg);
#          endif
#        ifdef CONFIG_TIVA_ADC0_SSE2_STEP2
#          ifdef CONFIG_TIVA_ADC0_SSE2_STEP2_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#          else
  chncfg = ADC_SSCTL_IE;
#          endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE2_STEP2_AIN));
  sse_register_chn(&adc0, 2, 2, CONFIG_TIVA_ADC0_SSE2_STEP2_AIN);
  sse_differential(&adc0, 2, 2, 0);
#            if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 2, 2, ADC_SSTH_SHOLD_16);
#            endif
#            ifndef CONFIG_TIVA_ADC0_SSE2_STEP3
  sse_step_cfg(&adc0, 2, 2, chncfg | ADC_SSCTL_END);
#            else
  sse_step_cfg(&adc0, 2, 2, chncfg);
#            endif
#          ifdef CONFIG_TIVA_ADC0_SSE2_STEP3
#            ifdef CONFIG_TIVA_ADC0_SSE2_STEP3_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#            else
  chncfg = ADC_SSCTL_IE;
#            endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE2_STEP3_AIN));
  sse_register_chn(&adc0, 2, 3, CONFIG_TIVA_ADC0_SSE2_STEP3_AIN);
  sse_differential(&adc0, 2, 3, 0);
#              if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 2, 3, ADC_SSTH_SHOLD_16);
#              endif
  sse_step_cfg(&adc0, 2, 3, chncfg | ADC_SSCTL_END);
#          endif /* CONFIG_TIVA_ADC0_SSE2_STEP3 */
#        endif /* CONFIG_TIVA_ADC0_SSE2_STEP2 */
#      endif /* CONFIG_TIVA_ADC0_SSE2_STEP1 */
#    endif /* CONFIG_TIVA_ADC0_SSE2_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC0_SSE2 */

#  ifdef CONFIG_TIVA_ADC0_SSE3
static void adc0_sse3_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC0_SSE3_STEP0
#      ifdef CONFIG_TIVA_ADC0_SSE3_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC0_SSE3_STEP0_AIN));
  sse_register_chn(&adc0, 3, 0, CONFIG_TIVA_ADC0_SSE3_STEP0_AIN);
  sse_differential(&adc0, 3, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC0_SSE3_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc0, 3, 0, ADC_SSTH_SHOLD_16);
#        endif
  sse_step_cfg(&adc0, 3, 0, chncfg | ADC_SSCTL_END);
#    endif /* CONFIG_TIVA_ADC0_SSE3_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC0_SSE3 */
#endif /* CONFIG_TIVA_ADC0 */

#ifdef CONFIG_TIVA_ADC1
#  ifdef CONFIG_TIVA_ADC1_SSE0
static void adc1_sse0_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC1_SSE0_STEP0
#      ifdef CONFIG_TIVA_ADC1_SSE0_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP0_AIN));
  sse_register_chn(&adc1, 0, 0, CONFIG_TIVA_ADC1_SSE0_STEP0_AIN);
  sse_differential(&adc1, 0, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 0, ADC_SSTH_SHOLD_16);
#        endif
#        ifndef CONFIG_TIVA_ADC1_SSE0_STEP1
  sse_step_cfg(&adc1, 0, 0, chncfg | ADC_SSCTL_END);
#        else
  sse_step_cfg(&adc1, 0, 0, chncfg);
#        endif
#      ifdef CONFIG_TIVA_ADC1_SSE0_STEP1
#        ifdef CONFIG_TIVA_ADC1_SSE0_STEP1_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#        else
  chncfg = ADC_SSCTL_IE;
#        endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP1_AIN));
  sse_register_chn(&adc1, 0, 1, CONFIG_TIVA_ADC1_SSE0_STEP1_AIN);
  sse_differential(&adc1, 0, 1, 0);
#          if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 1, ADC_SSTH_SHOLD_16);
#          endif
#          ifndef CONFIG_TIVA_ADC1_SSE0_STEP2
  sse_step_cfg(&adc1, 0, 1, chncfg | ADC_SSCTL_END);
#          else
  sse_step_cfg(&adc1, 0, 1, chncfg);
#          endif
#        ifdef CONFIG_TIVA_ADC1_SSE0_STEP2
#          ifdef CONFIG_TIVA_ADC1_SSE0_STEP2_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#          else
  chncfg = ADC_SSCTL_IE;
#          endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP2_AIN));
  sse_register_chn(&adc1, 0, 2, CONFIG_TIVA_ADC1_SSE0_STEP2_AIN);
  sse_differential(&adc1, 0, 2, 0);
#            if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 2, ADC_SSTH_SHOLD_16);
#            endif
#            ifndef CONFIG_TIVA_ADC1_SSE0_STEP3
  sse_step_cfg(&adc1, 0, 2, chncfg | ADC_SSCTL_END);
#            else
  sse_step_cfg(&adc1, 0, 2, chncfg);
#            endif
#          ifdef CONFIG_TIVA_ADC1_SSE0_STEP3
#            ifdef CONFIG_TIVA_ADC1_SSE0_STEP3_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#            else
  chncfg = ADC_SSCTL_IE;
#            endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP3_AIN));
  sse_register_chn(&adc1, 0, 3, CONFIG_TIVA_ADC1_SSE0_STEP3_AIN);
  sse_differential(&adc1, 0, 3, 0);
#              if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 3, ADC_SSTH_SHOLD_16);
#              endif
#              ifndef CONFIG_TIVA_ADC1_SSE0_STEP4
  sse_step_cfg(&adc1, 0, 3, chncfg | ADC_SSCTL_END);
#              else
  sse_step_cfg(&adc1, 0, 3, chncfg);
#              endif
#            ifdef CONFIG_TIVA_ADC1_SSE0_STEP4
#              ifdef CONFIG_TIVA_ADC1_SSE0_STEP4_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#              else
  chncfg = ADC_SSCTL_IE;
#              endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP4_AIN));
  sse_register_chn(&adc1, 0, 4, CONFIG_TIVA_ADC1_SSE0_STEP4_AIN);
  sse_differential(&adc1, 0, 4, 0);
#                if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 4, ADC_SSTH_SHOLD_16);
#                endif
#                ifndef CONFIG_TIVA_ADC1_SSE0_STEP5
  sse_step_cfg(&adc1, 0, 4, chncfg | ADC_SSCTL_END);
#                else
  sse_step_cfg(&adc1, 0, 4, chncfg);
#                endif
#              ifdef CONFIG_TIVA_ADC1_SSE0_STEP5
#                ifdef CONFIG_TIVA_ADC1_SSE0_STEP5_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#                else
  chncfg = ADC_SSCTL_IE;
#                endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP5_AIN));
  sse_register_chn(&adc1, 0, 5, CONFIG_TIVA_ADC1_SSE0_STEP5_AIN);
  sse_differential(&adc1, 0, 5, 0);
#                  if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 5, ADC_SSTH_SHOLD_16);
#                  endif
#                  ifndef CONFIG_TIVA_ADC1_SSE0_STEP6
  sse_step_cfg(&adc1, 0, 5, chncfg | ADC_SSCTL_END);
#                  else
  sse_step_cfg(&adc1, 0, 5, chncfg);
#                  endif
#                ifdef CONFIG_TIVA_ADC1_SSE0_STEP6
#                  ifdef CONFIG_TIVA_ADC1_SSE0_STEP6_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#                  else
  chncfg = ADC_SSCTL_IE;
#                  endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP6_AIN));
  sse_register_chn(&adc1, 0, 6, CONFIG_TIVA_ADC1_SSE0_STEP6_AIN);
  sse_differential(&adc1, 0, 6, 0);
#                    if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 6, ADC_SSTH_SHOLD_16);
#                    endif
#                    ifndef CONFIG_TIVA_ADC1_SSE0_STEP7
  sse_step_cfg(&adc1, 0, 6, chncfg | ADC_SSCTL_END);
#                    else
  sse_step_cfg(&adc1, 0, 6, chncfg);
#                    endif
#                  ifdef CONFIG_TIVA_ADC1_SSE0_STEP7
#                    ifdef CONFIG_TIVA_ADC1_SSE0_STEP7_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#                    else
  chncfg = ADC_SSCTL_IE;
#                    endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE0_STEP7_AIN));
  sse_register_chn(&adc1, 0, 7, CONFIG_TIVA_ADC1_SSE0_STEP7_AIN);
  sse_differential(&adc1, 0, 7, 0);
#                      if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE0_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 0, 7, ADC_SSTH_SHOLD_16);
#                      endif
  sse_step_cfg(&adc1, 0, 7, chncfg | ADC_SSCTL_END);
#                  endif /* CONFIG_TIVA_ADC1_SSE0_STEP7 */
#                endif /* CONFIG_TIVA_ADC1_SSE0_STEP6 */
#              endif /* CONFIG_TIVA_ADC1_SSE0_STEP5 */
#            endif /* CONFIG_TIVA_ADC1_SSE0_STEP4 */
#          endif /* CONFIG_TIVA_ADC1_SSE0_STEP3 */
#        endif /* CONFIG_TIVA_ADC1_SSE0_STEP2 */
#      endif /* CONFIG_TIVA_ADC1_SSE0_STEP1 */
#    endif /* CONFIG_TIVA_ADC1_SSE0_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC1_SSE0 */

#  ifdef CONFIG_TIVA_ADC1_SSE1
static void adc1_sse1_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC1_SSE1_STEP0
#      ifdef CONFIG_TIVA_ADC1_SSE1_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE1_STEP0_AIN));
  sse_register_chn(&adc1, 1, 0, CONFIG_TIVA_ADC1_SSE1_STEP0_AIN);
  sse_differential(&adc1, 1, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 1, 0, ADC_SSTH_SHOLD_16);
#        endif
#        ifndef CONFIG_TIVA_ADC1_SSE1_STEP1
  sse_step_cfg(&adc1, 1, 0, chncfg | ADC_SSCTL_END);
#        else
  sse_step_cfg(&adc1, 1, 0, chncfg);
#        endif
#      ifdef CONFIG_TIVA_ADC1_SSE1_STEP1
#        ifdef CONFIG_TIVA_ADC1_SSE1_STEP1_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#        else
  chncfg = ADC_SSCTL_IE;
#        endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE1_STEP1_AIN));
  sse_register_chn(&adc1, 1, 1, CONFIG_TIVA_ADC1_SSE1_STEP1_AIN);
  sse_differential(&adc1, 1, 1, 0);
#          if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 1, 1, ADC_SSTH_SHOLD_16);
#          endif
#          ifndef CONFIG_TIVA_ADC1_SSE1_STEP2
  sse_step_cfg(&adc1, 1, 1, chncfg | ADC_SSCTL_END);
#          else
  sse_step_cfg(&adc1, 1, 1, chncfg);
#          endif
#        ifdef CONFIG_TIVA_ADC1_SSE1_STEP2
#          ifdef CONFIG_TIVA_ADC1_SSE1_STEP2_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#          else
  chncfg = ADC_SSCTL_IE;
#          endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE1_STEP2_AIN));
  sse_register_chn(&adc1, 1, 2, CONFIG_TIVA_ADC1_SSE1_STEP2_AIN);
  sse_differential(&adc1, 1, 2, 0);
#            if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 1, 2, ADC_SSTH_SHOLD_16);
#            endif
#            ifndef CONFIG_TIVA_ADC1_SSE1_STEP3
  sse_step_cfg(&adc1, 1, 2, chncfg | ADC_SSCTL_END);
#            else
  sse_step_cfg(&adc1, 1, 2, chncfg);
#            endif
#          ifdef CONFIG_TIVA_ADC1_SSE1_STEP3
#            ifdef CONFIG_TIVA_ADC1_SSE1_STEP3_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#            else
  chncfg = ADC_SSCTL_IE;
#            endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE1_STEP3_AIN));
  sse_register_chn(&adc1, 1, 3, CONFIG_TIVA_ADC1_SSE1_STEP3_AIN);
  sse_differential(&adc1, 1, 3, 0);
#              if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE1_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 1, 3, ADC_SSTH_SHOLD_16);
#              endif
  sse_step_cfg(&adc1, 1, 3, chncfg | ADC_SSCTL_END);
#          endif /* CONFIG_TIVA_ADC1_SSE1_STEP3 */
#        endif /* CONFIG_TIVA_ADC1_SSE1_STEP2 */
#      endif /* CONFIG_TIVA_ADC1_SSE1_STEP1 */
#    endif /* CONFIG_TIVA_ADC1_SSE1_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC1_SSE1 */

#  ifdef CONFIG_TIVA_ADC1_SSE2
static void adc1_sse2_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC1_SSE2_STEP0
#      ifdef CONFIG_TIVA_ADC1_SSE2_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE2_STEP0_AIN));
  sse_register_chn(&adc1, 2, 0, CONFIG_TIVA_ADC1_SSE2_STEP0_AIN);
  sse_differential(&adc1, 2, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 2, 0, ADC_SSTH_SHOLD_16);
#        endif
#        ifndef CONFIG_TIVA_ADC1_SSE2_STEP1
  sse_step_cfg(&adc1, 2, 0, chncfg | ADC_SSCTL_END);
#        else
  sse_step_cfg(&adc1, 2, 0, chncfg);
#        endif
#      ifdef CONFIG_TIVA_ADC1_SSE2_STEP1
#        ifdef CONFIG_TIVA_ADC1_SSE2_STEP1_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#        else
  chncfg = ADC_SSCTL_IE;
#        endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE2_STEP1_AIN));
  sse_register_chn(&adc1, 2, 1, CONFIG_TIVA_ADC1_SSE2_STEP1_AIN);
  sse_differential(&adc1, 2, 1, 0);
#          if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 2, 1, ADC_SSTH_SHOLD_16);
#          endif
#          ifndef CONFIG_TIVA_ADC1_SSE2_STEP2
  sse_step_cfg(&adc1, 2, 1, chncfg | ADC_SSCTL_END);
#          else
  sse_step_cfg(&adc1, 2, 1, chncfg);
#          endif
#        ifdef CONFIG_TIVA_ADC1_SSE2_STEP2
#          ifdef CONFIG_TIVA_ADC1_SSE2_STEP2_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#          else
  chncfg = ADC_SSCTL_IE;
#          endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE2_STEP2_AIN));
  sse_register_chn(&adc1, 2, 2, CONFIG_TIVA_ADC1_SSE2_STEP2_AIN);
  sse_differential(&adc1, 2, 2, 0);
#            if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 2, 2, ADC_SSTH_SHOLD_16);
#            endif
#            ifndef CONFIG_TIVA_ADC1_SSE2_STEP3
  sse_step_cfg(&adc1, 2, 2, chncfg | ADC_SSCTL_END);
#            else
  sse_step_cfg(&adc1, 2, 2, chncfg);
#            endif
#          ifdef CONFIG_TIVA_ADC1_SSE2_STEP3
#            ifdef CONFIG_TIVA_ADC1_SSE2_STEP3_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#            else
  chncfg = ADC_SSCTL_IE;
#            endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE2_STEP3_AIN));
  sse_register_chn(&adc1, 2, 3, CONFIG_TIVA_ADC1_SSE2_STEP3_AIN);
  sse_differential(&adc1, 2, 3, 0);
#              if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE2_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 2, 3, ADC_SSTH_SHOLD_16);
#              endif
  sse_step_cfg(&adc1, 2, 3, chncfg | ADC_SSCTL_END);
#          endif /* CONFIG_TIVA_ADC1_SSE2_STEP3 */
#        endif /* CONFIG_TIVA_ADC1_SSE2_STEP2 */
#      endif /* CONFIG_TIVA_ADC1_SSE2_STEP1 */
#    endif /* CONFIG_TIVA_ADC1_SSE2_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC1_SSE2 */

#  ifdef CONFIG_TIVA_ADC1_SSE3
static void adc1_sse3_chn_cfg(void)
{
  uint32_t chncfg = 0;
#    ifdef CONFIG_TIVA_ADC1_SSE3_STEP0
#      ifdef CONFIG_TIVA_ADC1_SSE3_STEP0_TS
  chncfg = ADC_SSCTL_IE | ADC_SSCTL_TS;
#      else
  chncfg = ADC_SSCTL_IE;
#      endif
  tiva_configgpio(TIVA_ADC_PIN(CONFIG_TIVA_ADC1_SSE3_STEP0_AIN));
  sse_register_chn(&adc1, 3, 0, CONFIG_TIVA_ADC1_SSE3_STEP0_AIN);
  sse_differential(&adc1, 3, 0, 0);
#        if defined (CONFIG_ARCH_CHIP_TM4C129) && (CONFIG_TIVA_ADC1_SSE3_TRIGGER == ADC_EMUX_PROC)
  sse_sample_hold_time(&adc1, 3, 0, ADC_SSTH_SHOLD_16);
#        endif
  sse_step_cfg(&adc1, 3, 0, chncfg | ADC_SSCTL_END);
#    endif /* CONFIG_TIVA_ADC1_SSE3_STEP0 */
}
#  endif /* CONFIG_TIVA_ADC1_SSE3 */
#endif /* CONFIG_TIVA_ADC1 */

#endif /* CONFIG_TIVA_ADC0 | CONFIG_TIVA_ADC1 */