Setting GPIO High or Low generates same binary Rust Embassy STM32
⚓ Rust 📅 2025-09-30 👤 surdeus 👁️ 8When my code sets GPIO High or Low, rustc still generates the exact same binary. The binary generated is only 24 bytes long and I don't think it contains any instructions relating to GPIO.
Here is my Rust code (I am aware of the trivial warnings):
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed, Pin};
use embassy_stm32::Config;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let mut config = Config::default();
    let p = embassy_stm32::init(config);
    let mut pa3 = Output::new(p.PA3, Level::Low, Speed::VeryHigh);
    pa3.set_low();
    loop { }
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop { }
}
I ran cargo build and took the MD5 hash of the binary, then ran cargo clear which deleted the binary. Then I changed 2 lines:
let mut pa3 = Output::new(p.PA3, Level::Low, Speed::VeryHigh);
changed to:
let mut pa3 = Output::new(p.PA3, Level::High, Speed::VeryHigh);
And pa3.set_low(); changed to: pa3.set_high();
I ran cargo build and took the MD5 Hash of the new binary and it was exactly the same as the old one. Additionally, I added lines to set states of other pins like A4 to high, and the binary is also the same.
I tried to be very thorough as to not make any trivial mistakes. Here is what I know:
Even when I change the code as I described, the compiler still produces the exact same 24 byte binaries. Even when I add:
    pa3.set_high();
    embassy_time::Timer::after_millis(500).await;
    pa3.set_low();
    embassy_time::Timer::after_millis(500).await;
inside the loop, nothing changes.
Using advanced analysis (ChatGPT), the binary contains no instructions relating to GPIO. Maybe they are being optimized out for some reason? I am using Rust with Embassy on an STM32WLE5CBU6.
Additional Files
Cargo.toml
[package]
name = "spider-router"
version = "0.1.0"
edition = "2024"
[dependencies]
cortex-m-rt = "0.7.5"
embassy-executor = { version = "0.9.1", features = ["arch-cortex-m", "executor-thread"] }
embassy-stm32 = { version = "0.4.0", features = ["stm32wle5cb"] }
embassy-time = "0.5.0"
I will include any other config files as needed. Thanks.
2 posts - 2 participants
🏷️ Rust_feed