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. Simply, 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:

keil_view1

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.

sourceinsight

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:

 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
    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.
  • Locking mechanism (GPIOx_LCKR) 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 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:

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 🙂

Comments

comments

26 Thoughts on “STM32F0 Tutorial 2 (GPIO): Blinking LED with CubeMX, Keil ARM and Source Insight

  1. John on 12/02/2015 at 11:40 PM said:

    Very useful, thanks.

  2. Onia on 20/03/2015 at 3:52 AM said:

    I followed the turorial to the letter, flashing went well, no errors, but no blinking led.
    What could be wrong?

    • Le Tan Phuc on 20/03/2015 at 8:47 AM said:

      Hi Onia, did you press the Reset button (the black button) after flashing the program ?

      • Onia on 20/03/2015 at 2:39 PM said:

        Yes, I did. Makes no difference.

        When the board arrived there was already an blinking led program installed, so I know the led itself is ok.

        Do I need to set jumpers in a particular configuration?

        • Le Tan Phuc on 21/03/2015 at 5:21 PM said:

          Hi Onia,

          Can you zip the project files, upload and share it here so that I can help you. Nothing to do with the jumper as I made no change to the board.

  3. Onia on 22/03/2015 at 12:28 AM said:

    I redid everything for the third time, pressed the reset button, and voila, now it works!
    Thnx for the excellent turorial.

  4. Rahul Khatri on 05/10/2015 at 2:39 AM said:

    First of all i am very thankful to you for this type of demonstration .as i am new to programming .i want to understand one thing
    void HAL_GPIO_TogglePin (GPIO_TypeDef * GPIOx, uint16_t
    GPIO_Pin)

    How you have chaned in
    HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12) //i have used stm32f4 in PD12 has green led

  5. danial on 15/07/2016 at 8:03 PM said:

    hi
    thanks for sharing this . I followed the turorial and i did every thing step by step but when i complied the code i saw this error in console “..\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c(137): error: #1113: Inline assembler not permitted when generating Thumb code” please help for this . my MCU is STM32F103c8t6 . thanks again.

  6. danial on 19/07/2016 at 6:53 AM said:

    hi,ok so i think i should check keil, thanks.

    • Le Tan Phuc on 20/07/2016 at 9:20 PM said:

      Hi, you can try to move to Keil 5 and choose Keil 5 in the output configuration in CubeMX.

  7. Manh Linh on 06/09/2016 at 10:59 AM said:

    Chào bạn.
    Mình cũng thực hiện như ví dụ này, nhưng muốn debug bằng trình Simulation của Keil 4. Kết quả là không như mong đợi. Không biết bạn đã thử với Keil Simulation cho STM32F0 chưa nhỉ?

    • Le Tan Phuc on 06/09/2016 at 11:40 PM said:

      chào bạn, mình debug bằng keil5 bình thường, không bị gì cả

  8. Manh Linh on 07/09/2016 at 12:11 AM said:

    Cảm ơn vì đã trả lời.
    Mình không đag có bug ở đâu. Mặc dù mình đã tạo project trên stm32cubemx như hướng dẫn. Nhưng kết quả simulation là không đc. Nếu được, bạn có thể gửi mình code của ví dụ này trên Keil5, và một số hình ảnh simulation. Không biết lỗi có phải nằm ở bản keil5 mìh đag dùng không nữa.
    Thanks bạn

  9. Keyshav Mor on 16/11/2016 at 4:25 PM said:

    Hey Le Tan Phuc, great tutorials. I wanted to ask if the same APIs used in F0 would be applicable to F103 series or not? Can you please help me with learninf F1 series?

    • Le Tan Phuc on 16/11/2016 at 4:45 PM said:

      Hi, F1 line also has the same HAL library so yes, you can use it.

  10. Andriy on 01/12/2016 at 7:25 AM said:

    My the best wishes to author! It was the best STM32 tutotial I ever read! Waiting more tutorials 🙂

  11. Andriy on 10/12/2016 at 5:49 PM said:

    Hello!
    What do You think about – Does it possible make a definition of the funktion, like

    #define PIN_NAME_STATUS HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)

    To simplify the writing and the reading of the programm, for example

    PIN_NAME=PIN_NAME_STATUS;
    ?

  12. Andriy on 12/12/2016 at 2:43 AM said:

    Hello! I have tried #define macro by oureselves
    It works great. My example:

    1. In the #define section
    #define LED_GREEN_TOGGLE {HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9);}

    2. in the main loop
    LED_GREEN_TOGGLE

    Works great, and the code have more readable 🙂

  13. Hello, I have a a cheaper board with STM32F103C8T6 MCU (blue board with yellow headers), and more cheaper chinese STlink v2.0 (red board). I follow all steps, sure selecting my chip, and everything works fine.

    But I need mantain pressed reset button on board, click on Donload(keil uVision) and release reset button.

    How can upload program without make this?

Post Navigation