2022-04-20 13:49:59 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2023-05-30 14:03:31 +02:00
|
|
|
use core::cell::RefCell;
|
|
|
|
|
2022-04-20 13:49:59 +02:00
|
|
|
use cortex_m_rt::{entry, exception};
|
|
|
|
#[cfg(feature = "defmt")]
|
|
|
|
use defmt_rtt as _;
|
|
|
|
use embassy_boot_stm32::*;
|
2023-05-30 14:03:31 +02:00
|
|
|
use embassy_stm32::flash::{Flash, BANK1_REGION};
|
|
|
|
use embassy_sync::blocking_mutex::Mutex;
|
2022-04-20 13:49:59 +02:00
|
|
|
|
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
let p = embassy_stm32::init(Default::default());
|
|
|
|
|
|
|
|
// Uncomment this if you are debugging the bootloader with debugger/RTT attached,
|
|
|
|
// as it prevents a hard fault when accessing flash 'too early' after boot.
|
|
|
|
/*
|
|
|
|
for i in 0..10000000 {
|
|
|
|
cortex_m::asm::nop();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2023-05-25 21:40:54 +02:00
|
|
|
let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
|
2023-05-30 14:03:31 +02:00
|
|
|
let flash = Mutex::new(RefCell::new(layout.bank1_region));
|
|
|
|
|
|
|
|
let config = BootLoaderConfig::from_linkerfile_blocking(&flash);
|
|
|
|
let active_offset = config.active.offset();
|
|
|
|
let mut bl: BootLoader<_, _, _, 2048> = BootLoader::new(config);
|
|
|
|
|
|
|
|
bl.prepare();
|
|
|
|
|
|
|
|
unsafe { bl.load(BANK1_REGION.base + active_offset) }
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
|
|
|
|
unsafe extern "C" fn HardFault() {
|
|
|
|
cortex_m::peripheral::SCB::sys_reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[exception]
|
|
|
|
unsafe fn DefaultHandler(_: i16) -> ! {
|
|
|
|
const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
|
|
|
|
let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
|
|
|
|
|
|
|
|
panic!("DefaultHandler #{:?}", irqn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[panic_handler]
|
|
|
|
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
2022-04-26 18:33:09 +02:00
|
|
|
cortex_m::asm::udf();
|
2022-04-20 13:49:59 +02:00
|
|
|
}
|