Flashing the STM32VL with stlink

1. Introduction

The STM32VL Discovery board comes with an mini USB connector. While on windows the device shows up as an storage device when connected, on Linux it does not. It is said, that this is because the STM32 mass storage protocol is implemented poorly.

To be able to flash the device, and do on board debugging on Linux, the st-util from “Captains Missing Link” is one option. This little tool communicates directly with the STM32 through generic SCIS commands, using the stlink protocol. It then exposes an GDB server to which one is able to connect trough GDB. From within the GDB shell one could flash binaries to the device, start/stop programs, do single step execution, set breakpoints etc.

2. Prerequisites

To get “st-util” compiled you need the development library for generic SCSI access as well as libusb-1.0.0. On a Debian based system, you can install this library by executing the following as root:

apt-get install libsgutils2-dev libusb-1.0.0-dev

Also you will need “git” because the sources for “st-util” are only available through github. Again, on Debian you easily obtain git by issuing (as root):

apt-get install git

Well and since we are dealing with source code, I assume you have a working “gcc” installed on the system you intend to compile “st-util” :-).

To test the “st-util”, you also need a binary to flash to the STM32. Thus if you want to create a binary by your self, you will need a:

3. Getting the Sources

First, we need to get the sources from github. Change to a directory of your choice and start cloning the sources by entering:

git clone git://github.com/texane/stlink.git

This should create a directory named “stlink”. We will refer to this directory as “top level directory” further on.

4. Compiling the Sources

To compile the sources, execute the following command within the top level directory (which is “stlink”):

make

If everything went well, a binary called “st-util” is created within the “build” subdirectory.

5. Installing the Binary

The make file has no target for installing the tool, so we will do this by hand. The binary we are going to install to “/usr/bin” by executing the following command with root privileges (from within the top level directory):

install -s -m 775 gdbserver/st-util /usr/bin/st-util

Note: the “-s” switch strips the symbol tables of the binary while installing.

Next we need to install the udev rules for our st-link device, so that a device “/dev/stlink”, accessible form unprivileged user accounts, is created every time we connect our STM32 Discovery board. The needed rules are part of the sources, they come with the file “10-stlink.rules”. In the top level directory with root privileges we enter:

install -m 644 49-stlinkv1.rules /etc/udev/rules.d/49-stlinkv1.rules

To activate the new rules either reboot, or execute the following as root:

udevadm control --reload-rules

To speed up device creation, it is also suggested by “Captions Missing Link”, to append the following line to your “/etc/modprobe.d/”. One could do so by installing the following script:

install -m 644 stlink_v1.modprobe.conf /etc/modprobe.d/stlink_v1.modprobe.conf

6. Basic Usage

I will only cover the very basic usage of the tool in this article, and will not go into GDB details. The most common thing one wants to archive is to flash a binary to the STM32. To do so with the “st-util”, we first connect the STM32 to a USB-port. Then we fire up the “st-util”:

st-util -1 -p 1234

The above command connects to the STM32 through STLINKv1 (-1 option), and listens on port “1234” as a GDB-server for incoming connections. If the tool successfully connected to the STM32, the “COM” led goes to stady read on the board.

Note: it may take a few seconds until “/dev/stlink” is created, so the first trys to fire up the tool may fail.

Now we can start GDB and connect to the newly created server. Thus we change to the directory where our STM32 binary resides, and start GDB.

arm-none-eabi-gdb

Note: the GDB that comes with your cross-compile tool chain has to be used!

This opens the GDB command shell. Within the shell we first need to tell GDB to connect to our server on localhost at port 1234:

tar ext :1234

Assuming the binary you like to flash to the STM32 is called “firmware.elf”, you type into the GDB shell:

load firmware.elf

This flashes the binary (the red “COM” led on the board starts flashing), which you can execute by:

run

If you press the black reset button on your STM board, the program on the STM is stopped, and the GDB command line is ready to take commands again. You may execute “load” again to flash a new version of your program and then start it with “run”.

Note: Instead of using the HW reset, you may also hit Ctrl-C within GDB, then enter “kill” and answer “y” to the following confirmation. Now you may do a “load/run” as described before.

7. Other Tools for Flashing the STM32 Discovery

  • stm-flash – a python tool flashing the STM32 via USART
    stlink-download
  • stlink-download – an other program using stlink to flash via USB

2 Comments to Flashing the STM32VL with stlink

  1. 14. August 2012 at 16:55 | Permalink

    First, thanks for the information on what to reasonably do *after* you build the texane ST-Link package. That information is missing from the web.

    The command install -s -m 775 gdbserver/st-util /usr/bin/st-util fails with current texane builds, you may want to update.

Leave a Reply

You must be logged in to post a comment.