diff options
| author | kalkyl <[email protected]> | 2023-01-03 22:58:56 +0100 |
|---|---|---|
| committer | kalkyl <[email protected]> | 2023-01-03 22:58:56 +0100 |
| commit | 9428c40c8de1285271a5e6ba9ad2a7fed8a9475e (patch) | |
| tree | 30d672bdc0d80f714dfbb5338c9dd41f5ae82061 /embassy-boot/rp/src/lib.rs | |
| parent | 0aa2a9ac2705ead5186d4c1d53bba55064c33db7 (diff) | |
embassy-boot (rp): Add WatchdogFlash
Diffstat (limited to 'embassy-boot/rp/src/lib.rs')
| -rw-r--r-- | embassy-boot/rp/src/lib.rs | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/embassy-boot/rp/src/lib.rs b/embassy-boot/rp/src/lib.rs index 85fc81827..6eb429aaa 100644 --- a/embassy-boot/rp/src/lib.rs +++ b/embassy-boot/rp/src/lib.rs | |||
| @@ -5,7 +5,11 @@ | |||
| 5 | mod fmt; | 5 | mod fmt; |
| 6 | 6 | ||
| 7 | pub use embassy_boot::{AlignedBuffer, BootFlash, FirmwareUpdater, FlashConfig, Partition, SingleFlashConfig, State}; | 7 | pub use embassy_boot::{AlignedBuffer, BootFlash, FirmwareUpdater, FlashConfig, Partition, SingleFlashConfig, State}; |
| 8 | use embassy_rp::flash::{ERASE_SIZE, WRITE_SIZE}; | 8 | use embassy_rp::flash::{Flash, ERASE_SIZE, WRITE_SIZE}; |
| 9 | use embassy_rp::peripherals::{FLASH, WATCHDOG}; | ||
| 10 | use embassy_rp::watchdog::Watchdog; | ||
| 11 | use embassy_time::Duration; | ||
| 12 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | ||
| 9 | 13 | ||
| 10 | /// A bootloader for RP2040 devices. | 14 | /// A bootloader for RP2040 devices. |
| 11 | pub struct BootLoader { | 15 | pub struct BootLoader { |
| @@ -88,3 +92,48 @@ impl Default for BootLoader { | |||
| 88 | Self::new(active, dfu, state) | 92 | Self::new(active, dfu, state) |
| 89 | } | 93 | } |
| 90 | } | 94 | } |
| 95 | |||
| 96 | /// A flash implementation that wraps FLASH and will pet a watchdog when touching flash. | ||
| 97 | pub struct WatchdogFlash<'d, const SIZE: usize> { | ||
| 98 | flash: Flash<'d, FLASH, SIZE>, | ||
| 99 | watchdog: Watchdog, | ||
| 100 | } | ||
| 101 | |||
| 102 | impl<'d, const SIZE: usize> WatchdogFlash<'d, SIZE> { | ||
| 103 | /// Start a new watchdog with a given flash and watchdog peripheral and a timeout | ||
| 104 | pub fn start(flash: FLASH, watchdog: WATCHDOG, timeout: Duration) -> Self { | ||
| 105 | let flash: Flash<'_, FLASH, SIZE> = Flash::new(flash); | ||
| 106 | let mut watchdog = Watchdog::new(watchdog); | ||
| 107 | watchdog.start(timeout); | ||
| 108 | Self { flash, watchdog } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | impl<'d, const SIZE: usize> ErrorType for WatchdogFlash<'d, SIZE> { | ||
| 113 | type Error = <Flash<'d, FLASH, SIZE> as ErrorType>::Error; | ||
| 114 | } | ||
| 115 | |||
| 116 | impl<'d, const SIZE: usize> NorFlash for WatchdogFlash<'d, SIZE> { | ||
| 117 | const WRITE_SIZE: usize = <Flash<'d, FLASH, SIZE> as NorFlash>::WRITE_SIZE; | ||
| 118 | const ERASE_SIZE: usize = <Flash<'d, FLASH, SIZE> as NorFlash>::ERASE_SIZE; | ||
| 119 | |||
| 120 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 121 | self.watchdog.feed(); | ||
| 122 | self.flash.erase(from, to) | ||
| 123 | } | ||
| 124 | fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { | ||
| 125 | self.watchdog.feed(); | ||
| 126 | self.flash.write(offset, data) | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | impl<'d, const SIZE: usize> ReadNorFlash for WatchdogFlash<'d, SIZE> { | ||
| 131 | const READ_SIZE: usize = <Flash<'d, FLASH, SIZE> as ReadNorFlash>::READ_SIZE; | ||
| 132 | fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> { | ||
| 133 | self.watchdog.feed(); | ||
| 134 | self.flash.read(offset, data) | ||
| 135 | } | ||
| 136 | fn capacity(&self) -> usize { | ||
| 137 | self.flash.capacity() | ||
| 138 | } | ||
| 139 | } | ||
