Add support for F3 flash
This commit is contained in:
parent
f7af9a549f
commit
4f5088d79d
14 changed files with 314 additions and 32 deletions
|
@ -2,7 +2,7 @@
|
|||
authors = [
|
||||
"Ulf Lilleengen <lulf@redhat.com>",
|
||||
]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
name = "embassy-boot-stm32"
|
||||
version = "0.1.0"
|
||||
description = "Bootloader for STM32 chips"
|
||||
|
|
|
@ -33,13 +33,13 @@ impl<'d> Flash<'d> {
|
|||
|
||||
pub fn unlock(p: impl Unborrow<Target = FLASH>) -> Self {
|
||||
let flash = Self::new(p);
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_f3))]
|
||||
unsafe {
|
||||
pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123));
|
||||
pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB));
|
||||
}
|
||||
|
||||
#[cfg(any(flash_l0))]
|
||||
#[cfg(any(flash_l0, flash_l1))]
|
||||
unsafe {
|
||||
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x89ABCDEF));
|
||||
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x02030405));
|
||||
|
@ -51,7 +51,7 @@ impl<'d> Flash<'d> {
|
|||
}
|
||||
|
||||
pub fn lock(&mut self) {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_f3))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().modify(|w| w.set_lock(true));
|
||||
}
|
||||
|
@ -89,31 +89,56 @@ impl<'d> Flash<'d> {
|
|||
|
||||
self.clear_all_err();
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_f3))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().write(|w| w.set_pg(true))
|
||||
}
|
||||
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
for chunk in buf.chunks(WRITE_SIZE) {
|
||||
for val in chunk.chunks(4) {
|
||||
#[cfg(not(flash_f3))]
|
||||
let ret = {
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
for chunk in buf.chunks(WRITE_SIZE) {
|
||||
for val in chunk.chunks(4) {
|
||||
unsafe {
|
||||
write_volatile(
|
||||
offset as *mut u32,
|
||||
u32::from_le_bytes(val[0..4].try_into().unwrap()),
|
||||
);
|
||||
}
|
||||
offset += val.len() as u32;
|
||||
}
|
||||
|
||||
ret = self.blocking_wait_ready();
|
||||
if ret.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret
|
||||
};
|
||||
|
||||
#[cfg(flash_f3)]
|
||||
let ret = {
|
||||
let mut ret: Result<(), Error> = Ok(());
|
||||
let mut offset = offset;
|
||||
for chunk in buf.chunks(2) {
|
||||
unsafe {
|
||||
write_volatile(
|
||||
offset as *mut u32,
|
||||
u32::from_le_bytes(val[0..4].try_into().unwrap()),
|
||||
offset as *mut u16,
|
||||
u16::from_le_bytes(chunk[0..2].try_into().unwrap()),
|
||||
);
|
||||
}
|
||||
offset += val.len() as u32;
|
||||
}
|
||||
offset += chunk.len() as u32;
|
||||
|
||||
ret = self.blocking_wait_ready();
|
||||
if ret.is_err() {
|
||||
break;
|
||||
ret = self.blocking_wait_ready();
|
||||
if ret.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
};
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_f3))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().write(|w| w.set_pg(false))
|
||||
}
|
||||
|
@ -144,23 +169,41 @@ impl<'d> Flash<'d> {
|
|||
write_volatile(page as *mut u32, 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_f3))]
|
||||
unsafe {
|
||||
#[cfg(not(flash_f3))]
|
||||
let idx = page / ERASE_SIZE as u32;
|
||||
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_per(true);
|
||||
#[cfg(not(flash_f3))]
|
||||
w.set_pnb(idx as u8);
|
||||
#[cfg(any(flash_wl, flash_wb))]
|
||||
w.set_strt(true);
|
||||
#[cfg(any(flash_l4))]
|
||||
w.set_start(true);
|
||||
});
|
||||
|
||||
#[cfg(flash_f3)]
|
||||
pac::FLASH.ar().write(|w| w.set_far(page));
|
||||
|
||||
#[cfg(flash_f3)]
|
||||
pac::FLASH.cr().modify(|w| {
|
||||
w.set_strt(true);
|
||||
});
|
||||
}
|
||||
|
||||
let ret: Result<(), Error> = self.blocking_wait_ready();
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
unsafe {
|
||||
if pac::FLASH.sr().read().eop() {
|
||||
pac::FLASH.sr().modify(|w| w.set_eop(true));
|
||||
} else {
|
||||
panic!("wtf")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_f3))]
|
||||
unsafe {
|
||||
pac::FLASH.cr().modify(|w| w.set_per(false));
|
||||
}
|
||||
|
@ -196,10 +239,12 @@ impl<'d> Flash<'d> {
|
|||
return Err(Error::Protected);
|
||||
}
|
||||
|
||||
#[cfg(not(flash_f3))]
|
||||
if sr.pgaerr() {
|
||||
return Err(Error::Unaligned);
|
||||
}
|
||||
|
||||
#[cfg(not(flash_f3))]
|
||||
if sr.sizerr() {
|
||||
return Err(Error::Size);
|
||||
}
|
||||
|
@ -213,6 +258,11 @@ impl<'d> Flash<'d> {
|
|||
if sr.pgserr() {
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
|
||||
#[cfg(flash_f3)]
|
||||
if sr.pgerr() {
|
||||
return Err(Error::Seq);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
@ -223,36 +273,42 @@ impl<'d> Flash<'d> {
|
|||
pac::FLASH.sr().modify(|w| {
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4, flash_l0))]
|
||||
if w.rderr() {
|
||||
w.set_rderr(false);
|
||||
w.set_rderr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.fasterr() {
|
||||
w.set_fasterr(false);
|
||||
w.set_fasterr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.miserr() {
|
||||
w.set_miserr(false);
|
||||
w.set_miserr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.pgserr() {
|
||||
w.set_pgserr(false);
|
||||
w.set_pgserr(true);
|
||||
}
|
||||
#[cfg(flash_f3)]
|
||||
if w.pgerr() {
|
||||
w.set_pgerr(true);
|
||||
}
|
||||
#[cfg(not(flash_f3))]
|
||||
if w.sizerr() {
|
||||
w.set_sizerr(false);
|
||||
w.set_sizerr(true);
|
||||
}
|
||||
#[cfg(not(flash_f3))]
|
||||
if w.pgaerr() {
|
||||
w.set_pgaerr(false);
|
||||
w.set_pgaerr(true);
|
||||
}
|
||||
if w.wrperr() {
|
||||
w.set_wrperr(false);
|
||||
w.set_wrperr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.progerr() {
|
||||
w.set_progerr(false);
|
||||
w.set_progerr(true);
|
||||
}
|
||||
#[cfg(any(flash_wl, flash_wb, flash_l4))]
|
||||
if w.operr() {
|
||||
w.set_operr(false);
|
||||
w.set_operr(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ pub mod i2c;
|
|||
|
||||
#[cfg(crc)]
|
||||
pub mod crc;
|
||||
#[cfg(any(flash_l0, flash_l1, flash_wl, flash_wb, flash_l4))]
|
||||
#[cfg(any(flash_l0, flash_l1, flash_wl, flash_wb, flash_l4, flash_f3))]
|
||||
pub mod flash;
|
||||
pub mod pwm;
|
||||
#[cfg(rng)]
|
||||
|
|
6
examples/boot/stm32f3/.cargo/config.toml
Normal file
6
examples/boot/stm32f3/.cargo/config.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
# replace STM32F429ZITx with your chip as listed in `probe-run --list-chips`
|
||||
runner = "probe-run --chip STM32F303VCTx"
|
||||
|
||||
[build]
|
||||
target = "thumbv7em-none-eabihf"
|
26
examples/boot/stm32f3/Cargo.toml
Normal file
26
examples/boot/stm32f3/Cargo.toml
Normal file
|
@ -0,0 +1,26 @@
|
|||
[package]
|
||||
authors = ["Ulf Lilleengen <lulf@redhat.com>"]
|
||||
edition = "2021"
|
||||
name = "embassy-boot-stm32f3-examples"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] }
|
||||
embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f303re", "time-driver-any", "exti"] }
|
||||
embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" }
|
||||
embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" }
|
||||
|
||||
defmt = { version = "0.3", optional = true }
|
||||
defmt-rtt = { version = "0.3", optional = true }
|
||||
panic-reset = { version = "0.1.1" }
|
||||
embedded-hal = { version = "0.2.6" }
|
||||
|
||||
cortex-m = "0.7.3"
|
||||
cortex-m-rt = "0.7.0"
|
||||
|
||||
[features]
|
||||
defmt = [
|
||||
"dep:defmt",
|
||||
"embassy-stm32/defmt",
|
||||
"embassy-boot-stm32/defmt",
|
||||
]
|
29
examples/boot/stm32f3/README.md
Normal file
29
examples/boot/stm32f3/README.md
Normal file
|
@ -0,0 +1,29 @@
|
|||
# Examples using bootloader
|
||||
|
||||
Example for STM32F3 demonstrating the bootloader. The example consists of application binaries, 'a'
|
||||
which allows you to press a button to start the DFU process, and 'b' which is the updated
|
||||
application.
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* `cargo-binutils`
|
||||
* `cargo-flash`
|
||||
* `embassy-boot-stm32`
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
# Flash bootloader
|
||||
cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32f303re --chip STM32F303RETx
|
||||
# Build 'b'
|
||||
cargo build --release --bin b
|
||||
# Generate binary for 'b'
|
||||
cargo objcopy --release --bin b -- -O binary b.bin
|
||||
```
|
||||
|
||||
# Flash `a` (which includes b.bin)
|
||||
|
||||
```
|
||||
cargo flash --release --bin a --chip STM32F303RETx
|
||||
```
|
37
examples/boot/stm32f3/build.rs
Normal file
37
examples/boot/stm32f3/build.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
//! This build script copies the `memory.x` file from the crate root into
|
||||
//! a directory where the linker can always find it at build time.
|
||||
//! For many projects this is optional, as the linker always searches the
|
||||
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
||||
//! are using a workspace or have a more complicated build setup, this
|
||||
//! build script becomes required. Additionally, by requesting that
|
||||
//! Cargo re-run the build script whenever `memory.x` is changed,
|
||||
//! updating `memory.x` ensures a rebuild of the application with the
|
||||
//! new memory settings.
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Put `memory.x` in our output directory and ensure it's
|
||||
// on the linker search path.
|
||||
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||
File::create(out.join("memory.x"))
|
||||
.unwrap()
|
||||
.write_all(include_bytes!("memory.x"))
|
||||
.unwrap();
|
||||
println!("cargo:rustc-link-search={}", out.display());
|
||||
|
||||
// By default, Cargo will re-run a build script whenever
|
||||
// any file in the project changes. By specifying `memory.x`
|
||||
// here, we ensure the build script is only re-run when
|
||||
// `memory.x` is changed.
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||
if env::var("CARGO_FEATURE_DEFMT").is_ok() {
|
||||
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
|
||||
}
|
||||
}
|
15
examples/boot/stm32f3/memory.x
Normal file
15
examples/boot/stm32f3/memory.x
Normal file
|
@ -0,0 +1,15 @@
|
|||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 24K
|
||||
BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K
|
||||
FLASH : ORIGIN = 0x08008000, LENGTH = 32K
|
||||
DFU : ORIGIN = 0x08010000, LENGTH = 36K
|
||||
RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 32K
|
||||
}
|
||||
|
||||
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOTLOADER);
|
||||
|
||||
__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOTLOADER);
|
||||
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOTLOADER);
|
44
examples/boot/stm32f3/src/bin/a.rs
Normal file
44
examples/boot/stm32f3/src/bin/a.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy_boot_stm32::FirmwareUpdater;
|
||||
use embassy_stm32::exti::ExtiInput;
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use embassy_traits::adapter::BlockingAsync;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
static APP_B: &[u8] = include_bytes!("../../b.bin");
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_s: embassy::executor::Spawner, p: Peripherals) {
|
||||
let flash = Flash::unlock(p.FLASH);
|
||||
let mut flash = BlockingAsync::new(flash);
|
||||
|
||||
let button = Input::new(p.PC13, Pull::Up);
|
||||
let mut button = ExtiInput::new(button, p.EXTI13);
|
||||
|
||||
let mut led = Output::new(p.PA5, Level::Low, Speed::Low);
|
||||
led.set_high();
|
||||
|
||||
let mut updater = FirmwareUpdater::default();
|
||||
button.wait_for_falling_edge().await;
|
||||
let mut offset = 0;
|
||||
for chunk in APP_B.chunks(2048) {
|
||||
let mut buf: [u8; 2048] = [0; 2048];
|
||||
buf[..chunk.len()].copy_from_slice(chunk);
|
||||
updater
|
||||
.write_firmware(offset, &buf, &mut flash, 2048)
|
||||
.await
|
||||
.unwrap();
|
||||
offset += chunk.len();
|
||||
}
|
||||
updater.update(&mut flash).await.unwrap();
|
||||
led.set_low();
|
||||
cortex_m::peripheral::SCB::sys_reset();
|
||||
}
|
25
examples/boot/stm32f3/src/bin/b.rs
Normal file
25
examples/boot/stm32f3/src/bin/b.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embassy_stm32::Peripherals;
|
||||
use panic_reset as _;
|
||||
|
||||
#[cfg(feature = "defmt-rtt")]
|
||||
use defmt_rtt::*;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let mut led = Output::new(p.PA5, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
led.set_high();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
|
||||
led.set_low();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
|
@ -19,3 +19,4 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
|
|||
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
|
||||
heapless = { version = "0.7.5", default-features = false }
|
||||
nb = "1.0.0"
|
||||
embedded-storage = "0.3.0"
|
||||
|
|
|
@ -15,7 +15,7 @@ use panic_probe as _;
|
|||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
info!("Hello World!");
|
||||
|
||||
let mut led = Output::new(p.PE12, Level::High, Speed::Low);
|
||||
let mut led = Output::new(p.PA5, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
|
|
43
examples/stm32f3/src/bin/flash.rs
Normal file
43
examples/stm32f3/src/bin/flash.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::{info, unwrap};
|
||||
use embassy::executor::Spawner;
|
||||
use embassy_stm32::flash::Flash;
|
||||
use embassy_stm32::Peripherals;
|
||||
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
use panic_probe as _;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
info!("Hello Flash!");
|
||||
|
||||
const ADDR: u32 = 0x26000;
|
||||
|
||||
let mut f = Flash::unlock(p.FLASH);
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 8];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
|
||||
info!("Erasing...");
|
||||
unwrap!(f.erase(ADDR, ADDR + 2048));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 8];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read after erase: {=[u8]:x}", buf);
|
||||
|
||||
info!("Writing...");
|
||||
unwrap!(f.write(ADDR, &[1, 2, 3, 4, 5, 6, 7, 8]));
|
||||
|
||||
info!("Reading...");
|
||||
let mut buf = [0u8; 8];
|
||||
unwrap!(f.read(ADDR, &mut buf));
|
||||
info!("Read: {=[u8]:x}", buf);
|
||||
assert_eq!(&buf[..], &[1, 2, 3, 4, 5, 6, 7, 8]);
|
||||
}
|
|
@ -1 +1 @@
|
|||
Subproject commit 9abfa9d2b51e6071fdc7e680b4a171e4fa20c2fb
|
||||
Subproject commit b2d7a9f5de7dc3ae17c87c1ff94e13a822d18e74
|
Loading…
Reference in a new issue