STM32F4/29 Discovery with CMSIS library in Keil uVision

From this moment, I will use in my projects Keil uVision. It’s true, that Coocox is free, but it has some bugs, which are great fixed in Keil. Keil has some limitations for free (32KB flash support, no optimization) version, but for our examples and libraries, it is just fine. For updates, look at my Github projects repository, where are all projects and libraries. There is no included my libraries in bottom project, you have to download it manually from Github. All of my examples should work in Keil.

November 03, 2014 Update

I got some emails about this messing in Keil right now. For that purpose I’ve made a 2 new project templates. It’s features are almost independent of Keil version.

  • Supports various STM32F4 boards
    • STM32F4-Discovery
    • STM32F429-Discovery
    • STM32F401-Discovery
    • STM32F411-Discovery
    • Nucleo-F401RE
    • Nucleo-F411RE
    • STM32F446xx any device
    • For Nucleo boards, you can now choose (With target inside Keil uVision) if you will use with internal RC 16MHz oscillator or with external 8MHz crystal connected.
      • If you have connected crystal to Nucleo board, then don’t use “internal” option when you select target

        Keil uVision available targets in project

        Keil uVision available targets in project

  • All PLL settings are now set from “Options for target” window under “Defines” in “C/C++” tab
  • Standard peripheral drivers are included, no need to use the one provided in Keil uVision
    • They are basically dead for Keil uVision
      • But still perfect for the low level programmers and will be always available.
    • New HAL drivers are now supported with Keil, which don’t work with my libraries
  • Project template is split from standard peripheral drivers
    • This allows you to use the same drivers for all projects
    • When new update occurs, you just update everything from one place.
  • You need now installed STM32F4xx pack from Keil with at least version 2.2.0
    • This is because of STM32F411 device support for Nucleo-F411
  • When you need to add modules to your project, just hit right click on the left at “STD_Periph_Drivers” group, click “Add Exiting files to group STD_Periph_Drivers” and find your .c files in a included folder “\00-STM32F4xx_STANDARD_PERIPHERAL_DRIVERS\STM32F4xx_StdPeriph_Driver\src
  • I will (I hope) convert all my project to the new version, so they will be compiled without problems with new Keil uVision packs installed, but without HAL drivers.

Download project templates below:

  • Keil project with included STD Peripheral drivers 1.5.0 for STM32F4xx devices
  • Keil project with included STD Peripheral drivers 1.5.0 for STM32F4xx devices and USB configuration
    • STM32F429 and STM32F446 clock is set down to 168MHz
    • STM32F411 clock is set down to 96MHz
      • For Nucleo-F411
      • For STM32F411-Discovery
  • Both projects will toggle all leds on board every 500ms by default

November 04, 2014: All projects on Github are updated with project template like these two above, with STD peripheral drivers and new Keil STM32F4xx packs version 2.2.0 or newer.

Project folder structure

  • Targets
    • 5 folders for different targets selected
    • inside each folder are output files, including .hex file
  • TM
    • Folder, where you place my libraries for your project
  • User
    • User files, as main.c, defines.h, etc

Make your say about that.

October 31, 2014 Update

Keil has dropped support for standard peripheral library for STM32F4 in new MDK-ARM 5.12 version. New packs that are supported are STM32CubeF4 “HAL” drivers, which are totally incompatible with STD libraries.

So when you install packs for STM32F4, don’t install it with Keil, but go to their website and manually download version 1.0.8 for STM23F4.

Download pack 1.0.8 for STM32F4 from Keil packs here.

Step by step configuration in Keil uVision

Tutorial below has sense only if you are working with Keil uVision version between 5.0 and 5.11. Newest versions does not support STD peripheral drivers. You need to add them manually.

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

  1. Download Keil uVision from here. You will need to pass some information to their “registration” form, but you will be able to download Keil uVision MDK v5.
  2. Install as normal, nothing special, when finished, open it. First time Keil uVision
  3. On top menu select Project -> Manage -> Pack Installer… Keil uVision packs
  4. If you don’t have installed them yet, 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 here. You need version 1.0,8 if you want to use my libraries with Keil packs. How to do this, look at the begginning of the post.
  5. Close Pack installer
  6. Go back to top and select Project -> New uVision Project…
  7. Save it to computer. I choose My documents with subfolder “uVisionFirstProject“, and project name is “hello_world“, then click Save.
  8. A new window should be opened, “Select Device For Target ‘Target 1’…“, find here your device:
    • STM32F429ZI (for STM32F429 Discovery board)
    • STM32F407VG (for STM32F4 Discovery board)
    • STM32F401RE (for Nucleo F401-RE board)
    • STM32F401VC (for STM32F401 Discovery board)
    • and click OK
  9. A new window “Manage Run-Time Environment” should  be opened Manage Run-Time Environment
  10. Select options:
    • Under CMSIS select CORE,
    • Under Device select Startup
    • Under Device -> StdPeriph Drivers select Framework and all other components you will need in your program, (GPIO for pins, SPI for SPI communication..)
    • If you need DSP (Digital Signal Processing) instructions for some reason (FFT, arm math, etc), you need to select DSP under CMSIS.
  11. Click OK, window will be closed. In program on the left look at Project.New project created
  12. Click on “plus” on the left under “Device” group and open “system_stm32f4xx.c (startup)” file and search for

    and change it to (This is number in MHz, to match your crystal frequency. STM32F4/29 have both 8MHz crystal)

    If you are working with STM32F429 (or any other device, which works with 180MHz) and USB application, find something like that in the same file and edit 360 to 336 to decrease speed to 168MHz
  13. Rename Source Group 1 to User
  14. Right click on Target 1 and select Add Group…, and rename it to TMMake new Groups
  15. Go to your project directory, in my case “My documents\uVisionFirstProject” and create new files “main.c” and “defines.h
  16. Go back into uVision program, under User folder on the left make right click and select “Add Existing Files to Group ‘User’..” and select files main.c and defines.hAdd existing files to group
  17. Go to top in uVision and select Project -> Options for Target ‘Target 1’..
    • Under Target set Xtal to 8
    • Under C/C++ -> Define type: “USE_STDPERIPH_DRIVER,STM32F4XX,HSE_VALUE=8000000″ (without quotes; if you have other crystal, like 16MHz, type 16000000 for HSE_VALUE”)
    • Under C/C++ -> Include paths click on “” and set paths to (without that you files will not be found and you will get errors):
      • Your project folder, in my case path is “My documents\uVisionFirstProject”,
      • To folder, where your (my) external libraries are saved, in my case path is “My documents\STM32F4_libraries”
    • Under Debug find ST-Link Debugger and click settings

    Set Xtal to 8Mhz Set preprocessor symbols Include setup On top right select ST-Link Debugger and click Settings

  18. Under debugger settings in Debug tab set PORT to SWSW Debug mode
  19. Under Flash Download tab click on Add button and select “STM32F4xx 2MB Flash2MB Flash download
  20. Hit OK, than again OK and again OK.
  21. Open your main.c file in uVision editor and make int main function
  22. Press F7 to compile and make sure there is no errors.Project ready and compiled well

If you go to my Github account and download all files from there, you will be able to run projects directly from directories, no need to copy each needed library to your project. My Github account Have a nice programming time 🙂

Known bugs

  • ST-LINK USB communication error
    • If you have installed MDK5.11a version of Keil uVision, then you have a problem downloading hex file to the discovery board via ST-Link, says “ST-LINK USB communication error”. Solution is here.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...