aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-04-04 21:18:41 +0200
committerRasmus Melchior Jacobsen <[email protected]>2023-04-04 21:18:41 +0200
commit6c93309df490020f0ae4a515bf404dfd251b9b69 (patch)
treec46b934de8813b4a757665bc9ccd89b3324d74b2
parent25577e0eafd8a3d4ffaa4b8f17cb55399fd58038 (diff)
Remove the Flash trait
-rw-r--r--embassy-boot/boot/src/boot_loader.rs73
-rw-r--r--embassy-boot/boot/src/large_erase.rs6
-rw-r--r--embassy-boot/boot/src/lib.rs2
-rw-r--r--embassy-boot/boot/src/mem_flash.rs8
4 files changed, 32 insertions, 57 deletions
diff --git a/embassy-boot/boot/src/boot_loader.rs b/embassy-boot/boot/src/boot_loader.rs
index db067da5b..37fff621a 100644
--- a/embassy-boot/boot/src/boot_loader.rs
+++ b/embassy-boot/boot/src/boot_loader.rs
@@ -30,22 +30,18 @@ where
30 } 30 }
31} 31}
32 32
33/// Extension of the embedded-storage flash type information with block size and erase value.
34pub trait Flash: NorFlash {
35 /// The erase value of the flash. Typically the default of 0xFF is used, but some flashes use a different value.
36 const ERASE_VALUE: u8 = 0xFF;
37}
38
39/// Trait defining the flash handles used for active and DFU partition. 33/// Trait defining the flash handles used for active and DFU partition.
40/// The ACTIVE and DFU erase sizes must be equal. If this is not the case, then consider adding an adapter for the 34/// The ACTIVE and DFU erase sizes must be equal. If this is not the case, then consider adding an adapter for the
41/// smallest flash to increase its erase size such that they match. See e.g. [`crate::large_erase::LargeErase`]. 35/// smallest flash to increase its erase size such that they match. See e.g. [`crate::large_erase::LargeErase`].
42pub trait FlashConfig { 36pub trait FlashConfig {
37 /// The erase value of the state flash. Typically the default of 0xFF is used, but some flashes use a different value.
38 const STATE_ERASE_VALUE: u8 = 0xFF;
43 /// Flash type used for the state partition. 39 /// Flash type used for the state partition.
44 type STATE: Flash; 40 type STATE: NorFlash;
45 /// Flash type used for the active partition. 41 /// Flash type used for the active partition.
46 type ACTIVE: Flash; 42 type ACTIVE: NorFlash;
47 /// Flash type used for the dfu partition. 43 /// Flash type used for the dfu partition.
48 type DFU: Flash; 44 type DFU: NorFlash;
49 45
50 /// Return flash instance used to write/read to/from active partition. 46 /// Return flash instance used to write/read to/from active partition.
51 fn active(&mut self) -> &mut Self::ACTIVE; 47 fn active(&mut self) -> &mut Self::ACTIVE;
@@ -208,7 +204,7 @@ impl BootLoader {
208 let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; 204 let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE];
209 205
210 // Invalidate progress 206 // Invalidate progress
211 state_word.fill(!P::STATE::ERASE_VALUE); 207 state_word.fill(!P::STATE_ERASE_VALUE);
212 self.state 208 self.state
213 .write_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?; 209 .write_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?;
214 210
@@ -237,7 +233,7 @@ impl BootLoader {
237 233
238 self.state 234 self.state
239 .read_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?; 235 .read_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?;
240 if state_word.iter().any(|&b| b != P::STATE::ERASE_VALUE) { 236 if state_word.iter().any(|&b| b != P::STATE_ERASE_VALUE) {
241 // Progress is invalid 237 // Progress is invalid
242 return Ok(max_index); 238 return Ok(max_index);
243 } 239 }
@@ -249,7 +245,7 @@ impl BootLoader {
249 state_word, 245 state_word,
250 )?; 246 )?;
251 247
252 if state_word.iter().any(|&b| b == P::STATE::ERASE_VALUE) { 248 if state_word.iter().any(|&b| b == P::STATE_ERASE_VALUE) {
253 return Ok(index); 249 return Ok(index);
254 } 250 }
255 } 251 }
@@ -263,7 +259,7 @@ impl BootLoader {
263 aligned_buf: &mut [u8], 259 aligned_buf: &mut [u8],
264 ) -> Result<(), BootError> { 260 ) -> Result<(), BootError> {
265 let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE]; 261 let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE];
266 state_word.fill(!P::STATE::ERASE_VALUE); 262 state_word.fill(!P::STATE_ERASE_VALUE);
267 self.state 263 self.state
268 .write_blocking(p.state(), (2 + index) as u32 * P::STATE::WRITE_SIZE as u32, state_word)?; 264 .write_blocking(p.state(), (2 + index) as u32 * P::STATE::WRITE_SIZE as u32, state_word)?;
269 Ok(()) 265 Ok(())
@@ -386,16 +382,16 @@ fn assert_partitions(active: Partition, dfu: Partition, state: Partition, page_s
386} 382}
387 383
388/// A flash wrapper implementing the Flash and embedded_storage traits. 384/// A flash wrapper implementing the Flash and embedded_storage traits.
389pub struct BootFlash<F, const ERASE_VALUE: u8 = 0xFF> 385pub struct BootFlash<F>
390where 386where
391 F: NorFlash + ReadNorFlash, 387 F: NorFlash,
392{ 388{
393 flash: F, 389 flash: F,
394} 390}
395 391
396impl<F, const ERASE_VALUE: u8> BootFlash<F, ERASE_VALUE> 392impl<F> BootFlash<F>
397where 393where
398 F: NorFlash + ReadNorFlash, 394 F: NorFlash,
399{ 395{
400 /// Create a new instance of a bootable flash 396 /// Create a new instance of a bootable flash
401 pub fn new(flash: F) -> Self { 397 pub fn new(flash: F) -> Self {
@@ -403,23 +399,16 @@ where
403 } 399 }
404} 400}
405 401
406impl<F, const ERASE_VALUE: u8> Flash for BootFlash<F, ERASE_VALUE> 402impl<F> ErrorType for BootFlash<F>
407where
408 F: NorFlash + ReadNorFlash,
409{
410 const ERASE_VALUE: u8 = ERASE_VALUE;
411}
412
413impl<F, const ERASE_VALUE: u8> ErrorType for BootFlash<F, ERASE_VALUE>
414where 403where
415 F: ReadNorFlash + NorFlash, 404 F: NorFlash,
416{ 405{
417 type Error = F::Error; 406 type Error = F::Error;
418} 407}
419 408
420impl<F, const ERASE_VALUE: u8> NorFlash for BootFlash<F, ERASE_VALUE> 409impl<F> NorFlash for BootFlash<F>
421where 410where
422 F: ReadNorFlash + NorFlash, 411 F: NorFlash,
423{ 412{
424 const WRITE_SIZE: usize = F::WRITE_SIZE; 413 const WRITE_SIZE: usize = F::WRITE_SIZE;
425 const ERASE_SIZE: usize = F::ERASE_SIZE; 414 const ERASE_SIZE: usize = F::ERASE_SIZE;
@@ -433,9 +422,9 @@ where
433 } 422 }
434} 423}
435 424
436impl<F, const ERASE_VALUE: u8> ReadNorFlash for BootFlash<F, ERASE_VALUE> 425impl<F> ReadNorFlash for BootFlash<F>
437where 426where
438 F: ReadNorFlash + NorFlash, 427 F: NorFlash,
439{ 428{
440 const READ_SIZE: usize = F::READ_SIZE; 429 const READ_SIZE: usize = F::READ_SIZE;
441 430
@@ -451,14 +440,14 @@ where
451/// Convenience provider that uses a single flash for all partitions. 440/// Convenience provider that uses a single flash for all partitions.
452pub struct SingleFlashConfig<'a, F> 441pub struct SingleFlashConfig<'a, F>
453where 442where
454 F: Flash, 443 F: NorFlash,
455{ 444{
456 flash: &'a mut F, 445 flash: &'a mut F,
457} 446}
458 447
459impl<'a, F> SingleFlashConfig<'a, F> 448impl<'a, F> SingleFlashConfig<'a, F>
460where 449where
461 F: Flash, 450 F: NorFlash,
462{ 451{
463 /// Create a provider for a single flash. 452 /// Create a provider for a single flash.
464 pub fn new(flash: &'a mut F) -> Self { 453 pub fn new(flash: &'a mut F) -> Self {
@@ -468,7 +457,7 @@ where
468 457
469impl<'a, F> FlashConfig for SingleFlashConfig<'a, F> 458impl<'a, F> FlashConfig for SingleFlashConfig<'a, F>
470where 459where
471 F: Flash, 460 F: NorFlash,
472{ 461{
473 type STATE = F; 462 type STATE = F;
474 type ACTIVE = F; 463 type ACTIVE = F;
@@ -488,9 +477,9 @@ where
488/// Convenience flash provider that uses separate flash instances for each partition. 477/// Convenience flash provider that uses separate flash instances for each partition.
489pub struct MultiFlashConfig<'a, ACTIVE, STATE, DFU> 478pub struct MultiFlashConfig<'a, ACTIVE, STATE, DFU>
490where 479where
491 ACTIVE: Flash, 480 ACTIVE: NorFlash,
492 STATE: Flash, 481 STATE: NorFlash,
493 DFU: Flash, 482 DFU: NorFlash,
494{ 483{
495 active: &'a mut ACTIVE, 484 active: &'a mut ACTIVE,
496 state: &'a mut STATE, 485 state: &'a mut STATE,
@@ -499,9 +488,9 @@ where
499 488
500impl<'a, ACTIVE, STATE, DFU> MultiFlashConfig<'a, ACTIVE, STATE, DFU> 489impl<'a, ACTIVE, STATE, DFU> MultiFlashConfig<'a, ACTIVE, STATE, DFU>
501where 490where
502 ACTIVE: Flash, 491 ACTIVE: NorFlash,
503 STATE: Flash, 492 STATE: NorFlash,
504 DFU: Flash, 493 DFU: NorFlash,
505{ 494{
506 /// Create a new flash provider with separate configuration for all three partitions. 495 /// Create a new flash provider with separate configuration for all three partitions.
507 pub fn new(active: &'a mut ACTIVE, state: &'a mut STATE, dfu: &'a mut DFU) -> Self { 496 pub fn new(active: &'a mut ACTIVE, state: &'a mut STATE, dfu: &'a mut DFU) -> Self {
@@ -511,9 +500,9 @@ where
511 500
512impl<'a, ACTIVE, STATE, DFU> FlashConfig for MultiFlashConfig<'a, ACTIVE, STATE, DFU> 501impl<'a, ACTIVE, STATE, DFU> FlashConfig for MultiFlashConfig<'a, ACTIVE, STATE, DFU>
513where 502where
514 ACTIVE: Flash, 503 ACTIVE: NorFlash,
515 STATE: Flash, 504 STATE: NorFlash,
516 DFU: Flash, 505 DFU: NorFlash,
517{ 506{
518 type STATE = STATE; 507 type STATE = STATE;
519 type ACTIVE = ACTIVE; 508 type ACTIVE = ACTIVE;
diff --git a/embassy-boot/boot/src/large_erase.rs b/embassy-boot/boot/src/large_erase.rs
index d00d43599..b999a046f 100644
--- a/embassy-boot/boot/src/large_erase.rs
+++ b/embassy-boot/boot/src/large_erase.rs
@@ -3,8 +3,6 @@
3use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; 3use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
4use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash}; 4use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
5 5
6use crate::Flash;
7
8pub struct LargeErase<F, const ERASE_SIZE: usize>(pub F); 6pub struct LargeErase<F, const ERASE_SIZE: usize>(pub F);
9 7
10impl<F, const ERASE_SIZE: usize> LargeErase<F, ERASE_SIZE> { 8impl<F, const ERASE_SIZE: usize> LargeErase<F, ERASE_SIZE> {
@@ -13,10 +11,6 @@ impl<F, const ERASE_SIZE: usize> LargeErase<F, ERASE_SIZE> {
13 } 11 }
14} 12}
15 13
16impl<F: Flash, const ERASE_SIZE: usize> Flash for LargeErase<F, ERASE_SIZE> {
17 const ERASE_VALUE: u8 = F::ERASE_VALUE;
18}
19
20impl<F: ErrorType, const ERASE_SIZE: usize> ErrorType for LargeErase<F, ERASE_SIZE> { 14impl<F: ErrorType, const ERASE_SIZE: usize> ErrorType for LargeErase<F, ERASE_SIZE> {
21 type Error = F::Error; 15 type Error = F::Error;
22} 16}
diff --git a/embassy-boot/boot/src/lib.rs b/embassy-boot/boot/src/lib.rs
index 79759124b..cc812d797 100644
--- a/embassy-boot/boot/src/lib.rs
+++ b/embassy-boot/boot/src/lib.rs
@@ -11,7 +11,7 @@ mod large_erase;
11mod mem_flash; 11mod mem_flash;
12mod partition; 12mod partition;
13 13
14pub use boot_loader::{BootError, BootFlash, BootLoader, Flash, FlashConfig, MultiFlashConfig, SingleFlashConfig}; 14pub use boot_loader::{BootError, BootFlash, BootLoader, FlashConfig, MultiFlashConfig, SingleFlashConfig};
15pub use firmware_updater::{FirmwareUpdater, FirmwareUpdaterError}; 15pub use firmware_updater::{FirmwareUpdater, FirmwareUpdaterError};
16pub use partition::Partition; 16pub use partition::Partition;
17 17
diff --git a/embassy-boot/boot/src/mem_flash.rs b/embassy-boot/boot/src/mem_flash.rs
index 2598bf4de..dd85405c8 100644
--- a/embassy-boot/boot/src/mem_flash.rs
+++ b/embassy-boot/boot/src/mem_flash.rs
@@ -5,8 +5,6 @@ use core::ops::{Bound, Range, RangeBounds};
5use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash}; 5use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash};
6use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash}; 6use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
7 7
8use crate::Flash;
9
10pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> { 8pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> {
11 pub mem: [u8; SIZE], 9 pub mem: [u8; SIZE],
12 pub pending_write_successes: Option<usize>, 10 pub pending_write_successes: Option<usize>,
@@ -44,12 +42,6 @@ impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Defaul
44 } 42 }
45} 43}
46 44
47impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Flash
48 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
49{
50 const ERASE_VALUE: u8 = 0xFF;
51}
52
53impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType 45impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
54 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> 46 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
55{ 47{