diff options
| -rw-r--r-- | embassy-boot/rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | embassy-boot/rp/src/lib.rs | 51 | ||||
| -rw-r--r-- | examples/boot/application/rp/src/bin/a.rs | 7 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/boot/bootloader/rp/src/main.rs | 6 |
5 files changed, 64 insertions, 4 deletions
diff --git a/embassy-boot/rp/Cargo.toml b/embassy-boot/rp/Cargo.toml index 93099b233..ffc36a4e0 100644 --- a/embassy-boot/rp/Cargo.toml +++ b/embassy-boot/rp/Cargo.toml | |||
| @@ -20,6 +20,8 @@ log = { version = "0.4", optional = true } | |||
| 20 | embassy-sync = { path = "../../embassy-sync" } | 20 | embassy-sync = { path = "../../embassy-sync" } |
| 21 | embassy-rp = { path = "../../embassy-rp", default-features = false, features = ["nightly"] } | 21 | embassy-rp = { path = "../../embassy-rp", default-features = false, features = ["nightly"] } |
| 22 | embassy-boot = { path = "../boot", default-features = false } | 22 | embassy-boot = { path = "../boot", default-features = false } |
| 23 | embassy-time = { path = "../../embassy-time", features = ["nightly"] } | ||
| 24 | |||
| 23 | cortex-m = { version = "0.7.6" } | 25 | cortex-m = { version = "0.7.6" } |
| 24 | cortex-m-rt = { version = "0.7" } | 26 | cortex-m-rt = { version = "0.7" } |
| 25 | embedded-storage = "0.3.0" | 27 | embedded-storage = "0.3.0" |
diff --git a/embassy-boot/rp/src/lib.rs b/embassy-boot/rp/src/lib.rs index 85fc81827..6df34133e 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 will feed 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 | } | ||
diff --git a/examples/boot/application/rp/src/bin/a.rs b/examples/boot/application/rp/src/bin/a.rs index 3736c9141..e3ac634c2 100644 --- a/examples/boot/application/rp/src/bin/a.rs +++ b/examples/boot/application/rp/src/bin/a.rs | |||
| @@ -7,6 +7,7 @@ use embassy_boot_rp::*; | |||
| 7 | use embassy_executor::Spawner; | 7 | use embassy_executor::Spawner; |
| 8 | use embassy_rp::flash::Flash; | 8 | use embassy_rp::flash::Flash; |
| 9 | use embassy_rp::gpio::{Level, Output}; | 9 | use embassy_rp::gpio::{Level, Output}; |
| 10 | use embassy_rp::watchdog::Watchdog; | ||
| 10 | use embassy_time::{Duration, Timer}; | 11 | use embassy_time::{Duration, Timer}; |
| 11 | #[cfg(feature = "panic-probe")] | 12 | #[cfg(feature = "panic-probe")] |
| 12 | use panic_probe as _; | 13 | use panic_probe as _; |
| @@ -21,11 +22,16 @@ async fn main(_s: Spawner) { | |||
| 21 | let p = embassy_rp::init(Default::default()); | 22 | let p = embassy_rp::init(Default::default()); |
| 22 | let mut led = Output::new(p.PIN_25, Level::Low); | 23 | let mut led = Output::new(p.PIN_25, Level::Low); |
| 23 | 24 | ||
| 25 | // Override bootloader watchdog | ||
| 26 | let mut watchdog = Watchdog::new(p.WATCHDOG); | ||
| 27 | watchdog.start(Duration::from_secs(8)); | ||
| 28 | |||
| 24 | let mut flash: Flash<_, FLASH_SIZE> = Flash::new(p.FLASH); | 29 | let mut flash: Flash<_, FLASH_SIZE> = Flash::new(p.FLASH); |
| 25 | 30 | ||
| 26 | let mut updater = FirmwareUpdater::default(); | 31 | let mut updater = FirmwareUpdater::default(); |
| 27 | 32 | ||
| 28 | Timer::after(Duration::from_secs(5)).await; | 33 | Timer::after(Duration::from_secs(5)).await; |
| 34 | watchdog.feed(); | ||
| 29 | led.set_high(); | 35 | led.set_high(); |
| 30 | let mut offset = 0; | 36 | let mut offset = 0; |
| 31 | let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]); | 37 | let mut buf: AlignedBuffer<4096> = AlignedBuffer([0; 4096]); |
| @@ -43,6 +49,7 @@ async fn main(_s: Spawner) { | |||
| 43 | .unwrap(); | 49 | .unwrap(); |
| 44 | offset += chunk.len(); | 50 | offset += chunk.len(); |
| 45 | } | 51 | } |
| 52 | watchdog.feed(); | ||
| 46 | defmt::info!("firmware written, marking update"); | 53 | defmt::info!("firmware written, marking update"); |
| 47 | updater.mark_updated_blocking(&mut flash, &mut buf.0[..1]).unwrap(); | 54 | updater.mark_updated_blocking(&mut flash, &mut buf.0[..1]).unwrap(); |
| 48 | Timer::after(Duration::from_secs(2)).await; | 55 | Timer::after(Duration::from_secs(2)).await; |
diff --git a/examples/boot/bootloader/rp/Cargo.toml b/examples/boot/bootloader/rp/Cargo.toml index 580ced22e..c0b576cff 100644 --- a/examples/boot/bootloader/rp/Cargo.toml +++ b/examples/boot/bootloader/rp/Cargo.toml | |||
| @@ -11,6 +11,8 @@ defmt-rtt = { version = "0.4", optional = true } | |||
| 11 | 11 | ||
| 12 | embassy-rp = { path = "../../../../embassy-rp", default-features = false, features = ["nightly"] } | 12 | embassy-rp = { path = "../../../../embassy-rp", default-features = false, features = ["nightly"] } |
| 13 | embassy-boot-rp = { path = "../../../../embassy-boot/rp", default-features = false } | 13 | embassy-boot-rp = { path = "../../../../embassy-boot/rp", default-features = false } |
| 14 | embassy-time = { path = "../../../../embassy-time", features = ["nightly"] } | ||
| 15 | |||
| 14 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } | 16 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } |
| 15 | cortex-m-rt = { version = "0.7" } | 17 | cortex-m-rt = { version = "0.7" } |
| 16 | embedded-storage = "0.3.0" | 18 | embedded-storage = "0.3.0" |
diff --git a/examples/boot/bootloader/rp/src/main.rs b/examples/boot/bootloader/rp/src/main.rs index 5028ec688..fb7f0522b 100644 --- a/examples/boot/bootloader/rp/src/main.rs +++ b/examples/boot/bootloader/rp/src/main.rs | |||
| @@ -5,8 +5,8 @@ use cortex_m_rt::{entry, exception}; | |||
| 5 | #[cfg(feature = "defmt")] | 5 | #[cfg(feature = "defmt")] |
| 6 | use defmt_rtt as _; | 6 | use defmt_rtt as _; |
| 7 | use embassy_boot_rp::*; | 7 | use embassy_boot_rp::*; |
| 8 | use embassy_rp::flash::{Flash, ERASE_SIZE}; | 8 | use embassy_rp::flash::ERASE_SIZE; |
| 9 | use embassy_rp::peripherals::FLASH; | 9 | use embassy_time::Duration; |
| 10 | 10 | ||
| 11 | const FLASH_SIZE: usize = 2 * 1024 * 1024; | 11 | const FLASH_SIZE: usize = 2 * 1024 * 1024; |
| 12 | 12 | ||
| @@ -23,7 +23,7 @@ fn main() -> ! { | |||
| 23 | */ | 23 | */ |
| 24 | 24 | ||
| 25 | let mut bl: BootLoader = BootLoader::default(); | 25 | let mut bl: BootLoader = BootLoader::default(); |
| 26 | let flash: Flash<'_, FLASH, FLASH_SIZE> = Flash::new(p.FLASH); | 26 | let flash = WatchdogFlash::<FLASH_SIZE>::start(p.FLASH, p.WATCHDOG, Duration::from_secs(8)); |
| 27 | let mut flash = BootFlash::<_, ERASE_SIZE>::new(flash); | 27 | let mut flash = BootFlash::<_, ERASE_SIZE>::new(flash); |
| 28 | let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash)); | 28 | let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash)); |
| 29 | core::mem::drop(flash); | 29 | core::mem::drop(flash); |
