diff options
Diffstat (limited to 'examples/boot/bootloader')
| -rw-r--r-- | examples/boot/bootloader/nrf/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/boot/bootloader/nrf/memory-bm.x | 2 | ||||
| -rw-r--r-- | examples/boot/bootloader/nrf/memory.x | 2 | ||||
| -rw-r--r-- | examples/boot/bootloader/nrf/src/main.rs | 9 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/.cargo/config.toml | 8 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/Cargo.toml | 31 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/README.md | 17 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/build.rs | 28 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/memory.x | 19 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/src/main.rs | 51 | ||||
| -rw-r--r-- | examples/boot/bootloader/stm32/Cargo.toml | 2 |
11 files changed, 166 insertions, 5 deletions
diff --git a/examples/boot/bootloader/nrf/Cargo.toml b/examples/boot/bootloader/nrf/Cargo.toml index b417a40d1..8a6f53643 100644 --- a/examples/boot/bootloader/nrf/Cargo.toml +++ b/examples/boot/bootloader/nrf/Cargo.toml | |||
| @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0" | |||
| 7 | 7 | ||
| 8 | [dependencies] | 8 | [dependencies] |
| 9 | defmt = { version = "0.3", optional = true } | 9 | defmt = { version = "0.3", optional = true } |
| 10 | defmt-rtt = { version = "0.3", optional = true } | 10 | defmt-rtt = { version = "0.4", optional = true } |
| 11 | 11 | ||
| 12 | embassy-nrf = { path = "../../../../embassy-nrf", default-features = false, features = ["nightly"] } | 12 | embassy-nrf = { path = "../../../../embassy-nrf", default-features = false, features = ["nightly"] } |
| 13 | embassy-boot-nrf = { path = "../../../../embassy-boot/nrf", default-features = false } | 13 | embassy-boot-nrf = { path = "../../../../embassy-boot/nrf", default-features = false } |
diff --git a/examples/boot/bootloader/nrf/memory-bm.x b/examples/boot/bootloader/nrf/memory-bm.x index 8a32b905f..257d65644 100644 --- a/examples/boot/bootloader/nrf/memory-bm.x +++ b/examples/boot/bootloader/nrf/memory-bm.x | |||
| @@ -5,7 +5,7 @@ MEMORY | |||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x00006000, LENGTH = 4K | 5 | BOOTLOADER_STATE : ORIGIN = 0x00006000, LENGTH = 4K |
| 6 | ACTIVE : ORIGIN = 0x00007000, LENGTH = 64K | 6 | ACTIVE : ORIGIN = 0x00007000, LENGTH = 64K |
| 7 | DFU : ORIGIN = 0x00017000, LENGTH = 68K | 7 | DFU : ORIGIN = 0x00017000, LENGTH = 68K |
| 8 | RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 32K | 8 | RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K |
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); | 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); |
diff --git a/examples/boot/bootloader/nrf/memory.x b/examples/boot/bootloader/nrf/memory.x index 8a32b905f..257d65644 100644 --- a/examples/boot/bootloader/nrf/memory.x +++ b/examples/boot/bootloader/nrf/memory.x | |||
| @@ -5,7 +5,7 @@ MEMORY | |||
| 5 | BOOTLOADER_STATE : ORIGIN = 0x00006000, LENGTH = 4K | 5 | BOOTLOADER_STATE : ORIGIN = 0x00006000, LENGTH = 4K |
| 6 | ACTIVE : ORIGIN = 0x00007000, LENGTH = 64K | 6 | ACTIVE : ORIGIN = 0x00007000, LENGTH = 64K |
| 7 | DFU : ORIGIN = 0x00017000, LENGTH = 68K | 7 | DFU : ORIGIN = 0x00017000, LENGTH = 68K |
| 8 | RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 32K | 8 | RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K |
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); | 11 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE); |
diff --git a/examples/boot/bootloader/nrf/src/main.rs b/examples/boot/bootloader/nrf/src/main.rs index 8266206b3..aca3b857a 100644 --- a/examples/boot/bootloader/nrf/src/main.rs +++ b/examples/boot/bootloader/nrf/src/main.rs | |||
| @@ -6,6 +6,7 @@ use cortex_m_rt::{entry, exception}; | |||
| 6 | use defmt_rtt as _; | 6 | use defmt_rtt as _; |
| 7 | use embassy_boot_nrf::*; | 7 | use embassy_boot_nrf::*; |
| 8 | use embassy_nrf::nvmc::Nvmc; | 8 | use embassy_nrf::nvmc::Nvmc; |
| 9 | use embassy_nrf::wdt; | ||
| 9 | 10 | ||
| 10 | #[entry] | 11 | #[entry] |
| 11 | fn main() -> ! { | 12 | fn main() -> ! { |
| @@ -20,8 +21,14 @@ fn main() -> ! { | |||
| 20 | */ | 21 | */ |
| 21 | 22 | ||
| 22 | let mut bl = BootLoader::default(); | 23 | let mut bl = BootLoader::default(); |
| 24 | |||
| 25 | let mut wdt_config = wdt::Config::default(); | ||
| 26 | wdt_config.timeout_ticks = 32768 * 5; // timeout seconds | ||
| 27 | wdt_config.run_during_sleep = true; | ||
| 28 | wdt_config.run_during_debug_halt = false; | ||
| 29 | |||
| 23 | let start = bl.prepare(&mut SingleFlashConfig::new(&mut BootFlash::<_, 4096>::new( | 30 | let start = bl.prepare(&mut SingleFlashConfig::new(&mut BootFlash::<_, 4096>::new( |
| 24 | WatchdogFlash::start(Nvmc::new(p.NVMC), p.WDT, 5), | 31 | WatchdogFlash::start(Nvmc::new(p.NVMC), p.WDT, wdt_config), |
| 25 | ))); | 32 | ))); |
| 26 | unsafe { bl.load(start) } | 33 | unsafe { bl.load(start) } |
| 27 | } | 34 | } |
diff --git a/examples/boot/bootloader/rp/.cargo/config.toml b/examples/boot/bootloader/rp/.cargo/config.toml new file mode 100644 index 000000000..18bd4dfe8 --- /dev/null +++ b/examples/boot/bootloader/rp/.cargo/config.toml | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | runner = "probe-run --chip RP2040" | ||
| 3 | |||
| 4 | [build] | ||
| 5 | target = "thumbv6m-none-eabi" | ||
| 6 | |||
| 7 | [env] | ||
| 8 | DEFMT_LOG = "trace" | ||
diff --git a/examples/boot/bootloader/rp/Cargo.toml b/examples/boot/bootloader/rp/Cargo.toml new file mode 100644 index 000000000..c0b576cff --- /dev/null +++ b/examples/boot/bootloader/rp/Cargo.toml | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "rp-bootloader-example" | ||
| 4 | version = "0.1.0" | ||
| 5 | description = "Example bootloader for RP2040 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-rp = { path = "../../../../embassy-rp", default-features = false, features = ["nightly"] } | ||
| 13 | embassy-boot-rp = { path = "../../../../embassy-boot/rp", default-features = false } | ||
| 14 | embassy-time = { path = "../../../../embassy-time", features = ["nightly"] } | ||
| 15 | |||
| 16 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } | ||
| 17 | cortex-m-rt = { version = "0.7" } | ||
| 18 | embedded-storage = "0.3.0" | ||
| 19 | embedded-storage-async = "0.3.0" | ||
| 20 | cfg-if = "1.0.0" | ||
| 21 | |||
| 22 | [features] | ||
| 23 | defmt = [ | ||
| 24 | "dep:defmt", | ||
| 25 | "embassy-boot-rp/defmt", | ||
| 26 | "embassy-rp/defmt", | ||
| 27 | ] | ||
| 28 | debug = ["defmt-rtt", "defmt"] | ||
| 29 | |||
| 30 | [profile.release] | ||
| 31 | debug = true | ||
diff --git a/examples/boot/bootloader/rp/README.md b/examples/boot/bootloader/rp/README.md new file mode 100644 index 000000000..064e87273 --- /dev/null +++ b/examples/boot/bootloader/rp/README.md | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | # Bootloader for RP2040 | ||
| 2 | |||
| 3 | The bootloader uses `embassy-boot` to interact with the flash. | ||
| 4 | |||
| 5 | # Usage | ||
| 6 | |||
| 7 | Flashing the bootloader | ||
| 8 | |||
| 9 | ``` | ||
| 10 | cargo flash --release --chip RP2040 | ||
| 11 | ``` | ||
| 12 | |||
| 13 | To debug, use `cargo run` and enable the debug feature flag | ||
| 14 | |||
| 15 | ``` rust | ||
| 16 | cargo run --release --features debug | ||
| 17 | ``` | ||
diff --git a/examples/boot/bootloader/rp/build.rs b/examples/boot/bootloader/rp/build.rs new file mode 100644 index 000000000..c201704ad --- /dev/null +++ b/examples/boot/bootloader/rp/build.rs | |||
| @@ -0,0 +1,28 @@ | |||
| 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 | println!("cargo:rustc-link-arg-bins=-Tlink-rp.x"); | ||
| 25 | if env::var("CARGO_FEATURE_DEFMT").is_ok() { | ||
| 26 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | ||
| 27 | } | ||
| 28 | } | ||
diff --git a/examples/boot/bootloader/rp/memory.x b/examples/boot/bootloader/rp/memory.x new file mode 100644 index 000000000..d6ef38469 --- /dev/null +++ b/examples/boot/bootloader/rp/memory.x | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100 | ||
| 5 | FLASH : ORIGIN = 0x10000100, LENGTH = 24K | ||
| 6 | BOOTLOADER_STATE : ORIGIN = 0x10006000, LENGTH = 4K | ||
| 7 | ACTIVE : ORIGIN = 0x10007000, LENGTH = 512K | ||
| 8 | DFU : ORIGIN = 0x10087000, LENGTH = 516K | ||
| 9 | RAM : ORIGIN = 0x20000000, LENGTH = 256K | ||
| 10 | } | ||
| 11 | |||
| 12 | __bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOT2); | ||
| 13 | __bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOT2); | ||
| 14 | |||
| 15 | __bootloader_active_start = ORIGIN(ACTIVE) - ORIGIN(BOOT2); | ||
| 16 | __bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE) - ORIGIN(BOOT2); | ||
| 17 | |||
| 18 | __bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOT2); | ||
| 19 | __bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOT2); | ||
diff --git a/examples/boot/bootloader/rp/src/main.rs b/examples/boot/bootloader/rp/src/main.rs new file mode 100644 index 000000000..fb7f0522b --- /dev/null +++ b/examples/boot/bootloader/rp/src/main.rs | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use cortex_m_rt::{entry, exception}; | ||
| 5 | #[cfg(feature = "defmt")] | ||
| 6 | use defmt_rtt as _; | ||
| 7 | use embassy_boot_rp::*; | ||
| 8 | use embassy_rp::flash::ERASE_SIZE; | ||
| 9 | use embassy_time::Duration; | ||
| 10 | |||
| 11 | const FLASH_SIZE: usize = 2 * 1024 * 1024; | ||
| 12 | |||
| 13 | #[entry] | ||
| 14 | fn main() -> ! { | ||
| 15 | let p = embassy_rp::init(Default::default()); | ||
| 16 | |||
| 17 | // Uncomment this if you are debugging the bootloader with debugger/RTT attached, | ||
| 18 | // as it prevents a hard fault when accessing flash 'too early' after boot. | ||
| 19 | /* | ||
| 20 | for i in 0..10000000 { | ||
| 21 | cortex_m::asm::nop(); | ||
| 22 | } | ||
| 23 | */ | ||
| 24 | |||
| 25 | let mut bl: BootLoader = BootLoader::default(); | ||
| 26 | let flash = WatchdogFlash::<FLASH_SIZE>::start(p.FLASH, p.WATCHDOG, Duration::from_secs(8)); | ||
| 27 | let mut flash = BootFlash::<_, ERASE_SIZE>::new(flash); | ||
| 28 | let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash)); | ||
| 29 | core::mem::drop(flash); | ||
| 30 | |||
| 31 | unsafe { bl.load(start) } | ||
| 32 | } | ||
| 33 | |||
| 34 | #[no_mangle] | ||
| 35 | #[cfg_attr(target_os = "none", link_section = ".HardFault.user")] | ||
| 36 | unsafe extern "C" fn HardFault() { | ||
| 37 | cortex_m::peripheral::SCB::sys_reset(); | ||
| 38 | } | ||
| 39 | |||
| 40 | #[exception] | ||
| 41 | unsafe fn DefaultHandler(_: i16) -> ! { | ||
| 42 | const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32; | ||
| 43 | let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16; | ||
| 44 | |||
| 45 | panic!("DefaultHandler #{:?}", irqn); | ||
| 46 | } | ||
| 47 | |||
| 48 | #[panic_handler] | ||
| 49 | fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
| 50 | cortex_m::asm::udf(); | ||
| 51 | } | ||
diff --git a/examples/boot/bootloader/stm32/Cargo.toml b/examples/boot/bootloader/stm32/Cargo.toml index 4ddd1c99c..be659e02a 100644 --- a/examples/boot/bootloader/stm32/Cargo.toml +++ b/examples/boot/bootloader/stm32/Cargo.toml | |||
| @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0" | |||
| 7 | 7 | ||
| 8 | [dependencies] | 8 | [dependencies] |
| 9 | defmt = { version = "0.3", optional = true } | 9 | defmt = { version = "0.3", optional = true } |
| 10 | defmt-rtt = { version = "0.3", optional = true } | 10 | defmt-rtt = { version = "0.4", optional = true } |
| 11 | 11 | ||
| 12 | embassy-stm32 = { path = "../../../../embassy-stm32", default-features = false, features = ["nightly"] } | 12 | embassy-stm32 = { path = "../../../../embassy-stm32", default-features = false, features = ["nightly"] } |
| 13 | embassy-boot-stm32 = { path = "../../../../embassy-boot/stm32", default-features = false } | 13 | embassy-boot-stm32 = { path = "../../../../embassy-boot/stm32", default-features = false } |
