rp2040/
pio.rs

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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright OxidOS Automotive 2024.
//
// Author: Radu Matei <radu.matei.05.21@gmail.com>
//         Alberto Udrea <albertoudrea4@gmail.com>

//! Programmable Input Output (PIO) hardware.
//! Refer to the RP2040 Datasheet, Section 3 for more information.
//! RP2040 Datasheet [1].
//!
//! [1]: https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf

use core::cell::Cell;

use kernel::utilities::cells::OptionalCell;
use kernel::utilities::registers::interfaces::{ReadWriteable, Readable, Writeable};
use kernel::utilities::registers::{register_bitfields, register_structs, ReadOnly, ReadWrite};
use kernel::utilities::StaticRef;
use kernel::{debug, ErrorCode};

use crate::gpio::{GpioFunction, RPGpio, RPGpioPin};

const NUMBER_STATE_MACHINES: usize = 4;
const NUMBER_INSTR_MEMORY_LOCATIONS: usize = 32;

#[repr(C)]
struct InstrMem {
    // Write-only access to instruction memory locations 0-31
    instr_mem: ReadWrite<u32, INSTR_MEMx::Register>,
}

#[repr(C)]
struct StateMachineReg {
    // Clock divisor register for state machine x
    // Frequency = clock freq / (CLKDIV_INT + CLKDIV_FRAC / 256)
    clkdiv: ReadWrite<u32, SMx_CLKDIV::Register>,
    // Execution/behavioural settings for state machine x
    execctrl: ReadWrite<u32, SMx_EXECCTRL::Register>,
    // Control behaviour of the input/output shift registers for
    // state machine x
    shiftctrl: ReadWrite<u32, SMx_SHIFTCTRL::Register>,
    // Current instruction address of state machine x
    addr: ReadOnly<u32, SMx_ADDR::Register>,
    // Read to see the instruction currently addressed by state
    // machine x’s program counter Write to execute an instruction
    // immediately (including jumps) and then resume execution.
    instr: ReadWrite<u32, SMx_INSTR::Register>,
    // State machine pin control
    pinctrl: ReadWrite<u32, SMx_PINCTRL::Register>,
}

register_structs! {
PioRegisters {
        // PIO control register
        (0x000 => ctrl: ReadWrite<u32, CTRL::Register>),
        // FIFO status register
        (0x004 => fstat: ReadOnly<u32, FSTAT::Register>),
        // FIFO debug register
        (0x008 => fdebug: ReadWrite<u32, FDEBUG::Register>),
        // FIFO levels
        (0x00C => flevel: ReadOnly<u32, FLEVEL::Register>),
        // Direct write access to the TX FIFO for this state machine. Each
        // write pushes one word to the FIFO. Attempting to write to a full
        // FIFO has no effect on the FIFO state or contents, and sets the
        // sticky FDEBUG_TXOVER error flag for this FIFO.
        (0x010 => txf: [ReadWrite<u32, TXFx::Register>; 4]),
        // Direct read access to the RX FIFO for this state machine. Each
        // read pops one word from the FIFO. Attempting to read from an empty
        // FIFO has no effect on the FIFO state, and sets the sticky
        // FDEBUG_RXUNDER error flag for this FIFO. The data returned
        // to the system on a read from an empty FIFO is undefined.
        (0x020 => rxf: [ReadOnly<u32, RXFx::Register>; 4]),
        // State machine IRQ flags register. Write 1 to clear. There are 8
        // state machine IRQ flags, which can be set, cleared, and waited on
        // by the state machines. There’s no fixed association between
        // flags and state machines — any state machine can use any flag.
        // Any of the 8 flags can be used for timing synchronisation
        // between state machines, using IRQ and WAIT instructions. The
        // lower four of these flags are also routed out to system-level
        // interrupt requests, alongside FIFO status interrupts —
        // see e.g. IRQ0_INTE.
        (0x030 => irq: ReadWrite<u32, IRQ::Register>),
        // Writing a 1 to each of these bits will forcibly assert the
        // corresponding IRQ. Note this is different to the INTF register:
        // writing here affects PIO internal state. INTF just asserts the
        // processor-facing IRQ signal for testing ISRs, and is not visible to
        // the state machines.
        (0x034 => irq_force: ReadWrite<u32, IRQ_FORCE::Register>),
        // There is a 2-flipflop synchronizer on each GPIO input, which
        // protects PIO logic from metastabilities. This increases input
        // delay, and for fast synchronous IO (e.g. SPI) these synchronizers
        // may need to be bypassed. Each bit in this register corresponds
        // to one GPIO.
        // 0 → input is synchronized (default)
        // 1 → synchronizer is bypassed
        // If in doubt, leave this register as all zeroes.
        (0x038 => input_sync_bypass: ReadWrite<u32, INPUT_SYNC_BYPASS::Register>),
        // Read to sample the pad output values PIO is currently driving
        // to the GPIOs.
        (0x03C => dbg_padout: ReadOnly<u32, DBG_PADOUT::Register>),
        // Read to sample the pad output enables (direction) PIO is
        // currently driving to the GPIOs. On RP2040 there are 30 GPIOs,
        // so the two most significant bits are hardwired to 0.
        (0x040 => dbg_padoe: ReadOnly<u32, DBG_PADOE::Register>),
        // The PIO hardware has some free parameters that may vary
        // between chip products.
        (0x044 => dbg_cfginfo: ReadOnly<u32, DBG_CFGINFO::Register>),
        // Write-only access to instruction memory locations 0-31
        (0x048 => instr_mem: [InstrMem; NUMBER_INSTR_MEMORY_LOCATIONS]),
        // State Machines
        (0x0c8 => sm: [StateMachineReg; NUMBER_STATE_MACHINES]),
        // Raw Interrupts
        (0x128 => intr: ReadWrite<u32, INTR::Register>),
        // Interrupt Enable for irq0
        (0x12C => irq0_inte: ReadWrite<u32, IRQ0_INTE::Register>),
        // Interrupt Force for irq0
        (0x130 => irq0_intf: ReadWrite<u32, IRQ0_INTF::Register>),
        // Interrupt status after masking & forcing for irq0
        (0x134 => irq0_ints: ReadWrite<u32, IRQ0_INTS::Register>),
        // Interrupt Enable for irq1
        (0x138 => irq1_inte: ReadWrite<u32, IRQ1_INTE::Register>),
        // Interrupt Force for irq1
        (0x13C => irq1_intf: ReadWrite<u32, IRQ1_INTF::Register>),
        // Interrupt status after masking & forcing for irq1
        (0x140 => irq1_ints: ReadWrite<u32, IRQ1_INTS::Register>),
        (0x144 => @END),
    }
}

