Q: How to create a blinky baremetal application on STM32F407G-DISC1 using STM32CubeMX and Keil μVision
Answer: Blinking an LED is the equivalent to creating a "Hello World" application to verify the IDE/tool/compiler are installed correctly on general purpose computers. We will be using STM32CubeMX and Keil μVision targetting STM32F407G-DISC for this exercise.
Pre-requisite:
- Hardware: STM32F407G-DISC
- Part#: STM32F407G-DISC1
- Vendor: STMicroelectronics
- Board name: STM32F407G-DISC1
- Description: DISCOVERY STM32F407/STM32F417
- CPU: ARM® Cortex®-M4-based MCU with FPU
- Frequency: 168 MHz
- SRAM: 192KB
- Flash: 1MB
- Keil μVision already installed
- STM32CubeMX installed
- STSW-LINK009 USB Driver Installed
Step 0: On corporate environment with SSL inspection in place, add the corporate root CA certificate
PS C:\> C:\Users\u1\AppData\Local\Programs\STM32CubeMX\jre\bin\keytool.exe -trustcacerts -cacerts -importcert -file C:\tmp\CorpRCA.cer -alias corp-v1 -storepass changeit
- Change u1 to the correct user and/or path
- Chnage location to corporate certificate - CorpRCA.cer
Step 1: Open Keil uVision Pack Installer
Step 2: Install STM32F407G-DISC1 board support package (BSP)
Installing BSP will bring the License Agrement for Board Support Package (BSP), Device Family Pack (DFP) and Arm's Common Microcontroller Software Interface Standard (CMSIS) driver for STM32, hit on Next button everytime it shows up. Close Pack Installer once installation is complete.
Step 3: Create new Keil uVision project
Search for STM32F407, and then select STM32F407GTx in the treeview and hit on OK. In Manage Run-Time Environment window, expand on Device | CubeMX. Then select the checkbox and hit on Resolve. Hit on OK to bring another window. Do click on Start CubeMX. This should bring up STM32CubeMX - this can take awhile to load for the first time.
Step 4: Configure GPIO in STM32CubeMX
In System Core, click on drop down arrow and select GPIO. Zoom into Pinout view, select pin PD12 and then GPIO Output as pin type. From the previous step, change User Label to LD4 [Green Led]. The options will be explained in future blogs. Save the project Click on Generate Code Click on Yes for the missing firmware package You will need to login to ST to download the firmware. Below shows I am logging in to ST. You may have to create an account as needed. Accept License Agreement and hit on Finish button. The download and extraction may take awhile to complete. Close the Code Generation dialog Then exit the STM32CubeMX application.
Observe the code generated by STM32CubeMX as shown in Keil uVision Now add code to blink LED4 See Github gist code below for the complete main.c
Step 5: Set Debug options
Do Project | Options for Target * Select Debug tab, see @(1). Then ensure ST-Link Debugger is selected, see @(2). Then click on Settings, see @(3). In Debug Adapater pane, ensure Unit is set to ST-LINK/V2-1. Then hit on OK, see @(2) Finally, Build and Download to your target.











































