diff options
| author | Mick Chanthaseth <[email protected]> | 2025-04-10 14:02:39 -0700 |
|---|---|---|
| committer | Mick Chanthaseth <[email protected]> | 2025-04-10 14:02:39 -0700 |
| commit | e4739113b7c31a2949b4ebab7ac0ff2d44c39d6a (patch) | |
| tree | e1b5e61b63dc08e0afafa0ae6614299ed0d052e8 /examples/stm32h742/src | |
| parent | efde7e2841a993c996b3e90baa20922f3b41226c (diff) | |
added qspi example for stm32h742
Diffstat (limited to 'examples/stm32h742/src')
| -rw-r--r-- | examples/stm32h742/src/bin/qspi.rs | 300 |
1 files changed, 300 insertions, 0 deletions
diff --git a/examples/stm32h742/src/bin/qspi.rs b/examples/stm32h742/src/bin/qspi.rs new file mode 100644 index 000000000..707bb9922 --- /dev/null +++ b/examples/stm32h742/src/bin/qspi.rs | |||
| @@ -0,0 +1,300 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![allow(dead_code)] // Allow dead code as not all commands are used in the example | ||
| 4 | |||
| 5 | use defmt::info; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::mode::Blocking; | ||
| 8 | use embassy_stm32::qspi::enums::{ | ||
| 9 | AddressSize, ChipSelectHighTime, FIFOThresholdLevel, MemorySize, *, | ||
| 10 | }; | ||
| 11 | use embassy_stm32::qspi::{Config as QspiCfg, Instance, Qspi, TransferConfig}; | ||
| 12 | use embassy_stm32::Config as StmCfg; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | const MEMORY_PAGE_SIZE: usize = 256; | ||
| 16 | |||
| 17 | const CMD_READ: u8 = 0x03; | ||
| 18 | const CMD_HS_READ: u8 = 0x0B; | ||
| 19 | const CMD_QUAD_READ: u8 = 0x6B; | ||
| 20 | |||
| 21 | const CMD_WRITE_PG: u8 = 0xF2; | ||
| 22 | const CMD_QUAD_WRITE_PG: u8 = 0x32; | ||
| 23 | |||
| 24 | const CMD_READ_ID: u8 = 0x9F; | ||
| 25 | const CMD_READ_UUID: u8 = 0x4B; | ||
| 26 | |||
| 27 | const CMD_ENABLE_RESET: u8 = 0x66; | ||
| 28 | const CMD_RESET: u8 = 0x99; | ||
| 29 | |||
| 30 | const CMD_WRITE_ENABLE: u8 = 0x06; | ||
| 31 | const CMD_WRITE_DISABLE: u8 = 0x04; | ||
| 32 | |||
| 33 | const CMD_CHIP_ERASE: u8 = 0xC7; | ||
| 34 | const CMD_SECTOR_ERASE: u8 = 0x20; | ||
| 35 | const CMD_BLOCK_ERASE_32K: u8 = 0x52; | ||
| 36 | const CMD_BLOCK_ERASE_64K: u8 = 0xD8; | ||
| 37 | |||
| 38 | const CMD_READ_SR: u8 = 0x05; | ||
| 39 | const CMD_READ_CR: u8 = 0x35; | ||
| 40 | |||
| 41 | const CMD_WRITE_SR: u8 = 0x01; | ||
| 42 | const CMD_WRITE_CR: u8 = 0x31; | ||
| 43 | const MEMORY_ADDR: u32 = 0x00000001u32; | ||
| 44 | |||
| 45 | /// Implementation of access to flash chip. | ||
| 46 | /// Chip commands are hardcoded as it depends on used chip. | ||
| 47 | /// This implementation is using chip GD25Q64C from Giga Device | ||
| 48 | pub struct FlashMemory<I: Instance> { | ||
| 49 | qspi: Qspi<'static, I, Blocking>, | ||
| 50 | } | ||
| 51 | |||
| 52 | impl<I: Instance> FlashMemory<I> { | ||
| 53 | pub fn new(qspi: Qspi<'static, I, Blocking>) -> Self { | ||
| 54 | let mut memory = Self { qspi }; | ||
| 55 | |||
| 56 | memory.reset_memory(); | ||
| 57 | memory.enable_quad(); | ||
| 58 | |||
| 59 | memory | ||
| 60 | } | ||
| 61 | |||
| 62 | fn enable_quad(&mut self) { | ||
| 63 | let cr = self.read_cr(); | ||
| 64 | self.write_cr(cr | 0x02); | ||
| 65 | } | ||
| 66 | |||
| 67 | fn exec_command(&mut self, cmd: u8) { | ||
| 68 | let transaction = TransferConfig { | ||
| 69 | iwidth: QspiWidth::SING, | ||
| 70 | awidth: QspiWidth::NONE, | ||
| 71 | dwidth: QspiWidth::NONE, | ||
| 72 | instruction: cmd, | ||
| 73 | address: None, | ||
| 74 | dummy: DummyCycles::_0, | ||
| 75 | }; | ||
| 76 | self.qspi.blocking_command(transaction); | ||
| 77 | } | ||
| 78 | |||
| 79 | pub fn reset_memory(&mut self) { | ||
| 80 | self.exec_command(CMD_ENABLE_RESET); | ||
| 81 | self.exec_command(CMD_RESET); | ||
| 82 | self.wait_write_finish(); | ||
| 83 | } | ||
| 84 | |||
| 85 | pub fn enable_write(&mut self) { | ||
| 86 | self.exec_command(CMD_WRITE_ENABLE); | ||
| 87 | } | ||
| 88 | |||
| 89 | pub fn read_id(&mut self) -> [u8; 3] { | ||
| 90 | let mut buffer = [0; 3]; | ||
| 91 | let transaction: TransferConfig = TransferConfig { | ||
| 92 | iwidth: QspiWidth::SING, | ||
| 93 | awidth: QspiWidth::NONE, | ||
| 94 | dwidth: QspiWidth::SING, | ||
| 95 | instruction: CMD_READ_ID, | ||
| 96 | address: None, | ||
| 97 | dummy: DummyCycles::_0, | ||
| 98 | }; | ||
| 99 | self.qspi.blocking_read(&mut buffer, transaction); | ||
| 100 | buffer | ||
| 101 | } | ||
| 102 | |||
| 103 | pub fn read_uuid(&mut self) -> [u8; 16] { | ||
| 104 | let mut buffer = [0; 16]; | ||
| 105 | let transaction: TransferConfig = TransferConfig { | ||
| 106 | iwidth: QspiWidth::SING, | ||
| 107 | awidth: QspiWidth::SING, | ||
| 108 | dwidth: QspiWidth::SING, | ||
| 109 | instruction: CMD_READ_UUID, | ||
| 110 | address: Some(0), | ||
| 111 | dummy: DummyCycles::_8, | ||
| 112 | }; | ||
| 113 | self.qspi.blocking_read(&mut buffer, transaction); | ||
| 114 | buffer | ||
| 115 | } | ||
| 116 | |||
| 117 | pub fn read_memory(&mut self, addr: u32, buffer: &mut [u8]) { | ||
| 118 | let transaction = TransferConfig { | ||
| 119 | iwidth: QspiWidth::SING, | ||
| 120 | awidth: QspiWidth::SING, | ||
| 121 | dwidth: QspiWidth::QUAD, | ||
| 122 | instruction: CMD_QUAD_READ, | ||
| 123 | address: Some(addr), | ||
| 124 | dummy: DummyCycles::_8, | ||
| 125 | }; | ||
| 126 | self.qspi.blocking_read(buffer, transaction); | ||
| 127 | } | ||
| 128 | |||
| 129 | fn wait_write_finish(&mut self) { | ||
| 130 | while (self.read_sr() & 0x01) != 0 {} | ||
| 131 | } | ||
| 132 | |||
| 133 | fn perform_erase(&mut self, addr: u32, cmd: u8) { | ||
| 134 | let transaction = TransferConfig { | ||
| 135 | iwidth: QspiWidth::SING, | ||
| 136 | awidth: QspiWidth::SING, | ||
| 137 | dwidth: QspiWidth::NONE, | ||
| 138 | instruction: cmd, | ||
| 139 | address: Some(addr), | ||
| 140 | dummy: DummyCycles::_0, | ||
| 141 | }; | ||
| 142 | self.enable_write(); | ||
| 143 | self.qspi.blocking_command(transaction); | ||
| 144 | self.wait_write_finish(); | ||
| 145 | } | ||
| 146 | |||
| 147 | pub fn erase_sector(&mut self, addr: u32) { | ||
| 148 | self.perform_erase(addr, CMD_SECTOR_ERASE); | ||
| 149 | } | ||
| 150 | |||
| 151 | pub fn erase_block_32k(&mut self, addr: u32) { | ||
| 152 | self.perform_erase(addr, CMD_BLOCK_ERASE_32K); | ||
| 153 | } | ||
| 154 | |||
| 155 | pub fn erase_block_64k(&mut self, addr: u32) { | ||
| 156 | self.perform_erase(addr, CMD_BLOCK_ERASE_64K); | ||
| 157 | } | ||
| 158 | |||
| 159 | pub fn erase_chip(&mut self) { | ||
| 160 | self.exec_command(CMD_CHIP_ERASE); | ||
| 161 | } | ||
| 162 | |||
| 163 | fn write_page(&mut self, addr: u32, buffer: &[u8], len: usize) { | ||
| 164 | assert!( | ||
| 165 | (len as u32 + (addr & 0x000000ff)) <= MEMORY_PAGE_SIZE as u32, | ||
| 166 | "write_page(): page write length exceeds page boundary (len = {}, addr = {:X}", | ||
| 167 | len, | ||
| 168 | addr | ||
| 169 | ); | ||
| 170 | |||
| 171 | let transaction = TransferConfig { | ||
| 172 | iwidth: QspiWidth::SING, | ||
| 173 | awidth: QspiWidth::SING, | ||
| 174 | dwidth: QspiWidth::QUAD, | ||
| 175 | instruction: CMD_QUAD_WRITE_PG, | ||
| 176 | address: Some(addr), | ||
| 177 | dummy: DummyCycles::_0, | ||
| 178 | }; | ||
| 179 | self.enable_write(); | ||
| 180 | self.qspi.blocking_write(buffer, transaction); | ||
| 181 | self.wait_write_finish(); | ||
| 182 | } | ||
| 183 | |||
| 184 | pub fn write_memory(&mut self, addr: u32, buffer: &[u8]) { | ||
| 185 | let mut left = buffer.len(); | ||
| 186 | let mut place = addr; | ||
| 187 | let mut chunk_start = 0; | ||
| 188 | |||
| 189 | while left > 0 { | ||
| 190 | let max_chunk_size = MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize; | ||
| 191 | let chunk_size = if left >= max_chunk_size { | ||
| 192 | max_chunk_size | ||
| 193 | } else { | ||
| 194 | left | ||
| 195 | }; | ||
| 196 | let chunk = &buffer[chunk_start..(chunk_start + chunk_size)]; | ||
| 197 | self.write_page(place, chunk, chunk_size); | ||
| 198 | place += chunk_size as u32; | ||
| 199 | left -= chunk_size; | ||
| 200 | chunk_start += chunk_size; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | fn read_register(&mut self, cmd: u8) -> u8 { | ||
| 205 | let mut buffer = [0; 1]; | ||
| 206 | let transaction: TransferConfig = TransferConfig { | ||
| 207 | iwidth: QspiWidth::SING, | ||
| 208 | awidth: QspiWidth::NONE, | ||
| 209 | dwidth: QspiWidth::SING, | ||
| 210 | instruction: cmd, | ||
| 211 | address: None, | ||
| 212 | dummy: DummyCycles::_0, | ||
| 213 | }; | ||
| 214 | self.qspi.blocking_read(&mut buffer, transaction); | ||
| 215 | buffer[0] | ||
| 216 | } | ||
| 217 | |||
| 218 | fn write_register(&mut self, cmd: u8, value: u8) { | ||
| 219 | let buffer = [value; 1]; | ||
| 220 | let transaction: TransferConfig = TransferConfig { | ||
| 221 | iwidth: QspiWidth::SING, | ||
| 222 | awidth: QspiWidth::NONE, | ||
| 223 | dwidth: QspiWidth::SING, | ||
| 224 | instruction: cmd, | ||
| 225 | address: None, | ||
| 226 | dummy: DummyCycles::_0, | ||
| 227 | }; | ||
| 228 | self.qspi.blocking_write(&buffer, transaction); | ||
| 229 | } | ||
| 230 | |||
| 231 | pub fn read_sr(&mut self) -> u8 { | ||
| 232 | self.read_register(CMD_READ_SR) | ||
| 233 | } | ||
| 234 | |||
| 235 | pub fn read_cr(&mut self) -> u8 { | ||
| 236 | self.read_register(CMD_READ_CR) | ||
| 237 | } | ||
| 238 | |||
| 239 | pub fn write_sr(&mut self, value: u8) { | ||
| 240 | self.write_register(CMD_WRITE_SR, value); | ||
| 241 | } | ||
| 242 | |||
| 243 | pub fn write_cr(&mut self, value: u8) { | ||
| 244 | self.write_register(CMD_WRITE_CR, value); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | #[embassy_executor::main] | ||
| 249 | async fn main(_spawner: Spawner) -> ! { | ||
| 250 | let mut config = StmCfg::default(); | ||
| 251 | { | ||
| 252 | use embassy_stm32::rcc::*; | ||
| 253 | config.rcc.hsi = Some(HSIPrescaler::DIV1); | ||
| 254 | config.rcc.csi = true; | ||
| 255 | config.rcc.hsi48 = Some(Default::default()); // needed for RNG | ||
| 256 | config.rcc.pll1 = Some(Pll { | ||
| 257 | source: PllSource::HSI, | ||
| 258 | prediv: PllPreDiv::DIV4, | ||
| 259 | mul: PllMul::MUL50, | ||
| 260 | divp: Some(PllDiv::DIV2), | ||
| 261 | divq: None, | ||
| 262 | divr: None, | ||
| 263 | }); | ||
| 264 | config.rcc.sys = Sysclk::PLL1_P; // 400 Mhz | ||
| 265 | config.rcc.ahb_pre = AHBPrescaler::DIV2; // 200 Mhz | ||
| 266 | config.rcc.apb1_pre = APBPrescaler::DIV2; // 100 Mhz | ||
| 267 | config.rcc.apb2_pre = APBPrescaler::DIV2; // 100 Mhz | ||
| 268 | config.rcc.apb3_pre = APBPrescaler::DIV2; // 100 Mhz | ||
| 269 | config.rcc.apb4_pre = APBPrescaler::DIV2; // 100 Mhz | ||
| 270 | config.rcc.voltage_scale = VoltageScale::Scale1; | ||
| 271 | } | ||
| 272 | let p = embassy_stm32::init(config); | ||
| 273 | info!("Embassy initialized"); | ||
| 274 | |||
| 275 | let config = QspiCfg { | ||
| 276 | memory_size: MemorySize::_8MiB, | ||
| 277 | address_size: AddressSize::_24bit, | ||
| 278 | prescaler: 16, | ||
| 279 | cs_high_time: ChipSelectHighTime::_1Cycle, | ||
| 280 | fifo_threshold: FIFOThresholdLevel::_16Bytes, | ||
| 281 | }; | ||
| 282 | let driver = Qspi::new_blocking_bank1( | ||
| 283 | p.QUADSPI, p.PD11, p.PD12, p.PE2, p.PD13, p.PB2, p.PB10, config, | ||
| 284 | ); | ||
| 285 | let mut flash = FlashMemory::new(driver); | ||
| 286 | let flash_id = flash.read_id(); | ||
| 287 | info!("FLASH ID: {:?}", flash_id); | ||
| 288 | let mut wr_buf = [0u8; 256]; | ||
| 289 | for i in 0..256 { | ||
| 290 | wr_buf[i] = i as u8; | ||
| 291 | } | ||
| 292 | let mut rd_buf = [0u8; 256]; | ||
| 293 | flash.erase_sector(MEMORY_ADDR); | ||
| 294 | flash.write_memory(MEMORY_ADDR, &wr_buf); | ||
| 295 | flash.read_memory(MEMORY_ADDR, &mut rd_buf); | ||
| 296 | info!("WRITE BUF: {:?}", wr_buf); | ||
| 297 | info!("READ BUF: {:?}", rd_buf); | ||
| 298 | info!("End of Program, proceed to empty endless loop"); | ||
| 299 | loop {} | ||
| 300 | } | ||
