In our last session, we accomplished a critical setup task: configuring a GPIO pin as an output using the Hardware Abstraction Layer (HAL). You now have a variable named led that represents the physical LED on your board, with Rust's type system guaranteeing it's in the correct state for output. However, it's currently sitting idle in an empty loop.
This lesson brings your work to life. We will complete the classic "blinky" program—the "Hello, World!" of the embedded world. You will learn how to toggle the LED's state and, crucially, how to introduce a timed delay to make the blinking visible to the human eye. By the end, you'll have a fully functional embedded application running on your hardware.
Toggling the LED State
Your led variable, which is of a type like Output<PushPull>, has methods to control the pin's voltage level. The embedded-hal crate, which stm32f4xx-hal depends on, defines a standard trait called OutputPin. While this trait provides set_high() and set_low(), many HALs, including stm32f4xx-hal, also provide a convenient toggle() method. This method simply flips the pin's current state.
These methods are fallible—they return a Result—because a hardware fault could theoretically prevent the state change. For our simple application, we can confidently .unwrap() or .ok() the result.
If we were to just add the toggle command into our loop, the code would look like this:
// Inside the main loop
loop {
led.toggle(); // For stm32f4xx-hal, this is often on an extension trait
// and might not return a Result. If it does, use .ok() or .unwrap().
}
However, if you ran this code, you wouldn't see a satisfying blink. The microcontroller's CPU can execute this loop millions of times per second. The LED would switch on and off so rapidly that it would either appear to be constantly on (but dim) or off, due to an effect called persistence of vision. We need to slow it down.
Introducing a Delay
To create a visible blink, we must pause the program's execution between toggles. We can't use standard library functions for this because we're in a #![no_std] environment. Instead, we rely on the microcontroller's own hardware, specifically its timers.
The ARM Cortex-M core, which is the heart of your STM32 microcontroller, includes a standard, simple timer called SysTick. We can use this to create a delay.
To create an accurate delay, the delay mechanism needs to know how fast the system clock is running. Therefore, before we can create a delay provider, we must configure the microcontroller's clock system. This is a standard piece of boilerplate code you'll find at the beginning of most embedded Rust projects.
1. Configure the Clocks
First, we access the Reset and Clock Control (RCC) peripheral and use its constrain method. Then, we can configure our desired clock speeds and freeze them. This freeze operation locks in the clock configuration and returns a Clocks struct containing the frequencies of the various system clocks.
2. Create a Delay Provider
With the clocks object, we can now instantiate a delay provider. We'll also need to get a handle to the core peripherals (cp), which include the SysTick timer (cp.SYST). The stm32f4xx-hal provides a Delay struct that we can create from the SysTick timer and our clocks configuration.
The following video provides a fantastic walkthrough of this entire process, from setting up the pin to creating a timer and writing the final loop. While it uses a different board (Raspberry Pi Pico 2), the concepts are universal in embedded Rust: obtaining peripheral handles, configuring them, and using them in a loop.
Intro to Embedded Rust - Part 2: Blink and LED | DigiKey
This segment from a DigiKey tutorial demonstrates creating a full blinky application. Pay close attention to how it instantiates and uses a timer to create a delay within the main loop.
Watch the segment from pin to loop. It covers: Getting a handle to the specific GPIO pin. Creating a timer for delays, which requires passing in the clock configuration. Writing the final loop that uses set_high(), delay_ms(), set_low(), and another delay_ms(). This is the exact logic we are implementing.
The Complete 'Blinky' Application
Now, let's integrate the clock setup and delay logic into the code from our previous lesson. The final main.rs file will look like this.
#![no_std]
#![no_main]
use panic_halt as _;
// Import the necessary peripherals and traits
use cortex_m_rt::entry;
use stm32f4xx_hal::{
pac,
prelude::*,
delay::Delay,
};
#[entry]
fn main() -> ! {
// 1. Get handles to all the peripherals
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
// 2. Configure the clock system
// Constrain the RCC peripheral and freeze the clock configuration
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze();
// 3. Create a delay provider using the SysTick timer
let mut delay = Delay::new(cp.SYST, &clocks);
// 4. Configure the GPIO pin for the LED (from the previous lesson)
let gpioa = dp.GPIOA.split();
let mut led = gpioa.pa5.into_push_pull_output();
// 5. The main application loop
loop {
// Toggle the LED on
led.toggle();
// Wait for 500 milliseconds
delay.delay_ms(500_u32);
// Toggle the LED off
led.toggle();
// Wait for another 500 milliseconds
delay.delay_ms(500_u32);
}
}
This code first performs the necessary setup for clocks and the delay provider. Then, within the infinite loop, it toggles the LED and pauses for 500 milliseconds, creating a steady 1 Hz blink.
The image below shows a debugging session for a very similar 'blinky' program. You can see the code on the left and the debugger state on the right. The execution is paused on a delay_ms line—exactly the point where our program will spend most of its time.

Conclusion
Congratulations! You have now written a complete and functional embedded Rust application. By combining the HAL-based GPIO configuration with a hardware-backed delay, you've bridged the gap between high-level, safe Rust code and the physical behavior of your microcontroller.
Let's summarize the key steps to complete the 'blinky' program:
- Clock Configuration: Before creating an accurate delay, you must configure the system clocks and get a
clocksobject. This is a standard first step in most embedded applications. - Delay Provider: The Cortex-M core's SysTick timer is a convenient source for creating a delay provider, allowing you to pause execution for a specified duration (e.g.,
delay_ms()). - The Loop: The heart of a simple embedded application is an infinite
loop. Inside this loop, you perform your main logic—in this case, toggling the LED and delaying. - Toggling: The
toggle()method provides a concise way to flip the state of an output pin, perfect for blinking an LED.
You have now achieved the foundational milestone in your embedded Rust journey. You've set up a project from scratch, understood the layers of abstraction, and made hardware respond to your code.
In our next module, we will switch gears as you requested, to explore how some of these low-level programming concepts are handled in C and C++. We'll start by diving into pointers, the core mechanism for memory manipulation in C, which will give you a new perspective on the memory-safe abstractions you've been using in Rust.