Skip to main content

LED Blink with STM32Cube Framework

Let's blink an LED on the Blackpill STM32 board using the STM32Cube framework.

STM32Cube Framework

With STM32Cube framework, you can access the full power of the STM32 microcontroller. The STM32Cube framework provides low-level access to the STM32 hardware, allowing you to write code that is more efficient and optimized for performance.

Prerequisites

In this tutorial, the following hardware is used:

Blackpill STM32 board

MCU: STM32F411CEU6
Board: WeAct Black Pill V2.0
Schematic

You can purchase a cheap clone Blackpill STM32 board under $4. You can use other STM32 boards, but you need to modify platformio.ini file and the LED pin number.

You need this to upload the firmware to the board. You can purchase a cheap clone ST-LINK V2 under $5.

Installation

VScode with PlatformIO

You can install PlatformIO from the VSCode extension marketplace.

STM32CubeProgrammer

You need this to flash the firmware to the board.

STM32CubeMX

This tool allows you to configure the STM32 microcontroller and generate the initialization code for your project.

Clone the repository

git clone --branch blackpill-led-blink-stm32cube https://github.com/yuichiroaoki/stm32-tutorial.git
cd stm32-tutorial

Quickstart

  1. Connect the Blackpill STM32 board to the ST-LINK V2 programmer.

    ST-LINK V2 Clone
    Connect the ST-LINK V2 programmer to the Blackpill STM32 board as shown in the image above.

  2. Connect the ST-LINK V2 programmer to your computer.

  3. Open the project folder in VSCode.

  4. Click on the PlatformIO tab on the left sidebar and click on the Upload button to upload the firmware to the board.

    PlatformIO tab

Now you should see the LED on the Blackpill STM32 board blinking.

Initial code generation with STM32CubeMX

Using STM32CubeMX, you can configure the STM32 microcontroller and generate the initialization code for your project such as GPIO configuration, clock configuration, etc.

  1. Open STM32CubeMX and create a new project.

  2. Select the STM32F411CE microcontroller.

  3. Click SYS in the sidebar and select Serial Wire in the Debug drop-down menu.

    Serial Wire

    info

    This is important because without this setting, you won't be able to upload the firmware to the board.

  4. Click the LED pin in the Pinout & Configuration tab and select GPIO_Output in the Mode drop-down menu.

    For the Blackpill STM32 board, the LED pin is PC13. You should change the pin according to your board.

    GPIO Configuration

  5. Right-click on the LED pin and select Enter User Label and name the pin LED.

    tip

    This is useful because you can refer to the pin by name in your code.

    User Label

  6. Click RCC in the sidebar and select Crystal/Ceramic Resonator in the High Speed Clock (HSE) drop-down menu.

    RCC

  7. Click Clock Configuration tab and configure the clock settings as follows:

    1. Input 25MHz in the HSE Value field
    2. Select HSE in PLL Source Mux and PLLCLK in System Clock Mux
    3. Input 100MHz in the HCLK field and press Enter to calculate the values for the other fields.
    note

    The Blackpill STM32 board has a 25MHz crystal oscillator, so you should input 25MHz in the HSE Value field.

    Clock Configuration

  8. Click Project Manager tab, set Toolchain Folder Location to where you want to save the project, and generate the code.

Now you have the initialization code for your project.

Use the generated code in PlatformIO

  1. Copy the generated code to the src folder in the PlatformIO project folder.

    If you use Linux or macOS, you can use the following command to copy the code:

    cp -r <STM32CubeMX project folder>/Core/**/* <PlatformIO project folder>/src/
  2. Add the following two lines to the main.c file at the end of the while loop to blink the LED:

    main.c
    /* USER CODE BEGIN 3 */
    HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    HAL_Delay(1000);
    note

    If you named the LED pin LED in STM32CubeMX, the LED_GPIO_Port and LED_Pin will be defined in the gpio.h file, so you can use them in your code.

  3. Add the the following line to platformio.ini file to use the configuration header file generated by STM32CubeMX:

    platformio.ini
    board_build.stm32cube.custom_config_header = yes
  4. Click on the PlatformIO tab on the left sidebar and click on the Upload button to upload the firmware to the board.

    PlatformIO tab

Now you should see the LED on the Blackpill STM32 board blinking.

References