Add embassy-boot
Embassy-boot is a simple bootloader that works together with an application to provide firmware update capabilities with a minimal risk. The bootloader consists of a platform-independent part, which implements the swap algorithm, and a platform-dependent part (currently only for nRF) that provides addition functionality such as watchdog timers softdevice support.
This commit is contained in:
committed by
Ulf Lilleengen
parent
d91bd0b9a6
commit
ed2a87a262
7
examples/boot/.cargo/config.toml
Normal file
7
examples/boot/.cargo/config.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[unstable]
|
||||
namespaced-features = true
|
||||
build-std = ["core"]
|
||||
build-std-features = ["panic_immediate_abort"]
|
||||
|
||||
[build]
|
||||
target = "thumbv7em-none-eabi"
|
19
examples/boot/Cargo.toml
Normal file
19
examples/boot/Cargo.toml
Normal file
@ -0,0 +1,19 @@
|
||||
[package]
|
||||
authors = ["Ulf Lilleengen <lulf@redhat.com>"]
|
||||
edition = "2018"
|
||||
name = "embassy-boot-examples"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
embassy = { version = "0.1.0", path = "../../embassy" }
|
||||
embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["time-driver-rtc1", "gpiote"] }
|
||||
embassy-boot-nrf = { version = "0.1.0", path = "../../embassy-boot/nrf" }
|
||||
embassy-traits = { version = "0.1.0", path = "../../embassy-traits" }
|
||||
|
||||
defmt = { version = "0.3", optional = true }
|
||||
defmt-rtt = { version = "0.3", optional = true }
|
||||
panic-reset = { version = "0.1.1" }
|
||||
embedded-hal = { version = "0.2.6" }
|
||||
|
||||
cortex-m = "0.7.3"
|
||||
cortex-m-rt = "0.7.0"
|
31
examples/boot/README.md
Normal file
31
examples/boot/README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# Examples using bootloader
|
||||
|
||||
Example for nRF52 demonstrating the bootloader. The example consists of application binaries, 'a'
|
||||
which allows you to press a button to start the DFU process, and 'b' which is the updated
|
||||
application.
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* `cargo-binutils`
|
||||
* `cargo-flash`
|
||||
* `embassy-boot-nrf`
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
|
||||
```
|
||||
# Flash bootloader
|
||||
cargo flash --manifest-path ../../embassy-boot/nrf/Cargo.toml --release --features embassy-nrf/nrf52840 --chip nRF52840_xxAA
|
||||
# Build 'b'
|
||||
cargo build --release --features embassy-nrf/nrf52840 --bin b
|
||||
# Generate binary for 'b'
|
||||
cargo objcopy --release --features embassy-nrf/nrf52840 --bin b -- -O binary b.bin
|
||||
```
|
||||
|
||||
# Flash `a` (which includes b.bin)
|
||||
|
||||
```
|
||||
cargo flash --release --features embassy-nrf/nrf52840 --bin a --chip nRF52840_xxAA
|
||||
```
|
34
examples/boot/build.rs
Normal file
34
examples/boot/build.rs
Normal file
@ -0,0 +1,34 @@
|
||||
//! This build script copies the `memory.x` file from the crate root into
|
||||
//! a directory where the linker can always find it at build time.
|
||||
//! For many projects this is optional, as the linker always searches the
|
||||
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
||||
//! are using a workspace or have a more complicated build setup, this
|
||||
//! build script becomes required. Additionally, by requesting that
|
||||
//! Cargo re-run the build script whenever `memory.x` is changed,
|
||||
//! updating `memory.x` ensures a rebuild of the application with the
|
||||
//! new memory settings.
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Put `memory.x` in our output directory and ensure it's
|
||||
// on the linker search path.
|
||||
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||
File::create(out.join("memory.x"))
|
||||
.unwrap()
|
||||
.write_all(include_bytes!("memory.x"))
|
||||
.unwrap();
|
||||
println!("cargo:rustc-link-search={}", out.display());
|
||||
|
||||
// By default, Cargo will re-run a build script whenever
|
||||
// any file in the project changes. By specifying `memory.x`
|
||||
// here, we ensure the build script is only re-run when
|
||||
// `memory.x` is changed.
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||
}
|
14
examples/boot/memory.x
Normal file
14
examples/boot/memory.x
Normal file
@ -0,0 +1,14 @@
|
||||
MEMORY
|
||||
{
|
||||
/* NOTE 1 K = 1 KiBi = 1024 bytes */
|
||||
BOOTLOADER_STATE : ORIGIN = 0x00006000, LENGTH = 4K
|
||||
FLASH : ORIGIN = 0x00007000, LENGTH = 64K
|
||||
DFU : ORIGIN = 0x00017000, LENGTH = 68K
|
||||
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K
|
||||
}
|
||||
|
||||
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE);
|
||||
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE);
|
||||
|
||||
__bootloader_dfu_start = ORIGIN(DFU);
|
||||
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU);
|
49
examples/boot/src/bin/a.rs
Normal file
49
examples/boot/src/bin/a.rs
Normal file
@ -0,0 +1,49 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![macro_use]
|
||||
#![feature(generic_associated_types)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy_boot_nrf::updater;
|
||||
use embassy_nrf::{
|
||||
gpio::{Input, Pull},
|
||||
gpio::{Level, Output, OutputDrive},
|
||||
nvmc::Nvmc,
|
||||
Peripherals,
|
||||
};
|
||||
use embassy_traits::adapter::BlockingAsync;
|
||||
use embedded_hal::digital::v2::InputPin;
|
||||
use panic_reset as _;
|
||||
|
||||
static APP_B: &[u8] = include_bytes!("../../b.bin");
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_s: embassy::executor::Spawner, p: Peripherals) {
|
||||
let mut button = Input::new(p.P0_11, Pull::Up);
|
||||
let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard);
|
||||
//let mut led = Output::new(p.P1_10, Level::Low, OutputDrive::Standard);
|
||||
//let mut button = Input::new(p.P1_02, Pull::Up);
|
||||
|
||||
let nvmc = Nvmc::new(p.NVMC);
|
||||
let mut nvmc = BlockingAsync::new(nvmc);
|
||||
|
||||
loop {
|
||||
button.wait_for_any_edge().await;
|
||||
if button.is_low().unwrap() {
|
||||
let mut updater = updater::new();
|
||||
let mut offset = 0;
|
||||
for chunk in APP_B.chunks(4096) {
|
||||
let mut buf: [u8; 4096] = [0; 4096];
|
||||
buf[..chunk.len()].copy_from_slice(chunk);
|
||||
updater
|
||||
.write_firmware(offset, &buf, &mut nvmc)
|
||||
.await
|
||||
.unwrap();
|
||||
offset += chunk.len();
|
||||
}
|
||||
updater.mark_update(&mut nvmc).await.unwrap();
|
||||
led.set_high();
|
||||
cortex_m::peripheral::SCB::sys_reset();
|
||||
}
|
||||
}
|
||||
}
|
26
examples/boot/src/bin/b.rs
Normal file
26
examples/boot/src/bin/b.rs
Normal file
@ -0,0 +1,26 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![macro_use]
|
||||
#![feature(generic_associated_types)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_nrf::{
|
||||
gpio::{Level, Output, OutputDrive},
|
||||
Peripherals,
|
||||
};
|
||||
|
||||
use panic_reset as _;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_s: embassy::executor::Spawner, p: Peripherals) {
|
||||
let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard);
|
||||
//let mut led = Output::new(p.P1_10, Level::Low, OutputDrive::Standard);
|
||||
|
||||
loop {
|
||||
led.set_high();
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
led.set_low();
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user