ARM32 MCU bringup

I am trying to bring up a bare metal STM32 from scratch.

My code is super simple

#![no_main]
#![no_std]

use core::{
    panic::PanicInfo,
};

#[link_section = ".vector_table.reset_vector"]
#[no_mangle]
pub static __RESET_VECTOR: fn() -> ! = reset_handler;

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {}
}

pub fn reset_handler() -> ! {
    loop {}
}

This works fine. And if I disassemble it, I see:

 08000008 <_ZN3app13reset_handler17hd5651b8c92fda766E>:
 8000008:       e7fe            b.n     8000008 <_ZN3app13reset_handler17hd5651b8c92fda766E>
 800000a:       d4d4            bmi.n   7ffffb6 <__RESET_VECTOR-0x4e>

All nice. Now if I modify it just a bit and add a "main" function. I.e.

pub fn reset_handler() -> ! {
    main()
}


pub fn main() -> ! {
    loop {
    }
}

The MCU crashes. If I disassemble the code I see:

 08000008 <_ZN3app13reset_handler17hd5651b8c92fda766E>:
 8000008:       defe            udf     #254    ; 0xfe
 800000a:       d4d4            bmi.n   7ffffb6 <__RESET_VECTOR-0x4e>

Notice the udf.

Any idea what am I doing wrong?

Forgot. My cargo config is:

[target.thumbv6m-none-eabi]
rustflags = ["-C", "link-arg=-Tlink.x"]

[build]
target = "thumbv6m-none-eabi"

And my MCU is STM32F051R8T6 which is ARM®32-bit Cortex®-M0

I'm not sure, but it may be related to this.

1 Like

Thanks, alice. I think it is this: LLVM loop optimization can make safe programs crash · Issue #28728 · rust-lang/rust · GitHub

Yup, you can call compiler_fence inside the loop to work around the issue.

1 Like

Thanks jschievink. I don't plan to leave it empty, was just trying to smallest possible program for my bring up. So I think I should be find now that I know it's not something that I screwed up.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.