Simple Blinky tutorial on Nucleo-F401RE using STM32CubeMX and SW4STM32

As mentioned in previous article in this one I will explain how to blink LED on Nucleo F401RE using STM32CubeMX and SW4STM32. We won’t go into details about each line of code. Purpose of this tutorial is just to get LED to blink.

Preparing project

Start CubeMX and click on New project.

This is window for selecting microcontroller/board we will be using. At the top select tab named Board Selector it is easier ths way and some setting that are specific to this board are already configured. As vendor select ST as this is only option. As Board select Nucleo64, because our board fits in this group. In the MCU Series select STM32F4 -> F401RE. Finally select NUCLEO-F401RE because our microcontroller is STM32F401RET6. and click OK.

Here is where the magic happens. The setting we need for our Blinky are already set. All we need is to tell the Cube whrere to store our program and which compiler we will be using.

In the menu click Project > Settings.

You need to enter Project Name, Project Location and set Toolchain / IDE to SW4STM32.

Project Name is just a name for your program (I use a practice for not using any space in names, I use underscore instead of space).

For Project Location I would suggest the place of your SW4STM32 workspace.

Select SW4STM32 as Toolchain/IDE.

The rest you can leave as it is and click Ok.

Now select in menu Project > Generate Code.

After project is generated a popup window will show. Click on Open Project and the project will be imported in SW4STM32.

You should get this popup window from SW4STM32. This means that you have properly installed CubeMX and SW4STM32.

Now click on Build Project (a little hammer on toolbar).

It might take a while but at the end you must see text like this in console. This means that program has been properly compiled without errors. Here is where we will start to program our blinky with our own code.

Now we need to open main.c and start adding our program. Double click on it and it will open in editor.

We need to do a couple of things before the LED will start blinking:

– configure PIN A.5 as output

– create a loop in the main section of main.c

– add delays in loop so that we can see that LED blinks

We can find the pin where LED is connected to in this pdf on page 66. Below is a screenshot of that connection.

Before we start adding code to any of the files in our project it is important that you only add code between /* USER CODE BEGIN */ and /* USER CODE END */ and that is because what is in between here it stays here when you regenerate project in CubeMX. Everything that is outside of these gets deleted when regenerating project in CubeMX.

Let’s configure pin as output. With Cortex-M microcontrollers this is a little more complicated than with let’s say AVRs or PICs. For now we will use a couple of commands to configure it, we won’t go in details what each of them does, because in this tutorial we only want LED to blink so that we know our compiler and hardware works. To do this we need to enable clock for port A.

We need to add this line of code into main.c:

Above code will enable clock for port A.

Next we need to configure pin as output. We do this with next few commands:

With these we configured PINA.5 as output.

Next we need to switch on the LED.

Now we can try if it works.

First we need to build our program (click on the hammer in toolbar). In console you should get text “Build Finished….“. This means that program compiled successfully.

After that right click on project in project explorer and select Target -> Program chip…

It will open this window:

Check the checkbox that says Reset after program, this resets the board after programming, otherwise you need to press reset (black button) on Nucleo to start program and click OK.

After a while the Green LED on your Nucleo should light up. 🙂

To blink the LED all we need now is time delay and command to switch it off.

We need to add three more lines of code:

Recompile progrm again (hammer).

Now right click on down arrow next to green circle with white play button inside on the toolbar and select Run As -> 1 Ac6 STM32 C/C++ Application. This will program your microcontroller with new program. After you did this, your program is added to the list of programs as you can see on picture below. And after that you can just select your program and you no longer need to get to Run as -> 1 Ac6… to program your newest compiled program into microcontroller.

LED should now blink with 1 second interval.

This is it for this tutorial. Next time we will be looking more detailed in configuring pins.

 

2 thoughts on “Simple Blinky tutorial on Nucleo-F401RE using STM32CubeMX and SW4STM32

  1. My machine asks me “How do you want to open this type of file
    (cproject?)

    What have I done wrong? Any ideas. A file called “.cproject” is generated but I can’t do anything with it.

    • .cproject files are generated by Eclipse, program that is underlying the SW4STM32. I am not sure what you are trying to do, that it asks you this?

Leave a Reply

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