register_bitfields![u32,
CTRL [
    // Restart a state machine’s clock divider from an initial
    // phase of 0. Clock dividers are free-running, so once
    // started, their output (including fractional jitter) is
    // completely determined by the integer/fractional divisor
    // configured in SMx_CLKDIV. This means that, if multiple
    // clock dividers with the same divisor are restarted
    // simultaneously, by writing multiple 1 bits to this field, the
    // execution clocks of those state machines will run in
    // precise lockstep.
    // - SM_ENABLE does not stop the clock divider from running
    // - CLKDIV_RESTART can be written to whilst the state machine is running
    CLKDIV3_RESTART OFFSET(11) NUMBITS(1) [],
    CLKDIV2_RESTART OFFSET(10) NUMBITS(1) [],
    CLKDIV1_RESTART OFFSET(9) NUMBITS(1) [],
    CLKDIV0_RESTART OFFSET(8) NUMBITS(1) [],
    // Write 1 to instantly clear internal SM state which may be
    // otherwise difficult to access and will affect future
    // execution.
    // Specifically, the following are cleared: input and output
    // shift counters; the contents of the input shift register; the
    // delay counter; the waiting-on-IRQ state; any stalled
    // instruction written to SMx_INSTR or run by OUT/MOV
    // EXEC; any pin write left asserted due to OUT_STICKY.
    SM3_RESTART OFFSET(7) NUMBITS(1) [],
    SM2_RESTART OFFSET(6) NUMBITS(1) [],
    SM1_RESTART OFFSET(5) NUMBITS(1) [],
    SM0_RESTART OFFSET(4) NUMBITS(1) [],
    // Enable/disable each of the four state machines by writing
    // 1/0 to each of these four bits. When disabled, a state
    // machine will cease executing instructions, except those
    // written directly to SMx_INSTR by the system. Multiple bits
    // can be set/cleared at once to run/halt multiple state
    // machines simultaneously.
    SM3_ENABLE OFFSET(3) NUMBITS(1) [],
    SM2_ENABLE OFFSET(2) NUMBITS(1) [],
    SM1_ENABLE OFFSET(1) NUMBITS(1) [],
    SM0_ENABLE OFFSET(0) NUMBITS(1) [],
],
FSTAT [
    // State machine TX FIFO is empty
    TXEMPTY3 OFFSET(27) NUMBITS(1) [],
    TXEMPTY2 OFFSET(26) NUMBITS(1) [],
    TXEMPTY1 OFFSET(25) NUMBITS(1) [],
    TXEMPTY0 OFFSET(24) NUMBITS(1) [],
    // State machine TX FIFO is full
    TXFULL3 OFFSET(19) NUMBITS(1) [],
    TXFULL2 OFFSET(18) NUMBITS(1) [],
    TXFULL1 OFFSET(17) NUMBITS(1) [],
    TXFULL0 OFFSET(16) NUMBITS(1) [],
    // State machine RX FIFO is empty
    RXEMPTY3 OFFSET(11) NUMBITS(1) [],
    RXEMPTY2 OFFSET(10) NUMBITS(1) [],
    RXEMPTY1 OFFSET(9) NUMBITS(1) [],
    RXEMPTY0 OFFSET(8) NUMBITS(1) [],
    // State machine RX FIFO is full
    RXFULL3 OFFSET(3) NUMBITS(1) [],
    RXFULL2 OFFSET(2) NUMBITS(1) [],
    RXFULL1 OFFSET(1) NUMBITS(1) [],
    RXFULL0 OFFSET(0) NUMBITS(1) []
],
FDEBUG [
    // State machine has stalled on empty TX FIFO during a
    // blocking PULL, or an OUT with autopull enabled. Write 1 to
    // clear.
    TXSTALL OFFSET(24) NUMBITS(4) [],
    // TX FIFO overflow (i.e. write-on-full by the system) has
    // occurred. Write 1 to clear. Note that write-on-full does not
    // alter the state or contents of the FIFO in any way, but the
    // data that the system attempted to write is dropped, so if
    // this flag is set, your software has quite likely dropped
    // some data on the floor.
    TXOVER OFFSET(16) NUMBITS(4) [],
    // RX FIFO underflow (i.e. read-on-empty by the system) has
    // occurred. Write 1 to clear. Note that read-on-empty does
    // not perturb the state of the FIFO in any way, but the data
    // returned by reading from an empty FIFO is undefined, so
    // this flag generally only becomes set due to some kind of
    // software error.
    RXUNDER OFFSET(8) NUMBITS(4) [],
    // State machine has stalled on full RX FIFO during a
    // blocking PUSH, or an IN with autopush enabled. This flag
    // is also set when a nonblocking PUSH to a full FIFO took
    // place, in which case the state machine has dropped data.
    // Write 1 to clear.
    RXSTALL OFFSET(0) NUMBITS(4) []
],
FLEVEL [
    RX3 OFFSET(28) NUMBITS(4) [],
    TX3 OFFSET(24) NUMBITS(4) [],
    RX2 OFFSET(20) NUMBITS(4) [],
    TX2 OFFSET(16) NUMBITS(4) [],
    RX1 OFFSET(12) NUMBITS(4) [],
    TX1 OFFSET(8) NUMBITS(4) [],
    RX0 OFFSET(4) NUMBITS(4) [],
    TX0 OFFSET(0) NUMBITS(4) []
],
TXFx [
    TXF OFFSET(0) NUMBITS(32) []
],
RXFx [
    RXF OFFSET(0) NUMBITS(32) []
],
IRQ [
    IRQ7 OFFSET(7) NUMBITS(1) [],
    IRQ6 OFFSET(6) NUMBITS(1) [],
    IRQ5 OFFSET(5) NUMBITS(1) [],
    IRQ4 OFFSET(4) NUMBITS(1) [],
    IRQ3 OFFSET(3) NUMBITS(1) [],
    IRQ2 OFFSET(2) NUMBITS(1) [],
    IRQ1 OFFSET(1) NUMBITS(1) [],
    IRQ0 OFFSET(0) NUMBITS(1) []
],
IRQ_FORCE [
    IRQ_FORCE OFFSET(0) NUMBITS(8) []
],
INPUT_SYNC_BYPASS [
    INPUT_SYNC_BYPASS OFFSET(0) NUMBITS(32) []
],
DBG_PADOUT [
    DBG_PADOUT OFFSET(0) NUMBITS(32) []
],
DBG_PADOE [
    DBG_PADOE OFFSET(0) NUMBITS(32) []
],
DBG_CFGINFO [
    // The size of the instruction memory, measured in units of
    // one instruction
    IMEM_SIZE OFFSET(16) NUMBITS(6) [],
    // The number of state machines this PIO instance is
    // equipped with.
    SM_COUNT OFFSET(8) NUMBITS(4) [],
    // The depth of the state machine TX/RX FIFOs, measured in
    // words.
    FIFO_DEPTH OFFSET(0) NUMBITS(6) []
],
INSTR_MEMx [
    // Write-only access to instruction memory location x
    INSTR_MEM OFFSET(0) NUMBITS(16) []
],
SMx_CLKDIV [
    // Effective frequency is sysclk/(int + frac/256).
    // Value of 0 is interpreted as 65536. If INT is 0, FRAC must
    // also be 0.
    INT OFFSET(16) NUMBITS(16) [],
    // Fractional part of clock divisor
    FRAC OFFSET(8) NUMBITS(8) []
],
SMx_EXECCTRL [
    // If 1, an instruction written to SMx_INSTR is stalled, and
    // latched by the state machine. Will clear to 0 once this
    // instruction completes.
    EXEC_STALLED OFFSET(31) NUMBITS(1) [],
    // If 1, the MSB of the Delay/Side-set instruction field is used
    // as side-set enable, rather than a side-set data bit. This
    // allows instructions to perform side-set optionally, rather
    // than on every instruction, but the maximum possible side-
    // set width is reduced from 5 to 4. Note that the value of
    // PINCTRL_SIDESET_COUNT is inclusive of this enable bit.
    SIDE_EN OFFSET(30) NUMBITS(1) [],
    // If 1, side-set data is asserted to pin directions, instead of
    // pin values
    SIDE_PINDIR OFFSET(29) NUMBITS(1) [],
    // The GPIO number to use as condition for JMP PIN.
    // Unaffected by input mapping.
    JMP_PIN OFFSET(24) NUMBITS(5) [],
    // Which data bit to use for inline OUT enable
    OUT_EN_SEL OFFSET(19) NUMBITS(5) [],
    // If 1, use a bit of OUT data as an auxiliary write enable
    // When used in conjunction with OUT_STICKY, writes with
    // an enable of 0 will
    // deassert the latest pin write. This can create useful
    // masking/override behaviour
    // due to the priority ordering of state machine pin writes
    // (SM0 < SM1 < …)
    INLINE_OUT_EN OFFSET(18) NUMBITS(1) [],
    // Continuously assert the most recent OUT/SET to the pins
    OUT_STICKY OFFSET(17) NUMBITS(1) [],
    // After reaching this address, execution is wrapped to
    // wrap_bottom.
    // If the instruction is a jump, and the jump condition is true,
    // the jump takes priority.
    WRAP_TOP OFFSET(12) NUMBITS(5) [],
    // After reaching wrap_top, execution is wrapped to this
    // address.
    WRAP_BOTTOM OFFSET(7) NUMBITS(5) [],
    STATUS_SEL OFFSET(4) NUMBITS(1) [],
    // Comparison level for the MOV x, STATUS instruction
    STATUS_N OFFSET(0) NUMBITS(4) []
],
SMx_SHIFTCTRL [
    // When 1, RX FIFO steals the TX FIFO’s storage, and
    // becomes twice as deep.
    // TX FIFO is disabled as a result (always reads as both full
    // and empty).
    // FIFOs are flushed when this bit is changed.
    FJOIN_RX OFFSET(31) NUMBITS(1) [],
    // When 1, TX FIFO steals the RX FIFO’s storage, and
    // becomes twice as deep.
    // RX FIFO is disabled as a result (always reads as both full
    // and empty).
    // FIFOs are flushed when this bit is changed.
    FJOIN_TX OFFSET(30) NUMBITS(1) [],
    // Number of bits shifted out of OSR before autopull, or
    // conditional pull (PULL IFEMPTY), will take place.
    // Write 0 for value of 32.
    PULL_THRESH OFFSET(25) NUMBITS(5) [],
    // Number of bits shifted into ISR before autopush, or
    // conditional push (PUSH IFFULL), will take place.
    // Write 0 for value of 32
    PUSH_THRESH OFFSET(20) NUMBITS(5) [],
    OUT_SHIFTDIR OFFSET(19) NUMBITS(1) [
        ShiftRight = 1,
        ShiftLeft = 0
    ],
    IN_SHIFTDIR OFFSET(18) NUMBITS(1) [
        ShiftRight = 1,
        ShiftLeft = 0
    ],
    // Pull automatically when the output shift register is
    // emptied, i.e. on or following an OUT instruction which
    // causes the output shift counter to reach or exceed
    // PULL_THRESH.
    AUTOPULL OFFSET(17) NUMBITS(1) [],
    // Push automatically when the input shift register is filled,
    // i.e. on an IN instruction which causes the input shift
    // counter to reach or exceed PUSH_THRESH.
    AUTOPUSH OFFSET(16) NUMBITS(1) []
],
SMx_ADDR [
    ADDR OFFSET(0) NUMBITS(5) []
],
SMx_INSTR [
    INSTR OFFSET(0) NUMBITS(16) []
],
SMx_PINCTRL [
    // The number of MSBs of the Delay/Side-set instruction
    // field which are used for side-set. Inclusive of the enable
    // bit, if present. Minimum of 0 (all delay bits, no side-set)
    // and maximum of 5 (all side-set, no delay).
    SIDESET_COUNT OFFSET(29) NUMBITS(3) [],
    // The number of pins asserted by a SET. In the range 0 to 5
    // inclusive.
    SET_COUNT OFFSET(26) NUMBITS(3) [],
    // The number of pins asserted by an OUT PINS, OUT
    // PINDIRS or MOV PINS instruction. In the range 0 to 32
    // inclusive.
    OUT_COUNT OFFSET(20) NUMBITS(6) [],
    // The pin which is mapped to the least-significant bit of a
    // state machine’s IN data bus. Higher-numbered pins are
    // mapped to consecutively more-significant data bits, with a
    // modulo of 32 applied to pin number.
    IN_BASE OFFSET(15) NUMBITS(5) [],
    // The lowest-numbered pin that will be affected by a side-
    // set operation. The MSBs of an instruction’s side-set/delay
    // field (up to 5, determined by SIDESET_COUNT) are used
    // for side-set data, with the remaining LSBs used for delay.
    // The least-significant bit of the side-set portion is the bit
    // written to this pin, with more-significant bits written to
    // higher-numbered pins.
    SIDESET_BASE OFFSET(10) NUMBITS(5) [],
    // The lowest-numbered pin that will be affected by a SET
    // PINS or SET PINDIRS instruction. The data written to this
    // pin is the least-significant bit of the SET data.
    SET_BASE OFFSET(5) NUMBITS(5) [],
    // The lowest-numbered pin that will be affected by an OUT
    // PINS, OUT PINDIRS or MOV PINS instruction. The data
    // written to this pin will always be the least-significant bit of
    // the OUT or MOV data.
    OUT_BASE OFFSET(0) NUMBITS(5) []
],
INTR [
    SM3 OFFSET(11) NUMBITS(1) [],
    SM2 OFFSET(10) NUMBITS(1) [],
    SM1 OFFSET(9) NUMBITS(1) [],
    SM0 OFFSET(8) NUMBITS(1) [],
    SM3_TXNFULL OFFSET(7) NUMBITS(1) [],
    SM2_TXNFULL OFFSET(6) NUMBITS(1) [],
    SM1_TXNFULL OFFSET(5) NUMBITS(1) [],
    SM0_TXNFULL OFFSET(4) NUMBITS(1) [],
    SM3_RXNEMPTY OFFSET(3) NUMBITS(1) [],
    SM2_RXNEMPTY OFFSET(2) NUMBITS(1) [],
    SM1_RXNEMPTY OFFSET(1) NUMBITS(1) [],
    SM0_RXNEMPTY OFFSET(0) NUMBITS(1) []
],
IRQ0_INTE [
    SM3 OFFSET(11) NUMBITS(1) [],
    SM2 OFFSET(10) NUMBITS(1) [],
    SM1 OFFSET(9) NUMBITS(1) [],
    SM0 OFFSET(8) NUMBITS(1) [],
    SM3_TXNFULL OFFSET(7) NUMBITS(1) [],
    SM2_TXNFULL OFFSET(6) NUMBITS(1) [],
    SM1_TXNFULL OFFSET(5) NUMBITS(1) [],
    SM0_TXNFULL OFFSET(4) NUMBITS(1) [],
    SM3_RXNEMPTY OFFSET(3) NUMBITS(1) [],
    SM2_RXNEMPTY OFFSET(2) NUMBITS(1) [],
    SM1_RXNEMPTY OFFSET(1) NUMBITS(1) [],
    SM0_RXNEMPTY OFFSET(0) NUMBITS(1) []
],
IRQ0_INTF [
    SM3 OFFSET(11) NUMBITS(1) [],
    SM2 OFFSET(10) NUMBITS(1) [],
    SM1 OFFSET(9) NUMBITS(1) [],
    SM0 OFFSET(8) NUMBITS(1) [],
    SM3_TXNFULL OFFSET(7) NUMBITS(1) [],
    SM2_TXNFULL OFFSET(6) NUMBITS(1) [],
    SM1_TXNFULL OFFSET(5) NUMBITS(1) [],
    SM0_TXNFULL OFFSET(4) NUMBITS(1) [],
    SM3_RXNEMPTY OFFSET(3) NUMBITS(1) [],
    SM2_RXNEMPTY OFFSET(2) NUMBITS(1) [],
    SM1_RXNEMPTY OFFSET(1) NUMBITS(1) [],
    SM0_RXNEMPTY OFFSET(0) NUMBITS(1) []
],
IRQ0_INTS [
    SM3 OFFSET(0) NUMBITS(1) [],
    SM2 OFFSET(0) NUMBITS(1) [],
    SM1 OFFSET(0) NUMBITS(1) [],
    SM0 OFFSET(0) NUMBITS(1) [],
    SM3_TXNFULL OFFSET(0) NUMBITS(1) [],
    SM2_TXNFULL OFFSET(0) NUMBITS(1) [],
    SM1_TXNFULL OFFSET(0) NUMBITS(1) [],
    SM0_TXNFULL OFFSET(0) NUMBITS(1) [],
    SM3_RXNEMPTY OFFSET(0) NUMBITS(1) [],
    SM2_RXNEMPTY OFFSET(0) NUMBITS(1) [],
    SM1_RXNEMPTY OFFSET(0) NUMBITS(1) [],
    SM0_RXNEMPTY OFFSET(0) NUMBITS(1) []
],
IRQ1_INTE [
    SM3 OFFSET(11) NUMBITS(1) [],
    SM2 OFFSET(10) NUMBITS(1) [],
    SM1 OFFSET(9) NUMBITS(1) [],
    SM0 OFFSET(8) NUMBITS(1) [],
    SM3_TXNFULL OFFSET(7) NUMBITS(1) [],
    SM2_TXNFULL OFFSET(6) NUMBITS(1) [],
    SM1_TXNFULL OFFSET(5) NUMBITS(1) [],
    SM0_TXNFULL OFFSET(4) NUMBITS(1) [],
    SM3_RXNEMPTY OFFSET(3) NUMBITS(1) [],
    SM2_RXNEMPTY OFFSET(2) NUMBITS(1) [],
    SM1_RXNEMPTY OFFSET(1) NUMBITS(1) [],
    SM0_RXNEMPTY OFFSET(0) NUMBITS(1) []
],
IRQ1_INTF [
    SM3 OFFSET(11) NUMBITS(1) [],
    SM2 OFFSET(10) NUMBITS(1) [],
    SM1 OFFSET(9) NUMBITS(1) [],
    SM0 OFFSET(8) NUMBITS(1) [],
    SM3_TXNFULL OFFSET(7) NUMBITS(1) [],
    SM2_TXNFULL OFFSET(6) NUMBITS(1) [],
    SM1_TXNFULL OFFSET(5) NUMBITS(1) [],
    SM0_TXNFULL OFFSET(4) NUMBITS(1) [],
    SM3_RXNEMPTY OFFSET(3) NUMBITS(1) [],
    SM2_RXNEMPTY OFFSET(2) NUMBITS(1) [],
    SM1_RXNEMPTY OFFSET(1) NUMBITS(1) [],
    SM0_RXNEMPTY OFFSET(0) NUMBITS(1) []
],
IRQ1_INTS [
    SM3 OFFSET(11) NUMBITS(1) [],
    SM2 OFFSET(10) NUMBITS(1) [],
    SM1 OFFSET(9) NUMBITS(1) [],
    SM0 OFFSET(8) NUMBITS(1) [],
    SM3_TXNFULL OFFSET(7) NUMBITS(1) [],
    SM2_TXNFULL OFFSET(6) NUMBITS(1) [],
    SM1_TXNFULL OFFSET(5) NUMBITS(1) [],
    SM0_TXNFULL OFFSET(4) NUMBITS(1) [],
    SM3_RXNEMPTY OFFSET(3) NUMBITS(1) [],
    SM2_RXNEMPTY OFFSET(2) NUMBITS(1) [],
    SM1_RXNEMPTY OFFSET(1) NUMBITS(1) [],
    SM0_RXNEMPTY OFFSET(0) NUMBITS(1) []
]
];

