Setting-up project for STM32F4-Discovery using IAR IDE

Saturday, November 9, 2013 7:27 AM

Prerequisites

- Install the IAR Embedded workbench (software version is 6.4 for this tutorial).
- Download the STM32F4-Discovery firmware package from the STMicroelectronics web site in the Related Tools and Software section, or directly from this link. This firmware package include a firmware for the STM32F407VGT6 microcontroller, a firmware for the STM32F4-Discovery board and 22 examples (covering USB Host, audio, MEMS accelerometer and microphone).
- Download this file Useful_Files_STM32F4 which contain the source and header interruption handler files and the Library configuration file.
- You can download this project Here.
- If you want to see the video go to the bottom of this page.

The setting up of this project is made for STM32F4-Discovery which containt STM32F407VG microcontroller.
If you use another board than the STM32F4-Discovery you have to see this tutorial.

If you use another microcontroller you don't have to download these drivers shown above. you have to looking for your microcontroller's drivers from STMicroelectronics website. and you have to be carful during the setting up of the project.


Create new project

Create a new project folder for the project. (I create a folder in the desktop called “IAR_First_Project”).
Launch IAR, select Project/Create new project from the menu. Select in the windows the C language programming and main then click OK. Save the project under the “IAR_First_Project”.

int main()
{
  return 0;
}
 
            

The project and a main file appears in the Workspace windows:


Setting options

Right click on the project and select options.

A new windows options will appears.
Select General Options from categoy, open the target tab and select Device ST STM32F407VG (ST/STM32f407/ST STM32F407VG).

Open library configuration and check Use CMSIS.

Adding Source files

After downloading the STM32F4-Discovery firmware package, shown above. Unzip the contents in the locations of your choice, preferably in a safe place because you will use it in every new project. (For this tutorial i unzip the contents in the desktop).

I'll create 3 Groups/folders in the workspace to organize files. So i creat 3 groups (Right click on the project name, Add/Add Group).
EWARM
: Embedded Workbench ARM, this group include files configuration.
STM32F4xx_StdPeriph_Driver : Contains firmwar driver for the all the STM32F407VGT6 microcontroller peripherals.
STM32F4-Discovery : Contains firmwar driver for the STM32F4-Discovery board.
User : Contains the user files.


I drag the main.c source file into the User group.
We begin to add the sources files for the EWARM Group. Right click on the project name, Add/Add Files.
A browser dialog box appear:
Go to this link:
C:\Users\xxx\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\ST\STM32F4xx\Source\Templates\iar
and select the startup_stm32f4xx.s file.
This file define the vector table for the microcontrollers.

The startup_stm32f4xx.s source file call an extern function SystemInit which is located in the system_stm32f4xx.c source file. This function setups the system clock.
Go to the link and copy the source file and paste it in the project directory :
C:\Users\xxx\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\ST\STM32F4xx\Source\Templates
Now we move to the USER Group, it already contains the main.c source file.
To add the system_stm32f4xx.c file : Right click in the User group and select Add/Add files...

We have to add the source file that contain the interrupt handlers called stm32f4xx_it.c. You can find this file inside the Useful_Files_STM32 folder which normally you have already downloaded it from the Prerequisites section in the begin of the tutorial.

This folder contains 3 files, we copy only stm32f4xx_it.c and stm32f4xx_it.h (source and header) to the project directory folder.
Then add the stm32f4xx_it.c to the USER group. (Right click on the USER group select Add/Add files)

Till now we are only interested by the source file, later in the next section we will add the headers files.

The STM32F4xx_StdPeriph_Driver group must have the file which contains the firmware functions for microcontrollers peripheral. These files are located here :
C:\Users\Tizana_Men\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\STM32F4xx_StdPeriph_Driver\src

This folder containt all the driver necessary for the STM32F407VGT6 microcontroller. For example the Analog/Digital converter stm32f4xx_adc.c, theDigital/Analog converter stm32f4xx_dac.c, the I²C bus driver stm32f4xx_i2c.c
etc ....

We add the most important driver file, the misc.c. This file provides all miscellaneous firmware functions like Configuring the NVIC (Nested Vectored Interrupt Controller) Priority Grouping etc...

Right click on the STM32F4xx_StdPeriph_Driver group and select Add/Add files and select the misc.c source file.
We can add for example the driver for microcontroller GPIO called stm32f4xx_gpio.c.

You don’t have to add all the firmware driver. Just add the driver that you ll use it in your project.

The last group , STM32F4-Discovery, which includes files that provides set of firmware functions to manage Leds, push-button available and 3-axis accelerometer... on STM32F4-Discovery Kit from STMicroelectronics. These file are located in STM32F4-Discovery_FW_V1.1.0\Utilities\STM32F4-Discovery.
We can add stm32f4_discovery.c file.
Here's how it looks like the workspace after adding the sources files :


