aboutsummaryrefslogtreecommitdiff
path: root/tests/mspm0/src/bin/dma.rs
blob: 9c56acadcf2974c486cd140b015f85290a20561a (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
#![no_std]
#![no_main]

#[cfg(feature = "mspm0g3507")]
teleprobe_meta::target!(b"lp-mspm0g3507");

#[cfg(feature = "mspm0g3519")]
teleprobe_meta::target!(b"lp-mspm0g3519");

use core::slice;

use defmt::{assert, assert_eq, *};
use embassy_executor::Spawner;
use embassy_mspm0::Peri;
use embassy_mspm0::dma::{Channel, Transfer, TransferMode, TransferOptions, Word};
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let mut p = embassy_mspm0::init(Default::default());
    info!("Hello World!");

    {
        info!("Single u8 read (blocking)");
        single_read(p.DMA_CH0.reborrow(), 0x41_u8);

        info!("Single u16 read (blocking)");
        single_read(p.DMA_CH0.reborrow(), 0xFF41_u16);

        info!("Single u32 read (blocking)");
        single_read(p.DMA_CH0.reborrow(), 0xFFEE_FF41_u32);

        info!("Single u64 read (blocking)");
        single_read(p.DMA_CH0.reborrow(), 0x0011_2233_FFEE_FF41_u64);
    }

    // Widening transfers
    {
        info!("Single u8 read to u16");
        widening_single_read::<u8, u16>(p.DMA_CH0.reborrow(), 0x41);

        info!("Single u8 read to u32");
        widening_single_read::<u8, u32>(p.DMA_CH0.reborrow(), 0x43);

        info!("Single u8 read to u64");
        widening_single_read::<u8, u64>(p.DMA_CH0.reborrow(), 0x47);

        info!("Single u16 read to u32");
        widening_single_read::<u16, u32>(p.DMA_CH0.reborrow(), 0xAE43);

        info!("Single u16 read to u64");
        widening_single_read::<u16, u64>(p.DMA_CH0.reborrow(), 0xAF47);

        info!("Single u32 read to u64");
        widening_single_read::<u32, u64>(p.DMA_CH0.reborrow(), 0xDEAD_AF47);
    }

    // Narrowing transfers.
    {
        info!("Single u16 read to u8");
        narrowing_single_read::<u16, u8>(p.DMA_CH0.reborrow(), 0x4142);

        info!("Single u32 read to u8");
        narrowing_single_read::<u32, u8>(p.DMA_CH0.reborrow(), 0x4142_2414);

        info!("Single u64 read to u8");
        narrowing_single_read::<u64, u8>(p.DMA_CH0.reborrow(), 0x4142_2414_5153_7776);

        info!("Single u32 read to u16");
        narrowing_single_read::<u32, u16>(p.DMA_CH0.reborrow(), 0x4142_2414);

        info!("Single u64 read to u16");
        narrowing_single_read::<u64, u16>(p.DMA_CH0.reborrow(), 0x4142_2414_5153_7776);

        info!("Single u64 read to u32");
        narrowing_single_read::<u64, u32>(p.DMA_CH0.reborrow(), 0x4142_2414_5153_7776);
    }

    {
        info!("Single u8 read (async)");
        async_single_read(p.DMA_CH0.reborrow(), 0x42_u8).await;

        info!("Single u16 read (async)");
        async_single_read(p.DMA_CH0.reborrow(), 0xAE42_u16).await;

        info!("Single u32 read (async)");
        async_single_read(p.DMA_CH0.reborrow(), 0xFE44_1500_u32).await;

        info!("Single u64 read (async)");
        async_single_read(p.DMA_CH0.reborrow(), 0x8F7F_6F5F_4F3F_2F1F_u64).await;
    }

    {
        info!("Multiple u8 reads (blocking)");
        block_read::<_, 16>(p.DMA_CH0.reborrow(), 0x98_u8);

        info!("Multiple u16 reads (blocking)");
        block_read::<_, 2>(p.DMA_CH0.reborrow(), 0x9801_u16);

        info!("Multiple u32 reads (blocking)");
        block_read::<_, 4>(p.DMA_CH0.reborrow(), 0x9821_9801_u32);

        info!("Multiple u64 reads (blocking)");
        block_read::<_, 4>(p.DMA_CH0.reborrow(), 0xABCD_EF01_2345_6789_u64);
    }

    {
        info!("Multiple u8 reads (async)");
        async_block_read::<_, 8>(p.DMA_CH0.reborrow(), 0x86_u8).await;

        info!("Multiple u16 reads (async)");
        async_block_read::<_, 6>(p.DMA_CH0.reborrow(), 0x7777_u16).await;

        info!("Multiple u32 reads (async)");
        async_block_read::<_, 3>(p.DMA_CH0.reborrow(), 0xA5A5_A5A5_u32).await;

        info!("Multiple u64 reads (async)");
        async_block_read::<_, 14>(p.DMA_CH0.reborrow(), 0x5A5A_5A5A_A5A5_A5A5_u64).await;
    }

    // Intentionally skip testing multiple reads in single transfer mode.
    //
    // If the destination length is greater than 1 and single transfer mode is used then two transfers
    // are performed in a trigger. Similarly with any other length of destination above 2, only 2 transfers
    // are performed. Issuing another trigger (resume) results in no further progress. More than likely
    // the test does not work due to some combination of a hardware bug and the datasheet being unclear
    // regarding what ends a software trigger.
    //
    // However this case works fine with a hardware trigger (such as the ADC hardware trigger).

    {
        info!("Single u8 write (blocking)");
        single_write(p.DMA_CH0.reborrow(), 0x41_u8);

        info!("Single u16 write (blocking)");
        single_write(p.DMA_CH0.reborrow(), 0x4142_u16);

        info!("Single u32 write (blocking)");
        single_write(p.DMA_CH0.reborrow(), 0x4142_4344_u32);

        info!("Single u64 write (blocking)");
        single_write(p.DMA_CH0.reborrow(), 0x4142_4344_4546_4748_u64);
    }

    {
        info!("Single u8 write (async)");
        async_single_write(p.DMA_CH0.reborrow(), 0xAA_u8).await;

        info!("Single u16 write (async)");
        async_single_write(p.DMA_CH0.reborrow(), 0xBBBB_u16).await;

        info!("Single u32 write (async)");
        async_single_write(p.DMA_CH0.reborrow(), 0xCCCC_CCCC_u32).await;

        info!("Single u64 write (async)");
        async_single_write(p.DMA_CH0.reborrow(), 0xDDDD_DDDD_DDDD_DDDD_u64).await;
    }

    {
        info!("Multiple u8 writes (blocking)");
        block_write(p.DMA_CH0.reborrow(), &[0xFF_u8, 0x7F, 0x3F, 0x1F]);

        info!("Multiple u16 writes (blocking)");
        block_write(p.DMA_CH0.reborrow(), &[0xFFFF_u16, 0xFF7F, 0xFF3F, 0xFF1F]);

        info!("Multiple u32 writes (blocking)");
        block_write(
            p.DMA_CH0.reborrow(),
            &[0xFF00_00FF_u32, 0xFF00_007F, 0x0000_FF3F, 0xFF1F_0000],
        );

        info!("Multiple u64 writes (blocking)");
        block_write(
            p.DMA_CH0.reborrow(),
            &[
                0xFF00_0000_0000_00FF_u64,
                0x0000_FF00_007F_0000,
                0x0000_FF3F_0000_0000,
                0xFF1F_0000_1111_837A,
            ],
        );
    }

    {
        info!("Multiple u8 writes (async)");
        async_block_write(p.DMA_CH0.reborrow(), &[0u8, 1, 2, 3]).await;

        info!("Multiple u16 writes (async)");
        async_block_write(p.DMA_CH0.reborrow(), &[0x9801u16, 0x9802, 0x9803, 0x9800, 0x9000]).await;

        info!("Multiple u32 writes (async)");
        async_block_write(p.DMA_CH0.reborrow(), &[0x9801_ABCDu32, 0xFFAC_9802, 0xDEAD_9803]).await;

        info!("Multiple u64 writes (async)");
        async_block_write(
            p.DMA_CH0.reborrow(),
            &[
                0xA55A_1111_3333_5555_u64,
                0x1111_A55A_3333_5555,
                0x5555_A55A_3333_1111,
                0x01234_5678_89AB_CDEF,
            ],
        )
        .await;
    }

    // TODO: Mixed byte and word transfers.

    info!("Test OK");
    cortex_m::asm::bkpt();
}