const PIO_0_BASE_ADDRESS: usize = 0x50200000;
const PIO_1_BASE_ADDRESS: usize = 0x50300000;
const PIO0_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new(PIO_0_BASE_ADDRESS as *const PioRegisters) };
const PIO0_XOR_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new((PIO_0_BASE_ADDRESS + 0x1000) as *const PioRegisters) };
const PIO0_SET_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new((PIO_0_BASE_ADDRESS + 0x2000) as *const PioRegisters) };
const PIO0_CLEAR_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new((PIO_0_BASE_ADDRESS + 0x3000) as *const PioRegisters) };
const PIO1_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new(PIO_1_BASE_ADDRESS as *const PioRegisters) };
const PIO1_XOR_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new((PIO_1_BASE_ADDRESS + 0x1000) as *const PioRegisters) };
const PIO1_SET_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new((PIO_1_BASE_ADDRESS + 0x2000) as *const PioRegisters) };
const PIO1_CLEAR_BASE: StaticRef<PioRegisters> =
    unsafe { StaticRef::new((PIO_1_BASE_ADDRESS + 0x3000) as *const PioRegisters) };

/// Represents a relocated PIO program.
///
/// An [Iterator] that yields the original program except `JMP` instructions have
/// relocated target addresses based on an origin.
pub struct RelocatedProgram<'a, I>
where
    I: Iterator<Item = &'a u16>,
{
    iter: I,
    origin: usize,
}

