STM32CubeIDE Blinky Program for STM32F4 26

[otw_is sidebar=otw-sidebar-1]

We are going to demonstrate STM32F4 Discovery board based Blinky program in STM32CubeIDE. Blinky Program in STM32F4 using STM32CubeIDE is not only a hello world program in STM32 Embedded World, BUT also provides the proof that your STM32CubeIDE based development toolchain is active and ready to use. Which gives you confidence to work in more advanced projects in STM32 platform. We will not using uVision Keil due to its proprietary nature as well as the limitation of  code generation in lite version. We will walk you through the easy to follow steps to create a new project in STM32CubeIDE for the STM32F4 Discovery board and show you how to use HAL_GPIO drivers to toggle on Board LED. Before starting the tutorial you may want to ask one simple question.

Why Should you Choose STM32CubeIDE?


After acquiring Atollic TrueStudio, ST merged it with its own CubeMX Famous product for auto generating code. This comes in the end to the STM32CubeIDE, which is, Although, in its premature stage at current time as the latest version is 1.1.0, But it is already very powerful as it had eaten a very mature IDE Atollic TrueStudio, as well as world’s well known auto code generator CubeMX. So it is worth trying it right now before it becomes everyone’s need. Which is soon going to happen, because ST is supporting this and it is not only the official product but also the free one like before from ST. One more important feature of this product is its availability over all platforms, especially Linux and MacOS. So all you have to do is to go to the download section of STM32CubeIDE and select your target platform and follow the instructions.


STM32CubeIDE Blinky Program for STM32F4

Launching the CubeIDE

If you are using Windows, then there is no problem in using and launching your STM32CubeIDE because in windows you may easily find the launcher icon from your start menu or from the desktop shortcut created at the time of installation. But if you are using Linux Version and specially, like in my case, The Xfce Desktop environment, you may need to create a shortcut on desktop to easily access later if you are previously user of Windows and just shifted to Linux. The process to create the launcher shortcut is easy to create in Linux MX with Xfce desktop just right click on the desktop and you will see the option to create launcher shortcut. After clicking on it you will be provided with the option to choose your STM32CubeIDE file which comes in sh format. This file is simply a shell script file and could be run with sh command or simply by making executable of sh files in linux. Either way, once you are done, the shortcut will be created and once you will hit enter or left click on the launcher you will see a nice looking welcome screen of STM32CubeIDE like this



This could also lead to another launcher screen if you updated your IDE from some previous version. Just Ignore this and wait for it to properly launch. After launching it may ask you for your working directory to contain the active project into. This is Eclipse based IDE so it term this as your WORKSPACE. You just need to provide a valid Folder name or you may also leave it as default which will create a default workspace directory into your username folder under your home directory.


After this a nice looking welcome screen with the whole IDE and a in a center window a page of information center will be displayed. It is an indication that you are now ready to create your first project in STM32CubeIDE.


Create New project in STM32CubeIDE

To create a new project in STM32CubeIDE you have a number of options. Either way it will lead you to the same dialog screen to choose further options. As we assume that we are just launched our STM32CubeIDE for the first time, so the best way to create a new project in STM32CubeIDE is by clicking on the big blue button saying “Start New STM32 project” in Information Center page. But if you like to try other options, a normal way is by right clicking on project explorer window or simple from File menu. Remember that you need to choose STM32 Project in either option you choose.

[otw_is sidebar=otw-sidebar-2]



This way you will be able to use CubeMX for auto generating code for your target hardware, which in our case is STM32F4 Discovery board. Which you have to choose next from target selection window prompted to you. If you just need to select microcontroller you can type in or you may switch to “Target Board”  option which we are going to switch, and then locate our discovery board like following displayed in figure.



Once we are done selecting our target microcontroller or the target Hardware board we will be in our home screen ready to change our pin configurations from visual editor provided.


Note: if you are choosing some target for the first time it may take some time to download it’s related cubemx files from internet which sometimes could take some time. So don’t panic and let your working internet connection available at the time of this download. You may not need to download these files next time if you create a new project for the same target.


After everything finishes, the  ioc file will be displayed by default to change the visual configurations of the pin. This will be similar to STMCubeMX software if you had used it before. Here you may change the pin settings and could initialize some peripherals provided on GPIOs. Onces you done changing you need to save those changes and after saving it will ask your permission before creating auto generating code. You also may need to zoom in to better see the desired pins clearly.



Creating a Blinky Program

Now that everything is configured and your project is ready to accept your modifications, now it’s time to actually write your first hello world program in STM32F4 discovery board. Just zoom in your project’s ioc file to better see your green pins from PORTD. These pins will be automatically set to OUTPUT if you selected your target board as STM32F4 Discovery board. The names could be different like it is mostly LD1 LD2 … upto LD4, because by default STM32F4 has 4 onboard LEDs.  These LEDs are Red, Blue, Orange and Green in Color. So I mostly prefer to name them respectively. To change the default provided names to user defined names, all you have to do is to right click on the respective pin and choose user label option to change the label of that pin.

Once you are done with all the changing names for all the pins required just click save. When you will click on save it will ask your permission before generating the new code. Just allow and you are ready to change main file under core folder in your project structure.


I am just going to use Green LED. So by expanding the “Src” folder from “project explorer” window you will see a main.c file. Here you will see a lot of codes which you may have no idea at this stage. Even if the auto generated code doesn’t make sense for you, yet just bear with it for now and leave it as it is. The main thing that you may need to focus on for now is the initialization function like this.



Here you can see that, the structure of GPIO_InitStruct is created and provided with all pins defined for LEDs and their few parameters required to change to make them able to do some output operations in Push Pull mode. Although No Pull ups are used and the default speed is configured to be LOW, Yet this speed is enough for LEDs to do flash operation like we are just going to do right now.


Come to your main function and right into the while(1){} loop which is auto generated for you. Now just above the comments created for you to write user defined code in while loop enter these two function calls to toggle the Green LED of your board.




Upload Code: Debug Configuration

If you want to upload the code and debug it you can do debug the code in STM32CubeIDE. All you have to do is to click on Debug button from toolbar or go to “Run” Menu and Click on Debug, or you can choose to click keyboard shortcut “F11”. All will result to debug configuration window like this.


Choose STM32 Option as shown above and then click OK or hit Enter. Next you will be show to edit configurations. Remember to build your project before debugging. After a success full build, you will see the name of your project elf file with the same name, as the name of your project but with “.elf” extention.



At this stage, most of the time, all you have to do is to click OK. But if you wish to change your debugger settings from ST-Link to OpenOCD, you can do this by going to “Debugger” tab and change the respective option. After all the work of configuration done, a Debug is ready and don’t forget to attach your board with your computer before clicking the OK button or start debugging. Because after all it is going to be burned into the target board so it has to be connected before to attempt to burn into it.

Congratulations! Your Blinky program is ready.

[otw_is sidebar=otw-sidebar-3]

Some Possible Errors:

You may face error of DFU firmware update. If this is the case all you have to do is to follow the steps or options the IDE gives to you after prompting the Error. It will normaly takes you to the ST-Link Update window. You need to unplug the STM32F4Discovery board before updating and replug. It will take the board into DFU mode.


Or worse you may face following error if using in Linux like in my case


It took my hours to find out what’s wrong ? I tried different installation options even re-installaing GNU-ARM toolchain. But all in vain. As the tool chain and Cube Programmer comes built-in with STM32CubeIDE. Finally a simple command from this link do the magic


sudo apt install libncurses5

Leave a Reply

Your email address will not be published. Required fields are marked *