fn single_read<W: Word + Copy + Default + Eq + defmt::Format>(mut channel: Peri<'_, impl Channel>, mut src: W) {
    let options = TransferOptions::default();
    let mut dst = W::default();

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_read(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            &mut src,
            slice::from_mut(&mut dst),
            options,
        ))
    };
    transfer.blocking_wait();

    assert_eq!(src, dst);
}

async fn async_single_read<W: Word + Copy + Default + Eq + defmt::Format>(
    mut channel: Peri<'_, impl Channel>,
    mut src: W,
) {
    let options = TransferOptions::default();
    let mut dst = W::default();

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_read(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            &mut src,
            slice::from_mut(&mut dst),
            options,
        ))
    };
    transfer.await;

    assert_eq!(src, dst);
}

fn block_read<W: Word + Copy + Default + Eq + defmt::Format, const N: usize>(
    mut channel: Peri<'_, impl Channel>,
    mut src: W,
) {
    let mut options = TransferOptions::default();
    // Complete the entire transfer.
    options.mode = TransferMode::Block;

    let mut dst = [W::default(); N];

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_read(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            &mut src,
            &mut dst[..],
            options,
        ))
    };
    transfer.blocking_wait();

    assert_eq!(dst, [src; N]);
}

async fn async_block_read<W: Word + Copy + Default + Eq + defmt::Format, const N: usize>(
    mut channel: Peri<'_, impl Channel>,
    mut src: W,
) {
    let mut options = TransferOptions::default();
    // Complete the entire transfer.
    options.mode = TransferMode::Block;

    let mut dst = [W::default(); N];

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_read(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            &mut src,
            &mut dst[..],
            options,
        ))
    };
    transfer.await;

    assert_eq!(dst, [src; N]);
}

