From 776be79f7bb10b09e795e2ea93bb795a653c9b4c Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 24 Jun 2022 19:56:15 +0200 Subject: Move bootloader main to examples This should remove some confusion around embassy-boot-* being a library vs. a binary. The binary is now an example bootloader instead. --- .../boot/application/stm32wl/.cargo/config.toml | 9 +++++ examples/boot/application/stm32wl/Cargo.toml | 25 +++++++++++++ examples/boot/application/stm32wl/README.md | 29 +++++++++++++++ examples/boot/application/stm32wl/build.rs | 37 +++++++++++++++++++ examples/boot/application/stm32wl/memory.x | 15 ++++++++ examples/boot/application/stm32wl/src/bin/a.rs | 43 ++++++++++++++++++++++ examples/boot/application/stm32wl/src/bin/b.rs | 24 ++++++++++++ 7 files changed, 182 insertions(+) create mode 100644 examples/boot/application/stm32wl/.cargo/config.toml create mode 100644 examples/boot/application/stm32wl/Cargo.toml create mode 100644 examples/boot/application/stm32wl/README.md create mode 100644 examples/boot/application/stm32wl/build.rs create mode 100644 examples/boot/application/stm32wl/memory.x create mode 100644 examples/boot/application/stm32wl/src/bin/a.rs create mode 100644 examples/boot/application/stm32wl/src/bin/b.rs (limited to 'examples/boot/application/stm32wl') diff --git a/examples/boot/application/stm32wl/.cargo/config.toml b/examples/boot/application/stm32wl/.cargo/config.toml new file mode 100644 index 000000000..e395d75b4 --- /dev/null +++ b/examples/boot/application/stm32wl/.cargo/config.toml @@ -0,0 +1,9 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +# replace your chip as listed in `probe-run --list-chips` +runner = "probe-run --chip STM32WLE5JCIx" + +[build] +target = "thumbv7em-none-eabihf" + +[env] +DEFMT_LOG = "trace" diff --git a/examples/boot/application/stm32wl/Cargo.toml b/examples/boot/application/stm32wl/Cargo.toml new file mode 100644 index 000000000..ca22e6134 --- /dev/null +++ b/examples/boot/application/stm32wl/Cargo.toml @@ -0,0 +1,25 @@ +[package] +edition = "2021" +name = "embassy-boot-stm32wl-examples" +version = "0.1.0" + +[dependencies] +embassy = { version = "0.1.0", path = "../../../../embassy", features = ["nightly", "time-tick-32768hz"] } +embassy-stm32 = { version = "0.1.0", path = "../../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32wl55jc-cm4", "time-driver-any", "exti"] } +embassy-boot-stm32 = { version = "0.1.0", path = "../../../../embassy-boot/stm32" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../../embassy-embedded-hal" } + +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", +] diff --git a/examples/boot/application/stm32wl/README.md b/examples/boot/application/stm32wl/README.md new file mode 100644 index 000000000..c8dce0387 --- /dev/null +++ b/examples/boot/application/stm32wl/README.md @@ -0,0 +1,29 @@ +# Examples using bootloader + +Example for STM32WL 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 ../../bootloader/stm32/Cargo.toml --release --features embassy-stm32/stm32wl55jc-cm4 --chip STM32WLE5JCIx +# 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 STM32WLE5JCIx +``` diff --git a/examples/boot/application/stm32wl/build.rs b/examples/boot/application/stm32wl/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/application/stm32wl/build.rs @@ -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"); + } +} diff --git a/examples/boot/application/stm32wl/memory.x b/examples/boot/application/stm32wl/memory.x new file mode 100644 index 000000000..14b2a2c9f --- /dev/null +++ b/examples/boot/application/stm32wl/memory.x @@ -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); diff --git a/examples/boot/application/stm32wl/src/bin/a.rs b/examples/boot/application/stm32wl/src/bin/a.rs new file mode 100644 index 000000000..dc1eb9bed --- /dev/null +++ b/examples/boot/application/stm32wl/src/bin/a.rs @@ -0,0 +1,43 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; +use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; +use embassy_stm32::exti::ExtiInput; +use embassy_stm32::flash::Flash; +use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; +use embassy_stm32::Peripherals; +use panic_reset as _; + +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.PA0, Pull::Up); + let mut button = ExtiInput::new(button, p.EXTI0); + + let mut led = Output::new(p.PB9, Level::Low, Speed::Low); + led.set_high(); + + let mut updater = FirmwareUpdater::default(); + button.wait_for_falling_edge().await; + //defmt::info!("Starting update"); + 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); + // defmt::info!("Writing chunk at 0x{:x}", offset); + updater.write_firmware(offset, &buf, &mut flash, 2048).await.unwrap(); + offset += chunk.len(); + } + updater.update(&mut flash).await.unwrap(); + //defmt::info!("Marked as updated"); + led.set_low(); + cortex_m::peripheral::SCB::sys_reset(); +} diff --git a/examples/boot/application/stm32wl/src/bin/b.rs b/examples/boot/application/stm32wl/src/bin/b.rs new file mode 100644 index 000000000..f2344bd53 --- /dev/null +++ b/examples/boot/application/stm32wl/src/bin/b.rs @@ -0,0 +1,24 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[cfg(feature = "defmt-rtt")] +use defmt_rtt::*; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use panic_reset as _; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let mut led = Output::new(p.PB15, 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; + } +} -- cgit