diff options
| author | Ulf Lilleengen <[email protected]> | 2023-12-14 19:56:04 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-12-14 19:56:04 +0000 |
| commit | 5ec2fbe3a2ce3234ed6477fe5923c3d2425b55a7 (patch) | |
| tree | 94b50831f7e5d606643eba7e0c9c1e5c7bf1ec86 /examples | |
| parent | 485765320aaef82adbd4865b25e7171fb8f4041a (diff) | |
| parent | 33e8943e5b6e637b82f13c77bd88bb56d55ab515 (diff) | |
Merge pull request #2284 from Redrield/feature/embassy-usb-dfu
Add embassy-usb-dfu crate, with related modifications to embassy-boot
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/boot/application/stm32wb-dfu/.cargo/config.toml | 9 | ||||
| -rw-r--r-- | examples/boot/application/stm32wb-dfu/Cargo.toml | 32 | ||||
| -rw-r--r-- | examples/boot/application/stm32wb-dfu/README.md | 29 | ||||
| -rw-r--r-- | examples/boot/application/stm32wb-dfu/build.rs | 37 | ||||
| -rw-r--r-- | examples/boot/application/stm32wb-dfu/memory.x | 15 | ||||
| -rw-r--r-- | examples/boot/application/stm32wb-dfu/src/main.rs | 64 | ||||
| -rw-r--r-- | examples/boot/bootloader/stm32wb-dfu/Cargo.toml | 63 | ||||
| -rw-r--r-- | examples/boot/bootloader/stm32wb-dfu/README.md | 11 | ||||
| -rw-r--r-- | examples/boot/bootloader/stm32wb-dfu/build.rs | 27 | ||||
| -rw-r--r-- | examples/boot/bootloader/stm32wb-dfu/memory.x | 18 | ||||
| -rw-r--r-- | examples/boot/bootloader/stm32wb-dfu/src/main.rs | 93 |
11 files changed, 398 insertions, 0 deletions
diff --git a/examples/boot/application/stm32wb-dfu/.cargo/config.toml b/examples/boot/application/stm32wb-dfu/.cargo/config.toml new file mode 100644 index 000000000..4f8094ff2 --- /dev/null +++ b/examples/boot/application/stm32wb-dfu/.cargo/config.toml | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace your chip as listed in `probe-rs chip list` | ||
| 3 | runner = "probe-rs run --chip STM32WLE5JCIx" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv7em-none-eabihf" | ||
| 7 | |||
| 8 | [env] | ||
| 9 | DEFMT_LOG = "trace" | ||
diff --git a/examples/boot/application/stm32wb-dfu/Cargo.toml b/examples/boot/application/stm32wb-dfu/Cargo.toml new file mode 100644 index 000000000..f6beea498 --- /dev/null +++ b/examples/boot/application/stm32wb-dfu/Cargo.toml | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "embassy-boot-stm32wb-dfu-examples" | ||
| 4 | version = "0.1.0" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | embassy-sync = { version = "0.5.0", path = "../../../../embassy-sync" } | ||
| 9 | embassy-executor = { version = "0.4.0", path = "../../../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "nightly", "integrated-timers"] } | ||
| 10 | embassy-time = { version = "0.2", path = "../../../../embassy-time", features = [ "tick-hz-32_768"] } | ||
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../../../embassy-stm32", features = ["stm32wb55rg", "time-driver-any", "exti"] } | ||
| 12 | embassy-boot-stm32 = { version = "0.1.0", path = "../../../../embassy-boot/stm32", features = [] } | ||
| 13 | embassy-embedded-hal = { version = "0.1.0", path = "../../../../embassy-embedded-hal" } | ||
| 14 | embassy-usb = { version = "0.1.0", path = "../../../../embassy-usb" } | ||
| 15 | embassy-usb-dfu = { version = "0.1.0", path = "../../../../embassy-usb-dfu", features = ["application", "cortex-m"] } | ||
| 16 | |||
| 17 | defmt = { version = "0.3", optional = true } | ||
| 18 | defmt-rtt = { version = "0.4", optional = true } | ||
| 19 | panic-reset = { version = "0.1.1" } | ||
| 20 | embedded-hal = { version = "0.2.6" } | ||
| 21 | |||
| 22 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } | ||
| 23 | cortex-m-rt = "0.7.0" | ||
| 24 | |||
| 25 | [features] | ||
| 26 | defmt = [ | ||
| 27 | "dep:defmt", | ||
| 28 | "dep:defmt-rtt", | ||
| 29 | "embassy-stm32/defmt", | ||
| 30 | "embassy-boot-stm32/defmt", | ||
| 31 | "embassy-sync/defmt", | ||
| 32 | ] | ||
diff --git a/examples/boot/application/stm32wb-dfu/README.md b/examples/boot/application/stm32wb-dfu/README.md new file mode 100644 index 000000000..c8dce0387 --- /dev/null +++ b/examples/boot/application/stm32wb-dfu/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 ../../bootloader/stm32/Cargo.toml --release --features embassy-stm32/stm32wl55jc-cm4 --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/application/stm32wb-dfu/build.rs b/examples/boot/application/stm32wb-dfu/build.rs new file mode 100644 index 000000000..e1da69328 --- /dev/null +++ b/examples/boot/application/stm32wb-dfu/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/stm32wb-dfu/memory.x b/examples/boot/application/stm32wb-dfu/memory.x new file mode 100644 index 000000000..ff1b800d2 --- /dev/null +++ b/examples/boot/application/stm32wb-dfu/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 = 128K | ||
| 7 | DFU : ORIGIN = 0x08028000, LENGTH = 132K | ||
| 8 | RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K | ||
| 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/stm32wb-dfu/src/main.rs b/examples/boot/application/stm32wb-dfu/src/main.rs new file mode 100644 index 000000000..fbecbf23b --- /dev/null +++ b/examples/boot/application/stm32wb-dfu/src/main.rs | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use core::cell::RefCell; | ||
| 6 | |||
| 7 | #[cfg(feature = "defmt-rtt")] | ||
| 8 | use defmt_rtt::*; | ||
| 9 | use embassy_boot_stm32::{AlignedBuffer, BlockingFirmwareState, FirmwareUpdaterConfig}; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_stm32::flash::{Flash, WRITE_SIZE}; | ||
| 12 | use embassy_stm32::rcc::WPAN_DEFAULT; | ||
| 13 | use embassy_stm32::usb::{self, Driver}; | ||
| 14 | use embassy_stm32::{bind_interrupts, peripherals}; | ||
| 15 | use embassy_sync::blocking_mutex::Mutex; | ||
| 16 | use embassy_time::Duration; | ||
| 17 | use embassy_usb::Builder; | ||
| 18 | use embassy_usb_dfu::consts::DfuAttributes; | ||
| 19 | use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate}; | ||
| 20 | use panic_reset as _; | ||
| 21 | |||
| 22 | bind_interrupts!(struct Irqs { | ||
| 23 | USB_LP => usb::InterruptHandler<peripherals::USB>; | ||
| 24 | }); | ||
| 25 | |||
| 26 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let mut config = embassy_stm32::Config::default(); | ||
| 29 | config.rcc = WPAN_DEFAULT; | ||
| 30 | let p = embassy_stm32::init(config); | ||
| 31 | let flash = Flash::new_blocking(p.FLASH); | ||
| 32 | let flash = Mutex::new(RefCell::new(flash)); | ||
| 33 | |||
| 34 | let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | ||
| 35 | let mut magic = AlignedBuffer([0; WRITE_SIZE]); | ||
| 36 | let mut firmware_state = BlockingFirmwareState::from_config(config, &mut magic.0); | ||
| 37 | firmware_state.mark_booted().expect("Failed to mark booted"); | ||
| 38 | |||
| 39 | let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11); | ||
| 40 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 41 | config.manufacturer = Some("Embassy"); | ||
| 42 | config.product = Some("USB-DFU Runtime example"); | ||
| 43 | config.serial_number = Some("1235678"); | ||
| 44 | |||
| 45 | let mut device_descriptor = [0; 256]; | ||
| 46 | let mut config_descriptor = [0; 256]; | ||
| 47 | let mut bos_descriptor = [0; 256]; | ||
| 48 | let mut control_buf = [0; 64]; | ||
| 49 | let mut state = Control::new(firmware_state, DfuAttributes::CAN_DOWNLOAD); | ||
| 50 | let mut builder = Builder::new( | ||
| 51 | driver, | ||
| 52 | config, | ||
| 53 | &mut device_descriptor, | ||
| 54 | &mut config_descriptor, | ||
| 55 | &mut bos_descriptor, | ||
| 56 | &mut [], | ||
| 57 | &mut control_buf, | ||
| 58 | ); | ||
| 59 | |||
| 60 | usb_dfu::<_, _, ResetImmediate>(&mut builder, &mut state, Duration::from_millis(2500)); | ||
| 61 | |||
| 62 | let mut dev = builder.build(); | ||
| 63 | dev.run().await | ||
| 64 | } | ||
diff --git a/examples/boot/bootloader/stm32wb-dfu/Cargo.toml b/examples/boot/bootloader/stm32wb-dfu/Cargo.toml new file mode 100644 index 000000000..ada073970 --- /dev/null +++ b/examples/boot/bootloader/stm32wb-dfu/Cargo.toml | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "stm32wb-dfu-bootloader-example" | ||
| 4 | version = "0.1.0" | ||
| 5 | description = "Example USB DFUbootloader for the STM32WB series of chips" | ||
| 6 | license = "MIT OR Apache-2.0" | ||
| 7 | |||
| 8 | [dependencies] | ||
| 9 | defmt = { version = "0.3", optional = true } | ||
| 10 | defmt-rtt = { version = "0.4", optional = true } | ||
| 11 | |||
| 12 | embassy-stm32 = { path = "../../../../embassy-stm32", features = ["stm32wb55rg"] } | ||
| 13 | embassy-boot-stm32 = { path = "../../../../embassy-boot/stm32" } | ||
| 14 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } | ||
| 15 | embassy-sync = { version = "0.5.0", path = "../../../../embassy-sync" } | ||
| 16 | cortex-m-rt = { version = "0.7" } | ||
| 17 | embedded-storage = "0.3.1" | ||
| 18 | embedded-storage-async = "0.4.0" | ||
| 19 | cfg-if = "1.0.0" | ||
| 20 | embassy-usb-dfu = { version = "0.1.0", path = "../../../../embassy-usb-dfu", features = ["dfu", "cortex-m"] } | ||
| 21 | embassy-usb = { version = "0.1.0", path = "../../../../embassy-usb", default-features = false } | ||
| 22 | embassy-futures = { version = "0.1.1", path = "../../../../embassy-futures" } | ||
| 23 | |||
| 24 | [features] | ||
| 25 | defmt = [ | ||
| 26 | "dep:defmt", | ||
| 27 | "embassy-boot-stm32/defmt", | ||
| 28 | "embassy-stm32/defmt", | ||
| 29 | "embassy-usb/defmt", | ||
| 30 | "embassy-usb-dfu/defmt" | ||
| 31 | ] | ||
| 32 | debug = ["defmt-rtt", "defmt"] | ||
| 33 | |||
| 34 | [profile.dev] | ||
| 35 | debug = 2 | ||
| 36 | debug-assertions = true | ||
| 37 | incremental = false | ||
| 38 | opt-level = 'z' | ||
| 39 | overflow-checks = true | ||
| 40 | |||
| 41 | [profile.release] | ||
| 42 | codegen-units = 1 | ||
| 43 | debug = 2 | ||
| 44 | debug-assertions = false | ||
| 45 | incremental = false | ||
| 46 | lto = 'fat' | ||
| 47 | opt-level = 'z' | ||
| 48 | overflow-checks = false | ||
| 49 | |||
| 50 | # do not optimize proc-macro crates = faster builds from scratch | ||
| 51 | [profile.dev.build-override] | ||
| 52 | codegen-units = 8 | ||
| 53 | debug = false | ||
| 54 | debug-assertions = false | ||
| 55 | opt-level = 0 | ||
| 56 | overflow-checks = false | ||
| 57 | |||
| 58 | [profile.release.build-override] | ||
| 59 | codegen-units = 8 | ||
| 60 | debug = false | ||
| 61 | debug-assertions = false | ||
| 62 | opt-level = 0 | ||
| 63 | overflow-checks = false | ||
diff --git a/examples/boot/bootloader/stm32wb-dfu/README.md b/examples/boot/bootloader/stm32wb-dfu/README.md new file mode 100644 index 000000000..a82b730b9 --- /dev/null +++ b/examples/boot/bootloader/stm32wb-dfu/README.md | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | # Bootloader for STM32 | ||
| 2 | |||
| 3 | The bootloader uses `embassy-boot` to interact with the flash. | ||
| 4 | |||
| 5 | # Usage | ||
| 6 | |||
| 7 | Flash the bootloader | ||
| 8 | |||
| 9 | ``` | ||
| 10 | cargo flash --features embassy-stm32/stm32wl55jc-cm4 --release --chip STM32WLE5JCIx | ||
| 11 | ``` | ||
diff --git a/examples/boot/bootloader/stm32wb-dfu/build.rs b/examples/boot/bootloader/stm32wb-dfu/build.rs new file mode 100644 index 000000000..fd605991f --- /dev/null +++ b/examples/boot/bootloader/stm32wb-dfu/build.rs | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | use std::env; | ||
| 2 | use std::fs::File; | ||
| 3 | use std::io::Write; | ||
| 4 | use std::path::PathBuf; | ||
| 5 | |||
| 6 | fn main() { | ||
| 7 | // Put `memory.x` in our output directory and ensure it's | ||
| 8 | // on the linker search path. | ||
| 9 | let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
| 10 | File::create(out.join("memory.x")) | ||
| 11 | .unwrap() | ||
| 12 | .write_all(include_bytes!("memory.x")) | ||
| 13 | .unwrap(); | ||
| 14 | println!("cargo:rustc-link-search={}", out.display()); | ||
| 15 | |||
| 16 | // By default, Cargo will re-run a build script whenever | ||
| 17 | // any file in the project changes. By specifying `memory.x` | ||
| 18 | // here, we ensure the build script is only re-run when | ||
| 19 | // `memory.x` is changed. | ||
| 20 | println!("cargo:rerun-if-changed=memory.x"); | ||
| 21 | |||
| 22 | println!("cargo:rustc-link-arg-bins=--nmagic"); | ||
| 23 | println!("cargo:rustc-link-arg-bins=-Tlink.x"); | ||
| 24 | if env::var("CARGO_FEATURE_DEFMT").is_ok() { | ||
| 25 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | ||
| 26 | } | ||
| 27 | } | ||
diff --git a/examples/boot/bootloader/stm32wb-dfu/memory.x b/examples/boot/bootloader/stm32wb-dfu/memory.x new file mode 100644 index 000000000..858062631 --- /dev/null +++ b/examples/boot/bootloader/stm32wb-dfu/memory.x | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | FLASH : ORIGIN = 0x08000000, LENGTH = 24K | ||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x08006000, LENGTH = 4K | ||
| 6 | ACTIVE : ORIGIN = 0x08008000, LENGTH = 128K | ||
| 7 | DFU : ORIGIN = 0x08028000, LENGTH = 132K | ||
| 8 | RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 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/bootloader/stm32wb-dfu/src/main.rs b/examples/boot/bootloader/stm32wb-dfu/src/main.rs new file mode 100644 index 000000000..a7ab813b6 --- /dev/null +++ b/examples/boot/bootloader/stm32wb-dfu/src/main.rs | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use core::cell::RefCell; | ||
| 5 | |||
| 6 | use cortex_m_rt::{entry, exception}; | ||
| 7 | #[cfg(feature = "defmt")] | ||
| 8 | use defmt_rtt as _; | ||
| 9 | use embassy_boot_stm32::*; | ||
| 10 | use embassy_stm32::flash::{Flash, BANK1_REGION, WRITE_SIZE}; | ||
| 11 | use embassy_stm32::rcc::WPAN_DEFAULT; | ||
| 12 | use embassy_stm32::usb::Driver; | ||
| 13 | use embassy_stm32::{bind_interrupts, peripherals, usb}; | ||
| 14 | use embassy_sync::blocking_mutex::Mutex; | ||
| 15 | use embassy_usb::Builder; | ||
| 16 | use embassy_usb_dfu::consts::DfuAttributes; | ||
| 17 | use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | USB_LP => usb::InterruptHandler<peripherals::USB>; | ||
| 21 | }); | ||
| 22 | |||
| 23 | #[entry] | ||
| 24 | fn main() -> ! { | ||
| 25 | let mut config = embassy_stm32::Config::default(); | ||
| 26 | config.rcc = WPAN_DEFAULT; | ||
| 27 | let p = embassy_stm32::init(config); | ||
| 28 | |||
| 29 | // Prevent a hard fault when accessing flash 'too early' after boot. | ||
| 30 | #[cfg(feature = "defmt")] | ||
| 31 | for _ in 0..10000000 { | ||
| 32 | cortex_m::asm::nop(); | ||
| 33 | } | ||
| 34 | |||
| 35 | let layout = Flash::new_blocking(p.FLASH).into_blocking_regions(); | ||
| 36 | let flash = Mutex::new(RefCell::new(layout.bank1_region)); | ||
| 37 | |||
| 38 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); | ||
| 39 | let active_offset = config.active.offset(); | ||
| 40 | let bl = BootLoader::prepare::<_, _, _, 2048>(config); | ||
| 41 | if bl.state == State::DfuDetach { | ||
| 42 | let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11); | ||
| 43 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 44 | config.manufacturer = Some("Embassy"); | ||
| 45 | config.product = Some("USB-DFU Bootloader example"); | ||
| 46 | config.serial_number = Some("1235678"); | ||
| 47 | |||
| 48 | let fw_config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | ||
| 49 | let mut buffer = AlignedBuffer([0; WRITE_SIZE]); | ||
| 50 | let updater = BlockingFirmwareUpdater::new(fw_config, &mut buffer.0[..]); | ||
| 51 | |||
| 52 | let mut device_descriptor = [0; 256]; | ||
| 53 | let mut config_descriptor = [0; 256]; | ||
| 54 | let mut bos_descriptor = [0; 256]; | ||
| 55 | let mut control_buf = [0; 4096]; | ||
| 56 | let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD); | ||
| 57 | let mut builder = Builder::new( | ||
| 58 | driver, | ||
| 59 | config, | ||
| 60 | &mut device_descriptor, | ||
| 61 | &mut config_descriptor, | ||
| 62 | &mut bos_descriptor, | ||
| 63 | &mut [], | ||
| 64 | &mut control_buf, | ||
| 65 | ); | ||
| 66 | |||
| 67 | usb_dfu::<_, _, _, ResetImmediate, 4096>(&mut builder, &mut state); | ||
| 68 | |||
| 69 | let mut dev = builder.build(); | ||
| 70 | embassy_futures::block_on(dev.run()); | ||
| 71 | } | ||
| 72 | |||
| 73 | unsafe { bl.load(BANK1_REGION.base + active_offset) } | ||
| 74 | } | ||
| 75 | |||
| 76 | #[no_mangle] | ||
| 77 | #[cfg_attr(target_os = "none", link_section = ".HardFault.user")] | ||
| 78 | unsafe extern "C" fn HardFault() { | ||
| 79 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 80 | } | ||
| 81 | |||
| 82 | #[exception] | ||
| 83 | unsafe fn DefaultHandler(_: i16) -> ! { | ||
| 84 | const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32; | ||
| 85 | let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16; | ||
| 86 | |||
| 87 | panic!("DefaultHandler #{:?}", irqn); | ||
| 88 | } | ||
| 89 | |||
| 90 | #[panic_handler] | ||
| 91 | fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
| 92 | cortex_m::asm::udf(); | ||
| 93 | } | ||
