aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMick Chanthaseth <[email protected]>2025-04-10 14:02:39 -0700
committerMick Chanthaseth <[email protected]>2025-04-10 14:02:39 -0700
commite4739113b7c31a2949b4ebab7ac0ff2d44c39d6a (patch)
treee1b5e61b63dc08e0afafa0ae6614299ed0d052e8 /examples
parentefde7e2841a993c996b3e90baa20922f3b41226c (diff)
added qspi example for stm32h742
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32h742/.cargo/config.toml9
-rw-r--r--examples/stm32h742/Cargo.toml66
-rw-r--r--examples/stm32h742/build.rs5
-rw-r--r--examples/stm32h742/src/bin/qspi.rs300
4 files changed, 380 insertions, 0 deletions
diff --git a/examples/stm32h742/.cargo/config.toml b/examples/stm32h742/.cargo/config.toml
new file mode 100644
index 000000000..f30a52a79
--- /dev/null
+++ b/examples/stm32h742/.cargo/config.toml
@@ -0,0 +1,9 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2# replace STM32H742VITx with your chip as listed in `probe-rs chip list`
3runner = "probe-rs run --chip STM32H742VITx"
4
5[build]
6target = "thumbv7em-none-eabihf"
7
8[env]
9DEFMT_LOG = "trace"
diff --git a/examples/stm32h742/Cargo.toml b/examples/stm32h742/Cargo.toml
new file mode 100644
index 000000000..e2e0094b8
--- /dev/null
+++ b/examples/stm32h742/Cargo.toml
@@ -0,0 +1,66 @@
1[package]
2edition = "2021"
3name = "embassy-stm32f7-examples"
4version = "0.1.0"
5license = "MIT OR Apache-2.0"
6
7[dependencies]
8# Change stm32f777zi to your chip name, if necessary.
9embassy-stm32 = { version = "0.2.0", path = "../../embassy-stm32", features = [
10 "defmt",
11 "stm32h742vi",
12 "memory-x",
13 "unstable-pac",
14 "time-driver-any",
15 "exti",
16] }
17embassy-sync = { version = "0.6.2", path = "../../embassy-sync", features = [
18 "defmt",
19] }
20embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = [
21 "arch-cortex-m",
22 "executor-thread",
23 "defmt",
24] }
25embassy-time = { version = "0.4.0", path = "../../embassy-time", features = [
26 "defmt",
27 "defmt-timestamp-uptime",
28 "tick-hz-32_768",
29] }
30embassy-net = { version = "0.7.0", path = "../../embassy-net", features = [
31 "defmt",
32 "tcp",
33 "dhcpv4",
34 "medium-ethernet",
35] }
36embedded-io-async = { version = "0.6.1" }
37embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = [
38 "defmt",
39] }
40embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
41
42defmt = "0.3"
43defmt-rtt = "0.4"
44
45cortex-m = { version = "0.7.6", features = [
46 "inline-asm",
47 "critical-section-single-core",
48] }
49cortex-m-rt = "0.7.0"
50embedded-hal = "0.2.6"
51panic-probe = { version = "0.3", features = ["print-defmt"] }
52heapless = { version = "0.8", default-features = false }
53nb = "1.0.0"
54rand_core = "0.6.3"
55critical-section = "1.1"
56embedded-storage = "0.3.1"
57static_cell = "2"
58sha2 = { version = "0.10.8", default-features = false }
59hmac = "0.12.1"
60aes-gcm = { version = "0.10.3", default-features = false, features = [
61 "aes",
62 "heapless",
63] }
64
65[profile.release]
66debug = 2
diff --git a/examples/stm32h742/build.rs b/examples/stm32h742/build.rs
new file mode 100644
index 000000000..8cd32d7ed
--- /dev/null
+++ b/examples/stm32h742/build.rs
@@ -0,0 +1,5 @@
1fn main() {
2 println!("cargo:rustc-link-arg-bins=--nmagic");
3 println!("cargo:rustc-link-arg-bins=-Tlink.x");
4 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
5}
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
5use defmt::info;
6use embassy_executor::Spawner;
7use embassy_stm32::mode::Blocking;
8use embassy_stm32::qspi::enums::{
9 AddressSize, ChipSelectHighTime, FIFOThresholdLevel, MemorySize, *,
10};
11use embassy_stm32::qspi::{Config as QspiCfg, Instance, Qspi, TransferConfig};
12use embassy_stm32::Config as StmCfg;
13use {defmt_rtt as _, panic_probe as _};
14
15const MEMORY_PAGE_SIZE: usize = 256;
16
17const CMD_READ: u8 = 0x03;
18const CMD_HS_READ: u8 = 0x0B;
19const CMD_QUAD_READ: u8 = 0x6B;
20
21const CMD_WRITE_PG: u8 = 0xF2;
22const CMD_QUAD_WRITE_PG: u8 = 0x32;
23
24const CMD_READ_ID: u8 = 0x9F;
25const CMD_READ_UUID: u8 = 0x4B;
26
27const CMD_ENABLE_RESET: u8 = 0x66;
28const CMD_RESET: u8 = 0x99;
29
30const CMD_WRITE_ENABLE: u8 = 0x06;
31const CMD_WRITE_DISABLE: u8 = 0x04;
32
33const CMD_CHIP_ERASE: u8 = 0xC7;
34const CMD_SECTOR_ERASE: u8 = 0x20;
35const CMD_BLOCK_ERASE_32K: u8 = 0x52;
36const CMD_BLOCK_ERASE_64K: u8 = 0xD8;
37
38const CMD_READ_SR: u8 = 0x05;
39const CMD_READ_CR: u8 = 0x35;
40
41const CMD_WRITE_SR: u8 = 0x01;
42const CMD_WRITE_CR: u8 = 0x31;
43const 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
48pub struct FlashMemory<I: Instance> {
49 qspi: Qspi<'static, I, Blocking>,
50}
51
52impl<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]
249async 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}