diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/src/bin/ospi_memory_mapped.rs | 435 |
1 files changed, 435 insertions, 0 deletions
diff --git a/examples/stm32h7/src/bin/ospi_memory_mapped.rs b/examples/stm32h7/src/bin/ospi_memory_mapped.rs new file mode 100644 index 000000000..40f39f751 --- /dev/null +++ b/examples/stm32h7/src/bin/ospi_memory_mapped.rs | |||
| @@ -0,0 +1,435 @@ | |||
| 1 | #![no_main] | ||
| 2 | #![no_std] | ||
| 3 | |||
| 4 | // Tested on weact stm32h7b0 board + w25q64 spi flash | ||
| 5 | |||
| 6 | use defmt::info; | ||
| 7 | use defmt_rtt as _; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use 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 | }; | ||
| 17 | use embassy_time::Timer; | ||
| 18 | use panic_probe as _; | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async 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: 2, | ||
| 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 | info!("Enabling memory mapped mode"); | ||
| 98 | flash.enable_mm().await; | ||
| 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 | |||
| 117 | const MEMORY_PAGE_SIZE: usize = 8; | ||
| 118 | |||
| 119 | const CMD_QUAD_READ: u8 = 0x6B; | ||
| 120 | |||
| 121 | const CMD_QUAD_WRITE_PG: u8 = 0x32; | ||
| 122 | |||
| 123 | const CMD_READ_ID: u8 = 0x9F; | ||
| 124 | const CMD_READ_UUID: u8 = 0x4B; | ||
| 125 | |||
| 126 | const CMD_ENABLE_RESET: u8 = 0x66; | ||
| 127 | const CMD_RESET: u8 = 0x99; | ||
| 128 | |||
| 129 | const CMD_WRITE_ENABLE: u8 = 0x06; | ||
| 130 | |||
| 131 | const CMD_CHIP_ERASE: u8 = 0xC7; | ||
| 132 | const CMD_SECTOR_ERASE: u8 = 0x20; | ||
| 133 | const CMD_BLOCK_ERASE_32K: u8 = 0x52; | ||
| 134 | const CMD_BLOCK_ERASE_64K: u8 = 0xD8; | ||
| 135 | |||
| 136 | const CMD_READ_SR: u8 = 0x05; | ||
| 137 | const CMD_READ_CR: u8 = 0x35; | ||
| 138 | |||
| 139 | const CMD_WRITE_SR: u8 = 0x01; | ||
| 140 | const CMD_WRITE_CR: u8 = 0x31; | ||
| 141 | |||
| 142 | /// Implementation of access to flash chip. | ||
| 143 | /// Chip commands are hardcoded as it depends on used chip. | ||
| 144 | /// This implementation is using chip GD25Q64C from Giga Device | ||
| 145 | pub struct FlashMemory<I: Instance> { | ||
| 146 | ospi: Ospi<'static, I, Blocking>, | ||
| 147 | } | ||
| 148 | |||
| 149 | impl<I: Instance> FlashMemory<I> { | ||
| 150 | pub async fn new(ospi: Ospi<'static, I, Blocking>) -> Self { | ||
| 151 | let mut memory = Self { ospi }; | ||
| 152 | |||
| 153 | memory.reset_memory().await; | ||
| 154 | memory.enable_quad(); | ||
| 155 | memory | ||
| 156 | } | ||
| 157 | |||
| 158 | async fn qpi_mode(&mut self) { | ||
| 159 | // Enter qpi mode | ||
| 160 | self.exec_command(0x38).await; | ||
| 161 | |||
| 162 | // Set read param | ||
| 163 | let transaction = TransferConfig { | ||
| 164 | iwidth: OspiWidth::QUAD, | ||
| 165 | dwidth: OspiWidth::QUAD, | ||
| 166 | instruction: Some(0xC0), | ||
| 167 | ..Default::default() | ||
| 168 | }; | ||
| 169 | self.enable_write().await; | ||
| 170 | self.ospi.blocking_write(&[0x30_u8], transaction).unwrap(); | ||
| 171 | self.wait_write_finish(); | ||
| 172 | } | ||
| 173 | |||
| 174 | pub async fn disable_mm(&mut self) { | ||
| 175 | self.ospi.disable_memory_mapped_mode(); | ||
| 176 | } | ||
| 177 | |||
| 178 | pub async fn enable_mm(&mut self) { | ||
| 179 | self.qpi_mode().await; | ||
| 180 | |||
| 181 | let read_config = TransferConfig { | ||
| 182 | iwidth: OspiWidth::QUAD, | ||
| 183 | isize: AddressSize::_8Bit, | ||
| 184 | adwidth: OspiWidth::QUAD, | ||
| 185 | adsize: AddressSize::_24bit, | ||
| 186 | dwidth: OspiWidth::QUAD, | ||
| 187 | instruction: Some(0x0B), // Fast read in QPI mode | ||
| 188 | dummy: DummyCycles::_8, | ||
| 189 | ..Default::default() | ||
| 190 | }; | ||
| 191 | |||
| 192 | let write_config = TransferConfig { | ||
| 193 | iwidth: OspiWidth::SING, | ||
| 194 | isize: AddressSize::_8Bit, | ||
| 195 | adwidth: OspiWidth::SING, | ||
| 196 | adsize: AddressSize::_24bit, | ||
| 197 | dwidth: OspiWidth::QUAD, | ||
| 198 | instruction: Some(0x32), // Write config | ||
| 199 | dummy: DummyCycles::_0, | ||
| 200 | ..Default::default() | ||
| 201 | }; | ||
| 202 | self.ospi.enable_memory_mapped_mode(read_config, write_config).unwrap(); | ||
| 203 | } | ||
| 204 | |||
| 205 | fn enable_quad(&mut self) { | ||
| 206 | let cr = self.read_cr(); | ||
| 207 | // info!("Read cr: {:x}", cr); | ||
| 208 | self.write_cr(cr | 0x02); | ||
| 209 | // info!("Read cr after writing: {:x}", cr); | ||
| 210 | } | ||
| 211 | |||
| 212 | pub fn disable_quad(&mut self) { | ||
| 213 | let cr = self.read_cr(); | ||
| 214 | self.write_cr(cr & (!(0x02))); | ||
| 215 | } | ||
| 216 | |||
| 217 | async fn exec_command_4(&mut self, cmd: u8) { | ||
| 218 | let transaction = TransferConfig { | ||
| 219 | iwidth: OspiWidth::QUAD, | ||
| 220 | adwidth: OspiWidth::NONE, | ||
| 221 | // adsize: AddressSize::_24bit, | ||
| 222 | dwidth: OspiWidth::NONE, | ||
| 223 | instruction: Some(cmd as u32), | ||
| 224 | address: None, | ||
| 225 | dummy: DummyCycles::_0, | ||
| 226 | ..Default::default() | ||
| 227 | }; | ||
| 228 | self.ospi.command(&transaction).await.unwrap(); | ||
| 229 | } | ||
| 230 | |||
| 231 | async fn exec_command(&mut self, cmd: u8) { | ||
| 232 | let transaction = TransferConfig { | ||
| 233 | iwidth: OspiWidth::SING, | ||
| 234 | adwidth: OspiWidth::NONE, | ||
| 235 | // adsize: AddressSize::_24bit, | ||
| 236 | dwidth: OspiWidth::NONE, | ||
| 237 | instruction: Some(cmd as u32), | ||
| 238 | address: None, | ||
| 239 | dummy: DummyCycles::_0, | ||
| 240 | ..Default::default() | ||
| 241 | }; | ||
| 242 | // info!("Excuting command: {:x}", transaction.instruction); | ||
| 243 | self.ospi.command(&transaction).await.unwrap(); | ||
| 244 | } | ||
| 245 | |||
| 246 | pub async fn reset_memory(&mut self) { | ||
| 247 | self.exec_command_4(CMD_ENABLE_RESET).await; | ||
| 248 | self.exec_command_4(CMD_RESET).await; | ||
| 249 | self.exec_command(CMD_ENABLE_RESET).await; | ||
| 250 | self.exec_command(CMD_RESET).await; | ||
| 251 | self.wait_write_finish(); | ||
| 252 | } | ||
| 253 | |||
| 254 | pub async fn enable_write(&mut self) { | ||
| 255 | self.exec_command(CMD_WRITE_ENABLE).await; | ||
| 256 | } | ||
| 257 | |||
| 258 | pub fn read_id(&mut self) -> [u8; 3] { | ||
| 259 | let mut buffer = [0; 3]; | ||
| 260 | let transaction: TransferConfig = TransferConfig { | ||
| 261 | iwidth: OspiWidth::SING, | ||
| 262 | isize: AddressSize::_8Bit, | ||
| 263 | adwidth: OspiWidth::NONE, | ||
| 264 | // adsize: AddressSize::_24bit, | ||
| 265 | dwidth: OspiWidth::SING, | ||
| 266 | instruction: Some(CMD_READ_ID as u32), | ||
| 267 | ..Default::default() | ||
| 268 | }; | ||
| 269 | // info!("Reading id: 0x{:X}", transaction.instruction); | ||
| 270 | self.ospi.blocking_read(&mut buffer, transaction).unwrap(); | ||
| 271 | buffer | ||
| 272 | } | ||
| 273 | |||
| 274 | pub fn read_id_4(&mut self) -> [u8; 3] { | ||
| 275 | let mut buffer = [0; 3]; | ||
| 276 | let transaction: TransferConfig = TransferConfig { | ||
| 277 | iwidth: OspiWidth::SING, | ||
| 278 | isize: AddressSize::_8Bit, | ||
| 279 | adwidth: OspiWidth::NONE, | ||
| 280 | dwidth: OspiWidth::QUAD, | ||
| 281 | instruction: Some(CMD_READ_ID as u32), | ||
| 282 | ..Default::default() | ||
| 283 | }; | ||
| 284 | info!("Reading id: 0x{:X}", transaction.instruction); | ||
| 285 | self.ospi.blocking_read(&mut buffer, transaction).unwrap(); | ||
| 286 | buffer | ||
| 287 | } | ||
| 288 | |||
| 289 | pub fn read_memory(&mut self, addr: u32, buffer: &mut [u8], use_dma: bool) { | ||
| 290 | let transaction = TransferConfig { | ||
| 291 | iwidth: OspiWidth::SING, | ||
| 292 | adwidth: OspiWidth::SING, | ||
| 293 | adsize: AddressSize::_24bit, | ||
| 294 | dwidth: OspiWidth::QUAD, | ||
| 295 | instruction: Some(CMD_QUAD_READ as u32), | ||
| 296 | address: Some(addr), | ||
| 297 | dummy: DummyCycles::_8, | ||
| 298 | ..Default::default() | ||
| 299 | }; | ||
| 300 | if use_dma { | ||
| 301 | self.ospi.blocking_read(buffer, transaction).unwrap(); | ||
| 302 | } else { | ||
| 303 | self.ospi.blocking_read(buffer, transaction).unwrap(); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | fn wait_write_finish(&mut self) { | ||
| 308 | while (self.read_sr() & 0x01) != 0 {} | ||
| 309 | } | ||
| 310 | |||
| 311 | async fn perform_erase(&mut self, addr: u32, cmd: u8) { | ||
| 312 | let transaction = TransferConfig { | ||
| 313 | iwidth: OspiWidth::SING, | ||
| 314 | adwidth: OspiWidth::SING, | ||
| 315 | adsize: AddressSize::_24bit, | ||
| 316 | dwidth: OspiWidth::NONE, | ||
| 317 | instruction: Some(cmd as u32), | ||
| 318 | address: Some(addr), | ||
| 319 | dummy: DummyCycles::_0, | ||
| 320 | ..Default::default() | ||
| 321 | }; | ||
| 322 | self.enable_write().await; | ||
| 323 | self.ospi.command(&transaction).await.unwrap(); | ||
| 324 | self.wait_write_finish(); | ||
| 325 | } | ||
| 326 | |||
| 327 | pub async fn erase_sector(&mut self, addr: u32) { | ||
| 328 | self.perform_erase(addr, CMD_SECTOR_ERASE).await; | ||
| 329 | } | ||
| 330 | |||
| 331 | pub async fn erase_block_32k(&mut self, addr: u32) { | ||
| 332 | self.perform_erase(addr, CMD_BLOCK_ERASE_32K).await; | ||
| 333 | } | ||
| 334 | |||
| 335 | pub async fn erase_block_64k(&mut self, addr: u32) { | ||
| 336 | self.perform_erase(addr, CMD_BLOCK_ERASE_64K).await; | ||
| 337 | } | ||
| 338 | |||
| 339 | pub async fn erase_chip(&mut self) { | ||
| 340 | self.exec_command(CMD_CHIP_ERASE).await; | ||
| 341 | } | ||
| 342 | |||
| 343 | async fn write_page(&mut self, addr: u32, buffer: &[u8], len: usize, use_dma: bool) { | ||
| 344 | assert!( | ||
| 345 | (len as u32 + (addr & 0x000000ff)) <= MEMORY_PAGE_SIZE as u32, | ||
| 346 | "write_page(): page write length exceeds page boundary (len = {}, addr = {:X}", | ||
| 347 | len, | ||
| 348 | addr | ||
| 349 | ); | ||
| 350 | |||
| 351 | let transaction = TransferConfig { | ||
| 352 | iwidth: OspiWidth::SING, | ||
| 353 | adsize: AddressSize::_24bit, | ||
| 354 | adwidth: OspiWidth::SING, | ||
| 355 | dwidth: OspiWidth::QUAD, | ||
| 356 | instruction: Some(CMD_QUAD_WRITE_PG as u32), | ||
| 357 | address: Some(addr), | ||
| 358 | dummy: DummyCycles::_0, | ||
| 359 | ..Default::default() | ||
| 360 | }; | ||
| 361 | self.enable_write().await; | ||
| 362 | if use_dma { | ||
| 363 | self.ospi.blocking_write(buffer, transaction).unwrap(); | ||
| 364 | } else { | ||
| 365 | self.ospi.blocking_write(buffer, transaction).unwrap(); | ||
| 366 | } | ||
| 367 | self.wait_write_finish(); | ||
| 368 | } | ||
| 369 | |||
| 370 | pub async fn write_memory(&mut self, addr: u32, buffer: &[u8], use_dma: bool) { | ||
| 371 | let mut left = buffer.len(); | ||
| 372 | let mut place = addr; | ||
| 373 | let mut chunk_start = 0; | ||
| 374 | |||
| 375 | while left > 0 { | ||
| 376 | let max_chunk_size = MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize; | ||
| 377 | let chunk_size = if left >= max_chunk_size { max_chunk_size } else { left }; | ||
| 378 | let chunk = &buffer[chunk_start..(chunk_start + chunk_size)]; | ||
| 379 | self.write_page(place, chunk, chunk_size, use_dma).await; | ||
| 380 | place += chunk_size as u32; | ||
| 381 | left -= chunk_size; | ||
| 382 | chunk_start += chunk_size; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | fn read_register(&mut self, cmd: u8) -> u8 { | ||
| 387 | let mut buffer = [0; 1]; | ||
| 388 | let transaction: TransferConfig = TransferConfig { | ||
| 389 | iwidth: OspiWidth::SING, | ||
| 390 | isize: AddressSize::_8Bit, | ||
| 391 | adwidth: OspiWidth::NONE, | ||
| 392 | adsize: AddressSize::_24bit, | ||
| 393 | dwidth: OspiWidth::SING, | ||
| 394 | instruction: Some(cmd as u32), | ||
| 395 | address: None, | ||
| 396 | dummy: DummyCycles::_0, | ||
| 397 | ..Default::default() | ||
| 398 | }; | ||
| 399 | self.ospi.blocking_read(&mut buffer, transaction).unwrap(); | ||
| 400 | // info!("Read w25q64 register: 0x{:x}", buffer[0]); | ||
| 401 | buffer[0] | ||
| 402 | } | ||
| 403 | |||
| 404 | fn write_register(&mut self, cmd: u8, value: u8) { | ||
| 405 | let buffer = [value; 1]; | ||
| 406 | let transaction: TransferConfig = TransferConfig { | ||
| 407 | iwidth: OspiWidth::SING, | ||
| 408 | isize: AddressSize::_8Bit, | ||
| 409 | instruction: Some(cmd as u32), | ||
| 410 | adsize: AddressSize::_24bit, | ||
| 411 | adwidth: OspiWidth::NONE, | ||
| 412 | dwidth: OspiWidth::SING, | ||
| 413 | address: None, | ||
| 414 | dummy: DummyCycles::_0, | ||
| 415 | ..Default::default() | ||
| 416 | }; | ||
| 417 | self.ospi.blocking_write(&buffer, transaction).unwrap(); | ||
| 418 | } | ||
| 419 | |||
| 420 | pub fn read_sr(&mut self) -> u8 { | ||
| 421 | self.read_register(CMD_READ_SR) | ||
| 422 | } | ||
| 423 | |||
| 424 | pub fn read_cr(&mut self) -> u8 { | ||
| 425 | self.read_register(CMD_READ_CR) | ||
| 426 | } | ||
| 427 | |||
| 428 | pub fn write_sr(&mut self, value: u8) { | ||
| 429 | self.write_register(CMD_WRITE_SR, value); | ||
| 430 | } | ||
| 431 | |||
| 432 | pub fn write_cr(&mut self, value: u8) { | ||
| 433 | self.write_register(CMD_WRITE_CR, value); | ||
| 434 | } | ||
| 435 | } | ||
