aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32u5/src/bin/hspi_memory_mapped.rs455
1 files changed, 455 insertions, 0 deletions
diff --git a/examples/stm32u5/src/bin/hspi_memory_mapped.rs b/examples/stm32u5/src/bin/hspi_memory_mapped.rs
new file mode 100644
index 000000000..9fef4855e
--- /dev/null
+++ b/examples/stm32u5/src/bin/hspi_memory_mapped.rs
@@ -0,0 +1,455 @@
1#![no_main]
2#![no_std]
3
4// Tested on an STM32U5G9J-DK2 demo board using the on-board MX66LM1G45G flash memory
5// The flash is connected to the HSPI1 port as an OCTA-DTR device
6//
7// Use embassy-stm32 feature "stm32u5g9zj" and probe-rs chip "STM32U5G9ZJTxQ"
8
9use defmt::info;
10use embassy_executor::Spawner;
11use embassy_stm32::hspi::{
12 AddressSize, ChipSelectHighTime, DummyCycles, FIFOThresholdLevel, Hspi, HspiWidth, Instance, MemorySize,
13 MemoryType, TransferConfig, WrapSize,
14};
15use embassy_stm32::mode::Async;
16use embassy_stm32::rcc;
17use embassy_stm32::time::Hertz;
18use {defmt_rtt as _, panic_probe as _};
19
20#[embassy_executor::main]
21async fn main(_spawner: Spawner) {
22 info!("Start hspi_memory_mapped");
23
24 // RCC config
25 let mut config = embassy_stm32::Config::default();
26 config.rcc.hse = Some(rcc::Hse {
27 freq: Hertz(16_000_000),
28 mode: rcc::HseMode::Oscillator,
29 });
30 config.rcc.pll1 = Some(rcc::Pll {
31 source: rcc::PllSource::HSE,
32 prediv: rcc::PllPreDiv::DIV1,
33 mul: rcc::PllMul::MUL10,
34 divp: None,
35 divq: None,
36 divr: Some(rcc::PllDiv::DIV1),
37 });
38 config.rcc.sys = rcc::Sysclk::PLL1_R; // 160 Mhz
39 config.rcc.pll2 = Some(rcc::Pll {
40 source: rcc::PllSource::HSE,
41 prediv: rcc::PllPreDiv::DIV4,
42 mul: rcc::PllMul::MUL66,
43 divp: None,
44 divq: Some(rcc::PllDiv::DIV2),
45 divr: None,
46 });
47 config.rcc.mux.hspi1sel = rcc::mux::Hspisel::PLL2_Q; // 132 MHz
48
49 // Initialize peripherals
50 let p = embassy_stm32::init(config);
51
52 let flash_config = embassy_stm32::hspi::Config {
53 fifo_threshold: FIFOThresholdLevel::_4Bytes,
54 memory_type: MemoryType::Macronix,
55 device_size: MemorySize::_1GiB,
56 chip_select_high_time: ChipSelectHighTime::_2Cycle,
57 free_running_clock: false,
58 clock_mode: false,
59 wrap_size: WrapSize::None,
60 clock_prescaler: 0,
61 sample_shifting: false,
62 delay_hold_quarter_cycle: false,
63 chip_select_boundary: 0,
64 delay_block_bypass: false,
65 max_transfer: 0,
66 refresh: 0,
67 };
68
69 let use_dma = true;
70
71 info!("Testing flash in OCTA DTR mode and memory mapped mode");
72
73 let hspi = Hspi::new_octospi(
74 p.HSPI1,
75 p.PI3,
76 p.PH10,
77 p.PH11,
78 p.PH12,
79 p.PH13,
80 p.PH14,
81 p.PH15,
82 p.PI0,
83 p.PI1,
84 p.PH9,
85 p.PI2,
86 p.GPDMA1_CH7,
87 flash_config,
88 );
89
90 let mut flash = OctaDtrFlashMemory::new(hspi).await;
91
92 let flash_id = flash.read_id();
93 info!("FLASH ID: {=[u8]:x}", flash_id);
94
95 let mut rd_buf = [0u8; 16];
96 flash.read_memory(0, &mut rd_buf, use_dma).await;
97 info!("READ BUF: {=[u8]:#X}", rd_buf);
98
99 flash.erase_sector(0).await;
100 flash.read_memory(0, &mut rd_buf, use_dma).await;
101 info!("READ BUF: {=[u8]:#X}", rd_buf);
102 assert_eq!(rd_buf[0], 0xFF);
103 assert_eq!(rd_buf[15], 0xFF);
104
105 let mut wr_buf = [0u8; 16];
106 for i in 0..wr_buf.len() {
107 wr_buf[i] = i as u8;
108 }
109 info!("WRITE BUF: {=[u8]:#X}", wr_buf);
110 flash.write_memory(0, &wr_buf, use_dma).await;
111 flash.read_memory(0, &mut rd_buf, use_dma).await;
112 info!("READ BUF: {=[u8]:#X}", rd_buf);
113 assert_eq!(rd_buf[0], 0x00);
114 assert_eq!(rd_buf[15], 0x0F);
115
116 flash.enable_mm().await;
117 info!("Enabled memory mapped mode");
118
119 let first_u32 = unsafe { *(0xA0000000 as *const u32) };
120 info!("first_u32: 0x{=u32:X}", first_u32);
121 assert_eq!(first_u32, 0x03020100);
122
123 let second_u32 = unsafe { *(0xA0000004 as *const u32) };
124 assert_eq!(second_u32, 0x07060504);
125 info!("second_u32: 0x{=u32:X}", second_u32);
126
127 let first_u8 = unsafe { *(0xA0000000 as *const u8) };
128 assert_eq!(first_u8, 00);
129 info!("first_u8: 0x{=u8:X}", first_u8);
130
131 let second_u8 = unsafe { *(0xA0000001 as *const u8) };
132 assert_eq!(second_u8, 0x01);
133 info!("second_u8: 0x{=u8:X}", second_u8);
134
135 let third_u8 = unsafe { *(0xA0000002 as *const u8) };
136 assert_eq!(third_u8, 0x02);
137 info!("third_u8: 0x{=u8:X}", third_u8);
138
139 let fourth_u8 = unsafe { *(0xA0000003 as *const u8) };
140 assert_eq!(fourth_u8, 0x03);
141 info!("fourth_u8: 0x{=u8:X}", fourth_u8);
142
143 info!("DONE");
144}
145
146// Custom implementation for MX66UW1G45G NOR flash memory from Macronix.
147// Chip commands are hardcoded as they depend on the chip used.
148// This implementation enables Octa I/O (OPI) and Double Transfer Rate (DTR)
149
150pub struct OctaDtrFlashMemory<'d, I: Instance> {
151 hspi: Hspi<'d, I, Async>,
152}
153
154impl<'d, I: Instance> OctaDtrFlashMemory<'d, I> {
155 const MEMORY_PAGE_SIZE: usize = 256;
156
157 const CMD_READ_OCTA_DTR: u16 = 0xEE11;
158 const CMD_PAGE_PROGRAM_OCTA_DTR: u16 = 0x12ED;
159
160 const CMD_READ_ID_OCTA_DTR: u16 = 0x9F60;
161
162 const CMD_RESET_ENABLE: u8 = 0x66;
163 const CMD_RESET_ENABLE_OCTA_DTR: u16 = 0x6699;
164 const CMD_RESET: u8 = 0x99;
165 const CMD_RESET_OCTA_DTR: u16 = 0x9966;
166
167 const CMD_WRITE_ENABLE: u8 = 0x06;
168 const CMD_WRITE_ENABLE_OCTA_DTR: u16 = 0x06F9;
169
170 const CMD_SECTOR_ERASE_OCTA_DTR: u16 = 0x21DE;
171 const CMD_BLOCK_ERASE_OCTA_DTR: u16 = 0xDC23;
172
173 const CMD_READ_SR: u8 = 0x05;
174 const CMD_READ_SR_OCTA_DTR: u16 = 0x05FA;
175
176 const CMD_READ_CR2: u8 = 0x71;
177 const CMD_WRITE_CR2: u8 = 0x72;
178
179 const CR2_REG1_ADDR: u32 = 0x00000000;
180 const CR2_OCTA_DTR: u8 = 0x02;
181
182 const CR2_REG3_ADDR: u32 = 0x00000300;
183 const CR2_DC_6_CYCLES: u8 = 0x07;
184
185 pub async fn new(hspi: Hspi<'d, I, Async>) -> Self {
186 let mut memory = Self { hspi };
187
188 memory.reset_memory().await;
189 memory.enable_octa_dtr().await;
190 memory
191 }
192
193 async fn enable_octa_dtr(&mut self) {
194 self.write_enable_spi().await;
195 self.write_cr2_spi(Self::CR2_REG3_ADDR, Self::CR2_DC_6_CYCLES);
196 self.write_enable_spi().await;
197 self.write_cr2_spi(Self::CR2_REG1_ADDR, Self::CR2_OCTA_DTR);
198 }
199
200 pub async fn enable_mm(&mut self) {
201 let read_config = TransferConfig {
202 iwidth: HspiWidth::OCTO,
203 instruction: Some(Self::CMD_READ_OCTA_DTR as u32),
204 isize: AddressSize::_16Bit,
205 idtr: true,
206 adwidth: HspiWidth::OCTO,
207 adsize: AddressSize::_32Bit,
208 addtr: true,
209 dwidth: HspiWidth::OCTO,
210 ddtr: true,
211 dummy: DummyCycles::_6,
212 ..Default::default()
213 };
214
215 let write_config = TransferConfig {
216 iwidth: HspiWidth::OCTO,
217 isize: AddressSize::_16Bit,
218 idtr: true,
219 adwidth: HspiWidth::OCTO,
220 adsize: AddressSize::_32Bit,
221 addtr: true,
222 dwidth: HspiWidth::OCTO,
223 ddtr: true,
224 ..Default::default()
225 };
226 self.hspi.enable_memory_mapped_mode(read_config, write_config).unwrap();
227 }
228
229 async fn exec_command_spi(&mut self, cmd: u8) {
230 let transaction = TransferConfig {
231 iwidth: HspiWidth::SING,
232 instruction: Some(cmd as u32),
233 ..Default::default()
234 };
235 info!("Excuting command: 0x{:X}", transaction.instruction.unwrap());
236 self.hspi.blocking_command(&transaction).unwrap();
237 }
238
239 async fn exec_command_octa_dtr(&mut self, cmd: u16) {
240 let transaction = TransferConfig {
241 iwidth: HspiWidth::OCTO,
242 instruction: Some(cmd as u32),
243 isize: AddressSize::_16Bit,
244 idtr: true,
245 ..Default::default()
246 };
247 info!("Excuting command: 0x{:X}", transaction.instruction.unwrap());
248 self.hspi.blocking_command(&transaction).unwrap();
249 }
250
251 fn wait_write_finish_spi(&mut self) {
252 while (self.read_sr_spi() & 0x01) != 0 {}
253 }
254
255 fn wait_write_finish_octa_dtr(&mut self) {
256 while (self.read_sr_octa_dtr() & 0x01) != 0 {}
257 }
258
259 pub async fn reset_memory(&mut self) {
260 // servono entrambi i comandi?
261 self.exec_command_octa_dtr(Self::CMD_RESET_ENABLE_OCTA_DTR).await;
262 self.exec_command_octa_dtr(Self::CMD_RESET_OCTA_DTR).await;
263 self.exec_command_spi(Self::CMD_RESET_ENABLE).await;
264 self.exec_command_spi(Self::CMD_RESET).await;
265 self.wait_write_finish_spi();
266 }
267
268 async fn write_enable_spi(&mut self) {
269 self.exec_command_spi(Self::CMD_WRITE_ENABLE).await;
270 }
271
272 async fn write_enable_octa_dtr(&mut self) {
273 self.exec_command_octa_dtr(Self::CMD_WRITE_ENABLE_OCTA_DTR).await;
274 }
275
276 pub fn read_id(&mut self) -> [u8; 3] {
277 let mut buffer = [0; 6];
278 let transaction: TransferConfig = TransferConfig {
279 iwidth: HspiWidth::OCTO,
280 instruction: Some(Self::CMD_READ_ID_OCTA_DTR as u32),
281 isize: AddressSize::_16Bit,
282 idtr: true,
283 adwidth: HspiWidth::OCTO,
284 address: Some(0),
285 adsize: AddressSize::_32Bit,
286 addtr: true,
287 dwidth: HspiWidth::OCTO,
288 ddtr: true,
289 dummy: DummyCycles::_5,
290 ..Default::default()
291 };
292 info!("Reading flash id: 0x{:X}", transaction.instruction.unwrap());
293 self.hspi.blocking_read(&mut buffer, transaction).unwrap();
294 [buffer[0], buffer[2], buffer[4]]
295 }
296
297 pub async fn read_memory(&mut self, addr: u32, buffer: &mut [u8], use_dma: bool) {
298 let transaction = TransferConfig {
299 iwidth: HspiWidth::OCTO,
300 instruction: Some(Self::CMD_READ_OCTA_DTR as u32),
301 isize: AddressSize::_16Bit,
302 idtr: true,
303 adwidth: HspiWidth::OCTO,
304 address: Some(addr),
305 adsize: AddressSize::_32Bit,
306 addtr: true,
307 dwidth: HspiWidth::OCTO,
308 ddtr: true,
309 dummy: DummyCycles::_6,
310 ..Default::default()
311 };
312 if use_dma {
313 self.hspi.read(buffer, transaction).await.unwrap();
314 } else {
315 self.hspi.blocking_read(buffer, transaction).unwrap();
316 }
317 }
318
319 async fn perform_erase_octa_dtr(&mut self, addr: u32, cmd: u16) {
320 let transaction = TransferConfig {
321 iwidth: HspiWidth::OCTO,
322 instruction: Some(cmd as u32),
323 isize: AddressSize::_16Bit,
324 idtr: true,
325 adwidth: HspiWidth::OCTO,
326 address: Some(addr),
327 adsize: AddressSize::_32Bit,
328 addtr: true,
329 ..Default::default()
330 };
331 self.write_enable_octa_dtr().await;
332 self.hspi.blocking_command(&transaction).unwrap();
333 self.wait_write_finish_octa_dtr();
334 }
335
336 pub async fn erase_sector(&mut self, addr: u32) {
337 info!("Erasing 4K sector at address: 0x{:X}", addr);
338 self.perform_erase_octa_dtr(addr, Self::CMD_SECTOR_ERASE_OCTA_DTR).await;
339 }
340
341 pub async fn erase_block(&mut self, addr: u32) {
342 info!("Erasing 64K block at address: 0x{:X}", addr);
343 self.perform_erase_octa_dtr(addr, Self::CMD_BLOCK_ERASE_OCTA_DTR).await;
344 }
345
346 async fn write_page_octa_dtr(&mut self, addr: u32, buffer: &[u8], len: usize, use_dma: bool) {
347 assert!(
348 (len as u32 + (addr & 0x000000ff)) <= Self::MEMORY_PAGE_SIZE as u32,
349 "write_page(): page write length exceeds page boundary (len = {}, addr = {:X}",
350 len,
351 addr
352 );
353
354 let transaction = TransferConfig {
355 iwidth: HspiWidth::OCTO,
356 instruction: Some(Self::CMD_PAGE_PROGRAM_OCTA_DTR as u32),
357 isize: AddressSize::_16Bit,
358 idtr: true,
359 adwidth: HspiWidth::OCTO,
360 address: Some(addr),
361 adsize: AddressSize::_32Bit,
362 addtr: true,
363 dwidth: HspiWidth::OCTO,
364 ddtr: true,
365 ..Default::default()
366 };
367 self.write_enable_octa_dtr().await;
368 if use_dma {
369 self.hspi.write(buffer, transaction).await.unwrap();
370 } else {
371 self.hspi.blocking_write(buffer, transaction).unwrap();
372 }
373 self.wait_write_finish_octa_dtr();
374 }
375
376 pub async fn write_memory(&mut self, addr: u32, buffer: &[u8], use_dma: bool) {
377 let mut left = buffer.len();
378 let mut place = addr;
379 let mut chunk_start = 0;
380
381 while left > 0 {
382 let max_chunk_size = Self::MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize;
383 let chunk_size = if left >= max_chunk_size { max_chunk_size } else { left };
384 let chunk = &buffer[chunk_start..(chunk_start + chunk_size)];
385 self.write_page_octa_dtr(place, chunk, chunk_size, use_dma).await;
386 place += chunk_size as u32;
387 left -= chunk_size;
388 chunk_start += chunk_size;
389 }
390 }
391
392 pub fn read_sr_spi(&mut self) -> u8 {
393 let mut buffer = [0; 1];
394 let transaction: TransferConfig = TransferConfig {
395 iwidth: HspiWidth::SING,
396 instruction: Some(Self::CMD_READ_SR as u32),
397 dwidth: HspiWidth::SING,
398 ..Default::default()
399 };
400 self.hspi.blocking_read(&mut buffer, transaction).unwrap();
401 // info!("Read MX66LM1G45G SR register: 0x{:x}", buffer[0]);
402 buffer[0]
403 }
404
405 pub fn read_sr_octa_dtr(&mut self) -> u8 {
406 let mut buffer = [0; 2];
407 let transaction: TransferConfig = TransferConfig {
408 iwidth: HspiWidth::OCTO,
409 instruction: Some(Self::CMD_READ_SR_OCTA_DTR as u32),
410 isize: AddressSize::_16Bit,
411 idtr: true,
412 adwidth: HspiWidth::OCTO,
413 address: Some(0),
414 adsize: AddressSize::_32Bit,
415 addtr: true,
416 dwidth: HspiWidth::OCTO,
417 ddtr: true,
418 dummy: DummyCycles::_5,
419 ..Default::default()
420 };
421 self.hspi.blocking_read(&mut buffer, transaction).unwrap();
422 // info!("Read MX66LM1G45G SR register: 0x{:x}", buffer[0]);
423 buffer[0]
424 }
425
426 pub fn read_cr2_spi(&mut self, addr: u32) -> u8 {
427 let mut buffer = [0; 1];
428 let transaction: TransferConfig = TransferConfig {
429 iwidth: HspiWidth::SING,
430 instruction: Some(Self::CMD_READ_CR2 as u32),
431 adwidth: HspiWidth::SING,
432 address: Some(addr),
433 adsize: AddressSize::_32Bit,
434 dwidth: HspiWidth::SING,
435 ..Default::default()
436 };
437 self.hspi.blocking_read(&mut buffer, transaction).unwrap();
438 // info!("Read MX66LM1G45G CR2[0x{:X}] register: 0x{:x}", addr, buffer[0]);
439 buffer[0]
440 }
441
442 pub fn write_cr2_spi(&mut self, addr: u32, value: u8) {
443 let buffer = [value; 1];
444 let transaction: TransferConfig = TransferConfig {
445 iwidth: HspiWidth::SING,
446 instruction: Some(Self::CMD_WRITE_CR2 as u32),
447 adwidth: HspiWidth::SING,
448 address: Some(addr),
449 adsize: AddressSize::_32Bit,
450 dwidth: HspiWidth::SING,
451 ..Default::default()
452 };
453 self.hspi.blocking_write(&buffer, transaction).unwrap();
454 }
455}