Beginner's guide to STM32 Microcontroller


STM32F Getting Started Tutorial


Introduction


The STM32F series of microcontrollers are made by ST and are one of the most powerful microcontrollers out there. These devices feature an ARM Cortex M CPU surrounded by a variety of very useful modules. They exist in different offerings namely: Value line, medium density, high density and XL density. They are also available from most online sellers at reasonable prices.
Today I will explain the setup needed and how to proceed in making a project with these devices.
In case the reader is not familiar with microcontrollers I would recommend having a look at this post:

Tools needed to build a project on a STM32FXXX


Part 1 The Hardware


In terms of hardware what's needed is a development board, two choices are possible for the development board either an OEM board pictured below, but do note they contain no programmer:




and you might need an ST link programmer like any of these two below:





or the second option is an ST board that features a built in programmer:

In case you are starting from scratch and don't want to go for complications go for the ST boards, they are a safe investment and they contain a lot of a additional hardware on the board that can become very handy.
OEM boards on the other contain no programmer and no additional hardware(except maybe an RS232 level converter), but in case you do not want to bother building a PCB but simply want a ready board the OEM boards are a good option. Just be careful from whom you buy.

Part 2 The Software

In order to write code to the microcontroller, a development environment and a compiler is required. There are two options out there:

1) IAR Embedded workbench for ST ARM Microcontroller

Currently this is my preferred development environment. Do note that this is a paid product, but there is a time limited and code size limited version (32K). The code size limited version works nicely for me and you do not need to worry about installing compilers everything is in a single install. What's also nice is that there are frequent updates to the code limited version supporting newer microcontrollers from ST. The price point if you are thinking of purchasing is a little bit steep but worth it in case you are making a commercial project.

2) OR Coocox COIDE

COIDE is a software available from CooCox. It is a free IDE but does not come with a compiler. You will need to download and install GCC  which is a free compiler. Once completed the compiler needs to be linked to COIDE. If you do the steps right both COIDE and GCC should work right away. In order to be able to download and install COIDE a registration is required. COIDE will also prompt for the registered user account when launching.




3) The STM32 Standard Peripheral Library

Code for the STM32 can be written directly to use the hardware this is possible but can be complicated for a beginner. the other options is to use the STM32 Standard Peripheral Library. It can be downloaded from ST website. Below is an example for an STM32F10X microcontroller.

shown above are the folder that will be created once the library is installed. The folders are organized as follows:


Folder Name
Description
_htmlresc
Contains icons
Libraries
Contains subfolders for hardware libraries
Project
Contains example projects for hardware built into the MCU
Utilities
Libraries for STM32 evaluation boards
Release_Notes.html
Release notes on the current library version, updates, fixes, etc.
stm32f10x_stdperiph_lib_um.chm
Help file with plenty of description on the components in the library


4) Optional-The STM32 Cube editor

The modules on the microcontroller will need to be configured before they can be used, the pins to which the modules are attached will also need to be configured. In order to simplify this step, the STM32 cube can be used. I would not recommend it immediately for a new comer. Stick to the Standard peripheral library for the time being, more complications can be added later if needed.


Creating a project

It might be strange for you to start here but trust me that's the cleanest way I found to handle my projects. The steps I will provide below will be for IAR workbench but you can easily apply it to COIDE. In IAR workbench, the structure is organized into project and workspaces. A workspace can contain one or several projects. In order to keep things simple I am creating a single workspace with a single project in it. The steps below gives an example on how to organize your folder structure, from there you can devise your own.

1) Creating a project template folder structure

You might be working with a variety of ST devices in the long run, first thing create a folder for each device as follows:
Next go into the folder for you target device as from here I will build a project for an STM32F103C8T6 as an example name the folder as per your device from here.
First create a folder called Template  inside the STM32F103C8T6
In the Template folder create two folders named  Project Files  and workspace Files

Next In the Project folder create three folders System, User inc and User src
Open the System folder. Go back to where the standard peripheral library was installed, go to Libraries and find the STM32FXX_StdPeriph_Driver copy and paste it into the System folder
Remain in the location where the STM32FXX_StdPeriph_Driver was copied, open the CMSIS folder to find the CMX folder, CM3 for my example. Copy and paste the CM3  folder to your System folder.

There is another file needed for your project to run which you need to copy from the project examples folder. You can take it from any folder you want. In my case I am taking the file from
STM32F_StdPeriph_Lib_V3.5.0->Project->ADC->TimTriggerAutoInjection. Once in that folder search for stm32f10x_conf.h  and copy that into Project folder->Project Files->System->STM32F10x_StdPeriph_Driver->inc





The final folder structure should look as per below. In case anything has been missed it will help to clean things up



 Once you're done with the template folder there is an amendment which needs to be effected. Go to the following folder Template\ProjectFiles\System\CM3\DeviceSupport\ST\STM32F10x\startup\iar and look for a file called startup_stm32f10x_md.s
