diff options
| author | Mateusz Butkiewicz <[email protected]> | 2024-05-09 08:05:34 +0200 |
|---|---|---|
| committer | Mateusz Butkiewicz <[email protected]> | 2024-05-10 07:58:30 +0200 |
| commit | ee22e98fe1229ff9f4a85670dbb092717431ddde (patch) | |
| tree | 36120cf8c6a3dd594f4cecfc0716f85ac68e1abf /examples/stm32f7/src | |
| parent | 4a159453956464b534b7cbf38f5d17b9a3e36bb7 (diff) | |
feat(qspi): add example usage of QSPI
Diffstat (limited to 'examples/stm32f7/src')
| -rw-r--r-- | examples/stm32f7/src/bin/qspi.rs | 300 |
1 files changed, 300 insertions, 0 deletions
diff --git a/examples/stm32f7/src/bin/qspi.rs b/examples/stm32f7/src/bin/qspi.rs new file mode 100644 index 000000000..005694db3 --- /dev/null +++ b/examples/stm32f7/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::qspi::enums::{AddressSize, ChipSelectHighTime, FIFOThresholdLevel, MemorySize, *}; | ||
| 8 | use embassy_stm32::qspi::{Config as QspiCfg, Instance, Qspi, QuadDma, TransferConfig}; | ||
| 9 | use embassy_stm32::time::mhz; | ||
| 10 | use embassy_stm32::Config as StmCfg; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | const MEMORY_PAGE_SIZE: usize = 256; | ||
| 14 | |||
| 15 | const CMD_READ: u8 = 0x03; | ||
| 16 | const CMD_HS_READ: u8 = 0x0B; | ||
| 17 | const CMD_QUAD_READ: u8 = 0x6B; | ||
| 18 | |||
| 19 | const CMD_WRITE_PG: u8 = 0xF2; | ||
| 20 | const CMD_QUAD_WRITE_PG: u8 = 0x32; | ||
| 21 | |||
| 22 | const CMD_READ_ID: u8 = 0x9F; | ||
| 23 | const CMD_READ_UUID: u8 = 0x4B; | ||
| 24 | |||
| 25 | const CMD_ENABLE_RESET: u8 = 0x66; | ||
| 26 | const CMD_RESET: u8 = 0x99; | ||
| 27 | |||
| 28 | const CMD_WRITE_ENABLE: u8 = 0x06; | ||
| 29 | const CMD_WRITE_DISABLE: u8 = 0x04; | ||
| 30 | |||
| 31 | const CMD_CHIP_ERASE: u8 = 0xC7; | ||
| 32 | const CMD_SECTOR_ERASE: u8 = 0x20; | ||
| 33 | const CMD_BLOCK_ERASE_32K: u8 = 0x52; | ||
| 34 | const CMD_BLOCK_ERASE_64K: u8 = 0xD8; | ||
| 35 | |||
| 36 | const CMD_READ_SR: u8 = 0x05; | ||
| 37 | const CMD_READ_CR: u8 = 0x35; | ||
| 38 | |||
| 39 | const CMD_WRITE_SR: u8 = 0x01; | ||
| 40 | const CMD_WRITE_CR: u8 = 0x31; | ||
| 41 | const MEMORY_ADDR: u32 = 0x00000000u32; | ||
| 42 | |||
| 43 | /// Implementation of access to flash chip. | ||
| 44 | /// Chip commands are hardcoded as it depends on used chip. | ||
| 45 | /// This implementation is using chip GD25Q64C from Giga Device | ||
| 46 | pub struct FlashMemory<I: Instance, D: QuadDma<I>> { | ||
| 47 | qspi: Qspi<'static, I, D>, | ||
| 48 | } | ||
| 49 | |||
| 50 | impl<I: Instance, D: QuadDma<I>> FlashMemory<I, D> { | ||
| 51 | pub fn new(qspi: Qspi<'static, I, D>) -> Self { | ||
| 52 | let mut memory = Self { qspi }; | ||
| 53 | |||
| 54 | memory.reset_memory(); | ||
| 55 | memory.enable_quad(); | ||
| 56 | |||
| 57 | memory | ||
| 58 | } | ||
| 59 | |||
| 60 | fn enable_quad(&mut self) { | ||
| 61 | let cr = self.read_cr(); | ||
| 62 | self.write_cr(cr | 0x02); | ||
| 63 | } | ||
| 64 | |||
| 65 | fn exec_command(&mut self, cmd: u8) { | ||
| 66 | let transaction = TransferConfig { | ||
| 67 | iwidth: QspiWidth::SING, | ||
| 68 | awidth: QspiWidth::NONE, | ||
| 69 | dwidth: QspiWidth::NONE, | ||
| 70 | instruction: cmd, | ||
| 71 | address: None, | ||
| 72 | dummy: DummyCycles::_0, | ||
| 73 | }; | ||
| 74 | self.qspi.command(transaction); | ||
| 75 | } | ||
| 76 | |||
| 77 | pub fn reset_memory(&mut self) { | ||
| 78 | self.exec_command(CMD_ENABLE_RESET); | ||
| 79 | self.exec_command(CMD_RESET); | ||
| 80 | self.wait_write_finish(); | ||
| 81 | } | ||
| 82 | |||
| 83 | pub fn enable_write(&mut self) { | ||
| 84 | self.exec_command(CMD_WRITE_ENABLE); | ||
| 85 | } | ||
| 86 | |||
| 87 | pub fn read_id(&mut self) -> [u8; 3] { | ||
| 88 | let mut buffer = [0; 3]; | ||
| 89 | let transaction: TransferConfig = TransferConfig { | ||
| 90 | iwidth: QspiWidth::SING, | ||
| 91 | awidth: QspiWidth::NONE, | ||
| 92 | dwidth: QspiWidth::SING, | ||
| 93 | instruction: CMD_READ_ID, | ||
| 94 | address: None, | ||
| 95 | dummy: DummyCycles::_0, | ||
| 96 | }; | ||
| 97 | self.qspi.blocking_read(&mut buffer, transaction); | ||
| 98 | buffer | ||
| 99 | } | ||
| 100 | |||
| 101 | pub fn read_uuid(&mut self) -> [u8; 16] { | ||
| 102 | let mut buffer = [0; 16]; | ||
| 103 | let transaction: TransferConfig = TransferConfig { | ||
| 104 | iwidth: QspiWidth::SING, | ||
| 105 | awidth: QspiWidth::SING, | ||
| 106 | dwidth: QspiWidth::SING, | ||
| 107 | instruction: CMD_READ_UUID, | ||
| 108 | address: Some(0), | ||
| 109 | dummy: DummyCycles::_8, | ||
| 110 | }; | ||
| 111 | self.qspi.blocking_read(&mut buffer, transaction); | ||
| 112 | buffer | ||
| 113 | } | ||
| 114 | |||
| 115 | pub fn read_memory(&mut self, addr: u32, buffer: &mut [u8], use_dma: bool) { | ||
| 116 | let transaction = TransferConfig { | ||
| 117 | iwidth: QspiWidth::SING, | ||
| 118 | awidth: QspiWidth::SING, | ||
| 119 | dwidth: QspiWidth::QUAD, | ||
| 120 | instruction: CMD_QUAD_READ, | ||
| 121 | address: Some(addr), | ||
| 122 | dummy: DummyCycles::_8, | ||
| 123 | }; | ||
| 124 | if use_dma { | ||
| 125 | self.qspi.blocking_read_dma(buffer, transaction); | ||
| 126 | } else { | ||
| 127 | self.qspi.blocking_read(buffer, transaction); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | fn wait_write_finish(&mut self) { | ||
| 132 | while (self.read_sr() & 0x01) != 0 {} | ||
| 133 | } | ||
| 134 | |||
| 135 | fn perform_erase(&mut self, addr: u32, cmd: u8) { | ||
| 136 | let transaction = TransferConfig { | ||
| 137 | iwidth: QspiWidth::SING, | ||
| 138 | awidth: QspiWidth::SING, | ||
| 139 | dwidth: QspiWidth::NONE, | ||
| 140 | instruction: cmd, | ||
| 141 | address: Some(addr), | ||
| 142 | dummy: DummyCycles::_0, | ||
| 143 | }; | ||
| 144 | self.enable_write(); | ||
| 145 | self.qspi.command(transaction); | ||
| 146 | self.wait_write_finish(); | ||
| 147 | } | ||
| 148 | |||
| 149 | pub fn erase_sector(&mut self, addr: u32) { | ||
| 150 | self.perform_erase(addr, CMD_SECTOR_ERASE); | ||
| 151 | } | ||
| 152 | |||
| 153 | pub fn erase_block_32k(&mut self, addr: u32) { | ||
| 154 | self.perform_erase(addr, CMD_BLOCK_ERASE_32K); | ||
| 155 | } | ||
| 156 | |||
| 157 | pub fn erase_block_64k(&mut self, addr: u32) { | ||
| 158 | self.perform_erase(addr, CMD_BLOCK_ERASE_64K); | ||
| 159 | } | ||
| 160 | |||
| 161 | pub fn erase_chip(&mut self) { | ||
| 162 | self.exec_command(CMD_CHIP_ERASE); | ||
| 163 | } | ||
| 164 | |||
| 165 | fn write_page(&mut self, addr: u32, buffer: &[u8], len: usize, use_dma: bool) { | ||
| 166 | assert!( | ||
| 167 | (len as u32 + (addr & 0x000000ff)) <= MEMORY_PAGE_SIZE as u32, | ||
| 168 | "write_page(): page write length exceeds page boundary (len = {}, addr = {:X}", | ||
| 169 | len, | ||
| 170 | addr | ||
| 171 | ); | ||
| 172 | |||
| 173 | let transaction = TransferConfig { | ||
| 174 | iwidth: QspiWidth::SING, | ||
| 175 | awidth: QspiWidth::SING, | ||
| 176 | dwidth: QspiWidth::QUAD, | ||
| 177 | instruction: CMD_QUAD_WRITE_PG, | ||
| 178 | address: Some(addr), | ||
| 179 | dummy: DummyCycles::_0, | ||
| 180 | }; | ||
| 181 | self.enable_write(); | ||
| 182 | if use_dma { | ||
| 183 | self.qspi.blocking_write_dma(buffer, transaction); | ||
| 184 | } else { | ||
| 185 | self.qspi.blocking_write(buffer, transaction); | ||
| 186 | } | ||
| 187 | self.wait_write_finish(); | ||
| 188 | } | ||
| 189 | |||
| 190 | pub fn write_memory(&mut self, addr: u32, buffer: &[u8], use_dma: bool) { | ||
| 191 | let mut left = buffer.len(); | ||
| 192 | let mut place = addr; | ||
| 193 | let mut chunk_start = 0; | ||
| 194 | |||
| 195 | while left > 0 { | ||
| 196 | let max_chunk_size = MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize; | ||
| 197 | let chunk_size = if left >= max_chunk_size { max_chunk_size } else { left }; | ||
| 198 | let chunk = &buffer[chunk_start..(chunk_start + chunk_size)]; | ||
| 199 | self.write_page(place, chunk, chunk_size, use_dma); | ||
| 200 | place += chunk_size as u32; | ||
| 201 | left -= chunk_size; | ||
| 202 | chunk_start += chunk_size; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | fn read_register(&mut self, cmd: u8) -> u8 { | ||
| 207 | let mut buffer = [0; 1]; | ||
| 208 | let transaction: TransferConfig = TransferConfig { | ||
| 209 | iwidth: QspiWidth::SING, | ||
| 210 | awidth: QspiWidth::NONE, | ||
| 211 | dwidth: QspiWidth::SING, | ||
| 212 | instruction: cmd, | ||
| 213 | address: None, | ||
| 214 | dummy: DummyCycles::_0, | ||
| 215 | }; | ||
| 216 | self.qspi.blocking_read(&mut buffer, transaction); | ||
| 217 | buffer[0] | ||
| 218 | } | ||
| 219 | |||
| 220 | fn write_register(&mut self, cmd: u8, value: u8) { | ||
| 221 | let buffer = [value; 1]; | ||
| 222 | let transaction: TransferConfig = TransferConfig { | ||
| 223 | iwidth: QspiWidth::SING, | ||
| 224 | awidth: QspiWidth::NONE, | ||
| 225 | dwidth: QspiWidth::SING, | ||
| 226 | instruction: cmd, | ||
| 227 | address: None, | ||
| 228 | dummy: DummyCycles::_0, | ||
| 229 | }; | ||
| 230 | self.qspi.blocking_write(&buffer, transaction); | ||
| 231 | } | ||
| 232 | |||
| 233 | pub fn read_sr(&mut self) -> u8 { | ||
| 234 | self.read_register(CMD_READ_SR) | ||
| 235 | } | ||
| 236 | |||
| 237 | pub fn read_cr(&mut self) -> u8 { | ||
| 238 | self.read_register(CMD_READ_CR) | ||
| 239 | } | ||
| 240 | |||
| 241 | pub fn write_sr(&mut self, value: u8) { | ||
| 242 | self.write_register(CMD_WRITE_SR, value); | ||
| 243 | } | ||
| 244 | |||
| 245 | pub fn write_cr(&mut self, value: u8) { | ||
| 246 | self.write_register(CMD_WRITE_CR, value); | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | #[embassy_executor::main] | ||
| 251 | async fn main(_spawner: Spawner) -> ! { | ||
| 252 | let mut config = StmCfg::default(); | ||
| 253 | { | ||
| 254 | use embassy_stm32::rcc::*; | ||
| 255 | config.rcc.hse = Some(Hse { | ||
| 256 | freq: mhz(8), | ||
| 257 | mode: HseMode::Oscillator, | ||
| 258 | }); | ||
| 259 | config.rcc.pll_src = PllSource::HSE; | ||
| 260 | config.rcc.pll = Some(Pll { | ||
| 261 | prediv: PllPreDiv::DIV4, | ||
| 262 | mul: PllMul::MUL216, | ||
| 263 | divp: Some(PllPDiv::DIV2), // 8mhz / 4 * 216 / 2 = 216Mhz | ||
| 264 | divq: None, | ||
| 265 | divr: None, | ||
| 266 | }); | ||
| 267 | config.rcc.ahb_pre = AHBPrescaler::DIV1; | ||
| 268 | config.rcc.apb1_pre = APBPrescaler::DIV4; | ||
| 269 | config.rcc.apb2_pre = APBPrescaler::DIV2; | ||
| 270 | config.rcc.sys = Sysclk::PLL1_P; | ||
| 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_bk1( | ||
| 283 | p.QUADSPI, p.PF8, p.PF9, p.PE2, p.PF6, p.PF10, p.PB10, p.DMA2_CH7, 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, true); | ||
| 295 | flash.read_memory(MEMORY_ADDR, &mut rd_buf, true); | ||
| 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 | } | ||
