aboutsummaryrefslogtreecommitdiff
path: root/embassy-boot
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-05-30 13:41:10 +0200
committerRasmus Melchior Jacobsen <[email protected]>2023-05-30 13:41:10 +0200
commitb23e40f72242a9f4759d8a6d4a924c8c9d041c67 (patch)
tree9da5657aaa0903b55eca01c5a9086ee967e21786 /embassy-boot
parent1cd87f0028c8a0f9b87a09016a22179fb05ced33 (diff)
Add TestFlash helper
Diffstat (limited to 'embassy-boot')
-rw-r--r--embassy-boot/boot/src/lib.rs6
-rw-r--r--embassy-boot/boot/src/test_flash/asynch.rs57
-rw-r--r--embassy-boot/boot/src/test_flash/blocking.rs65
-rw-r--r--embassy-boot/boot/src/test_flash/mod.rs7
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;
7mod boot_loader; 7mod boot_loader;
8mod digest_adapters; 8mod digest_adapters;
9mod firmware_updater; 9mod firmware_updater;
10#[cfg(test)]
10mod mem_flash; 11mod mem_flash;
11mod partition; 12mod partition;
13#[cfg(test)]
14mod test_flash;
12 15
13pub use partition::Partition; 16pub use partition::Partition;
17// The expected value of the flash after an erase
18// TODO: Use the value provided by NorFlash when available
19pub(crate) const STATE_ERASE_VALUE: u8 = 0xFF;
14pub use boot_loader::{BootError, BootLoader, BootLoaderConfig}; 20pub use boot_loader::{BootError, BootLoader, BootLoaderConfig};
15#[cfg(feature = "nightly")] 21#[cfg(feature = "nightly")]
16pub use firmware_updater::FirmwareUpdater; 22pub 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 @@
1use embassy_embedded_hal::flash::partition::Partition;
2use embassy_sync::blocking_mutex::raw::NoopRawMutex;
3use embassy_sync::mutex::Mutex;
4use embedded_storage_async::nor_flash::NorFlash;
5
6pub struct AsyncTestFlash<ACTIVE, DFU, STATE>
7where
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
17impl<ACTIVE, DFU, STATE> AsyncTestFlash<ACTIVE, DFU, STATE>
18where
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
48impl<ACTIVE, DFU, STATE> AsyncTestFlash<ACTIVE, DFU, STATE>
49where
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 @@
1use core::cell::RefCell;
2
3use embassy_embedded_hal::flash::partition::BlockingPartition;
4use embassy_sync::blocking_mutex::raw::NoopRawMutex;
5use embassy_sync::blocking_mutex::Mutex;
6use embedded_storage::nor_flash::NorFlash;
7
8pub struct BlockingTestFlash<ACTIVE, DFU, STATE>
9where
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
19impl<ACTIVE, DFU, STATE> BlockingTestFlash<ACTIVE, DFU, STATE>
20where
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
52impl<ACTIVE, DFU, STATE> BlockingTestFlash<ACTIVE, DFU, STATE>
53where
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")]
2mod asynch;
3mod blocking;
4
5#[cfg(feature = "nightly")]
6pub(crate) use asynch::AsyncTestFlash;
7pub(crate) use blocking::BlockingTestFlash;