fn single_write<W: Word + Default + Eq + defmt::Format>(mut channel: Peri<'_, impl Channel>, src: W) {
    let options = TransferOptions::default();
    let mut dst = W::default();

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_write(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            slice::from_ref(&src),
            &mut dst,
            options,
        ))
    };
    transfer.blocking_wait();

    assert_eq!(src, dst);
}

async fn async_single_write<W: Word + Default + Eq + defmt::Format>(mut channel: Peri<'_, impl Channel>, src: W) {
    let options = TransferOptions::default();
    let mut dst = W::default();

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_write(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            slice::from_ref(&src),
            &mut dst,
            options,
        ))
    };
    transfer.await;

    assert_eq!(src, dst);
}

fn block_write<W: Word + Default + Eq + defmt::Format>(mut channel: Peri<'_, impl Channel>, src: &[W]) {
    let mut options = TransferOptions::default();
    // Complete the entire transfer.
    options.mode = TransferMode::Block;

    let mut dst = W::default();

    // Starting from 1 because a zero length transfer does nothing.
    for i in 1..src.len() {
        info!("-> {} write(s)", i);

        // SAFETY: src and dst outlive the transfer.
        let transfer = unsafe {
            unwrap!(Transfer::new_write(
                channel.reborrow(),
                Transfer::SOFTWARE_TRIGGER,
                &src[..i],
                &mut dst,
                options,
            ))
        };
        transfer.blocking_wait();

        // The result will be the last value written.
        assert_eq!(dst, src[i - 1]);
    }
}

