Saturday, February 2, 2013

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

Maybe everyone knows how to do it. I did not. I spent days, trying to understand the logic. It *seems* to me that I grasped it somehow, but I fail to see to what extent. But since it's working for me now, I hurry to write this down so I can always check back to this post and know how to start.


So, what I have here is STM32F3-Discovery from STMicroelectronics. I also have IAR Embedded Workbench IDE installed. I had to fall back to IAR as CooCox IDE doesn't support STM32F3-Discovery and they don't tell when they are going to have it. This happens not the first time with them - STM32F4-Discovery still poorly supported and I'm not sure if STM32F0-Discovery is supported either. So I decided I'd go with IAR.

So, I have a project folder where I store all my projects, it's E:\Documents\STM32\F3_projects. First thing to do is to go to STM32F3-Discovery page and download firmware from Design Support tab: STM32F3 Discovery kit firmware package, including 28 examples and preconfigured projects for 4 different IDEs. Unpack it and copy  two folders in E:\Documents\STM32\F3_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\F3_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 UM1562: Getting started with software and firmware environments for the STM32F3DISCOVERY Kit (copy-pasting from there):

In the Options dialog box, select the General Options category, open the Target tab 
and select Device - ST -STM32F303
The heart of STM32F3-Discovery is STM32F303VCT6 microcontroller featuring 256 KB Flash, 48 KB RAM in an LQFP100 package
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\F3_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\F3_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\F3_projects\Libraries and here: E:\Documents\STM32\F3_projects\Utilities) :

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


$PROJ_DIR$\..\..\..\Utilities\STM32F3_Discovery (dev board define and functions)
$PROJ_DIR$\..\..\..\Libraries\STM32F30x_StdPeriph_Driver\inc (Standard Peripherials Drivers by STMicroelectronics)
$PROJ_DIR$\..\..\..\Libraries\CMSIS\Include (CMSIS Library)
$PROJ_DIR$\..\..\..\Libraries\CMSIS\Device\ST\STM32F30x\Include (STM32F303VCT6 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 STM32F30X

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

This is what I couldn't get for long. How to add all those libraries to project. Turns out, the answer is 'One-by-one'. Ok, I can deal with that, although I'm very lazy. Here's the thing. 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):

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

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

To complete the libraries setup we need the file stm32f30x_conf.h, which is included from CMSIS's stm32f30x.h. I took this file from demo project that comes with STM32F3-Discovery firmware (STM32F3-Discovery_FW_V1.1.0\Project\Demonstration\stm32f30x_conf.h. There you can also grab files for interrupts - stm32f30x_it.c and stm32f30x_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\F3_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 "stm32f30x.h"
#include "stm32f3_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.
Wow. I did it. Click some ads if you like it.

P.S. Here's the link to the project:
Post a Comment