Fixed hardfault due to broken memory layout

This commit is contained in:
Sebastian 2024-12-03 17:50:35 +01:00
parent 57fdf05f00
commit f7483fe42a
3 changed files with 39 additions and 37 deletions

View file

@ -1,4 +1,4 @@
use core::ptr::addr_of_mut;
use core::{mem::MaybeUninit, ptr::addr_of_mut};
use stm32f4xx_hal::pac;
@ -23,33 +23,28 @@ fn jump_to_bootloader() {
const BOOTLOADER_REQUESTED: u32 = 0xdecafbad;
fn magic_mut_ptr() -> *mut u32 {
extern "C" {
#[link_name = "_dfu_magic"]
static mut magic: u32;
}
unsafe { addr_of_mut!(magic) }
}
#[link_section = ".uninit.DFU_FLAG"]
static mut DFU_FLAG: MaybeUninit<u32> = MaybeUninit::uninit();
fn get_bootloader_flag() -> u32 {
unsafe { magic_mut_ptr().read_volatile() }
unsafe { DFU_FLAG.assume_init() }
}
fn set_bootloader_flag() {
unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) };
unsafe { DFU_FLAG.write(BOOTLOADER_REQUESTED) };
}
fn clear_bootloader_flag() {
unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) };
unsafe { DFU_FLAG.write(0) };
}
pub fn init() {
let requested = get_bootloader_flag() == BOOTLOADER_REQUESTED;
if requested {
clear_bootloader_flag();
jump_to_bootloader();
}
clear_bootloader_flag();
}
pub fn reboot_to_bootloader() -> ! {