STM32F0 GPIO: Blinking LED with CubeMX, Keil ARM and Source Insight - Tutorial 2

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

  1. Create new project using STM32CubeMX and export project folder and generate Keil ARM project file with all necessary libraries.
  2. Set up new Source Insight project, 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 blinking when you follow this tutorial. So, let’s get started.

1. Create new project with STM32CubeMX and Keil ARM:

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

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

2. Set up new Source Insight project, synchronize files:

I don’t know how 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:


The color of the keywords, variables… is quite boring and difficult to recognize. Moreover, when things become more complicated like thousands lines of code or multiple libraries .c of .h files, you will wish you never have 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, 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, structures… in your program, shade it with different colors, and help you to find the location of the function and it’s content.


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

This is the code in the video, very simple:

  /* Infinite loop */
  while (1)
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9); //Toggle the state of pin PC9
    HAL_Delay(100); //delay 100ms

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

3. Learn about STM32F0 GPIO:

General-purpose Input/Outputs (GPIO) is a very common, basic peripheral of microcontroller, including whether it is an input or output pin, can be controlled by the user at run time. If you are working with other types of 8bit microcontroller such as AVR or PIC, you must be very familiar with this.  Unlike 8bit microcontroller which has maximum 8 pins in 1 port, the STM32F0 and other 32bit have 16 pins in 1 port. For specific, the STM32F051 on this Discovery board has 5 GPIO ports name GPIOA, GPIOB, GPIOC, GPIOD and GPIOF. Not all the ports have 16 pins, only GPIOA, GPIOB and GPIOC do. Let’s have a look on 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, function 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
  • 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.
  • Locking mechanism (GPIOx_LCKR) provided to freeze the port A or B I/O port
  • Analog function.
  • Alternate function selection registers for ports A and B (at most 16 AFs possible per
  • 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 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 off and high-z (open, not connected) when turn it on. Some pins are 3.3V (mostly the pins with analog input) and some are 5V tolerant, you can check it on 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 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 GPIO pin for your future switch, button,…

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

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);

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 ! Next tutorial is about the External Interrupts, check it if you have already made everything work :)