diff options
| author | Ulf Lilleengen <[email protected]> | 2022-04-20 13:49:59 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2022-04-27 15:17:18 +0200 |
| commit | 484e0acc638c27366e19275c32db9c8487ea8fba (patch) | |
| tree | 5649591dad34cadcb28503f94c1bbca0bf5578a1 /examples | |
| parent | 9c283cd44504d6d9d6f9e352e4c7a8d043bd673f (diff) | |
Add stm32 flash + bootloader support
* Add flash drivers for L0, L1, L4, WB and WL. Not tested for WB, but
should be similar to WL.
* Add embassy-boot-stm32 for bootloading on STM32.
* Add flash examples and bootloader examples
* Update stm32-data
Diffstat (limited to 'examples')
41 files changed, 892 insertions, 22 deletions
diff --git a/examples/boot/Cargo.toml b/examples/boot/Cargo.toml deleted file mode 100644 index 2da659478..000000000 --- a/examples/boot/Cargo.toml +++ /dev/null | |||
| @@ -1,19 +0,0 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-boot-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | embassy = { version = "0.1.0", path = "../../embassy", features = ["nightly"] } | ||
| 9 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["time-driver-rtc1", "gpiote", "nightly"] } | ||
| 10 | embassy-boot-nrf = { version = "0.1.0", path = "../../embassy-boot/nrf" } | ||
| 11 | embassy-traits = { version = "0.1.0", path = "../../embassy-traits" } | ||
| 12 | |||
| 13 | defmt = { version = "0.3", optional = true } | ||
| 14 | defmt-rtt = { version = "0.3", optional = true } | ||
| 15 | panic-reset = { version = "0.1.1" } | ||
| 16 | embedded-hal = { version = "0.2.6" } | ||
| 17 | |||
| 18 | cortex-m = "0.7.3" | ||
| 19 | cortex-m-rt = "0.7.0" | ||
diff --git a/examples/boot/nrf/Cargo.toml b/examples/boot/nrf/Cargo.toml new file mode 100644 index 000000000..0a5bb8f9d --- /dev/null +++ b/examples/boot/nrf/Cargo.toml | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-boot-nrf-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } | ||
| 9 | embassy-nrf = { version = "0.1.0", path = "../../../embassy-nrf", features = ["time-driver-rtc1", "gpiote", "nightly"] } | ||
| 10 | embassy-boot-nrf = { version = "0.1.0", path = "../../../embassy-boot/nrf" } | ||
| 11 | embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } | ||
| 12 | |||
| 13 | defmt = { version = "0.3", optional = true } | ||
| 14 | defmt-rtt = { version = "0.3", optional = true } | ||
| 15 | panic-reset = { version = "0.1.1" } | ||
| 16 | embedded-hal = { version = "0.2.6" } | ||
| 17 | |||
| 18 | cortex-m = "0.7.3" | ||
| 19 | cortex-m-rt = "0.7.0" | ||
diff --git a/examples/boot/README.md b/examples/boot/nrf/README.md index b97513a9d..b97513a9d 100644 --- a/examples/boot/README.md +++ b/examples/boot/nrf/README.md | |||
diff --git a/examples/boot/build.rs b/examples/boot/nrf/build.rs index cd1a264c4..cd1a264c4 100644 --- a/examples/boot/build.rs +++ b/examples/boot/nrf/build.rs | |||
diff --git a/examples/boot/memory.x b/examples/boot/nrf/memory.x index dfb72103f..dfb72103f 100644 --- a/examples/boot/memory.x +++ b/examples/boot/nrf/memory.x | |||
diff --git a/examples/boot/src/bin/a.rs b/examples/boot/nrf/src/bin/a.rs index d18b508cc..caf8140d8 100644 --- a/examples/boot/src/bin/a.rs +++ b/examples/boot/nrf/src/bin/a.rs | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | #![feature(generic_associated_types)] | 4 | #![feature(generic_associated_types)] |
| 5 | #![feature(type_alias_impl_trait)] | 5 | #![feature(type_alias_impl_trait)] |
| 6 | 6 | ||
| 7 | use embassy_boot_nrf::updater; | 7 | use embassy_boot_nrf::FirmwareUpdater; |
| 8 | use embassy_nrf::{ | 8 | use embassy_nrf::{ |
| 9 | gpio::{Input, Pull}, | 9 | gpio::{Input, Pull}, |
| 10 | gpio::{Level, Output, OutputDrive}, | 10 | gpio::{Level, Output, OutputDrive}, |
| @@ -26,10 +26,10 @@ async fn main(_s: embassy::executor::Spawner, p: Peripherals) { | |||
| 26 | let nvmc = Nvmc::new(p.NVMC); | 26 | let nvmc = Nvmc::new(p.NVMC); |
| 27 | let mut nvmc = BlockingAsync::new(nvmc); | 27 | let mut nvmc = BlockingAsync::new(nvmc); |
| 28 | 28 | ||
| 29 | let mut updater = FirmwareUpdater::default(); | ||
| 29 | loop { | 30 | loop { |
| 30 | button.wait_for_any_edge().await; | 31 | button.wait_for_any_edge().await; |
| 31 | if button.is_low() { | 32 | if button.is_low() { |
| 32 | let mut updater = updater::new(); | ||
| 33 | let mut offset = 0; | 33 | let mut offset = 0; |
| 34 | for chunk in APP_B.chunks(4096) { | 34 | for chunk in APP_B.chunks(4096) { |
| 35 | let mut buf: [u8; 4096] = [0; 4096]; | 35 | let mut buf: [u8; 4096] = [0; 4096]; |
diff --git a/examples/boot/src/bin/b.rs b/examples/boot/nrf/src/bin/b.rs index 18bb6330c..18bb6330c 100644 --- a/examples/boot/src/bin/b.rs +++ b/examples/boot/nrf/src/bin/b.rs | |||
diff --git a/examples/boot/stm32l0/.cargo/config.toml b/examples/boot/stm32l0/.cargo/config.toml new file mode 100644 index 000000000..840faa62e --- /dev/null +++ b/examples/boot/stm32l0/.cargo/config.toml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip STM32L072CZTx" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv6m-none-eabi" | ||
diff --git a/examples/boot/stm32l0/Cargo.toml b/examples/boot/stm32l0/Cargo.toml new file mode 100644 index 000000000..2e093d771 --- /dev/null +++ b/examples/boot/stm32l0/Cargo.toml | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-boot-stm32l0-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } | ||
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32l072cz", "time-driver-any", "exti", "memory-x"] } | ||
| 10 | embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32", features = ["flash-128", "invert-erase", "thumbv6"] } | ||
| 11 | embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } | ||
| 12 | |||
| 13 | defmt = { version = "0.3", optional = true } | ||
| 14 | defmt-rtt = { version = "0.3", optional = true } | ||
| 15 | panic-reset = { version = "0.1.1" } | ||
| 16 | embedded-hal = { version = "0.2.6" } | ||
| 17 | |||
| 18 | cortex-m = "0.7.3" | ||
| 19 | cortex-m-rt = "0.7.0" | ||
| 20 | |||
| 21 | [features] | ||
| 22 | defmt = [ | ||
| 23 | "dep:defmt", | ||
| 24 | "embassy-stm32/defmt", | ||
| 25 | "embassy-boot-stm32/defmt", | ||
| 26 | ] | ||
diff --git a/examples/boot/stm32l0/README.md b/examples/boot/stm32l0/README.md new file mode 100644 index 000000000..9c8660821 --- /dev/null +++ b/examples/boot/stm32l0/README.md | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | # Examples using bootloader | ||
| 2 | |||
| 3 | Example for STM32L0 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 | cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32l072cz,flash-128,invert-erase,thumbv6 --chip STM32L072CZTx | ||
| 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 STM32L072CZTx | ||
| 29 | ``` | ||
diff --git a/examples/boot/stm32l0/build.rs b/examples/boot/stm32l0/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/stm32l0/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/stm32l0/memory.x b/examples/boot/stm32l0/memory.x new file mode 100644 index 000000000..fd5bf1a5d --- /dev/null +++ b/examples/boot/stm32l0/memory.x | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 24K | ||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K | ||
| 6 | FLASH : ORIGIN = 0x08008000, LENGTH = 32K | ||
| 7 | DFU : ORIGIN = 0x08010000, LENGTH = 36K | ||
| 8 | RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 16K | ||
| 9 | } | ||
| 10 | |||
| 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); | ||
| 12 | __bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE); | ||
| 13 | |||
| 14 | __bootloader_dfu_start = ORIGIN(DFU); | ||
| 15 | __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU); | ||
diff --git a/examples/boot/stm32l0/src/bin/a.rs b/examples/boot/stm32l0/src/bin/a.rs new file mode 100644 index 000000000..7b9000c91 --- /dev/null +++ b/examples/boot/stm32l0/src/bin/a.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::time::{Duration, Timer}; | ||
| 6 | use embassy_boot_stm32::FirmwareUpdater; | ||
| 7 | use embassy_stm32::exti::ExtiInput; | ||
| 8 | use embassy_stm32::flash::Flash; | ||
| 9 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 10 | use embassy_stm32::Peripherals; | ||
| 11 | use embassy_traits::adapter::BlockingAsync; | ||
| 12 | use panic_reset as _; | ||
| 13 | |||
| 14 | #[cfg(feature = "defmt-rtt")] | ||
| 15 | use defmt_rtt::*; | ||
| 16 | |||
| 17 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | ||
| 18 | |||
| 19 | #[embassy::main] | ||
| 20 | async fn main(_s: embassy::executor::Spawner, p: Peripherals) { | ||
| 21 | let flash = Flash::unlock(p.FLASH); | ||
| 22 | let mut flash = BlockingAsync::new(flash); | ||
| 23 | |||
| 24 | let button = Input::new(p.PB2, Pull::Up); | ||
| 25 | let mut button = ExtiInput::new(button, p.EXTI2); | ||
| 26 | |||
| 27 | let mut led = Output::new(p.PB5, Level::Low, Speed::Low); | ||
| 28 | |||
| 29 | led.set_high(); | ||
| 30 | |||
| 31 | let mut updater = FirmwareUpdater::default(); | ||
| 32 | button.wait_for_falling_edge().await; | ||
| 33 | let mut offset = 0; | ||
| 34 | for chunk in APP_B.chunks(128) { | ||
| 35 | let mut buf: [u8; 128] = [0; 128]; | ||
| 36 | buf[..chunk.len()].copy_from_slice(chunk); | ||
| 37 | updater | ||
| 38 | .write_firmware(offset, &buf, &mut flash, 128) | ||
| 39 | .await | ||
| 40 | .unwrap(); | ||
| 41 | offset += chunk.len(); | ||
| 42 | } | ||
| 43 | |||
| 44 | updater.mark_update(&mut flash).await.unwrap(); | ||
| 45 | led.set_low(); | ||
| 46 | Timer::after(Duration::from_secs(1)).await; | ||
| 47 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 48 | } | ||
diff --git a/examples/boot/stm32l0/src/bin/b.rs b/examples/boot/stm32l0/src/bin/b.rs new file mode 100644 index 000000000..ed774fd70 --- /dev/null +++ b/examples/boot/stm32l0/src/bin/b.rs | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::executor::Spawner; | ||
| 6 | use embassy::time::{Duration, Timer}; | ||
| 7 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 8 | use embassy_stm32::Peripherals; | ||
| 9 | use panic_reset as _; | ||
| 10 | |||
| 11 | #[cfg(feature = "defmt-rtt")] | ||
| 12 | use defmt_rtt::*; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | let mut led = Output::new(p.PB6, Level::High, Speed::Low); | ||
| 17 | |||
| 18 | loop { | ||
| 19 | led.set_high(); | ||
| 20 | Timer::after(Duration::from_millis(500)).await; | ||
| 21 | |||
| 22 | led.set_low(); | ||
| 23 | Timer::after(Duration::from_millis(500)).await; | ||
| 24 | } | ||
| 25 | } | ||
diff --git a/examples/boot/stm32l1/.cargo/config.toml b/examples/boot/stm32l1/.cargo/config.toml new file mode 100644 index 000000000..04985720b --- /dev/null +++ b/examples/boot/stm32l1/.cargo/config.toml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip STM32L151CBxxA" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv7m-none-eabi" | ||
diff --git a/examples/boot/stm32l1/Cargo.toml b/examples/boot/stm32l1/Cargo.toml new file mode 100644 index 000000000..ec396bef2 --- /dev/null +++ b/examples/boot/stm32l1/Cargo.toml | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-boot-stm32l1-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } | ||
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32l151cb-a", "time-driver-any", "exti"] } | ||
| 10 | embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32", features = ["flash-256", "invert-erase"] } | ||
| 11 | embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } | ||
| 12 | |||
| 13 | defmt = { version = "0.3", optional = true } | ||
| 14 | defmt-rtt = { version = "0.3", optional = true } | ||
| 15 | panic-reset = { version = "0.1.1" } | ||
| 16 | embedded-hal = { version = "0.2.6" } | ||
| 17 | |||
| 18 | cortex-m = "0.7.3" | ||
| 19 | cortex-m-rt = "0.7.0" | ||
| 20 | |||
| 21 | [features] | ||
| 22 | defmt = [ | ||
| 23 | "dep:defmt", | ||
| 24 | "embassy-stm32/defmt", | ||
| 25 | "embassy-boot-stm32/defmt", | ||
| 26 | ] | ||
diff --git a/examples/boot/stm32l1/README.md b/examples/boot/stm32l1/README.md new file mode 100644 index 000000000..1a9e85a75 --- /dev/null +++ b/examples/boot/stm32l1/README.md | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | # Examples using bootloader | ||
| 2 | |||
| 3 | Example for STM32L1 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 | cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32l151cb-a,flash-256,invert-erase --chip STM32L151CBxxA | ||
| 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 STM32L151CBxxA | ||
| 29 | ``` | ||
diff --git a/examples/boot/stm32l1/build.rs b/examples/boot/stm32l1/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/stm32l1/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/stm32l1/memory.x b/examples/boot/stm32l1/memory.x new file mode 100644 index 000000000..fd5bf1a5d --- /dev/null +++ b/examples/boot/stm32l1/memory.x | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 24K | ||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K | ||
| 6 | FLASH : ORIGIN = 0x08008000, LENGTH = 32K | ||
| 7 | DFU : ORIGIN = 0x08010000, LENGTH = 36K | ||
| 8 | RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 16K | ||
| 9 | } | ||
| 10 | |||
| 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); | ||
| 12 | __bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE); | ||
| 13 | |||
| 14 | __bootloader_dfu_start = ORIGIN(DFU); | ||
| 15 | __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU); | ||
diff --git a/examples/boot/stm32l1/src/bin/a.rs b/examples/boot/stm32l1/src/bin/a.rs new file mode 100644 index 000000000..7b9000c91 --- /dev/null +++ b/examples/boot/stm32l1/src/bin/a.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::time::{Duration, Timer}; | ||
| 6 | use embassy_boot_stm32::FirmwareUpdater; | ||
| 7 | use embassy_stm32::exti::ExtiInput; | ||
| 8 | use embassy_stm32::flash::Flash; | ||
| 9 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 10 | use embassy_stm32::Peripherals; | ||
| 11 | use embassy_traits::adapter::BlockingAsync; | ||
| 12 | use panic_reset as _; | ||
| 13 | |||
| 14 | #[cfg(feature = "defmt-rtt")] | ||
| 15 | use defmt_rtt::*; | ||
| 16 | |||
| 17 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | ||
| 18 | |||
| 19 | #[embassy::main] | ||
| 20 | async fn main(_s: embassy::executor::Spawner, p: Peripherals) { | ||
| 21 | let flash = Flash::unlock(p.FLASH); | ||
| 22 | let mut flash = BlockingAsync::new(flash); | ||
| 23 | |||
| 24 | let button = Input::new(p.PB2, Pull::Up); | ||
| 25 | let mut button = ExtiInput::new(button, p.EXTI2); | ||
| 26 | |||
| 27 | let mut led = Output::new(p.PB5, Level::Low, Speed::Low); | ||
| 28 | |||
| 29 | led.set_high(); | ||
| 30 | |||
| 31 | let mut updater = FirmwareUpdater::default(); | ||
| 32 | button.wait_for_falling_edge().await; | ||
| 33 | let mut offset = 0; | ||
| 34 | for chunk in APP_B.chunks(128) { | ||
| 35 | let mut buf: [u8; 128] = [0; 128]; | ||
| 36 | buf[..chunk.len()].copy_from_slice(chunk); | ||
| 37 | updater | ||
| 38 | .write_firmware(offset, &buf, &mut flash, 128) | ||
| 39 | .await | ||
| 40 | .unwrap(); | ||
| 41 | offset += chunk.len(); | ||
| 42 | } | ||
| 43 | |||
| 44 | updater.mark_update(&mut flash).await.unwrap(); | ||
| 45 | led.set_low(); | ||
| 46 | Timer::after(Duration::from_secs(1)).await; | ||
| 47 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 48 | } | ||
diff --git a/examples/boot/stm32l1/src/bin/b.rs b/examples/boot/stm32l1/src/bin/b.rs new file mode 100644 index 000000000..ed774fd70 --- /dev/null +++ b/examples/boot/stm32l1/src/bin/b.rs | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::executor::Spawner; | ||
| 6 | use embassy::time::{Duration, Timer}; | ||
| 7 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 8 | use embassy_stm32::Peripherals; | ||
| 9 | use panic_reset as _; | ||
| 10 | |||
| 11 | #[cfg(feature = "defmt-rtt")] | ||
| 12 | use defmt_rtt::*; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | let mut led = Output::new(p.PB6, Level::High, Speed::Low); | ||
| 17 | |||
| 18 | loop { | ||
| 19 | led.set_high(); | ||
| 20 | Timer::after(Duration::from_millis(500)).await; | ||
| 21 | |||
| 22 | led.set_low(); | ||
| 23 | Timer::after(Duration::from_millis(500)).await; | ||
| 24 | } | ||
| 25 | } | ||
diff --git a/examples/boot/stm32l4/.cargo/config.toml b/examples/boot/stm32l4/.cargo/config.toml new file mode 100644 index 000000000..7b6c4c0ac --- /dev/null +++ b/examples/boot/stm32l4/.cargo/config.toml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip STM32L475VG" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv7em-none-eabihf" | ||
diff --git a/examples/boot/stm32l4/Cargo.toml b/examples/boot/stm32l4/Cargo.toml new file mode 100644 index 000000000..394f26a12 --- /dev/null +++ b/examples/boot/stm32l4/Cargo.toml | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-boot-stm32l4-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } | ||
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32l475vg", "time-driver-any", "exti"] } | ||
| 10 | embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32", features = ["flash-2k"] } | ||
| 11 | embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } | ||
| 12 | |||
| 13 | defmt = { version = "0.3", optional = true } | ||
| 14 | defmt-rtt = { version = "0.3", optional = true } | ||
| 15 | panic-reset = { version = "0.1.1" } | ||
| 16 | embedded-hal = { version = "0.2.6" } | ||
| 17 | |||
| 18 | cortex-m = "0.7.3" | ||
| 19 | cortex-m-rt = "0.7.0" | ||
| 20 | |||
| 21 | [features] | ||
| 22 | defmt = [ | ||
| 23 | "dep:defmt", | ||
| 24 | "embassy-stm32/defmt", | ||
| 25 | "embassy-boot-stm32/defmt", | ||
| 26 | ] | ||
diff --git a/examples/boot/stm32l4/README.md b/examples/boot/stm32l4/README.md new file mode 100644 index 000000000..09e09d6ef --- /dev/null +++ b/examples/boot/stm32l4/README.md | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | # Examples using bootloader | ||
| 2 | |||
| 3 | Example for STM32L4 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 | cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32l475vg,flash-2k --chip STM32L475VG | ||
| 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 STM32L475VG | ||
| 29 | ``` | ||
diff --git a/examples/boot/stm32l4/build.rs b/examples/boot/stm32l4/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/stm32l4/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/stm32l4/memory.x b/examples/boot/stm32l4/memory.x new file mode 100644 index 000000000..fd5bf1a5d --- /dev/null +++ b/examples/boot/stm32l4/memory.x | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 24K | ||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K | ||
| 6 | FLASH : ORIGIN = 0x08008000, LENGTH = 32K | ||
| 7 | DFU : ORIGIN = 0x08010000, LENGTH = 36K | ||
| 8 | RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 16K | ||
| 9 | } | ||
| 10 | |||
| 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); | ||
| 12 | __bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE); | ||
| 13 | |||
| 14 | __bootloader_dfu_start = ORIGIN(DFU); | ||
| 15 | __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU); | ||
diff --git a/examples/boot/stm32l4/src/bin/a.rs b/examples/boot/stm32l4/src/bin/a.rs new file mode 100644 index 000000000..a5a9e2302 --- /dev/null +++ b/examples/boot/stm32l4/src/bin/a.rs | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy_boot_stm32::FirmwareUpdater; | ||
| 6 | use embassy_stm32::exti::ExtiInput; | ||
| 7 | use embassy_stm32::flash::Flash; | ||
| 8 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 9 | use embassy_stm32::Peripherals; | ||
| 10 | use embassy_traits::adapter::BlockingAsync; | ||
| 11 | use panic_reset as _; | ||
| 12 | |||
| 13 | #[cfg(feature = "defmt-rtt")] | ||
| 14 | use defmt_rtt::*; | ||
| 15 | |||
| 16 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | ||
| 17 | |||
| 18 | #[embassy::main] | ||
| 19 | async 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::Up); | ||
| 24 | let mut button = ExtiInput::new(button, p.EXTI13); | ||
| 25 | |||
| 26 | let mut led = Output::new(p.PB14, Level::Low, Speed::Low); | ||
| 27 | led.set_high(); | ||
| 28 | |||
| 29 | let mut updater = FirmwareUpdater::default(); | ||
| 30 | button.wait_for_falling_edge().await; | ||
| 31 | let mut offset = 0; | ||
| 32 | for chunk in APP_B.chunks(2048) { | ||
| 33 | let mut buf: [u8; 2048] = [0; 2048]; | ||
| 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.mark_update(&mut flash).await.unwrap(); | ||
| 42 | led.set_low(); | ||
| 43 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 44 | } | ||
diff --git a/examples/boot/stm32l4/src/bin/b.rs b/examples/boot/stm32l4/src/bin/b.rs new file mode 100644 index 000000000..814275988 --- /dev/null +++ b/examples/boot/stm32l4/src/bin/b.rs | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::executor::Spawner; | ||
| 6 | use embassy::time::{Duration, Timer}; | ||
| 7 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 8 | use embassy_stm32::Peripherals; | ||
| 9 | use panic_reset as _; | ||
| 10 | |||
| 11 | #[cfg(feature = "defmt-rtt")] | ||
| 12 | use defmt_rtt::*; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | let mut led = Output::new(p.PA5, Level::High, Speed::Low); | ||
| 17 | |||
| 18 | loop { | ||
| 19 | led.set_high(); | ||
| 20 | Timer::after(Duration::from_millis(500)).await; | ||
| 21 | |||
| 22 | led.set_low(); | ||
| 23 | Timer::after(Duration::from_millis(500)).await; | ||
| 24 | } | ||
| 25 | } | ||
diff --git a/examples/boot/stm32wl/.cargo/config.toml b/examples/boot/stm32wl/.cargo/config.toml new file mode 100644 index 000000000..60076e06b --- /dev/null +++ b/examples/boot/stm32wl/.cargo/config.toml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace your chip as listed in `probe-run --list-chips` | ||
| 3 | runner = "probe-run --chip STM32WLE5JCIx" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv7em-none-eabihf" | ||
diff --git a/examples/boot/stm32wl/Cargo.toml b/examples/boot/stm32wl/Cargo.toml new file mode 100644 index 000000000..9c69f4a65 --- /dev/null +++ b/examples/boot/stm32wl/Cargo.toml | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-boot-stm32wl-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } | ||
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32wl55jc-cm4", "time-driver-any", "exti"] } | ||
| 10 | embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32", features = ["flash-2k"] } | ||
| 11 | embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } | ||
| 12 | |||
| 13 | defmt = { version = "0.3", optional = true } | ||
| 14 | defmt-rtt = { version = "0.3", optional = true } | ||
| 15 | panic-reset = { version = "0.1.1" } | ||
| 16 | embedded-hal = { version = "0.2.6" } | ||
| 17 | |||
| 18 | cortex-m = "0.7.3" | ||
| 19 | cortex-m-rt = "0.7.0" | ||
| 20 | |||
| 21 | [features] | ||
| 22 | defmt = [ | ||
| 23 | "dep:defmt", | ||
| 24 | "embassy-stm32/defmt", | ||
| 25 | "embassy-boot-stm32/defmt", | ||
| 26 | ] | ||
diff --git a/examples/boot/stm32wl/README.md b/examples/boot/stm32wl/README.md new file mode 100644 index 000000000..a26a23852 --- /dev/null +++ b/examples/boot/stm32wl/README.md | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | # Examples using bootloader | ||
| 2 | |||
| 3 | Example for STM32WL 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 | cargo flash --manifest-path ../../../embassy-boot/stm32/Cargo.toml --release --features embassy-stm32/stm32wl55jc-cm4,flash-2k --chip STM32WLE5JCIx | ||
| 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 STM32WLE5JCIx | ||
| 29 | ``` | ||
diff --git a/examples/boot/stm32wl/build.rs b/examples/boot/stm32wl/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/stm32wl/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/stm32wl/memory.x b/examples/boot/stm32wl/memory.x new file mode 100644 index 000000000..78dd69c35 --- /dev/null +++ b/examples/boot/stm32wl/memory.x | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | BOOTLOADER : ORIGIN = 0x08000000, LENGTH = 24K | ||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K | ||
| 6 | FLASH : ORIGIN = 0x08008000, LENGTH = 32K | ||
| 7 | DFU : ORIGIN = 0x08010000, LENGTH = 36K | ||
| 8 | RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 32K | ||
| 9 | } | ||
| 10 | |||
| 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); | ||
| 12 | __bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE); | ||
| 13 | |||
| 14 | __bootloader_dfu_start = ORIGIN(DFU); | ||
| 15 | __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU); | ||
diff --git a/examples/boot/stm32wl/src/bin/a.rs b/examples/boot/stm32wl/src/bin/a.rs new file mode 100644 index 000000000..b1f4a4a03 --- /dev/null +++ b/examples/boot/stm32wl/src/bin/a.rs | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy_boot_stm32::FirmwareUpdater; | ||
| 6 | use embassy_stm32::exti::ExtiInput; | ||
| 7 | use embassy_stm32::flash::Flash; | ||
| 8 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 9 | use embassy_stm32::Peripherals; | ||
| 10 | use embassy_traits::adapter::BlockingAsync; | ||
| 11 | use panic_reset as _; | ||
| 12 | |||
| 13 | #[cfg(feature = "defmt-rtt")] | ||
| 14 | use defmt_rtt::*; | ||
| 15 | |||
| 16 | static APP_B: &[u8] = include_bytes!("../../b.bin"); | ||
| 17 | |||
| 18 | #[embassy::main] | ||
| 19 | async fn main(_s: embassy::executor::Spawner, p: Peripherals) { | ||
| 20 | let flash = Flash::new(p.FLASH); | ||
| 21 | let mut flash = BlockingAsync::new(flash); | ||
| 22 | |||
| 23 | let button = Input::new(p.PA0, Pull::Up); | ||
| 24 | let mut button = ExtiInput::new(button, p.EXTI0); | ||
| 25 | |||
| 26 | let mut led = Output::new(p.PB9, Level::Low, Speed::Low); | ||
| 27 | |||
| 28 | let mut updater = FirmwareUpdater::default(); | ||
| 29 | button.wait_for_falling_edge().await; | ||
| 30 | let mut offset = 0; | ||
| 31 | for chunk in APP_B.chunks(2048) { | ||
| 32 | let mut buf: [u8; 2048] = [0; 2048]; | ||
| 33 | buf[..chunk.len()].copy_from_slice(chunk); | ||
| 34 | // defmt::info!("Writing chunk at 0x{:x}", offset); | ||
| 35 | updater | ||
| 36 | .write_firmware(offset, &buf, &mut flash, 2048) | ||
| 37 | .await | ||
| 38 | .unwrap(); | ||
| 39 | offset += chunk.len(); | ||
| 40 | } | ||
| 41 | updater.mark_update(&mut flash).await.unwrap(); | ||
| 42 | // defmt::info!("Marked as updated"); | ||
| 43 | led.set_high(); | ||
| 44 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 45 | } | ||
diff --git a/examples/boot/stm32wl/src/bin/b.rs b/examples/boot/stm32wl/src/bin/b.rs new file mode 100644 index 000000000..ffe15b661 --- /dev/null +++ b/examples/boot/stm32wl/src/bin/b.rs | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::executor::Spawner; | ||
| 6 | use embassy::time::{Duration, Timer}; | ||
| 7 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 8 | use embassy_stm32::Peripherals; | ||
| 9 | use panic_reset as _; | ||
| 10 | |||
| 11 | #[cfg(feature = "defmt-rtt")] | ||
| 12 | use defmt_rtt::*; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | let mut led = Output::new(p.PB15, Level::High, Speed::Low); | ||
| 17 | |||
| 18 | loop { | ||
| 19 | led.set_high(); | ||
| 20 | Timer::after(Duration::from_millis(500)).await; | ||
| 21 | |||
| 22 | led.set_low(); | ||
| 23 | Timer::after(Duration::from_millis(500)).await; | ||
| 24 | } | ||
| 25 | } | ||
diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml index 0b21f6742..1cdb2f374 100644 --- a/examples/stm32l0/Cargo.toml +++ b/examples/stm32l0/Cargo.toml | |||
| @@ -21,6 +21,8 @@ lorawan = { version = "0.7.1", default-features = false, features = ["default-cr | |||
| 21 | defmt = "0.3" | 21 | defmt = "0.3" |
| 22 | defmt-rtt = "0.3" | 22 | defmt-rtt = "0.3" |
| 23 | 23 | ||
| 24 | embedded-storage = "0.3.0" | ||
| 25 | |||
| 24 | cortex-m = "0.7.3" | 26 | cortex-m = "0.7.3" |
| 25 | cortex-m-rt = "0.7.0" | 27 | cortex-m-rt = "0.7.0" |
| 26 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 28 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
diff --git a/examples/stm32l0/src/bin/flash.rs b/examples/stm32l0/src/bin/flash.rs new file mode 100644 index 000000000..0ab7b133b --- /dev/null +++ b/examples/stm32l0/src/bin/flash.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{info, unwrap}; | ||
| 6 | use embassy::executor::Spawner; | ||
| 7 | use embassy_stm32::flash::Flash; | ||
| 8 | use embassy_stm32::Peripherals; | ||
| 9 | use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; | ||
| 10 | |||
| 11 | use defmt_rtt as _; // global logger | ||
| 12 | use panic_probe as _; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | info!("Hello Flash!"); | ||
| 17 | |||
| 18 | const ADDR: u32 = 0x8026000; | ||
| 19 | |||
| 20 | let mut f = Flash::unlock(p.FLASH); | ||
| 21 | |||
| 22 | info!("Reading..."); | ||
| 23 | let mut buf = [0u8; 8]; | ||
| 24 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 25 | info!("Read: {=[u8]:x}", buf); | ||
| 26 | |||
| 27 | info!("Erasing..."); | ||
| 28 | unwrap!(f.erase(ADDR, ADDR + 128)); | ||
| 29 | |||
| 30 | info!("Reading..."); | ||
| 31 | let mut buf = [0u8; 8]; | ||
| 32 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 33 | info!("Read after erase: {=[u8]:x}", buf); | ||
| 34 | |||
| 35 | info!("Writing..."); | ||
| 36 | unwrap!(f.write(ADDR, &[1, 2, 3, 4, 5, 6, 7, 8])); | ||
| 37 | |||
| 38 | info!("Reading..."); | ||
| 39 | let mut buf = [0u8; 8]; | ||
| 40 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 41 | info!("Read: {=[u8]:x}", buf); | ||
| 42 | assert_eq!(&buf[..], &[1, 2, 3, 4, 5, 6, 7, 8]); | ||
| 43 | } | ||
diff --git a/examples/stm32l1/Cargo.toml b/examples/stm32l1/Cargo.toml index a7a6c228e..ce6b07729 100644 --- a/examples/stm32l1/Cargo.toml +++ b/examples/stm32l1/Cargo.toml | |||
| @@ -18,3 +18,4 @@ embedded-hal = "0.2.6" | |||
| 18 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 18 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 19 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 19 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 20 | heapless = { version = "0.7.5", default-features = false } | 20 | heapless = { version = "0.7.5", default-features = false } |
| 21 | embedded-storage = "0.3.0" | ||
diff --git a/examples/stm32l1/src/bin/flash.rs b/examples/stm32l1/src/bin/flash.rs new file mode 100644 index 000000000..b234289af --- /dev/null +++ b/examples/stm32l1/src/bin/flash.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{info, unwrap}; | ||
| 6 | use embassy::executor::Spawner; | ||
| 7 | use embassy_stm32::flash::Flash; | ||
| 8 | use embassy_stm32::Peripherals; | ||
| 9 | use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; | ||
| 10 | |||
| 11 | use defmt_rtt as _; // global logger | ||
| 12 | use panic_probe as _; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | info!("Hello Flash!"); | ||
| 17 | |||
| 18 | const ADDR: u32 = 0x8026000; | ||
| 19 | |||
| 20 | let mut f = Flash::unlock(p.FLASH); | ||
| 21 | |||
| 22 | info!("Reading..."); | ||
| 23 | let mut buf = [0u8; 8]; | ||
| 24 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 25 | info!("Read: {=[u8]:x}", buf); | ||
| 26 | |||
| 27 | info!("Erasing..."); | ||
| 28 | unwrap!(f.erase(ADDR, ADDR + 256)); | ||
| 29 | |||
| 30 | info!("Reading..."); | ||
| 31 | let mut buf = [0u8; 8]; | ||
| 32 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 33 | info!("Read after erase: {=[u8]:x}", buf); | ||
| 34 | |||
| 35 | info!("Writing..."); | ||
| 36 | unwrap!(f.write(ADDR, &[1, 2, 3, 4, 5, 6, 7, 8])); | ||
| 37 | |||
| 38 | info!("Reading..."); | ||
| 39 | let mut buf = [0u8; 8]; | ||
| 40 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 41 | info!("Read: {=[u8]:x}", buf); | ||
| 42 | assert_eq!(&buf[..], &[1, 2, 3, 4, 5, 6, 7, 8]); | ||
| 43 | } | ||
diff --git a/examples/stm32wl/Cargo.toml b/examples/stm32wl/Cargo.toml index 919104b01..01f317039 100644 --- a/examples/stm32wl/Cargo.toml +++ b/examples/stm32wl/Cargo.toml | |||
| @@ -8,7 +8,7 @@ resolver = "2" | |||
| 8 | [dependencies] | 8 | [dependencies] |
| 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } | 9 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } |
| 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "subghz", "unstable-pac", "exti"] } | 10 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "subghz", "unstable-pac", "exti"] } |
| 11 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time"] } | 11 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } |
| 12 | 12 | ||
| 13 | lorawan-device = { version = "0.7.1", default-features = false, features = ["async"] } | 13 | lorawan-device = { version = "0.7.1", default-features = false, features = ["async"] } |
| 14 | lorawan = { version = "0.7.1", default-features = false, features = ["default-crypto"] } | 14 | lorawan = { version = "0.7.1", default-features = false, features = ["default-crypto"] } |
| @@ -19,6 +19,7 @@ defmt-rtt = "0.3" | |||
| 19 | cortex-m = "0.7.3" | 19 | cortex-m = "0.7.3" |
| 20 | cortex-m-rt = "0.7.0" | 20 | cortex-m-rt = "0.7.0" |
| 21 | embedded-hal = "0.2.6" | 21 | embedded-hal = "0.2.6" |
| 22 | embedded-storage = "0.3.0" | ||
| 22 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 23 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 23 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 24 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 24 | heapless = { version = "0.7.5", default-features = false } | 25 | heapless = { version = "0.7.5", default-features = false } |
diff --git a/examples/stm32wl/src/bin/flash.rs b/examples/stm32wl/src/bin/flash.rs new file mode 100644 index 000000000..9e13c702a --- /dev/null +++ b/examples/stm32wl/src/bin/flash.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{info, unwrap}; | ||
| 6 | use embassy::executor::Spawner; | ||
| 7 | use embassy_stm32::flash::Flash; | ||
| 8 | use embassy_stm32::Peripherals; | ||
| 9 | use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; | ||
| 10 | |||
| 11 | use defmt_rtt as _; // global logger | ||
| 12 | use panic_probe as _; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | info!("Hello Flash!"); | ||
| 17 | |||
| 18 | const ADDR: u32 = 0x8036000; | ||
| 19 | |||
| 20 | let mut f = Flash::unlock(p.FLASH); | ||
| 21 | |||
| 22 | info!("Reading..."); | ||
| 23 | let mut buf = [0u8; 8]; | ||
| 24 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 25 | info!("Read: {=[u8]:x}", buf); | ||
| 26 | |||
| 27 | info!("Erasing..."); | ||
| 28 | unwrap!(f.erase(ADDR, ADDR + 2048)); | ||
| 29 | |||
| 30 | info!("Reading..."); | ||
| 31 | let mut buf = [0u8; 8]; | ||
| 32 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 33 | info!("Read: {=[u8]:x}", buf); | ||
| 34 | |||
| 35 | info!("Writing..."); | ||
| 36 | unwrap!(f.write(ADDR, &[1, 2, 3, 4, 5, 6, 7, 8])); | ||
| 37 | |||
| 38 | info!("Reading..."); | ||
| 39 | let mut buf = [0u8; 8]; | ||
| 40 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 41 | info!("Read: {=[u8]:x}", buf); | ||
| 42 | assert_eq!(&buf[..], &[1, 2, 3, 4, 5, 6, 7, 8]); | ||
| 43 | } | ||
