feat: make nrf bootloader watchdog generic for any flash

This commit is contained in:
Ulf Lilleengen 2023-08-02 22:57:42 +02:00
parent 0d8a9b1e7a
commit bcaef1de18

View file

@ -6,7 +6,7 @@ mod fmt;
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
pub use embassy_boot::FirmwareUpdater; pub use embassy_boot::FirmwareUpdater;
pub use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, BootLoaderConfig, FirmwareUpdaterConfig}; pub use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, BootLoaderConfig, FirmwareUpdaterConfig};
use embassy_nrf::nvmc::{Nvmc, PAGE_SIZE}; use embassy_nrf::nvmc::PAGE_SIZE;
use embassy_nrf::peripherals::WDT; use embassy_nrf::peripherals::WDT;
use embassy_nrf::wdt; use embassy_nrf::wdt;
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
@ -104,15 +104,15 @@ impl<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize>
} }
} }
/// A flash implementation that wraps NVMC and will pet a watchdog when touching flash. /// A flash implementation that wraps any flash and will pet a watchdog when touching flash.
pub struct WatchdogFlash<'d> { pub struct WatchdogFlash<FLASH> {
flash: Nvmc<'d>, flash: FLASH,
wdt: wdt::WatchdogHandle, wdt: wdt::WatchdogHandle,
} }
impl<'d> WatchdogFlash<'d> { impl<FLASH> WatchdogFlash<FLASH> {
/// Start a new watchdog with a given flash and WDT peripheral and a timeout /// Start a new watchdog with a given flash and WDT peripheral and a timeout
pub fn start(flash: Nvmc<'d>, wdt: WDT, config: wdt::Config) -> Self { pub fn start(flash: FLASH, wdt: WDT, config: wdt::Config) -> Self {
let (_wdt, [wdt]) = match wdt::Watchdog::try_new(wdt, config) { let (_wdt, [wdt]) = match wdt::Watchdog::try_new(wdt, config) {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(_) => {
@ -127,13 +127,13 @@ impl<'d> WatchdogFlash<'d> {
} }
} }
impl<'d> ErrorType for WatchdogFlash<'d> { impl<FLASH: ErrorType> ErrorType for WatchdogFlash<FLASH> {
type Error = <Nvmc<'d> as ErrorType>::Error; type Error = FLASH::Error;
} }
impl<'d> NorFlash for WatchdogFlash<'d> { impl<FLASH: NorFlash> NorFlash for WatchdogFlash<FLASH> {
const WRITE_SIZE: usize = <Nvmc<'d> as NorFlash>::WRITE_SIZE; const WRITE_SIZE: usize = FLASH::WRITE_SIZE;
const ERASE_SIZE: usize = <Nvmc<'d> as NorFlash>::ERASE_SIZE; const ERASE_SIZE: usize = FLASH::ERASE_SIZE;
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
self.wdt.pet(); self.wdt.pet();
@ -145,8 +145,8 @@ impl<'d> NorFlash for WatchdogFlash<'d> {
} }
} }
impl<'d> ReadNorFlash for WatchdogFlash<'d> { impl<FLASH: ReadNorFlash> ReadNorFlash for WatchdogFlash<FLASH> {
const READ_SIZE: usize = <Nvmc<'d> as ReadNorFlash>::READ_SIZE; const READ_SIZE: usize = FLASH::READ_SIZE;
fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> { fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> {
self.wdt.pet(); self.wdt.pet();
self.flash.read(offset, data) self.flash.read(offset, data)