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
//! DMA abstractions
use core::mem::size_of;
use core::ops::Not;
use rcc::AHB1;
/// DMA channel, implemented by the types `C0`, `C1`, `C2`, …
pub trait DmaChannel {
/// Numeric channel number
fn channel() -> u8;
}
/// DMA channel
pub struct C0;
impl DmaChannel for C0 {
fn channel() -> u8 { 0 }
}
/// DMA channel
pub struct C1;
impl DmaChannel for C1 {
fn channel() -> u8 { 1 }
}
/// DMA channel
pub struct C2;
impl DmaChannel for C2 {
fn channel() -> u8 { 2 }
}
/// DMA channel
pub struct C3;
impl DmaChannel for C3 {
fn channel() -> u8 { 3 }
}
/// DMA channel
pub struct C4;
impl DmaChannel for C4 {
fn channel() -> u8 { 4 }
}
/// DMA channel
pub struct C5;
impl DmaChannel for C5 {
fn channel() -> u8 { 5 }
}
/// DMA channel
pub struct C6;
impl DmaChannel for C6 {
fn channel() -> u8 { 6 }
}
/// DMA channel
pub struct C7;
impl DmaChannel for C7 {
fn channel() -> u8 { 7 }
}
/// Split the DMA device into separate streams.
pub trait DmaExt {
/// Target type
type Streams;
/// Split into separate streams.
fn split(self, ahb: &mut AHB1) -> Self::Streams;
}
/// Events to enable interrupts for.
pub enum Event {
/// Half transfer
HalfTransfer,
/// Transfer complete
TransferComplete,
}
#[derive(Debug, Clone, Copy)]
enum DoubleBuffer {
Memory0 = 0,
Memory1 = 1,
}
impl Not for DoubleBuffer {
type Output = Self;
fn not(self) -> Self::Output {
match self {
DoubleBuffer::Memory0 =>
DoubleBuffer::Memory1,
DoubleBuffer::Memory1 =>
DoubleBuffer::Memory0,
}
}
}
/// DMA stream peripheral
pub trait DmaStream {
/// Enable interrupt
fn listen(&mut self, event: Event);
/// Disable interrupt
fn unlisten(&mut self, event: Event);
/// Transfer is complete?
fn is_complete(&self) -> bool;
/// Transfer has error?
fn has_error(&self) -> bool;
/// Reset after a transfer
fn reset(&mut self);
}
/// DMA stream that can start DMA transfer `X`
pub trait DmaStreamTransfer<S, T, X: Transfer<Self>>: DmaStream + Sized {
/// Start DMA transfer
fn start_transfer<CHANNEL: DmaChannel>(self, source: S, target: &mut T) -> X;
}
/// DMA transfer
pub trait Transfer<STREAM>: Sized {
/// Transfer is complete?
fn is_complete(&self) -> bool;
/// Transfer has error?
fn has_error(&self) -> bool;
/// Reset after a transfer
///
/// Consumes the finished transfer and returns the stream.
fn reset(self) -> STREAM;
/// Wait until transfer is either complete or has error.
fn wait(self) -> Result<STREAM, STREAM> {
while !self.is_complete() && !self.has_error() {}
if self.is_complete() {
Ok(self.reset())
} else {
Err(self.reset())
}
}
}
macro_rules! dma {
($($DMAX:ident: ($dmaX:ident, $dmaXen:ident, $dmaXrst:ident, {
$($SX:ident: (
$sx:ident,
$crX:ident: $CRX:ident,
$ndtrX:ident: $NDTRX:ident,
$parX:ident: $PARX:ident,
$m0arX:ident: $M0ARX:ident,
$m1arX:ident: $M1ARX:ident,
$isr:ident: $ISR:ident,
$ifcr:ident: $IFCR:ident,
$tcif:ident, $teif:ident,
$ctcif:ident, $cteif:ident,
),)+
}),)+) => {
$(
/// Peripheral abstraction for DMA
pub mod $dmaX {
use stm32f429::{$DMAX, dma2};
use rcc::AHB1;
use dma::{DmaExt, DmaStream, DmaStreamTransfer, DmaChannel,
Event, data_size};
/// The numbered DMA streams of a device that you can
/// use separately.
#[derive(Debug)]
pub struct Streams {
$(
/// DMA stream `$sx`
pub $sx: $SX
),+
}
$(
/// A handle to the `$SX` DMA peripheral
#[derive(Debug)]
pub struct $SX { _0: () }
impl $SX {
fn isr(&self) -> dma2::$isr::R {
// NOTE(unsafe) atomic read with no side effects
unsafe { (*$DMAX::ptr()).$isr.read() }
}
fn ifcr(&self) -> &dma2::$IFCR {
unsafe { &(*$DMAX::ptr()).$ifcr }
}
fn cr(&mut self) -> &dma2::$CRX {
unsafe { &(*$DMAX::ptr()).$crX }
}
fn ndtr(&mut self) -> &dma2::$NDTRX {
unsafe { &(*$DMAX::ptr()).$ndtrX }
}
// fn get_ndtr(&self) -> u32 {
// // NOTE(unsafe) atomic read with no side effects
// unsafe { (*$DMAX::ptr()).$ndtrX.read().bits() }
// }
fn par(&mut self) -> &dma2::$PARX {
unsafe { &(*$DMAX::ptr()).$parX }
}
fn m0ar(&mut self) -> &dma2::$M0ARX {
unsafe { &(*$DMAX::ptr()).$m0arX }
}
fn m1ar(&mut self) -> &dma2::$M1ARX {
unsafe { &(*$DMAX::ptr()).$m1arX }
}
}
impl DmaStream for $SX {
fn listen(&mut self, event: Event) {
match event {
Event::HalfTransfer => self.cr().modify(|_, w| w.htie().set_bit()),
Event::TransferComplete => {
self.cr().modify(|_, w| w.tcie().set_bit())
}
}
}
fn unlisten(&mut self, event: Event) {
match event {
Event::HalfTransfer => {
self.cr().modify(|_, w| w.htie().clear_bit())
},
Event::TransferComplete => {
self.cr().modify(|_, w| w.tcie().clear_bit())
}
}
}
fn is_complete(&self) -> bool {
self.isr().$tcif().bit()
}
fn has_error(&self) -> bool {
self.isr().$teif().bit()
}
fn reset(&mut self) {
// Disable Stream
self.cr().modify(|_, w| w.en().clear_bit());
// Clear status bits
self.ifcr().modify(|_, w| {
w.$ctcif().set_bit()
.$cteif().set_bit()
});
}
}
impl<'s, S> DmaStreamTransfer<(&'s [S], &'s [S]), S, $sx::DoubleBufferedTransfer<S>> for $SX {
/// Configure, enable, and return a double-buffered DMA transfer.
fn start_transfer<CHANNEL: DmaChannel>(mut self, (source0, source1): (&'s [S], &'s [S]), target: &mut S) -> $sx::DoubleBufferedTransfer<S> {
assert_eq!(source0.len(), source1.len());
self.cr().modify(|_, w| unsafe {
w.msize().bits(data_size::<S>())
.minc().set_bit()
.psize().bits(data_size::<S>())
.pinc().clear_bit()
.dbm().set_bit()
.ct().clear_bit()
.circ().set_bit()
// Memory to peripheral
.dir().bits(0b01)
.chsel().bits(CHANNEL::channel())
});
let source0_addr = &source0[0] as *const _ as u32;
self.m0ar().write(|w| unsafe { w.bits(source0_addr) });
let source1_addr = &source1[0] as *const _ as u32;
self.m1ar().write(|w| unsafe { w.bits(source1_addr) });
let source_len = source0.len() as u32;
self.ndtr().write(|w| unsafe { w.bits(source_len) });
let target_addr = target as *const _ as u32;
self.par().write(|w| unsafe { w.bits(target_addr) });
// Enable Stream
self.cr().modify(|_, w| w.en().set_bit());
$sx::DoubleBufferedTransfer::new(self)
}
}
impl<T, S: AsRef<[T]>> DmaStreamTransfer<S, T, $sx::OneShotTransfer<S>> for $SX {
/// Configure, enable, and return a double-buffered DMA transfer.
fn start_transfer<CHANNEL: DmaChannel>(mut self, source: S, target: &mut T) -> $sx::OneShotTransfer<S> {
self.cr().modify(|_, w| unsafe {
w.msize().bits(data_size::<T>())
.minc().set_bit()
.psize().bits(data_size::<T>())
.pinc().clear_bit()
.dbm().clear_bit()
.ct().clear_bit()
.circ().clear_bit()
// Memory to peripheral
.dir().bits(0b01)
.chsel().bits(CHANNEL::channel())
});
let source_addr = source.as_ref() as *const _ as *const () as u32;
self.m0ar().write(|w| unsafe { w.bits(source_addr) });
let source_len = source.as_ref().len() as u32;
self.ndtr().write(|w| unsafe { w.bits(source_len) });
let target_addr = target as *const _ as u32;
self.par().write(|w| unsafe { w.bits(target_addr) });
// Enable Stream
self.cr().modify(|_, w| w.en().set_bit());
$sx::OneShotTransfer::new(self, source)
}
}
/// Contains the `DoubleBufferedTransfer` and the `OneShotTransfer` for `$SX`
pub mod $sx {
use core::marker::PhantomData;
use dma::{DmaStream, Transfer, DoubleBuffer};
use super::$SX;
/// Double-buffered DMA transfer
pub struct DoubleBufferedTransfer<S> {
/// So that `poll()` can detect a buffer switch
sent: [bool; 2],
_source_el: PhantomData<S>,
stream: $SX,
}
impl<S> Transfer<$SX> for DoubleBufferedTransfer<S> {
fn is_complete(&self) -> bool {
self.stream.is_complete()
}
fn has_error(&self) -> bool {
self.stream.has_error()
}
fn reset(mut self) -> $SX {
self.stream.reset();
self.stream
}
}
impl<S> DoubleBufferedTransfer<S> {
/// Construct a new DMA transfer state,
/// returned by `start_transfer` which
/// configures and enables the stream
/// before.
pub fn new<'s>(stream: $SX) -> Self {
Self {
sent: [false; 2],
_source_el: PhantomData,
stream,
}
}
/// Return the index of the buffer currently being sent
#[inline]
fn front_buffer(&mut self) -> DoubleBuffer {
if self.stream.cr().read().ct().bit() {
DoubleBuffer::Memory1
} else {
DoubleBuffer::Memory0
}
}
/// Return the index of the buffer **not** currently being sent
#[inline]
fn back_buffer(&mut self) -> DoubleBuffer {
! self.front_buffer()
}
/// Has the back buffer been sent?
///
/// As this is used for polling, the
/// function updates the `sent` status of
/// the front buffer.
pub fn writable(&mut self) -> bool {
// Mark front buffer as being sent
self.sent[self.front_buffer() as usize] = true;
self.sent[self.back_buffer() as usize]
}
/// Update the back buffer.
pub fn write<'s>(&mut self, source: &'s [S]) -> Result<(), ()> {
if self.has_error() {
return Err(())
}
let source_addr = &source[0] as *const _ as u32;
let bb = self.back_buffer();
match bb {
DoubleBuffer::Memory0 =>
self.stream.m0ar().write(|w| unsafe { w.bits(source_addr) }),
DoubleBuffer::Memory1 =>
self.stream.m1ar().write(|w| unsafe { w.bits(source_addr) }),
}
// Let `writable()` mark it when it becomes the `front_buffer()`
self.sent[bb as usize] = false;
Ok(())
}
}
/// One-shot DMA transfer
pub struct OneShotTransfer<S> {
source: S,
stream: $SX,
}
impl<S> Transfer<$SX> for OneShotTransfer<S> {
fn is_complete(&self) -> bool {
self.stream.is_complete()
}
fn has_error(&self) -> bool {
self.stream.has_error()
}
fn reset(mut self) -> $SX {
drop(self.source);
self.stream.reset();
self.stream
}
}
impl<S> OneShotTransfer<S> {
/// Construct a new DMA transfer state,
/// returned by `start_transfer` which
/// configures and enables the stream
/// before.
pub fn new<'s>(stream: $SX, source: S) -> Self {
Self {
source,
stream,
}
}
/// debug
pub fn status(&mut self) -> u32 {
self.stream.cr().read().bits()
}
}
}
)+
impl DmaExt for $DMAX {
type Streams = Streams;
fn split(self, ahb: &mut AHB1) -> Streams {
ahb.enr().modify(|_, w| w.$dmaXen().set_bit());
ahb.rstr().modify(|_, w| w.$dmaXrst().set_bit());
ahb.rstr().modify(|_, w| w.$dmaXrst().clear_bit());
// reset the DMA control registers (stops all on-going transfers)
$(
self.$crX.reset();
)+
Streams {
$($sx: $SX { _0: () }),+
}
}
}
}
)+
}
}
dma! {
DMA1: (dma1, dma1en, dma1rst, {
S0: (
s0,
s0cr: S0CR,
s0ndtr: S0NDTR,
s0par: S0PAR,
s0m0ar: S0M0AR,
s0m1ar: S0M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif0, teif0,
ctcif0, cteif0,
),
S1: (
s1,
s1cr: S1CR,
s1ndtr: S1NDTR,
s1par: S1PAR,
s1m0ar: S1M0AR,
s1m1ar: S1M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif1, teif1,
ctcif1, cteif1,
),
S2: (
s2,
s2cr: S2CR,
s2ndtr: S2NDTR,
s2par: S2PAR,
s2m0ar: S2M0AR,
s2m1ar: S2M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif2, teif2,
ctcif2, cteif2,
),
S3: (
s3,
s3cr: S3CR,
s3ndtr: S3NDTR,
s3par: S3PAR,
s3m0ar: S3M0AR,
s3m1ar: S3M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif3, teif3,
ctcif3, cteif3,
),
S4: (
s4,
s4cr: S4CR,
s4ndtr: S4NDTR,
s4par: S4PAR,
s4m0ar: S4M0AR,
s4m1ar: S4M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif4, teif4,
ctcif4, cteif4,
),
S5: (
s5,
s5cr: S5CR,
s5ndtr: S5NDTR,
s5par: S5PAR,
s5m0ar: S5M0AR,
s5m1ar: S5M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif5, teif5,
ctcif5, cteif5,
),
S6: (
s6,
s6cr: S6CR,
s6ndtr: S6NDTR,
s6par: S6PAR,
s6m0ar: S6M0AR,
s6m1ar: S6M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif6, teif6,
ctcif6, cteif6,
),
S7: (
s7,
s7cr: S7CR,
s7ndtr: S7NDTR,
s7par: S7PAR,
s7m0ar: S7M0AR,
s7m1ar: S7M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif7, teif7,
ctcif7, cteif7,
),
}),
DMA2: (dma2, dma2en, dma2rst, {
S0: (
s0,
s0cr: S0CR,
s0ndtr: S0NDTR,
s0par: S0PAR,
s0m0ar: S0M0AR,
s0m1ar: S0M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif0, teif0,
ctcif0, cteif0,
),
S1: (
s1,
s1cr: S1CR,
s1ndtr: S1NDTR,
s1par: S1PAR,
s1m0ar: S1M0AR,
s1m1ar: S1M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif1, teif1,
ctcif1, cteif1,
),
S2: (
s2,
s2cr: S2CR,
s2ndtr: S2NDTR,
s2par: S2PAR,
s2m0ar: S2M0AR,
s2m1ar: S2M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif2, teif2,
ctcif2, cteif2,
),
S3: (
s3,
s3cr: S3CR,
s3ndtr: S3NDTR,
s3par: S3PAR,
s3m0ar: S3M0AR,
s3m1ar: S3M1AR,
lisr: LISR,
lifcr: LIFCR,
tcif3, teif3,
ctcif3, cteif3,
),
S4: (
s4,
s4cr: S4CR,
s4ndtr: S4NDTR,
s4par: S4PAR,
s4m0ar: S4M0AR,
s4m1ar: S4M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif4, teif4,
ctcif4, cteif4,
),
S5: (
s5,
s5cr: S5CR,
s5ndtr: S5NDTR,
s5par: S5PAR,
s5m0ar: S5M0AR,
s5m1ar: S5M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif5, teif5,
ctcif5, cteif5,
),
S6: (
s6,
s6cr: S6CR,
s6ndtr: S6NDTR,
s6par: S6PAR,
s6m0ar: S6M0AR,
s6m1ar: S6M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif6, teif6,
ctcif6, cteif6,
),
S7: (
s7,
s7cr: S7CR,
s7ndtr: S7NDTR,
s7par: S7PAR,
s7m0ar: S7M0AR,
s7m1ar: S7M1AR,
hisr: HISR,
hifcr: HIFCR,
tcif7, teif7,
ctcif7, cteif7,
),
}),
}
fn data_size<T>() -> u8 {
match size_of::<T>() {
1 => 0b00,
2 => 0b01,
4 => 0b10,
_ => panic!("No such data size"),
}
}