diff options
| author | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-30 13:41:10 +0200 |
|---|---|---|
| committer | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-30 13:41:10 +0200 |
| commit | b23e40f72242a9f4759d8a6d4a924c8c9d041c67 (patch) | |
| tree | 9da5657aaa0903b55eca01c5a9086ee967e21786 | |
| parent | 1cd87f0028c8a0f9b87a09016a22179fb05ced33 (diff) | |
Add TestFlash helper
| -rw-r--r-- | embassy-boot/boot/src/lib.rs | 6 | ||||
| -rw-r--r-- | embassy-boot/boot/src/test_flash/asynch.rs | 57 | ||||
| -rw-r--r-- | embassy-boot/boot/src/test_flash/blocking.rs | 65 | ||||
| -rw-r--r-- | embassy-boot/boot/src/test_flash/mod.rs | 7 |
4 files changed, 135 insertions, 0 deletions
diff --git a/embassy-boot/boot/src/lib.rs b/embassy-boot/boot/src/lib.rs index f2034fa8a..c76087ff1 100644 --- a/embassy-boot/boot/src/lib.rs +++ b/embassy-boot/boot/src/lib.rs | |||
| @@ -7,10 +7,16 @@ mod fmt; | |||
| 7 | mod boot_loader; | 7 | mod boot_loader; |
| 8 | mod digest_adapters; | 8 | mod digest_adapters; |
| 9 | mod firmware_updater; | 9 | mod firmware_updater; |
| 10 | #[cfg(test)] | ||
| 10 | mod mem_flash; | 11 | mod mem_flash; |
| 11 | mod partition; | 12 | mod partition; |
| 13 | #[cfg(test)] | ||
| 14 | mod test_flash; | ||
| 12 | 15 | ||
| 13 | pub use partition::Partition; | 16 | pub use partition::Partition; |
| 17 | // The expected value of the flash after an erase | ||
| 18 | // TODO: Use the value provided by NorFlash when available | ||
| 19 | pub(crate) const STATE_ERASE_VALUE: u8 = 0xFF; | ||
| 14 | pub use boot_loader::{BootError, BootLoader, BootLoaderConfig}; | 20 | pub use boot_loader::{BootError, BootLoader, BootLoaderConfig}; |
| 15 | #[cfg(feature = "nightly")] | 21 | #[cfg(feature = "nightly")] |
| 16 | pub use firmware_updater::FirmwareUpdater; | 22 | pub use firmware_updater::FirmwareUpdater; |
diff --git a/embassy-boot/boot/src/test_flash/asynch.rs b/embassy-boot/boot/src/test_flash/asynch.rs new file mode 100644 index 000000000..b5b3c2538 --- /dev/null +++ b/embassy-boot/boot/src/test_flash/asynch.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | use embassy_embedded_hal::flash::partition::Partition; | ||
| 2 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 3 | use embassy_sync::mutex::Mutex; | ||
| 4 | use embedded_storage_async::nor_flash::NorFlash; | ||
| 5 | |||
| 6 | pub struct AsyncTestFlash<ACTIVE, DFU, STATE> | ||
| 7 | where | ||
| 8 | ACTIVE: NorFlash, | ||
| 9 | DFU: NorFlash, | ||
| 10 | STATE: NorFlash, | ||
| 11 | { | ||
| 12 | active: Mutex<NoopRawMutex, ACTIVE>, | ||
| 13 | dfu: Mutex<NoopRawMutex, DFU>, | ||
| 14 | state: Mutex<NoopRawMutex, STATE>, | ||
| 15 | } | ||
| 16 | |||
| 17 | impl<ACTIVE, DFU, STATE> AsyncTestFlash<ACTIVE, DFU, STATE> | ||
| 18 | where | ||
| 19 | ACTIVE: NorFlash, | ||
| 20 | DFU: NorFlash, | ||
| 21 | STATE: NorFlash, | ||
| 22 | { | ||
| 23 | pub fn new(active: ACTIVE, dfu: DFU, state: STATE) -> Self { | ||
| 24 | Self { | ||
| 25 | active: Mutex::new(active), | ||
| 26 | dfu: Mutex::new(dfu), | ||
| 27 | state: Mutex::new(state), | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | pub fn active(&self) -> Partition<NoopRawMutex, ACTIVE> { | ||
| 32 | Self::create_partition(&self.active) | ||
| 33 | } | ||
| 34 | |||
| 35 | pub fn dfu(&self) -> Partition<NoopRawMutex, DFU> { | ||
| 36 | Self::create_partition(&self.dfu) | ||
| 37 | } | ||
| 38 | |||
| 39 | pub fn state(&self) -> Partition<NoopRawMutex, STATE> { | ||
| 40 | Self::create_partition(&self.state) | ||
| 41 | } | ||
| 42 | |||
| 43 | fn create_partition<T: NorFlash>(mutex: &Mutex<NoopRawMutex, T>) -> Partition<NoopRawMutex, T> { | ||
| 44 | Partition::new(mutex, 0, mutex.try_lock().unwrap().capacity() as u32) | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | impl<ACTIVE, DFU, STATE> AsyncTestFlash<ACTIVE, DFU, STATE> | ||
| 49 | where | ||
| 50 | ACTIVE: NorFlash + embedded_storage::nor_flash::NorFlash, | ||
| 51 | DFU: NorFlash + embedded_storage::nor_flash::NorFlash, | ||
| 52 | STATE: NorFlash + embedded_storage::nor_flash::NorFlash, | ||
| 53 | { | ||
| 54 | pub fn into_blocking(self) -> super::BlockingTestFlash<ACTIVE, DFU, STATE> { | ||
| 55 | super::BlockingTestFlash::new(self.active.into_inner(), self.dfu.into_inner(), self.state.into_inner()) | ||
| 56 | } | ||
| 57 | } | ||
diff --git a/embassy-boot/boot/src/test_flash/blocking.rs b/embassy-boot/boot/src/test_flash/blocking.rs new file mode 100644 index 000000000..77876a218 --- /dev/null +++ b/embassy-boot/boot/src/test_flash/blocking.rs | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | use core::cell::RefCell; | ||
| 2 | |||
| 3 | use embassy_embedded_hal::flash::partition::BlockingPartition; | ||
| 4 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 5 | use embassy_sync::blocking_mutex::Mutex; | ||
| 6 | use embedded_storage::nor_flash::NorFlash; | ||
| 7 | |||
| 8 | pub struct BlockingTestFlash<ACTIVE, DFU, STATE> | ||
| 9 | where | ||
| 10 | ACTIVE: NorFlash, | ||
| 11 | DFU: NorFlash, | ||
| 12 | STATE: NorFlash, | ||
| 13 | { | ||
| 14 | active: Mutex<NoopRawMutex, RefCell<ACTIVE>>, | ||
| 15 | dfu: Mutex<NoopRawMutex, RefCell<DFU>>, | ||
| 16 | state: Mutex<NoopRawMutex, RefCell<STATE>>, | ||
| 17 | } | ||
| 18 | |||
| 19 | impl<ACTIVE, DFU, STATE> BlockingTestFlash<ACTIVE, DFU, STATE> | ||
| 20 | where | ||
| 21 | ACTIVE: NorFlash, | ||
| 22 | DFU: NorFlash, | ||
| 23 | STATE: NorFlash, | ||
| 24 | { | ||
| 25 | pub fn new(active: ACTIVE, dfu: DFU, state: STATE) -> Self { | ||
| 26 | Self { | ||
| 27 | active: Mutex::new(RefCell::new(active)), | ||
| 28 | dfu: Mutex::new(RefCell::new(dfu)), | ||
| 29 | state: Mutex::new(RefCell::new(state)), | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | pub fn active(&self) -> BlockingPartition<NoopRawMutex, ACTIVE> { | ||
| 34 | Self::create_partition(&self.active) | ||
| 35 | } | ||
| 36 | |||
| 37 | pub fn dfu(&self) -> BlockingPartition<NoopRawMutex, DFU> { | ||
| 38 | Self::create_partition(&self.dfu) | ||
| 39 | } | ||
| 40 | |||
| 41 | pub fn state(&self) -> BlockingPartition<NoopRawMutex, STATE> { | ||
| 42 | Self::create_partition(&self.state) | ||
| 43 | } | ||
| 44 | |||
| 45 | pub fn create_partition<T: NorFlash>( | ||
| 46 | mutex: &Mutex<NoopRawMutex, RefCell<T>>, | ||
| 47 | ) -> BlockingPartition<NoopRawMutex, T> { | ||
| 48 | BlockingPartition::new(mutex, 0, mutex.lock(|f| f.borrow().capacity()) as u32) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl<ACTIVE, DFU, STATE> BlockingTestFlash<ACTIVE, DFU, STATE> | ||
| 53 | where | ||
| 54 | ACTIVE: NorFlash + embedded_storage_async::nor_flash::NorFlash, | ||
| 55 | DFU: NorFlash + embedded_storage_async::nor_flash::NorFlash, | ||
| 56 | STATE: NorFlash + embedded_storage_async::nor_flash::NorFlash, | ||
| 57 | { | ||
| 58 | pub fn into_async(self) -> super::AsyncTestFlash<ACTIVE, DFU, STATE> { | ||
| 59 | super::AsyncTestFlash::new( | ||
| 60 | self.active.into_inner().into_inner(), | ||
| 61 | self.dfu.into_inner().into_inner(), | ||
| 62 | self.state.into_inner().into_inner(), | ||
| 63 | ) | ||
| 64 | } | ||
| 65 | } | ||
diff --git a/embassy-boot/boot/src/test_flash/mod.rs b/embassy-boot/boot/src/test_flash/mod.rs new file mode 100644 index 000000000..a0672322e --- /dev/null +++ b/embassy-boot/boot/src/test_flash/mod.rs | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | #[cfg(feature = "nightly")] | ||
| 2 | mod asynch; | ||
| 3 | mod blocking; | ||
| 4 | |||
| 5 | #[cfg(feature = "nightly")] | ||
| 6 | pub(crate) use asynch::AsyncTestFlash; | ||
| 7 | pub(crate) use blocking::BlockingTestFlash; | ||
