STM32F4 Discovery project with CMSIS library;

Step by step configuration in Keil uVision

  In this step-by-step tutorial we will set new project for STM32F4 Discovery board and show, how to use ow to use CMSIS libraries, which are integrated in Keil uVision.

1. Open Keil uVision MDK v5.

First time Keil uVision

2. On top menu select Project -> Manage -> Pack Installer...

Keil uVision packs

3. Make sure these are installed:

  • ARM::CMSIS (Core CMSIS functions)
  • Keil::MDK-Middleware
  • Keil::STM32F4xx_DFP (Functions for STM32F4xx Series)

Install them manually, if they are not listed, you can download pack 1.0.8 for STM32F4 here.

4. Close Pack installer

5. Download necessary files for CMSIS & Drivers, ... from here (which is a file like this:)

Keil uVision packs

6. In Keil, select Project -> New uVision Project...

7. Save it to STM32F4xx_template, subfolder Project-001. Name your project; for example "GPIO", then click Save.

8. A new window should be opened, "Select Device For Target 'Target 1'...", select STM32F407VG (for STM32F4 Discovery board) and click OK.

Keil uVision packs

9. A new window “Manage Run-Time Environment” should be opened. Select options:

  • Under CMSIS select CORE,
  • Under Device select Startup

Keil uVision packs

Click OK, window will be closed.

10. Press in the toolbar to Configure file extensions. "Manage Project Items" window will be open.

Keil uVision packs

By the use of icons, manage your project items like this:

Keil uVision packs

11. In the Groups select STM32F4xx_StdPeriph (just like in the above image), then click on Add Files. Go to "...\STM32F4xx_template\STM32F4xx_StdPeriph_Driver\src" and add stm32f4xx_gpio.c and stm32f4xx_rcc.c.

Keil uVision packs

In the "Manage Project Items" click OK, window will be closed.

12. In the "Project" window click on "plus" on the left under "Device" group to see "system_stm32f4xx.c (startup)".

Keil uVision packs

13. Open "system_stm32f4xx.c (startup)" file and search for

252 
253
254
255
256
257
258
259
260
261
262
/************************* PLL Parameters *************************************/
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 25
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 7

#if defined (STM32F40_41xxx)
#define PLL_N 336
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
#endif /* STM32F40_41xxx */

Change line 254 to #define PLL_M 8

Cause of this change is that, STM32F4-Discovery has 8MHz crystal. We would like to run the core at 168MHz. In order to generate a core clock frequency of 168MHz, according to formulae in reference manual (which Mentioned in line 253 of "system_stm32f4xx.c (startup)"), we need PLL_M=8, PLL_N=336 and PLL_P=2.

Keil uVision packs

14. Press the magic wand in the toolbar to show the "Options for target" window and configure target options. Select the Target tab and:

Keil uVision packs Keil uVision packs Keil uVision packs Keil uVision packs Keil uVision packs

By the use of icons, setup compiler include paths:

Keil uVision packs

15. In the "Debug" tab:

Keil uVision packs

In "Cortex-M Target Driver Setup":

Keil uVision packs Keil uVision packs Keil uVision packs

16. In the Utilities tab:

Keil uVision packs

17. In uVision program, under Main Source folder on the left make right click and select "Add New Item to Group 'src'..". Then create  "main.c" and save it in the "./src" path.

Keil uVision packs

Note: If  we wanted to use an existing file (.c, .cpp, .h or etc.), we had to choose "Add Existing file to '?'...".

18. Type your main.c code. (Click here to see a main.c example)

19. Press F7 (or click on ) to compile and make sure there is no errors.

Rebuild target 'GPIO'
compiling main.c...
compiling stm32f4xx_rcc.c...
compiling stm32f4xx_gpio.c...
assembling startup_stm32f40_41xxx.s...
compiling system_stm32f4xx.c...
linking...
Program Size: Code=1036 RO-data=408 RW-data=0 ZI-data=1632 
".\out\GPIO.axf" - 0 Error(s), 0 Warning(s).

20. Click on to download code to flash memory.

Load ".\\out\\GPIO.axf" 
Erase Done..
Programming Done.
Verify OK.
Application running ...

THE END.

Rename file of this example to "LastName-StudentID-001", we use it as a template for our future projects and exercises.

Sharif University of Technology, Tehran, Iran