Split the single app partition into two slots, ota_0 and ota_1, with an otadata partition that tells the bootloader which to run, so an update can be written to the idle slot while the other keeps running. The Zigbee storage partition is unchanged, so the light keeps its network. Moving from the old table needs one USB flash. * src/ota.rs logs the running slot and the state of its image at boot, marks an image valid after it has stayed up for 20 seconds, and can switch to the other slot and restart. The slot in use is read from the flash mapping, not from otadata. * src/flash.rs shares the one flash handle: esp-storage allows only one, and both the Zigbee stack and the update code need it. * The runner clears otadata on every USB flash. A USB flash always writes slot 0, so without this a board that had switched to slot 1 would keep booting the old image. * Two development features, ota-test and ota-test-crash, exercise slot switching and an unconfirmed image without an update source. FW_LABEL tells builds apart in the log. Tested on the hardware: switching between two images across six reboots, each booting the slot it had just selected, resuming its Zigbee network, and confirming itself. Rollback is not available: the bootloader espflash installs is not built with it, so a newly selected image reports "New" rather than "PendingVerify", and an image that resets without confirming itself is booted again indefinitely. Needs a rollback-enabled bootloader. Nothing delivers updates yet. Both are recorded in TODO.md. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
80 lines
2.7 KiB
Rust
80 lines
2.7 KiB
Rust
//! The one flash handle, shared.
|
|
//!
|
|
//! `esp_storage::FlashStorage` can only be created once, but two things need
|
|
//! the flash: the Zigbee stack, which persists its state there, and the
|
|
//! firmware update code, which reads the partition table and the OTA data.
|
|
//!
|
|
//! Every access takes a critical section. `esp-storage` already makes each
|
|
//! flash operation atomic with respect to interrupts, so this adds no new
|
|
//! stall for a single read, erase or write; it only keeps two tasks from
|
|
//! interleaving a sequence of them.
|
|
|
|
use core::cell::RefCell;
|
|
|
|
use embassy_sync::blocking_mutex::Mutex;
|
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
use embedded_storage::nor_flash::ErrorType;
|
|
use embedded_storage::nor_flash::MultiwriteNorFlash;
|
|
use embedded_storage::nor_flash::NorFlash;
|
|
use embedded_storage::nor_flash::ReadNorFlash;
|
|
pub use esp_storage::FlashStorage;
|
|
|
|
static FLASH: Mutex<CriticalSectionRawMutex, RefCell<Option<FlashStorage<'static>>>> =
|
|
Mutex::new(RefCell::new(None));
|
|
|
|
/// Hands the flash over to this module. Call once, before anything uses it.
|
|
pub fn init(flash: FlashStorage<'static>) {
|
|
FLASH.lock(|cell| *cell.borrow_mut() = Some(flash));
|
|
}
|
|
|
|
/// Runs `f` with exclusive access to the flash.
|
|
///
|
|
/// `f` must not call back into this module: the flash is already borrowed.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// If [`init`] has not been called.
|
|
pub fn with<R>(f: impl FnOnce(&mut FlashStorage<'static>) -> R) -> R {
|
|
FLASH.lock(|cell| {
|
|
let mut flash = cell.borrow_mut();
|
|
f(flash.as_mut().expect("flash not initialised"))
|
|
})
|
|
}
|
|
|
|
/// A handle to the shared flash that is itself a flash device, for code that
|
|
/// wants to own one, such as the Zigbee storage.
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
#[cfg_attr(not(feature = "zigbee"), allow(dead_code))]
|
|
pub struct SharedFlash;
|
|
|
|
impl ErrorType for SharedFlash {
|
|
type Error = <FlashStorage<'static> as ErrorType>::Error;
|
|
}
|
|
|
|
impl ReadNorFlash for SharedFlash {
|
|
const READ_SIZE: usize = <FlashStorage<'static> as ReadNorFlash>::READ_SIZE;
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
with(|flash| flash.read(offset, bytes))
|
|
}
|
|
|
|
fn capacity(&self) -> usize {
|
|
with(|flash| flash.capacity())
|
|
}
|
|
}
|
|
|
|
impl NorFlash for SharedFlash {
|
|
const WRITE_SIZE: usize = <FlashStorage<'static> as NorFlash>::WRITE_SIZE;
|
|
const ERASE_SIZE: usize = <FlashStorage<'static> as NorFlash>::ERASE_SIZE;
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
with(|flash| flash.erase(from, to))
|
|
}
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
with(|flash| flash.write(offset, bytes))
|
|
}
|
|
}
|
|
|
|
impl MultiwriteNorFlash for SharedFlash {}
|