diff options
| author | Kaitlyn Kenwell <[email protected]> | 2023-12-14 09:36:22 -0500 |
|---|---|---|
| committer | Kaitlyn Kenwell <[email protected]> | 2023-12-14 09:36:22 -0500 |
| commit | e27e00f6280683293f427d0731aa69ca32dbbe60 (patch) | |
| tree | e238a3ed8fda1b7c89c27864b62d387786e75925 /examples/boot/bootloader/stm32wb-dfu | |
| parent | b60b3f4eb8f22ecda1c30d63213010f1b6b47686 (diff) | |
Address reviews
Diffstat (limited to 'examples/boot/bootloader/stm32wb-dfu')
| -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 | 94 |
5 files changed, 213 insertions, 0 deletions
diff --git a/examples/boot/bootloader/stm32wb-dfu/Cargo.toml b/examples/boot/bootloader/stm32wb-dfu/Cargo.toml new file mode 100644 index 000000000..774a8223d --- /dev/null +++ b/examples/boot/bootloader/stm32wb-dfu/Cargo.toml | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "stm32-bootloader-example" | ||
| 4 | version = "0.1.0" | ||
| 5 | description = "Example bootloader for STM32 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 = ["bootloader"] } | ||
| 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..b6f185ef7 --- /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 = 32K | ||
| 7 | DFU : ORIGIN = 0x08010000, LENGTH = 36K | ||
| 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..a2b884770 --- /dev/null +++ b/examples/boot/bootloader/stm32wb-dfu/src/main.rs | |||
| @@ -0,0 +1,94 @@ | |||
| 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}; | ||
| 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 | |||
| 36 | let layout = Flash::new_blocking(p.FLASH).into_blocking_regions(); | ||
| 37 | let flash = Mutex::new(RefCell::new(layout.bank1_region)); | ||
| 38 | |||
| 39 | let config = BootLoaderConfig::from_linkerfile_blocking(&flash); | ||
| 40 | let active_offset = config.active.offset(); | ||
| 41 | let bl = BootLoader::prepare::<_, _, _, 2048>(config); | ||
| 42 | if bl.state == State::DfuDetach { | ||
| 43 | let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11); | ||
| 44 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 45 | config.manufacturer = Some("Embassy"); | ||
| 46 | config.product = Some("USB-DFU Bootloader example"); | ||
| 47 | config.serial_number = Some("1235678"); | ||
| 48 | |||
| 49 | let fw_config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash); | ||
| 50 | let mut buffer = AlignedBuffer([0; WRITE_SIZE]); | ||
| 51 | let updater = BlockingFirmwareUpdater::new(fw_config, &mut buffer.0[..]); | ||
| 52 | |||
| 53 | let mut device_descriptor = [0; 256]; | ||
| 54 | let mut config_descriptor = [0; 256]; | ||
| 55 | let mut bos_descriptor = [0; 256]; | ||
| 56 | let mut control_buf = [0; 4096]; | ||
| 57 | let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD); | ||
| 58 | let mut builder = Builder::new( | ||
| 59 | driver, | ||
| 60 | config, | ||
| 61 | &mut device_descriptor, | ||
| 62 | &mut config_descriptor, | ||
| 63 | &mut bos_descriptor, | ||
| 64 | &mut [], | ||
| 65 | &mut control_buf, | ||
| 66 | ); | ||
| 67 | |||
| 68 | usb_dfu::<_, _, _, 4096>(&mut builder, &mut state); | ||
| 69 | |||
| 70 | let mut dev = builder.build(); | ||
| 71 | embassy_futures::block_on(dev.run()); | ||
| 72 | } | ||
| 73 | |||
| 74 | unsafe { bl.load(BANK1_REGION.base + active_offset) } | ||
| 75 | } | ||
| 76 | |||
| 77 | #[no_mangle] | ||
| 78 | #[cfg_attr(target_os = "none", link_section = ".HardFault.user")] | ||
| 79 | unsafe extern "C" fn HardFault() { | ||
| 80 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 81 | } | ||
| 82 | |||
| 83 | #[exception] | ||
| 84 | unsafe fn DefaultHandler(_: i16) -> ! { | ||
| 85 | const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32; | ||
| 86 | let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16; | ||
| 87 | |||
| 88 | panic!("DefaultHandler #{:?}", irqn); | ||
| 89 | } | ||
| 90 | |||
| 91 | #[panic_handler] | ||
| 92 | fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
| 93 | cortex_m::asm::udf(); | ||
| 94 | } | ||
