aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
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.rs292
4 files changed, 372 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..aee07f3f2
--- /dev/null
+++ b/examples/stm32h742/src/bin/qspi.rs
@@ -0,0 +1,292 @@
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::{AddressSize, ChipSelectHighTime, FIFOThresholdLevel, MemorySize, *};
9use embassy_stm32::qspi::{Config as QspiCfg, Instance, Qspi, TransferConfig};
10use embassy_stm32::Config as StmCfg;
11use {defmt_rtt as _, panic_probe as _};
12
13const MEMORY_PAGE_SIZE: usize = 256;
14
15const CMD_READ: u8 = 0x03;
16const CMD_HS_READ: u8 = 0x0B;
17const CMD_QUAD_READ: u8 = 0x6B;
18
19const CMD_WRITE_PG: u8 = 0xF2;
20const CMD_QUAD_WRITE_PG: u8 = 0x32;
21
22const CMD_READ_ID: u8 = 0x9F;
23const CMD_READ_UUID: u8 = 0x4B;
24
25const CMD_ENABLE_RESET: u8 = 0x66;
26const CMD_RESET: u8 = 0x99;
27
28const CMD_WRITE_ENABLE: u8 = 0x06;
29const CMD_WRITE_DISABLE: u8 = 0x04;
30
31const CMD_CHIP_ERASE: u8 = 0xC7;
32const CMD_SECTOR_ERASE: u8 = 0x20;
33const CMD_BLOCK_ERASE_32K: u8 = 0x52;
34const CMD_BLOCK_ERASE_64K: u8 = 0xD8;
35
36const CMD_READ_SR: u8 = 0x05;
37const CMD_READ_CR: u8 = 0x35;
38
39const CMD_WRITE_SR: u8 = 0x01;
40const CMD_WRITE_CR: u8 = 0x31;
41const MEMORY_ADDR: u32 = 0x00000001u32;
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
46pub struct FlashMemory<I: Instance> {
47 qspi: Qspi<'static, I, Blocking>,
48}
49
50impl<I: Instance> FlashMemory<I> {
51 pub fn new(qspi: Qspi<'static, I, Blocking>) -> 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.blocking_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]) {
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 self.qspi.blocking_read(buffer, transaction);
125 }
126
127 fn wait_write_finish(&mut self) {
128 while (self.read_sr() & 0x01) != 0 {}
129 }
130
131 fn perform_erase(&mut self, addr: u32, cmd: u8) {
132 let transaction = TransferConfig {
133 iwidth: QspiWidth::SING,
134 awidth: QspiWidth::SING,
135 dwidth: QspiWidth::NONE,
136 instruction: cmd,
137 address: Some(addr),
138 dummy: DummyCycles::_0,
139 };
140 self.enable_write();
141 self.qspi.blocking_command(transaction);
142 self.wait_write_finish();
143 }
144
145 pub fn erase_sector(&mut self, addr: u32) {
146 self.perform_erase(addr, CMD_SECTOR_ERASE);
147 }
148
149 pub fn erase_block_32k(&mut self, addr: u32) {
150 self.perform_erase(addr, CMD_BLOCK_ERASE_32K);
151 }
152
153 pub fn erase_block_64k(&mut self, addr: u32) {
154 self.perform_erase(addr, CMD_BLOCK_ERASE_64K);
155 }
156
157 pub fn erase_chip(&mut self) {
158 self.exec_command(CMD_CHIP_ERASE);
159 }
160
161 fn write_page(&mut self, addr: u32, buffer: &[u8], len: usize) {
162 assert!(
163 (len as u32 + (addr & 0x000000ff)) <= MEMORY_PAGE_SIZE as u32,
164 "write_page(): page write length exceeds page boundary (len = {}, addr = {:X}",
165 len,
166 addr
167 );
168
169 let transaction = TransferConfig {
170 iwidth: QspiWidth::SING,
171 awidth: QspiWidth::SING,
172 dwidth: QspiWidth::QUAD,
173 instruction: CMD_QUAD_WRITE_PG,
174 address: Some(addr),
175 dummy: DummyCycles::_0,
176 };
177 self.enable_write();
178 self.qspi.blocking_write(buffer, transaction);
179 self.wait_write_finish();
180 }
181
182 pub fn write_memory(&mut self, addr: u32, buffer: &[u8]) {
183 let mut left = buffer.len();
184 let mut place = addr;
185 let mut chunk_start = 0;
186
187 while left > 0 {
188 let max_chunk_size = MEMORY_PAGE_SIZE - (place & 0x000000ff) as usize;
189 let chunk_size = if left >= max_chunk_size { max_chunk_size } else { left };
190 let chunk = &buffer[chunk_start..(chunk_start + chunk_size)];
191 self.write_page(place, chunk, chunk_size);
192 place += chunk_size as u32;
193 left -= chunk_size;
194 chunk_start += chunk_size;
195 }
196 }
197
198 fn read_register(&mut self, cmd: u8) -> u8 {
199 let mut buffer = [0; 1];
200 let transaction: TransferConfig = TransferConfig {
201 iwidth: QspiWidth::SING,
202 awidth: QspiWidth::NONE,
203 dwidth: QspiWidth::SING,
204 instruction: cmd,
205 address: None,
206 dummy: DummyCycles::_0,
207 };
208 self.qspi.blocking_read(&mut buffer, transaction);
209 buffer[0]
210 }
211
212 fn write_register(&mut self, cmd: u8, value: u8) {
213 let buffer = [value; 1];
214 let transaction: TransferConfig = TransferConfig {
215 iwidth: QspiWidth::SING,
216 awidth: QspiWidth::NONE,
217 dwidth: QspiWidth::SING,
218 instruction: cmd,
219 address: None,
220 dummy: DummyCycles::_0,
221 };
222 self.qspi.blocking_write(&buffer, transaction);
223 }
224
225 pub fn read_sr(&mut self) -> u8 {
226 self.read_register(CMD_READ_SR)
227 }
228
229 pub fn read_cr(&mut self) -> u8 {
230 self.read_register(CMD_READ_CR)
231 }
232
233 pub fn write_sr(&mut self, value: u8) {
234 self.write_register(CMD_WRITE_SR, value);
235 }
236
237 pub fn write_cr(&mut self, value: u8) {
238 self.write_register(CMD_WRITE_CR, value);
239 }
240}
241
242#[embassy_executor::main]
243async fn main(_spawner: Spawner) -> ! {
244 let mut config = StmCfg::default();
245 {
246 use embassy_stm32::rcc::*;
247 config.rcc.hsi = Some(HSIPrescaler::DIV1);
248 config.rcc.csi = true;
249 config.rcc.hsi48 = Some(Default::default()); // needed for RNG
250 config.rcc.pll1 = Some(Pll {
251 source: PllSource::HSI,
252 prediv: PllPreDiv::DIV4,
253 mul: PllMul::MUL50,
254 divp: Some(PllDiv::DIV2),
255 divq: None,
256 divr: None,
257 });
258 config.rcc.sys = Sysclk::PLL1_P; // 400 Mhz
259 config.rcc.ahb_pre = AHBPrescaler::DIV2; // 200 Mhz
260 config.rcc.apb1_pre = APBPrescaler::DIV2; // 100 Mhz
261 config.rcc.apb2_pre = APBPrescaler::DIV2; // 100 Mhz
262 config.rcc.apb3_pre = APBPrescaler::DIV2; // 100 Mhz
263 config.rcc.apb4_pre = APBPrescaler::DIV2; // 100 Mhz
264 config.rcc.voltage_scale = VoltageScale::Scale1;
265 }
266 let p = embassy_stm32::init(config);
267 info!("Embassy initialized");
268
269 let config = QspiCfg {
270 memory_size: MemorySize::_8MiB,
271 address_size: AddressSize::_24bit,
272 prescaler: 16,
273 cs_high_time: ChipSelectHighTime::_1Cycle,
274 fifo_threshold: FIFOThresholdLevel::_16Bytes,
275 };
276 let driver = Qspi::new_blocking_bank1(p.QUADSPI, p.PD11, p.PD12, p.PE2, p.PD13, p.PB2, p.PB10, config);
277 let mut flash = FlashMemory::new(driver);
278 let flash_id = flash.read_id();
279 info!("FLASH ID: {:?}", flash_id);
280 let mut wr_buf = [0u8; 256];
281 for i in 0..256 {
282 wr_buf[i] = i as u8;
283 }
284 let mut rd_buf = [0u8; 256];
285 flash.erase_sector(MEMORY_ADDR);
286 flash.write_memory(MEMORY_ADDR, &wr_buf);
287 flash.read_memory(MEMORY_ADDR, &mut rd_buf);
288 info!("WRITE BUF: {:?}", wr_buf);
289 info!("READ BUF: {:?}", rd_buf);
290 info!("End of Program, proceed to empty endless loop");
291 loop {}
292}