2022-12-01 18:26:22 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2023-05-30 14:05:38 +02:00
|
|
|
use core::cell::RefCell;
|
|
|
|
|
2022-12-01 18:26:22 +01:00
|
|
|
use defmt_rtt as _;
|
|
|
|
use embassy_boot_rp::*;
|
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_rp::flash::Flash;
|
|
|
|
use embassy_rp::gpio::{Level, Output};
|
2023-01-03 22:58:56 +01:00
|
|
|
use embassy_rp::watchdog::Watchdog;
|
2023-05-30 14:03:31 +02:00
|
|
|
use embassy_sync::blocking_mutex::Mutex;
|
2023-05-30 14:05:38 +02:00
|
|
|
use embassy_time::{Duration, Timer};
|
2023-05-30 14:03:31 +02:00
|
|
|
use embedded_storage::nor_flash::NorFlash;
|
2022-12-01 18:26:22 +01:00
|
|
|
#[cfg(feature = "panic-probe")]
|
|
|
|
use panic_probe as _;
|
|
|
|
#[cfg(feature = "panic-reset")]
|
|
|
|
use panic_reset as _;
|
|
|
|
|
2023-06-19 23:30:51 +02:00
|
|
|
#[cfg(feature = "skip-include")]
|
|
|
|
static APP_B: &[u8] = &[0, 1, 2, 3];
|
|
|
|
#[cfg(not(feature = "skip-include"))]
|
2022-12-01 18:26:22 +01:00
|
|
|
static APP_B: &[u8] = include_bytes!("../../b.bin");
|
2023-06-19 23:30:51 +02:00
|
|
|
|
2022-12-01 18:26:22 +01:00
|
|
|
const FLASH_SIZE: usize = 2 * 1024 * 1024;
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_s: Spawner) {
|
|
|
|
let p = embassy_rp::init(Default::default());
|
|
|
|
let mut led = Output::new(p.PIN_25, Level::Low);
|
|
|
|
|
2023-01-03 22:58:56 +01:00
|
|
|
// Override bootloader watchdog
|
|
|
|
let mut watchdog = Watchdog::new(p.WATCHDOG);
|
|
|
|
watchdog.start(Duration::from_secs(8));
|
|
|
|
|
2023-05-30 14:03:31 +02:00
|
|
|
let flash: Flash<_, FLASH_SIZE> = Flash::new(p.FLASH);
|
|
|
|
let flash = Mutex::new(RefCell::new(flash));
|
2022-12-01 18:26:22 +01:00
|
|
|
|
2023-05-30 14:03:31 +02:00
|
|
|
let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash);
|
|
|
|
let mut updater = BlockingFirmwareUpdater::new(config);
|
2022-12-01 18:26:22 +01:00
|
|
|
|
|
|
|
Timer::after(Duration::from_secs(5)).await;
|
2023-01-03 22:58:56 +01:00
|
|
|
watchdog.feed();
|
2022-12-01 18:26:22 +01:00
|
|
|
led.set_high();
|
|
|
|
let mut offset = 0;
|
|
|
|
let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]);
|
|
|
|
defmt::info!("preparing update");
|
2023-05-30 14:03:31 +02:00
|
|
|
let writer = updater
|
2023-06-19 23:30:51 +02:00
|
|
|
.prepare_update(&mut buf.0[..1])
|
2022-12-01 18:26:22 +01:00
|
|
|
.map_err(|e| defmt::warn!("E: {:?}", defmt::Debug2Format(&e)))
|
|
|
|
.unwrap();
|
|
|
|
defmt::info!("writer created, starting write");
|
|
|
|
for chunk in APP_B.chunks(4096) {
|
|
|
|
buf.0[..chunk.len()].copy_from_slice(chunk);
|
|
|
|
defmt::info!("writing block at offset {}", offset);
|
2023-05-30 14:05:38 +02:00
|
|
|
writer.write(offset, &buf.0[..]).unwrap();
|
2023-05-30 14:03:31 +02:00
|
|
|
offset += chunk.len() as u32;
|
2022-12-01 18:26:22 +01:00
|
|
|
}
|
2023-01-03 22:58:56 +01:00
|
|
|
watchdog.feed();
|
2022-12-01 18:26:22 +01:00
|
|
|
defmt::info!("firmware written, marking update");
|
2023-05-30 14:03:31 +02:00
|
|
|
updater.mark_updated(&mut buf.0[..1]).unwrap();
|
2022-12-01 18:26:22 +01:00
|
|
|
Timer::after(Duration::from_secs(2)).await;
|
|
|
|
led.set_low();
|
|
|
|
defmt::info!("update marked, resetting");
|
|
|
|
cortex_m::peripheral::SCB::sys_reset();
|
|
|
|
}
|