Sunday, March 17, 2013

Howto create a new project for STM32F4-Discovery in IAR from scratch (step-by-step)

This is a twin post of Creating a new project for STM32F3-Discovery in IAR from scratch, but for STM32F4-Discovery.


So, what I have here is STM32F4-Discovery from STMicroelectronics. I also have IAR Embedded Workbench IDE installed.

I have a project folder where I store all my projects, it's E:\Documents\STM32\F4_projects. First thing to do is to go to STM32F4-Discovery page and download firmware from Design Support tab: STM32F4DISCOVERY board firmware package, including 22 examples (covering USB Host, audio, MEMS accelerometer and microphone) and preconfigured projects for 4 different IDEs. Unpack it and copy  two folders in E:\Documents\STM32\F4_projects: Utilities and Libraries. Also create Projects directory there:

 Volume in drive E has no label.
 Volume Serial Number is 7091

 Directory of E:\Documents\STM32\F4_projects

02.02.2013  12:54    <DIR>          .
02.02.2013  12:54    <DIR>          ..
01.02.2013  22:22    <DIR>          Libraries
02.02.2013  11:40    <DIR>          Projects
01.02.2013  22:22    <DIR>          Utilities
               0 File(s)              0 bytes

I'll be creating for each new project a folder in Projects, while keeping all the libraries and devboard files in Libraries and Utilities folder (so I don't have those huge folders in each project folder).

Create IAR workspace and project

Launch IAR, select File->New->Workspace from the menu.
Select Project->Create New Project from menu, in the dialog select Empty Project, click OK
Save as dialog will appear. Navigate to Projects folder, create a new folder for project, give it a name (I will use name Scratch for this tutorial). In this folder, create a new folder again, this time for saving IAR and project setting files, call it IAR. Enter the folder and give project a name (I'll use FromScratchProject for this tutorial). Click Save.

Setting project target and options

Right click on the project name in workspace and select Options.

Then set the target and options as per UM1467: Getting started with software and firmware environments for the STM32F4DISCOVERY Kit (copy-pasting from there):

In the Options dialog box, select the General Options category, open the Target tab 
and select Device - ST - STM32F407VG
The heart of STM32F4-Discovery is STM32F407VGT6 microcontroller
Select the Linker category and open the Config tab; in the Linker configuration file pane, select Override default and click Edit to display the Linker configuration file editor
Note RAM END value!
Click Save to save linker options (in IAR folder of the project)

If your source files include header files, select the C/C++ Compiler category, open the Preprocessor tab (you may need to use Left-Right buttons to see that tab), and specify their paths. The path of the  include directory is a relative path, and always starts with the project directory location referenced by $PROJ_DIR$. (What I found really is that it's possible to use absolute paths, but I guess there might be problems when moving project. I prefer absolute myself, but for this tutorial will try to create relative paths). 
In case you didn't know, \..\ in a path means a step back. In our case:

$PROJ_DIR$ is E:\Documents\STM32\F4_projects\Projects\Scratch\IAR

But I don't want to use this IAR folder for sources and headers. I will put my headers in E:\Documents\STM32\F4_projects\Projects\Scratch. So first path to search for includes will be:
I also want to add libraries and devboard headers (they are here: E:\Documents\STM32\F4_projects\Libraries and here: E:\Documents\STM32\F4_projects\Utilities) :

$PROJ_DIR$\..\ <- E:\Documents\STM32\F4_projects\Projects\Scratch
$PROJ_DIR$\..\..\ <- E:\Documents\STM32\F4_projects\Projects
$PROJ_DIR$\..\..\..\ <- E:\Documents\STM32\F4_projects


$PROJ_DIR$\..\..\..\Utilities\STM32F4-Discovery (dev board define and functions)
$PROJ_DIR$\..\..\..\Libraries\STM32F4xx_StdPeriph_Driver\inc (Standard Peripherials Drivers by STMicroelectronics)
$PROJ_DIR$\..\..\..\Libraries\CMSIS\Include (CMSIS Library)
$PROJ_DIR$\..\..\..\Libraries\CMSIS\ST\STM32F4xx\Include (STM32F407VGT6 Device driver)

Also, define the following symbols:


This is an alternative to #define directive. You could also do this by adding #define in main.c:

#define STM32F4XX

To set up the ST-Link embedded debug tool interface, select the Debugger category, open the Setup tab and, from the drop-down Driver menu, select ST-Link

Open the Download tab and select Use flash loader(s)

Select the ST-Link category, open the ST-Link tab and select SWD as the connection protocol

Click OK

Adding Libraries to project

We have to add all the libraries to project one by one.By now, compiler knows where the headers are. But the linker doesn't know where the source code is. So I'll have to add all the library source code to the project. I'll create a group for each library (right click on the project name, Add->Add Group...). Then I'll add sources (right click on group name, Add->Add Files):

STM32F4xx_StdPeriph_Driver (add all *.c files from \Libraries\STM32F4xx_StdPeriph_Driver\src. In reality you'll only need files for the peripherals you work with, e.g. only stm32f4xx_rcc.c and  stm32f4xx_gpio.c, but I add all of them for simplicity)

STM32F4_Discovery (add all *.c files from Utilities\STM32F4-Discovery)
We don't need to add CMSIS sources as it only has headers.

To complete the libraries setup we need the file stm32f4xx_conf.h, which is included from CMSIS's stm32f4xx.h. I took this file from demo project that comes with STM32F4-Discovery firmware (\STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_PWM_Input\stm32f4xx_conf.h). There you can also grab files for interrupts - stm32f4xx_it.c and stm32f4xx_it.h). Add this file to user group of the project. The idea of this file is to include all the headers from one place, it's short and intuitive.

Writing code

In workspace, right-click on project name and select Add->Add Group... Call it user. I'll put my code and files that need to be modified. Press Ctrl+N to create a new file. Save it as main.c in the project folder (E:\Documents\STM32\F4_projects\Projects\Scratch). Add this file to user folder by right clicking on the folder name and selecting Add->Add Files...
Write some code in it and download it to the board to see if it works, e.g.:

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

void main (void) {

  while(1) {

After you download the code and reset the board, you should see the red led comes on.


There's no such button in IAR as to Download code. You can do many things, but there's no Download. I used to use menu for this Project->Download->Download Active Application. Then I found how to assign a hot key to this. Go to Tools->Options->Key Bindings, select Project from drop-down, find Download the active application command and bind a key to it.
Post a Comment