Compile and Flashing problem and elf file problem

Hi Team,
I am trying to run a LED blink program on the STM32F410RBT6 board. The compilation is successful, but the program is not flashing. The next issue is that the ELF file is not showing up properly, and there seems to be a linking issue. I need some guidance on this. I have attached the program and documentation for reference.


main.rs file
#![no_std]
#![no_main]

use cortex_m_rt::entry;
use stm32f4::stm32f410;
use stm32f4xx_hal::{prelude::*, delay::Delay};
use cortex_m::peripheral::Peripherals; // Correct import for Peripherals

// Panic handler: loops forever on panic
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {
// Stay here in case of panic
}
}

#[entry]
fn main() -> ! {
// Take ownership of the STM32F4 peripherals
let dp = stm32f410::Peripherals::take().unwrap();

// Take ownership of the core peripherals (SYST, NVIC, etc.)
let cp = Peripherals::take().unwrap();

// Configure and freeze system clocks
let clocks = dp.RCC.constrain().cfgr.freeze();

// Initialize delay provider using the system timer (SYST)
let mut delay = Delay::new(cp.SYST, &clocks);

// Set up GPIO pin for LED (assuming LED is connected to a pin)
let gpioa = dp.GPIOA.split();
let mut led = gpioa.pa5.into_push_pull_output(); // Assuming LED is on pin PA5

loop {
    // Toggle LED every second
    led.toggle();
    delay.delay_ms(1_000_u16);  // 1-second delay
}

}
Cargo.toml file
[package]

name = "stm32_led_blink"

version = "0.1.0"

edition = "2021"

[dependencies]

cortex-m-rtic = "1.1"

cortex-m-rt = "0.7"

cortex-m = "0.7"

panic-halt = "0.2"

stm32f4xx-hal = { version = "0.11", features = ["stm32f410"] }

stm32f4 = { version = "0.14.0", features = ["stm32f410"] }
memory.x file
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 128K
RAM : ORIGIN = 0x20000000, LENGTH = 32K
}

SECTIONS
{
.text : { (.text) } > FLASH
.bss : { (.bss) } > RAM
.data : { (.data) } > RAM
}
config.toml file
[target.thumbv7em-none-eabihf]
runner = "probe-rs run --chip STM32F410RBT6 --gdb-port 3333"

I think you are missing the necessary configuration to use the linker script of cortex-m-rt, which is named link.x. without it, your memory.x is useless.

if you want to put this in .cargo/config.toml, you must have a line like this:

# .cargo/config.toml
rustflags = [
  "-C", "link-arg=-Tlink.x",
]

alternatively, you can set the flag in your build script, which should include a line like this:

// build.rs
fn main() {
    // Set the linker script to the one provided by cortex-m-rt.
    println!("cargo:rustc-link-arg=-Tlink.x");
}

side note:

I strongly recommend to use a template to setup a bare metal project, such as:

Thank you for your quick reply.
After you mentioned it, the ELF file is now generated properly, and I tried to flash it, but debugging is still not working. Please guide me further.

what do you mean by "not working"? it's hard to know what exact problem you are encountering.

I wonder what example/guide/tutorial are you following? if you are completely new to embedded rust, it's better to follow an example (or a step-by-step guide).

if you like to learn by tinkering code, you can start with one of the examples provided by the hal crate or the recommended template for stm32.

if you like written materials, you should start with the embedded rust book and follow the steps there. the book uses the "cortex-m quick start" template I posted previously.

for debugging specifically, this blog post contains detailed guide on how to setup everything.

Hi @nerditation,
your solution worked perfectly for my issue.Now I'm able to debug my code. Thank you so much for taking the time and effort to help. It's because of generous support like yours that I could resolve this problem. Thanks again!

Thanks &Regards,
Muthu Arasan R

1 Like

Please don't forget to mark @nerditation's reply as the solution using the checkbox at the bottom of their message.