aboutsummaryrefslogtreecommitdiff
path: root/examples/boot/bootloader
diff options
context:
space:
mode:
Diffstat (limited to 'examples/boot/bootloader')
-rw-r--r--examples/boot/bootloader/nrf/.cargo/config.toml20
-rw-r--r--examples/boot/bootloader/nrf/Cargo.toml58
-rw-r--r--examples/boot/bootloader/nrf/README.md11
-rw-r--r--examples/boot/bootloader/nrf/build.rs37
-rw-r--r--examples/boot/bootloader/nrf/memory-bm.x18
-rw-r--r--examples/boot/bootloader/nrf/memory-s140.x31
-rw-r--r--examples/boot/bootloader/nrf/memory.x18
-rw-r--r--examples/boot/bootloader/nrf/src/main.rs48
-rw-r--r--examples/boot/bootloader/stm32/Cargo.toml57
-rw-r--r--examples/boot/bootloader/stm32/README.md11
-rw-r--r--examples/boot/bootloader/stm32/build.rs32
-rw-r--r--examples/boot/bootloader/stm32/memory.x18
-rw-r--r--examples/boot/bootloader/stm32/src/main.rs46
13 files changed, 405 insertions, 0 deletions
diff --git a/examples/boot/bootloader/nrf/.cargo/config.toml b/examples/boot/bootloader/nrf/.cargo/config.toml
new file mode 100644
index 000000000..1060800a3
--- /dev/null
+++ b/examples/boot/bootloader/nrf/.cargo/config.toml
@@ -0,0 +1,20 @@
1[unstable]
2build-std = ["core"]
3build-std-features = ["panic_immediate_abort"]
4
5[target.'cfg(all(target_arch = "arm", target_os = "none"))']
6#runner = "./fruitrunner"
7runner = "probe-run --chip nrf52840_xxAA"
8
9rustflags = [
10 # Code-size optimizations.
11 "-Z", "trap-unreachable=no",
12 #"-C", "no-vectorize-loops",
13 "-C", "force-frame-pointers=yes",
14]
15
16[build]
17target = "thumbv7em-none-eabi"
18
19[env]
20DEFMT_LOG = "trace"
diff --git a/examples/boot/bootloader/nrf/Cargo.toml b/examples/boot/bootloader/nrf/Cargo.toml
new file mode 100644
index 000000000..8eb98623c
--- /dev/null
+++ b/examples/boot/bootloader/nrf/Cargo.toml
@@ -0,0 +1,58 @@
1[package]
2edition = "2021"
3name = "nrf-bootloader-example"
4version = "0.1.0"
5description = "Bootloader for nRF chips"
6
7[dependencies]
8defmt = { version = "0.3", optional = true }
9defmt-rtt = { version = "0.3", optional = true }
10
11embassy = { path = "../../../../embassy", default-features = false }
12embassy-nrf = { path = "../../../../embassy-nrf", default-features = false, features = ["nightly"] }
13embassy-boot-nrf = { path = "../../../../embassy-boot/nrf", default-features = false }
14cortex-m = { version = "0.7" }
15cortex-m-rt = { version = "0.7" }
16cfg-if = "1.0.0"
17
18[features]
19defmt = [
20 "dep:defmt",
21 "embassy-boot-nrf/defmt",
22 "embassy-nrf/defmt",
23]
24softdevice = [
25 "embassy-boot-nrf/softdevice",
26]
27debug = ["defmt-rtt"]
28
29[profile.dev]
30debug = 2
31debug-assertions = true
32incremental = false
33opt-level = 'z'
34overflow-checks = true
35
36[profile.release]
37codegen-units = 1
38debug = 2
39debug-assertions = false
40incremental = false
41lto = 'fat'
42opt-level = 'z'
43overflow-checks = false
44
45# do not optimize proc-macro crates = faster builds from scratch
46[profile.dev.build-override]
47codegen-units = 8
48debug = false
49debug-assertions = false
50opt-level = 0
51overflow-checks = false
52
53[profile.release.build-override]
54codegen-units = 8
55debug = false
56debug-assertions = false
57opt-level = 0
58overflow-checks = false
diff --git a/examples/boot/bootloader/nrf/README.md b/examples/boot/bootloader/nrf/README.md
new file mode 100644
index 000000000..23497a038
--- /dev/null
+++ b/examples/boot/bootloader/nrf/README.md
@@ -0,0 +1,11 @@
1# Bootloader for nRF
2
3The bootloader uses `embassy-boot` to interact with the flash.
4
5# Usage
6
7Flash the bootloader
8
9```
10cargo flash --features embassy-nrf/nrf52832 --release --chip nRF52832_xxAA
11```
diff --git a/examples/boot/bootloader/nrf/build.rs b/examples/boot/bootloader/nrf/build.rs
new file mode 100644
index 000000000..e1da69328
--- /dev/null
+++ b/examples/boot/bootloader/nrf/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
11use std::env;
12use std::fs::File;
13use std::io::Write;
14use std::path::PathBuf;
15
16fn 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/bootloader/nrf/memory-bm.x b/examples/boot/bootloader/nrf/memory-bm.x
new file mode 100644
index 000000000..8a32b905f
--- /dev/null
+++ b/examples/boot/bootloader/nrf/memory-bm.x
@@ -0,0 +1,18 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 FLASH : ORIGIN = 0x00000000, LENGTH = 24K
5 BOOTLOADER_STATE : ORIGIN = 0x00006000, LENGTH = 4K
6 ACTIVE : ORIGIN = 0x00007000, LENGTH = 64K
7 DFU : ORIGIN = 0x00017000, LENGTH = 68K
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_active_start = ORIGIN(ACTIVE);
15__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE);
16
17__bootloader_dfu_start = ORIGIN(DFU);
18__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU);
diff --git a/examples/boot/bootloader/nrf/memory-s140.x b/examples/boot/bootloader/nrf/memory-s140.x
new file mode 100644
index 000000000..105db9972
--- /dev/null
+++ b/examples/boot/bootloader/nrf/memory-s140.x
@@ -0,0 +1,31 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 MBR : ORIGIN = 0x00000000, LENGTH = 4K
5 SOFTDEVICE : ORIGIN = 0x00001000, LENGTH = 155648
6 ACTIVE : ORIGIN = 0x00027000, LENGTH = 425984
7 DFU : ORIGIN = 0x0008F000, LENGTH = 430080
8 FLASH : ORIGIN = 0x000f9000, LENGTH = 24K
9 BOOTLOADER_STATE : ORIGIN = 0x000ff000, LENGTH = 4K
10 RAM (rwx) : ORIGIN = 0x20000008, LENGTH = 0x2fff8
11 uicr_bootloader_start_address (r) : ORIGIN = 0x10001014, LENGTH = 0x4
12}
13
14__bootloader_state_start = ORIGIN(BOOTLOADER_STATE);
15__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE);
16
17__bootloader_active_start = ORIGIN(ACTIVE);
18__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE);
19
20__bootloader_dfu_start = ORIGIN(DFU);
21__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU);
22
23__bootloader_start = ORIGIN(FLASH);
24
25SECTIONS
26{
27 .uicr_bootloader_start_address :
28 {
29 LONG(__bootloader_start)
30 } > uicr_bootloader_start_address
31}
diff --git a/examples/boot/bootloader/nrf/memory.x b/examples/boot/bootloader/nrf/memory.x
new file mode 100644
index 000000000..8a32b905f
--- /dev/null
+++ b/examples/boot/bootloader/nrf/memory.x
@@ -0,0 +1,18 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 FLASH : ORIGIN = 0x00000000, LENGTH = 24K
5 BOOTLOADER_STATE : ORIGIN = 0x00006000, LENGTH = 4K
6 ACTIVE : ORIGIN = 0x00007000, LENGTH = 64K
7 DFU : ORIGIN = 0x00017000, LENGTH = 68K
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_active_start = ORIGIN(ACTIVE);
15__bootloader_active_end = ORIGIN(ACTIVE) + LENGTH(ACTIVE);
16
17__bootloader_dfu_start = ORIGIN(DFU);
18__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU);
diff --git a/examples/boot/bootloader/nrf/src/main.rs b/examples/boot/bootloader/nrf/src/main.rs
new file mode 100644
index 000000000..bc7e0755f
--- /dev/null
+++ b/examples/boot/bootloader/nrf/src/main.rs
@@ -0,0 +1,48 @@
1#![no_std]
2#![no_main]
3
4use cortex_m_rt::{entry, exception};
5#[cfg(feature = "defmt")]
6use defmt_rtt as _;
7use embassy_boot_nrf::*;
8use embassy_nrf::nvmc::Nvmc;
9
10#[entry]
11fn main() -> ! {
12 let p = embassy_nrf::init(Default::default());
13
14 // Uncomment this if you are debugging the bootloader with debugger/RTT attached,
15 // as it prevents a hard fault when accessing flash 'too early' after boot.
16 /*
17 for i in 0..10000000 {
18 cortex_m::asm::nop();
19 }
20 */
21
22 let mut bl = BootLoader::default();
23 let start = bl.prepare(&mut SingleFlashProvider::new(&mut WatchdogFlash::start(
24 Nvmc::new(p.NVMC),
25 p.WDT,
26 5,
27 )));
28 unsafe { bl.load(start) }
29}
30
31#[no_mangle]
32#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
33unsafe extern "C" fn HardFault() {
34 cortex_m::peripheral::SCB::sys_reset();
35}
36
37#[exception]
38unsafe fn DefaultHandler(_: i16) -> ! {
39 const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
40 let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
41
42 panic!("DefaultHandler #{:?}", irqn);
43}
44
45#[panic_handler]
46fn panic(_info: &core::panic::PanicInfo) -> ! {
47 cortex_m::asm::udf();
48}
diff --git a/examples/boot/bootloader/stm32/Cargo.toml b/examples/boot/bootloader/stm32/Cargo.toml
new file mode 100644
index 000000000..b99a8fbcd
--- /dev/null
+++ b/examples/boot/bootloader/stm32/Cargo.toml
@@ -0,0 +1,57 @@
1[package]
2edition = "2021"
3name = "stm32-bootloader-example"
4version = "0.1.0"
5description = "Example bootloader for STM32 chips"
6
7[dependencies]
8defmt = { version = "0.3", optional = true }
9defmt-rtt = { version = "0.3", optional = true }
10
11embassy = { path = "../../../../embassy", default-features = false }
12embassy-stm32 = { path = "../../../../embassy-stm32", default-features = false, features = ["nightly"] }
13embassy-boot-stm32 = { path = "../../../../embassy-boot/stm32", default-features = false }
14cortex-m = { version = "0.7" }
15cortex-m-rt = { version = "0.7" }
16embedded-storage = "0.3.0"
17embedded-storage-async = "0.3.0"
18cfg-if = "1.0.0"
19
20[features]
21defmt = [
22 "dep:defmt",
23 "embassy-boot-stm32/defmt",
24 "embassy-stm32/defmt",
25]
26debug = ["defmt-rtt"]
27
28[profile.dev]
29debug = 2
30debug-assertions = true
31incremental = false
32opt-level = 'z'
33overflow-checks = true
34
35[profile.release]
36codegen-units = 1
37debug = 2
38debug-assertions = false
39incremental = false
40lto = 'fat'
41opt-level = 'z'
42overflow-checks = false
43
44# do not optimize proc-macro crates = faster builds from scratch
45[profile.dev.build-override]
46codegen-units = 8
47debug = false
48debug-assertions = false
49opt-level = 0
50overflow-checks = false
51
52[profile.release.build-override]
53codegen-units = 8
54debug = false
55debug-assertions = false
56opt-level = 0
57overflow-checks = false
diff --git a/examples/boot/bootloader/stm32/README.md b/examples/boot/bootloader/stm32/README.md
new file mode 100644
index 000000000..a82b730b9
--- /dev/null
+++ b/examples/boot/bootloader/stm32/README.md
@@ -0,0 +1,11 @@
1# Bootloader for STM32
2
3The bootloader uses `embassy-boot` to interact with the flash.
4
5# Usage
6
7Flash the bootloader
8
9```
10cargo flash --features embassy-stm32/stm32wl55jc-cm4 --release --chip STM32WLE5JCIx
11```
diff --git a/examples/boot/bootloader/stm32/build.rs b/examples/boot/bootloader/stm32/build.rs
new file mode 100644
index 000000000..3997702f6
--- /dev/null
+++ b/examples/boot/bootloader/stm32/build.rs
@@ -0,0 +1,32 @@
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 if env::var("CARGO_FEATURE_DEFMT").is_ok() {
25 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
26 }
27
28 let target = env::var("TARGET").unwrap();
29 if target.starts_with("thumbv6m-") {
30 println!("cargo:rustc-cfg=armv6m");
31 }
32}
diff --git a/examples/boot/bootloader/stm32/memory.x b/examples/boot/bootloader/stm32/memory.x
new file mode 100644
index 000000000..110c23259
--- /dev/null
+++ b/examples/boot/bootloader/stm32/memory.x
@@ -0,0 +1,18 @@
1MEMORY
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 = 0x20000008, 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/stm32/src/main.rs b/examples/boot/bootloader/stm32/src/main.rs
new file mode 100644
index 000000000..45c511ced
--- /dev/null
+++ b/examples/boot/bootloader/stm32/src/main.rs
@@ -0,0 +1,46 @@
1#![no_std]
2#![no_main]
3
4use cortex_m_rt::{entry, exception};
5#[cfg(feature = "defmt")]
6use defmt_rtt as _;
7use embassy_boot_stm32::*;
8use embassy_stm32::flash::{Flash, ERASE_SIZE};
9
10#[entry]
11fn main() -> ! {
12 let p = embassy_stm32::init(Default::default());
13
14 // Uncomment this if you are debugging the bootloader with debugger/RTT attached,
15 // as it prevents a hard fault when accessing flash 'too early' after boot.
16 /*
17 for i in 0..10000000 {
18 cortex_m::asm::nop();
19 }
20 */
21
22 let mut bl: BootLoader<ERASE_SIZE> = BootLoader::default();
23 let mut flash = Flash::unlock(p.FLASH);
24 let start = bl.prepare(&mut SingleFlashProvider::new(&mut flash));
25 core::mem::drop(flash);
26 unsafe { bl.load(start) }
27}
28
29#[no_mangle]
30#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
31unsafe extern "C" fn HardFault() {
32 cortex_m::peripheral::SCB::sys_reset();
33}
34
35#[exception]
36unsafe fn DefaultHandler(_: i16) -> ! {
37 const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
38 let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
39
40 panic!("DefaultHandler #{:?}", irqn);
41}
42
43#[panic_handler]
44fn panic(_info: &core::panic::PanicInfo) -> ! {
45 cortex_m::asm::udf();
46}