impl<'a, I> RelocatedProgram<'a, I>
where
    I: Iterator<Item = &'a u16>,
{
    fn new(iter: I, origin: usize) -> Self {
        Self { iter, origin }
    }
}

impl<'a, I> Iterator for RelocatedProgram<'a, I>
where
    I: Iterator<Item = &'a u16>,
{
    type Item = u16;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|&instr| {
            if instr & 0b1110_0000_0000_0000 == 0 {
                // this is a JMP instruction -> add offset to address
                let address = instr & 0b1_1111;
                let address = address.wrapping_add(self.origin as u16) % 32;
                instr & (!0b11111) | address
            } else {
                instr
            }
        })
    }
}

pub struct LoadedProgram {
    used_memory: u32,
    origin: usize,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ProgramError {
    /// Insufficient consecutive free instruction space to load program.
    InsufficientSpace,
    /// Loading a program would overwrite the existing one
    AddrInUse(usize),
}

/// There are a total of 4 State Machines per PIO.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum SMNumber {
    SM0 = 0,
    SM1 = 1,
    SM2 = 2,
    SM3 = 3,
}

/// Array of all SMNumbers, used for convenience in Pio constructor
const SM_NUMBERS: [SMNumber; 4] = [SMNumber::SM0, SMNumber::SM1, SMNumber::SM2, SMNumber::SM3];

/// There can be 2 PIOs per RP2040.
#[derive(PartialEq)]
pub enum PIONumber {
    PIO0 = 0,
    PIO1 = 1,
}

/// The FIFO queues can be joined together for twice the length in one direction.
#[derive(PartialEq)]
pub enum PioFifoJoin {
    PioFifoJoinNone = 0,
    PioFifoJoinTx = 1,
    PioFifoJoinRx = 2,
}

/// PIO interrupt source numbers for PIO related interrupts
#[derive(PartialEq)]
pub enum InterruptSources {
    Interrupt0 = 0,
    Interrupt1 = 1,
    Interrupt2 = 2,
    Interrupt3 = 3,
    Sm0TXNotFull = 4,
    Sm1TXNotFull = 5,
    Sm2TXNotFull = 6,
    Sm3TXNotFull = 7,
    Sm0RXNotEmpty = 8,
    Sm1RXNotEmpty = 9,
    Sm2RXNotEmpty = 10,
    Sm3RXNotEmpty = 11,
}

pub trait PioTxClient {
    fn on_buffer_space_available(&self);
}

pub trait PioRxClient {
    fn on_data_received(&self, data: u32);
}

#[derive(Clone, Copy, Debug, Default)]
enum StateMachineState {
    #[default]
    Ready,
    Waiting,
}

pub struct StateMachine {
    sm_number: SMNumber,
    registers: StaticRef<PioRegisters>,
    xor_registers: StaticRef<PioRegisters>,
    set_registers: StaticRef<PioRegisters>,
    tx_state: Cell<StateMachineState>,
    tx_client: OptionalCell<&'static dyn PioTxClient>,
    rx_state: Cell<StateMachineState>,
    rx_client: OptionalCell<&'static dyn PioRxClient>,
}

impl StateMachine {
    fn new(
        sm_id: SMNumber,
        registers: StaticRef<PioRegisters>,
        xor_registers: StaticRef<PioRegisters>,
        set_registers: StaticRef<PioRegisters>,
    ) -> StateMachine {
        StateMachine {
            sm_number: sm_id,
            registers,
            xor_registers,
            set_registers,
            tx_state: Cell::new(StateMachineState::Ready),
            tx_client: OptionalCell::empty(),
            rx_state: Cell::new(StateMachineState::Ready),
            rx_client: OptionalCell::empty(),
        }
    }

    /// State machine configuration with any config structure.
    pub fn config(&self, config: &StateMachineConfiguration) {
        self.set_in_pins(config.in_pins_base);
        self.set_out_pins(config.out_pins_base, config.out_pins_count);
        self.set_set_pins(config.set_pins_base, config.set_pins_count);
        self.set_side_set_pins(
            config.side_set_base,
            config.side_set_bit_count,
            config.side_set_opt_enable,
            config.side_set_pindirs,
        );
        self.set_in_shift(
            config.in_shift_direction_right,
            config.in_autopush,
            config.in_push_threshold,
        );
        self.set_out_shift(
            config.out_shift_direction_right,
            config.out_autopull,
            config.out_pull_threshold,
        );
        self.set_jmp_pin(config.jmp_pin);
        self.set_wrap(config.wrap_to, config.wrap);
        self.set_mov_status(config.mov_status_sel, config.mov_status_n);
        self.set_out_special(
            config.out_special_sticky,
            config.out_special_has_enable_pin,
            config.out_special_enable_pin_index,
        );
        self.set_clkdiv_int_frac(config.div_int, config.div_frac);
    }

    /// Set tx client for a state machine.
    pub fn set_tx_client(&self, client: &'static dyn PioTxClient) {
        self.tx_client.set(client);
    }

    /// Set rx client for a state machine.
    pub fn set_rx_client(&self, client: &'static dyn PioRxClient) {
        self.rx_client.set(client);
    }

    /// Set every config for the IN pins.
    ///
    /// in_base => the starting location for the input pins
    pub fn set_in_pins(&self, in_base: u32) {
        self.registers.sm[self.sm_number as usize]
            .pinctrl
            .modify(SMx_PINCTRL::IN_BASE.val(in_base));
    }

    /// Set every config for the SET pins.
    ///
    /// set_base => the starting location for the SET pins
    /// set_count => the number of SET pins
    pub fn set_set_pins(&self, set_base: u32, set_count: u32) {
        self.registers.sm[self.sm_number as usize]
            .pinctrl
            .modify(SMx_PINCTRL::SET_BASE.val(set_base));
        self.registers.sm[self.sm_number as usize]
            .pinctrl
            .modify(SMx_PINCTRL::SET_COUNT.val(set_count));
    }

    /// Set every config for the OUT pins.
    ///
    /// out_base => the starting location for the OUT pins
    /// out_count => the number of OUT pins
    pub fn set_out_pins(&self, out_base: u32, out_count: u32) {
        self.registers.sm[self.sm_number as usize]
            .pinctrl
            .modify(SMx_PINCTRL::OUT_BASE.val(out_base));
        self.registers.sm[self.sm_number as usize]
            .pinctrl
            .modify(SMx_PINCTRL::OUT_COUNT.val(out_count));
    }

