forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dma.rs
110 lines (87 loc) · 3.08 KB
/
dma.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! Example of Memory to Memory Transfer with the DMA
#![allow(clippy::transmute_ptr_to_ptr)]
#![deny(warnings)]
#![no_main]
#![no_std]
use core::{mem, mem::MaybeUninit};
use cortex_m_rt::entry;
#[macro_use]
mod utilities;
use stm32h7xx_hal::{pac, prelude::*};
use stm32h7xx_hal::dma::{
dma::{DmaConfig, StreamsTuple},
traits::Direction,
MemoryToMemory, Transfer,
};
use log::info;
// DMA1/DMA2 cannot interact with our stack. Instead, buffers for use with the
// DMA must be placed somewhere that DMA1/DMA2 can access.
//
// The runtime does not initialise these SRAM banks.
#[link_section = ".sram4.buffers"]
static mut SOURCE_BUFFER: MaybeUninit<[u32; 20]> = MaybeUninit::uninit();
#[link_section = ".axisram.buffers"]
static mut TARGET_BUFFER: MaybeUninit<[u32; 20]> = MaybeUninit::uninit();
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc
.sys_ck(200.mhz())
.pll1_q_ck(200.mhz())
.freeze(pwrcfg, &dp.SYSCFG);
// True RNG
let mut rng = dp.RNG.constrain(ccdr.peripheral.RNG, &ccdr.clocks);
info!("");
info!("stm32h7xx-hal example - Memory to Memory DMA");
info!("");
// Initialise the source buffer with truly random data, without taking any
// references to uninitialisated memory
let source_buffer: &'static mut [u32; 20] = {
let buf: &mut [MaybeUninit<u32>; 20] =
unsafe { mem::transmute(&mut SOURCE_BUFFER) };
for value in buf.iter_mut() {
unsafe {
value.as_mut_ptr().write(rng.gen().unwrap());
}
}
unsafe { mem::transmute(buf) }
};
// Save a copy on the stack so we can check it later
let source_buffer_cloned = *source_buffer;
// Setup DMA
//
// We need to specify the transfer size with a type annotation
let streams = StreamsTuple::new(dp.DMA1, ccdr.peripheral.DMA1);
let config = DmaConfig::default()
.memory_increment(true) // destination mem
.peripheral_increment(true) // source mem
.fifo_enable(true);
let mut transfer: Transfer<_, _, MemoryToMemory<u32>, _, _> =
Transfer::init(
streams.4,
MemoryToMemory::new(),
unsafe { mem::transmute(&mut TARGET_BUFFER) }, // Uninitialised memory
Some(source_buffer),
config,
);
transfer.start(|_| {});
// Wait for transfer to complete
while !transfer.get_transfer_complete_flag() {}
// Now the target memory is actually initialised
let target_buffer: &'static mut [u32; 20] =
unsafe { mem::transmute(&mut TARGET_BUFFER) };
// Comparison check
assert_eq!(&source_buffer_cloned, target_buffer);
info!("Memory to Memory DMA completed successfully");
loop {
cortex_m::asm::nop()
}
}