Hello STM32 World - or Blinky

Let's update the hello_stm32_world project from the previous chapter and build a "blinky" project. We'll start of with the blinky example from the embassy crate. This example will toggle the LED on the STM32F103C8T6 board every 300ms.

main.rs

#![no_std]
#![no_main]

use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_stm32::init(Default::default());
    info!("Starting Blinky...");

    let mut led = Output::new(p.PC13, Level::High, Speed::Low);

    loop {
        info!("high");
        led.set_high();
        Timer::after_millis(300).await;

        info!("low");
        led.set_low();
        Timer::after_millis(300).await;
    }
}

Run the project with:

$ cargo run --release

And you should see the LED on the STM32F103C8T6 board blink every 300ms.

If that works build a breadboard circuit with a LED and a 150Ω resistor and connect it to pin PB5 on the STM32F103C8T6 board.

Schematic:

Hello World schematic


Update the main.rs file to look like this:

main.rs

#![no_std]
#![no_main]

use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_stm32::init(Default::default());
    info!("Starting Blinky...");

    let mut led = Output::new(p.PB5, Level::High, Speed::Low);

    loop {
        info!("high");
        led.set_high();
        Timer::after_millis(500).await;

        info!("low");
        led.set_low();
        Timer::after_millis(500).await;
    }
}

Run

$ cargo run --release

If everything is wired up correctly, you should see the LED blink every 500ms.

Summary

In this chapter, we built a "blinky" project using the embassy crate. We used the Output struct to control the LED on the STM32F103C8T6 board. We also used the Timer struct to create delays between turning the LED on and off.