    /// Setup 'in' shifting parameters.
    ///
    ///  shift_right => true to shift ISR to right or false to shift to left
    ///  autopush => true to enable, false to disable
    ///  push_threshold => threshold in bits to shift in before auto/conditional re-pushing of the ISR
    pub fn set_in_shift(&self, shift_right: bool, autopush: bool, push_threshold: u32) {
        self.registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::IN_SHIFTDIR.val(shift_right.into()));
        self.registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::AUTOPUSH.val(autopush.into()));
        self.registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::PUSH_THRESH.val(push_threshold));
    }

    /// Setup 'out' shifting parameters.
    ///
    /// shift_right => `true` to shift OSR to right or false to shift to left
    /// autopull => true to enable, false to disable
    /// pull_threshold => threshold in bits to shift out before auto/conditional re-pulling of the OSR
    pub fn set_out_shift(&self, shift_right: bool, autopull: bool, pull_threshold: u32) {
        self.registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::OUT_SHIFTDIR.val(shift_right.into()));
        self.registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::AUTOPULL.val(autopull.into()));
        self.registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::PULL_THRESH.val(pull_threshold));
    }

    /// Set special OUT operations in a state machine.
    ///
    /// sticky
    /// => true to enable sticky output (rere-asserting most recent OUT/SET pin values on subsequent cycles)
    /// => false to disable sticky output
    /// has_enable_pin
    /// => true to enable auxiliary OUT enable pin
    /// => false to disable auxiliary OUT enable pin
    /// enable_pin_index => pin index for auxiliary OUT enable
    pub fn set_out_special(&self, sticky: bool, has_enable_pin: bool, enable_pin_index: u32) {
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::OUT_STICKY.val(sticky as u32));
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::INLINE_OUT_EN.val(has_enable_pin as u32));
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::OUT_EN_SEL.val(enable_pin_index));
    }

    /// Set the 'jmp' pin.
    ///
    /// pin => the raw GPIO pin number to use as the source for a jmp pin instruction
    pub fn set_jmp_pin(&self, pin: u32) {
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::JMP_PIN.val(pin));
    }

    /// Set the clock divider for a state machine.
    ///
    /// div_int => Integer part of the divisor
    /// div_frac => Fractional part in 1/256ths
    pub fn set_clkdiv_int_frac(&self, div_int: u32, div_frac: u32) {
        self.registers.sm[self.sm_number as usize]
            .clkdiv
            .modify(SMx_CLKDIV::INT.val(div_int));
        self.registers.sm[self.sm_number as usize]
            .clkdiv
            .modify(SMx_CLKDIV::FRAC.val(div_frac));
    }

    /// Setup the FIFO joining in a state machine.
    ///
    /// fifo_join => specifies the join type - see the `PioFifoJoin` type
    pub fn set_fifo_join(&self, fifo_join: PioFifoJoin) {
        if fifo_join == PioFifoJoin::PioFifoJoinRx {
            self.registers.sm[self.sm_number as usize]
                .shiftctrl
                .modify(SMx_SHIFTCTRL::FJOIN_RX.val(fifo_join as u32));
        } else if fifo_join == PioFifoJoin::PioFifoJoinTx {
            self.registers.sm[self.sm_number as usize]
                .shiftctrl
                .modify(SMx_SHIFTCTRL::FJOIN_TX.val(fifo_join as u32));
        }
    }

    /// Set every config for the SIDESET pins.
    ///
    /// sideset_base => the starting location for the SIDESET pins
    /// bit_count => number of SIDESET bits per instruction - max 5
    /// optional
    /// => true to use the topmost sideset bit as a flag for whether to apply side set on that instruction
    /// => false to use sideset with every instruction
    /// pindirs
    /// => true to affect pin direction
    /// => false to affect value of a pin
    pub fn set_side_set_pins(
        &self,
        sideset_base: u32,
        bit_count: u32,
        optional: bool,
        pindirs: bool,
    ) {
        self.registers.sm[self.sm_number as usize]
            .pinctrl
            .modify(SMx_PINCTRL::SIDESET_BASE.val(sideset_base));
        self.registers.sm[self.sm_number as usize]
            .pinctrl
            .modify(SMx_PINCTRL::SIDESET_COUNT.val(bit_count));
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::SIDE_EN.val(optional as u32));
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::SIDE_PINDIR.val(pindirs as u32));
    }

    /// Use a state machine to set the same pin direction for multiple consecutive pins for the PIO instance.
    /// This is the pio_sm_set_consecutive_pindirs function from the pico sdk, renamed to be more clear.
    ///
    /// pin => starting pin
    /// count => how many pins (including the base) should be changed
    /// is_out
    /// => true to set the pin as OUT
    /// => false to set the pin as IN
    pub fn set_pins_dirs(&self, mut pin: u32, mut count: u32, is_out: bool) {
        // "set pindirs, 0" command created by pioasm
        let set_pindirs_0: u16 = 0b1110000010000000;
        self.with_paused(|| {
            let mut pindir_val: u8 = 0x00;
            if is_out {
                pindir_val = 0x1f;
            }
            while count > 5 {
                self.registers.sm[self.sm_number as usize]
                    .pinctrl
                    .modify(SMx_PINCTRL::SET_COUNT.val(5));
                self.registers.sm[self.sm_number as usize]
                    .pinctrl
                    .modify(SMx_PINCTRL::SET_BASE.val(pin));
                self.exec((set_pindirs_0) | (pindir_val as u16));
                count -= 5;
                pin = (pin + 5) & 0x1f;
            }
            self.registers.sm[self.sm_number as usize]
                .pinctrl
                .modify(SMx_PINCTRL::SET_COUNT.val(count));
            self.registers.sm[self.sm_number as usize]
                .pinctrl
                .modify(SMx_PINCTRL::SET_BASE.val(pin));
            self.exec((set_pindirs_0) | (pindir_val as u16));
        });
    }

    /// Sets pin output values. Pauses the state machine to run `SET` commands
    /// and temporarily unsets the `OUT_STICKY` bit to avoid side effects.
    ///
    /// pins => pins to set the value for
    /// high => true to set the pin high
    pub fn set_pins(&self, pins: &[&RPGpioPin<'_>], high: bool) {
        self.with_paused(|| {
            for pin in pins {
                self.registers.sm[self.sm_number as usize]
                    .pinctrl
                    .modify(SMx_PINCTRL::SET_BASE.val(pin.pin() as u32));
                self.registers.sm[self.sm_number as usize]
                    .pinctrl
                    .modify(SMx_PINCTRL::SET_COUNT.val(1));

                self.exec(0b11100_000_000_00000 | high as u16);
            }
        });
    }

    /// Set the wrap addresses for a state machine.
    ///
    /// wrap_target => the instruction memory address to wrap to
    /// wrap => the instruction memory address after which the program counters wraps to the target
    pub fn set_wrap(&self, wrap_target: u32, wrap: u32) {
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::WRAP_BOTTOM.val(wrap_target));
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::WRAP_TOP.val(wrap));
    }

    /// Resets the state machine to a consistent state and configures it.
    pub fn init(&self) {
        self.clear_fifos();
        self.restart();
        self.clkdiv_restart();
        self.registers.sm[self.sm_number as usize]
            .instr
            .modify(SMx_INSTR::INSTR.val(0));
    }

    /// Restart a state machine.
    pub fn restart(&self) {
        match self.sm_number {
            SMNumber::SM0 => self.set_registers.ctrl.modify(CTRL::SM0_RESTART::SET),
            SMNumber::SM1 => self.set_registers.ctrl.modify(CTRL::SM1_RESTART::SET),
            SMNumber::SM2 => self.set_registers.ctrl.modify(CTRL::SM2_RESTART::SET),
            SMNumber::SM3 => self.set_registers.ctrl.modify(CTRL::SM3_RESTART::SET),
        }
    }

    /// Clear a state machine’s TX and RX FIFOs.
    pub fn clear_fifos(&self) {
        self.xor_registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::FJOIN_RX::SET);
        self.xor_registers.sm[self.sm_number as usize]
            .shiftctrl
            .modify(SMx_SHIFTCTRL::FJOIN_RX::SET);
    }

    /// Restart a state machine's clock divider.
    pub fn clkdiv_restart(&self) {
        match self.sm_number {
            SMNumber::SM0 => self.set_registers.ctrl.modify(CTRL::CLKDIV0_RESTART::SET),
            SMNumber::SM1 => self.set_registers.ctrl.modify(CTRL::CLKDIV1_RESTART::SET),
            SMNumber::SM2 => self.set_registers.ctrl.modify(CTRL::CLKDIV2_RESTART::SET),
            SMNumber::SM3 => self.set_registers.ctrl.modify(CTRL::CLKDIV3_RESTART::SET),
        }
    }

    /// Returns true if the TX FIFO is full.
    pub fn tx_full(&self) -> bool {
        let field = match self.sm_number {
            SMNumber::SM0 => FSTAT::TXFULL0,
            SMNumber::SM1 => FSTAT::TXFULL1,
            SMNumber::SM2 => FSTAT::TXFULL2,
            SMNumber::SM3 => FSTAT::TXFULL3,
        };
        self.registers.fstat.read(field) != 0
    }

    /// Returns true if the RX FIFO is empty.
    pub fn rx_empty(&self) -> bool {
        let field = match self.sm_number {
            SMNumber::SM0 => FSTAT::RXEMPTY0,
            SMNumber::SM1 => FSTAT::RXEMPTY1,
            SMNumber::SM2 => FSTAT::RXEMPTY2,
            SMNumber::SM3 => FSTAT::RXEMPTY3,
        };
        self.registers.fstat.read(field) != 0
    }

    /// Immediately execute an instruction on a state machine.
    ///
    /// => instr: the instruction to execute
    /// Implicitly restricted size of instr to u16, cause it's the size pio asm instr
    pub fn exec(&self, instr: u16) {
        self.registers.sm[self.sm_number as usize]
            .instr
            .modify(SMx_INSTR::INSTR.val(instr as u32));
    }

    /// Executes a program on a state machine.
    /// Jumps to the instruction at given address and runs the program.
    ///
    /// => program: a program loaded to PIO
    /// => wrap: true to wrap the program, so it runs in loop
    pub fn exec_program(&self, program: LoadedProgram, wrap: bool) {
        if wrap {
            self.set_wrap(
                program.origin as u32,
                program.origin as u32 + program.used_memory.count_ones(),
            );
        }
        self.exec((program.origin as u16) & 0x1fu16)
    }

    /// Set source for 'mov status' in a state machine.
    ///
    /// status_sel => comparison used for the `MOV x, STATUS` instruction
    /// status_n => comparison level for the `MOV x, STATUS` instruction
    pub fn set_mov_status(&self, status_sel: PioMovStatusType, status_n: u32) {
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::STATUS_SEL.val(status_sel as u32));
        self.registers.sm[self.sm_number as usize]
            .execctrl
            .modify(SMx_EXECCTRL::STATUS_N.val(status_n));
    }

    /// Set a state machine's state to enabled or to disabled.
    ///
    /// enabled => true to enable the state machine
    pub fn set_enabled(&self, enabled: bool) {
        match self.sm_number {
            SMNumber::SM0 => self.registers.ctrl.modify(match enabled {
                true => CTRL::SM0_ENABLE::SET,
                false => CTRL::SM0_ENABLE::CLEAR,
            }),
            SMNumber::SM1 => self.registers.ctrl.modify(match enabled {
                true => CTRL::SM1_ENABLE::SET,
                false => CTRL::SM1_ENABLE::CLEAR,
            }),
            SMNumber::SM2 => self.registers.ctrl.modify(match enabled {
                true => CTRL::SM2_ENABLE::SET,
                false => CTRL::SM2_ENABLE::CLEAR,
            }),
            SMNumber::SM3 => self.registers.ctrl.modify(match enabled {
                true => CTRL::SM3_ENABLE::SET,
                false => CTRL::SM3_ENABLE::CLEAR,
            }),
        }
    }

    /// Is state machine enabled.
    pub fn is_enabled(&self) -> bool {
        let field = match self.sm_number {
            SMNumber::SM0 => CTRL::SM0_ENABLE,
            SMNumber::SM1 => CTRL::SM1_ENABLE,
            SMNumber::SM2 => CTRL::SM2_ENABLE,
            SMNumber::SM3 => CTRL::SM3_ENABLE,
        };
        self.registers.ctrl.read(field) != 0
    }

    /// Runs function with the state machine paused.
    /// Keeps pinctrl and execctrl of the SM the same during execution
    fn with_paused(&self, f: impl FnOnce()) {
        let enabled = self.is_enabled();
        self.set_enabled(false);

        let pio_sm = &self.registers.sm[self.sm_number as usize];

        let pinctrl = pio_sm.pinctrl.get();
        let execctrl = pio_sm.execctrl.get();
        // Hold pins value set by latest OUT/SET op
        pio_sm.execctrl.modify(SMx_EXECCTRL::OUT_STICKY::CLEAR);

        f();

        pio_sm.pinctrl.set(pinctrl);
        pio_sm.execctrl.set(execctrl);
        self.set_enabled(enabled);
    }

    /// Write a word of data to a state machine’s TX FIFO.
    /// If the FIFO is full, the client will be notified when space is available.
    ///
    /// => data: the data to write to the FIFO
    pub fn push(&self, data: u32) -> Result<(), ErrorCode> {
        match self.tx_state.get() {
            StateMachineState::Ready => {
                if self.tx_full() {
                    // TX queue is full, set interrupt
                    let field = match self.sm_number {
                        SMNumber::SM0 => IRQ0_INTE::SM0_TXNFULL::SET,
                        SMNumber::SM1 => IRQ0_INTE::SM1_TXNFULL::SET,
                        SMNumber::SM2 => IRQ0_INTE::SM2_TXNFULL::SET,
                        SMNumber::SM3 => IRQ0_INTE::SM3_TXNFULL::SET,
                    };
                    self.registers.irq0_inte.modify(field);
                    self.tx_state.set(StateMachineState::Waiting);
                    Err(ErrorCode::BUSY)
                } else {
                    self.registers.txf[self.sm_number as usize].set(data);
                    Ok(())
                }
            }
            StateMachineState::Waiting => Err(ErrorCode::BUSY),
        }
    }

    /// Wait until a state machine's TX FIFO is empty, then write a word of data to it.
    /// If state machine is disabled and there is no space, an error will be returned.
    /// If SM is stalled on RX or in loop, this will block forever.
    ///
    /// => data: the data to write to the FIFO
    pub fn push_blocking(&self, data: u32) -> Result<(), ErrorCode> {
        if self.tx_full() && !self.is_enabled() {
            return Err(ErrorCode::OFF);
        }
        while self.tx_full() {}
        self.registers.txf[self.sm_number as usize].set(data);
        Ok(())
    }

    /// Read a word of data from a state machine’s RX FIFO.
    /// If the FIFO is empty, the client will be notified when data is available.
    pub fn pull(&self) -> Result<u32, ErrorCode> {
        match self.rx_state.get() {
            StateMachineState::Ready => {
                if self.rx_empty() {
                    // RX queue is empty, set interrupt
                    let field = match self.sm_number {
                        SMNumber::SM0 => IRQ0_INTE::SM0_RXNEMPTY::SET,
                        SMNumber::SM1 => IRQ0_INTE::SM1_RXNEMPTY::SET,
                        SMNumber::SM2 => IRQ0_INTE::SM2_RXNEMPTY::SET,
                        SMNumber::SM3 => IRQ0_INTE::SM3_RXNEMPTY::SET,
                    };
                    self.registers.irq0_inte.modify(field);
                    self.rx_state.set(StateMachineState::Waiting);
                    Err(ErrorCode::BUSY)
                } else {
                    Ok(self.registers.rxf[self.sm_number as usize].read(RXFx::RXF))
                }
            }
            StateMachineState::Waiting => Err(ErrorCode::BUSY),
        }
    }

    /// Reads a word of data from a state machine’s RX FIFO.
    /// If state machine is disabled and there is no space, an error will be returned.
    /// If SM is stalled on TX or in loop, this will block forever.
    pub fn pull_blocking(&self) -> Result<u32, ErrorCode> {
        if self.tx_full() && !self.is_enabled() {
            return Err(ErrorCode::OFF);
        }
        while self.rx_empty() {}
        Ok(self.registers.rxf[self.sm_number as usize].read(RXFx::RXF))
    }

    /// Handle a TX interrupt - notify that buffer space is available.
    fn handle_tx_interrupt(&self) {
        match self.tx_state.get() {
            StateMachineState::Waiting => {
                // TX queue has emptied, clear interrupt
                let field = match self.sm_number {
                    SMNumber::SM0 => IRQ0_INTE::SM0_TXNFULL::CLEAR,
                    SMNumber::SM1 => IRQ0_INTE::SM1_TXNFULL::CLEAR,
                    SMNumber::SM2 => IRQ0_INTE::SM2_TXNFULL::CLEAR,
                    SMNumber::SM3 => IRQ0_INTE::SM3_TXNFULL::CLEAR,
                };
                self.registers.irq0_inte.modify(field);
                self.tx_state.set(StateMachineState::Ready);
                self.tx_client.map(|client| {
                    client.on_buffer_space_available();
                });
            }
            StateMachineState::Ready => {}
        }
    }

    /// Handle an RX interrupt - notify that data has been received.
    fn handle_rx_interrupt(&self) {
        match self.rx_state.get() {
            StateMachineState::Waiting => {
                // RX queue has data, clear interrupt
                let field = match self.sm_number {
                    SMNumber::SM0 => IRQ0_INTE::SM0_RXNEMPTY::CLEAR,
                    SMNumber::SM1 => IRQ0_INTE::SM1_RXNEMPTY::CLEAR,
                    SMNumber::SM2 => IRQ0_INTE::SM2_RXNEMPTY::CLEAR,
                    SMNumber::SM3 => IRQ0_INTE::SM3_RXNEMPTY::CLEAR,
                };
                self.registers.irq0_inte.modify(field);
                self.rx_state.set(StateMachineState::Ready);
                self.rx_client.map(|client| {
                    client.on_data_received(
                        self.registers.rxf[self.sm_number as usize].read(RXFx::RXF),
                    );
                });
            }
            StateMachineState::Ready => {}
        }
    }
}

