October 1, 2017

The STM32F103C8T6 and the USB boot loader

A fellow named Roger Clark wrote a DFU boot loader for the STM32. DFU is part of the USB standard, and stands for "Device Firmware Upgrade". There are DFU 1.0 and DFU 1.1 specifications. The idea is to have a boot loader that complies with the standard specification so that existing utilities such as dfu-util under linux can be used with it.

This DFU boot loader is what the Arduino crowd seems to use to load "sketches" into the STM32. It is very easy for me to put this into one of my devices using my ST-Link gadget. People who don't have one of these do it using the serial port and a python script.

After cloning this from Github and studying it for some time, I was able beat the Makefile into submission and get it to build on my Fedora 24 system.
make generic-pc13
make flash
I added the "flash" target to my Makefile to do the download using ST-Link and OpenOCD. It is then a 5 second job to flash the image into my device using the STLINK dongle, and as soon as I do that, the onboard LED begins to blink rapidly. When I hit the reset button while watching the log on my Fedora system, I see:
Dec  1 16:10:16 trona kernel: usb 1-1.1.1: new full-speed USB device number 52 using ehci-pci
Dec  1 16:10:16 trona kernel: usb 1-1.1.1: New USB device found, idVendor=1eaf, idProduct=0003
Dec  1 16:10:16 trona kernel: usb 1-1.1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Dec  1 16:10:16 trona kernel: usb 1-1.1.1: Product: Maple 003
Dec  1 16:10:16 trona kernel: usb 1-1.1.1: Manufacturer: LeafLabs
Dec  1 16:10:16 trona kernel: usb 1-1.1.1: SerialNumber: LLM 003

Serial console output

This produces absolutely no output on the serial console. Since I am able to build this from source, I can add my own serial output as an aid to my own understanding. This is easier said than done, but took a couple of hours one evening. The major aspects of this are: With the serial code added, the boot loader image no longer fits into 8K, so the ability of the bootloader to download to 0x08002000 will no longer work, but that is fine for what I want to do (namely learn a thing or two).

Host side

This may be something called the "maple loader". The details of this are buried in the Arduino magic, one of the things I hate about the arduino development environment. This is a DFU boot loader, so perhaps there is something by this name? There is something called "dfu-util" which may be what I am looking for. If you install arduino for the STM32, this is likely in the Arduino_STM32 directory. You may need to install special udev rules on linux so your DFU device gets properly recognized.

I have downloaded at least 4 different git repositories with versions of dfu-util and have no idea which one is the one to use.

Also, on Fedora 25, I see these packages:

dfu-programmer.x86_64                    0.6.2-5.fc24                   fedora
dfu-util.x86_64                          0.9-1.fc25                     fedora
dfuzzer.x86_64                           1.4-3.fc24                     fedora
libdfu.i686                              0.7.5-1.fc25                   fedora
libdfu.x86_64                            0.7.5-1.fc25                   fedora
libdfu-devel.i686                        0.7.5-1.fc25                   fedora
libdfu-devel.x86_64                      0.7.5-1.fc25                   fedora
So I install dfu-util via "dnf install dfu-util". There are no package dependencies. It has a man page, and indicates that "dfu-util -l" should list currently attached DFU capable devices. So, I flash the STM32duino boot loader into my STM32 and run it. I do have to run it as root or it just tells me "Cannot open DFU device 1eaf:0003".
I get:
su
dfu-util -l

dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Found DFU: [1eaf:0003] ver=0201, devnum=8, cfg=1, intf=0, path="1-1.1.1", alt=2, name="STM32duino bootloader v1.0  Upload to Flash 0x8002000", serial="LLM 003"
Found DFU: [1eaf:0003] ver=0201, devnum=8, cfg=1, intf=0, path="1-1.1.1", alt=1, name="STM32duino bootloader v1.0  Upload to Flash 0x8005000", serial="LLM 003"
Found DFU: [1eaf:0003] ver=0201, devnum=8, cfg=1, intf=0, path="1-1.1.1", alt=0, name="STM32duino bootloader v1.0  ERROR. Upload to RAM not supported.", serial="LLM 003"
Meanwhile the PC13 LED on my STM32 keeps blinking rapidly.

The following should upload a block of memory, but it has not worked properly yet.

dfu-util -a 1 -s 0x08000000:1024 -U newfile.bin

DFU-util sources

Fetching the Fedora SRPM and unbundling it, I look at the spec file to try to find out where they get their sources. There is no patch file, so apparently fedora does the build verbatim (which is nice). I will take the tar bundle from the SRPM and stash it for study. Here is an excerpt from the spec file:

Name:          dfu-util
Version:       0.9
Release:       1%{?dist}
Summary:       USB Device Firmware Upgrade tool
License:       GPLv2+

# Can't use normal SourceForge URL per Fedora Packaging/SourceURL
#   https://fedoraproject.org/wiki/Packaging:SourceURL
# because the project is not actually using the SourceForge file release
# system. They're just using SourceForge as a web server.
URL:            http://dfu-util.sourceforge.net/
Source0:        http://dfu-util.sourceforge.net/releases/%{name}-%{version}.tar.gz

# The old home page, which is down according to their Gitorious page:
#   https://gitorious.org/dfu-util
#URL:           http://dfu-util.gnumonks.org/
#Source0:       http://dfu-util.gnumonks.org/releases/%{name}-%{version}.tar.gz

BuildRequires: libusbx-devel

Feedback? Questions? Drop me a line!

Tom's Computer Info / [email protected]