Getting started with STM32 development using STM32CubeIDE

Introduction

This guide walks through the process of getting started with STM32 development. We will be using STM32CubeIDE which is an Integrated Development Environment provided by STMicroelectronics. STM32CubeIDE is based on Eclipse IDE and provides environment for writing, build code, manage project files, debugging and flashing program on STM32 target. In this article, you will learn:

  • Download and install STM32CubeIDE
  • Create a new project for a target STM32 chip
  • Build a project and create binary files for uploading to target
  • Flash a program on to a STM32 chip
  • Debug an application using STM32CubeIDE
  • How to build and run your first blinky application on STM32

Prerequisites

Hardware

In this tutorial, we will be using a development board based on STM32F103VB6 chip. STM32F103VB6 has an ARM Cortex M3 chip and has the following resources:

  • 128 kB flash memory and 20 kB SRAM memory
  • 80 I/O ports
  • 2 12-bit ADC
  • 7 DMA channel
  • 7 timers, 2 I2C interfaces, 3 USARTs, 2 SPIs, USB 2.0 interface
  • Other resources (see the chip’s datasheet for details)

You can use any other STM32 chip and use this guide as a reference. You may need to adapt certain steps in order to make it work on your board. Some examples of STM32 development kit includes

Affiliate Disclosure: When you click on links in this section and make a purchase, this may result in this site earning a commission at no extra cost to you

Software

You need to download STM32CubeIDE from STMicroelectronics website. It will ask you to provide your email address and send a link to your email. You click the link and download the latest software version, then follow instructions to install it on your machine (either Windows, Mac or Linux).

Using STM32CubeIDE

Create a new project

First time opening STM32CubeIDE, it will show initial screen which has links to guides and other resources which help you get started. They are useful as you can use them as a reference when needed. They can be accessed by clicking Help > Information Center.

Now, click File > New > STM32 Project. It will download more information from STMicroelectronics’ server and show Target selection dialog where you can select STM32 target. You can see there are 4 tabs: MCU/MPU Selector, Board Selector, Example Selector and Cross Selector. We will start with MCU/MPU Selector where you can choose your target STM32 chip. There is a filter which you can enter part number directly, or you can filter by selecting Core, Series, Line, Package or Peripheral. Type in STM32F103VB in the Part filter. Once you type in the part number, you will see on the right hand side showing 3 items. Select the STM32F103VBTx with LQFP100 package which is the chip we are using.

It is very handy to have all the information about the chip loaded. The STM32CubeIDE retrieves chip information from ST server and show chip feature, block diagram, docs and resources and datasheet all in one place. Compared with other IDEs, STM32CubeIDE is really great in terms of documentations. You can download the documents that you are interested in and use them as reference when needed. After that, click Next button.

The following dialog asks you to enter a name for the project, which we will name get_started and select target language, which we will use C. Then Click Next and Finish. The STM32CubeIDE will download necessary files and create a project for us. Wait a little while until the process finishes.

Understanding project structure

On the left hand side of the STM32CubeIDE is the Project Explorer where your project files are organised.

get_started
|---Includes
|---Core
    |---Src
        |---main.c
|---Drivers
|---Debug
|---get_started.ioc
|---STM32F103VBTX_FLASH.ld

It is worth to mention a few important files in the project. Gradually, we will learnt all of them, but we will look at a few of them in this getting started guide.

  • get_started.ioc: If you double-click the file, it will open device configuration tool where you can configure the STM32 chip. There are several tabs:
    • Pinout & Configuration: In this tab, you can see a diagram of the chip with all of its pin. You can, for example, configure a pin as an output port, and configure another pin as an input port. The diagram is very handy as you don’t need to remember pin location. Here you can configure various system core parameters and peripherals.
    • Clock configuration: There’s a diagram for configuring system clock.
    • Project Manager: This tab stores project-related settings and rules for automatically generating code based on selected configuration.
    • If you make change in Pinout & Configuration tab, for example, you set a pin as an output port, and Save it, the IDE will ask you whether you want to generate code automatically. Click Yes and code will be generated in appropriate place in main.c for you.
  • STM32F103VBTX_FLASH.ld: This is the linker script which specifies where your code reside in physical memory. You can see the addresses of RAM and FLASH, as well as their respective sizes. To learn more about the meaning of this linker script file, check out the STM32CubeIDE User Guide. You can find this document in Help > Information Center > STM32CubeIDE User Guide.
  • main.c: This is where your main application goes. If you take a close look at the file, you will see that there are sections of code that are automatically generated for you based on your configuration. There are other sections that you can place your code, as indicated by comment lines. You should put your code in these sections and try to avoid messing up with auto-generated code.

Adding user code

We will be implementing a simple blinky project in which we toggle an LED that is connected to a GPIO pin every second. We will use GPIO D10 for this purpose. Connect a LED in series with a resistor. One end of the LED is connected to GPIO D10, one end of the resistor goes to ground.

As described previously, we first need to open get_started.ioc. Click on PD10 in the chip diagram and select GPIO_Output. Click Save and hit Yes to enable generating code automatically. Then open main.c and paste the following code in the while() loop:

/* Infinite loop */
while (1)
{
    HAL_Delay(1000);
    HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_10);
}

This snippet does a simple thing: toggle the GPIO D10 every 1 second which makes the LED on and off repeatedly with frequency of 1 Hz. Save main.c. In the next section, we will learn how to build this project and flash it on the target hardware.

Build project

To compile the project and generate binary files, click Project > Build Project from the menu. You should see the log messages printed on the console. By inspecting the log messages, you can infer how STM32CubeIDE works internally, which tools it invokes, and the result of the build process.

Flash and run the program on the target STM32

To flash and run the program, you’ll need to create a Run configuration from Run > Run configurations... where you need to specify the programmer or debug probe to use. You can use a SEGGER J-LINK, or ST-LINK probe. Depending on the programmer that you have, you might need to install additional tools to support it. After selecting the debug probe, click Run button. It will load the firmware to target chip and reset the chip. If flashing is successful, you will see the LED blinks every 1 second as expected.

Debug the program

To debug the program, you need to create a Debug configuration from Run > Debug configurations... which is similear to run configuration above, then click the Debug button. The STM32CubeIDE will open Debug Perspective and stop code execution at the first line inside main() function. From there, you can set a break point, resume or stop code execution, step in, step over, step out and find out what is going on with each line of program.

Wrapping Up

In this tutorial, you have learnt how to get started with STM32 development using STM32CubeIDE. There’s a bit of learning curve, however, it is reasonably straight-forward. In my opinion, STM32CubeIDE is an excellent integrated development environment. It provides all the necessary tools and documentations that you need to quickly deploy your first application. This article serves as the reference when we are working with more complex STM32 projects. Thanks for reading.

Leave a Comment