aboutsummaryrefslogtreecommitdiff
path: root/embassy-boot/nrf/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-boot/nrf/src/lib.rs')
-rw-r--r--embassy-boot/nrf/src/lib.rs145
1 files changed, 0 insertions, 145 deletions
diff --git a/embassy-boot/nrf/src/lib.rs b/embassy-boot/nrf/src/lib.rs
deleted file mode 100644
index 5b20a93c6..000000000
--- a/embassy-boot/nrf/src/lib.rs
+++ /dev/null
@@ -1,145 +0,0 @@
1#![no_std]
2#![warn(missing_docs)]
3#![doc = include_str!("../README.md")]
4mod fmt;
5
6pub use embassy_boot::{
7 AlignedBuffer, BlockingFirmwareState, BlockingFirmwareUpdater, BootLoaderConfig, FirmwareState, FirmwareUpdater,
8 FirmwareUpdaterConfig,
9};
10use embassy_nrf::nvmc::PAGE_SIZE;
11use embassy_nrf::peripherals::WDT;
12use embassy_nrf::wdt;
13use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
14
15/// A bootloader for nRF devices.
16pub struct BootLoader<const BUFFER_SIZE: usize = PAGE_SIZE>;
17
18impl<const BUFFER_SIZE: usize> BootLoader<BUFFER_SIZE> {
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>(
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 boot.prepare_boot(&mut aligned_buf.0).expect("Boot prepare error");
26 Self
27 }
28
29 /// Boots the application without softdevice mechanisms.
30 ///
31 /// # Safety
32 ///
33 /// This modifies the stack pointer and reset vector and will run code placed in the active partition.
34 #[cfg(not(feature = "softdevice"))]
35 pub unsafe fn load(self, start: u32) -> ! {
36 let mut p = cortex_m::Peripherals::steal();
37 p.SCB.invalidate_icache();
38 p.SCB.vtor.write(start);
39 cortex_m::asm::bootload(start as *const u32)
40 }
41
42 /// Boots the application assuming softdevice is present.
43 ///
44 /// # Safety
45 ///
46 /// This modifies the stack pointer and reset vector and will run code placed in the active partition.
47 #[cfg(feature = "softdevice")]
48 pub unsafe fn load(self, _app: u32) -> ! {
49 use nrf_softdevice_mbr as mbr;
50 const NRF_SUCCESS: u32 = 0;
51
52 // Address of softdevice which we'll forward interrupts to
53 let addr = 0x1000;
54 let mut cmd = mbr::sd_mbr_command_t {
55 command: mbr::NRF_MBR_COMMANDS_SD_MBR_COMMAND_IRQ_FORWARD_ADDRESS_SET,
56 params: mbr::sd_mbr_command_t__bindgen_ty_1 {
57 irq_forward_address_set: mbr::sd_mbr_command_irq_forward_address_set_t { address: addr },
58 },
59 };
60 let ret = mbr::sd_mbr_command(&mut cmd);
61 assert_eq!(ret, NRF_SUCCESS);
62
63 let msp = *(addr as *const u32);
64 let rv = *((addr + 4) as *const u32);
65
66 trace!("msp = {=u32:x}, rv = {=u32:x}", msp, rv);
67
68 // These instructions perform the following operations:
69 //
70 // * Modify control register to use MSP as stack pointer (clear spsel bit)
71 // * Synchronize instruction barrier
72 // * Initialize stack pointer (0x1000)
73 // * Set link register to not return (0xFF)
74 // * Jump to softdevice reset vector
75 core::arch::asm!(
76 "mrs {tmp}, CONTROL",
77 "bics {tmp}, {spsel}",
78 "msr CONTROL, {tmp}",
79 "isb",
80 "msr MSP, {msp}",
81 "mov lr, {new_lr}",
82 "bx {rv}",
83 // `out(reg) _` is not permitted in a `noreturn` asm! call,
84 // so instead use `in(reg) 0` and don't restore it afterwards.
85 tmp = in(reg) 0,
86 spsel = in(reg) 2,
87 new_lr = in(reg) 0xFFFFFFFFu32,
88 msp = in(reg) msp,
89 rv = in(reg) rv,
90 options(noreturn),
91 );
92 }
93}
94
95/// A flash implementation that wraps any flash and will pet a watchdog when touching flash.
96pub struct WatchdogFlash<FLASH> {
97 flash: FLASH,
98 wdt: wdt::WatchdogHandle,
99}
100
101impl<FLASH> WatchdogFlash<FLASH> {
102 /// Start a new watchdog with a given flash and WDT peripheral and a timeout
103 pub fn start(flash: FLASH, wdt: WDT, config: wdt::Config) -> Self {
104 let (_wdt, [wdt]) = match wdt::Watchdog::try_new(wdt, config) {
105 Ok(x) => x,
106 Err(_) => {
107 // In case the watchdog is already running, just spin and let it expire, since
108 // we can't configure it anyway. This usually happens when we first program
109 // the device and the watchdog was previously active
110 info!("Watchdog already active with wrong config, waiting for it to timeout...");
111 loop {}
112 }
113 };
114 Self { flash, wdt }
115 }
116}
117
118impl<FLASH: ErrorType> ErrorType for WatchdogFlash<FLASH> {
119 type Error = FLASH::Error;
120}
121
122impl<FLASH: NorFlash> NorFlash for WatchdogFlash<FLASH> {
123 const WRITE_SIZE: usize = FLASH::WRITE_SIZE;
124 const ERASE_SIZE: usize = FLASH::ERASE_SIZE;
125
126 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
127 self.wdt.pet();
128 self.flash.erase(from, to)
129 }
130 fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
131 self.wdt.pet();
132 self.flash.write(offset, data)
133 }
134}
135
136impl<FLASH: ReadNorFlash> ReadNorFlash for WatchdogFlash<FLASH> {
137 const READ_SIZE: usize = FLASH::READ_SIZE;
138 fn read(&mut self, offset: u32, data: &mut [u8]) -> Result<(), Self::Error> {
139 self.wdt.pet();
140 self.flash.read(offset, data)
141 }
142 fn capacity(&self) -> usize {
143 self.flash.capacity()
144 }
145}