STM32F0 Tutorial 2 (GPIO): Blinking LED with CubeMX

STM32F0 Tutorial 2 (GPIO): Blinking LED with CubeMX

In the first tutorial, I have shown some software that you needed to install to play with STM32F0. This STM32F0 tutorial will straightforwardly show how to create a new blinking LED project for the STM32F0 Discovery kit with STM32CubeMX and then, generate the Keil ARM project and synchronize files with Source Insight. Through this specific example, we can learn how the GPIO function of the STM32F0 works compared to other 8bit microcontrollers such as AVR or PIC. This tutorial will cover:

  1. Create a new project using STM32CubeMX and export the project folder and generate Keil ARM project file with all necessary libraries.
  2. Set up a new Source Insight project, and synchronize files. Edit the code, compile and load the program into the STM32F0 Discovery kit to test.
  3. Learn some basic knowledge on STM32F0 GPIO peripheral after testing the code.

It will take you less than 30 minutes to make your F0 Discovery board’s LED blink when you follow this tutorial. So, let’s get started.

Create a new project with STM32CubeMX and Keil ARM

As I mentioned earlier, STM32CubeMX is a newly released software from ST that helps you create projects, and configure peripherals in the easiest way. This also comes up with newly developed HAL libraries supporting all types of STM32 from L0 to F4. Please take a look at this video and follow these steps to create your own project:

At this very first stage, we will not change anything inside Keil’s configuration. Simply, isn’t it 😀


Set up new Source Insight project, synchronize files

I don’t know what to say but this Source Insight software really helps me to get through all the code like a charm. When programming with Keil ARM or IAR or whatever, you will generally see this kind of code interface:

keil_view1

The color of the keywords, and variables… is quite boring and difficult to recognize. Moreover, when things become more complicated like thousands of lines of code or multiple libraries .c of .h files, you will wish you never had to read and understand it. But luckily, Source Insight may bring you out of the mess and that’s why some big companies including IBM, Microsoft, and Sony… choose to use it, a small but very useful tool. The idea is that you add all the files that will be used in your project, then the software will make a list index of all the functions, variables, and structures… in your program, shade it with different colors, and help you to find the location of the function and its content.

sourceinsight

So I think we have enough compliments for it, let’s go to the main point. The following video will show how to create a new project in Source Insight. All you need to do is follow step by step and practice yourself:

This is the code in the video, very simple:

/* USER CODE BEGIN 3 */
  /* Infinite loop */
  while (1)
  {
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9); //Toggle the state of pin PC9
    HAL_Delay(100); //delay 100ms
  }
/* USER CODE END 3 */

Until here, you should be able to make your STM32F0 Discovery board blink. Then we can go on to understand what happened with the code and how to write a new code on your own.

Test with STM32F0 GPIO

General-purpose Input/Outputs (GPIO) is a very common, basic peripheral of microcontroller, including whether it is an input or output pin, which can be controlled by the user at run time. If you are working with other types of 8bit microcontrollers such as AVR or PIC, you must be very familiar with this. Unlike the 8bit microcontroller which has a maximum of 8 pins in 1 port, the STM32F0 and the other 32bit have 16 pins in 1 port. For specific, the STM32F051 on this Discovery board has 5 GPIO ports named GPIOA, GPIOB, GPIOC, GPIOD, and GPIOF. Not all the ports have 16 pins, only GPIOA, GPIOB and GPIOC do. Let’s have a look at the STM32F0’s GPIO structure on page 158, F0 reference manual:

IO 3.3V page 125 - reference manual

There are 2 different driver blocks for one GPIO pin, one for input and another for output control. As you can see, there are a lot of features, and functions that one GPIO pin has, including:

  • Output states: push-pull or open-drain + pull-up/down.
  • Output data from output data register (GPIOx_ODR) or peripheral (alternate function
    output).
  • Speed selection for each I/O.
  • Input states floating, pull-up/down, analog.
  • Input data to input data register (GPIOx_IDR) or peripheral (alternate function input).
  • Bit set and reset register (GPIOx_ BSRR) for bitwise write access to GPIOx_ODR.
  • A locking mechanism (GPIOx_LCKR) was provided to freeze the port A or B I/O port
    configuration.
  • Analog function.
  • Alternate function selection registers for ports A and B (at most 16 AFs possible per
    I/O).
  • Fast toggle capable of changing every two clock cycles.
  • Highly flexible pin multiplexing allows the use of I/O pins as GPIOs or as one of several
    peripheral functions

So can you guess what function of the GPIO we have used in this LED blinking tutorial? Of course, it is the Output states with the push-pull signal. When they say push-pull, it means 0 when turning off and 1 when turning on. Another type of output signal is open-drain, which means 0 when turning it off and high-z (open, not connected) when turning it on. Some pins are 3.3V (mostly the pins with analog input) and some are 5V tolerant, you can check it in Table 12 and Table 13 of the STM32F051 datasheet, which can be downloaded from here

I found this article is really well explained on GPIO function of the STM32F0, so if you are interested in the GPIO register’s details, I recommend you to check here. I will focus on specific examples to get you more familiar with the STM32F0.

What should we do next?

Since we are playing with GPIO and the Discovery board has one button, why don’t we learn to use it to start and stop the blinking LED 😀 From this, you will know how to read the input signal from the GPIO pin for your future switch, button,…

According to the schematic of the STM32F0 Discovery kit, the USER Button is connected as follows:

button_sch

So, whenever we press the button, PA0 will be high and vice versa. I’ve made a clip showing how to configure and program to read the button states to control the LED, please have a look and follow these steps:

The code in the video:

if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))  
   {
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9);
    HAL_Delay(100);
   }
else  
   {
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_RESET);
   }

What we have covered in this tutorial?

  1. Learn how to set up a new project with STM32CubeMx and Keil ARM
  2. Synchronize project with Source Insight
  3. Program a simple blinking LED code – Know the GPIO output
  4. Program a simple button input code – Know the GPIO input

Hope it help! The next tutorial is about the External Interrupts, check it if you have already made everything work 🙂