Get started with the STM32F4 on Ubuntu Linux
Updated 2014-04-03
Introduction
This tutorial describes how to set up a complete and free toolchain for STM32F4xx microcontrollers, including how to use hardware floating point support. It is mostly aimed towards beginners with ARM microcontrollers, however, experienced developers could probably find something useful here as well. It is assumed that the reader is a bit familiar with the C programming language and the Bash terminal.
At the end of this tutorial, the reader should be able to build and upload programs to the STM32F4** using the STLinkV2 interface (such as the one found on the STM32F4 Discovery board). This is done using Ubuntu Linux in this tutorial, however, the instructions should be general enough to make this work on any Debian-based GNU/Linux distribution.
The following hardware/software will be used:
- STM32F4 Discovery evaluation board
- Ubuntu Linux 12.04 (different versions should work as well)
- QSTLink2 (with some improvements)
- My modified summon-arm-toolchain script
Firstly, a few packages have to be installed. Open a terminal and use the following command:
| ```bash |
| sudo apt-get install build-essential git flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo libtool libftdi-dev libusb-1.0-0-dev zlib1g zlib1g-dev python-yaml |
sudo apt-get install build-essential git flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo libtool libftdi-dev libusb-1.0-0-dev zlib1g zlib1g-dev python-yaml
# Install a toolchain and OpenOcd
Follow the instructions at this location to install the toolchain:
<https://launchpad.net/~terry.guo/+archive/gcc-arm-embedded>
Build and install OpenOCD
| |
| --- |
| ```bash
git clone git://openocd.git.sourceforge.net/gitroot/openocd/openocd
cd openocd
./bootstrap
./configure --enable-maintainer-mode --disable-option-checking --disable-werror --prefix=${PREFIX} --enable-dummy --enable-usb_blaster_libftdi --enable-ep93xx --enable-at91rm9200 --enable-presto_libftdi --enable-usbprog --enable-jlink --enable-vsllink --enable-rlink --enable-stlink --enable-arm-jtag-ew
make
sudo make install
``` |
git clone git://openocd.git.sourceforge.net/gitroot/openocd/openocd
cd openocd
./bootstrap
./configure --enable-maintainer-mode --disable-option-checking --disable-werror --prefix=${PREFIX} --enable-dummy --enable-usb\_blaster\_libftdi --enable-ep93xx --enable-at91rm9200 --enable-presto\_libftdi --enable-usbprog --enable-jlink --enable-vsllink --enable-rlink --enable-stlink --enable-arm-jtag-ew
make
sudo make install
# Alternative: Download and build GCC, GDB, OpenOCD and newlib
At this point you should be able to build all the required tools, for which the summon-arm script can be used. For me it didn’t work right out of the box, so I modified the script a bit. You can download the modified version here:
[summon-arm-toolchain](https://github.com/vedderb/summon-arm-toolchain/archive/master.zip)
Unzip the files to some directory and run the script:
| |
| --- |
| ```bash
cd PATH_TO_DOWNLOADED FILE
unzip summon-arm-toolchain.zip
cd summon-arm-toolchain
chmod +x summon-arm-toolchain
./summon-arm-toolchain
``` |
cd PATH\_TO\_DOWNLOADED FILE
unzip summon-arm-toolchain.zip
cd summon-arm-toolchain
chmod +x summon-arm-toolchain
./summon-arm-toolchain
Wait for it to finish building (it will take a while). While this is building, you can make sure that the tools are in you path by opening another terminal and typing (make sure to replace YOU\_USER with your user name):
| |
| --- |
| ```bash
sudo su
echo 'export PATH=/home/YOUR_USER/sat/bin:$PATH' > /etc/profile.d/arm_tools.sh
exit
``` |
sudo su
echo 'export PATH=/home/YOUR\_USER/sat/bin:$PATH' > /etc/profile.d/arm\_tools.sh
exit
This should make sure that you can run the tools from any directory without the need to specify the full path. For the path update to take effect you have to log out and log in again.
I have experienced that the texinfo version included with Ubuntu 13.10 can cause problems when running the build script. In that case, downgrading it could help:
| |
| --- |
| ```bash
wget http://ftp.gnu.org/gnu/texinfo/texinfo-4.13a.tar.gz
tar -zxvf texinfo-4.13a.tar.gz
cd texinfo-4.13
./configure
make
sudo make install
``` |
wget http://ftp.gnu.org/gnu/texinfo/texinfo-4.13a.tar.gz
tar -zxvf texinfo-4.13a.tar.gz
cd texinfo-4.13
./configure
make
sudo make install
After the downgrade, try running summon-arm-toolchain again and see if it works.
When the summon-arm script finishes, you should be able to build programs for the STM32F4 (and many other) microcontrollers and use hardware floating point support.
# A simple example project
I have created a simple makefile project for the STM32F4 discovery board with a blinking LED, stdout (printf, etc) connected to UART2 and the system timer interrupt. A simple performance test for hardware floating point speed is also included. All of the latest ST standard peripheral libraries are included as well. You can download the project here: [STM32F4\_Sample](http://vedder.se/wp-content/uploads/2012/07/STM32F4_Sample.zip)
Just unzip the project and run make from the project root directory to build it:
| |
| --- |
| ```bash
cd PATH_TO_DOWNLOADED FILE
unzip STM32F4_Sample.zip
cd STM32F4_Sample
make
``` |
cd PATH\_TO\_DOWNLOADED FILE
unzip STM32F4\_Sample.zip
cd STM32F4\_Sample
make
You should now have binary files in the build directory of the project.
If you connect the RX line of a 3.3V Usb-to-UART chip (115K 8n1) to PA2 you should be able to use a serial terminal to see the the *stdout* output (printf) from the application. This way you can print debug information, which can be very useful when dealing with peripherals.
**Important:** Do not connect any RS232 cable to these pins, as these voltage levels will damage the stm32f4. Use a 3.3V USB-to-UART converter.
You can also use the micro-usb port on the stm32f4-discovery if you follow the instructions in [this](/u/e7106b285a2e53a3) post.
If you want to experiment with the hardware floating point support, look at this part in the makefile:
| |
| --- |
| ```bash
# Check for valid float argument
# NOTE that you have to run make clan after
# changing these as hardfloat and softfloat are not
# binary compatible
ifneq ($(FLOAT_TYPE), hard)
ifneq ($(FLOAT_TYPE), soft)
override FLOAT_TYPE = hard
#override FLOAT_TYPE = soft
endif
endif
``` |
# Check for valid float argument
# NOTE that you have to run make clan after
# changing these as hardfloat and softfloat are not
# binary compatible
ifneq ($(FLOAT\_TYPE), hard)
ifneq ($(FLOAT\_TYPE), soft)
override FLOAT\_TYPE = hard
#override FLOAT\_TYPE = soft
endif
endif
**Note** that you have to run *make clean* after changing this before you build the project, as the libraries are not binary-compatible between hardfloat and softfloat builds.
**Also note** that you should use the 32-bit float version of the standard math libraries (sinf, powf, sqrtf etc, they have an f at the end) to take advantage of the hardware floating point unit. 64-bit double is not supported by the hardware.
**Important:** When using the Linaro-GCC version of the original summon-arm-toolchain script, 64-bit double will not work when using hardfloat (at least it did not for me). So the change to Linaro-GCC 2012-06 was necessary. But remember, as the hardware floating point unit does not support 64-bit double, those operations will be done in software; which is up to 100 times slower than using 32-bit float.
# Using Eclipse for development
**NOTE:** Feel free to skip this section if you don’t plan to use Eclipse.
While any text editor and a command line is enough to develop for the STM32F4, an IDE is a lot more convenient. I personally like [Eclipse](http://www.eclipse.org/) as it is really useful with code-completion, source navigation, syntax checking etc.
If you don’t have java installed, install it with:
| |
| --- |
| ```bash
sudo apt-get install openjdk-7-jdk
``` |
sudo apt-get install openjdk-7-jdk
Start by downloading Eclipse Classic from [here](http://www.eclipse.org/downloads/). Unpack the archive and move it to */opt/* by typing the following in a terminal from where you have unpacked eclipse:
| |
| --- |
| ```bash
sudo mv eclipse /opt/
``` |
sudo mv eclipse /opt/
Now create a symbolic link to eclipse by typing
| |
| --- |
| ```bash
sudo ln -s /opt/eclipse/eclipse /usr/bin
``` |
sudo ln -s /opt/eclipse/eclipse /usr/bin
You should be able to start eclipse from the terminal now by typing *eclipse*. You can also install *alacarte* (to add eclipse to the unity launcher, gnome menu etc.) by typing
| |
| --- |
| ```bash
sudo apt-get install alacarte
``` |
sudo apt-get install alacarte
The next step is to install the [GNU ARM](http://gnuarmeclipse.sourceforge.net/) plugin. Open eclipse from a terminal by typing *eclipse* and go to *Help > Install new software*. Add the following source
> http://gnuarmeclipse.sourceforge.net/updates
and follow the steps to install it.
## Create a project in eclipse
Open eclipse and go to the workbench. Go to *Window > Open Perspective > Other…* and choose *C/C++*. Select *File > New > C Project*. Call the project *STM32F4\_Sample* and choose *Makefile project > Empty Project* with the *ARM Linux GCC (Summon)* toolchain and click *Finish*.
Take the [previously downloaded](http://vedder.se/wp-content/uploads/2012/07/STM32F4_Sample.zip) files and paste them into your Eclipse project. You can either paste the files directly into eclipse by right-clicking on the project and selecting paste, or you can paste them into your working directory using the file browser and then right-click on the project in eclipse and choose *Refresh*.
This project still uses the same makefile as before. The Eclipse ARM and CDT plugins can also handle the build for you, however, I prefer managing the makefile by myself as I know exactly what is going on this way.
You can build the project by right-clicking on it and selecting *Build Project* (which makes eclipse run *make all*). You can also clean the project by right-clicking on it and selecting *Clean Project* (which makes eclipse run *make clean*).
# Upload the program to the STM32F4 discovery
In order to upload the binary files to the STM32F4 Discovery board I have been using a program called [QSTLink2](http://code.google.com/p/qstlink2/). I have modified the program a bit and added a button to upload the last uploaded file (which is pretty convenient) and fixed a few bugs in the cli interface. You can download the modified version [here](http://vedder.se/wp-content/uploads/2012/07/qstlink2.zip).
You have to build QSTLink2 from source in order to use it. First, install some dependencies:
| |
| --- |
| ```bash
sudo apt-get install qt4-qmake libqt4-dev libqt4-gui libqt4-xml qt4-designer qtcreator libusb-0.1-4 libusb-1.0-0-dev
``` |
sudo apt-get install qt4-qmake libqt4-dev libqt4-gui libqt4-xml qt4-designer qtcreator libusb-0.1-4 libusb-1.0-0-dev
Now unzip QSTLink2 to some directory and cd to it from a terminal. Then type:
| |
| --- |
| ```bash
qmake-qt4
make
sudo make install
sudo reload udev
``` |
qmake-qt4
make
sudo make install
sudo reload udev
Plug in your STM32F4 Discovery board. You should now be able to start QSTLink2 (*./qstlink2*), connect to the STM32F4 Discovery and upload the binary file which is generated in the *build* directory of the sample project when running *make*.
**NOTE:** I had some problems uploading files to other STM discovery boards based on Cortex M3 using QSTLink2. So if you have the same issue, give [Texane STLink](https://github.com/texane/stlink/) a try. Detailed instructions on how to use it can be found on the page where you download it.
# Alternative: Upload the program using OpenOCD
From the build directory (where the .elf file is located) run:
| |
| --- |
| ```bash
openocd -f interface/stlink-v2.cfg -c "set WORKAREASIZE 0x2000" -f target/stm32f4x_stlink.cfg -c "program project.elf verify reset"
``` |
openocd -f interface/stlink-v2.cfg -c "set WORKAREASIZE 0x2000" -f target/stm32f4x\_stlink.cfg -c "program project.elf verify reset"
Replace project.elf with the name of your .elf file.
# Debugging
I have written about hardware debugging by using eclipse and OpenOCD/gdb [here](/u/5d0f4d7c360c27ee).
[](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=7ZMDJJFHUSCV4&lc=US&item_name=vedder%2ese¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted)