aboutsummaryrefslogtreecommitdiff
path: root/examples/boot/bootloader
diff options
context:
space:
mode:
authorQuentin Smith <[email protected]>2023-07-17 21:31:43 -0400
committerQuentin Smith <[email protected]>2023-07-17 21:31:43 -0400
commit6f02403184eb7fb7990fb88fc9df9c4328a690a3 (patch)
tree748f510e190bb2724750507a6e69ed1a8e08cb20 /examples/boot/bootloader
parentd896f80405aa8963877049ed999e4aba25d6e2bb (diff)
parent6b5df4523aa1c4902f02e803450ae4b418e0e3ca (diff)
Merge remote-tracking branch 'origin/main' into nrf-pdm
Diffstat (limited to 'examples/boot/bootloader')
-rw-r--r--examples/boot/bootloader/nrf/.cargo/config.toml2
-rw-r--r--examples/boot/bootloader/nrf/Cargo.toml10
-rw-r--r--examples/boot/bootloader/nrf/memory-bm.x2
-rw-r--r--examples/boot/bootloader/nrf/memory.x2
-rw-r--r--examples/boot/bootloader/nrf/src/main.rs26
-rw-r--r--examples/boot/bootloader/rp/.cargo/config.toml8
-rw-r--r--examples/boot/bootloader/rp/Cargo.toml33
-rw-r--r--examples/boot/bootloader/rp/README.md17
-rw-r--r--examples/boot/bootloader/rp/build.rs28
-rw-r--r--examples/boot/bootloader/rp/memory.x19
-rw-r--r--examples/boot/bootloader/rp/src/main.rs56
-rw-r--r--examples/boot/bootloader/stm32/Cargo.toml12
-rw-r--r--examples/boot/bootloader/stm32/src/main.rs20
13 files changed, 210 insertions, 25 deletions
diff --git a/examples/boot/bootloader/nrf/.cargo/config.toml b/examples/boot/bootloader/nrf/.cargo/config.toml
index 1060800a3..c292846aa 100644
--- a/examples/boot/bootloader/nrf/.cargo/config.toml
+++ b/examples/boot/bootloader/nrf/.cargo/config.toml
@@ -4,7 +4,7 @@ build-std-features = ["panic_immediate_abort"]
4 4
5[target.'cfg(all(target_arch = "arm", target_os = "none"))'] 5[target.'cfg(all(target_arch = "arm", target_os = "none"))']
6#runner = "./fruitrunner" 6#runner = "./fruitrunner"
7runner = "probe-run --chip nrf52840_xxAA" 7runner = "probe-rs run --chip nrf52840_xxAA"
8 8
9rustflags = [ 9rustflags = [
10 # Code-size optimizations. 10 # Code-size optimizations.
diff --git a/examples/boot/bootloader/nrf/Cargo.toml b/examples/boot/bootloader/nrf/Cargo.toml
index aa2a13ecb..40656f359 100644
--- a/examples/boot/bootloader/nrf/Cargo.toml
+++ b/examples/boot/bootloader/nrf/Cargo.toml
@@ -3,14 +3,16 @@ edition = "2021"
3name = "nrf-bootloader-example" 3name = "nrf-bootloader-example"
4version = "0.1.0" 4version = "0.1.0"
5description = "Bootloader for nRF chips" 5description = "Bootloader for nRF chips"
6license = "MIT OR Apache-2.0"
6 7
7[dependencies] 8[dependencies]
8defmt = { version = "0.3", optional = true } 9defmt = { version = "0.3", optional = true }
9defmt-rtt = { version = "0.3", optional = true } 10defmt-rtt = { version = "0.4", optional = true }
10 11
11embassy-nrf = { path = "../../../../embassy-nrf", default-features = false, features = ["nightly"] } 12embassy-nrf = { path = "../../../../embassy-nrf", features = ["nightly"] }
12embassy-boot-nrf = { path = "../../../../embassy-boot/nrf", default-features = false } 13embassy-boot-nrf = { path = "../../../../embassy-boot/nrf" }
13cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } 14cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }
15embassy-sync = { path = "../../../../embassy-sync" }
14cortex-m-rt = { version = "0.7" } 16cortex-m-rt = { version = "0.7" }
15cfg-if = "1.0.0" 17cfg-if = "1.0.0"
16 18
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 bc7e0755f..72c95c02a 100644
--- a/examples/boot/bootloader/nrf/src/main.rs
+++ b/examples/boot/bootloader/nrf/src/main.rs
@@ -1,11 +1,15 @@
1#![no_std] 1#![no_std]
2#![no_main] 2#![no_main]
3 3
4use core::cell::RefCell;
5
4use cortex_m_rt::{entry, exception}; 6use cortex_m_rt::{entry, exception};
5#[cfg(feature = "defmt")] 7#[cfg(feature = "defmt")]
6use defmt_rtt as _; 8use defmt_rtt as _;
7use embassy_boot_nrf::*; 9use embassy_boot_nrf::*;
8use embassy_nrf::nvmc::Nvmc; 10use embassy_nrf::nvmc::Nvmc;
11use embassy_nrf::wdt;
12use embassy_sync::blocking_mutex::Mutex;
9 13
10#[entry] 14#[entry]
11fn main() -> ! { 15fn main() -> ! {
@@ -19,13 +23,21 @@ fn main() -> ! {
19 } 23 }
20 */ 24 */
21 25
22 let mut bl = BootLoader::default(); 26 let mut wdt_config = wdt::Config::default();
23 let start = bl.prepare(&mut SingleFlashProvider::new(&mut WatchdogFlash::start( 27 wdt_config.timeout_ticks = 32768 * 5; // timeout seconds
24 Nvmc::new(p.NVMC), 28 wdt_config.run_during_sleep = true;
25 p.WDT, 29 wdt_config.run_during_debug_halt = false;
26 5, 30
27 ))); 31 let flash = WatchdogFlash::start(Nvmc::new(p.NVMC), p.WDT, wdt_config);
28 unsafe { bl.load(start) } 32 let flash = Mutex::new(RefCell::new(flash));
33
34 let config = BootLoaderConfig::from_linkerfile_blocking(&flash);
35 let active_offset = config.active.offset();
36 let mut bl: BootLoader<_, _, _> = BootLoader::new(config);
37
38 bl.prepare();
39
40 unsafe { bl.load(active_offset) }
29} 41}
30 42
31#[no_mangle] 43#[no_mangle]
diff --git a/examples/boot/bootloader/rp/.cargo/config.toml b/examples/boot/bootloader/rp/.cargo/config.toml
new file mode 100644
index 000000000..9d48ecdc9
--- /dev/null
+++ b/examples/boot/bootloader/rp/.cargo/config.toml
@@ -0,0 +1,8 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2runner = "probe-rs run --chip RP2040"
3
4[build]
5target = "thumbv6m-none-eabi"
6
7[env]
8DEFMT_LOG = "trace"
diff --git a/examples/boot/bootloader/rp/Cargo.toml b/examples/boot/bootloader/rp/Cargo.toml
new file mode 100644
index 000000000..c1dc99eec
--- /dev/null
+++ b/examples/boot/bootloader/rp/Cargo.toml
@@ -0,0 +1,33 @@
1[package]
2edition = "2021"
3name = "rp-bootloader-example"
4version = "0.1.0"
5description = "Example bootloader for RP2040 chips"
6license = "MIT OR Apache-2.0"
7
8[dependencies]
9defmt = { version = "0.3", optional = true }
10defmt-rtt = { version = "0.4", optional = true }
11
12embassy-rp = { path = "../../../../embassy-rp", features = ["nightly"] }
13embassy-boot-rp = { path = "../../../../embassy-boot/rp" }
14embassy-sync = { path = "../../../../embassy-sync" }
15embassy-time = { path = "../../../../embassy-time", features = ["nightly"] }
16
17cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }
18cortex-m-rt = { version = "0.7" }
19embedded-storage = "0.3.0"
20embedded-storage-async = "0.4.0"
21cfg-if = "1.0.0"
22
23[features]
24defmt = [
25 "dep:defmt",
26 "embassy-boot-rp/defmt",
27 "embassy-rp/defmt",
28]
29debug = ["defmt-rtt", "defmt"]
30
31[profile.release]
32debug = true
33opt-level = 's'
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
3The bootloader uses `embassy-boot` to interact with the flash.
4
5# Usage
6
7Flashing the bootloader
8
9```
10cargo flash --release --chip RP2040
11```
12
13To debug, use `cargo run` and enable the debug feature flag
14
15``` rust
16cargo 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 @@
1use std::env;
2use std::fs::File;
3use std::io::Write;
4use std::path::PathBuf;
5
6fn 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 @@
1MEMORY
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..6a81db804
--- /dev/null
+++ b/examples/boot/bootloader/rp/src/main.rs
@@ -0,0 +1,56 @@
1#![no_std]
2#![no_main]
3
4use core::cell::RefCell;
5
6use cortex_m_rt::{entry, exception};
7#[cfg(feature = "defmt")]
8use defmt_rtt as _;
9use embassy_boot_rp::*;
10use embassy_sync::blocking_mutex::Mutex;
11use embassy_time::Duration;
12
13const FLASH_SIZE: usize = 2 * 1024 * 1024;
14
15#[entry]
16fn main() -> ! {
17 let p = embassy_rp::init(Default::default());
18
19 // Uncomment this if you are debugging the bootloader with debugger/RTT attached,
20 // as it prevents a hard fault when accessing flash 'too early' after boot.
21 /*
22 for i in 0..10000000 {
23 cortex_m::asm::nop();
24 }
25 */
26
27 let flash = WatchdogFlash::<FLASH_SIZE>::start(p.FLASH, p.WATCHDOG, Duration::from_secs(8));
28 let flash = Mutex::new(RefCell::new(flash));
29
30 let config = BootLoaderConfig::from_linkerfile_blocking(&flash);
31 let active_offset = config.active.offset();
32 let mut bl: BootLoader<_, _, _> = BootLoader::new(config);
33
34 bl.prepare();
35
36 unsafe { bl.load(embassy_rp::flash::FLASH_BASE as u32 + active_offset) }
37}
38
39#[no_mangle]
40#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
41unsafe extern "C" fn HardFault() {
42 cortex_m::peripheral::SCB::sys_reset();
43}
44
45#[exception]
46unsafe fn DefaultHandler(_: i16) -> ! {
47 const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
48 let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
49
50 panic!("DefaultHandler #{:?}", irqn);
51}
52
53#[panic_handler]
54fn panic(_info: &core::panic::PanicInfo) -> ! {
55 cortex_m::asm::udf();
56}
diff --git a/examples/boot/bootloader/stm32/Cargo.toml b/examples/boot/bootloader/stm32/Cargo.toml
index 491777103..6436f2fee 100644
--- a/examples/boot/bootloader/stm32/Cargo.toml
+++ b/examples/boot/bootloader/stm32/Cargo.toml
@@ -3,17 +3,19 @@ edition = "2021"
3name = "stm32-bootloader-example" 3name = "stm32-bootloader-example"
4version = "0.1.0" 4version = "0.1.0"
5description = "Example bootloader for STM32 chips" 5description = "Example bootloader for STM32 chips"
6license = "MIT OR Apache-2.0"
6 7
7[dependencies] 8[dependencies]
8defmt = { version = "0.3", optional = true } 9defmt = { version = "0.3", optional = true }
9defmt-rtt = { version = "0.3", optional = true } 10defmt-rtt = { version = "0.4", optional = true }
10 11
11embassy-stm32 = { path = "../../../../embassy-stm32", default-features = false, features = ["nightly"] } 12embassy-stm32 = { path = "../../../../embassy-stm32", features = ["nightly"] }
12embassy-boot-stm32 = { path = "../../../../embassy-boot/stm32", default-features = false } 13embassy-boot-stm32 = { path = "../../../../embassy-boot/stm32" }
13cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } 14cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }
15embassy-sync = { path = "../../../../embassy-sync" }
14cortex-m-rt = { version = "0.7" } 16cortex-m-rt = { version = "0.7" }
15embedded-storage = "0.3.0" 17embedded-storage = "0.3.0"
16embedded-storage-async = "0.3.0" 18embedded-storage-async = "0.4.0"
17cfg-if = "1.0.0" 19cfg-if = "1.0.0"
18 20
19[features] 21[features]
diff --git a/examples/boot/bootloader/stm32/src/main.rs b/examples/boot/bootloader/stm32/src/main.rs
index 45c511ced..262eed200 100644
--- a/examples/boot/bootloader/stm32/src/main.rs
+++ b/examples/boot/bootloader/stm32/src/main.rs
@@ -1,11 +1,14 @@
1#![no_std] 1#![no_std]
2#![no_main] 2#![no_main]
3 3
4use core::cell::RefCell;
5
4use cortex_m_rt::{entry, exception}; 6use cortex_m_rt::{entry, exception};
5#[cfg(feature = "defmt")] 7#[cfg(feature = "defmt")]
6use defmt_rtt as _; 8use defmt_rtt as _;
7use embassy_boot_stm32::*; 9use embassy_boot_stm32::*;
8use embassy_stm32::flash::{Flash, ERASE_SIZE}; 10use embassy_stm32::flash::{Flash, BANK1_REGION};
11use embassy_sync::blocking_mutex::Mutex;
9 12
10#[entry] 13#[entry]
11fn main() -> ! { 14fn main() -> ! {
@@ -19,11 +22,16 @@ fn main() -> ! {
19 } 22 }
20 */ 23 */
21 24
22 let mut bl: BootLoader<ERASE_SIZE> = BootLoader::default(); 25 let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
23 let mut flash = Flash::unlock(p.FLASH); 26 let flash = Mutex::new(RefCell::new(layout.bank1_region));
24 let start = bl.prepare(&mut SingleFlashProvider::new(&mut flash)); 27
25 core::mem::drop(flash); 28 let config = BootLoaderConfig::from_linkerfile_blocking(&flash);
26 unsafe { bl.load(start) } 29 let active_offset = config.active.offset();
30 let mut bl: BootLoader<_, _, _, 2048> = BootLoader::new(config);
31
32 bl.prepare();
33
34 unsafe { bl.load(BANK1_REGION.base + active_offset) }
27} 35}
28 36
29#[no_mangle] 37#[no_mangle]