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. Correction, all the initialization is done in MX_GPIO_Init() function.
If the initilization was not done by MX_GPIO_Init() function, we would need to add this line of code into main.c:
1 2 3 |
/* USER CODE BEGIN 1 */ __HAL_RCC_GPIOA_CLK_ENABLE(); /* USER CODE END 1 */ |
Above code will enable clock for port A.
We would also need to configure pin as output. We do this with next few commands:
1 2 3 4 |
/* USER CODE BEGIN 1 */ __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef Init_LED; /* USER CODE END 1 */ |
The code below is also not needed because of MX_GPIO_Init() function.
1 2 3 4 5 6 7 |
/* USER CODE BEGIN 2 */ Init_LED.Pin = LD2_Pin; Init_LED.Mode = GPIO_MODE_OUTPUT_PP; Init_LED.Pull = GPIO_NOPULL; Init_LED.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LD2_GPIO_Port, &Init_LED); /* USER CODE END 2 */ |
The unneeded code above is to explain a few things for beginners.
Next we need to switch on the LED.
1 2 3 4 |
/* USER CODE BEGIN 3 */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); } /* USER CODE END 3 */ |
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 two more lines of code:
1 2 3 4 5 |
/* USER CODE BEGIN 3 */ HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); HAL_Delay(1000); } /* USER CODE END 3 */ |
Recompile program again (hammer icon).
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.
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?
Thank you for the article. Some comments:
1. There is no need to configure port A and pin for LED. Cube is already generated MX_GPIO_Init function for this purpose.
2. You use different defines in the code: LD2_Pin vs GPIO_PIN_5, LD2_GPIO_Port vs GPIOA. Please use 1 style in all your code.
3. To blink the LED all we need is 2 lines of code:
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
HAL_Delay(100);
Thank you for the comment and added knowledge!
Can you share a SD Card read write project over SDIO communication for stm32f4xx with using HAL libraries.
Hi!
I am too new to STM32 to be able to do that, but you can check here.
The only line i’ve put in is
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
in the user begin 3 part. It tells me to start occurance in GPIOA and it won’t turn on the LED
I don’t understand what you mean that it tells you to start occurance. Did you call MX_GPIO_Init() function?
Slemi
In the last bit of code you add to the progect, there is a problem. As the code is written, it will not compile as HAL_GPIO_TogglePin only takes 2 arguments. If you delete the last argument (GPIO_PIN_RESET), the code will compile and run, but it will appear the LED is not on because you are turning it off right afteryou turn it on. Try adding a 500mS delay after setting the bit and then changing your original HAL_GPIO_TogglePin to HAL_GPIO_WritePin (with 3 arguments). Then change to the last delay to 500 mS and you have a nice blinky. Thank you for the step-by-step tutorial, it was very helpful as I have not worked much with STMCube.
You are right. I have corrected the program. Thank you for your comment!