In case you are using an XL density file you need to do the modification into startup_stm32f10x_xl.s

First you will need to remove the read-only  attribute from the file in windows. Right click on the startup file, in my case startup_stm32f10x_md.s in windows explorer. Then uncheck the Read-only attribute and click OK.



Next edit the file in notepad and find the sections that have .text:CODE:REORDER(1)  and change them to display .text:CODE:REORDER:NOROOT(1) and those that contain .text:CODE:REORDER(2) to .text:CODE:REORDER:NOROOT(2). This fix is required by IAR to avoid warnings when compiling.





2) Copying and renaming the Template folder to create a new project


Next start by copying the Template folder , paste it. Next rename it as per your project, mine will be called GPIOTest.


All the required files should be automatically included. This is where all the new projects will start now. A question might remain as to why do this? Well in practice I sometimes ended up modifying some of the library files namely for interrupts and always had to clean it later on.

 

3) Creating the project in IAR Embedded Workbench

Choosing the project type and saving the project

Once you have IAR embedded workbench for ST setup, the next step is to launch the software and click Project and Create New Project as shown below.



 Next chose a C main project and click OK

You will be prompted next where to save your project, save it in the Project Files folder created previously. I have called mine GPIOTest Project




The Workspace navigator and the Code Editor window will be loaded . The next thing I usually do is to remove the main that's automatically created, though a right click Remove action. The reason why I do that is that I want to control where my files go in my folder structure. We will replace it don't worry just bare with me for the time being.


The next step consists of selecting the project in the Workspace navigator and clicking on Project  and then  options. Note that the Project menu will display options based on what's selected in the workspace navigator. If the project is not selected most of the options will be grayed out.



Setting up project options- Choosing target device

Once you get into the next screen at the General Options category, you need to select the device you want to work with. For my project I will be using an STM32F103C8T6 so I will go and select ST-.STM32F103->STM32F103X8. The chosen device should be displayed as shown below.



Setting up project options- Preprocessor directives

Next to define the path where your files are stored you need to go to C/C++ Compiler section under Category. Scroll the tabs until you see Preprocessor. If you have followed by folder structure above you just need to enter the following under Additional include directories(one per line)

$PROJ_DIR$\System\CM3\DeviceSupport\ST\STM32F10x\startup\iar
$PROJ_DIR$\System\CM3\DeviceSupport\ST\STM32F10x\
$PROJ_DIR$\System\STM32F10x_StdPeriph_Driver\inc
$PROJ_DIR$\User inc\

In case you wonder what the $PROJ_DIR$ means, well simply put its a dynamic way of pointing to your project folder.

Next enter the following into Defined symbols (one per line)

USE_STDPERIPH_DRIVER
STM32F10X_MD

The two lines above enforce the use of the standard peripheral library mentioned earlier and tells IAR that the target device is an STM32F10X medium density. You might need to change this last line based on the device you are using.

If everything is done properly the Preprocessor tab should look as shown below.


Setting up project options- Linker

Next under Category go to Linker and simply tick the checkbox that says Override default 


Setting up project options- Debugger

Next you need to define how you will connect to the target board. Currently I am going to use an ST Link programmer. In order to do that go to Debugger  under Category. In the Debugger section set the driver to ST-LINK as shown below


 

Setting up project options- ST-LINK

Next we need to define how the programmer connects to the board there are two options:

JTAG: Normally used by OEM boards and have 20 pins divided in two rows of 10 pins each
SWD: Used on the ST boards and require a total of 5 pins

In my case I will use a JTAG connector set as per below



That's it now you can click  OK  to complete the project settings.

Creating Groups and adding in required files

Ready to start coding? Well not yet. The reason why the standard peripheral library was downloaded was to effectively make use of this library. The paths have already been defined in the section Setting up project options- Preprocessor directives above but we still need to define which files will be used and this is done in the  Workspace explorer.

Begin by right clicking on the Project which in my case is GPIOTest Project and in the menu that displays choose Add->Add Group as shown below. The purpose of creating a group is to structure the files in the Workspace explorer in order to navigate through easily.



When prompted to enter a group name enter  CM3. Then create a sub group using the same option I just used to create a subgroup called CoreSupport below CM3. You should get the result below




Once you're done right click on CoreSupport  and select Add Files


You will need to browse to your project folder-> System-> CM3. Once you are there add the files Core_CM3.C  and Core_CM3.H. It should look like below when you are done.


Now you will need to repeat the steps I have explained above to create a structure that looks like the one below. What you see in the groups are the minimal files needed to interact with the IO pins. Once you finish adding files to the StdPeriphLib Src group you are done adding the system files. Of course if you are building a project that uses more than the IO e.g. SPI you will need to add the required files. Make sure the files that have the H extension stay in the Inc  group and the files that have the C  extension stay in the Src  folder.

