1138: embassy-boot (rp): Add WatchdogFlash r=Dirbaio a=kalkyl



Co-authored-by: kalkyl <henrik.alser@me.com>
This commit is contained in:
bors[bot] 2023-01-03 23:25:52 +00:00 committed by GitHub
commit 35afb60dd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 4 deletions

View File

@ -20,6 +20,8 @@ log = { version = "0.4", optional = true }
embassy-sync = { path = "../../embassy-sync" } embassy-sync = { path = "../../embassy-sync" }
embassy-rp = { path = "../../embassy-rp", default-features = false, features = ["nightly"] } embassy-rp = { path = "../../embassy-rp", default-features = false, features = ["nightly"] }
embassy-boot = { path = "../boot", default-features = false } embassy-boot = { path = "../boot", default-features = false }
embassy-time = { path = "../../embassy-time", features = ["nightly"] }
cortex-m = { version = "0.7.6" } cortex-m = { version = "0.7.6" }
cortex-m-rt = { version = "0.7" } cortex-m-rt = { version = "0.7" }
embedded-storage = "0.3.0" embedded-storage = "0.3.0"

View File

@ -5,7 +5,11 @@
mod fmt; mod fmt;
pub use embassy_boot::{AlignedBuffer, BootFlash, FirmwareUpdater, FlashConfig, Partition, SingleFlashConfig, State}; pub use embassy_boot::{AlignedBuffer, BootFlash, FirmwareUpdater, FlashConfig, Partition, SingleFlashConfig, State};
use embassy_rp::flash::{ERASE_SIZE, WRITE_SIZE}; use embassy_rp::flash::{Flash, ERASE_SIZE, WRITE_SIZE};
use embassy_rp::peripherals::{FLASH, WATCHDOG};
use embassy_rp::watchdog::Watchdog;
use embassy_time::Duration;
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
/// A bootloader for RP2040 devices. /// A bootloader for RP2040 devices.
pub struct BootLoader { pub struct BootLoader {
@ -88,3 +92,48 @@ impl Default for BootLoader {
Self::new(active, dfu, state) Self::new(active, dfu, state)
} }
} }
/// A flash implementation that will feed a watchdog when touching flash.
pub struct WatchdogFlash<'d, const SIZE: usize> {
flash: Flash<'d, FLASH, SIZE>,
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 {
let flash: Flash<'_, FLASH, SIZE> = Flash::new(flash);
let mut watchdog = Watchdog::new(watchdog);
watchdog.start(timeout);
Self { flash, watchdog }
}
}
impl<'d, const SIZE: usize> ErrorType for WatchdogFlash<'d, SIZE> {
type Error = <Flash<'d, FLASH, SIZE> as ErrorType>::Error;
}
impl<'d, const SIZE: usize> NorFlash for WatchdogFlash<'d, SIZE> {
const WRITE_SIZE: usize = <Flash<'d, FLASH, SIZE> as NorFlash>::WRITE_SIZE;
const ERASE_SIZE: usize = <Flash<'d, FLASH, SIZE> as NorFlash>::ERASE_SIZE;
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
self.watchdog.feed();
self.flash.erase(from, to)
}
fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
self.watchdog.feed();
self.flash.write(offset, data)
}
}
impl<'d, const SIZE: usize> ReadNorFlash for WatchdogFlash<'d, SIZE> {
const READ_SIZE: usize = <Flash<'d, FLASH, SIZE> as ReadNorFlash>::READ_SIZE;
fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> {
self.watchdog.feed();
self.flash.read(offset, data)
}
fn capacity(&self) -> usize {
self.flash.capacity()
}
}

View File

@ -7,6 +7,7 @@ use embassy_boot_rp::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_rp::flash::Flash; use embassy_rp::flash::Flash;
use embassy_rp::gpio::{Level, Output}; use embassy_rp::gpio::{Level, Output};
use embassy_rp::watchdog::Watchdog;
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
#[cfg(feature = "panic-probe")] #[cfg(feature = "panic-probe")]
use panic_probe as _; use panic_probe as _;
@ -21,11 +22,16 @@ async fn main(_s: Spawner) {
let p = embassy_rp::init(Default::default()); let p = embassy_rp::init(Default::default());
let mut led = Output::new(p.PIN_25, Level::Low); let mut led = Output::new(p.PIN_25, Level::Low);
// Override bootloader watchdog
let mut watchdog = Watchdog::new(p.WATCHDOG);
watchdog.start(Duration::from_secs(8));
let mut flash: Flash<_, FLASH_SIZE> = Flash::new(p.FLASH); let mut flash: Flash<_, FLASH_SIZE> = Flash::new(p.FLASH);
let mut updater = FirmwareUpdater::default(); let mut updater = FirmwareUpdater::default();
Timer::after(Duration::from_secs(5)).await; Timer::after(Duration::from_secs(5)).await;
watchdog.feed();
led.set_high(); led.set_high();
let mut offset = 0; let mut offset = 0;
let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]); let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]);
@ -43,6 +49,7 @@ async fn main(_s: Spawner) {
.unwrap(); .unwrap();
offset += chunk.len(); offset += chunk.len();
} }
watchdog.feed();
defmt::info!("firmware written, marking update"); defmt::info!("firmware written, marking update");
updater.mark_updated_blocking(&mut flash, &mut buf.0[..1]).unwrap(); updater.mark_updated_blocking(&mut flash, &mut buf.0[..1]).unwrap();
Timer::after(Duration::from_secs(2)).await; Timer::after(Duration::from_secs(2)).await;

View File

@ -11,6 +11,8 @@ defmt-rtt = { version = "0.4", optional = true }
embassy-rp = { path = "../../../../embassy-rp", default-features = false, features = ["nightly"] } embassy-rp = { path = "../../../../embassy-rp", default-features = false, features = ["nightly"] }
embassy-boot-rp = { path = "../../../../embassy-boot/rp", default-features = false } embassy-boot-rp = { path = "../../../../embassy-boot/rp", default-features = false }
embassy-time = { path = "../../../../embassy-time", features = ["nightly"] }
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
cortex-m-rt = { version = "0.7" } cortex-m-rt = { version = "0.7" }
embedded-storage = "0.3.0" embedded-storage = "0.3.0"

View File

@ -5,8 +5,8 @@ use cortex_m_rt::{entry, exception};
#[cfg(feature = "defmt")] #[cfg(feature = "defmt")]
use defmt_rtt as _; use defmt_rtt as _;
use embassy_boot_rp::*; use embassy_boot_rp::*;
use embassy_rp::flash::{Flash, ERASE_SIZE}; use embassy_rp::flash::ERASE_SIZE;
use embassy_rp::peripherals::FLASH; use embassy_time::Duration;
const FLASH_SIZE: usize = 2 * 1024 * 1024; const FLASH_SIZE: usize = 2 * 1024 * 1024;
@ -23,7 +23,7 @@ fn main() -> ! {
*/ */
let mut bl: BootLoader = BootLoader::default(); let mut bl: BootLoader = BootLoader::default();
let flash: Flash<'_, FLASH, FLASH_SIZE> = Flash::new(p.FLASH); let flash = WatchdogFlash::<FLASH_SIZE>::start(p.FLASH, p.WATCHDOG, Duration::from_secs(8));
let mut flash = BootFlash::<_, ERASE_SIZE>::new(flash); let mut flash = BootFlash::<_, ERASE_SIZE>::new(flash);
let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash)); let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash));
core::mem::drop(flash); core::mem::drop(flash);