STM32 write flash and jump to address

Hi!
I'm moving my bootloader for STM32 from C to Rust.

  1. How to write to the stm32f1 flash on Rust? That's easy on C using FLASH_Unlock()->FLASH_ErasePage()->FLASH_ProgramHalfWord()->FLASH_Lock(); but i didn't find similar on Rust.

  2. How to jump from app of bootloaader to the main app? Example of C:

typedef  void (*pFunction)(void);
pFunction Jump_To_Application;
uint32_t JumpAddress;

JumpAddress = *(uint32_t*) (APPLICATION_BEGIN + 4);
Jump_To_Application = (pFunction) JumpAddress;
//	Initialize user application's Stack Pointer
__set_MSP(*(vu32*) APPLICATION_BEGIN);
Jump_To_Application();
  1. How to set location of constant static variable if FLASH?
struct DeviceInfo{
 v1: u8,
 v2: u32,
}

static const device_info: DeviceInfo... // how to set location?

What crates are you using? What have you tried so far? Please give us some information to work with.

If you are using the stm32f1 crate, you can get the FLASH peripheral and do the same as in c.

Jumping to a certain address should be fairly easy as well, just transmute an address to a extern "C" fn() and you should be good to go:

fn jump_to_main() {
    let addr = 0x8000;
    unsafe {
        (std::mem::transmute::<_, extern "C" fn()>(addr))();
    }
}

Solution:

            #[allow(unused_mut)]
                let mut p = cortex_m::Peripherals::steal();
            #[cfg(not(armv6m))]
            p.SCB.invalidate_icache();
            p.SCB.vtor.write(address as u32);
            cortex_m::asm::bootload(address as *const u32);

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.