Blink the LEDs of STM32F3-DISCOVERY Board

Reading Time: 5 minutes

So here we are back again. I hope you are done with the Debugging session from the last blog. If you haven’t then you can debug your code by taking help from this blog. Okay, let’s move ahead now. In this blog, we are going to Blink the LEDs of the stm32f3-discovery board. For that you need.

  1. hardware stm32f3 discovery board.
  2. some crates(Don’t worry I’ll provide the link for these crates).

Let’s Get Start

To Blink stm32f3-discovery led what you have to do first. You need to know about the device peripheral first and the crates we are going to use.

So, include these dependencies in your new Rust project.

  • cortex-m (This crate is going to provide the delay between your blinking led).
  • cortex-m-rt(This crate is going to provide the [entry] attribute to set a starting point in your program).
  • panic-hal(crate panic_hal crate will provide you panic handler to fight against panic errors).
  • stm32f30x-hal(The most important crate which will provide you access to the hardware peripheral(led, registers, etc)).

Okay, now after adding all these dependencies your Cargo.toml will look like this.

Now, let’s move towards the main program.

Step 1:

  • You need to create your program with no_std and no_main why? Check out this for the answer.
  • So, add both these attributes to your program.
  • Now add cortex_m_rt::entry crate and put [entry] attribute as the starting point of the program after no_main.
  • Now we are going to provide the infinite loop to the code as we have used fn main() -> ! which means our program will neither end nor it will return anything.
  • In this infinite loop, we are going to blink the led and delay those blink.

Your src-main will look like this.

Step 2->

Okay now we have started our program and now we are going to control or access the device peripherals of our hardware. For that, you need to include.

use stm32f30x_hal::{stm32f30x,prelude::*};

where stm32f30x will provide you the stm32f3 series hardware access and prelude will provide you the access to constrain and split method you are going to use soon.

Next, is what?

Next, we are going to set up the device peripheral using a statement.

let device_peripheral = stm32f30x::Peripherals::take().unwrap();

what this statement means, here the variable device_peripheral is taking the stm32f3 hardware peripheral and unwrapping its features to use.

After setting up this we need to access the Rcc(Reset clock control). This we can do by using this statement.

let mut reset_clock_control = device_peripheral.RCC.constrain();
  • RCC stands for Reset and Clock Control. Rcc controls not only the clock configuration but it’s responsible for enabling, resetting, and disabling other peripherals
  • .contrain() is providing the features like ahb, cfrg, etc. constrain method makes our Rcc specific to our peripheral.

Okay, let’s move further now to the last step of setting the peripheral. Now we have to access the gpio (general purpose input/output) to access the led and use them as input or output.

GPIO is the path to the LEDs and the different registers present inside the hardware.

So we need to access the gpioe – ‘e’ port. Port E contains the LEDs of this hardware.
We have to use this statement.

let mut gpioe = device_peripheral.GPIOE.split(&mut reset_clock_control.ahb);
  • where .split is splitting the pins and the registers to make them independent so that you can access them as per your need.
  • we need to provide the &mut rcc.ahb inside the split method where ahb is the Bus architecture for the micro-controller. It is a part of the Advanced Microprocessor Bus Architecture (AMBA) used in the fast execution of instructions.

So we are done with the setting of peripheral to access the components of the hardware. This is how it looks now.

Step-3 ->

Now we are going to point to the address of the particular pin and store them in the variable. So that we can access that led present at that pin’s address. Use this statement to do the following task.

let mut led_3 = gpioe
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);

Now, what this statement says?

  • We are pointing to the pe9 ie: Port E 9th pin through gpioe path and using mode push-pull output for this particular pin.
  • gpioe.moder and gpioe.otyper are two registers of gpio where moder provides mode and otyper provides output type to the gpio pins.
  • Port Mode Register (MODER) – configure the I/O direction mode (input/output/alternate/analog).
  • Output Type Register (TYPER) – configure the output type of the I/O port (push-pull/open-drain).

Therefore like this, you can configure all the 8 pins. You can know about pin no. from here. Page :18 Section-6.4

Okay now, this is how your code is looking till here.

Step-4 ->

It’s time to add the third crate to our program that is cortex_m::asm::delay. So for this use command.

use cortex_m::asm::delay; 

As you can notice we are inside the infinite loop, now let’s code to blink the led which we have already accessed using gpio. Put statement.


Let’s put the next command to delay the led for few cycles. For that use:


This will delay your next command for given cycles. Now to Blink stm32f3-discovery, we need to make the led off. So for that put command.


And again delay the blink of next led by using.


Now what set_high() and set_low() are doing. Don’t worry they are just like on and off for an led.

My code inside the loop looks something like this. Note* I have added one more Led.

The whole code looks like this.

This is not over yet. We don’t want this blog to expand too much so I have divided this into two parts. In the next part, I am going to build this program.

Will this much code work to Blink stm32f3-discovery or we are going to face any more difficulties in blinking an LED? Is this error-free?

We will find the answers to these questions, till then wait for the next to come. Hope you have liked this part. You can read more about stm32-discovery board.

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter:

Written by 

Nitin is a Software Consultant, with experience of more than 1.4 years. He works on Rust Programming Language and Embedded Development using Rust. He is also fond of Java Programming & Artificial Intelligence. Apart from that, his hobbies are Watching Netflix, Reading, Singing & Writing.

2 thoughts on “Blink the LEDs of STM32F3-DISCOVERY Board6 min read

Comments are closed.