Skip to content

Getting started with STM32F[0|3|4]discovery boards

René Kijewski edited this page Jun 17, 2014 · 18 revisions

This is a short guide which will lead you through the first steps with STM32F0/3/4discovery boards from the very beginning to degugging an example project. The document describes development on a Ubuntu-PC.

Preparation

Of course the first thing you need is RIOT. Check out the wiki if you don't know how to clone the repository. The article about Creating your first RIOT project may be helpful as a reference during the course of this guide.

Next, you will need a toolchain for ARM processors to compile, link, debug etc. your projects for the respecting platform/device (in this case STM32F0/3/4discovery boards). Some possible toolchains are listed in the wiki entry: Family: ARM. However, in my case the gcc-arm-embedded toolchain worked without any problems. You have to add the directory with executables to your PATH variable. For the gcc-arm-embedded example this should look like this:

export PATH=/home/*path_to_file*/gcc-arm-none-eabi-4_8-2014q1/bin:$PATH
export PATH=/home/*path_to_file*/gcc-arm-none-eabi-4_8-2014q1/arm-none-eabi/bin:$PATH

It's safer to add the directory at the beginning of the PATH variable so the operating system won't find another preinstalled version of GCC at first and uses this. You can check if that worked with:

echo $PATH

Note that each time you open a new terminal-window you have to adapt the PATH variable again. If you do not want to do this, you can put this command in a file called .bashrc located in your home directory. Note that you will have to edit this entry when you want to use a different compiler (for example for another board).

Furthermore you need the stlink-tool to use the onboard programmer via USB connection from your computer. Check this wiki-entry for installation details. Don't forget to add the st-flash and the st-util tools to your bin path. This is simply done by copying these files from the directoy where you downloaded stlink to your bin path in the root directory.

To monitor if your example program works, it is comfortable to use a UART to USB Converter so you can control output on a terminal window.

Compiling files

When the preparation is done, you should be able to use the right toolchain for ARM embedded processors. You can check that by typing:

gcc -v

Next, switch to RIOT's examples directory and choose hello-world. Note that you have to change the PROJECT or in newer versions the APPLICATION variable in the Makefile to your own project name if you copy-paste the hello-world example to generate your own example project. The project can be compiled for the board by typing:

make BOARD=stm32f4discovery

(For other boards you type the respective board name). Now the files should have been successfully compiled in the bin folder. A new subfolder carrying the name of your board should have been created there.

Flashing files

Now you have to connect the device to your computer via USB-microUSB to write your program on it. Use the CN1 connector on the STM32F4discovery board for this. Normally, the device shoud be flashed automatically by typing:

make flash

which should run the st-flash tool. However, this did not work at my setup so I flashed the board manually using the st-flash tool:

sudo st-flash write bin/stm32f4discovery/hello-world.hex 0x8000000

The .hex file should be written to the devices program-storage with start address 0x8000000

Running program

After the hello-world program has been flashed, it should automatically run on your device. Somtimes it's necessary to reset the board with the B2 push-button, so the program restarts. Now you want to see if it runs. Therefore a UART to USB converter would be nice. To connect such an adapter you need the preconfigured board pins:

  • PA2 = RX Data
  • PA3 = TX_Data
  • GND = GND

Now you can open any terminal program. In RIOT's directory RIOT/dist/tools/pyterm/ you can find pyterm which can be opened with the appropriate ttyUSB-port:

sudo ./pyterm.py /dev/ttyUSB0

In this case the adapter is connected to ttyUSB-port zero. If there is more than one ttyUSB device connected to your PC, you have to check which port your adapter uses. If you are successfully connected, you should now see "Hello world" on your screen. If not, try to reset the board. With each reset the program starts afresh.

Some might like to add the pyterm tool to the PATH for faster access, but that's not neccessary.

Debugging program

To check some debugging, functions it could be helpful to generate your own example project (for example by copy-pasting the hello-world example) and write a little test script in the main.c. Compiling and flashing must be done again for the new project. Don't forget to change the APPLICATION variable in the makefile to your own project name if you created a new folder in the examples directory! So, for the debugging test I wrote:

int i;
while (1) {
    for (i = 0; i < 100; i++) {
        printf("%d\n", i);
        if (i == 100) {
            i = 0;
        }
    }
}

Now that all above steps are done, you can start the st-util tool with:

sudo st-util

This should automatically connect to your device and start a GDB-Server listening at port :4242. In another terminal you start the GDB with:

arm-none-eabi-gdb -tui *your_project*/bin/stm32f4discovery/hello-wolrd.elf

The declaration of the .elf file is important because otherwise there wouldn't be meta-information like code line numbers. The option -tui (Text User Interface) runs a terminal interface which shows the source file, assembly output, program registers etc. and makes it more comfortable to debug. The last step to debug your program is to connect the GDB to the GDB-server (which runs in the other terminal and waits for connection). After the GDB started just type:

target extended-remote :4242

Congratulations! You should now be able to debug your program using the GDB-commands.

Clone this wiki locally