aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32h7b0/src
diff options
context:
space:
mode:
authorHaobo Gu <[email protected]>2024-10-26 23:50:16 +0800
committerHaobo Gu <[email protected]>2024-10-26 23:50:16 +0800
commit04c9130d326990dc92577f2ed4b2dc927efe2c13 (patch)
tree3c1a4bc774a2e6543acabae408649ac32a68f7b0 /examples/stm32h7b0/src
parentca6bcb4250fc294c2294265fbc83bf00398e089c (diff)
feat(example): move ospi memory mapped example for stm32h7b0 to separate folder
Signed-off-by: Haobo Gu <[email protected]>
Diffstat (limited to 'examples/stm32h7b0/src')
-rw-r--r--examples/stm32h7b0/src/bin/ospi_memory_mapped.rs434
1 files changed, 434 insertions, 0 deletions
diff --git a/examples/stm32h7b0/src/bin/ospi_memory_mapped.rs b/examples/stm32h7b0/src/bin/ospi_memory_mapped.rs
new file mode 100644
index 000000000..e32a22e41
--- /dev/null
+++ b/examples/stm32h7b0/src/bin/ospi_memory_mapped.rs
@@ -0,0 +1,434 @@
1#![no_main]
2#![no_std]
3
4// Tested on weact stm32h7b0 board + w25q64 spi flash
5
6use defmt::info;
7use defmt_rtt as _;
8use embassy_executor::Spawner;
9use embassy_stm32::{
10 gpio::{Level, Output, Speed},
11 mode::Blocking,
12 ospi::{AddressSize, DummyCycles, Instance, Ospi, OspiWidth, TransferConfig},
13 ospi::{ChipSelectHighTime, FIFOThresholdLevel, MemorySize, MemoryType, WrapSize},
14 time::Hertz,
15 Config,
16};
17use embassy_time::Timer;
18use panic_probe as _;
19
20#[embassy_executor::main]
21async fn main(_spawner: Spawner) {
22 // RCC config
23 let mut config = Config::default();
24 info!("START");
25 {
26 use embassy_stm32::rcc::*;
27 config.rcc.hsi = Some(HSIPrescaler::DIV1);
28 config.rcc.csi = true;
29 // Needed for USB
30 config.rcc.hsi48 = Some(Hsi48Config { sync_from_usb: true });
31 // External oscillator 25MHZ
32 config.rcc.hse = Some(Hse {
33 freq: Hertz(25_000_000),
34 mode: HseMode::Oscillator,
35 });
36 config.rcc.pll1 = Some(Pll {
37 source: PllSource::HSE,
38 prediv: PllPreDiv::DIV5,
39 mul: PllMul::MUL112,
40 divp: Some(PllDiv::DIV2),
41 divq: Some(PllDiv::DIV2),
42 divr: Some(PllDiv::DIV2),
43 });
44 config.rcc.sys = Sysclk::PLL1_P;
45 config.rcc.ahb_pre = AHBPrescaler::DIV2;
46 config.rcc.apb1_pre = APBPrescaler::DIV2;
47 config.rcc.apb2_pre = APBPrescaler::DIV2;
48 config.rcc.apb3_pre = APBPrescaler::DIV2;
49 config.rcc.apb4_pre = APBPrescaler::DIV2;
50 config.rcc.voltage_scale = VoltageScale::Scale0;
51 }
52
53 // Initialize peripherals
54 let p = embassy_stm32::init(config);
55
56 let qspi_config = embassy_stm32::ospi::Config {
57 fifo_threshold: FIFOThresholdLevel::_16Bytes,
58 memory_type: MemoryType::Micron,
59 device_size: MemorySize::_8MiB,
60 chip_select_high_time: ChipSelectHighTime::_1Cycle,
61 free_running_clock: false,
62 clock_mode: false,
63 wrap_size: WrapSize::None,
64 clock_prescaler: 4,
65 sample_shifting: true,
66 delay_hold_quarter_cycle: false,
67 chip_select_boundary: 0,
68 delay_block_bypass: true,
69 max_transfer: 0,
70 refresh: 0,
71 };
72 let ospi = embassy_stm32::ospi::Ospi::new_blocking_quadspi(
73 p.OCTOSPI1,
74 p.PB2,
75 p.PD11,
76 p.PD12,
77 p.PE2,
78 p.PD13,
79 p.PB6,
80 qspi_config,
81 );
82
83 let mut flash = FlashMemory::new(ospi).await;
84
85 let flash_id = flash.read_id();
86 info!("FLASH ID: {=[u8]:x}", flash_id);
87 let mut wr_buf = [0u8; 8];
88 for i in 0..8 {
89 wr_buf[i] = i as u8;
90 }
91 let mut rd_buf = [0u8; 8];
92 flash.erase_sector(0).await;
93 flash.write_memory(0, &wr_buf, true).await;
94 flash.read_memory(0, &mut rd_buf, true);
95 info!("WRITE BUF: {=[u8]:#X}", wr_buf);
96 info!("READ BUF: {=[u8]:#X}", rd_buf);
97 flash.enable_mm().await;
98 info!("Enabled memory mapped mode");
99
100 let first_u32 = unsafe { *(0x90000000 as *const u32) };
101 assert_eq!(first_u32, 0x03020100);
102
103 let second_u32 = unsafe { *(0x90000004 as *const u32) };
104 assert_eq!(second_u32, 0x07060504);
105 flash.disable_mm().await;
106
107 info!("DONE");
108 // Output pin PE3
109 let mut led = Output::new(p.PE3, Level::Low, Speed::Low);
110
111 loop {
112 led.toggle();
113 Timer::after_millis(1000).await;
114 }
115}
116
117const MEMORY_PAGE_SIZE: usize = 8;
118
119const CMD_QUAD_READ: u8 = 0x6B;
120
121const CMD_QUAD_WRITE_PG: u8 = 0x32;
122
123const CMD_READ_ID: u8 = 0x9F;
124
125const CMD_ENABLE_RESET: u8 = 0x66;
126const CMD_RESET: u8 = 0x99;
127
128const CMD_WRITE_ENABLE: u8 = 0x06;
129
130const CMD_CHIP_ERASE: u8 = 0xC7;
131const CMD_SECTOR_ERASE: u8 = 0x20;
132const CMD_BLOCK_ERASE_32K: u8 = 0x52;
133const CMD_BLOCK_ERASE_64K: u8 = 0xD8;
134
135const CMD_READ_SR: u8 = 0x05;
136const CMD_READ_CR: u8 = 0x35;
137
138const CMD_WRITE_SR: u8 = 0x01;
139const CMD_WRITE_CR: u8 = 0x31;
140
141/// Implementation of access to flash chip.
142/// Chip commands are hardcoded as it depends on used chip.
143/// This implementation is using chip GD25Q64C from Giga Device
144pub struct FlashMemory<I: Instance> {
145 ospi: Ospi<'static, I, Blocking>,
146}
147
148impl<I: Instance> FlashMemory<I> {
149 pub async fn new(ospi: Ospi<'static, I, Blocking>) -> Self {
150 let mut memory = Self { ospi };
151
152 memory.reset_memory().await;
153 memory.enable_quad();
154 memory
155 }
156
157 async fn qpi_mode(&mut self) {
158 // Enter qpi mode
159 self.exec_command(0x38).await;
160
161 // Set read param
162 let transaction = TransferConfig {
163 iwidth: OspiWidth::QUAD,
164 dwidth: OspiWidth::QUAD,
165 instruction: Some(0xC0),
166 ..Default::default()
167 };
168 self.enable_write().await;
169 self.ospi.blocking_write(&[0x30_u8], transaction).unwrap();
170 self.wait_write_finish();
171 }
172
173 pub async fn disable_mm(&mut self) {
174 self.ospi.disable_memory_mapped_mode();
175 }
176
177 pub async fn enable_mm(&mut self) {
178 self.qpi_mode().await;
179
180 let read_config = TransferConfig {
181 iwidth: OspiWidth::QUAD,
182 isize: AddressSize::_8Bit,
183 adwidth: OspiWidth::QUAD,
184 adsize: AddressSize::_24bit,
185 dwidth: OspiWidth::QUAD,
186 instruction: Some(0x0B), // Fast read in QPI mode
187 dummy: DummyCycles::_8,
188 ..Default::default()
189 };
190
191 let write_config = TransferConfig {
192 iwidth: OspiWidth::SING,
193 isize: AddressSize::_8Bit,
194 adwidth: OspiWidth::SING,
195 adsize: AddressSize::_24bit,
196 dwidth: OspiWidth::QUAD,
197 instruction: Some(0x32), // Write config
198 dummy: DummyCycles::_0,
199 ..Default::default()
200 };
201 self.ospi.enable_memory_mapped_mode(read_config, write_config).unwrap();
202 }
203
204 fn enable_quad(&mut self) {
205 let cr = self.read_cr();
206 // info!("Read cr: {:x}", cr);
207 self.write_cr(cr | 0x02);
208 // info!("Read cr after writing: {:x}", cr);
209 }
210
211 pub fn disable_quad(&mut self) {
212 let cr = self.read_cr();
213 self.write_cr(cr & (!(0x02)));
214 }
215
216 async fn exec_command_4(&mut self, cmd: u8) {
217 let transaction = TransferConfig {
218 iwidth: OspiWidth::QUAD,
219 adwidth: OspiWidth::NONE,
220 // adsize: AddressSize::_24bit,
221 dwidth: OspiWidth::NONE,
222 instruction: Some(cmd as u32),
223 address: None,
224 dummy: DummyCycles::_0,
225 ..Default::default()
226 };
227 self.ospi.command(&transaction).await.unwrap();
228 }
229
230 async fn exec_command(&mut self, cmd: u8) {
231 let transaction = TransferConfig {
232 iwidth: OspiWidth::SING,
233 adwidth: OspiWidth::NONE,
234 // adsize: AddressSize::_24bit,
235 dwidth: OspiWidth::NONE,
236 instruction: Some(cmd as u32),
237 address: None,
238 dummy: DummyCycles::_0,
239 ..Default::default()
240 };
241 // info!("Excuting command: {:x}", transaction.instruction);
242 self.ospi.command(&transaction).await.unwrap();
243 }
244
245 pub async fn reset_memory(&mut self) {
246 self.exec_command_4(CMD_ENABLE_RESET).await;
247 self.exec_command_4(CMD_RESET).await;
248 self.exec_command(CMD_ENABLE_RESET).await;
249 self.exec_command(CMD_RESET).await;
250 self.wait_write_finish();
251 }
252
253 pub async fn enable_write(&mut self) {
254 self.exec_command(CMD_WRITE_ENABLE).await;
255 }
256
257 pub fn read_id(&mut self) -> [u8; 3] {
258 let mut buffer = [0; 3];
259 let transaction: TransferConfig = TransferConfig {
260 iwidth: OspiWidth::SING,
261 isize: AddressSize::_8Bit,
262 adwidth: OspiWidth::NONE,
263 // adsize: AddressSize::_24bit,
264 dwidth: OspiWidth::SING,
265 instruction: Some(CMD_READ_ID as u32),
266 ..Default::default()
267 };
268 // info!("Reading id: 0x{:X}", transaction.instruction);
269 self.ospi.blocking_read(&mut buffer, transaction).unwrap();
270 buffer
271 }
272
273 pub fn read_id_4(&mut self) -> [u8; 3] {
274 let mut buffer = [0; 3];
275 let transaction: TransferConfig = TransferConfig {
276 iwidth: OspiWidth::SING,
277 isize: AddressSize::_8Bit,
278 adwidth: OspiWidth::NONE,
279 dwidth: OspiWidth::QUAD,
280 instruction: Some(CMD_READ_ID as u32),
281 ..Default::default()
282 };
283 info!("Reading id: 0x{:X}", transaction.instruction);
284 self.ospi.blocking_read(&mut buffer, transaction).unwrap();
285 buffer
286 }
287
288 pub fn read_memory(&mut self, addr: u32, buffer: &mut [u8], use_dma: bool) {
289 let transaction = TransferConfig {
290 iwidth: OspiWidth::SING,
291 adwidth: OspiWidth::SING,
292 adsize: AddressSize::_24bit,
293 dwidth: OspiWidth::QUAD,
294 instruction: Some(CMD_QUAD_READ as u32),
295 address: Some(addr),
296 dummy: DummyCycles::_8,
297 ..Default::default()
298 };
299 if use_dma {
300 self.ospi.blocking_read(buffer, transaction).unwrap();
301 } else {
302 self.ospi.blocking_read(buffer, transaction).unwrap();
303 }
304 }
305
306 fn wait_write_finish(&mut self) {
307 while (self.read_sr() & 0x01) != 0 {}
308 }
309
310 async fn perform_erase(&mut self, addr: u32, cmd: u8) {
311 let transaction = TransferConfig {
312 iwidth: OspiWidth::SING,
313 adwidth: OspiWidth::SING,
314 adsize: AddressSize::_24bit,
315 dwidth: OspiWidth::NONE,
316 instruction: Some(cmd as u32),
317 address: Some(addr),
318 dummy: DummyCycles::_0,
319 ..Default::default()
320 };
321 self.enable_write().await;
322 self.ospi.command(&transaction).await.unwrap();
323 self.wait_write_finish();
324 }
325
326 pub async fn erase_sector(&mut self, addr: u32) {
327 self.perform_erase(addr, CMD_SECTOR_ERASE).await;
328 }
329
330 pub async fn erase_block_32k(&mut self, addr: u32) {
331 self.perform_erase(addr, CMD_BLOCK_ERASE_32K).await;
332 }
333
334 pub async fn erase_block_64k(&mut self, addr: u32) {
335 self.perform_erase(addr, CMD_BLOCK_ERASE_64K).await;
336 }
337
338 pub async fn erase_chip(&mut self) {
339 self.exec_command(CMD_CHIP_ERASE).await;
340 }
341
342 async fn write_page(&mut self, addr: u32, buffer: &[u8], len: usize, use_dma: bool) {
343 assert!(
344 (len as u32 + (addr & 0x000000ff)) <= MEMORY_PAGE_SIZE as u32,
345 "write_page(): page write length exceeds page boundary (len = {}, addr = {:X}",
346 len,
347 addr
348 );
349
350 let transaction = TransferConfig {
351 iwidth: OspiWidth::SING,
352 adsize: AddressSize::_24bit,
353 adwidth: OspiWidth::SING,
354 dwidth: OspiWidth::QUAD,
355 instruction: Some(CMD_QUAD_WRITE_PG as u32),
356 address: Some(addr),
357 dummy: DummyCycles::_0,
358 ..Default::default()
359 };
360 self.enable_write().await;
361 if use_dma {
362 self.ospi.blocking_write(buffer, transaction).unwrap();
363 } else {
364 self.ospi.blocking_write(buffer, transaction).unwrap();
365 }
366 self.wait_write_finish();
367 }
368
369 pub async fn write_memory(&mut self, addr: u32, buffer: &[u8], use_dma: bool) {
370 let mut left = buffer.len();
371 let mut place = addr;
372 let mut chunk_start = 0;
373
374 while left > 0 {
375 let max_chunk_size = MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize;
376 let chunk_size = if left >= max_chunk_size { max_chunk_size } else { left };
377 let chunk = &buffer[chunk_start..(chunk_start + chunk_size)];
378 self.write_page(place, chunk, chunk_size, use_dma).await;
379 place += chunk_size as u32;
380 left -= chunk_size;
381 chunk_start += chunk_size;
382 }
383 }
384
385 fn read_register(&mut self, cmd: u8) -> u8 {
386 let mut buffer = [0; 1];
387 let transaction: TransferConfig = TransferConfig {
388 iwidth: OspiWidth::SING,
389 isize: AddressSize::_8Bit,
390 adwidth: OspiWidth::NONE,
391 adsize: AddressSize::_24bit,
392 dwidth: OspiWidth::SING,
393 instruction: Some(cmd as u32),
394 address: None,
395 dummy: DummyCycles::_0,
396 ..Default::default()
397 };
398 self.ospi.blocking_read(&mut buffer, transaction).unwrap();
399 // info!("Read w25q64 register: 0x{:x}", buffer[0]);
400 buffer[0]
401 }
402
403 fn write_register(&mut self, cmd: u8, value: u8) {
404 let buffer = [value; 1];
405 let transaction: TransferConfig = TransferConfig {
406 iwidth: OspiWidth::SING,
407 isize: AddressSize::_8Bit,
408 instruction: Some(cmd as u32),
409 adsize: AddressSize::_24bit,
410 adwidth: OspiWidth::NONE,
411 dwidth: OspiWidth::SING,
412 address: None,
413 dummy: DummyCycles::_0,
414 ..Default::default()
415 };
416 self.ospi.blocking_write(&buffer, transaction).unwrap();
417 }
418
419 pub fn read_sr(&mut self) -> u8 {
420 self.read_register(CMD_READ_SR)
421 }
422
423 pub fn read_cr(&mut self) -> u8 {
424 self.read_register(CMD_READ_CR)
425 }
426
427 pub fn write_sr(&mut self, value: u8) {
428 self.write_register(CMD_WRITE_SR, value);
429 }
430
431 pub fn write_cr(&mut self, value: u8) {
432 self.write_register(CMD_WRITE_CR, value);
433 }
434}