async fn async_block_write<W: Word + Default + Eq + defmt::Format>(mut channel: Peri<'_, impl Channel>, src: &[W]) {
    let mut options = TransferOptions::default();
    // Complete the entire transfer.
    options.mode = TransferMode::Block;

    let mut dst = W::default();

    // Starting from 1 because a zero length transfer does nothing.
    for i in 1..src.len() {
        info!("-> {} write(s)", i);
        // SAFETY: src and dst outlive the transfer.
        let transfer = unsafe {
            unwrap!(Transfer::new_write(
                channel.reborrow(),
                Transfer::SOFTWARE_TRIGGER,
                &src[..i],
                &mut dst,
                options,
            ))
        };
        transfer.await;

        // The result will be the last value written.
        assert_eq!(dst, src[i - 1]);
    }
}

/// [`single_read`], but testing when the destination is wider than the source.
///
/// The MSPM0 DMA states that the upper bytes when the destination is longer than the source are zeroed.
/// This matches the behavior in Rust for all unsigned integer types.
fn widening_single_read<SW, DW>(mut channel: Peri<'_, impl Channel>, mut src: SW)
where
    SW: Word + Copy + Default + Eq + defmt::Format,
    DW: Word + Copy + Default + Eq + defmt::Format + From<SW>,
{
    assert!(
        DW::size() > SW::size(),
        "This test only works when the destination is larger than the source"
    );

    let options = TransferOptions::default();
    let mut dst = DW::default();

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_read(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            &mut src,
            slice::from_mut(&mut dst),
            options,
        ))
    };
    transfer.blocking_wait();

    assert_eq!(DW::from(src), dst);
}

/// [`single_read`], but testing when the destination is narrower than the source.
///
/// The MSPM0 DMA states that the upper bytes when the source is longer than the destination are dropped.
/// This matches the behavior in Rust for all unsigned integer types.
fn narrowing_single_read<SW, DW>(mut channel: Peri<'_, impl Channel>, mut src: SW)
where
    SW: Word + Copy + Default + Eq + defmt::Format + From<DW>,
    DW: Word + Copy + Default + Eq + defmt::Format + Narrow<SW>,
{
    assert!(
        SW::size() > DW::size(),
        "This test only works when the source is larger than the destination"
    );

    let options = TransferOptions::default();
    let mut dst = DW::default();

    // SAFETY: src and dst outlive the transfer.
    let transfer = unsafe {
        unwrap!(Transfer::new_read(
            channel.reborrow(),
            Transfer::SOFTWARE_TRIGGER,
            &mut src,
            slice::from_mut(&mut dst),
            options,
        ))
    };
    transfer.blocking_wait();

    // The expected value is the source value masked by the maximum destination value.
    // This is effectively `src as DW as SW` to drop the upper byte(s).
    let expect = SW::from(DW::narrow(src));
    assert_eq!(expect, dst.into());
}

/// A pseudo `as` trait to allow downcasting integer types (TryFrom could fail).
trait Narrow<T> {
    fn narrow(value: T) -> Self;
}

impl Narrow<u16> for u8 {
    fn narrow(value: u16) -> Self {
        value as u8
    }
}

impl Narrow<u32> for u8 {
    fn narrow(value: u32) -> Self {
        value as u8
    }
}

impl Narrow<u64> for u8 {
    fn narrow(value: u64) -> Self {
        value as u8
    }
}

impl Narrow<u32> for u16 {
    fn narrow(value: u32) -> Self {
        value as u16
    }
}

impl Narrow<u64> for u16 {
    fn narrow(value: u64) -> Self {
        value as u16
    }
}

impl Narrow<u64> for u32 {
    fn narrow(value: u64) -> Self {
        value as u32
    }
}