stabilizer/hardware/
platform.rs1const DFU_REBOOT_FLAG: u32 = 0xDEAD_BEEF;
3
4extern "C" {
5 static mut _bootflag: u8;
6}
7
8pub fn start_dfu_reboot() {
10 unsafe {
11 core::ptr::write_unaligned(
12 core::ptr::addr_of_mut!(_bootflag).cast(),
13 DFU_REBOOT_FLAG,
14 );
15 }
16
17 cortex_m::peripheral::SCB::sys_reset();
18}
19
20pub fn dfu_bootflag() -> bool {
22 unsafe {
23 let start_ptr = core::ptr::addr_of_mut!(_bootflag).cast();
24 let set = DFU_REBOOT_FLAG == core::ptr::read_unaligned(start_ptr);
25
26 core::ptr::write_unaligned(start_ptr, 0);
28 set
29 }
30}
31
32pub fn execute_system_bootloader() {
38 cortex_m::interrupt::disable();
41
42 let systick = unsafe { &*cortex_m::peripheral::SYST::PTR };
44 unsafe {
45 systick.csr.write(0);
46 systick.rvr.write(0);
47 systick.cvr.write(0);
48 }
49
50 let nvic = unsafe { &*cortex_m::peripheral::NVIC::PTR };
52 for reg in nvic.icer.iter() {
53 unsafe {
54 reg.write(u32::MAX);
55 }
56 }
57
58 for reg in nvic.icpr.iter() {
59 unsafe {
60 reg.write(u32::MAX);
61 }
62 }
63
64 unsafe { cortex_m::interrupt::enable() };
65
66 unsafe {
69 let system_memory_address: *const u32 = 0x1FF0_9800 as *const u32;
70 log::info!("Jumping to DFU");
71 cortex_m::asm::bootload(system_memory_address);
72 }
73}