Set up Keil for STM32-Discovery boards

After some troubles (not so few), I’ve finally created a skeleton project with Keil uVision to easy develop programs for the STM32-Discovery boards. STMicroelectronics released a library for each Discovery series board to speed up the “time to market”, called “Standard Peripheral Library”. This library include a lot of high level function and peripheral support so that developing a program is MUCH more simple and fast.

I’m using Windows 7 and the following is referred to STM32F0-Discovery board, but with few modifications (trust me, really few), can be used with any STM32-Discovery board.

In this tutorial we’ll create a “skeleton” folder in which there is everything needed to take advantages from “standard peripheral library” with Keil. Anytime you need to develop a new program, simply copy this folder and open the Keil project inside.

So, let’s start!

What do you need?

  • Keil MDK-ARM from the Keil site (https://www.keil.com/download/product/) – we’ll use the demo (lite/evaluation) version, in which the most important limitation is that the maximum project size that is only 32 kBytes (enough for not-to-big applications). A fast registration is required.
  • The “standard peripheral library” that suits your board, downloadable from STMicroelectronics site (http://www.st.com/stonline/stappl/productcatalog/app?page=partNumberSearchPage&levelid=SC1169&parentid=1743&resourcetype=SW). Here you have to select the one according to your microprocessor, so let’s read description for each entry searching for “standard peripheral library”. Click on the correct item and a new page is loaded, here scroll down up to “Sample and Buy section” e click on purple button “Download”
  • Some space on disk to install everything (MDK-ARM requires about 1.4GBytes on local disk)

COMPLETE TUTORIAL

Obviously, install Keil’s MDK-ARM (it will take some time depending on your machine)

Unzip the “standard peripheral library” in a place of your choice, a folder with the following content will be created

stdperiph_unzip

Now, since I’m a person really sensible to the order and I hate wasting space, I’ve taken only useful files and folders and rearranged them in a more comfortable way. To do so, create a folder named “STM32F0-Discovery_skeleton” and in this folder create other four folders:

  1. “inc”
  2. “sys”
  3. “obj”
  4. “project_files”

The final result should be:

folder_first_struct

Now it’s time to copy files, “*STD_PERIPH_UNZIP*” is your “standard peripheral library” unzipped folder

  • Copy each file from “*STD_PERIPH_UNZIP*\Libraries\CMSIS\Device\ST\STM32F0xx\Include” to “STM32F0-Discovery_skeleton\sys”
  • Copy “startup_stm32f0xx.s” from “*STD_PERIPH_UNZIP*\Libraries\CMSIS\Device\ST\STM32F0xx\Source\Templates\arm” to “STM32F0-Discovery_skeleton\sys”
  • Copy every file from “*STD_PERIPH_UNZIP*\Libraries\CMSIS\Include” to “STM32F0-Discovery_skeleton\sys”
  • Copy every file from “*STD_PERIPH_UNZIP*\Libraries\STM32F0xx_StdPeriph_Driver\inc” and “*STD_PERIPH_UNZIP*\Libraries\STM32F0xx_StdPeriph_Driver\src” to “STM32F0-Discovery_skeleton\inc”
  • Copy “main.c”, “main.h”, “stm32f0xx_conf.h”, “stm32f0xx_it.c”, “stm32f0xx_it.h”, “system_stm32f0xx.c” from “*STD_PERIPH_UNZIP*\Project\STM32F0xx_StdPeriph_Templates” to “STM32F0-Discovery_skeleton\project_files”

It’s time to open Keil uVision and create a new project (open the program, click on “Project” -> “New uVision Project…”, then select the folder that we have created and write “Project” as name, than click on “Save” button

keil_new_proj

Keil uVision asks now to select the target device, in the case of STM32F0-Discovery is STMicroelectronics -> STM32F051R8, then click on “OK”

When prompted to copy and add “startup_stm32f0xx.s” to project, click on “No”

prompt

Well, the environment is now basically ready, but we need further tuning. Press Alt+F7 to open the target option menu. Here just select “Use MicroLIB”

options_target

Then click on “Output” tab and then “Select Folder for Objects…”, now choose the “obj” folder and click on “OK”

options_output

Now, open “C/C++” tab and:

  1. Insert in the “Define” field “USE_STDPERIPH_DRIVER, STM32F0XX, USE_DEFAULT_TIMEOUT_CALLBACK”
  2. Choose Optimization “Level 2 (-O2)”
  3. Choose “One ELF Section per Function”
  4. Insert in the “Include Paths” field “.\sys;.\inc;.\project_files”

options_c_main

Click on “Debug” tab and make the window so that any options is like the following, than click on “Settings”

options_debug

Another window will appear, click on “Flash Download” tab and select “Reset and Run” option, than click “OK”

options_stlink

Click on “Utilities” tab and choose ST-Link Debugger” for cascade option menu, than click “OK”

options_utilities

Now, right-click on “Target 1” and choose “Manage Components…”

target_manage_components

A new window will appear

target_components_blank

In this windows there is a bit to do:

  1. Rename “Target 1” to “STM32F0-Discovery”
  2. Create three groups: “project_files”, “inc”, “sys”
  3. Add file to each group
  • In the “inc” group, every file (*.h and *.c) that is present inside the folder “inc” should be added
  • In the “sys” group, every file (*.h, *.c and *.s) that is present inside the folder “sys” should be added
  • In “project_files” add every file (*.h and *.c) that is inside “project_files” folder.

Then click on “OK”

target_components

Now press F7 to check that everything works correctly 😀

If you liked this tutorial, share it!

15 thoughts on “Set up Keil for STM32-Discovery boards

  1. Hello, I think I’ve found a mistake in your article. In the part “Now it’s time to copy files, “*STD_PERIPH_UNZIP*” is your “standard peripheral library” unzipped folder…” is nothing copied to the “inc” folder, but in the end of the article you write, that everything from “inc” folder should be copied to the “inc” group.

    • You are alright! I have corrected now. Basically everything from *STD_PERIPH_UNZIP*\Libraries\STM32F0xx_StdPeriph_Driver\ – \inc and \src must be copied in the inc folder, then everything from this folder should be added to the correct group.

  2. Hi,

    Thank you very for exhibit your work , but in the inc ,sys,… divided group section it may be mistake or I could not figure out the step. In the beginning we copy from the peripheral library to inc,sys and project_files. And there is no file movement for inc directory but in the end project component “.c and h.” files is waited to add to inc directory. Could you help me about it?

    Thank you in advance

    • I’ve corrected the mistake just now, see other comments to notice the little variation. Thank you for interest!

  3. There is a thing that puzzles me :
    “When prompted to copy and add “startup_stm32f0xx.s” to project, click on “No”

    What is the role of this startup file? Is it repalced by a functional equivalent, and how? Or is it thoroughly unuseful/nocive?

    • the startup file is replaced with a functionally equivalent one, provided within the ST’s standard peripheral library, to avoid any compatibility error.

  4. Great tutorial, helped me a lot !!!

    I believe that it does not cause me trouble, however if you can help me, when I compile it generates many warnings, know how I can fix this?

    Thank you and Congratulations !!!

    Build target ‘STM32F0-Discovery’
    compiling main.c…
    .\sys\stm32f0xx.h(82): warning: #47-D: incompatible redefinition of macro “STM32F051”
    #define STM32F051
    project_files\main.c: 1 warning, 0 errors
    linking…
    Program Size: Code=376 RO-data=208 RW-data=0 ZI-data=1024
    “.\Template_STM32F051.axf” – 0 Error(s), 26 Warning(s).

    • I’ve remove the repeated part of your comment. However there is a multiple redefinition of the same thing, in this case “STM32F051” check if this is defined in the compiler tab inside Keil porject’s options. However if it is defined here, simple remove the line #define STM32F051 in the main.c file

  5. i do the following procedure as per above mentioned but i cant read the device name STM32F030, I am currently using keil 3.85 .how can i add the stm32f0 device in device database of keil

    • what do you mean for “I can’t read the device name”? is the STLINK embedded in the discovery board that can’t read the ID? if so, check for the jumpers on the board itself, otherwise you can be working with a defective part. in the datasheet check also for SWD and SIO pin and check if these (plus RESET and GND pin) are correctly wired to the STLINK

  6. I was trying to setup Keil uVision to program STM32L Discovery Board (the one with the LCD embedded). I followed all the instructions replacing the files of the STM32F0-Discovery board with the corresponding ones of the STM32L1-Discovery board. However when I press F7 to compile the following error messages appear:

    Build target ‘STM32L1-Discovery’
    compiling main.c…
    assembling startup_stm32l1xx_hd.s…
    assembling startup_stm32l1xx_md.s…
    assembling startup_stm32l1xx_mdp.s…
    assembling startup_stm32l1xx_xl.s…
    linking…
    .\obj\Project.axf: Error: L6200E: Symbol __Vectors_Size multiply defined (by startup_stm32l1xx_md.o and startup_stm32l1xx_hd.o).

    —– similar entries removed —–

    Not enough information to list image symbols.
    Not enough information to list the image map.
    Finished: 2 information, 0 warning and 176 error messages.
    “.\obj\Project.axf” – 176 Error(s), 0 Warning(s).
    Target not created

    Any suggestion to fix this?
    Thanks

    • the problem is here:

      assembling startup_stm32l1xx_hd.s…
      assembling startup_stm32l1xx_md.s…
      assembling startup_stm32l1xx_mdp.s…
      assembling startup_stm32l1xx_xl.s…

      you have to check which is the exact file for you processor [that should be STM32L152RBT6 (128 KB Flash memory, 16 KB RAM, 4 KB EEPROM) or STM32L152RCT6 (256 KB Flash memory, 32 KB RAM, 8 KB EEPROM)] and leave only this file removing the other three

  7. Thanks for the explaination, it is really helpful. I am using Win7 prof, with STM32F0 discovery, and everything works fine. Now my question is when I plug the Board and click the Load button, it says : Error Flash Download Failed. Please help me.

    • Sorry for the really late reply. Right click on the project name in Project tree on the left, then choose “Options for…”, then Debug tab, then verify that is configured to use ST-Link debugger. Also, Run to main() should be checked. If still no luck, in the same tab choose Setting at the upper right, and send me an image of the actual configuration for the three tabs in the new window

  8. Thank you Fabio! I am studying the very basics of ARM microcontroller and I have a STM32L1 board. Your post helped me a lot, I just had to modify a couple of things and all went well. Your blog is excellent. Cheers from Argentina. ¡Chau amigo!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.