diff options
| author | Ulf Lilleengen <[email protected]> | 2022-06-24 19:56:15 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2022-06-24 19:56:15 +0200 |
| commit | 776be79f7bb10b09e795e2ea93bb795a653c9b4c (patch) | |
| tree | 269046d330ee503c84049bb8fc47baf0297ecb80 /examples/boot/application/stm32h7 | |
| parent | 84628d36cf743193cbf0e7d47ef1cfa9fb590890 (diff) | |
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.
Diffstat (limited to 'examples/boot/application/stm32h7')
| -rw-r--r-- | examples/boot/application/stm32h7/.cargo/config.toml | 9 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/Cargo.toml | 25 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/README.md | 29 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/build.rs | 37 | ||||
| -rwxr-xr-x | examples/boot/application/stm32h7/flash-boot.sh | 8 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/memory-bl.x | 18 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/memory.x | 15 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/src/bin/a.rs | 40 | ||||
| -rw-r--r-- | examples/boot/application/stm32h7/src/bin/b.rs | 26 |
9 files changed, 207 insertions, 0 deletions
diff --git a/examples/boot/application/stm32h7/.cargo/config.toml b/examples/boot/application/stm32h7/.cargo/config.toml new file mode 100644 index 000000000..fefdd370e --- /dev/null +++ b/examples/boot/application/stm32h7/.cargo/config.toml | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace STM32F429ZITx with your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip STM32H743ZITx" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv7em-none-eabihf" | ||
| 7 | |||
| 8 | [env] | ||
| 9 | DEFMT_LOG = "trace" | ||
diff --git a/examples/boot/application/stm32h7/Cargo.toml b/examples/boot/application/stm32h7/Cargo.toml new file mode 100644 index 000000000..54530cc5b --- /dev/null +++ b/examples/boot/application/stm32h7/Cargo.toml | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "embassy-boot-stm32f7-examples" | ||
| 4 | version = "0.1.0" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | embassy = { version = "0.1.0", path = "../../../../embassy", features = ["nightly", "time-tick-32768hz"] } | ||
| 8 | embassy-stm32 = { version = "0.1.0", path = "../../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32h743zi", "time-driver-any", "exti"] } | ||
| 9 | embassy-boot-stm32 = { version = "0.1.0", path = "../../../../embassy-boot/stm32" } | ||
| 10 | embassy-embedded-hal = { version = "0.1.0", path = "../../../../embassy-embedded-hal" } | ||
| 11 | |||
| 12 | defmt = { version = "0.3", optional = true } | ||
| 13 | defmt-rtt = { version = "0.3", optional = true } | ||
| 14 | panic-reset = { version = "0.1.1" } | ||
| 15 | embedded-hal = { version = "0.2.6" } | ||
| 16 | |||
| 17 | cortex-m = "0.7.3" | ||
| 18 | cortex-m-rt = "0.7.0" | ||
| 19 | |||
| 20 | [features] | ||
| 21 | defmt = [ | ||
| 22 | "dep:defmt", | ||
| 23 | "embassy-stm32/defmt", | ||
| 24 | "embassy-boot-stm32/defmt", | ||
| 25 | ] | ||
diff --git a/examples/boot/application/stm32h7/README.md b/examples/boot/application/stm32h7/README.md new file mode 100644 index 000000000..1fdc305e6 --- /dev/null +++ b/examples/boot/application/stm32h7/README.md | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | # Examples using bootloader | ||
| 2 | |||
| 3 | Example for STM32H7 demonstrating the bootloader. The example consists of application binaries, 'a' | ||
| 4 | which allows you to press a button to start the DFU process, and 'b' which is the updated | ||
| 5 | application. | ||
| 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' | ||
| 20 | cargo build --release --bin b | ||
| 21 | # Generate binary for 'b' | ||
| 22 | cargo objcopy --release --bin b -- -O binary b.bin | ||
| 23 | ``` | ||
| 24 | |||
| 25 | # Flash `a` (which includes b.bin) | ||
| 26 | |||
| 27 | ``` | ||
| 28 | cargo flash --release --bin a --chip STM32H743ZITx | ||
| 29 | ``` | ||
diff --git a/examples/boot/application/stm32h7/build.rs b/examples/boot/application/stm32h7/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/application/stm32h7/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 | |||
| 11 | use std::env; | ||
| 12 | use std::fs::File; | ||
| 13 | use std::io::Write; | ||
| 14 | use std::path::PathBuf; | ||
| 15 | |||
| 16 | fn 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/application/stm32h7/flash-boot.sh b/examples/boot/application/stm32h7/flash-boot.sh new file mode 100755 index 000000000..debdb17a7 --- /dev/null +++ b/examples/boot/application/stm32h7/flash-boot.sh | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | mv ../../bootloader/stm32/memory.x ../../bootloader/stm32/memory-old.x | ||
| 3 | cp memory-bl.x ../../bootloader/stm32/memory.x | ||
| 4 | |||
| 5 | cargo flash --manifest-path ../../bootloader/stm32/Cargo.toml --release --features embassy-stm32/stm32f767zi --chip STM32F767ZITx --target thumbv7em-none-eabihf | ||
| 6 | |||
| 7 | rm ../../bootloader/stm32/memory.x | ||
| 8 | mv ../../bootloader/stm32/memory-old.x ../../bootloader/stm32/memory.x | ||
diff --git a/examples/boot/application/stm32h7/memory-bl.x b/examples/boot/application/stm32h7/memory-bl.x new file mode 100644 index 000000000..c6f447d8b --- /dev/null +++ b/examples/boot/application/stm32h7/memory-bl.x | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | FLASH : ORIGIN = 0x08000000, LENGTH = 128K | ||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x08020000, LENGTH = 128K | ||
| 6 | ACTIVE : ORIGIN = 0x08040000, LENGTH = 128K | ||
| 7 | DFU : ORIGIN = 0x08100000, LENGTH = 512K | ||
| 8 | RAM (rwx) : ORIGIN = 0x24000000, LENGTH = 368K | ||
| 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/application/stm32h7/memory.x b/examples/boot/application/stm32h7/memory.x new file mode 100644 index 000000000..497a09e41 --- /dev/null +++ b/examples/boot/application/stm32h7/memory.x | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 128K | ||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x08020000, LENGTH = 128K | ||
| 6 | FLASH : ORIGIN = 0x08040000, LENGTH = 256K | ||
| 7 | DFU : ORIGIN = 0x08100000, LENGTH = 512K | ||
| 8 | RAM (rwx) : ORIGIN = 0x24000000, LENGTH = 368K | ||
| 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/application/stm32h7/src/bin/a.rs b/examples/boot/application/stm32h7/src/bin/a.rs new file mode 100644 index 000000000..704979dba --- /dev/null +++ b/examples/boot/application/stm32h7/src/bin/a.rs | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[cfg(feature = "defmt-rtt")] | ||
| 6 | use defmt_rtt::*; | ||
| 7 | use embassy_boot_stm32::FirmwareUpdater; | ||
| 8 | use embassy_embedded_hal::adapter::BlockingAsync; | ||
| 9 | use embassy_stm32::exti::ExtiInput; | ||
| 10 | use embassy_stm32::flash::Flash; | ||
| 11 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 12 | use embassy_stm32::Peripherals; | ||
| 13 | use panic_reset as _; | ||
| 14 | |||
| 15 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | ||
| 16 | |||
| 17 | #[embassy::main] | ||
| 18 | async fn main(_s: embassy::executor::Spawner, p: Peripherals) { | ||
| 19 | let flash = Flash::unlock(p.FLASH); | ||
| 20 | let mut flash = BlockingAsync::new(flash); | ||
| 21 | |||
| 22 | let button = Input::new(p.PC13, Pull::Down); | ||
| 23 | let mut button = ExtiInput::new(button, p.EXTI13); | ||
| 24 | |||
| 25 | let mut led = Output::new(p.PB14, Level::Low, Speed::Low); | ||
| 26 | led.set_high(); | ||
| 27 | |||
| 28 | let mut updater = FirmwareUpdater::default(); | ||
| 29 | button.wait_for_rising_edge().await; | ||
| 30 | let mut offset = 0; | ||
| 31 | let mut buf: [u8; 128 * 1024] = [0; 128 * 1024]; | ||
| 32 | for chunk in APP_B.chunks(128 * 1024) { | ||
| 33 | buf[..chunk.len()].copy_from_slice(chunk); | ||
| 34 | updater.write_firmware(offset, &buf, &mut flash, 2048).await.unwrap(); | ||
| 35 | offset += chunk.len(); | ||
| 36 | } | ||
| 37 | updater.update(&mut flash).await.unwrap(); | ||
| 38 | led.set_low(); | ||
| 39 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 40 | } | ||
diff --git a/examples/boot/application/stm32h7/src/bin/b.rs b/examples/boot/application/stm32h7/src/bin/b.rs new file mode 100644 index 000000000..ea0140253 --- /dev/null +++ b/examples/boot/application/stm32h7/src/bin/b.rs | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[cfg(feature = "defmt-rtt")] | ||
| 6 | use defmt_rtt::*; | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy::time::{Duration, Timer}; | ||
| 9 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 10 | use embassy_stm32::Peripherals; | ||
| 11 | use panic_reset as _; | ||
| 12 | |||
| 13 | #[embassy::main] | ||
| 14 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 15 | Timer::after(Duration::from_millis(300)).await; | ||
| 16 | let mut led = Output::new(p.PB14, Level::High, Speed::Low); | ||
| 17 | led.set_high(); | ||
| 18 | |||
| 19 | loop { | ||
| 20 | led.set_high(); | ||
| 21 | Timer::after(Duration::from_millis(500)).await; | ||
| 22 | |||
| 23 | led.set_low(); | ||
| 24 | Timer::after(Duration::from_millis(500)).await; | ||
| 25 | } | ||
| 26 | } | ||
