2022-12-01 18:26:22 +01:00
|
|
|
#![no_std]
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
#![doc = include_str!("../README.md")]
|
|
|
|
mod fmt;
|
|
|
|
|
2023-08-03 20:56:04 +02:00
|
|
|
pub use embassy_boot::{
|
2023-11-29 17:23:48 +01:00
|
|
|
AlignedBuffer, BlockingFirmwareState, BlockingFirmwareUpdater, BootLoaderConfig, FirmwareState, FirmwareUpdater,
|
|
|
|
FirmwareUpdaterConfig, State,
|
2023-08-03 20:56:04 +02:00
|
|
|
};
|
2023-07-25 23:54:33 +02:00
|
|
|
use embassy_rp::flash::{Blocking, Flash, ERASE_SIZE};
|
2023-01-03 22:58:56 +01:00
|
|
|
use embassy_rp::peripherals::{FLASH, WATCHDOG};
|
|
|
|
use embassy_rp::watchdog::Watchdog;
|
|
|
|
use embassy_time::Duration;
|
|
|
|
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
|
2022-12-01 18:26:22 +01:00
|
|
|
|
|
|
|
/// A bootloader for RP2040 devices.
|
2023-08-11 19:47:24 +02:00
|
|
|
pub struct BootLoader<const BUFFER_SIZE: usize = ERASE_SIZE>;
|
2022-12-01 18:26:22 +01:00
|
|
|
|
2023-08-11 19:47:24 +02:00
|
|
|
impl<const BUFFER_SIZE: usize> BootLoader<BUFFER_SIZE> {
|
|
|
|
/// Inspect the bootloader state and perform actions required before booting, such as swapping firmware
|
|
|
|
pub fn prepare<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash>(
|
|
|
|
config: BootLoaderConfig<ACTIVE, DFU, STATE>,
|
|
|
|
) -> Self {
|
|
|
|
let mut aligned_buf = AlignedBuffer([0; BUFFER_SIZE]);
|
|
|
|
let mut boot = embassy_boot::BootLoader::new(config);
|
|
|
|
boot.prepare_boot(aligned_buf.as_mut()).expect("Boot prepare error");
|
|
|
|
Self
|
2022-12-01 18:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Boots the application.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This modifies the stack pointer and reset vector and will run code placed in the active partition.
|
2023-05-30 14:03:31 +02:00
|
|
|
pub unsafe fn load(self, start: u32) -> ! {
|
2022-12-01 18:26:22 +01:00
|
|
|
trace!("Loading app at 0x{:x}", start);
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut p = cortex_m::Peripherals::steal();
|
|
|
|
#[cfg(not(armv6m))]
|
|
|
|
p.SCB.invalidate_icache();
|
2023-05-30 13:57:03 +02:00
|
|
|
p.SCB.vtor.write(start);
|
2022-12-01 18:26:22 +01:00
|
|
|
|
|
|
|
cortex_m::asm::bootload(start as *const u32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 23:34:50 +01:00
|
|
|
/// A flash implementation that will feed a watchdog when touching flash.
|
2023-01-03 22:58:56 +01:00
|
|
|
pub struct WatchdogFlash<'d, const SIZE: usize> {
|
2023-07-25 23:54:33 +02:00
|
|
|
flash: Flash<'d, FLASH, Blocking, SIZE>,
|
2023-01-03 22:58:56 +01:00
|
|
|
watchdog: Watchdog,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, const SIZE: usize> WatchdogFlash<'d, SIZE> {
|
|
|
|
/// Start a new watchdog with a given flash and watchdog peripheral and a timeout
|
|
|
|
pub fn start(flash: FLASH, watchdog: WATCHDOG, timeout: Duration) -> Self {
|
2023-08-18 13:12:19 +02:00
|
|
|
let flash = Flash::<_, Blocking, SIZE>::new_blocking(flash);
|
2023-01-03 22:58:56 +01:00
|
|
|
let mut watchdog = Watchdog::new(watchdog);
|
|
|
|
watchdog.start(timeout);
|
|
|
|
Self { flash, watchdog }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, const SIZE: usize> ErrorType for WatchdogFlash<'d, SIZE> {
|
2023-07-25 23:54:33 +02:00
|
|
|
type Error = <Flash<'d, FLASH, Blocking, SIZE> as ErrorType>::Error;
|
2023-01-03 22:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, const SIZE: usize> NorFlash for WatchdogFlash<'d, SIZE> {
|
2023-07-25 23:54:33 +02:00
|
|
|
const WRITE_SIZE: usize = <Flash<'d, FLASH, Blocking, SIZE> as NorFlash>::WRITE_SIZE;
|
|
|
|
const ERASE_SIZE: usize = <Flash<'d, FLASH, Blocking, SIZE> as NorFlash>::ERASE_SIZE;
|
2023-01-03 22:58:56 +01:00
|
|
|
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
self.watchdog.feed();
|
2023-08-18 13:12:19 +02:00
|
|
|
self.flash.blocking_erase(from, to)
|
2023-01-03 22:58:56 +01:00
|
|
|
}
|
|
|
|
fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.watchdog.feed();
|
2023-08-18 13:12:19 +02:00
|
|
|
self.flash.blocking_write(offset, data)
|
2023-01-03 22:58:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, const SIZE: usize> ReadNorFlash for WatchdogFlash<'d, SIZE> {
|
2023-07-25 23:54:33 +02:00
|
|
|
const READ_SIZE: usize = <Flash<'d, FLASH, Blocking, SIZE> as ReadNorFlash>::READ_SIZE;
|
2023-01-03 22:58:56 +01:00
|
|
|
fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
self.watchdog.feed();
|
2023-08-18 13:12:19 +02:00
|
|
|
self.flash.blocking_read(offset, data)
|
2023-01-03 22:58:56 +01:00
|
|
|
}
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
self.flash.capacity()
|
|
|
|
}
|
|
|
|
}
|