2022-01-24 12:54:09 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![macro_use]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-04-20 13:49:59 +02:00
|
|
|
use embassy_boot_nrf::FirmwareUpdater;
|
2022-05-26 18:54:58 +02:00
|
|
|
use embassy_embedded_hal::adapter::BlockingAsync;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-06-12 22:15:44 +02:00
|
|
|
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pull};
|
|
|
|
use embassy_nrf::nvmc::Nvmc;
|
2023-01-04 01:07:07 +01:00
|
|
|
use embassy_nrf::wdt::{self, Watchdog};
|
2022-01-24 12:54:09 +01:00
|
|
|
use panic_reset as _;
|
|
|
|
|
|
|
|
static APP_B: &[u8] = include_bytes!("../../b.bin");
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2022-01-24 12:54:09 +01:00
|
|
|
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);
|
|
|
|
|
2023-01-04 01:07:07 +01:00
|
|
|
// The following code block illustrates how to obtain a watchdog that is configured
|
|
|
|
// as per the existing watchdog. Ordinarily, we'd use the handle returned to "pet" the
|
|
|
|
// watchdog periodically. If we don't, and we're not going to for this example, then
|
|
|
|
// the watchdog will cause the device to reset as per its configured timeout in the bootloader.
|
|
|
|
// This helps is avoid a situation where new firmware might be bad and block our executor.
|
|
|
|
// If firmware is bad in this way then the bootloader will revert to any previous version.
|
|
|
|
let wdt_config = wdt::Config::try_new(&p.WDT).unwrap();
|
|
|
|
let (_wdt, [_wdt_handle]) = match Watchdog::try_new(p.WDT, wdt_config) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => {
|
|
|
|
// Watchdog already active with the wrong number of handles, waiting for it to timeout...
|
|
|
|
loop {
|
|
|
|
cortex_m::asm::wfe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-24 12:54:09 +01:00
|
|
|
let nvmc = Nvmc::new(p.NVMC);
|
|
|
|
let mut nvmc = BlockingAsync::new(nvmc);
|
|
|
|
|
2022-04-20 13:49:59 +02:00
|
|
|
let mut updater = FirmwareUpdater::default();
|
2022-01-24 12:54:09 +01:00
|
|
|
loop {
|
2022-04-28 14:08:33 +02:00
|
|
|
led.set_low();
|
2022-01-24 12:54:09 +01:00
|
|
|
button.wait_for_any_edge().await;
|
2022-04-19 14:42:38 +02:00
|
|
|
if button.is_low() {
|
2022-01-24 12:54:09 +01:00
|
|
|
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);
|
2022-06-12 22:15:44 +02:00
|
|
|
updater.write_firmware(offset, &buf, &mut nvmc, 4096).await.unwrap();
|
2022-01-24 12:54:09 +01:00
|
|
|
offset += chunk.len();
|
|
|
|
}
|
2022-08-30 13:07:35 +02:00
|
|
|
let mut magic = [0; 4];
|
|
|
|
updater.mark_updated(&mut nvmc, &mut magic).await.unwrap();
|
2022-01-24 12:54:09 +01:00
|
|
|
led.set_high();
|
|
|
|
cortex_m::peripheral::SCB::sys_reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|