Welcome! In our previous lesson, we explored the crucial role of the Hardware Abstraction Layer (HAL) in providing a safe and ergonomic interface to microcontroller peripherals. You learned how HALs use Rust's powerful type system to prevent common hardware-related bugs at compile time.
Today, we transition from theory to practice. Your goal is to apply that knowledge to write the specific Rust code needed to configure a General-Purpose Input/Output (GPIO) pin. By the end of this lesson, you will have configured the user-programmable LED on your development board as an output, ready to be controlled. This is the foundational step for any embedded application that interacts with the outside world.
A Quick Look at Our Target
Throughout this module, we'll be working with an STM32 Nucleo series development board. The specific model these lessons are based on is the Nucleo-F401RE, but the principles apply broadly across the family.

This is the STM32 Nucleo development board, our platform for learning embedded Rust. It features an ARM Cortex-M based STM32 microcontroller and includes a user-programmable LED and button that we will be using.
Step 1: Adding the HAL Crate to Your Project
Every family of microcontrollers requires its own specific HAL implementation. For the STM32F4 series, the community maintains the stm32f4xx-hal crate. We need to add this as a dependency to our blinky-app project.
The following resource provides a clear guide on the necessary additions to your Cargo.toml file.
This blog post walks through setting up a project for the same board we are using. We'll focus on the Cargo.toml configuration.
In the article, find the section "Step 1: Installing the environment". Scroll down to the first Cargo.toml code block. You will need to add the dependencies listed there to your own Cargo.toml file. Specifically, add the lines for the dependencies under the [dependencies] section of your blinky-app/Cargo.toml. Pay close attention to the stm32f4xx-hal line: the features = ["stm32f401"] part tells the crate to compile the specific code for the STM32F401 chip on your board.
After updating your Cargo.toml file, you can run cargo build in your terminal to ensure the new dependency is downloaded and compiles correctly.
Step 2: Writing the GPIO Configuration Code
Now that our project includes the HAL, we can use it in our src/main.rs file. Our goal is to get a handle to the specific pin connected to the user LED and configure it as an output. We'll do this in three main steps within our main function.
The following blog post provides an excellent walkthrough of this exact process. We will break it down piece by piece.
STM32F4 Embedded Rust at the HAL: GPIO Button ...
This resource details how to control the on-board LED and button for the Nucleo-F401RE board. We will focus on the setup code for the LED.
First, read the brief "Connections" section to confirm which pin the LED is connected to. Then, read through the "Peripheral Configuration Code" section, from step 1 through step 3. This section covers exactly the code we are about to write.
Let's implement these steps in our src/main.rs.
1. Take Ownership of Peripherals
As you read, the first step is to get exclusive access to the microcontroller's peripherals. This is done using a singleton pattern, ensuring that no other part of the code can simultaneously try to control the hardware.
Add the necessary use statements at the top of your main.rs and the line to take the peripherals inside your main function:
#![no_std]
#![no_main]
use panic_halt as _;
// Import the HAL prelude, PAC, and entry macro
use stm32f4xx_hal::{pac, prelude::*};
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
// 1. Get a handle to the device peripherals.
// This is the singleton pattern in action.
let dp = pac::Peripherals::take().unwrap();
loop {
// Our program will do nothing for now.
}
}
The pac::Peripherals::take() call returns an Option, which we .unwrap() to get the peripheral struct dp. This can only be called successfully once.
2. Access and Split the GPIO Port
The on-board user LED on the Nucleo-F401RE is connected to pin PA5, which means pin 5 of GPIO Port A. To get control over this individual pin, we first access the entire GPIOA port from our dp handle and then .split() it into individual pin objects.
// Inside main(), after taking the peripherals
// 2. Get a handle to the GPIOA port and split it into individual pins.
let gpioa = dp.GPIOA.split();
The .split() method consumes the GPIOA struct and returns a new struct where each pin (pa0, pa1, ..., pa15) is an individual object that we can now own and manipulate separately.
3. Configure the Pin as an Output
This is where the HAL's type-state programming, which we discussed in the last lesson, comes into play. By default, gpioa.pa5 is an object representing a floating input pin. We need to convert it into a push-pull output pin.
// Inside main(), after splitting the GPIOA port
// 3. Configure pin PA5 as a push-pull output.
// The `into_push_pull_output` method consumes the pin in its previous
// state and returns a new pin object configured as an output.
let mut led = gpioa.pa5.into_push_pull_output();
This single line is doing a lot:
gpioa.pa5: We access the pin object for PA5..into_push_pull_output(): We call a method that is only available on a pin that can be an output. This method consumes the input pin object.let mut led = ...: The method returns a new object representingPA5in anOutput<PushPull>state. This newledobject has methods likeset_high()andset_low(), which the original input pin object did not. The compiler will now allow us to call these output-specific methods onled.
This transformation is the heart of HAL-based safety. The video clip below demonstrates this concept of transforming a pin from one state to another.
Blinking an LED: Embedded Rust ecosystem explored
This video segment from "The Rusty Bits" shows the process of taking a pin and transforming it into a push-pull output using a HAL.
Watch the segment from pin configuration. Notice how the code first gets a handle to the port, then accesses a specific pin, and finally calls a method to turn it into an output. This is the exact pattern we just implemented.
Your Complete Configuration Code
Here is the complete main.rs file with all the configuration steps in place. Make sure your code matches this, then run cargo build one more time to verify that everything compiles.
#![no_std]
#![no_main]
use panic_halt as _;
use stm32f4xx_hal::{pac, prelude::*};
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
// 1. Get a handle to the device peripherals.
let dp = pac::Peripherals::take().unwrap();
// 2. Get a handle to the GPIOA port and split it.
let gpioa = dp.GPIOA.split();
// 3. Configure pin PA5 as a push-pull output.
let mut led = gpioa.pa5.into_push_pull_output();
loop {
// The LED is configured, but we are not doing anything with it yet.
}
}
If the build succeeds, you have successfully used an embedded Rust HAL to configure a hardware peripheral. You now have a variable, led, which is a type-safe handle to the physical LED on your board.
Conclusion
In this lesson, you took the theoretical concepts of a Hardware Abstraction Layer and applied them to write real hardware configuration code. You have successfully prepared a GPIO pin for output, which is a fundamental skill in embedded programming.
Here are the key takeaways from today's practical session:
- HAL Crate Dependency: The first step is to add the correct HAL for your target microcontroller (
stm32f4xx-halin our case) to yourCargo.toml. - The Configuration Pattern: You learned the common three-step pattern for GPIO configuration:
- Take: Get exclusive access to all peripherals using
pac::Peripherals::take(). - Split: Isolate the desired GPIO port and
.split()it to get ownership of individual pins. - Convert: Use an
into_..._output()method to transform the pin from its default input state into a new object representing an output state.
- Take: Get exclusive access to all peripherals using
- Type-State in Practice: You saw how Rust's type system is used to ensure a pin is in the correct output mode before you can even attempt to use output-related functions.
You now have a configured LED pin, represented by the led variable. But it's not doing anything yet. In our next lesson, we will complete the journey by writing the code inside the loop to toggle the LED, finally bringing your 'blinky' application to life.