aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Johnston <[email protected]>2025-04-04 15:47:32 +0800
committerMatt Johnston <[email protected]>2025-04-04 17:41:53 +0800
commit43ef76b1b6c49085e1af06e0b1dd09a2d6c664e7 (patch)
treefa70f69989c3ea27c0cccb30f2530ac76c7bceb7
parent5f7da4cfc8e1028a9cffb0c539d860a73e830f03 (diff)
Add stm32h7rs xpi_memory_mapped example
Based on ospi_memory_mapped, targetting stm32h7s3 nucleo board. This works in single mode, no octo mode yet.
-rw-r--r--examples/stm32h7rs/src/bin/xspi_memory_mapped.rs448
1 files changed, 448 insertions, 0 deletions
diff --git a/examples/stm32h7rs/src/bin/xspi_memory_mapped.rs b/examples/stm32h7rs/src/bin/xspi_memory_mapped.rs
new file mode 100644
index 000000000..88d914180
--- /dev/null
+++ b/examples/stm32h7rs/src/bin/xspi_memory_mapped.rs
@@ -0,0 +1,448 @@
1#![no_main]
2#![no_std]
3
4//! For Nucleo STM32H7S3L8 MB1737, has MX25UW25645GXDI00
5//!
6//! TODO: Currently this only uses single SPI, pending flash chip documentation for octo SPI.
7
8use defmt::info;
9use embassy_executor::Spawner;
10use embassy_stm32::gpio::{Level, Output, Speed};
11use embassy_stm32::mode::Blocking;
12use embassy_stm32::time::Hertz;
13use embassy_stm32::xspi::{
14 AddressSize, ChipSelectHighTime, DummyCycles, FIFOThresholdLevel, Instance, MemorySize, MemoryType, TransferConfig,
15 WrapSize, Xspi, XspiWidth,
16};
17use embassy_stm32::Config;
18use embassy_time::Timer;
19use {defmt_rtt as _, panic_probe as _};
20
21#[embassy_executor::main]
22async fn main(_spawner: Spawner) {
23 // RCC config
24 let mut config = Config::default();
25 {
26 use embassy_stm32::rcc::*;
27 config.rcc.hse = Some(Hse {
28 freq: Hertz(24_000_000),
29 mode: HseMode::Oscillator,
30 });
31 config.rcc.pll1 = Some(Pll {
32 source: PllSource::HSE,
33 prediv: PllPreDiv::DIV3,
34 mul: PllMul::MUL150,
35 divp: Some(PllDiv::DIV2),
36 divq: None,
37 divr: None,
38 });
39 config.rcc.sys = Sysclk::PLL1_P; // 600 Mhz
40 config.rcc.ahb_pre = AHBPrescaler::DIV2; // 300 Mhz
41 config.rcc.apb1_pre = APBPrescaler::DIV2; // 150 Mhz
42 config.rcc.apb2_pre = APBPrescaler::DIV2; // 150 Mhz
43 config.rcc.apb4_pre = APBPrescaler::DIV2; // 150 Mhz
44 config.rcc.apb5_pre = APBPrescaler::DIV2; // 150 Mhz
45 config.rcc.voltage_scale = VoltageScale::HIGH;
46 }
47
48 // Initialize peripherals
49 let p = embassy_stm32::init(config);
50
51 let spi_config = embassy_stm32::xspi::Config {
52 fifo_threshold: FIFOThresholdLevel::_4Bytes,
53 memory_type: MemoryType::Macronix,
54 delay_hold_quarter_cycle: true,
55 // memory_type: MemoryType::Micron,
56 // delay_hold_quarter_cycle: false,
57 device_size: MemorySize::_32MiB,
58 chip_select_high_time: ChipSelectHighTime::_2Cycle,
59 free_running_clock: false,
60 clock_mode: false,
61 wrap_size: WrapSize::None,
62 // 300mhz / (4+1) = 60mhz. Unsure the limit, need to find a MX25UW25645GXDI00 datasheet.
63 clock_prescaler: 3,
64 sample_shifting: false,
65 chip_select_boundary: 0,
66 max_transfer: 0,
67 refresh: 0,
68 };
69
70 let mut cor = cortex_m::Peripherals::take().unwrap();
71
72 // Not necessary, but recommended if using XIP
73 cor.SCB.enable_icache();
74 cor.SCB.enable_dcache(&mut cor.CPUID);
75
76 let xspi = embassy_stm32::xspi::Xspi::new_blocking_xspi(
77 p.XSPI2, p.PN6, p.PN2, p.PN3, p.PN4, p.PN5, p.PN8, p.PN9, p.PN10, p.PN11, p.PN1, spi_config,
78 );
79
80 let mut flash = FlashMemory::new(xspi).await;
81
82 let flash_id = flash.read_id();
83 info!("FLASH ID: {=[u8]:x}", flash_id);
84
85 let mut wr_buf = [0u8; 8];
86 for i in 0..8 {
87 wr_buf[i] = 0x90 + i as u8;
88 }
89 let mut rd_buf = [0u8; 8];
90 flash.erase_sector(0).await;
91 flash.write_memory(0, &wr_buf, true).await;
92 flash.read_memory(0, &mut rd_buf, true);
93 info!("WRITE BUF: {=[u8]:#X}", wr_buf);
94 info!("READ BUF: {=[u8]:#X}", rd_buf);
95 flash.enable_mm().await;
96 info!("Enabled memory mapped mode");
97
98 let first_u32 = unsafe { *(0x70000000 as *const u32) };
99 assert_eq!(first_u32, 0x93929190);
100 info!("first_u32 {:08x}", first_u32);
101
102 let second_u32 = unsafe { *(0x70000004 as *const u32) };
103 assert_eq!(second_u32, 0x97969594);
104 info!("second_u32 {:08x}", first_u32);
105
106 flash.disable_mm().await;
107 info!("Disabled memory mapped mode");
108
109 info!("DONE");
110 // Output pin PE3
111 let mut led = Output::new(p.PE3, Level::Low, Speed::Low);
112
113 loop {
114 led.toggle();
115 Timer::after_millis(1000).await;
116 }
117}
118
119const MEMORY_PAGE_SIZE: usize = 8;
120
121const CMD_READ: u8 = 0x0B;
122const _CMD_QUAD_READ: u8 = 0x6B;
123
124const CMD_WRITE_PG: u8 = 0x02;
125const _CMD_QUAD_WRITE_PG: u8 = 0x32;
126
127const CMD_READ_ID: u8 = 0x9F;
128const CMD_READ_ID_OCTO: u16 = 0x9F60;
129
130const CMD_ENABLE_RESET: u8 = 0x66;
131const CMD_RESET: u8 = 0x99;
132
133const CMD_WRITE_ENABLE: u8 = 0x06;
134
135const CMD_CHIP_ERASE: u8 = 0xC7;
136const CMD_SECTOR_ERASE: u8 = 0x20;
137const CMD_BLOCK_ERASE_32K: u8 = 0x52;
138const CMD_BLOCK_ERASE_64K: u8 = 0xD8;
139
140const CMD_READ_SR: u8 = 0x05;
141const CMD_READ_CR: u8 = 0x35;
142
143const CMD_WRITE_SR: u8 = 0x01;
144const CMD_WRITE_CR: u8 = 0x31;
145
146/// Implementation of access to flash chip.
147///
148/// Chip commands are hardcoded as it depends on used chip.
149/// This targets a MX25UW25645GXDI00.
150pub struct FlashMemory<I: Instance> {
151 xspi: Xspi<'static, I, Blocking>,
152}
153
154impl<I: Instance> FlashMemory<I> {
155 pub async fn new(xspi: Xspi<'static, I, Blocking>) -> Self {
156 let mut memory = Self { xspi };
157
158 memory.reset_memory().await;
159 memory.enable_octo();
160 memory
161 }
162
163 async fn qpi_mode(&mut self) {
164 // Enter qpi mode
165 self.exec_command(0x38).await;
166
167 // Set read param
168 let transaction = TransferConfig {
169 iwidth: XspiWidth::QUAD,
170 dwidth: XspiWidth::QUAD,
171 instruction: Some(0xC0),
172 ..Default::default()
173 };
174 self.enable_write().await;
175 self.xspi.blocking_write(&[0x30_u8], transaction).unwrap();
176 self.wait_write_finish();
177 }
178
179 pub async fn disable_mm(&mut self) {
180 self.xspi.disable_memory_mapped_mode();
181 }
182
183 pub async fn enable_mm(&mut self) {
184 self.qpi_mode().await;
185
186 let read_config = TransferConfig {
187 iwidth: XspiWidth::SING,
188 isize: AddressSize::_8bit,
189 adwidth: XspiWidth::SING,
190 adsize: AddressSize::_24bit,
191 dwidth: XspiWidth::SING,
192 instruction: Some(CMD_READ as u32),
193 dummy: DummyCycles::_8,
194 ..Default::default()
195 };
196
197 let write_config = TransferConfig {
198 iwidth: XspiWidth::SING,
199 isize: AddressSize::_8bit,
200 adwidth: XspiWidth::SING,
201 adsize: AddressSize::_24bit,
202 dwidth: XspiWidth::SING,
203 instruction: Some(CMD_WRITE_PG as u32),
204 dummy: DummyCycles::_0,
205 ..Default::default()
206 };
207 self.xspi.enable_memory_mapped_mode(read_config, write_config).unwrap();
208 }
209
210 fn enable_octo(&mut self) {
211 let cr = self.read_cr();
212 // info!("Read cr: {:x}", cr);
213 self.write_cr(cr | 0x02);
214 // info!("Read cr after writing: {:x}", cr);
215 }
216
217 pub fn disable_octo(&mut self) {
218 let cr = self.read_cr();
219 self.write_cr(cr & (!(0x02)));
220 }
221
222 async fn exec_command_4(&mut self, cmd: u8) {
223 let transaction = TransferConfig {
224 iwidth: XspiWidth::QUAD,
225 adwidth: XspiWidth::NONE,
226 // adsize: AddressSize::_24bit,
227 dwidth: XspiWidth::NONE,
228 instruction: Some(cmd as u32),
229 address: None,
230 dummy: DummyCycles::_0,
231 ..Default::default()
232 };
233 self.xspi.blocking_command(&transaction).unwrap();
234 }
235
236 async fn exec_command(&mut self, cmd: u8) {
237 let transaction = TransferConfig {
238 iwidth: XspiWidth::SING,
239 adwidth: XspiWidth::NONE,
240 // adsize: AddressSize::_24bit,
241 dwidth: XspiWidth::NONE,
242 instruction: Some(cmd as u32),
243 address: None,
244 dummy: DummyCycles::_0,
245 ..Default::default()
246 };
247 // info!("Excuting command: {:x}", transaction.instruction);
248 self.xspi.blocking_command(&transaction).unwrap();
249 }
250
251 pub async fn reset_memory(&mut self) {
252 self.exec_command_4(CMD_ENABLE_RESET).await;
253 self.exec_command_4(CMD_RESET).await;
254 self.exec_command(CMD_ENABLE_RESET).await;
255 self.exec_command(CMD_RESET).await;
256 self.wait_write_finish();
257 }
258
259 pub async fn enable_write(&mut self) {
260 self.exec_command(CMD_WRITE_ENABLE).await;
261 }
262
263 pub fn read_id(&mut self) -> [u8; 3] {
264 let mut buffer = [0; 3];
265 let transaction: TransferConfig = TransferConfig {
266 iwidth: XspiWidth::SING,
267 isize: AddressSize::_8bit,
268 adwidth: XspiWidth::NONE,
269 // adsize: AddressSize::_24bit,
270 dwidth: XspiWidth::SING,
271 instruction: Some(CMD_READ_ID as u32),
272 ..Default::default()
273 };
274 // info!("Reading id: 0x{:X}", transaction.instruction);
275 self.xspi.blocking_read(&mut buffer, transaction).unwrap();
276 buffer
277 }
278
279 pub fn read_id_8(&mut self) -> [u8; 3] {
280 let mut buffer = [0; 3];
281 let transaction: TransferConfig = TransferConfig {
282 iwidth: XspiWidth::OCTO,
283 isize: AddressSize::_16bit,
284 adwidth: XspiWidth::OCTO,
285 address: Some(0),
286 adsize: AddressSize::_32bit,
287 dwidth: XspiWidth::OCTO,
288 instruction: Some(CMD_READ_ID_OCTO as u32),
289 dummy: DummyCycles::_4,
290 ..Default::default()
291 };
292 info!("Reading id: {:#X}", transaction.instruction);
293 self.xspi.blocking_read(&mut buffer, transaction).unwrap();
294 buffer
295 }
296
297 pub fn read_memory(&mut self, addr: u32, buffer: &mut [u8], use_dma: bool) {
298 let transaction = TransferConfig {
299 iwidth: XspiWidth::SING,
300 adwidth: XspiWidth::SING,
301 adsize: AddressSize::_24bit,
302 dwidth: XspiWidth::SING,
303 instruction: Some(CMD_READ as u32),
304 dummy: DummyCycles::_8,
305 // dwidth: XspiWidth::QUAD,
306 // instruction: Some(CMD_QUAD_READ as u32),
307 // dummy: DummyCycles::_8,
308 address: Some(addr),
309 ..Default::default()
310 };
311 if use_dma {
312 self.xspi.blocking_read(buffer, transaction).unwrap();
313 } else {
314 self.xspi.blocking_read(buffer, transaction).unwrap();
315 }
316 }
317
318 fn wait_write_finish(&mut self) {
319 while (self.read_sr() & 0x01) != 0 {}
320 }
321
322 async fn perform_erase(&mut self, addr: u32, cmd: u8) {
323 let transaction = TransferConfig {
324 iwidth: XspiWidth::SING,
325 adwidth: XspiWidth::SING,
326 adsize: AddressSize::_24bit,
327 dwidth: XspiWidth::NONE,
328 instruction: Some(cmd as u32),
329 address: Some(addr),
330 dummy: DummyCycles::_0,
331 ..Default::default()
332 };
333 self.enable_write().await;
334 self.xspi.blocking_command(&transaction).unwrap();
335 self.wait_write_finish();
336 }
337
338 pub async fn erase_sector(&mut self, addr: u32) {
339 self.perform_erase(addr, CMD_SECTOR_ERASE).await;
340 }
341
342 pub async fn erase_block_32k(&mut self, addr: u32) {
343 self.perform_erase(addr, CMD_BLOCK_ERASE_32K).await;
344 }
345
346 pub async fn erase_block_64k(&mut self, addr: u32) {
347 self.perform_erase(addr, CMD_BLOCK_ERASE_64K).await;
348 }
349
350 pub async fn erase_chip(&mut self) {
351 self.exec_command(CMD_CHIP_ERASE).await;
352 }
353
354 async fn write_page(&mut self, addr: u32, buffer: &[u8], len: usize, use_dma: bool) {
355 assert!(
356 (len as u32 + (addr & 0x000000ff)) <= MEMORY_PAGE_SIZE as u32,
357 "write_page(): page write length exceeds page boundary (len = {}, addr = {:X}",
358 len,
359 addr
360 );
361
362 let transaction = TransferConfig {
363 iwidth: XspiWidth::SING,
364 adsize: AddressSize::_24bit,
365 adwidth: XspiWidth::SING,
366 dwidth: XspiWidth::SING,
367 instruction: Some(CMD_WRITE_PG as u32),
368 // dwidth: XspiWidth::QUAD,
369 // instruction: Some(CMD_QUAD_WRITE_PG as u32),
370 address: Some(addr),
371 dummy: DummyCycles::_0,
372 ..Default::default()
373 };
374 self.enable_write().await;
375 if use_dma {
376 self.xspi.blocking_write(buffer, transaction).unwrap();
377 } else {
378 self.xspi.blocking_write(buffer, transaction).unwrap();
379 }
380 self.wait_write_finish();
381 }
382
383 pub async fn write_memory(&mut self, addr: u32, buffer: &[u8], use_dma: bool) {
384 let mut left = buffer.len();
385 let mut place = addr;
386 let mut chunk_start = 0;
387
388 while left > 0 {
389 let max_chunk_size = MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize;
390 let chunk_size = if left >= max_chunk_size { max_chunk_size } else { left };
391 let chunk = &buffer[chunk_start..(chunk_start + chunk_size)];
392 self.write_page(place, chunk, chunk_size, use_dma).await;
393 place += chunk_size as u32;
394 left -= chunk_size;
395 chunk_start += chunk_size;
396 }
397 }
398
399 fn read_register(&mut self, cmd: u8) -> u8 {
400 let mut buffer = [0; 1];
401 let transaction: TransferConfig = TransferConfig {
402 iwidth: XspiWidth::SING,
403 isize: AddressSize::_8bit,
404 adwidth: XspiWidth::NONE,
405 adsize: AddressSize::_24bit,
406 dwidth: XspiWidth::SING,
407 instruction: Some(cmd as u32),
408 address: None,
409 dummy: DummyCycles::_0,
410 ..Default::default()
411 };
412 self.xspi.blocking_read(&mut buffer, transaction).unwrap();
413 // info!("Read w25q64 register: 0x{:x}", buffer[0]);
414 buffer[0]
415 }
416
417 fn write_register(&mut self, cmd: u8, value: u8) {
418 let buffer = [value; 1];
419 let transaction: TransferConfig = TransferConfig {
420 iwidth: XspiWidth::SING,
421 isize: AddressSize::_8bit,
422 instruction: Some(cmd as u32),
423 adsize: AddressSize::_24bit,
424 adwidth: XspiWidth::NONE,
425 dwidth: XspiWidth::SING,
426 address: None,
427 dummy: DummyCycles::_0,
428 ..Default::default()
429 };
430 self.xspi.blocking_write(&buffer, transaction).unwrap();
431 }
432
433 pub fn read_sr(&mut self) -> u8 {
434 self.read_register(CMD_READ_SR)
435 }
436
437 pub fn read_cr(&mut self) -> u8 {
438 self.read_register(CMD_READ_CR)
439 }
440
441 pub fn write_sr(&mut self, value: u8) {
442 self.write_register(CMD_WRITE_SR, value);
443 }
444
445 pub fn write_cr(&mut self, value: u8) {
446 self.write_register(CMD_WRITE_CR, value);
447 }
448}