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
|
//! DMA driver for MCXA276.
//!
//! This module provides a typed channel abstraction over the EDMA_0_TCD0 array
//! and helpers for configuring the channel MUX. The driver supports both
//! low-level TCD configuration and higher-level async transfer APIs.
//!
//! # Architecture
//!
//! The MCXA276 has 8 DMA channels (0-7), each with its own interrupt vector.
//! Each channel has a Transfer Control Descriptor (TCD) that defines the
//! transfer parameters.
//!
//! # Choosing the Right API
//!
//! This module provides several API levels to match different use cases:
//!
//! ## High-Level Async API (Recommended for Most Users)
//!
//! Use the async methods when you want simple, safe DMA transfers:
//!
//! | Method | Description |
//! |--------|-------------|
//! | [`DmaChannel::mem_to_mem()`] | Memory-to-memory copy |
//! | [`DmaChannel::memset()`] | Fill memory with a pattern |
//! | [`DmaChannel::write()`] | Memory-to-peripheral (TX) |
//! | [`DmaChannel::read()`] | Peripheral-to-memory (RX) |
//!
//! These return a [`Transfer`] future that can be `.await`ed:
//!
//! ```no_run
//! # use embassy_mcxa::dma::{DmaChannel, TransferOptions};
//! # let dma_ch = DmaChannel::new(p.DMA_CH0);
//! # let src = [0u32; 4];
//! # let mut dst = [0u32; 4];
//! // Simple memory-to-memory transfer
//! unsafe {
//! dma_ch.mem_to_mem(&src, &mut dst, TransferOptions::default()).await;
//! }
//! ```
//!
//! ## Setup Methods (For Peripheral Drivers)
//!
//! Use setup methods when you need manual lifecycle control:
//!
//! | Method | Description |
//! |--------|-------------|
//! | [`DmaChannel::setup_write()`] | Configure TX without starting |
//! | [`DmaChannel::setup_read()`] | Configure RX without starting |
//!
//! These configure the TCD but don't start the transfer. You control:
//! 1. When to call [`DmaChannel::enable_request()`]
//! 2. How to detect completion (polling or interrupts)
//! 3. When to clean up with [`DmaChannel::clear_done()`]
//!
//! ## Circular/Ring Buffer API (For Continuous Reception)
//!
//! Use [`DmaChannel::setup_circular_read()`] for continuous data reception:
//!
//! ```no_run
//! # use embassy_mcxa::dma::DmaChannel;
//! # let dma_ch = DmaChannel::new(p.DMA_CH0);
//! # let uart_rx_addr = 0x4000_0000 as *const u8;
//! static mut RX_BUF: [u8; 64] = [0; 64];
//!
//! let ring_buf = unsafe {
//! dma_ch.setup_circular_read(uart_rx_addr, &mut RX_BUF)
//! };
//!
//! // Read data as it arrives
//! let mut buf = [0u8; 16];
//! let n = ring_buf.read(&mut buf).await.unwrap();
//! ```
//!
//! ## Scatter-Gather Builder (For Chained Transfers)
//!
//! Use [`ScatterGatherBuilder`] for complex multi-segment transfers:
//!
//! ```no_run
//! # use embassy_mcxa::dma::{DmaChannel, ScatterGatherBuilder};
//! # let dma_ch = DmaChannel::new(p.DMA_CH0);
//! let mut builder = ScatterGatherBuilder::<u32>::new();
//! builder.add_transfer(&src1, &mut dst1);
//! builder.add_transfer(&src2, &mut dst2);
//!
//! let transfer = unsafe { builder.build(&dma_ch).unwrap() };
//! transfer.await;
//! ```
//!
//! ## Direct TCD Access (For Advanced Use Cases)
//!
//! For full control, use the channel's `tcd()` method to access TCD registers directly.
//! See the `dma_*` examples for patterns.
//!
//! # Example
//!
//! ```no_run
//! use embassy_mcxa::dma::{DmaChannel, TransferOptions, Direction};
//!
//! let dma_ch = DmaChannel::new(p.DMA_CH0);
//! // Configure and trigger a transfer...
//! ```
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicUsize, Ordering, fence};
use core::task::{Context, Poll};
use embassy_hal_internal::PeripheralType;
use embassy_sync::waitqueue::AtomicWaker;
use crate::clocks::Gate;
use crate::pac;
use crate::pac::Interrupt;
use crate::peripherals::DMA0;
/// Initialize DMA controller (clock enabled, reset released, controller configured).
///
/// This function is intended to be called ONCE during HAL initialization (`hal::init()`).
///
/// The function enables the DMA0 clock, releases reset, and configures the controller
/// for normal operation with round-robin arbitration.
pub(crate) fn init() {
unsafe {
// Enable DMA0 clock and release reset
DMA0::enable_clock();
DMA0::release_reset();
// Configure DMA controller
let dma = &(*pac::Dma0::ptr());
dma.mp_csr().modify(|_, w| {
w.edbg()
.enable()
.erca()
.enable()
.halt()
.normal_operation()
.gclc()
.available()
.gmrc()
.available()
});
}
}
// ============================================================================
// Phase 1: Foundation Types (Embassy-aligned)
// ============================================================================
/// DMA transfer direction.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Direction {
/// Transfer from memory to memory.
MemoryToMemory,
/// Transfer from memory to a peripheral register.
MemoryToPeripheral,
/// Transfer from a peripheral register to memory.
PeripheralToMemory,
}
/// DMA transfer priority.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Priority {
/// Low priority (channel priority 7).
Low,
/// Medium priority (channel priority 4).
Medium,
/// High priority (channel priority 1).
#[default]
High,
/// Highest priority (channel priority 0).
Highest,
}
impl Priority {
/// Convert to hardware priority value (0 = highest, 7 = lowest).
pub fn to_hw_priority(self) -> u8 {
match self {
Priority::Low => 7,
Priority::Medium => 4,
Priority::High => 1,
Priority::Highest => 0,
}
}
}
/// DMA transfer data width.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WordSize {
/// 8-bit (1 byte) transfers.
OneByte,
/// 16-bit (2 byte) transfers.
TwoBytes,
/// 32-bit (4 byte) transfers.
#[default]
FourBytes,
}
impl WordSize {
/// Size in bytes.
pub const fn bytes(self) -> usize {
match self {
WordSize::OneByte => 1,
WordSize::TwoBytes => 2,
WordSize::FourBytes => 4,
}
}
/// Convert to hardware SSIZE/DSIZE field value.
pub const fn to_hw_size(self) -> u8 {
match self {
WordSize::OneByte => 0,
WordSize::TwoBytes => 1,
WordSize::FourBytes => 2,
}
}
/// Create from byte width (1, 2, or 4).
pub const fn from_bytes(bytes: u8) -> Option<Self> {
match bytes {
1 => Some(WordSize::OneByte),
2 => Some(WordSize::TwoBytes),
4 => Some(WordSize::FourBytes),
_ => None,
}
}
}
/// Trait for types that can be transferred via DMA.
///
/// This provides compile-time type safety for DMA transfers.
pub trait Word: Copy + 'static {
/// The word size for this type.
fn size() -> WordSize;
}
impl Word for u8 {
fn size() -> WordSize {
WordSize::OneByte
}
}
impl Word for u16 {
fn size() -> WordSize {
WordSize::TwoBytes
}
}
impl Word for u32 {
fn size() -> WordSize {
WordSize::FourBytes
}
}
/// DMA transfer options.
///
/// This struct configures various aspects of a DMA transfer.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub struct TransferOptions {
/// Transfer priority.
pub priority: Priority,
/// Enable circular (continuous) mode.
///
/// When enabled, the transfer repeats automatically after completing.
pub circular: bool,
/// Enable interrupt on half transfer complete.
pub half_transfer_interrupt: bool,
/// Enable interrupt on transfer complete.
pub complete_transfer_interrupt: bool,
}
impl Default for TransferOptions {
fn default() -> Self {
Self {
priority: Priority::High,
circular: false,
half_transfer_interrupt: false,
complete_transfer_interrupt: true,
}
}
}
/// DMA error types.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
/// The DMA controller reported a bus error.
BusError,
/// The transfer was aborted.
Aborted,
/// Configuration error (e.g., invalid parameters).
Configuration,
/// Buffer overrun (for ring buffers).
Overrun,
}
/// Whether to enable the major loop completion interrupt.
///
/// This enum provides better readability than a boolean parameter
/// for functions that configure DMA interrupt behavior.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EnableInterrupt {
/// Enable the interrupt on major loop completion.
Yes,
/// Do not enable the interrupt.
No,
}
// ============================================================================
// DMA Constants
// ============================================================================
/// Maximum bytes per DMA transfer (eDMA4 CITER/BITER are 15-bit fields).
///
/// This is a hardware limitation of the eDMA4 controller. Transfers larger
/// than this must be split into multiple DMA operations.
pub const DMA_MAX_TRANSFER_SIZE: usize = 0x7FFF;
// ============================================================================
// DMA Request Source Types (Type-Safe API)
// ============================================================================
/// Trait for type-safe DMA request sources.
///
/// Each peripheral that can trigger DMA requests implements this trait
/// with marker types that encode the correct request source number at
/// compile time. This prevents using the wrong request source for a
/// peripheral.
///
/// # Example
///
/// ```ignore
/// // The LPUART2 RX request source is automatically derived from the type:
/// channel.set_request_source::<Lpuart2RxRequest>();
/// ```
///
/// This trait is sealed and cannot be implemented outside this crate.
#[allow(private_bounds)]
pub trait DmaRequest: sealed::SealedDmaRequest {
/// The hardware request source number for the DMA mux.
const REQUEST_NUMBER: u8;
}
/// Macro to define a DMA request type.
///
/// Creates a zero-sized marker type that implements `DmaRequest` with
/// the specified request number.
macro_rules! define_dma_request {
($(#[$meta:meta])* $name:ident = $num:expr) => {
$(#[$meta])*
#[derive(Debug, Copy, Clone)]
pub struct $name;
impl sealed::SealedDmaRequest for $name {}
impl DmaRequest for $name {
const REQUEST_NUMBER: u8 = $num;
}
};
}
// LPUART DMA request sources (from MCXA276 reference manual Table 4-8)
define_dma_request!(
/// DMA request source for LPUART0 RX.
Lpuart0RxRequest = 21
);
define_dma_request!(
/// DMA request source for LPUART0 TX.
Lpuart0TxRequest = 22
);
define_dma_request!(
/// DMA request source for LPUART1 RX.
Lpuart1RxRequest = 23
);
define_dma_request!(
/// DMA request source for LPUART1 TX.
Lpuart1TxRequest = 24
);
define_dma_request!(
/// DMA request source for LPUART2 RX.
Lpuart2RxRequest = 25
);
define_dma_request!(
/// DMA request source for LPUART2 TX.
Lpuart2TxRequest = 26
);
define_dma_request!(
/// DMA request source for LPUART3 RX.
Lpuart3RxRequest = 27
);
define_dma_request!(
/// DMA request source for LPUART3 TX.
Lpuart3TxRequest = 28
);
define_dma_request!(
/// DMA request source for LPUART4 RX.
Lpuart4RxRequest = 29
);
define_dma_request!(
/// DMA request source for LPUART4 TX.
Lpuart4TxRequest = 30
);
define_dma_request!(
/// DMA request source for LPUART5 RX.
Lpuart5RxRequest = 31
);
define_dma_request!(
/// DMA request source for LPUART5 TX.
Lpuart5TxRequest = 32
);
// ============================================================================
// Channel Trait (Sealed Pattern)
// ============================================================================
mod sealed {
use crate::pac::Interrupt;
/// Sealed trait for DMA channels.
pub trait SealedChannel {
/// Zero-based channel index into the TCD array.
fn index(&self) -> usize;
/// Interrupt vector for this channel.
fn interrupt(&self) -> Interrupt;
}
/// Sealed trait for DMA request sources.
pub trait SealedDmaRequest {}
}
/// Marker trait implemented by HAL peripheral tokens that map to a DMA0
/// channel backed by one EDMA_0_TCD0 TCD slot.
///
/// This trait is sealed and cannot be implemented outside this crate.
#[allow(private_bounds)]
pub trait Channel: sealed::SealedChannel + PeripheralType + Into<AnyChannel> + 'static {
/// Zero-based channel index into the TCD array.
const INDEX: usize;
/// Interrupt vector for this channel.
const INTERRUPT: Interrupt;
}
/// Type-erased DMA channel.
///
/// This allows storing DMA channels in a uniform way regardless of their
/// concrete type, useful for async transfer futures and runtime channel selection.
#[derive(Debug, Clone, Copy)]
pub struct AnyChannel {
index: usize,
interrupt: Interrupt,
}
impl AnyChannel {
/// Get the channel index.
#[inline]
pub const fn index(&self) -> usize {
self.index
}
/// Get the channel interrupt.
#[inline]
pub const fn interrupt(&self) -> Interrupt {
self.interrupt
}
/// Get a reference to the TCD register block for this channel.
///
/// This steals the eDMA pointer internally since MCXA276 has only one eDMA instance.
#[inline]
fn tcd(&self) -> &'static pac::edma_0_tcd0::Tcd {
// Safety: MCXA276 has a single eDMA instance, and we're only accessing
// the TCD for this specific channel
let edma = unsafe { &*pac::Edma0Tcd0::ptr() };
edma.tcd(self.index)
}
/// Check if the channel's DONE flag is set.
pub fn is_done(&self) -> bool {
self.tcd().ch_csr().read().done().bit_is_set()
}
/// Get the waker for this channel.
pub fn waker(&self) -> &'static AtomicWaker {
&STATES[self.index].waker
}
}
impl sealed::SealedChannel for AnyChannel {
fn index(&self) -> usize {
self.index
}
fn interrupt(&self) -> Interrupt {
self.interrupt
}
}
/// Macro to implement Channel trait for a peripheral.
macro_rules! impl_channel {
($peri:ident, $index:expr, $irq:ident) => {
impl sealed::SealedChannel for crate::peripherals::$peri {
fn index(&self) -> usize {
$index
}
fn interrupt(&self) -> Interrupt {
Interrupt::$irq
}
}
impl Channel for crate::peripherals::$peri {
const INDEX: usize = $index;
const INTERRUPT: Interrupt = Interrupt::$irq;
}
impl From<crate::peripherals::$peri> for AnyChannel {
fn from(_: crate::peripherals::$peri) -> Self {
AnyChannel {
index: $index,
interrupt: Interrupt::$irq,
}
}
}
};
}
impl_channel!(DMA_CH0, 0, DMA_CH0);
impl_channel!(DMA_CH1, 1, DMA_CH1);
impl_channel!(DMA_CH2, 2, DMA_CH2);
impl_channel!(DMA_CH3, 3, DMA_CH3);
impl_channel!(DMA_CH4, 4, DMA_CH4);
impl_channel!(DMA_CH5, 5, DMA_CH5);
impl_channel!(DMA_CH6, 6, DMA_CH6);
impl_channel!(DMA_CH7, 7, DMA_CH7);
/// Strongly-typed handle to a DMA0 channel.
///
/// The lifetime of this value is tied to the unique peripheral token
/// supplied by `embassy_hal_internal::peripherals!`, so safe code cannot
/// create two `DmaChannel` instances for the same hardware channel.
pub struct DmaChannel<C: Channel> {
_ch: core::marker::PhantomData<C>,
}
// ============================================================================
// DMA Transfer Methods - API Overview
// ============================================================================
//
// The DMA API provides two categories of methods for configuring transfers:
//
// ## 1. Async Methods (Return `Transfer` Future)
//
// These methods return a [`Transfer`] Future that must be `.await`ed:
//
// - [`write()`](DmaChannel::write) - Memory-to-peripheral using default eDMA TCD block
// - [`read()`](DmaChannel::read) - Peripheral-to-memory using default eDMA TCD block
// - [`write_to_peripheral()`](DmaChannel::write_to_peripheral) - Memory-to-peripheral with custom eDMA TCD block
// - [`read_from_peripheral()`](DmaChannel::read_from_peripheral) - Peripheral-to-memory with custom eDMA TCD block
// - [`mem_to_mem()`](DmaChannel::mem_to_mem) - Memory-to-memory using default eDMA TCD block
//
// The `Transfer` manages the DMA lifecycle automatically:
// - Enables channel request
// - Waits for completion via async/await
// - Cleans up on completion
//
// **Important:** `Transfer::Drop` aborts the transfer if dropped before completion.
// This means you MUST `.await` the Transfer or it will be aborted when it goes out of scope.
//
// **Use case:** When you want to use async/await and let the Transfer handle lifecycle management.
//
// ## 2. Setup Methods (Configure TCD Only)
//
// These methods configure the TCD but do NOT return a `Transfer`:
//
// - [`setup_write()`](DmaChannel::setup_write) - Memory-to-peripheral using default eDMA TCD block
// - [`setup_read()`](DmaChannel::setup_read) - Peripheral-to-memory using default eDMA TCD block
// - [`setup_write_to_peripheral()`](DmaChannel::setup_write_to_peripheral) - Memory-to-peripheral with custom eDMA TCD block
// - [`setup_read_from_peripheral()`](DmaChannel::setup_read_from_peripheral) - Peripheral-to-memory with custom eDMA TCD block
//
// The caller is responsible for the complete DMA lifecycle:
// 1. Call [`enable_request()`](DmaChannel::enable_request) to start the transfer
// 2. Poll [`is_done()`](DmaChannel::is_done) or use interrupts to detect completion
// 3. Call [`disable_request()`](DmaChannel::disable_request), [`clear_done()`](DmaChannel::clear_done),
// [`clear_interrupt()`](DmaChannel::clear_interrupt) for cleanup
//
// **Use case:** Peripheral drivers (like LPUART) that need fine-grained control over
// DMA setup before starting a `Transfer`.
//
// ============================================================================
impl<C: Channel> DmaChannel<C> {
/// Wrap a DMA channel token (takes ownership of the Peri wrapper).
///
/// Note: DMA is initialized during `hal::init()` via `dma::init()`.
#[inline]
pub fn new(_ch: embassy_hal_internal::Peri<'_, C>) -> Self {
unsafe {
cortex_m::peripheral::NVIC::unmask(C::INTERRUPT);
}
Self {
_ch: core::marker::PhantomData,
}
}
/// Channel index in the EDMA_0_TCD0 array.
#[inline]
pub const fn index(&self) -> usize {
C::INDEX
}
/// Convert this typed channel into a type-erased `AnyChannel`.
#[inline]
pub fn into_any(self) -> AnyChannel {
AnyChannel {
index: C::INDEX,
interrupt: C::INTERRUPT,
}
}
/// Get a reference to the type-erased channel info.
#[inline]
pub fn as_any(&self) -> AnyChannel {
AnyChannel {
index: C::INDEX,
interrupt: C::INTERRUPT,
}
}
/// Return a reference to the underlying TCD register block.
///
/// This steals the eDMA pointer internally since MCXA276 has only one eDMA instance.
///
/// # Note
///
/// This is exposed for advanced use cases that need direct TCD access.
/// For most use cases, prefer the higher-level transfer methods.
#[inline]
pub fn tcd(&self) -> &'static pac::edma_0_tcd0::Tcd {
// Safety: MCXA276 has a single eDMA instance
let edma = unsafe { &*pac::Edma0Tcd0::ptr() };
edma.tcd(C::INDEX)
}
fn clear_tcd(t: &'static pac::edma_0_tcd0::Tcd) {
// Full TCD reset following NXP SDK pattern (EDMA_TcdResetExt).
// Reset ALL TCD registers to 0 to clear any stale configuration from
// previous transfers. This is critical when reusing a channel.
t.tcd_saddr().write(|w| unsafe { w.saddr().bits(0) });
t.tcd_soff().write(|w| unsafe { w.soff().bits(0) });
t.tcd_attr().write(|w| unsafe { w.bits(0) });
t.tcd_nbytes_mloffno().write(|w| unsafe { w.nbytes().bits(0) });
t.tcd_slast_sda().write(|w| unsafe { w.slast_sda().bits(0) });
t.tcd_daddr().write(|w| unsafe { w.daddr().bits(0) });
t.tcd_doff().write(|w| unsafe { w.doff().bits(0) });
t.tcd_citer_elinkno().write(|w| unsafe { w.bits(0) });
t.tcd_dlast_sga().write(|w| unsafe { w.dlast_sga().bits(0) });
t.tcd_csr().write(|w| unsafe { w.bits(0) }); // Clear CSR completely
t.tcd_biter_elinkno().write(|w| unsafe { w.bits(0) });
}
#[inline]
fn set_major_loop_ct_elinkno(t: &'static pac::edma_0_tcd0::Tcd, count: u16) {
t.tcd_biter_elinkno().write(|w| unsafe { w.biter().bits(count) });
t.tcd_citer_elinkno().write(|w| unsafe { w.citer().bits(count) });
}
#[inline]
fn set_minor_loop_ct_no_offsets(t: &'static pac::edma_0_tcd0::Tcd, count: u32) {
t.tcd_nbytes_mloffno().write(|w| unsafe { w.nbytes().bits(count) });
}
#[inline]
fn set_no_final_adjustments(t: &'static pac::edma_0_tcd0::Tcd) {
// No source/dest adjustment after major loop
t.tcd_slast_sda().write(|w| unsafe { w.slast_sda().bits(0) });
t.tcd_dlast_sga().write(|w| unsafe { w.dlast_sga().bits(0) });
}
#[inline]
fn set_source_ptr<T>(t: &'static pac::edma_0_tcd0::Tcd, p: *const T) {
t.tcd_saddr().write(|w| unsafe { w.saddr().bits(p as u32) });
}
#[inline]
fn set_source_increment(t: &'static pac::edma_0_tcd0::Tcd, sz: WordSize) {
t.tcd_soff().write(|w| unsafe { w.soff().bits(sz.bytes() as u16) });
}
#[inline]
fn set_source_fixed(t: &'static pac::edma_0_tcd0::Tcd) {
t.tcd_soff().write(|w| unsafe { w.soff().bits(0) });
}
#[inline]
fn set_dest_ptr<T>(t: &'static pac::edma_0_tcd0::Tcd, p: *mut T) {
t.tcd_daddr().write(|w| unsafe { w.daddr().bits(p as u32) });
}
#[inline]
fn set_dest_increment(t: &'static pac::edma_0_tcd0::Tcd, sz: WordSize) {
t.tcd_doff().write(|w| unsafe { w.doff().bits(sz.bytes() as u16) });
}
#[inline]
fn set_dest_fixed(t: &'static pac::edma_0_tcd0::Tcd) {
t.tcd_doff().write(|w| unsafe { w.doff().bits(0) });
}
#[inline]
fn set_even_transfer_size(t: &'static pac::edma_0_tcd0::Tcd, sz: WordSize) {
let hw_size = sz.to_hw_size();
t.tcd_attr()
.write(|w| unsafe { w.ssize().bits(hw_size).dsize().bits(hw_size) });
}
#[inline]
fn reset_channel_state(t: &'static pac::edma_0_tcd0::Tcd) {
// CSR: Resets to all zeroes (disabled), "done" is cleared by writing 1
t.ch_csr().write(|w| w.done().clear_bit_by_one());
// ES: Resets to all zeroes (disabled), "err" is cleared by writing 1
t.ch_es().write(|w| w.err().clear_bit_by_one());
// INT: Resets to all zeroes (disabled), "int" is cleared by writing 1
t.ch_int().write(|w| w.int().clear_bit_by_one());
}
/// Start an async transfer.
///
/// The channel must already be configured. This enables the channel
/// request and returns a `Transfer` future that resolves when the
/// DMA transfer completes.
///
/// # Safety
///
/// The caller must ensure the DMA channel has been properly configured
/// and that source/destination buffers remain valid for the duration
/// of the transfer.
pub unsafe fn start_transfer(&self) -> Transfer<'_> {
// Clear any previous DONE/INT flags
let t = self.tcd();
t.ch_csr().modify(|_, w| w.done().clear_bit_by_one());
t.ch_int().write(|w| w.int().clear_bit_by_one());
// Enable the channel request
t.ch_csr().modify(|_, w| w.erq().enable());
Transfer::new(self.as_any())
}
// ========================================================================
// Type-Safe Transfer Methods (Embassy-style API)
// ========================================================================
/// Perform a memory-to-memory DMA transfer (simplified API).
///
/// This is a type-safe wrapper that uses the `Word` trait to determine
/// the correct transfer width automatically. Uses the global eDMA TCD
/// register accessor internally.
///
/// # Arguments
///
/// * `src` - Source buffer
/// * `dst` - Destination buffer (must be at least as large as src)
/// * `options` - Transfer configuration options
///
/// # Safety
///
/// The source and destination buffers must remain valid for the
/// duration of the transfer.
pub fn mem_to_mem<W: Word>(
&self,
src: &[W],
dst: &mut [W],
options: TransferOptions,
) -> Result<Transfer<'_>, Error> {
let mut invalid = false;
invalid |= src.is_empty();
invalid |= src.len() > dst.len();
invalid |= src.len() > 0x7fff;
if invalid {
return Err(Error::Configuration);
}
let size = W::size();
let byte_count = (src.len() * size.bytes()) as u32;
let t = self.tcd();
// Reset channel state - clear DONE, disable requests, clear errors
Self::reset_channel_state(t);
// Memory barrier to ensure channel state is fully reset before touching TCD
cortex_m::asm::dsb();
Self::clear_tcd(t);
// Memory barrier after TCD reset
cortex_m::asm::dsb();
// Note: Priority is managed by round-robin arbitration (set in init())
// Per-channel priority can be configured via ch_pri() if needed
// Now configure the new transfer
// Source address and increment
Self::set_source_ptr(t, src.as_ptr());
Self::set_source_increment(t, size);
// Destination address and increment
Self::set_dest_ptr(t, dst.as_mut_ptr());
Self::set_dest_increment(t, size);
// Transfer attributes (size)
Self::set_even_transfer_size(t, size);
// Minor loop: transfer all bytes in one minor loop
Self::set_minor_loop_ct_no_offsets(t, byte_count);
// No source/dest adjustment after major loop
Self::set_no_final_adjustments(t);
// Major loop count = 1 (single major loop)
// Write BITER first, then CITER (CITER must match BITER at start)
Self::set_major_loop_ct_elinkno(t, 1);
// Memory barrier before setting START
cortex_m::asm::dsb();
// Control/status: interrupt on major complete, start
// Write this last after all other TCD registers are configured
let int_major = options.complete_transfer_interrupt;
t.tcd_csr().write(|w| {
w.intmajor()
.bit(int_major)
.inthalf()
.bit(options.half_transfer_interrupt)
.dreq()
.set_bit() // Auto-disable request after major loop
.start()
.set_bit() // Start the channel
});
Ok(Transfer::new(self.as_any()))
}
/// Fill a memory buffer with a pattern value (memset).
///
/// This performs a DMA transfer where the source address remains fixed
/// (pattern value) while the destination address increments through the buffer.
/// It's useful for quickly filling large memory regions with a constant value.
///
/// # Arguments
///
/// * `pattern` - Reference to the pattern value (will be read repeatedly)
/// * `dst` - Destination buffer to fill
/// * `options` - Transfer configuration options
///
/// # Example
///
/// ```no_run
/// use embassy_mcxa::dma::{DmaChannel, TransferOptions};
///
/// let dma_ch = DmaChannel::new(p.DMA_CH0);
/// let pattern: u32 = 0xDEADBEEF;
/// let mut buffer = [0u32; 256];
///
/// unsafe {
/// dma_ch.memset(&pattern, &mut buffer, TransferOptions::default()).await;
/// }
/// // buffer is now filled with 0xDEADBEEF
/// ```
///
pub fn memset<W: Word>(&self, pattern: &W, dst: &mut [W], options: TransferOptions) -> Transfer<'_> {
assert!(!dst.is_empty());
assert!(dst.len() <= 0x7fff);
let size = W::size();
let byte_size = size.bytes();
// Total bytes to transfer - all in one minor loop for software-triggered transfers
let total_bytes = (dst.len() * byte_size) as u32;
let t = self.tcd();
// Reset channel state - clear DONE, disable requests, clear errors
Self::reset_channel_state(t);
// Memory barrier to ensure channel state is fully reset before touching TCD
cortex_m::asm::dsb();
Self::clear_tcd(t);
// Memory barrier after TCD reset
cortex_m::asm::dsb();
// Now configure the new transfer
//
// For software-triggered memset, we use a SINGLE minor loop that transfers
// all bytes at once. The source address stays fixed (SOFF=0) while the
// destination increments (DOFF=byte_size). The eDMA will read from the
// same source address for each destination word.
//
// This is necessary because the START bit only triggers ONE minor loop
// iteration. Using CITER>1 with software trigger would require multiple
// START triggers.
// Source: pattern address, fixed (soff=0)
Self::set_source_ptr(t, pattern);
Self::set_source_fixed(t);
// Destination: memory buffer, incrementing by word size
Self::set_dest_ptr(t, dst.as_mut_ptr());
Self::set_dest_increment(t, size);
// Transfer attributes - source and dest are same word size
Self::set_even_transfer_size(t, size);
// Minor loop: transfer ALL bytes in one minor loop (like mem_to_mem)
// This allows the entire transfer to complete with a single START trigger
Self::set_minor_loop_ct_no_offsets(t, total_bytes);
// No address adjustment after major loop
Self::set_no_final_adjustments(t);
// Major loop count = 1 (single major loop, all data in minor loop)
// Write BITER first, then CITER (CITER must match BITER at start)
Self::set_major_loop_ct_elinkno(t, 1);
// Memory barrier before setting START
cortex_m::asm::dsb();
// Control/status: interrupt on major complete, start immediately
// Write this last after all other TCD registers are configured
let int_major = options.complete_transfer_interrupt;
t.tcd_csr().write(|w| {
w.intmajor()
.bit(int_major)
.inthalf()
.bit(options.half_transfer_interrupt)
.dreq()
.set_bit() // Auto-disable request after major loop
.start()
.set_bit() // Start the channel
});
Transfer::new(self.as_any())
}
/// Write data from memory to a peripheral register.
///
/// The destination address remains fixed (peripheral register) while
/// the source address increments through the buffer.
///
/// # Arguments
///
/// * `buf` - Source buffer to write from
/// * `peri_addr` - Peripheral register address
/// * `options` - Transfer configuration options
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for writes.
pub unsafe fn write<W: Word>(&self, buf: &[W], peri_addr: *mut W, options: TransferOptions) -> Transfer<'_> {
self.write_to_peripheral(buf, peri_addr, options)
}
/// Configure a memory-to-peripheral DMA transfer without starting it.
///
/// This is a convenience wrapper around [`setup_write_to_peripheral()`](Self::setup_write_to_peripheral)
/// that uses the default eDMA TCD register block.
///
/// This method configures the TCD but does NOT return a `Transfer`. The caller
/// is responsible for the complete DMA lifecycle:
/// 1. Call [`enable_request()`](Self::enable_request) to start the transfer
/// 2. Poll [`is_done()`](Self::is_done) or use interrupts to detect completion
/// 3. Call [`disable_request()`](Self::disable_request), [`clear_done()`](Self::clear_done),
/// [`clear_interrupt()`](Self::clear_interrupt) for cleanup
///
/// # Example
///
/// ```no_run
/// # use embassy_mcxa::dma::DmaChannel;
/// # let dma_ch = DmaChannel::new(p.DMA_CH0);
/// # let uart_tx_addr = 0x4000_0000 as *mut u8;
/// let data = [0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello"
///
/// unsafe {
/// // Configure the transfer
/// dma_ch.setup_write(&data, uart_tx_addr, EnableInterrupt::Yes);
///
/// // Start when peripheral is ready
/// dma_ch.enable_request();
///
/// // Wait for completion (or use interrupt)
/// while !dma_ch.is_done() {}
///
/// // Clean up
/// dma_ch.clear_done();
/// dma_ch.clear_interrupt();
/// }
/// ```
///
/// # Arguments
///
/// * `buf` - Source buffer to write from
/// * `peri_addr` - Peripheral register address
/// * `enable_interrupt` - Whether to enable interrupt on completion
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for writes.
pub unsafe fn setup_write<W: Word>(&self, buf: &[W], peri_addr: *mut W, enable_interrupt: EnableInterrupt) {
self.setup_write_to_peripheral(buf, peri_addr, enable_interrupt)
}
/// Write data from memory to a peripheral register.
///
/// The destination address remains fixed (peripheral register) while
/// the source address increments through the buffer.
///
/// # Arguments
///
/// * `buf` - Source buffer to write from
/// * `peri_addr` - Peripheral register address
/// * `options` - Transfer configuration options
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for writes.
pub unsafe fn write_to_peripheral<W: Word>(
&self,
buf: &[W],
peri_addr: *mut W,
options: TransferOptions,
) -> Transfer<'_> {
assert!(!buf.is_empty());
assert!(buf.len() <= 0x7fff);
let size = W::size();
let byte_size = size.bytes();
let t = self.tcd();
// Reset channel state
Self::reset_channel_state(t);
// Addresses
Self::set_source_ptr(t, buf.as_ptr());
Self::set_dest_ptr(t, peri_addr);
// Offsets: Source increments, Dest fixed
Self::set_source_increment(t, size);
Self::set_dest_fixed(t);
// Attributes: set size and explicitly disable modulo
Self::set_even_transfer_size(t, size);
// Minor loop: transfer one word per request (match old: only set nbytes)
Self::set_minor_loop_ct_no_offsets(t, byte_size as u32);
// No final adjustments
Self::set_no_final_adjustments(t);
// Major loop count = number of words
let count = buf.len() as u16;
Self::set_major_loop_ct_elinkno(t, count);
// CSR: interrupt on major loop complete and auto-clear ERQ
t.tcd_csr().write(|w| {
let w = if options.complete_transfer_interrupt {
w.intmajor().enable()
} else {
w.intmajor().disable()
};
w.inthalf()
.disable()
.dreq()
.erq_field_clear() // Disable request when done
.esg()
.normal_format()
.majorelink()
.disable()
.eeop()
.disable()
.esda()
.disable()
.bwc()
.no_stall()
});
// Ensure all TCD writes have completed before DMA engine reads them
cortex_m::asm::dsb();
Transfer::new(self.as_any())
}
/// Read data from a peripheral register to memory.
///
/// The source address remains fixed (peripheral register) while
/// the destination address increments through the buffer.
///
/// # Arguments
///
/// * `peri_addr` - Peripheral register address
/// * `buf` - Destination buffer to read into
/// * `options` - Transfer configuration options
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for reads.
pub unsafe fn read<W: Word>(&self, peri_addr: *const W, buf: &mut [W], options: TransferOptions) -> Transfer<'_> {
self.read_from_peripheral(peri_addr, buf, options)
}
/// Configure a peripheral-to-memory DMA transfer without starting it.
///
/// This is a convenience wrapper around [`setup_read_from_peripheral()`](Self::setup_read_from_peripheral)
/// that uses the default eDMA TCD register block.
///
/// This method configures the TCD but does NOT return a `Transfer`. The caller
/// is responsible for the complete DMA lifecycle:
/// 1. Call [`enable_request()`](Self::enable_request) to start the transfer
/// 2. Poll [`is_done()`](Self::is_done) or use interrupts to detect completion
/// 3. Call [`disable_request()`](Self::disable_request), [`clear_done()`](Self::clear_done),
/// [`clear_interrupt()`](Self::clear_interrupt) for cleanup
///
/// # Example
///
/// ```no_run
/// # use embassy_mcxa::dma::DmaChannel;
/// # let dma_ch = DmaChannel::new(p.DMA_CH0);
/// # let uart_rx_addr = 0x4000_0000 as *const u8;
/// let mut buf = [0u8; 32];
///
/// unsafe {
/// // Configure the transfer
/// dma_ch.setup_read(uart_rx_addr, &mut buf, EnableInterrupt::Yes);
///
/// // Start when peripheral is ready
/// dma_ch.enable_request();
///
/// // Wait for completion (or use interrupt)
/// while !dma_ch.is_done() {}
///
/// // Clean up
/// dma_ch.clear_done();
/// dma_ch.clear_interrupt();
/// }
/// // buf now contains received data
/// ```
///
/// # Arguments
///
/// * `peri_addr` - Peripheral register address
/// * `buf` - Destination buffer to read into
/// * `enable_interrupt` - Whether to enable interrupt on completion
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for reads.
pub unsafe fn setup_read<W: Word>(&self, peri_addr: *const W, buf: &mut [W], enable_interrupt: EnableInterrupt) {
self.setup_read_from_peripheral(peri_addr, buf, enable_interrupt)
}
/// Read data from a peripheral register to memory.
///
/// The source address remains fixed (peripheral register) while
/// the destination address increments through the buffer.
///
/// # Arguments
///
/// * `peri_addr` - Peripheral register address
/// * `buf` - Destination buffer to read into
/// * `options` - Transfer configuration options
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for reads.
pub unsafe fn read_from_peripheral<W: Word>(
&self,
peri_addr: *const W,
buf: &mut [W],
options: TransferOptions,
) -> Transfer<'_> {
assert!(!buf.is_empty());
assert!(buf.len() <= 0x7fff);
let size = W::size();
let byte_size = size.bytes();
let t = self.tcd();
// Reset channel control/error/interrupt state
Self::reset_channel_state(t);
// Source: peripheral register, fixed
Self::set_source_ptr(t, peri_addr);
Self::set_source_fixed(t);
// Destination: memory buffer, incrementing
Self::set_dest_ptr(t, buf.as_mut_ptr());
Self::set_dest_increment(t, size);
// Transfer attributes: set size and explicitly disable modulo
Self::set_even_transfer_size(t, size);
// Minor loop: transfer one word per request, no offsets
Self::set_minor_loop_ct_no_offsets(t, byte_size as u32);
// Major loop count = number of words
let count = buf.len() as u16;
Self::set_major_loop_ct_elinkno(t, count);
// No address adjustment after major loop
Self::set_no_final_adjustments(t);
// Control/status: interrupt on major complete, auto-clear ERQ when done
t.tcd_csr().write(|w| {
let w = if options.complete_transfer_interrupt {
w.intmajor().enable()
} else {
w.intmajor().disable()
};
let w = if options.half_transfer_interrupt {
w.inthalf().enable()
} else {
w.inthalf().disable()
};
w.dreq()
.erq_field_clear() // Disable request when done (important for peripheral DMA)
.esg()
.normal_format()
.majorelink()
.disable()
.eeop()
.disable()
.esda()
.disable()
.bwc()
.no_stall()
});
// Ensure all TCD writes have completed before DMA engine reads them
cortex_m::asm::dsb();
Transfer::new(self.as_any())
}
/// Configure a memory-to-peripheral DMA transfer without starting it.
///
/// This configures the TCD for a memory-to-peripheral transfer but does NOT
/// return a Transfer object. The caller is responsible for:
/// 1. Enabling the peripheral's DMA request
/// 2. Calling `enable_request()` to start the transfer
/// 3. Polling `is_done()` or using interrupts to detect completion
/// 4. Calling `disable_request()`, `clear_done()`, `clear_interrupt()` for cleanup
///
/// Use this when you need manual control over the DMA lifecycle (e.g., in
/// peripheral drivers that have their own completion polling).
///
/// # Arguments
///
/// * `buf` - Source buffer to write from
/// * `peri_addr` - Peripheral register address
/// * `enable_interrupt` - Whether to enable interrupt on completion
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for writes.
pub unsafe fn setup_write_to_peripheral<W: Word>(
&self,
buf: &[W],
peri_addr: *mut W,
enable_interrupt: EnableInterrupt,
) {
assert!(!buf.is_empty());
assert!(buf.len() <= 0x7fff);
let size = W::size();
let byte_size = size.bytes();
let t = self.tcd();
// Reset channel state
Self::reset_channel_state(t);
// Addresses
Self::set_source_ptr(t, buf.as_ptr());
Self::set_dest_ptr(t, peri_addr);
// Offsets: Source increments, Dest fixed
Self::set_source_increment(t, size);
Self::set_dest_fixed(t);
// Attributes: set size and explicitly disable modulo
Self::set_even_transfer_size(t, size);
// Minor loop: transfer one word per request
Self::set_minor_loop_ct_no_offsets(t, byte_size as u32);
// No final adjustments
Self::set_no_final_adjustments(t);
// Major loop count = number of words
let count = buf.len() as u16;
Self::set_major_loop_ct_elinkno(t, count);
// CSR: optional interrupt on major loop complete and auto-clear ERQ
t.tcd_csr().write(|w| {
let w = match enable_interrupt {
EnableInterrupt::Yes => w.intmajor().enable(),
EnableInterrupt::No => w.intmajor().disable(),
};
w.inthalf()
.disable()
.dreq()
.erq_field_clear()
.esg()
.normal_format()
.majorelink()
.disable()
.eeop()
.disable()
.esda()
.disable()
.bwc()
.no_stall()
});
// Ensure all TCD writes have completed before DMA engine reads them
cortex_m::asm::dsb();
}
/// Configure a peripheral-to-memory DMA transfer without starting it.
///
/// This configures the TCD for a peripheral-to-memory transfer but does NOT
/// return a Transfer object. The caller is responsible for:
/// 1. Enabling the peripheral's DMA request
/// 2. Calling `enable_request()` to start the transfer
/// 3. Polling `is_done()` or using interrupts to detect completion
/// 4. Calling `disable_request()`, `clear_done()`, `clear_interrupt()` for cleanup
///
/// Use this when you need manual control over the DMA lifecycle (e.g., in
/// peripheral drivers that have their own completion polling).
///
/// # Arguments
///
/// * `peri_addr` - Peripheral register address
/// * `buf` - Destination buffer to read into
/// * `enable_interrupt` - Whether to enable interrupt on completion
///
/// # Safety
///
/// - The buffer must remain valid for the duration of the transfer.
/// - The peripheral address must be valid for reads.
pub unsafe fn setup_read_from_peripheral<W: Word>(
&self,
peri_addr: *const W,
buf: &mut [W],
enable_interrupt: EnableInterrupt,
) {
assert!(!buf.is_empty());
assert!(buf.len() <= 0x7fff);
let size = W::size();
let byte_size = size.bytes();
let t = self.tcd();
// Reset channel control/error/interrupt state
Self::reset_channel_state(t);
// Source: peripheral register, fixed
Self::set_source_ptr(t, peri_addr);
Self::set_source_fixed(t);
// Destination: memory buffer, incrementing
Self::set_dest_ptr(t, buf.as_mut_ptr());
Self::set_dest_increment(t, size);
// Attributes: set size and explicitly disable modulo
Self::set_even_transfer_size(t, size);
// Minor loop: transfer one word per request
Self::set_minor_loop_ct_no_offsets(t, byte_size as u32);
// No final adjustments
Self::set_no_final_adjustments(t);
// Major loop count = number of words
let count = buf.len() as u16;
Self::set_major_loop_ct_elinkno(t, count);
// CSR: optional interrupt on major loop complete and auto-clear ERQ
t.tcd_csr().write(|w| {
let w = match enable_interrupt {
EnableInterrupt::Yes => w.intmajor().enable(),
EnableInterrupt::No => w.intmajor().disable(),
};
w.inthalf()
.disable()
.dreq()
.erq_field_clear()
.esg()
.normal_format()
.majorelink()
.disable()
.eeop()
.disable()
.esda()
.disable()
.bwc()
.no_stall()
});
// Ensure all TCD writes have completed before DMA engine reads them
cortex_m::asm::dsb();
}
/// Configure the integrated channel MUX to use the given typed
/// DMA request source (e.g., [`Lpuart2TxRequest`] or [`Lpuart2RxRequest`]).
///
/// This is the type-safe version that uses marker types to ensure
/// compile-time verification of request source validity.
///
/// # Safety
///
/// The channel must be properly configured before enabling requests.
/// The caller must ensure the DMA request source matches the peripheral
/// that will drive this channel.
///
/// # Note
///
/// The NXP SDK requires a two-step write sequence: first clear
/// the mux to 0, then set the actual source. This is a hardware
/// requirement on eDMA4 for the mux to properly latch.
///
/// # Example
///
/// ```ignore
/// use embassy_mcxa::dma::{DmaChannel, Lpuart2RxRequest};
///
/// // Type-safe: compiler verifies this is a valid DMA request type
/// unsafe {
/// channel.set_request_source::<Lpuart2RxRequest>();
/// }
/// ```
#[inline]
pub unsafe fn set_request_source<R: DmaRequest>(&self) {
// Two-step write per NXP SDK: clear to 0, then set actual source.
self.tcd().ch_mux().write(|w| w.src().bits(0));
cortex_m::asm::dsb(); // Ensure the clear completes before setting new source
self.tcd().ch_mux().write(|w| w.src().bits(R::REQUEST_NUMBER));
}
/// Enable hardware requests for this channel (ERQ=1).
///
/// # Safety
///
/// The channel must be properly configured before enabling requests.
pub unsafe fn enable_request(&self) {
let t = self.tcd();
t.ch_csr().modify(|_, w| w.erq().enable());
}
/// Disable hardware requests for this channel (ERQ=0).
///
/// # Safety
///
/// Disabling requests on an active transfer may leave the transfer incomplete.
pub unsafe fn disable_request(&self) {
let t = self.tcd();
t.ch_csr().modify(|_, w| w.erq().disable());
}
/// Return true if the channel's DONE flag is set.
pub fn is_done(&self) -> bool {
let t = self.tcd();
t.ch_csr().read().done().bit_is_set()
}
/// Clear the DONE flag for this channel.
///
/// Uses modify to preserve other bits (especially ERQ) unlike write
/// which would clear ERQ and halt an active transfer.
///
/// # Safety
///
/// Clearing DONE while a transfer is in progress may cause undefined behavior.
pub unsafe fn clear_done(&self) {
let t = self.tcd();
t.ch_csr().modify(|_, w| w.done().clear_bit_by_one());
}
/// Clear the channel interrupt flag (CH_INT.INT).
///
/// # Safety
///
/// Must be called from the correct interrupt context or with interrupts disabled.
pub unsafe fn clear_interrupt(&self) {
let t = self.tcd();
t.ch_int().write(|w| w.int().clear_bit_by_one());
}
/// Trigger a software start for this channel.
///
/// # Safety
///
/// The channel must be properly configured with a valid TCD before triggering.
pub unsafe fn trigger_start(&self) {
let t = self.tcd();
t.tcd_csr().modify(|_, w| w.start().channel_started());
}
/// Get the waker for this channel
pub fn waker(&self) -> &'static AtomicWaker {
&STATES[C::INDEX].waker
}
/// Enable the interrupt for this channel in the NVIC.
pub fn enable_interrupt(&self) {
unsafe {
cortex_m::peripheral::NVIC::unmask(C::INTERRUPT);
}
}
/// Enable Major Loop Linking.
///
/// When the major loop completes, the hardware will trigger a service request
/// on `link_ch`.
///
/// # Arguments
///
/// * `link_ch` - Target channel index (0-7) to link to
///
/// # Safety
///
/// The channel must be properly configured before setting up linking.
pub unsafe fn set_major_link(&self, link_ch: usize) {
let t = self.tcd();
t.tcd_csr()
.modify(|_, w| w.majorelink().enable().majorlinkch().bits(link_ch as u8));
}
/// Disable Major Loop Linking.
///
/// Removes any major loop channel linking previously configured.
///
/// # Safety
///
/// The caller must ensure this doesn't disrupt an active transfer that
/// depends on the linking.
pub unsafe fn clear_major_link(&self) {
let t = self.tcd();
t.tcd_csr().modify(|_, w| w.majorelink().disable());
}
/// Enable Minor Loop Linking.
///
/// After each minor loop, the hardware will trigger a service request
/// on `link_ch`.
///
/// # Arguments
///
/// * `link_ch` - Target channel index (0-7) to link to
///
/// # Note
///
/// This rewrites CITER and BITER registers to the ELINKYES format.
/// It preserves the current loop count.
///
/// # Safety
///
/// The channel must be properly configured before setting up linking.
pub unsafe fn set_minor_link(&self, link_ch: usize) {
let t = self.tcd();
// Read current CITER (assuming ELINKNO format initially)
let current_citer = t.tcd_citer_elinkno().read().citer().bits();
let current_biter = t.tcd_biter_elinkno().read().biter().bits();
// Write back using ELINKYES format
t.tcd_citer_elinkyes().write(|w| {
w.citer()
.bits(current_citer)
.elink()
.enable()
.linkch()
.bits(link_ch as u8)
});
t.tcd_biter_elinkyes().write(|w| {
w.biter()
.bits(current_biter)
.elink()
.enable()
.linkch()
.bits(link_ch as u8)
});
}
/// Disable Minor Loop Linking.
///
/// Removes any minor loop channel linking previously configured.
/// This rewrites CITER and BITER registers to the ELINKNO format,
/// preserving the current loop count.
///
/// # Safety
///
/// The caller must ensure this doesn't disrupt an active transfer that
/// depends on the linking.
pub unsafe fn clear_minor_link(&self) {
let t = self.tcd();
// Read current CITER (could be in either format, but we only need the count)
// Note: In ELINKYES format, citer is 9 bits; in ELINKNO, it's 15 bits.
// We read from ELINKNO which will give us the combined value.
let current_citer = t.tcd_citer_elinkno().read().citer().bits();
let current_biter = t.tcd_biter_elinkno().read().biter().bits();
// Write back using ELINKNO format (disabling link)
t.tcd_citer_elinkno()
.write(|w| w.citer().bits(current_citer).elink().disable());
t.tcd_biter_elinkno()
.write(|w| w.biter().bits(current_biter).elink().disable());
}
/// Load a TCD from memory into the hardware channel registers.
///
/// This is useful for scatter/gather and ping-pong transfers where
/// TCDs are prepared in RAM and then loaded into the hardware.
///
/// # Safety
///
/// - The TCD must be properly initialized.
/// - The caller must ensure no concurrent access to the same channel.
pub unsafe fn load_tcd(&self, tcd: &Tcd) {
let t = self.tcd();
t.tcd_saddr().write(|w| w.saddr().bits(tcd.saddr));
t.tcd_soff().write(|w| w.soff().bits(tcd.soff as u16));
t.tcd_attr().write(|w| w.bits(tcd.attr));
t.tcd_nbytes_mloffno().write(|w| w.nbytes().bits(tcd.nbytes));
t.tcd_slast_sda().write(|w| w.slast_sda().bits(tcd.slast as u32));
t.tcd_daddr().write(|w| w.daddr().bits(tcd.daddr));
t.tcd_doff().write(|w| w.doff().bits(tcd.doff as u16));
t.tcd_citer_elinkno().write(|w| w.citer().bits(tcd.citer));
t.tcd_dlast_sga().write(|w| w.dlast_sga().bits(tcd.dlast_sga as u32));
t.tcd_csr().write(|w| w.bits(tcd.csr));
t.tcd_biter_elinkno().write(|w| w.biter().bits(tcd.biter));
}
}
/// In-memory representation of a Transfer Control Descriptor (TCD).
///
/// This matches the hardware layout (32 bytes).
#[repr(C, align(32))]
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Tcd {
pub saddr: u32,
pub soff: i16,
pub attr: u16,
pub nbytes: u32,
pub slast: i32,
pub daddr: u32,
pub doff: i16,
pub citer: u16,
pub dlast_sga: i32,
pub csr: u16,
pub biter: u16,
}
struct State {
/// Waker for transfer complete interrupt
waker: AtomicWaker,
/// Waker for half-transfer interrupt
half_waker: AtomicWaker,
}
impl State {
const fn new() -> Self {
Self {
waker: AtomicWaker::new(),
half_waker: AtomicWaker::new(),
}
}
}
static STATES: [State; 8] = [
State::new(),
State::new(),
State::new(),
State::new(),
State::new(),
State::new(),
State::new(),
State::new(),
];
pub(crate) fn waker(idx: usize) -> &'static AtomicWaker {
&STATES[idx].waker
}
pub(crate) fn half_waker(idx: usize) -> &'static AtomicWaker {
&STATES[idx].half_waker
}
// ============================================================================
// Async Transfer Future
// ============================================================================
/// An in-progress DMA transfer.
///
/// This type implements `Future` and can be `.await`ed to wait for the
/// transfer to complete. Dropping the transfer will abort it.
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Transfer<'a> {
channel: AnyChannel,
_phantom: core::marker::PhantomData<&'a ()>,
}
impl<'a> Transfer<'a> {
/// Create a new transfer for the given channel.
///
/// The caller must have already configured and started the DMA channel.
pub(crate) fn new(channel: AnyChannel) -> Self {
Self {
channel,
_phantom: core::marker::PhantomData,
}
}
/// Check if the transfer is still running.
pub fn is_running(&self) -> bool {
!self.channel.is_done()
}
/// Get the remaining transfer count.
pub fn remaining(&self) -> u16 {
let t = self.channel.tcd();
t.tcd_citer_elinkno().read().citer().bits()
}
/// Block until the transfer completes.
pub fn blocking_wait(self) {
while self.is_running() {
core::hint::spin_loop();
}
// Ensure all DMA writes are visible
fence(Ordering::SeqCst);
// Don't run drop (which would abort)
core::mem::forget(self);
}
/// Wait for the half-transfer interrupt asynchronously.
///
/// This is useful for double-buffering scenarios where you want to process
/// the first half of the buffer while the second half is being filled.
///
/// Returns `true` if the half-transfer occurred, `false` if the transfer
/// completed before the half-transfer interrupt.
///
/// # Note
///
/// The transfer must be configured with `TransferOptions::half_transfer_interrupt = true`
/// for this method to work correctly.
pub async fn wait_half(&mut self) -> Result<bool, TransferErrorRaw> {
use core::future::poll_fn;
poll_fn(|cx| {
let state = &STATES[self.channel.index];
// Register the half-transfer waker
state.half_waker.register(cx.waker());
// Check if there's an error
let t = self.channel.tcd();
let es = t.ch_es().read();
if es.err().is_error() {
// Currently, all error fields are in the lowest 8 bits, as-casting truncates
let errs = es.bits() as u8;
return Poll::Ready(Err(TransferErrorRaw(errs)));
}
// Check if we're past the half-way point
let biter = t.tcd_biter_elinkno().read().biter().bits();
let citer = t.tcd_citer_elinkno().read().citer().bits();
let half_point = biter / 2;
if self.channel.is_done() {
// Transfer completed before half-transfer
Poll::Ready(Ok(false))
} else if citer <= half_point {
// We're past the half-way point
fence(Ordering::SeqCst);
Poll::Ready(Ok(true))
} else {
Poll::Pending
}
})
.await
}
/// Abort the transfer.
fn abort(&mut self) {
let t = self.channel.tcd();
// Disable channel requests
t.ch_csr().modify(|_, w| w.erq().disable());
// Clear any pending interrupt
t.ch_int().write(|w| w.int().clear_bit_by_one());
// Clear DONE flag
t.ch_csr().modify(|_, w| w.done().clear_bit_by_one());
fence(Ordering::SeqCst);
}
}
/// Raw transfer error bits. Can be queried or all errors can be iterated over
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Copy, Clone, Debug)]
pub struct TransferErrorRaw(u8);
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Copy, Clone, Debug)]
pub struct TransferErrorRawIter(u8);
impl TransferErrorRaw {
const MAP: &[(u8, TransferError)] = &[
(1 << 0, TransferError::DestinationBus),
(1 << 1, TransferError::SourceBus),
(1 << 2, TransferError::ScatterGatherConfiguration),
(1 << 3, TransferError::NbytesCiterConfiguration),
(1 << 4, TransferError::DestinationOffset),
(1 << 5, TransferError::DestinationAddress),
(1 << 6, TransferError::SourceOffset),
(1 << 7, TransferError::SourceAddress),
];
/// Convert to an iterator of contained errors
pub fn err_iter(self) -> TransferErrorRawIter {
TransferErrorRawIter(self.0)
}
/// Destination Bus Error
#[inline]
pub fn has_destination_bus_err(&self) -> bool {
(self.0 & (1 << 0)) != 0
}
/// Source Bus Error
#[inline]
pub fn has_source_bus_err(&self) -> bool {
(self.0 & (1 << 1)) != 0
}
/// Indicates that `TCDn_DLAST_SGA` is not on a 32-byte boundary. This field is
/// checked at the beginning of a scatter/gather operation after major loop completion
/// if `TCDn_CSR[ESG]` is enabled.
#[inline]
pub fn has_scatter_gather_configuration_err(&self) -> bool {
(self.0 & (1 << 2)) != 0
}
/// This error indicates that one of the following has occurred:
///
/// * `TCDn_NBYTES` is not a multiple of `TCDn_ATTR[SSIZE]` and `TCDn_ATTR[DSIZE]`
/// * `TCDn_CITER[CITER]` is equal to zero
/// * `TCDn_CITER[ELINK]` is not equal to `TCDn_BITER[ELINK]`
#[inline]
pub fn has_nbytes_citer_configuration_err(&self) -> bool {
(self.0 & (1 << 3)) != 0
}
/// `TCDn_DOFF` is inconsistent with `TCDn_ATTR[DSIZE]`.
#[inline]
pub fn has_destination_offset_err(&self) -> bool {
(self.0 & (1 << 4)) != 0
}
/// `TCDn_DADDR` is inconsistent with `TCDn_ATTR[DSIZE]`.
#[inline]
pub fn has_destination_address_err(&self) -> bool {
(self.0 & (1 << 5)) != 0
}
/// `TCDn_SOFF` is inconsistent with `TCDn_ATTR[SSIZE]`.
#[inline]
pub fn has_source_offset_err(&self) -> bool {
(self.0 & (1 << 6)) != 0
}
/// `TCDn_SADDR` is inconsistent with `TCDn_ATTR[SSIZE]`
#[inline]
pub fn has_source_address_err(&self) -> bool {
(self.0 & (1 << 7)) != 0
}
}
impl Iterator for TransferErrorRawIter {
type Item = TransferError;
fn next(&mut self) -> Option<Self::Item> {
if self.0 == 0 {
return None;
}
for (mask, var) in TransferErrorRaw::MAP {
// If the bit is set...
if self.0 | mask != 0 {
// clear the bit
self.0 &= !mask;
// and return the answer
return Some(*var);
}
}
// Shouldn't happen, but oh well.
None
}
}
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum TransferError {
/// `TCDn_SADDR` is inconsistent with `TCDn_ATTR[SSIZE]`
SourceAddress,
/// `TCDn_SOFF` is inconsistent with `TCDn_ATTR[SSIZE]`.
SourceOffset,
/// `TCDn_DADDR` is inconsistent with `TCDn_ATTR[DSIZE]`.
DestinationAddress,
/// `TCDn_DOFF` is inconsistent with `TCDn_ATTR[DSIZE]`.
DestinationOffset,
/// This error indicates that one of the following has occurred:
///
/// * `TCDn_NBYTES` is not a multiple of `TCDn_ATTR[SSIZE]` and `TCDn_ATTR[DSIZE]`
/// * `TCDn_CITER[CITER]` is equal to zero
/// * `TCDn_CITER[ELINK]` is not equal to `TCDn_BITER[ELINK]`
NbytesCiterConfiguration,
/// Indicates that `TCDn_DLAST_SGA` is not on a 32-byte boundary. This field is
/// checked at the beginning of a scatter/gather operation after major loop completion
/// if `TCDn_CSR[ESG]` is enabled.
ScatterGatherConfiguration,
/// Source Bus Error
SourceBus,
/// Destination Bus Error
DestinationBus,
}
impl<'a> Unpin for Transfer<'a> {}
impl<'a> Future for Transfer<'a> {
type Output = Result<(), TransferErrorRaw>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let state = &STATES[self.channel.index];
// Register waker first
state.waker.register(cx.waker());
let done = self.channel.is_done();
if done {
// Ensure all DMA writes are visible before returning
fence(Ordering::SeqCst);
let es = self.channel.tcd().ch_es().read();
if es.err().is_error() {
// Currently, all error fields are in the lowest 8 bits, as-casting truncates
let errs = es.bits() as u8;
Poll::Ready(Err(TransferErrorRaw(errs)))
} else {
Poll::Ready(Ok(()))
}
} else {
Poll::Pending
}
}
}
impl<'a> Drop for Transfer<'a> {
fn drop(&mut self) {
// Only abort if the transfer is still running
// If already complete, no need to abort
if self.is_running() {
self.abort();
// Wait for abort to complete
while self.is_running() {
core::hint::spin_loop();
}
}
fence(Ordering::SeqCst);
}
}
// ============================================================================
// Ring Buffer for Circular DMA
// ============================================================================
/// A ring buffer for continuous DMA reception.
///
/// This structure manages a circular DMA transfer, allowing continuous
/// reception of data without losing bytes between reads. It uses both
/// half-transfer and complete-transfer interrupts to track available data.
///
/// # Example
///
/// ```no_run
/// use embassy_mcxa::dma::{DmaChannel, RingBuffer, TransferOptions};
///
/// static mut RX_BUF: [u8; 64] = [0; 64];
///
/// let dma_ch = DmaChannel::new(p.DMA_CH0);
/// let ring_buf = unsafe {
/// dma_ch.setup_circular_read(
/// uart_rx_addr,
/// &mut RX_BUF,
/// )
/// };
///
/// // Read data as it arrives
/// let mut buf = [0u8; 16];
/// let n = ring_buf.read(&mut buf).await?;
/// ```
pub struct RingBuffer<'a, W: Word> {
channel: AnyChannel,
/// Buffer pointer. We use NonNull instead of &mut because DMA acts like
/// a separate thread writing to this buffer, and &mut claims exclusive
/// access which the compiler could optimize incorrectly.
buf: NonNull<[W]>,
/// Buffer length cached for convenience
buf_len: usize,
/// Read position in the buffer (consumer side)
read_pos: AtomicUsize,
/// Phantom data to tie the lifetime to the original buffer
_lt: PhantomData<&'a mut [W]>,
}
impl<'a, W: Word> RingBuffer<'a, W> {
/// Create a new ring buffer for the given channel and buffer.
///
/// # Safety
///
/// The caller must ensure:
/// - The DMA channel has been configured for circular transfer
/// - The buffer remains valid for the lifetime of the ring buffer
/// - Only one RingBuffer exists per DMA channel at a time
pub(crate) unsafe fn new(channel: AnyChannel, buf: &'a mut [W]) -> Self {
let buf_len = buf.len();
Self {
channel,
buf: NonNull::from(buf),
buf_len,
read_pos: AtomicUsize::new(0),
_lt: PhantomData,
}
}
/// Get a slice reference to the buffer.
///
/// # Safety
///
/// The caller must ensure that DMA is not actively writing to the
/// portion of the buffer being accessed, or that the access is
/// appropriately synchronized.
#[inline]
unsafe fn buf_slice(&self) -> &[W] {
self.buf.as_ref()
}
/// Get the current DMA write position in the buffer.
///
/// This reads the current destination address from the DMA controller
/// and calculates the buffer offset.
fn dma_write_pos(&self) -> usize {
let t = self.channel.tcd();
let daddr = t.tcd_daddr().read().daddr().bits() as usize;
let buf_start = self.buf.as_ptr() as *const W as usize;
// Calculate offset from buffer start
let offset = daddr.wrapping_sub(buf_start) / core::mem::size_of::<W>();
// Ensure we're within bounds (DMA wraps around)
offset % self.buf_len
}
/// Returns the number of bytes available to read.
pub fn available(&self) -> usize {
let write_pos = self.dma_write_pos();
let read_pos = self.read_pos.load(Ordering::Acquire);
if write_pos >= read_pos {
write_pos - read_pos
} else {
self.buf_len - read_pos + write_pos
}
}
/// Check if the buffer has overrun (data was lost).
///
/// This happens when DMA writes faster than the application reads.
pub fn is_overrun(&self) -> bool {
// In a true overrun, the DMA would have wrapped around and caught up
// to our read position. We can detect this by checking if available()
// equals the full buffer size (minus 1 to distinguish from empty).
self.available() >= self.buf_len - 1
}
/// Read data from the ring buffer into the provided slice.
///
/// Returns the number of elements read, which may be less than
/// `dst.len()` if not enough data is available.
///
/// This method does not block; use `read_async()` for async waiting.
pub fn read_immediate(&self, dst: &mut [W]) -> usize {
let write_pos = self.dma_write_pos();
let read_pos = self.read_pos.load(Ordering::Acquire);
// Calculate available bytes
let available = if write_pos >= read_pos {
write_pos - read_pos
} else {
self.buf_len - read_pos + write_pos
};
let to_read = dst.len().min(available);
if to_read == 0 {
return 0;
}
// Safety: We only read from portions of the buffer that DMA has
// already written to (between read_pos and write_pos).
let buf = unsafe { self.buf_slice() };
// Read data, handling wrap-around
let first_chunk = (self.buf_len - read_pos).min(to_read);
dst[..first_chunk].copy_from_slice(&buf[read_pos..read_pos + first_chunk]);
if to_read > first_chunk {
let second_chunk = to_read - first_chunk;
dst[first_chunk..to_read].copy_from_slice(&buf[..second_chunk]);
}
// Update read position
let new_read_pos = (read_pos + to_read) % self.buf_len;
self.read_pos.store(new_read_pos, Ordering::Release);
to_read
}
/// Read data from the ring buffer asynchronously.
///
/// This waits until at least one byte is available, then reads as much
/// as possible into the destination buffer.
///
/// Returns the number of elements read.
pub async fn read(&self, dst: &mut [W]) -> Result<usize, Error> {
use core::future::poll_fn;
if dst.is_empty() {
return Ok(0);
}
poll_fn(|cx| {
// Check for overrun
if self.is_overrun() {
return Poll::Ready(Err(Error::Overrun));
}
// Try to read immediately
let n = self.read_immediate(dst);
if n > 0 {
return Poll::Ready(Ok(n));
}
// Register wakers for both half and complete interrupts
let state = &STATES[self.channel.index()];
state.waker.register(cx.waker());
state.half_waker.register(cx.waker());
// Check again after registering waker (avoid race)
let n = self.read_immediate(dst);
if n > 0 {
return Poll::Ready(Ok(n));
}
Poll::Pending
})
.await
}
/// Clear the ring buffer, discarding all unread data.
pub fn clear(&self) {
let write_pos = self.dma_write_pos();
self.read_pos.store(write_pos, Ordering::Release);
}
/// Stop the DMA transfer and consume the ring buffer.
///
/// Returns any remaining unread data count.
pub fn stop(mut self) -> usize {
let res = self.teardown();
drop(self);
res
}
/// Stop the DMA transfer. Intended to be called by `stop()` or `Drop`.
fn teardown(&mut self) -> usize {
let available = self.available();
// Disable the channel
let t = self.channel.tcd();
t.ch_csr().modify(|_, w| w.erq().disable());
// Clear flags
t.ch_int().write(|w| w.int().clear_bit_by_one());
t.ch_csr().modify(|_, w| w.done().clear_bit_by_one());
fence(Ordering::SeqCst);
available
}
}
impl<'a, W: Word> Drop for RingBuffer<'a, W> {
fn drop(&mut self) {
self.teardown();
}
}
impl<C: Channel> DmaChannel<C> {
/// Set up a circular DMA transfer for continuous peripheral-to-memory reception.
///
/// This configures the DMA channel for circular operation with both half-transfer
/// and complete-transfer interrupts enabled. The transfer runs continuously until
/// stopped via [`RingBuffer::stop()`].
///
/// # Arguments
///
/// * `peri_addr` - Peripheral register address to read from
/// * `buf` - Destination buffer (should be power-of-2 size for best efficiency)
///
/// # Returns
///
/// A [`RingBuffer`] that can be used to read received data.
///
/// # Safety
///
/// - The buffer must remain valid for the lifetime of the returned RingBuffer.
/// - The peripheral address must be valid for reads.
/// - The peripheral's DMA request must be configured to trigger this channel.
pub unsafe fn setup_circular_read<'a, W: Word>(&self, peri_addr: *const W, buf: &'a mut [W]) -> RingBuffer<'a, W> {
assert!(!buf.is_empty());
assert!(buf.len() <= 0x7fff);
// For circular mode, buffer size should ideally be power of 2
// but we don't enforce it
let size = W::size();
let byte_size = size.bytes();
let t = self.tcd();
// Reset channel state
Self::reset_channel_state(t);
// Source: peripheral register, fixed
Self::set_source_ptr(t, peri_addr);
Self::set_source_fixed(t);
// Destination: memory buffer, incrementing
Self::set_dest_ptr(t, buf.as_mut_ptr());
Self::set_dest_increment(t, size);
// Transfer attributes
Self::set_even_transfer_size(t, size);
// Minor loop: transfer one word per request
Self::set_minor_loop_ct_no_offsets(t, byte_size as u32);
// Major loop count = buffer size
let count = buf.len() as u16;
Self::set_major_loop_ct_elinkno(t, count);
// After major loop: reset destination to buffer start (circular)
let buf_bytes = (buf.len() * byte_size) as i32;
t.tcd_slast_sda().write(|w| w.slast_sda().bits(0)); // Source doesn't change
t.tcd_dlast_sga().write(|w| w.dlast_sga().bits((-buf_bytes) as u32));
// Control/status: enable both half and complete interrupts, NO DREQ (continuous)
t.tcd_csr().write(|w| {
w.intmajor()
.enable()
.inthalf()
.enable()
.dreq()
.channel_not_affected() // Don't clear ERQ on complete (circular)
.esg()
.normal_format()
.majorelink()
.disable()
.eeop()
.disable()
.esda()
.disable()
.bwc()
.no_stall()
});
cortex_m::asm::dsb();
// Enable the channel request
t.ch_csr().modify(|_, w| w.erq().enable());
// Enable NVIC interrupt for this channel so async wakeups work
self.enable_interrupt();
RingBuffer::new(self.as_any(), buf)
}
}
// ============================================================================
// Scatter-Gather Builder
// ============================================================================
/// Maximum number of TCDs in a scatter-gather chain.
pub const MAX_SCATTER_GATHER_TCDS: usize = 16;
/// A builder for constructing scatter-gather DMA transfer chains.
///
/// This provides a type-safe way to build TCD chains for scatter-gather
/// transfers without manual TCD manipulation.
///
/// # Example
///
/// ```no_run
/// use embassy_mcxa::dma::{DmaChannel, ScatterGatherBuilder};
///
/// let mut builder = ScatterGatherBuilder::<u32>::new();
///
/// // Add transfer segments
/// builder.add_transfer(&src1, &mut dst1);
/// builder.add_transfer(&src2, &mut dst2);
/// builder.add_transfer(&src3, &mut dst3);
///
/// // Build and execute
/// let transfer = unsafe { builder.build(&dma_ch).unwrap() };
/// transfer.await;
/// ```
pub struct ScatterGatherBuilder<'a, W: Word> {
/// TCD pool (must be 32-byte aligned)
tcds: [Tcd; MAX_SCATTER_GATHER_TCDS],
/// Number of TCDs configured
count: usize,
/// Phantom marker for word type
_phantom: core::marker::PhantomData<W>,
_plt: core::marker::PhantomData<&'a mut W>,
}
impl<'a, W: Word> ScatterGatherBuilder<'a, W> {
/// Create a new scatter-gather builder.
pub fn new() -> Self {
ScatterGatherBuilder {
tcds: [Tcd::default(); MAX_SCATTER_GATHER_TCDS],
count: 0,
_phantom: core::marker::PhantomData,
_plt: core::marker::PhantomData,
}
}
/// Add a memory-to-memory transfer segment to the chain.
///
/// # Arguments
///
/// * `src` - Source buffer for this segment
/// * `dst` - Destination buffer for this segment
///
/// # Panics
///
/// Panics if the maximum number of segments (16) is exceeded.
pub fn add_transfer<'b: 'a>(&mut self, src: &'b [W], dst: &'b mut [W]) -> &mut Self {
assert!(self.count < MAX_SCATTER_GATHER_TCDS, "Too many scatter-gather segments");
assert!(!src.is_empty());
assert!(dst.len() >= src.len());
let size = W::size();
let byte_size = size.bytes();
let hw_size = size.to_hw_size();
let nbytes = (src.len() * byte_size) as u32;
// Build the TCD for this segment
self.tcds[self.count] = Tcd {
saddr: src.as_ptr() as u32,
soff: byte_size as i16,
attr: ((hw_size as u16) << 8) | (hw_size as u16), // SSIZE | DSIZE
nbytes,
slast: 0,
daddr: dst.as_mut_ptr() as u32,
doff: byte_size as i16,
citer: 1,
dlast_sga: 0, // Will be filled in by build()
csr: 0x0002, // INTMAJOR only (ESG will be set for non-last TCDs)
biter: 1,
};
self.count += 1;
self
}
/// Get the number of transfer segments added.
pub fn segment_count(&self) -> usize {
self.count
}
/// Build the scatter-gather chain and start the transfer.
///
/// # Arguments
///
/// * `channel` - The DMA channel to use for the transfer
///
/// # Returns
///
/// A `Transfer` future that completes when the entire chain has executed.
pub fn build<C: Channel>(&mut self, channel: &DmaChannel<C>) -> Result<Transfer<'a>, Error> {
if self.count == 0 {
return Err(Error::Configuration);
}
// Link TCDs together
//
// CSR bit definitions:
// - START = bit 0 = 0x0001 (triggers transfer when set)
// - INTMAJOR = bit 1 = 0x0002 (interrupt on major loop complete)
// - ESG = bit 4 = 0x0010 (enable scatter-gather, loads next TCD on complete)
//
// When hardware loads a TCD via scatter-gather (ESG), it copies the TCD's
// CSR directly into the hardware register. If START is not set in that CSR,
// the hardware will NOT auto-execute the loaded TCD.
//
// Strategy:
// - First TCD: ESG | INTMAJOR (no START - we add it manually after loading)
// - Middle TCDs: ESG | INTMAJOR | START (auto-execute when loaded via S/G)
// - Last TCD: INTMAJOR | START (auto-execute, no further linking)
for i in 0..self.count {
let is_first = i == 0;
let is_last = i == self.count - 1;
if is_first {
if is_last {
// Only one TCD - no ESG, no START (we add START manually)
self.tcds[i].dlast_sga = 0;
self.tcds[i].csr = 0x0002; // INTMAJOR only
} else {
// First of multiple - ESG to link, no START (we add START manually)
self.tcds[i].dlast_sga = &self.tcds[i + 1] as *const Tcd as i32;
self.tcds[i].csr = 0x0012; // ESG | INTMAJOR
}
} else if is_last {
// Last TCD (not first) - no ESG, but START so it auto-executes
self.tcds[i].dlast_sga = 0;
self.tcds[i].csr = 0x0003; // INTMAJOR | START
} else {
// Middle TCD - ESG to link, and START so it auto-executes
self.tcds[i].dlast_sga = &self.tcds[i + 1] as *const Tcd as i32;
self.tcds[i].csr = 0x0013; // ESG | INTMAJOR | START
}
}
let t = channel.tcd();
// Reset channel state - clear DONE, disable requests, clear errors
// This ensures the channel is in a clean state before loading the TCD
DmaChannel::<C>::reset_channel_state(t);
// Memory barrier to ensure channel state is reset before loading TCD
cortex_m::asm::dsb();
// Load first TCD into hardware
unsafe {
channel.load_tcd(&self.tcds[0]);
}
// Memory barrier before setting START
cortex_m::asm::dsb();
// Start the transfer
t.tcd_csr().modify(|_, w| w.start().channel_started());
Ok(Transfer::new(channel.as_any()))
}
/// Reset the builder for reuse.
pub fn clear(&mut self) {
self.count = 0;
}
}
impl<W: Word> Default for ScatterGatherBuilder<'_, W> {
fn default() -> Self {
Self::new()
}
}
/// A completed scatter-gather transfer result.
///
/// This type is returned after a scatter-gather transfer completes,
/// providing access to any error information.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScatterGatherResult {
/// Number of segments successfully transferred
pub segments_completed: usize,
/// Error if any occurred
pub error: Option<Error>,
}
// ============================================================================
// Interrupt Handler
// ============================================================================
/// Interrupt handler helper.
///
/// Call this from your interrupt handler to clear the interrupt flag and wake the waker.
/// This handles both half-transfer and complete-transfer interrupts.
///
/// # Safety
/// Must be called from the correct DMA channel interrupt context.
pub unsafe fn on_interrupt(ch_index: usize) {
let p = pac::Peripherals::steal();
let edma = &p.edma_0_tcd0;
let t = edma.tcd(ch_index);
// Read TCD CSR to determine interrupt source
let csr = t.tcd_csr().read();
// Check if this is a half-transfer interrupt
// INTHALF is set and we're at or past the half-way point
if csr.inthalf().bit_is_set() {
let biter = t.tcd_biter_elinkno().read().biter().bits();
let citer = t.tcd_citer_elinkno().read().citer().bits();
let half_point = biter / 2;
if citer <= half_point && citer > 0 {
// Half-transfer interrupt - wake half_waker
half_waker(ch_index).wake();
}
}
// Clear INT flag
t.ch_int().write(|w| w.int().clear_bit_by_one());
// If DONE is set, this is a complete-transfer interrupt
// Only wake the full-transfer waker when the transfer is actually complete
if t.ch_csr().read().done().bit_is_set() {
waker(ch_index).wake();
}
}
// ============================================================================
// Type-level Interrupt Handlers
// ============================================================================
/// Macro to generate DMA channel interrupt handlers.
macro_rules! impl_dma_interrupt_handler {
($irq:ident, $ch:expr) => {
#[interrupt]
fn $irq() {
unsafe {
on_interrupt($ch);
}
}
};
}
use crate::pac::interrupt;
impl_dma_interrupt_handler!(DMA_CH0, 0);
impl_dma_interrupt_handler!(DMA_CH1, 1);
impl_dma_interrupt_handler!(DMA_CH2, 2);
impl_dma_interrupt_handler!(DMA_CH3, 3);
impl_dma_interrupt_handler!(DMA_CH4, 4);
impl_dma_interrupt_handler!(DMA_CH5, 5);
impl_dma_interrupt_handler!(DMA_CH6, 6);
impl_dma_interrupt_handler!(DMA_CH7, 7);
|