diff options
Diffstat (limited to 'embassy-boot-stm32/src/lib.rs')
| -rw-r--r-- | embassy-boot-stm32/src/lib.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/embassy-boot-stm32/src/lib.rs b/embassy-boot-stm32/src/lib.rs new file mode 100644 index 000000000..4b4091ac9 --- /dev/null +++ b/embassy-boot-stm32/src/lib.rs | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![warn(missing_docs)] | ||
| 3 | #![doc = include_str!("../README.md")] | ||
| 4 | mod fmt; | ||
| 5 | |||
| 6 | pub use embassy_boot::{ | ||
| 7 | AlignedBuffer, BlockingFirmwareState, BlockingFirmwareUpdater, BootLoaderConfig, FirmwareState, FirmwareUpdater, | ||
| 8 | FirmwareUpdaterConfig, State, | ||
| 9 | }; | ||
| 10 | use embedded_storage::nor_flash::NorFlash; | ||
| 11 | |||
| 12 | /// A bootloader for STM32 devices. | ||
| 13 | pub struct BootLoader { | ||
| 14 | /// The reported state of the bootloader after preparing for boot | ||
| 15 | pub state: State, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl BootLoader { | ||
| 19 | /// Inspect the bootloader state and perform actions required before booting, such as swapping firmware | ||
| 20 | pub fn prepare<ACTIVE: NorFlash, DFU: NorFlash, STATE: NorFlash, const BUFFER_SIZE: usize>( | ||
| 21 | config: BootLoaderConfig<ACTIVE, DFU, STATE>, | ||
| 22 | ) -> Self { | ||
| 23 | let mut aligned_buf = AlignedBuffer([0; BUFFER_SIZE]); | ||
| 24 | let mut boot = embassy_boot::BootLoader::new(config); | ||
| 25 | let state = boot.prepare_boot(aligned_buf.as_mut()).expect("Boot prepare error"); | ||
| 26 | Self { state } | ||
| 27 | } | ||
| 28 | |||
| 29 | /// Boots the application. | ||
| 30 | /// | ||
| 31 | /// # Safety | ||
| 32 | /// | ||
| 33 | /// This modifies the stack pointer and reset vector and will run code placed in the active partition. | ||
| 34 | pub unsafe fn load(self, start: u32) -> ! { | ||
| 35 | trace!("Loading app at 0x{:x}", start); | ||
| 36 | #[allow(unused_mut)] | ||
| 37 | let mut p = cortex_m::Peripherals::steal(); | ||
| 38 | #[cfg(not(armv6m))] | ||
| 39 | p.SCB.invalidate_icache(); | ||
| 40 | p.SCB.vtor.write(start); | ||
| 41 | |||
| 42 | cortex_m::asm::bootload(start as *const u32) | ||
| 43 | } | ||
| 44 | } | ||