The stm32f4xx_gpio.c source file is added under the STM32F4xx_StdPeriph_Driver group only because i want to show you later how to add the associated header file.

 

Adding Header files

Right click on the project and select options.


A new windows options will appears. Select C/C++ Compiler from categoy, open the Preprocessor tab and go to the Additional include directories.

The first header file to add is the cortex M4 header file and is located under
\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\Include
.

So we copy the link and we past it in the Additional include directories.
C:\Users\xxx\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\Include

The links are different from one pc to another, taking into account the location of the files in your computer. In my case i put the STM32F4-Discovery_FW_V1.1.0 in the desktop.
We can also write the link otherwise using these symbol $PROJ_DIR$ and '\..\'. these one allow us to looking for the header file using the project directory location.

$PROJ_DIR$\..\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\Include

This link mean that we start from the project directory using the $PROJ_DIR$. then we go to the parent directory which is in our case the Desktop using\..\ finally we look for the header file inside the driver folder which is actually located in the Destkop. 'STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\Include'

The second header file to add is the system_stm32f4xx.h from
\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\ST\STM32F4xx\Include
We copy the link and we past it in the Additional include directories.
C:\Users\xxx\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\ST\STM32F4xx\Include

The second file in this folder the stm32f4xx.h is also necessary.

Inside the stm32f4xx.h we have a condition to include the stm32f4xx_conf.h header file. So we copy the USE_STDPERIPH_DRIVER and the STM32F4XX in the defined symboles.
Here's how it look like the options windows :

The stm32f4xx_conf.h is located in the Useful_Files_STM32 folder. We copy this file under the project directory.

Now we have to add the interrupt handlers stm32f4xx_it.h and the Library configuration file stm32f4xx_conf.h. so we write $PROJ_DIR$ in the Additional include directoriers to refer to the project directory.

The Library configuration file stm32f4xx_conf.h include the list of all the header file of the firmware drivers. We have to uncomment all the header files that we need to, the misc.h and the stm32f4xx_gpio.h.

When you add the stm32f4_discovery.c source file you have immediately to uncomment 4 others header files even if you don't use any function inside the stm32f4_discovery.c otherwise you will get errors during compilation.

These header files are :
stm32f4xx_exti.h
stm32f4xx_gpio.h
stm32f4xx_rcc.h
stm32f4xx_syscfg.h

These header file are located in the \STM32F4-Discovery_FW_V1.1.0\Libraries\STM32F4xx_StdPeriph_Driver\inc.
So we add this link to the Additional include directoriers.

The last header file belong to the STM32F4-Discovery firmware and is located in the
\STM32F4-Discovery_FW_V1.1.0\Utilities\STM32F4-Discovery

Here's how it looks like the Preprocessor options windows

Additional include directories :
C:\Users\Tizana_Men\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\Include
C:\Users\Tizana_Men\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\ST\STM32F4xx\Include
$PROJ_DIR$
C:\Users\Tizana_Men\Desktop\STM32F4-Discovery_FW_V1.1.0\Libraries\STM32F4xx_StdPeriph_Driver\inc
C:\Users\Tizana_Men\Desktop\STM32F4-Discovery_FW_V1.1.0\Utilities\STM32F4-Discovery


The second methode to add the include directories using the project directory location.

Additional include directories :
$PROJ_DIR$\..\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\Include
$PROJ_DIR$\..\STM32F4-Discovery_FW_V1.1.0\Libraries\CMSIS\ST\STM32F4xx\Include
$PROJ_DIR$
$PROJ_DIR$\..\STM32F4-Discovery_FW_V1.1.0\Libraries\STM32F4xx_StdPeriph_Driver\inc
$PROJ_DIR$\..\STM32F4-Discovery_FW_V1.1.0\Utilities\STM32F4-Discovery


The project directory and the library location shouldn't be moved, otherwise you should modify the links.


Defined symbols:
USE_STDPERIPH_DRIVER
STM32F4XX


The last thing you have to include in the main file two header files stm32f4xx.h and stm32f4_discovery.h

#include "stm32f4xx.h"
#include "stm32f4_discovery.h"

int main()
{
  return 0;
}

 
            

 

Setting the Debugger

Right click on the project and select options.
Select Debugger from categoy, open the Setup tab and select Device ST-LINK as driver.

Then select the Download tab and check the Verify download and Use flash loader(s).

Go to the ST-LINK tab and select SWD for Serial Wire Debug.


 

Linker

Select Linker from categoy, open the Config tab. In the linker configuration file check Override default and click Edit to display the Linker configuration file editor

Click Save to save linker options (in IAR folder of the project)

Putt the file name and save it.

Here's how it looks the linker configuration file.

tricks

For better visibility, you can change the font as I did on my IDE. Go to the menu bar and select Tools/Options.

In the Common Fonts select Calibri, size = 10 in the proportional width Font.

In the Colors and Fonts select Consolas, size = 10 as Font.

The Font modification is optional. Do as you think best.


Video