pub struct Pio {
    registers: StaticRef<PioRegisters>,
    pio_number: PIONumber,
    sms: [StateMachine; NUMBER_STATE_MACHINES],
    instructions_used: Cell<u32>,
    _clear_registers: StaticRef<PioRegisters>,
}

/// 'MOV STATUS' types.
#[derive(Clone, Copy)]
pub enum PioMovStatusType {
    StatusTxLessthan = 0,
    StatusRxLessthan = 1,
}

/// PIO State Machine configuration structure
///
/// Used to initialize a PIO with all of its state machines.
pub struct StateMachineConfiguration {
    pub out_pins_count: u32,
    pub out_pins_base: u32,
    pub set_pins_count: u32,
    pub set_pins_base: u32,
    pub in_pins_base: u32,
    pub side_set_base: u32,
    pub side_set_opt_enable: bool,
    pub side_set_bit_count: u32,
    pub side_set_pindirs: bool,
    pub wrap: u32,
    pub wrap_to: u32,
    pub in_shift_direction_right: bool,
    pub in_autopush: bool,
    pub in_push_threshold: u32,
    pub out_shift_direction_right: bool,
    pub out_autopull: bool,
    pub out_pull_threshold: u32,
    pub jmp_pin: u32,
    pub out_special_sticky: bool,
    pub out_special_has_enable_pin: bool,
    pub out_special_enable_pin_index: u32,
    pub mov_status_sel: PioMovStatusType,
    pub mov_status_n: u32,
    pub div_int: u32,
    pub div_frac: u32,
}

