forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qspi_mdma.rs
129 lines (104 loc) · 3.51 KB
/
qspi_mdma.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! Example of Memory to Peripheral Transfer to the QSPI peripheral using the
//! Master DMA (MDMA)
//!
//! Tested on a STM32H747I-DISCO development board
#![deny(warnings)]
#![no_main]
#![no_std]
#[macro_use]
mod utilities;
use core::mem;
use cortex_m_rt::entry;
use stm32h7xx_hal::{pac, prelude::*, xspi, xspi::QspiMode};
use stm32h7xx_hal::dma::{
mdma::{
MdmaConfig, MdmaIncrement, MdmaTransferRequest, MdmaTrigger,
StreamsTuple,
},
MemoryToPeripheral, Transfer,
};
use log::info;
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(200.MHz()).freeze(pwrcfg, &dp.SYSCFG);
// Acquire the GPIO peripherals. This also enables the clock for
// the GPIOs in the RCC register.
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
let _qspi_cs = gpiog.pg6.into_alternate::<10>();
let sck = gpiof.pf10.into_alternate();
let io0 = gpiof.pf8.into_alternate();
let io1 = gpiod.pd12.into_alternate();
let io2 = gpioe.pe2.into_alternate();
let io3 = gpiod.pd13.into_alternate();
info!("");
info!("stm32h7xx-hal example - QSPI with MDMA");
info!("");
let config: xspi::Config = 1.MHz().into();
// Threshold when half full
let config = config.fifo_threshold(16);
// Initialise the QSPI peripheral.
let mut qspi = dp.QUADSPI.bank1(
(sck, io0, io1, io2, io3),
config,
&ccdr.clocks,
ccdr.peripheral.QSPI,
);
qspi.configure_mode(QspiMode::FourBit).unwrap();
// Disable address phase
qspi.inner_mut()
.ccr
.modify(|_, w| unsafe { w.admode().bits(0) });
// Source buffer in TCM
let mut source_buffer: [u8; 80] = [0x4A; 80];
for i in 0..20 {
let j = i * 4;
let i = i as u8;
source_buffer[j] |= ((i << 4) & 0x10) | ((i >> 1) & 1);
source_buffer[j + 1] |= ((i << 2) & 0x10) | ((i >> 3) & 1);
source_buffer[j + 2] |= (i & 0x10) | ((i >> 5) & 1);
source_buffer[j + 3] |= ((i >> 2) & 0x10) | ((i >> 7) & 1);
}
// Setup DMA
let streams = StreamsTuple::new(dp.MDMA, ccdr.peripheral.MDMA);
let config = MdmaConfig::default()
.source_increment(MdmaIncrement::Increment)
.destination_increment(MdmaIncrement::Fixed)
// Triggered by hardware each buffer
.hardware_transfer_request(MdmaTransferRequest::QuadspiFtTrg)
.trigger_mode(MdmaTrigger::Buffer)
// 16-byte buffer fits in QSPI FIFO
.buffer_length(16);
qspi.begin_write(0x00, 80).unwrap();
let mut transfer: Transfer<
_,
_,
MemoryToPeripheral,
&'static mut [u8; 80],
_,
> = Transfer::init_master(
streams.0,
qspi, // Dest: QSPI
unsafe { mem::transmute(&mut source_buffer) }, // Source: TCM (stack)
None,
config,
);
// Buffer is 16-bytes to fit in QSPI FIFO
assert_eq!(transfer.get_buffer_length(), 16);
// Start block
transfer.start(|_| {});
// Wait for transfer to complete
while !transfer.get_transfer_complete_flag() {}
loop {
cortex_m::asm::nop();
}
}