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
- 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
- They are basically dead for Keil uVision
- 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.
- 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.
- Install as normal, nothing special, when finished, open it.
- On top menu select Project -> Manage -> Pack Installer…
- 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.
- Close Pack installer
- Go back to top and select Project -> New uVision Project…
- Save it to computer. I choose My documents with subfolder “uVisionFirstProject“, and project name is “hello_world“, then click Save.
- 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
- A new window “Manage Run-Time Environment” should be opened
- 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.
- Click OK, window will be closed. In program on the left look at Project.
- Click on “plus” on the left under “Device” group and open “system_stm32f4xx.c (startup)” file and search for
1#define PLL_M 25
and change it to (This is number in MHz, to match your crystal frequency. STM32F4/29 have both 8MHz crystal)
1#define PLL_M 8
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
1234567891011121314151617181920212223/************************* PLL Parameters *************************************//* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */#define PLL_M 8/* 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 */#if defined (STM32F427_437xx) || defined (STM32F429_439xx)#define PLL_N 336 /* Set this to 336 if you use USB on device, default 360 *//* SYSCLK = PLL_VCO / PLL_P */#define PLL_P 2#endif /* STM32F427_437x || STM32F429_439xx */#if defined (STM32F401xx)#define PLL_N 336/* SYSCLK = PLL_VCO / PLL_P */#define PLL_P 4#endif /* STM32F401xx */ - Rename Source Group 1 to User
- Right click on Target 1 and select Add Group…, and rename it to TM
- Go to your project directory, in my case “My documents\uVisionFirstProject” and create new files “main.c” and “defines.h“
- 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.h
- 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
- Under debugger settings in Debug tab set PORT to SW
- Under Flash Download tab click on Add button and select “STM32F4xx 2MB Flash“
- Hit OK, than again OK and again OK.
- Open your main.c file in uVision editor and make int main function
- Press F7 to compile and make sure there is no errors.
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.
Recent comments