impl Default for StateMachineConfiguration {
    fn default() -> Self {
        StateMachineConfiguration {
            out_pins_count: 32,
            out_pins_base: 0,
            set_pins_count: 0,
            set_pins_base: 0,
            in_pins_base: 0,
            side_set_base: 0,
            side_set_opt_enable: false,
            side_set_bit_count: 0,
            side_set_pindirs: false,
            wrap: 31,
            wrap_to: 0,
            in_shift_direction_right: true,
            in_autopush: false,
            in_push_threshold: 32,
            out_shift_direction_right: true,
            out_autopull: false,
            out_pull_threshold: 32,
            jmp_pin: 0,
            out_special_sticky: false,
            out_special_has_enable_pin: false,
            out_special_enable_pin_index: 0,
            mov_status_sel: PioMovStatusType::StatusTxLessthan,
            mov_status_n: 0,
            div_int: 0,
            div_frac: 0,
        }
    }
}

impl Pio {
    /// Setup the function select for a GPIO to use output from the given PIO instance.
    pub fn gpio_init(&self, pin: &RPGpioPin) {
        if self.pio_number == PIONumber::PIO1 {
            pin.set_function(GpioFunction::PIO1)
        } else {
            pin.set_function(GpioFunction::PIO0)
        }
    }

    /// Create a new PIO0 struct.
    pub fn new_pio0() -> Self {
        Self {
            registers: PIO0_BASE,
            _clear_registers: PIO0_CLEAR_BASE,
            pio_number: PIONumber::PIO0,
            sms: SM_NUMBERS.map(|x| StateMachine::new(x, PIO0_BASE, PIO0_XOR_BASE, PIO0_SET_BASE)),
            instructions_used: Cell::new(0),
        }
    }

    /// Create a new PIO1 struct.
    pub fn new_pio1() -> Self {
        Self {
            registers: PIO1_BASE,
            _clear_registers: PIO1_CLEAR_BASE,
            pio_number: PIONumber::PIO1,
            sms: SM_NUMBERS.map(|x| StateMachine::new(x, PIO1_BASE, PIO1_XOR_BASE, PIO1_SET_BASE)),
            instructions_used: Cell::new(0),
        }
    }

    /// Get state machine
    pub fn sm(&self, sm_number: SMNumber) -> &StateMachine {
        &self.sms[sm_number as usize]
    }

    /// Enable/Disable a single source on a PIO's IRQ index.
    pub fn set_irq_source(
        &self,
        irq_index: u32,
        interrupt_source: InterruptSources,
        enabled: bool,
    ) {
        if irq_index == 0 {
            match interrupt_source {
                InterruptSources::Interrupt0 => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM0.val(enabled as u32)),
                InterruptSources::Interrupt1 => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM1.val(enabled as u32)),
                InterruptSources::Interrupt2 => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM2.val(enabled as u32)),
                InterruptSources::Interrupt3 => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM3.val(enabled as u32)),
                InterruptSources::Sm0TXNotFull => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM0_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm1TXNotFull => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM1_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm2TXNotFull => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM2_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm3TXNotFull => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM3_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm0RXNotEmpty => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM0_RXNEMPTY.val(enabled as u32)),
                InterruptSources::Sm1RXNotEmpty => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM1_RXNEMPTY.val(enabled as u32)),
                InterruptSources::Sm2RXNotEmpty => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM2_RXNEMPTY.val(enabled as u32)),
                InterruptSources::Sm3RXNotEmpty => self
                    .registers
                    .irq0_inte
                    .modify(IRQ0_INTE::SM3_RXNEMPTY.val(enabled as u32)),
            }
        } else if irq_index == 1 {
            match interrupt_source {
                InterruptSources::Interrupt0 => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM0.val(enabled as u32)),
                InterruptSources::Interrupt1 => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM1.val(enabled as u32)),
                InterruptSources::Interrupt2 => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM2.val(enabled as u32)),
                InterruptSources::Interrupt3 => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM3.val(enabled as u32)),
                InterruptSources::Sm0TXNotFull => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM0_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm1TXNotFull => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM1_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm2TXNotFull => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM2_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm3TXNotFull => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM3_TXNFULL.val(enabled as u32)),
                InterruptSources::Sm0RXNotEmpty => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM0_RXNEMPTY.val(enabled as u32)),
                InterruptSources::Sm1RXNotEmpty => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM1_RXNEMPTY.val(enabled as u32)),
                InterruptSources::Sm2RXNotEmpty => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM2_RXNEMPTY.val(enabled as u32)),
                InterruptSources::Sm3RXNotEmpty => self
                    .registers
                    .irq1_inte
                    .modify(IRQ1_INTE::SM3_RXNEMPTY.val(enabled as u32)),
            }
        } else {
            debug!("IRQ Index invalid - must be 0 or 1");
        }
    }

    /// Checks if a PIO interrupt is set.
    pub fn interrupt_get(&self, irq_num: u32) -> bool {
        let mut temp = 0;
        match irq_num {
            0 => temp = self.registers.irq.read(IRQ::IRQ0),
            1 => temp = self.registers.irq.read(IRQ::IRQ1),
            2 => temp = self.registers.irq.read(IRQ::IRQ2),
            3 => temp = self.registers.irq.read(IRQ::IRQ3),
            4 => temp = self.registers.irq.read(IRQ::IRQ4),
            5 => temp = self.registers.irq.read(IRQ::IRQ5),
            6 => temp = self.registers.irq.read(IRQ::IRQ6),
            7 => temp = self.registers.irq.read(IRQ::IRQ7),
            _ => debug!("IRQ Number invalid - must be from 0 to 7"),
        };
        temp != 0
    }

    /// Clear a PIO interrupt.
    pub fn interrupt_clear(&self, irq_num: u32) {
        match irq_num {
            0 => self.registers.irq.modify(IRQ::IRQ0.val(1)),
            1 => self.registers.irq.modify(IRQ::IRQ1.val(1)),
            2 => self.registers.irq.modify(IRQ::IRQ2.val(1)),
            3 => self.registers.irq.modify(IRQ::IRQ3.val(1)),
            4 => self.registers.irq.modify(IRQ::IRQ4.val(1)),
            5 => self.registers.irq.modify(IRQ::IRQ5.val(1)),
            6 => self.registers.irq.modify(IRQ::IRQ6.val(1)),
            7 => self.registers.irq.modify(IRQ::IRQ7.val(1)),
            _ => debug!("IRQ Number invalid - must be from 0 to 7"),
        }
    }

    /// Handle interrupts
    pub fn handle_interrupt(&self) {
        let ints = &self.registers.irq0_ints;
        for (sm, irq) in self.sms.iter().zip([
            IRQ0_INTS::SM0_TXNFULL,
            IRQ0_INTS::SM1_TXNFULL,
            IRQ0_INTS::SM2_TXNFULL,
            IRQ0_INTS::SM3_TXNFULL,
        ]) {
            if ints.is_set(irq) {
                sm.handle_tx_interrupt();
            }
        }
        for (sm, irq) in self.sms.iter().zip([
            IRQ0_INTS::SM0_RXNEMPTY,
            IRQ0_INTS::SM1_RXNEMPTY,
            IRQ0_INTS::SM2_RXNEMPTY,
            IRQ0_INTS::SM3_RXNEMPTY,
        ]) {
            if ints.is_set(irq) {
                sm.handle_rx_interrupt();
            }
        }
    }

    /// Adds a program to PIO.
    /// Call this with `add_program(Some(0), include_bytes!("path_to_file"))`.
    /// => origin: the address in the PIO instruction memory to start the program at or None to find an empty space
    /// => program: the program to load into the PIO
    /// Returns `LoadedProgram` which contains information about program location and length.
    pub fn add_program(
        &self,
        origin: Option<usize>,
        program: &[u8],
    ) -> Result<LoadedProgram, ProgramError> {
        let mut program_u16: [u16; NUMBER_INSTR_MEMORY_LOCATIONS / 2] =
            [0; NUMBER_INSTR_MEMORY_LOCATIONS / 2];
        for (i, chunk) in program.chunks(2).enumerate() {
            program_u16[i] = ((chunk[0] as u16) << 8) | (chunk[1] as u16);
        }

        self.add_program16(origin, &program_u16[0..program.len() / 2])
    }

    /// Adds a program to PIO.
    /// Takes `&[u16]` as input, cause pio-asm operations are 16bit.
    /// => origin: the address in the PIO instruction memory to start the program at or None to find an empty space
    /// => program: the program to load into the PIO
    /// Returns `LoadedProgram` which contains information about program location and size.
    pub fn add_program16(
        &self,
        origin: Option<usize>,
        program: &[u16],
    ) -> Result<LoadedProgram, ProgramError> {
        // if origin is not set, try naively to find an empty space
        match origin {
            Some(origin) => {
                assert!(origin < NUMBER_INSTR_MEMORY_LOCATIONS);
                self.try_load_program_at(origin, program)
                    .map_err(|_| ProgramError::AddrInUse(origin))
            }
            None => {
                for origin in 0..NUMBER_INSTR_MEMORY_LOCATIONS {
                    if let res @ Ok(_) = self.try_load_program_at(origin, program) {
                        return res;
                    }
                }
                Err(ProgramError::InsufficientSpace)
            }
        }
    }

    /// Try to load program at a specific origin, relocate operations if necessary.
    /// Only for internals, use `add_program` or `add_program16` instead.
    /// => origin: the address in the PIO instruction memory to start the program at
    /// => program: the program to load into the PIO
    /// Returns Ok(()) if the program was loaded successfully, otherwise an error.
    fn try_load_program_at(
        &self,
        origin: usize,
        program: &[u16],
    ) -> Result<LoadedProgram, ProgramError> {
        // Relocate program
        let program = RelocatedProgram::new(program.iter(), origin);
        let mut used_mask = 0;
        for (i, instr) in program.enumerate() {
            // wrapping around the end of program memory is valid
            let addr = (i + origin) % 32;
            let mask = 1 << addr;
            if (self.instructions_used.get() | used_mask) & mask != 0 {
                return Err(ProgramError::AddrInUse(addr));
            }
            self.registers.instr_mem[addr]
                .instr_mem
                .modify(INSTR_MEMx::INSTR_MEM.val(instr as u32));
            used_mask |= mask;
        }
        // update the mask of used instructions slots
        self.instructions_used
            .set(self.instructions_used.get() | used_mask);
        Ok(LoadedProgram {
            used_memory: used_mask,
            origin,
        })
    }

    /// Clears all of a PIO instance's instruction memory.
    pub fn clear_instr_registers(&self) {
        for i in 0..NUMBER_INSTR_MEMORY_LOCATIONS {
            self.registers.instr_mem[i]
                .instr_mem
                .modify(INSTR_MEMx::INSTR_MEM::CLEAR);
        }
    }

    /// Initialize a new PIO with the same default configuration for all four state machines.
    pub fn init(&self) {
        let default_config: StateMachineConfiguration = StateMachineConfiguration::default();
        for state_machine in self.sms.iter() {
            state_machine.config(&default_config);
        }
        self.clear_instr_registers()
    }
}