In case you have seen some other form of project setup in IAR you will have noticed that the header files are normally not included, this prevents from jumping into function details when stepping into code while debugging. with the setup below you should be able to easily go into any level of detail when debugging.



A side note for beginners, the Output  group does not have to be created it should be there by default when you created the project.

User files and group structure

Ready for the coding part? here we go. First of all, currently we are using C programming language, to properly structure your code specific functionalities need to be set in separate files. All files written in C require a header and a source file. The header only defines the functions that will be used and the source file details the function operation. Header files have the .H extension and source files have a .C  extension. In my group structure all header files will go under the User->Inc group and source files under User->Src  group.


Creating and adding a header file to the User->Inc Group 


To create a new header file go to File->New->File as shown below


Once this step is done a new tab will be displayed as shown below.  First save the file by right clicking on the tab and choose Save untitled 1




In my example I am going to call this file GPIOTest.h and its saved in the User->Inc  folder we defined before




Now this file needs to be added to the group structure User->Src in the Workspace explorer. To do this right click on the Src group under User  and choose Add "GPIOTest.h"  




Note: In case your file does not show up as show don't panic. Instead used Add Files  and browse to find your file.

Creating and adding a source file to the User->Src Group 

To create a new source file go to File->New->File as shown below


Now this file needs to be saved so like we did in the previous step right click on the untitled1  tab and click save untitled1. since this will be a source file that will implement the functions defined in GPIOTest.h I will call this file GPIOTest.c



Like we did before we need to add this source file to the User->Src  group. so for that I will right click on the Src group under User  and select  Add "GPIOTest.c"


Note: In case your file does not show up as show don't panic. Instead used Add Files  and browse to find your file.




 Creating the Main file

In order for any project to execute it needs a starting point or main entry point called main. To make things simple I will implement this in a file called Main.c You will note that for the main file I am not creating a header file this is the only exception. So first we need to go to the same File->New-File option we used previously.


Next I will again right click on the untitled 1  tab and choose save untitled1


This time I will call this file Main.c  as shown in the screen below in the User src  folder


Once this is done I will add the Main.c  file into the  User->Src in IAR


Note: In case your file does not show up as show don't panic. Instead used Add Files  and browse to find your file.

If everything went well your Workspace explorer should look like the one below


Compiling and making a project

Once all code are in place(more on this in another post), you will need to go the Projects  menu and click on  Compile. Simply this will compile your code



In case there are errors they will show up as shown below in your Build window at the bottom left of your IAR IDE. These errors MUST  be resolved you can't get away with them and you can't write then into your microcontroller.


Otherwise you might get warnings. Warnings are not as sever as errors, you can get away with them but my advice is to sort them out as you could get unpredictable behavior when your program is running.



In case everything is fine your build window should look like the one below



Now to make the project, make the project??? Well this last step in simple words involves linking your compiled code to your libraries into a single unit. In order to do that you proceed by again clicking on the Project menu then Make as shown below


If you get errors at this stage go back to the steps above called Setting up project options- Preprocessor directives  and Creating Groups and adding in required files  may you missed something there otherwise check your files and folders if everything is present.

If all goes well you should get the message below



You can compare the message above and the one for compile and note that here it is mentioning Linking.


Burning the program into the STM32

Since your program now has been coded properly the next step it to write it to the STM32. Now before doing this make sure the board is powered. For the ST boards that means plugging a USB cable connected to a pc is enough. For the OEM boards they have a jumper where you select power from the programmer or USB. I recommend using the USB as the programmer only powers the board after you have hit the button to program the STM32. I have also had STM32's that are so power hungry(XL density) that the programmer can't cope with them and needed a USB cable to get the power.

Now once your board is powered, click on the Project menu you will have two options which I highlighted below:

1) Download and debug will write the code to your STM32 but you will then need to make it execute like by line by pressing  F10/F11  key on your keyboard. You can also press F5 to skip the debugging and make the code execute on its own(provided you did not put any breakpoints in the code)

2) Download -> download active application(recommended) will write the code to your microcontroller. You need to press the reset button on your board for your code to execute. If you don't press reset the board will not do anything.



If you have reached this stage your code should be executing on your microcontroller. If something does not work as expected you need to go back to your code and fix what is wrong.

 

Conclusion

I hope the above steps will help anyone get started with an STM32 microcontroller and using IAR. Like I mentioned the steps above can be used in Coocox IDE as well. I have purposefully not included code in the above so that at lest the steps to use the software are clear. Coding the STM32 should come in another post.




Comments

Popular posts from this blog

Writing code for the STM32

Beginner's Guide to STM32CubeMX