aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-boot/stm32/src/lib.rs2
-rw-r--r--embassy-stm32/src/flash/f7.rs138
-rw-r--r--embassy-stm32/src/flash/mod.rs4
-rw-r--r--embassy-stm32/src/lib.rs2
-rw-r--r--examples/boot/stm32f7/.cargo/config.toml6
-rw-r--r--examples/boot/stm32f7/Cargo.toml26
-rw-r--r--examples/boot/stm32f7/README.md29
-rw-r--r--examples/boot/stm32f7/build.rs37
-rwxr-xr-xexamples/boot/stm32f7/flash-boot.sh8
-rw-r--r--examples/boot/stm32f7/memory-bl.x18
-rw-r--r--examples/boot/stm32f7/memory.x15
-rw-r--r--examples/boot/stm32f7/src/bin/a.rs44
-rw-r--r--examples/boot/stm32f7/src/bin/b.rs27
-rw-r--r--examples/stm32f7/Cargo.toml1
-rw-r--r--examples/stm32f7/src/bin/flash.rs59
15 files changed, 413 insertions, 3 deletions
diff --git a/embassy-boot/stm32/src/lib.rs b/embassy-boot/stm32/src/lib.rs
index 82e32a97d..48512534b 100644
--- a/embassy-boot/stm32/src/lib.rs
+++ b/embassy-boot/stm32/src/lib.rs
@@ -67,7 +67,7 @@ impl<const PAGE_SIZE: usize> BootLoader<PAGE_SIZE> {
67 [(); <<F as FlashProvider>::ACTIVE as FlashConfig>::FLASH::ERASE_SIZE]:, 67 [(); <<F as FlashProvider>::ACTIVE as FlashConfig>::FLASH::ERASE_SIZE]:,
68 { 68 {
69 match self.boot.prepare_boot(flash) { 69 match self.boot.prepare_boot(flash) {
70 Ok(_) => self.boot.boot_address(), 70 Ok(_) => embassy_stm32::flash::FLASH_BASE + self.boot.boot_address(),
71 Err(_) => panic!("boot prepare error!"), 71 Err(_) => panic!("boot prepare error!"),
72 } 72 }
73 } 73 }
diff --git a/embassy-stm32/src/flash/f7.rs b/embassy-stm32/src/flash/f7.rs
new file mode 100644
index 000000000..16316fd93
--- /dev/null
+++ b/embassy-stm32/src/flash/f7.rs
@@ -0,0 +1,138 @@
1use core::convert::TryInto;
2use core::ptr::write_volatile;
3
4use atomic_polyfill::{fence, Ordering};
5
6use crate::flash::Error;
7use crate::pac;
8
9pub(crate) unsafe fn lock() {
10 pac::FLASH.cr().modify(|w| w.set_lock(true));
11}
12
13pub(crate) unsafe fn unlock() {
14 pac::FLASH.keyr().write(|w| w.set_key(0x4567_0123));
15 pac::FLASH.keyr().write(|w| w.set_key(0xCDEF_89AB));
16}
17
18pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error> {
19 pac::FLASH.cr().write(|w| {
20 w.set_pg(true);
21 w.set_psize(pac::flash::vals::Psize::PSIZE32);
22 });
23
24 let ret = {
25 let mut ret: Result<(), Error> = Ok(());
26 let mut offset = offset;
27 for chunk in buf.chunks(super::WRITE_SIZE) {
28 for val in chunk.chunks(4) {
29 write_volatile(
30 offset as *mut u32,
31 u32::from_le_bytes(val[0..4].try_into().unwrap()),
32 );
33 offset += val.len() as u32;
34
35 // prevents parallelism errors
36 fence(Ordering::SeqCst);
37 }
38
39 ret = blocking_wait_ready();
40 if ret.is_err() {
41 break;
42 }
43 }
44 ret
45 };
46
47 pac::FLASH.cr().write(|w| w.set_pg(false));
48
49 ret
50}
51
52pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> {
53 let start_sector = if from >= (super::FLASH_BASE + super::ERASE_SIZE / 2) as u32 {
54 4 + (from - super::FLASH_BASE as u32) / super::ERASE_SIZE as u32
55 } else {
56 (from - super::FLASH_BASE as u32) / (super::ERASE_SIZE as u32 / 8)
57 };
58
59 let end_sector = if to >= (super::FLASH_BASE + super::ERASE_SIZE / 2) as u32 {
60 4 + (to - super::FLASH_BASE as u32) / super::ERASE_SIZE as u32
61 } else {
62 (to - super::FLASH_BASE as u32) / (super::ERASE_SIZE as u32 / 8)
63 };
64
65 for sector in start_sector..end_sector {
66 let ret = erase_sector(sector as u8);
67 if ret.is_err() {
68 return ret;
69 }
70 }
71
72 Ok(())
73}
74
75unsafe fn erase_sector(sector: u8) -> Result<(), Error> {
76 pac::FLASH.cr().modify(|w| {
77 w.set_ser(true);
78 w.set_snb(sector)
79 });
80
81 pac::FLASH.cr().modify(|w| {
82 w.set_strt(true);
83 });
84
85 let ret: Result<(), Error> = blocking_wait_ready();
86
87 pac::FLASH.cr().modify(|w| w.set_ser(false));
88
89 clear_all_err();
90
91 ret
92}
93
94pub(crate) unsafe fn clear_all_err() {
95 pac::FLASH.sr().modify(|w| {
96 if w.erserr() {
97 w.set_erserr(true);
98 }
99 if w.pgperr() {
100 w.set_pgperr(true);
101 }
102 if w.pgaerr() {
103 w.set_pgaerr(true);
104 }
105 if w.wrperr() {
106 w.set_wrperr(true);
107 }
108 if w.eop() {
109 w.set_eop(true);
110 }
111 });
112}
113
114pub(crate) unsafe fn blocking_wait_ready() -> Result<(), Error> {
115 loop {
116 let sr = pac::FLASH.sr().read();
117
118 if !sr.bsy() {
119 if sr.erserr() {
120 return Err(Error::Seq);
121 }
122
123 if sr.pgperr() {
124 return Err(Error::Parallelism);
125 }
126
127 if sr.pgaerr() {
128 return Err(Error::Unaligned);
129 }
130
131 if sr.wrperr() {
132 return Err(Error::Protected);
133 }
134
135 return Ok(());
136 }
137 }
138}
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs
index 8b2a77110..8efbe476c 100644
--- a/embassy-stm32/src/flash/mod.rs
+++ b/embassy-stm32/src/flash/mod.rs
@@ -16,6 +16,7 @@ const FLASH_END: usize = FLASH_BASE + FLASH_SIZE;
16 16
17#[cfg_attr(any(flash_wl, flash_wb, flash_l0, flash_l1, flash_l4), path = "l.rs")] 17#[cfg_attr(any(flash_wl, flash_wb, flash_l0, flash_l1, flash_l4), path = "l.rs")]
18#[cfg_attr(flash_f3, path = "f3.rs")] 18#[cfg_attr(flash_f3, path = "f3.rs")]
19#[cfg_attr(flash_f7, path = "f7.rs")]
19mod family; 20mod family;
20 21
21pub struct Flash<'d> { 22pub struct Flash<'d> {
@@ -75,7 +76,7 @@ impl<'d> Flash<'d> {
75 if to < from || to as usize > FLASH_END { 76 if to < from || to as usize > FLASH_END {
76 return Err(Error::Size); 77 return Err(Error::Size);
77 } 78 }
78 if from as usize % ERASE_SIZE != 0 || to as usize % ERASE_SIZE != 0 { 79 if ((to - from) as usize % ERASE_SIZE) != 0 {
79 return Err(Error::Unaligned); 80 return Err(Error::Unaligned);
80 } 81 }
81 82
@@ -104,6 +105,7 @@ pub enum Error {
104 Seq, 105 Seq,
105 Protected, 106 Protected,
106 Unaligned, 107 Unaligned,
108 Parallelism,
107} 109}
108 110
109impl<'d> ErrorType for Flash<'d> { 111impl<'d> ErrorType for Flash<'d> {
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index 8f9b4a58d..74d8ed86a 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -50,7 +50,7 @@ pub mod i2c;
50 50
51#[cfg(crc)] 51#[cfg(crc)]
52pub mod crc; 52pub mod crc;
53#[cfg(any(flash_l0, flash_l1, flash_wl, flash_wb, flash_l4, flash_f3))] 53#[cfg(any(flash_l0, flash_l1, flash_wl, flash_wb, flash_l4, flash_f3, flash_f7))]
54pub mod flash; 54pub mod flash;
55pub mod pwm; 55pub mod pwm;
56#[cfg(rng)] 56#[cfg(rng)]
diff --git a/examples/boot/stm32f7/.cargo/config.toml b/examples/boot/stm32f7/.cargo/config.toml
new file mode 100644
index 000000000..df5114520
--- /dev/null
+++ b/examples/boot/stm32f7/.cargo/config.toml
@@ -0,0 +1,6 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
3runner = "probe-run --chip STM32F767ZITx -v"
4
5[build]
6target = "thumbv7em-none-eabihf"
diff --git a/examples/boot/stm32f7/Cargo.toml b/examples/boot/stm32f7/Cargo.toml
new file mode 100644
index 000000000..857b287d5
--- /dev/null
+++ b/examples/boot/stm32f7/Cargo.toml
@@ -0,0 +1,26 @@
1[package]
2authors = ["Ulf Lilleengen <[email protected]>"]
3edition = "2021"
4name = "embassy-boot-stm32f7-examples"
5version = "0.1.0"
6
7[dependencies]
8embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] }
9embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f767zi", "time-driver-any", "exti"] }
10embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" }
11embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" }
12
13defmt = { version = "0.3", optional = true }
14defmt-rtt = { version = "0.3", optional = true }
15panic-reset = { version = "0.1.1" }
16embedded-hal = { version = "0.2.6" }
17
18cortex-m = "0.7.3"
19cortex-m-rt = "0.7.0"
20
21[features]
22defmt = [
23 "dep:defmt",
24 "embassy-stm32/defmt",
25 "embassy-boot-stm32/defmt",
26]
diff --git a/examples/boot/stm32f7/README.md b/examples/boot/stm32f7/README.md
new file mode 100644
index 000000000..bf9142a1c
--- /dev/null
+++ b/examples/boot/stm32f7/README.md
@@ -0,0 +1,29 @@
1# Examples using bootloader
2
3Example for STM32F7 demonstrating the bootloader. The example consists of application binaries, 'a'
4which allows you to press a button to start the DFU process, and 'b' which is the updated
5application.
6
7
8## Prerequisites
9
10* `cargo-binutils`
11* `cargo-flash`
12* `embassy-boot-stm32`
13
14## Usage
15
16```
17# Flash bootloader
18./flash-boot.sh
19# Build 'b'
20cargo build --release --bin b
21# Generate binary for 'b'
22cargo objcopy --release --bin b -- -O binary b.bin
23```
24
25# Flash `a` (which includes b.bin)
26
27```
28cargo flash --release --bin a --chip STM32F767ZITx
29```
diff --git a/examples/boot/stm32f7/build.rs b/examples/boot/stm32f7/build.rs
new file mode 100644
index 000000000..e1da69328
--- /dev/null
+++ b/examples/boot/stm32f7/build.rs
@@ -0,0 +1,37 @@
1//! This build script copies the `memory.x` file from the crate root into
2//! a directory where the linker can always find it at build time.
3//! For many projects this is optional, as the linker always searches the
4//! project root directory -- wherever `Cargo.toml` is. However, if you
5//! are using a workspace or have a more complicated build setup, this
6//! build script becomes required. Additionally, by requesting that
7//! Cargo re-run the build script whenever `memory.x` is changed,
8//! updating `memory.x` ensures a rebuild of the application with the
9//! new memory settings.
10
11use std::env;
12use std::fs::File;
13use std::io::Write;
14use std::path::PathBuf;
15
16fn main() {
17 // Put `memory.x` in our output directory and ensure it's
18 // on the linker search path.
19 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
20 File::create(out.join("memory.x"))
21 .unwrap()
22 .write_all(include_bytes!("memory.x"))
23 .unwrap();
24 println!("cargo:rustc-link-search={}", out.display());
25
26 // By default, Cargo will re-run a build script whenever
27 // any file in the project changes. By specifying `memory.x`
28 // here, we ensure the build script is only re-run when
29 // `memory.x` is changed.
30 println!("cargo:rerun-if-changed=memory.x");
31
32 println!("cargo:rustc-link-arg-bins=--nmagic");
33 println!("cargo:rustc-link-arg-bins=-Tlink.x");
34 if env::var("CARGO_FEATURE_DEFMT").is_ok() {
35 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
36 }
37}
diff --git a/examples/boot/stm32f7/flash-boot.sh b/examples/boot/stm32f7/flash-boot.sh
new file mode 100755
index 000000000..86074ffa3
--- /dev/null
+++ b/examples/boot/stm32f7/flash-boot.sh
@@ -0,0 +1,8 @@
1#!/bin/bash
2mv ../../../embassy-boot/stm32/memory.x ../../../embassy-boot/stm32/memory-old.x
3cp memory-bl.x ../../../embassy-boot/stm32/memory.x
4
5cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f767zi --chip STM32F767ZITx --target thumbv7em-none-eabihf
6
7rm ../../../embassy-boot/stm32/memory.x
8mv ../../../embassy-boot/stm32/memory-old.x ../../../embassy-boot/stm32/memory.x
diff --git a/examples/boot/stm32f7/memory-bl.x b/examples/boot/stm32f7/memory-bl.x
new file mode 100644
index 000000000..47f3f4d9b
--- /dev/null
+++ b/examples/boot/stm32f7/memory-bl.x
@@ -0,0 +1,18 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 FLASH : ORIGIN = 0x08000000, LENGTH = 256K
5 BOOTLOADER_STATE : ORIGIN = 0x08040000, LENGTH = 256K
6 ACTIVE : ORIGIN = 0x08080000, LENGTH = 256K
7 DFU : ORIGIN = 0x080c0000, LENGTH = 512K
8 RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 368K + 16K
9}
10
11__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(FLASH);
12__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(FLASH);
13
14__bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(FLASH);
15__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(FLASH);
16
17__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(FLASH);
18__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(FLASH);
diff --git a/examples/boot/stm32f7/memory.x b/examples/boot/stm32f7/memory.x
new file mode 100644
index 000000000..1c5537d17
--- /dev/null
+++ b/examples/boot/stm32f7/memory.x
@@ -0,0 +1,15 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 256K
5 BOOTLOADER_STATE : ORIGIN = 0x08040000, LENGTH = 256K
6 FLASH : ORIGIN = 0x08080000, LENGTH = 256K
7 DFU : ORIGIN = 0x080c0000, LENGTH = 512K
8 RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 368K + 16K
9}
10
11__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
12__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
13
14__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER);
15__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER);
diff --git a/examples/boot/stm32f7/src/bin/a.rs b/examples/boot/stm32f7/src/bin/a.rs
new file mode 100644
index 000000000..ca154f0af
--- /dev/null
+++ b/examples/boot/stm32f7/src/bin/a.rs
@@ -0,0 +1,44 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use embassy_boot_stm32::FirmwareUpdater;
6use embassy_stm32::exti::ExtiInput;
7use embassy_stm32::flash::Flash;
8use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
9use embassy_stm32::Peripherals;
10use embassy_traits::adapter::BlockingAsync;
11use panic_reset as _;
12
13#[cfg(feature = "defmt-rtt")]
14use defmt_rtt::*;
15
16static APP_B: &[u8] = include_bytes!("../../b.bin");
17
18#[embassy::main]
19async fn main(_s: embassy::executor::Spawner, p: Peripherals) {
20 let flash = Flash::unlock(p.FLASH);
21 let mut flash = BlockingAsync::new(flash);
22
23 let button = Input::new(p.PC13, Pull::Down);
24 let mut button = ExtiInput::new(button, p.EXTI13);
25
26 let mut led = Output::new(p.PB7, Level::Low, Speed::Low);
27 led.set_high();
28
29 let mut updater = FirmwareUpdater::default();
30 button.wait_for_rising_edge().await;
31 let mut offset = 0;
32 let mut buf: [u8; 256 * 1024] = [0; 256 * 1024];
33 for chunk in APP_B.chunks(256 * 1024) {
34 buf[..chunk.len()].copy_from_slice(chunk);
35 updater
36 .write_firmware(offset, &buf, &mut flash, 2048)
37 .await
38 .unwrap();
39 offset += chunk.len();
40 }
41 updater.update(&mut flash).await.unwrap();
42 led.set_low();
43 cortex_m::peripheral::SCB::sys_reset();
44}
diff --git a/examples/boot/stm32f7/src/bin/b.rs b/examples/boot/stm32f7/src/bin/b.rs
new file mode 100644
index 000000000..ed37137f5
--- /dev/null
+++ b/examples/boot/stm32f7/src/bin/b.rs
@@ -0,0 +1,27 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use embassy::executor::Spawner;
6use embassy::time::{Duration, Timer};
7use embassy_stm32::gpio::{Level, Output, Speed};
8use embassy_stm32::Peripherals;
9use panic_reset as _;
10
11#[cfg(feature = "defmt-rtt")]
12use defmt_rtt::*;
13
14#[embassy::main]
15async fn main(_spawner: Spawner, p: Peripherals) {
16 Timer::after(Duration::from_millis(300)).await;
17 let mut led = Output::new(p.PB7, Level::High, Speed::Low);
18 led.set_high();
19
20 loop {
21 led.set_high();
22 Timer::after(Duration::from_millis(500)).await;
23
24 led.set_low();
25 Timer::after(Duration::from_millis(500)).await;
26 }
27}
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml
index 09a06aa7f..ce0a6d48f 100644
--- a/examples/stm32f7/Cargo.toml
+++ b/examples/stm32f7/Cargo.toml
@@ -22,6 +22,7 @@ heapless = { version = "0.7.5", default-features = false }
22nb = "1.0.0" 22nb = "1.0.0"
23rand_core = "0.6.3" 23rand_core = "0.6.3"
24critical-section = "0.2.3" 24critical-section = "0.2.3"
25embedded-storage = "0.3.0"
25 26
26[dependencies.smoltcp] 27[dependencies.smoltcp]
27version = "0.8.0" 28version = "0.8.0"
diff --git a/examples/stm32f7/src/bin/flash.rs b/examples/stm32f7/src/bin/flash.rs
new file mode 100644
index 000000000..9eb8e4b94
--- /dev/null
+++ b/examples/stm32f7/src/bin/flash.rs
@@ -0,0 +1,59 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy::executor::Spawner;
7use embassy::time::{Duration, Timer};
8use embassy_stm32::flash::Flash;
9use embassy_stm32::Peripherals;
10use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
11
12use defmt_rtt as _; // global logger
13use panic_probe as _;
14
15#[embassy::main]
16async fn main(_spawner: Spawner, p: Peripherals) {
17 info!("Hello Flash!");
18
19 const ADDR: u32 = 0x8_0000;
20
21 // wait a bit before accessing the flash
22 Timer::after(Duration::from_millis(300)).await;
23
24 let mut f = Flash::unlock(p.FLASH);
25
26 info!("Reading...");
27 let mut buf = [0u8; 32];
28 unwrap!(f.read(ADDR, &mut buf));
29 info!("Read: {=[u8]:x}", buf);
30
31 info!("Erasing...");
32 unwrap!(f.erase(ADDR, ADDR + 256 * 1024));
33
34 info!("Reading...");
35 let mut buf = [0u8; 32];
36 unwrap!(f.read(ADDR, &mut buf));
37 info!("Read after erase: {=[u8]:x}", buf);
38
39 info!("Writing...");
40 unwrap!(f.write(
41 ADDR,
42 &[
43 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
44 25, 26, 27, 28, 29, 30, 31, 32
45 ]
46 ));
47
48 info!("Reading...");
49 let mut buf = [0u8; 32];
50 unwrap!(f.read(ADDR, &mut buf));
51 info!("Read: {=[u8]:x}", buf);
52 assert_eq!(
53 &buf[..],
54 &[
55 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
56 25, 26, 27, 28, 29, 30, 31, 32
57 ]
58 );
59}