mod examples {
    use super::{
        debug, Pio, RPGpio, RPGpioPin, Readable, SMNumber, SMx_EXECCTRL, SMx_INSTR, SMx_PINCTRL,
        StateMachineConfiguration, DBG_PADOUT, FDEBUG,
    };

    impl RPGpio {
        fn from_u32(value: u32) -> RPGpio {
            match value {
                0 => RPGpio::GPIO0,
                1 => RPGpio::GPIO1,
                2 => RPGpio::GPIO2,
                3 => RPGpio::GPIO3,
                4 => RPGpio::GPIO4,
                5 => RPGpio::GPIO5,
                6 => RPGpio::GPIO6,
                7 => RPGpio::GPIO7,
                8 => RPGpio::GPIO8,
                9 => RPGpio::GPIO9,
                10 => RPGpio::GPIO10,
                11 => RPGpio::GPIO11,
                12 => RPGpio::GPIO12,
                13 => RPGpio::GPIO13,
                14 => RPGpio::GPIO14,
                15 => RPGpio::GPIO15,
                16 => RPGpio::GPIO16,
                17 => RPGpio::GPIO17,
                18 => RPGpio::GPIO18,
                19 => RPGpio::GPIO19,
                20 => RPGpio::GPIO20,
                21 => RPGpio::GPIO21,
                22 => RPGpio::GPIO22,
                23 => RPGpio::GPIO23,
                24 => RPGpio::GPIO24,
                25 => RPGpio::GPIO25,
                26 => RPGpio::GPIO26,
                27 => RPGpio::GPIO27,
                28 => RPGpio::GPIO28,
                29 => RPGpio::GPIO29,
                _ => panic!(
                    "Unknown value for GPIO pin: {} (should be from 0 to 29)",
                    value
                ),
            }
        }
    }

    impl Pio {
        // # Examples
        /// Used for the examples in the pico explorer base main.rs file.

        pub fn blinking_hello_program_init(
            &self,
            sm_number: SMNumber,
            pin: u32,
            config: &StateMachineConfiguration,
        ) {
            let sm = &self.sms[sm_number as usize];
            sm.config(config);
            self.gpio_init(&RPGpioPin::new(RPGpio::from_u32(pin)));
            sm.set_enabled(false);
            sm.set_pins_dirs(pin, 1, true);
            sm.set_set_pins(pin, 1);
            sm.init();
            sm.set_enabled(true);
        }

        pub fn blink_program_init(
            &self,
            sm_number: SMNumber,
            pin: u32,
            config: &StateMachineConfiguration,
        ) {
            let sm = &self.sms[sm_number as usize];
            sm.config(config);
            self.gpio_init(&RPGpioPin::new(RPGpio::from_u32(pin)));
            sm.set_enabled(false);
            sm.set_pins_dirs(pin, 1, true);
            sm.set_set_pins(pin, 1);
            sm.init();
            sm.set_enabled(true);
        }

        pub fn sideset_program_init(
            &self,
            sm_number: SMNumber,
            pin: u32,
            config: &StateMachineConfiguration,
        ) {
            let sm = &self.sms[sm_number as usize];
            sm.config(config);
            self.gpio_init(&RPGpioPin::new(RPGpio::from_u32(pin)));
            self.gpio_init(&RPGpioPin::new(RPGpio::GPIO7));
            sm.set_enabled(false);
            sm.set_pins_dirs(pin, 1, true);
            sm.set_pins_dirs(7, 1, true);
            sm.set_set_pins(pin, 1);
            sm.set_side_set_pins(7, 1, false, true);
            sm.init();
            sm.set_enabled(true);
        }

        pub fn hello_program_init(
            &self,
            sm_number: SMNumber,
            pin1: u32,
            pin2: u32,
            config: &StateMachineConfiguration,
        ) {
            let sm = &self.sms[sm_number as usize];
            // This is used to turn on specifically GPIOs 6 and 7 - LSB is for GPIO 0, next bit is for GPIO 1 etc.
            let turn_on_gpio_6_7 = 0b11000000;
            sm.config(config);
            self.gpio_init(&RPGpioPin::new(RPGpio::from_u32(pin1)));
            self.gpio_init(&RPGpioPin::new(RPGpio::from_u32(pin2)));
            sm.set_enabled(false);
            sm.set_pins_dirs(pin1, 1, true);
            sm.set_pins_dirs(pin2, 1, true);
            sm.init();
            sm.set_enabled(true);
            sm.push_blocking(turn_on_gpio_6_7).ok();
            sm.push_blocking(0).ok();
        }

        pub fn pwm_program_init(
            &self,
            sm_number: SMNumber,
            pin: u32,
            pwm_period: u32,
            config: &StateMachineConfiguration,
        ) {
            let sm = &self.sms[sm_number as usize];
            // "pull" command created by pioasm
            let pull_command = 0x8080_u16;
            // "out isr, 32" command created by pioasm
            let out_isr_32_command = 0x60c0_u16;
            sm.config(config);
            self.gpio_init(&RPGpioPin::new(RPGpio::from_u32(pin)));
            sm.set_enabled(false);
            sm.set_pins_dirs(pin, 1, true);
            sm.set_side_set_pins(pin, 1, false, true);
            sm.init();
            sm.push_blocking(pwm_period).ok();
            sm.exec(pull_command);
            sm.exec(out_isr_32_command);
            sm.set_enabled(true);
        }

        // # Debugging
        /// Returns current instruction running on the state machine.
        pub fn read_instr(&self, sm_number: SMNumber) -> u32 {
            self.registers.sm[sm_number as usize]
                .instr
                .read(SMx_INSTR::INSTR)
        }

        pub fn read_sideset_reg(&self, sm_number: SMNumber) {
            debug!(
                "{}",
                self.registers.sm[sm_number as usize]
                    .pinctrl
                    .read(SMx_PINCTRL::SIDESET_COUNT)
            );
            debug!(
                "{}",
                self.registers.sm[sm_number as usize]
                    .execctrl
                    .read(SMx_EXECCTRL::SIDE_EN)
            );
            debug!(
                "{}",
                self.registers.sm[sm_number as usize]
                    .execctrl
                    .read(SMx_EXECCTRL::SIDE_PINDIR)
            );
            debug!(
                "{}",
                self.registers.sm[sm_number as usize]
                    .pinctrl
                    .read(SMx_PINCTRL::SIDESET_BASE)
            );
        }

        pub fn read_dbg_padout(&self) -> u32 {
            self.registers.dbg_padout.read(DBG_PADOUT::DBG_PADOUT)
        }

        pub fn read_fdebug(&self, tx: bool, stall: bool) -> u32 {
            if tx {
                if stall {
                    self.registers.fdebug.read(FDEBUG::TXSTALL)
                } else {
                    self.registers.fdebug.read(FDEBUG::TXOVER)
                }
            } else if stall {
                self.registers.fdebug.read(FDEBUG::RXSTALL)
            } else {
                self.registers.fdebug.read(FDEBUG::RXUNDER)
            }
        }
    }
}