aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/stm32f2-f4/include/series/dma.h
blob: 472dfea66995f96f656c80885fb8c93fa416b849 (plain)
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
/******************************************************************************
 * The MIT License
 *
 * Copyright (c) 2012 LeafLabs, LLC
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *****************************************************************************/

/**
 * @file libmaple/stm32f2-f4/include/series/dma.h
 * @author Marti Bolivar <mbolivar@leaflabs.com>
 * @brief STM32F2-F4 DMA series header
 */

#ifndef _LIBMAPLE_STM32F2F4_DMA_H_
#define _LIBMAPLE_STM32F2F4_DMA_H_

#ifdef __cplusplus
extern "C"{
#endif

#include <libmaple/dma_common.h>
#include <libmaple/libmaple_types.h>

/*
 * Register map and base pointers
 */

/**
 * @brief STM32F2-F4 DMA register map type.
 */
typedef struct dma_reg_map {
    /* Isn't it nice how on F1, it's CCR1, but on F2, it's S1CR? */

    /* Global DMA registers */
    __io uint32 LISR;   /**< Low interrupt status register */
    __io uint32 HISR;   /**< High interrupt status register */
    __io uint32 LIFCR;  /**< Low interrupt flag clear register */
    __io uint32 HIFCR;  /**< High interrupt flag clear register */
    /* Stream 0 registers */
    __io uint32 S0CR;   /**< Stream 0 control register */
    __io uint32 S0NDTR; /**< Stream 0 number of data register */
    __io uint32 S0PAR;  /**< Stream 0 peripheral address register */
    __io uint32 S0M0AR; /**< Stream 0 memory 0 address register */
    __io uint32 S0M1AR; /**< Stream 0 memory 1 address register */
    __io uint32 S0FCR;  /**< Stream 0 FIFO control register */
    /* Stream 1 registers */
    __io uint32 S1CR;   /**< Stream 1 control register */
    __io uint32 S1NDTR; /**< Stream 1 number of data register */
    __io uint32 S1PAR;  /**< Stream 1 peripheral address register */
    __io uint32 S1M0AR; /**< Stream 1 memory 0 address register */
    __io uint32 S1M1AR; /**< Stream 1 memory 1 address register */
    __io uint32 S1FCR;  /**< Stream 1 FIFO control register */
    /* Stream 2 registers */
    __io uint32 S2CR;   /**< Stream 2 control register */
    __io uint32 S2NDTR; /**< Stream 2 number of data register */
    __io uint32 S2PAR;  /**< Stream 2 peripheral address register */
    __io uint32 S2M0AR; /**< Stream 2 memory 0 address register */
    __io uint32 S2M1AR; /**< Stream 2 memory 1 address register */
    __io uint32 S2FCR;  /**< Stream 2 FIFO control register */
    /* Stream 3 registers */
    __io uint32 S3CR;   /**< Stream 3 control register */
    __io uint32 S3NDTR; /**< Stream 3 number of data register */
    __io uint32 S3PAR;  /**< Stream 3 peripheral address register */
    __io uint32 S3M0AR; /**< Stream 3 memory 0 address register */
    __io uint32 S3M1AR; /**< Stream 3 memory 1 address register */
    __io uint32 S3FCR;  /**< Stream 3 FIFO control register */
    /* Stream 4 registers */
    __io uint32 S4CR;   /**< Stream 4 control register */
    __io uint32 S4NDTR; /**< Stream 4 number of data register */
    __io uint32 S4PAR;  /**< Stream 4 peripheral address register */
    __io uint32 S4M0AR; /**< Stream 4 memory 0 address register */
    __io uint32 S4M1AR; /**< Stream 4 memory 1 address register */
    __io uint32 S4FCR;  /**< Stream 4 FIFO control register */
    /* Stream 5 registers */
    __io uint32 S5CR;   /**< Stream 5 control register */
    __io uint32 S5NDTR; /**< Stream 5 number of data register */
    __io uint32 S5PAR;  /**< Stream 5 peripheral address register */
    __io uint32 S5M0AR; /**< Stream 5 memory 0 address register */
    __io uint32 S5M1AR; /**< Stream 5 memory 1 address register */
    __io uint32 S5FCR;  /**< Stream 5 FIFO control register */
    /* Stream 6 registers */
    __io uint32 S6CR;   /**< Stream 6 control register */
    __io uint32 S6NDTR; /**< Stream 6 number of data register */
    __io uint32 S6PAR;  /**< Stream 6 peripheral address register */
    __io uint32 S6M0AR; /**< Stream 6 memory 0 address register */
    __io uint32 S6M1AR; /**< Stream 6 memory 1 address register */
    __io uint32 S6FCR;  /**< Stream 6 FIFO control register */
    /* Stream 7 registers */
    __io uint32 S7CR;   /**< Stream 7 control register */
    __io uint32 S7NDTR; /**< Stream 7 number of data register */
    __io uint32 S7PAR;  /**< Stream 7 peripheral address register */
    __io uint32 S7M0AR; /**< Stream 7 memory 0 address register */
    __io uint32 S7M1AR; /**< Stream 7 memory 1 address register */
    __io uint32 S7FCR;  /**< Stream 7 FIFO control register */
} dma_reg_map;

/** DMA controller 1 register map base pointer */
#define DMA1_BASE                       ((struct dma_reg_map*)0x40026000)
/** DMA controller 2 register map base pointer */
#define DMA2_BASE                       ((struct dma_reg_map*)0x40026400)

/**
 * @brief STM32F2-F4 DMA stream (i.e. tube) register map type.
 * Provides access to an individual stream's registers.
 * @see dma_tube_regs()
 */
typedef struct dma_tube_reg_map {
    __io uint32 SCR;   /**< Stream configuration register */
    __io uint32 SNDTR; /**< Stream number of data register */
    __io uint32 SPAR;  /**< Stream peripheral address register */
    __io uint32 SM0AR; /**< Stream memory 0 address register */
    __io uint32 SM1AR; /**< Stream memory 1 address register */
    __io uint32 SFCR;  /**< Stream FIFO control register */
} dma_tube_reg_map;

/** DMA1 stream 0 register map base pointer */
#define DMA1S0_BASE ((struct dma_tube_reg_map*)0x40026010)
/** DMA1 stream 1 register map base pointer */
#define DMA1S1_BASE ((struct dma_tube_reg_map*)0x40026028)
/** DMA1 stream 2 register map base pointer */
#define DMA1S2_BASE ((struct dma_tube_reg_map*)0x40026040)
/** DMA1 stream 3 register map base pointer */
#define DMA1S3_BASE ((struct dma_tube_reg_map*)0x40026058)
/** DMA1 stream 4 register map base pointer */
#define DMA1S4_BASE ((struct dma_tube_reg_map*)0x40026070)
/** DMA1 stream 5 register map base pointer */
#define DMA1S5_BASE ((struct dma_tube_reg_map*)0x40026088)
/** DMA1 stream 6 register map base pointer */
#define DMA1S6_BASE ((struct dma_tube_reg_map*)0x400260A0)
/** DMA1 stream 7 register map base pointer */
#define DMA1S7_BASE ((struct dma_tube_reg_map*)0x400260B8)

/** DMA2 stream 0 register map base pointer */
#define DMA2S0_BASE ((struct dma_tube_reg_map*)0x40026410)
/** DMA2 stream 1 register map base pointer */
#define DMA2S1_BASE ((struct dma_tube_reg_map*)0x40026028)
/** DMA2 stream 2 register map base pointer */
#define DMA2S2_BASE ((struct dma_tube_reg_map*)0x40026040)
/** DMA2 stream 3 register map base pointer */
#define DMA2S3_BASE ((struct dma_tube_reg_map*)0x40026058)
/** DMA2 stream 4 register map base pointer */
#define DMA2S4_BASE ((struct dma_tube_reg_map*)0x40026070)
/** DMA2 stream 5 register map base pointer */
#define DMA2S5_BASE ((struct dma_tube_reg_map*)0x40026088)
/** DMA2 stream 6 register map base pointer */
#define DMA2S6_BASE ((struct dma_tube_reg_map*)0x400260A0)
/** DMA2 stream 7 register map base pointer */
#define DMA2S7_BASE ((struct dma_tube_reg_map*)0x400260B8)

/*
 * Register bit definitions
 */

/* Low interrupt status register */

#define DMA_LISR_TCIF3_BIT           27
#define DMA_LISR_HTIF3_BIT           26
#define DMA_LISR_TEIF3_BIT           25
#define DMA_LISR_DMEIF3_BIT          24
#define DMA_LISR_FEIF3_BIT           22
#define DMA_LISR_TCIF2_BIT           21
#define DMA_LISR_HTIF2_BIT           20
#define DMA_LISR_TEIF2_BIT           19
#define DMA_LISR_DMEIF2_BIT          18
#define DMA_LISR_FEIF2_BIT           16
#define DMA_LISR_TCIF1_BIT           11
#define DMA_LISR_HTIF1_BIT           10
#define DMA_LISR_TEIF1_BIT            9
#define DMA_LISR_DMEIF1_BIT           8
#define DMA_LISR_FEIF1_BIT            6
#define DMA_LISR_TCIF0_BIT            5
#define DMA_LISR_HTIF0_BIT            4
#define DMA_LISR_TEIF0_BIT            3
#define DMA_LISR_DMEIF0_BIT           2
#define DMA_LISR_FEIF0_BIT            0

#define DMA_LISR_TCIF3               (1U << DMA_LISR_TCIF3_BIT)
#define DMA_LISR_HTIF3               (1U << DMA_LISR_HTIF3_BIT)
#define DMA_LISR_TEIF3               (1U << DMA_LISR_TEIF3_BIT)
#define DMA_LISR_DMEIF3              (1U << DMA_LISR_DMEIF3_BIT)
#define DMA_LISR_FEIF3               (1U << DMA_LISR_FEIF3_BIT)
#define DMA_LISR_TCIF2               (1U << DMA_LISR_TCIF2_BIT)
#define DMA_LISR_HTIF2               (1U << DMA_LISR_HTIF2_BIT)
#define DMA_LISR_TEIF2               (1U << DMA_LISR_TEIF2_BIT)
#define DMA_LISR_DMEIF2              (1U << DMA_LISR_DMEIF2_BIT)
#define DMA_LISR_FEIF2               (1U << DMA_LISR_FEIF2_BIT)
#define DMA_LISR_TCIF1               (1U << DMA_LISR_TCIF1_BIT)
#define DMA_LISR_HTIF1               (1U << DMA_LISR_HTIF1_BIT)
#define DMA_LISR_TEIF1               (1U << DMA_LISR_TEIF1_BIT)
#define DMA_LISR_DMEIF1              (1U << DMA_LISR_DMEIF1_BIT)
#define DMA_LISR_FEIF1               (1U << DMA_LISR_FEIF1_BIT)
#define DMA_LISR_TCIF0               (1U << DMA_LISR_TCIF0_BIT)
#define DMA_LISR_HTIF0               (1U << DMA_LISR_HTIF0_BIT)
#define DMA_LISR_TEIF0               (1U << DMA_LISR_TEIF0_BIT)
#define DMA_LISR_DMEIF0              (1U << DMA_LISR_DMEIF0_BIT)
#define DMA_LISR_FEIF0               (1U << DMA_LISR_FEIF0_BIT)

/* High interrupt status register */

#define DMA_HISR_TCIF7_BIT           27
#define DMA_HISR_HTIF7_BIT           26
#define DMA_HISR_TEIF7_BIT           25
#define DMA_HISR_DMEIF7_BIT          24
#define DMA_HISR_FEIF7_BIT           22
#define DMA_HISR_TCIF6_BIT           21
#define DMA_HISR_HTIF6_BIT           20
#define DMA_HISR_TEIF6_BIT           19
#define DMA_HISR_DMEIF6_BIT          18
#define DMA_HISR_FEIF6_BIT           16
#define DMA_HISR_TCIF5_BIT           11
#define DMA_HISR_HTIF5_BIT           10
#define DMA_HISR_TEIF5_BIT            9
#define DMA_HISR_DMEIF5_BIT           8
#define DMA_HISR_FEIF5_BIT            6
#define DMA_HISR_TCIF4_BIT            5
#define DMA_HISR_HTIF4_BIT            4
#define DMA_HISR_TEIF4_BIT            3
#define DMA_HISR_DMEIF4_BIT           2
#define DMA_HISR_FEIF4_BIT            0

#define DMA_HISR_TCIF7               (1U << DMA_HISR_TCIF7_BIT)
#define DMA_HISR_HTIF7               (1U << DMA_HISR_HTIF7_BIT)
#define DMA_HISR_TEIF7               (1U << DMA_HISR_TEIF7_BIT)
#define DMA_HISR_DMEIF7              (1U << DMA_HISR_DMEIF7_BIT)
#define DMA_HISR_FEIF7               (1U << DMA_HISR_FEIF7_BIT)
#define DMA_HISR_TCIF6               (1U << DMA_HISR_TCIF6_BIT)
#define DMA_HISR_HTIF6               (1U << DMA_HISR_HTIF6_BIT)
#define DMA_HISR_TEIF6               (1U << DMA_HISR_TEIF6_BIT)
#define DMA_HISR_DMEIF6              (1U << DMA_HISR_DMEIF6_BIT)
#define DMA_HISR_FEIF6               (1U << DMA_HISR_FEIF6_BIT)
#define DMA_HISR_TCIF5               (1U << DMA_HISR_TCIF5_BIT)
#define DMA_HISR_HTIF5               (1U << DMA_HISR_HTIF5_BIT)
#define DMA_HISR_TEIF5               (1U << DMA_HISR_TEIF5_BIT)
#define DMA_HISR_DMEIF5              (1U << DMA_HISR_DMEIF5_BIT)
#define DMA_HISR_FEIF5               (1U << DMA_HISR_FEIF5_BIT)
#define DMA_HISR_TCIF4               (1U << DMA_HISR_TCIF4_BIT)
#define DMA_HISR_HTIF4               (1U << DMA_HISR_HTIF4_BIT)
#define DMA_HISR_TEIF4               (1U << DMA_HISR_TEIF4_BIT)
#define DMA_HISR_DMEIF4              (1U << DMA_HISR_DMEIF4_BIT)
#define DMA_HISR_FEIF4               (1U << DMA_HISR_FEIF4_BIT)

/* Low interrupt flag clear register */

#define DMA_LIFCR_CTCIF3_BIT             27
#define DMA_LIFCR_CHTIF3_BIT             26
#define DMA_LIFCR_CTEIF3_BIT             25
#define DMA_LIFCR_CDMEIF3_BIT            24
#define DMA_LIFCR_CFEIF3_BIT             22
#define DMA_LIFCR_CTCIF2_BIT             21
#define DMA_LIFCR_CHTIF2_BIT             20
#define DMA_LIFCR_CTEIF2_BIT             19
#define DMA_LIFCR_CDMEIF2_BIT            18
#define DMA_LIFCR_CFEIF2_BIT             16
#define DMA_LIFCR_CTCIF1_BIT             11
#define DMA_LIFCR_CHTIF1_BIT             10
#define DMA_LIFCR_CTEIF1_BIT              9
#define DMA_LIFCR_CDMEIF1_BIT             8
#define DMA_LIFCR_CFEIF1_BIT              6
#define DMA_LIFCR_CTCIF0_BIT              5
#define DMA_LIFCR_CHTIF0_BIT              4
#define DMA_LIFCR_CTEIF0_BIT              3
#define DMA_LIFCR_CDMEIF0_BIT             2
#define DMA_LIFCR_CFEIF0_BIT              0

#define DMA_LIFCR_CTCIF3                 (1U << DMA_LIFCR_CTCIF3_BIT)
#define DMA_LIFCR_CHTIF3                 (1U << DMA_LIFCR_CHTIF3_BIT)
#define DMA_LIFCR_CTEIF3                 (1U << DMA_LIFCR_CTEIF3_BIT)
#define DMA_LIFCR_CDMEIF3                (1U << DMA_LIFCR_CDMEIF3_BIT)
#define DMA_LIFCR_CFEIF3                 (1U << DMA_LIFCR_CFEIF3_BIT)
#define DMA_LIFCR_CTCIF2                 (1U << DMA_LIFCR_CTCIF2_BIT)
#define DMA_LIFCR_CHTIF2                 (1U << DMA_LIFCR_CHTIF2_BIT)
#define DMA_LIFCR_CTEIF2                 (1U << DMA_LIFCR_CTEIF2_BIT)
#define DMA_LIFCR_CDMEIF2                (1U << DMA_LIFCR_CDMEIF2_BIT)
#define DMA_LIFCR_CFEIF2                 (1U << DMA_LIFCR_CFEIF2_BIT)
#define DMA_LIFCR_CTCIF1                 (1U << DMA_LIFCR_CTCIF1_BIT)
#define DMA_LIFCR_CHTIF1                 (1U << DMA_LIFCR_CHTIF1_BIT)
#define DMA_LIFCR_CTEIF1                 (1U << DMA_LIFCR_CTEIF1_BIT)
#define DMA_LIFCR_CDMEIF1                (1U << DMA_LIFCR_CDMEIF1_BIT)
#define DMA_LIFCR_CFEIF1                 (1U << DMA_LIFCR_CFEIF1_BIT)
#define DMA_LIFCR_CTCIF0                 (1U << DMA_LIFCR_CTCIF0_BIT)
#define DMA_LIFCR_CHTIF0                 (1U << DMA_LIFCR_CHTIF0_BIT)
#define DMA_LIFCR_CTEIF0                 (1U << DMA_LIFCR_CTEIF0_BIT)
#define DMA_LIFCR_CDMEIF0                (1U << DMA_LIFCR_CDMEIF0_BIT)
#define DMA_LIFCR_CFEIF0                 (1U << DMA_LIFCR_CFEIF0_BIT)

/* High interrupt flag clear regsister */

#define DMA_HIFCR_CTCIF7_BIT             27
#define DMA_HIFCR_CHTIF7_BIT             26
#define DMA_HIFCR_CTEIF7_BIT             25
#define DMA_HIFCR_CDMEIF7_BIT            24
#define DMA_HIFCR_CFEIF7_BIT             22
#define DMA_HIFCR_CTCIF6_BIT             21
#define DMA_HIFCR_CHTIF6_BIT             20
#define DMA_HIFCR_CTEIF6_BIT             19
#define DMA_HIFCR_CDMEIF6_BIT            18
#define DMA_HIFCR_CFEIF6_BIT             16
#define DMA_HIFCR_CTCIF5_BIT             11
#define DMA_HIFCR_CHTIF5_BIT             10
#define DMA_HIFCR_CTEIF5_BIT              9
#define DMA_HIFCR_CDMEIF5_BIT             8
#define DMA_HIFCR_CFEIF5_BIT              6
#define DMA_HIFCR_CTCIF4_BIT              5
#define DMA_HIFCR_CHTIF4_BIT              4
#define DMA_HIFCR_CTEIF4_BIT              3
#define DMA_HIFCR_CDMEIF4_BIT             2
#define DMA_HIFCR_CFEIF4_BIT              0

#define DMA_HIFCR_CTCIF7                 (1U << DMA_HIFCR_CTCIF7_BIT)
#define DMA_HIFCR_CHTIF7                 (1U << DMA_HIFCR_CHTIF7_BIT)
#define DMA_HIFCR_CTEIF7                 (1U << DMA_HIFCR_CTEIF7_BIT)
#define DMA_HIFCR_CDMEIF7                (1U << DMA_HIFCR_CDMEIF7_BIT)
#define DMA_HIFCR_CFEIF7                 (1U << DMA_HIFCR_CFEIF7_BIT)
#define DMA_HIFCR_CTCIF6                 (1U << DMA_HIFCR_CTCIF6_BIT)
#define DMA_HIFCR_CHTIF6                 (1U << DMA_HIFCR_CHTIF6_BIT)
#define DMA_HIFCR_CTEIF6                 (1U << DMA_HIFCR_CTEIF6_BIT)
#define DMA_HIFCR_CDMEIF6                (1U << DMA_HIFCR_CDMEIF6_BIT)
#define DMA_HIFCR_CFEIF6                 (1U << DMA_HIFCR_CFEIF6_BIT)
#define DMA_HIFCR_CTCIF5                 (1U << DMA_HIFCR_CTCIF5_BIT)
#define DMA_HIFCR_CHTIF5                 (1U << DMA_HIFCR_CHTIF5_BIT)
#define DMA_HIFCR_CTEIF5                 (1U << DMA_HIFCR_CTEIF5_BIT)
#define DMA_HIFCR_CDMEIF5                (1U << DMA_HIFCR_CDMEIF5_BIT)
#define DMA_HIFCR_CFEIF5                 (1U << DMA_HIFCR_CFEIF5_BIT)
#define DMA_HIFCR_CTCIF4                 (1U << DMA_HIFCR_CTCIF4_BIT)
#define DMA_HIFCR_CHTIF4                 (1U << DMA_HIFCR_CHTIF4_BIT)
#define DMA_HIFCR_CTEIF4                 (1U << DMA_HIFCR_CTEIF4_BIT)
#define DMA_HIFCR_CDMEIF4                (1U << DMA_HIFCR_CDMEIF4_BIT)
#define DMA_HIFCR_CFEIF4                 (1U << DMA_HIFCR_CFEIF4_BIT)

/* Stream configuration register */

#define DMA_SCR_CT_BIT                   19
#define DMA_SCR_DBM_BIT                  18
#define DMA_SCR_PINCOS_BIT               15
#define DMA_SCR_MINC_BIT                 10
#define DMA_SCR_PINC_BIT                  9
#define DMA_SCR_CIRC_BIT                  8
#define DMA_SCR_PFCTRL_BIT                5
#define DMA_SCR_TCIE_BIT                  4
#define DMA_SCR_HTIE_BIT                  3
#define DMA_SCR_TEIE_BIT                  2
#define DMA_SCR_DMEIE_BIT                 1
#define DMA_SCR_EN_BIT                    0

#define DMA_SCR_CHSEL                    (0x7 << 25)
#define DMA_SCR_CHSEL_CH_0               (0x0 << 25)
#define DMA_SCR_CHSEL_CH_1               (0x1 << 25)
#define DMA_SCR_CHSEL_CH_2               (0x2 << 25)
#define DMA_SCR_CHSEL_CH_3               (0x3 << 25)
#define DMA_SCR_CHSEL_CH_4               (0x4 << 25)
#define DMA_SCR_CHSEL_CH_5               (0x5 << 25)
#define DMA_SCR_CHSEL_CH_6               (0x6 << 25)
#define DMA_SCR_CHSEL_CH_7               (0x7 << 25)
#define DMA_SCR_MBURST                   (0x3 << 23)
#define DMA_SCR_MBURST_SINGLE            (0x0 << 23)
#define DMA_SCR_MBURST_INCR4             (0x1 << 23)
#define DMA_SCR_MBURST_INCR8             (0x2 << 23)
#define DMA_SCR_MBURST_INCR16            (0x3 << 23)
#define DMA_SCR_PBURST                   (0x3 << 21)
#define DMA_SCR_PBURST_SINGLE            (0x0 << 21)
#define DMA_SCR_PBURST_INCR4             (0x1 << 21)
#define DMA_SCR_PBURST_INCR8             (0x2 << 21)
#define DMA_SCR_PBURST_INCR16            (0x3 << 21)
#define DMA_SCR_CT                       (1U << DMA_SCR_CT_BIT)
#define DMA_SCR_DBM                      (1U << DMA_SCR_DBM_BIT)
#define DMA_SCR_PL                       (0x3 << 16)
#define DMA_SCR_PL_LOW                   (0x0 << 16)
#define DMA_SCR_PL_MEDIUM                (0x1 << 16)
#define DMA_SCR_PL_HIGH                  (0x2 << 16)
#define DMA_SCR_VERY_HIGH                (0x3 << 16)
#define DMA_SCR_PINCOS                   (1U << DMA_SCR_PINCOS_BIT)
#define DMA_SCR_MSIZE                    (0x3 << 13)
#define DMA_SCR_MSIZE_8BITS              (0x0 << 13)
#define DMA_SCR_MSIZE_16BITS             (0x1 << 13)
#define DMA_SCR_MSIZE_32BITS             (0x2 << 13)
#define DMA_SCR_PSIZE                    (0x3 << 11)
#define DMA_SCR_PSIZE_8BITS              (0x0 << 11)
#define DMA_SCR_PSIZE_16BITS             (0x1 << 11)
#define DMA_SCR_PSIZE_32BITS             (0x2 << 11)
#define DMA_SCR_MINC                     (1U << DMA_SCR_MINC_BIT)
#define DMA_SCR_PINC                     (1U << DMA_SCR_PINC_BIT)
#define DMA_SCR_CIRC                     (1U << DMA_SCR_CIRC_BIT)
#define DMA_SCR_DIR                      (0x3 << 6)
#define DMA_SCR_DIR_PER_TO_MEM           (0x0 << 6)
#define DMA_SCR_DIR_MEM_TO_PER           (0x1 << 6)
#define DMA_SCR_DIR_MEM_TO_MEM           (0x2 << 6)
#define DMA_SCR_PFCTRL                   (1U << DMA_SCR_PFCTRL_BIT)
#define DMA_SCR_TCIE                     (1U << DMA_SCR_TCIE_BIT)
#define DMA_SCR_HTIE                     (1U << DMA_SCR_HTIE_BIT)
#define DMA_SCR_TEIE                     (1U << DMA_SCR_TEIE_BIT)
#define DMA_SCR_DMEIE                    (1U << DMA_SCR_DMEIE_BIT)
#define DMA_SCR_EN                       (1U << DMA_SCR_EN_BIT)

/* Stream FIFO control register */

#define DMA_SFCR_FEIE_BIT                            7
#define DMA_SFCR_DMDIS_BIT                           2

#define DMA_SFCR_FEIE                                (1U << DMA_SFCR_FEIE_BIT)
#define DMA_SFCR_FS                                  (0x7 << 3)
#define DMA_SFCR_FS_ZERO_TO_QUARTER                  (0x0 << 3)
#define DMA_SFCR_FS_QUARTER_TO_HALF                  (0x1 << 3)
#define DMA_SFCR_FS_HALF_TO_THREE_QUARTERS           (0x2 << 3)
#define DMA_SFCR_FS_THREE_QUARTERS_TO_FULL           (0x3 << 3)
#define DMA_SFCR_FS_EMPTY                            (0x4 << 3)
#define DMA_SFCR_FS_FULL                             (0x5 << 3)
#define DMA_SFCR_DMDIS                               (1U << DMA_SFCR_DMDIS_BIT)
#define DMA_SFCR_FTH                                 (0x3 << 0)
#define DMA_SFCR_FTH_QUARTER_FULL                    (0x0 << 3)
#define DMA_SFCR_FTH_HALF_FULL                       (0x1 << 3)
#define DMA_SFCR_FTH_THREE_QUARTERS_FULL             (0x2 << 3)
#define DMA_SFCR_FTH_FULL                            (0x3 << 3)

/*
 * Devices
 */

extern dma_dev *DMA1;
extern dma_dev *DMA2;

/*
 * Other types needed by, or useful for, <libmaple/dma.h>
 */

/**
 * @brief DMA streams
 * This is also the dma_tube type for STM32F2.
 * @see dma_tube
 */
typedef enum dma_stream {
    DMA_S0 = 0,
    DMA_S1 = 1,
    DMA_S2 = 2,
    DMA_S3 = 3,
    DMA_S4 = 4,
    DMA_S5 = 5,
    DMA_S6 = 6,
    DMA_S7 = 7,
} dma_stream;

/** STM32F2-F4 dma_tube (=dma_stream) */
#define dma_tube dma_stream

/**
 * @brief STM32F2-F4 configuration flags for dma_tube_config.
 * @see struct dma_tube_config
 */
typedef enum dma_cfg_flags {
    /* NB: flags that aren't SCR bits are treated specially. */

    /**
     * Source address increment mode
     *
     * If this flag is set, the source address is incremented (by the
     * source size) after each DMA transfer.
     */
    DMA_CFG_SRC_INC = 1U << 31,

    /**
     * Destination address increment mode
     *
     * If this flag is set, the destination address is incremented (by
     * the destination size) after each DMA transfer.
     */
    DMA_CFG_DST_INC = 1U << 30,

    /**
     * Circular mode
     *
     * This mode is not available for memory-to-memory transfers.
     */
    DMA_CFG_CIRC = DMA_SCR_CIRC,

    /** Transfer complete interrupt enable */
    DMA_CFG_CMPLT_IE      = DMA_SCR_TCIE,
    /** Transfer half-complete interrupt enable  */
    DMA_CFG_HALF_CMPLT_IE = DMA_SCR_HTIE,
    /** Transfer error interrupt enable */
    DMA_CFG_ERR_IE        = DMA_SCR_TEIE,
    /** Direct mode error interrupt enable */
    DMA_CFG_DM_ERR_IE     = DMA_SCR_DMEIE,
    /** FIFO error interrupt enable  */
    DMA_CFG_FIFO_ERR_IE   = (1U << 29),
} dma_cfg_flags;

/**
 * @brief STM32F2-F4 DMA request sources.
 *
 * IMPORTANT:
 *
 * 1. On STM32F2, a particular dma_request_src is always tied to a
 * single DMA controller, but often can be supported by multiple
 * streams. For example, DMA requests from ADC1 (DMA_REQ_SRC_ADC1) can
 * only be handled by DMA2, but they can go to either stream 0 or
 * stream 4 (though not any other stream). If you try to use a request
 * source with the wrong DMA controller or the wrong stream on
 * STM32F2, dma_tube_cfg() will fail.
 *
 * 2. A single stream can only handle a single request source at a
 * time. If you change a stream's request source later, it will stop
 * serving requests from the old source. However, for some streams,
 * some sources conflict with one another (when they correspond to the
 * same channel on that stream), and on STM32F2, Terrible Super-Bad
 * Things will happen if two conflicting request sources are active at
 * the same time.
 *
 * @see struct dma_tube_config
 * @see dma_tube_cfg()
 */
typedef enum dma_request_src {
    /* These are constructed like so (though this may change, so user
     * code shouldn't depend on it):
     *
     * Bits 0--2: Channel associated with request source
     *
     * Bits 3--9: rcc_clk_id of DMA controller associated with request source
     *
     * Bits 10--17: Bit mask of streams which can handle that request
     *              source.  (E.g., bit 10 set means stream 0 can
     *              handle the source, bit 11 set means stream 1 can,
     *              etc.)
     *
     * Among other things, this is used for error checking in
     * dma_tube_cfg(). If you change this bit encoding, you need to
     * update the helper functions in stm32f2/dma.c.
     */
#define _DMA_STM32F2_REQ_SRC(stream_mask, clk_id, channel) \
    (((stream_mask) << 10) | ((clk_id) << 3) | (channel))
#define _DMA_S(n) (1U << (n))

    /* DMA1 request sources */
#define _DMA_1_REQ_SRC(stream_mask, channel) \
    _DMA_STM32F2_REQ_SRC(stream_mask, RCC_DMA1, channel)

    /* Channel 0 */
    DMA_REQ_SRC_SPI3_RX = _DMA_1_REQ_SRC(_DMA_S(0) | _DMA_S(2), 0),
    DMA_REQ_SRC_SPI2_RX = _DMA_1_REQ_SRC(_DMA_S(3), 0),
    DMA_REQ_SRC_SPI2_TX = _DMA_1_REQ_SRC(_DMA_S(4), 0),
    DMA_REQ_SRC_SPI3_TX = _DMA_1_REQ_SRC(_DMA_S(5) | _DMA_S(7), 0),

    /* Channel 1 */
    DMA_REQ_SRC_I2C1_RX = _DMA_1_REQ_SRC(_DMA_S(0) | _DMA_S(5), 1),
    DMA_REQ_SRC_TIM7_UP = _DMA_1_REQ_SRC(_DMA_S(2) | _DMA_S(4), 1),
    DMA_REQ_SRC_I2C1_TX = _DMA_1_REQ_SRC(_DMA_S(6) | _DMA_S(7), 1),

    /* Channel 2 */
    DMA_REQ_SRC_TIM4_CH1 = _DMA_1_REQ_SRC(_DMA_S(0), 2),
    DMA_REQ_SRC_TIM4_CH2 = _DMA_1_REQ_SRC(_DMA_S(3), 2),
    DMA_REQ_SRC_TIM4_UP  = _DMA_1_REQ_SRC(_DMA_S(6), 2),
    DMA_REQ_SRC_TIM4_CH3 = _DMA_1_REQ_SRC(_DMA_S(7), 2),

    /* Channel 3 */
    DMA_REQ_SRC_TIM2_UP  = _DMA_1_REQ_SRC(_DMA_S(1) | _DMA_S(7), 3),
    DMA_REQ_SRC_TIM2_CH3 = _DMA_1_REQ_SRC(_DMA_S(1), 3),
    DMA_REQ_SRC_I2C3_RX  = _DMA_1_REQ_SRC(_DMA_S(2), 3),
    DMA_REQ_SRC_I2C3_TX  = _DMA_1_REQ_SRC(_DMA_S(4), 3),
    DMA_REQ_SRC_TIM2_CH1 = _DMA_1_REQ_SRC(_DMA_S(5), 3),
    DMA_REQ_SRC_TIM2_CH2 = _DMA_1_REQ_SRC(_DMA_S(6), 3),
    DMA_REQ_SRC_TIM2_CH4 = _DMA_1_REQ_SRC(_DMA_S(6) | _DMA_S(7), 3),

    /* Channel 4 */
    DMA_REQ_SRC_UART5_RX  = _DMA_1_REQ_SRC(_DMA_S(0), 4),
    DMA_REQ_SRC_USART3_RX = _DMA_1_REQ_SRC(_DMA_S(1), 4),
    DMA_REQ_SRC_UART4_RX  = _DMA_1_REQ_SRC(_DMA_S(2), 4),
    DMA_REQ_SRC_USART3_TX = _DMA_1_REQ_SRC(_DMA_S(3), 4),
    DMA_REQ_SRC_UART4_TX  = _DMA_1_REQ_SRC(_DMA_S(4), 4),
    DMA_REQ_SRC_USART2_RX = _DMA_1_REQ_SRC(_DMA_S(5), 4),
    DMA_REQ_SRC_USART2_TX = _DMA_1_REQ_SRC(_DMA_S(6), 4),
    DMA_REQ_SRC_UART5_TX  = _DMA_1_REQ_SRC(_DMA_S(7), 4),

    /* Channel 5 */
    DMA_REQ_SRC_TIM3_CH4  = _DMA_1_REQ_SRC(_DMA_S(2), 5),
    DMA_REQ_SRC_TIM3_UP   = _DMA_1_REQ_SRC(_DMA_S(2), 5),
    DMA_REQ_SRC_TIM3_CH1  = _DMA_1_REQ_SRC(_DMA_S(4), 5),
    DMA_REQ_SRC_TIM3_TRIG = _DMA_1_REQ_SRC(_DMA_S(4), 5),
    DMA_REQ_SRC_TIM3_CH2  = _DMA_1_REQ_SRC(_DMA_S(5), 5),
    DMA_REQ_SRC_TIM3_CH3  = _DMA_1_REQ_SRC(_DMA_S(7), 5),

    /* Channel 6 */
    DMA_REQ_SRC_TIM5_CH3  = _DMA_1_REQ_SRC(_DMA_S(0), 6),
    DMA_REQ_SRC_TIM5_UP   = _DMA_1_REQ_SRC(_DMA_S(0) | _DMA_S(6), 6),
    DMA_REQ_SRC_TIM5_CH4  = _DMA_1_REQ_SRC(_DMA_S(1) | _DMA_S(3), 6),
    DMA_REQ_SRC_TIM5_TRIG = _DMA_1_REQ_SRC(_DMA_S(1) | _DMA_S(3), 6),
    DMA_REQ_SRC_TIM5_CH1  = _DMA_1_REQ_SRC(_DMA_S(2), 6),
    DMA_REQ_SRC_TIM5_CH2  = _DMA_1_REQ_SRC(_DMA_S(4), 6),

    /* Channel 7 */
    DMA_REQ_SRC_TIM6_UP   = _DMA_1_REQ_SRC(_DMA_S(1), 7),
    DMA_REQ_SRC_I2C2_RX   = _DMA_1_REQ_SRC(_DMA_S(2) | _DMA_S(3), 7),
    DMA_REQ_SRC_USART3_TX_ALTERNATE = _DMA_1_REQ_SRC(_DMA_S(4), 7),
    DMA_REQ_SRC_DAC1      = _DMA_1_REQ_SRC(_DMA_S(5), 7),
    DMA_REQ_SRC_DAC2      = _DMA_1_REQ_SRC(_DMA_S(6), 7),
    DMA_REQ_SRC_I2C2_TX   = _DMA_1_REQ_SRC(_DMA_S(7), 7),
#undef _DMA_1_REQ_SRC

    /* DMA2 request sources */
#define _DMA_2_REQ_SRC(stream_mask, channel) \
    _DMA_STM32F2_REQ_SRC(stream_mask, RCC_DMA2, channel)

    /* Channel 0 */
    DMA_REQ_SRC_ADC1     = _DMA_2_REQ_SRC(_DMA_S(0) | _DMA_S(4), 0),
    /* You can use these "DMA_REQ_SRC_TIMx_CHx_ALTERNATE" if you know
     * what you're doing, but the other ones (for channels 6 and 7),
     * are better, in that they don't conflict with one another. */
    DMA_REQ_SRC_TIM8_CH1_ALTERNATE = _DMA_2_REQ_SRC(_DMA_S(2), 0),
    DMA_REQ_SRC_TIM8_CH2_ALTERNATE = _DMA_2_REQ_SRC(_DMA_S(2), 0),
    DMA_REQ_SRC_TIM8_CH3_ALTERNATE = _DMA_2_REQ_SRC(_DMA_S(2), 0),
    DMA_REQ_SRC_TIM1_CH1_ALTERNATE = _DMA_2_REQ_SRC(_DMA_S(6), 0),
    DMA_REQ_SRC_TIM1_CH2_ALTERNATE = _DMA_2_REQ_SRC(_DMA_S(6), 0),
    DMA_REQ_SRC_TIM1_CH3_ALTENRATE = _DMA_2_REQ_SRC(_DMA_S(6), 0),

    /* Channel 1 */
    DMA_REQ_SRC_DCMI = _DMA_2_REQ_SRC(_DMA_S(1) | _DMA_S(7), 1),
    DMA_REQ_SRC_ADC2 = _DMA_2_REQ_SRC(_DMA_S(2) | _DMA_S(3), 1),

    /* Channel 2 */
    DMA_REQ_SRC_ADC3     = _DMA_2_REQ_SRC(_DMA_S(0) | _DMA_S(1), 2),
    DMA_REQ_SRC_CRYP_OUT = _DMA_2_REQ_SRC(_DMA_S(5), 2),
    DMA_REQ_SRC_CRYP_IN  = _DMA_2_REQ_SRC(_DMA_S(6), 2),
    DMA_REQ_SRC_HASH_IN  = _DMA_2_REQ_SRC(_DMA_S(7), 2),

    /* Channel 3 */
    DMA_REQ_SRC_SPI1_RX = _DMA_2_REQ_SRC(_DMA_S(0) | _DMA_S(2), 3),
    DMA_REQ_SRC_SPI1_TX = _DMA_2_REQ_SRC(_DMA_S(3) | _DMA_S(5), 3),

    /* Channel 4 */
    DMA_REQ_SRC_USART1_RX = _DMA_2_REQ_SRC(_DMA_S(2) | _DMA_S(5), 4),
    DMA_REQ_SRC_SDIO      = _DMA_2_REQ_SRC(_DMA_S(3) | _DMA_S(6), 4),
    DMA_REQ_SRC_USART1_TX = _DMA_2_REQ_SRC(_DMA_S(7), 4),

    /* Channel 5 */
    DMA_REQ_SRC_USART6_RX = _DMA_2_REQ_SRC(_DMA_S(1) | _DMA_S(2), 5),
    DMA_REQ_SRC_USART6_TX = _DMA_2_REQ_SRC(_DMA_S(6) | _DMA_S(7), 5),

    /* Channel 6 */
    DMA_REQ_SRC_TIM1_TRIG = _DMA_2_REQ_SRC(_DMA_S(0) | _DMA_S(4), 6),
    DMA_REQ_SRC_TIM1_CH1  = _DMA_2_REQ_SRC(_DMA_S(1) | _DMA_S(3), 6),
    DMA_REQ_SRC_TIM1_CH2  = _DMA_2_REQ_SRC(_DMA_S(3), 6),
    DMA_REQ_SRC_TIM1_CH4  = _DMA_2_REQ_SRC(_DMA_S(4), 6),
    DMA_REQ_SRC_TIM1_COM  = _DMA_2_REQ_SRC(_DMA_S(4), 6),
    DMA_REQ_SRC_TIM1_UP   = _DMA_2_REQ_SRC(_DMA_S(5), 6),
    DMA_REQ_SRC_TIM1_CH3  = _DMA_2_REQ_SRC(_DMA_S(6), 6),

    /* Channel 7 */
    DMA_REQ_SRC_TIM8_UP   = _DMA_2_REQ_SRC(_DMA_S(1), 7),
    DMA_REQ_SRC_TIM8_CH1  = _DMA_2_REQ_SRC(_DMA_S(2), 7),
    DMA_REQ_SRC_TIM8_CH2  = _DMA_2_REQ_SRC(_DMA_S(3), 7),
    DMA_REQ_SRC_TIM8_CH3  = _DMA_2_REQ_SRC(_DMA_S(4), 7),
    DMA_REQ_SRC_TIM8_CH4  = _DMA_2_REQ_SRC(_DMA_S(7), 7),
    DMA_REQ_SRC_TIM8_TRIG = _DMA_2_REQ_SRC(_DMA_S(7), 7),
    DMA_REQ_SRC_TIM8_COM  = _DMA_2_REQ_SRC(_DMA_S(7), 7),
#undef _DMA_2_REQ_SRC
#undef _DMA_S
} dma_request_src;

/*
 * Tube conveniences
 */

static inline dma_tube_reg_map* dma_tube_regs(dma_dev *dev,
                                              dma_tube tube) {
    ASSERT(DMA_S0 <= tube && tube <= DMA_S7);
    switch (dev->clk_id) {
    case RCC_DMA1:
        return DMA1S0_BASE + (int)tube;
    case RCC_DMA2:
        return DMA2S0_BASE + (int)tube;
    default:
        /* Can't happen */
        ASSERT(0);
        return 0;
    }
}

static inline uint8 dma_is_enabled(dma_dev *dev, dma_tube tube) {
    return dma_tube_regs(dev, tube)->SCR & DMA_SCR_EN;
}

/* F2-only; available because of double-buffering. */
void dma_set_mem_n_addr(dma_dev *dev, dma_tube tube, int n,
                        __io void *address);

/**
 * @brief Set memory 0 address.
 * Availability: STM32F2.
 *
 * @param dev DMA device
 * @param tube Tube whose memory 0 address to set
 * @param addr Address to use as memory 0
 */
static __always_inline void
dma_set_mem0_addr(dma_dev *dev, dma_tube tube, __io void *addr) {
    dma_set_mem_n_addr(dev, tube, 0, addr);
}

/**
 * @brief Set memory 1 address.
 * Availability: STM32F2.
 *
 * @param dev DMA device
 * @param tube Tube whose memory 1 address to set
 * @param addr Address to use as memory 1
 */
static __always_inline void
dma_set_mem1_addr(dma_dev *dev, dma_tube tube, __io void *addr) {
    dma_set_mem_n_addr(dev, tube, 1, addr);
}

/* Assume the user means SM0AR in a non-double-buffered configuration. */
static __always_inline void
dma_set_mem_addr(dma_dev *dev, dma_tube tube, __io void *addr) {
    dma_set_mem0_addr(dev, tube, addr);
}

/* SM0AR and SM1AR are treated as though they have the same size */
static inline dma_xfer_size dma_get_mem_size(dma_dev *dev, dma_tube tube) {
    return (dma_xfer_size)(dma_tube_regs(dev, tube)->SCR >> 13);
}

static inline dma_xfer_size dma_get_per_size(dma_dev *dev, dma_tube tube) {
    return (dma_xfer_size)(dma_tube_regs(dev, tube)->SCR >> 11);
}

void dma_enable_fifo(dma_dev *dev, dma_tube tube);
void dma_disable_fifo(dma_dev *dev, dma_tube tube);

static __always_inline int dma_is_fifo_enabled(dma_dev *dev, dma_tube tube) {
    return dma_tube_regs(dev, tube)->SFCR & DMA_SFCR_DMDIS;
}

/*
 * TODO:
 * - Double-buffer configuration function
 * - FIFO configuration function
 * - MBURST/PBURST configuration function
 */

/*
 * ISR/IFCR conveniences.
 */

/* (undocumented) helper for reading LISR/HISR and writing
 * LIFCR/HIFCR. For these registers,
 *
 * S0, S4:  bits start at bit 0
 * S1, S5:                    6
 * S2, S6:                   16
 * S3, S7:                   22
 *
 * I can't imagine why ST didn't just use a byte for each group. The
 * bits fit, and it would have made functions like these simpler and
 * faster.  Oh well. */
static __always_inline uint32 _dma_sr_fcr_shift(dma_tube tube) {
    switch (tube) {
    case DMA_S0:                /* fall through */
    case DMA_S4:
        return 0;
    case DMA_S1:                /* fall through */
    case DMA_S5:
        return 6;
    case DMA_S2:                /* fall through */
    case DMA_S6:
        return 16;
    case DMA_S3:                /* fall through */
    case DMA_S7:
        return 22;
    }
    /* Can't happen */
    ASSERT(0);
    return 0;
}

static inline uint8 dma_get_isr_bits(dma_dev *dev, dma_tube tube) {
    dma_reg_map *regs = dev->regs;
    __io uint32 *isr = tube > DMA_S3 ? &regs->HISR : &regs->LISR;
    return (*isr >> _dma_sr_fcr_shift(tube)) & 0x3D;
}

static inline void dma_clear_isr_bits(dma_dev *dev, dma_tube tube) {
    dma_reg_map *regs = dev->regs;
    __io uint32 *ifcr = tube > DMA_S3 ? &regs->HIFCR : &regs->LIFCR;
    *ifcr = (0x3D << _dma_sr_fcr_shift(tube));
}

#undef _DMA_IRQ_BIT_SHIFT

#ifdef __cplusplus
} // extern "C"
#endif

#endif