Methods for Flashing Mecrisp-Stellaris onto a STM32

There are a number of methods to upload a binary image of Mecrisp-Stellaris to a STM32 MCU. These include SWD, JTAG and Serial Bootloader which are listed below. See the “Software” Heading below for more information.

If this is your first time, and youre in a hurry, checkout the Quickstart page ?

SWD

  • Easiest and most common
  • Uses the SWD pins on the MCU and STLINKv2 firmware (factory installed on STM32 “Discovery Boards”)
  • Requires a USB/SWD programmer (Every STM32 “Discovery Board” is a USB to SWD programmer and comes with “STLINK”)
  • Requires a USB port on the PC
  • Use STLINKv2 only, avoid all old STLINKv1 SWD boards like the plague
  • Can be used for On Chip Debugging with GDB
Example: STM32F0 Discovery Board
  • FreeBSD OS is used in this example, so your file locations, names and versions may differ to mine if you use Linux etc.
  • The USB connector on the STM32F0 Discovery Board connects to a PC
  • Usb to 3.3v dongle for talking serial to Forth on the mini-stm32f103 board via a serial terminal. PL2303 chipsets are recommended being Unix friendly.
  • Picocom is used in this example as the serial terminal

Hardware Used: STM32F051 Discovery Board

_images/stm32-disco.jpg

JTAG

  • Older less used method
  • Uses the JTAG pins on the MCU
  • Requires a USB/JTAG Dongle and a JTAG connector on the MCU pcb.
  • Requires a USB port on the PC
  • The author uses a ‘Olimex ARM-USB-TINY-H” USB to 20 pin IDC JTAG connector on both Linux and FreeBSD
  • Can be used for On Chip Debugging with GDB
Example: STM (Shenzen Mini-Arm board) STM32F103C8T6
  • FreeBSD OS is used in this example, so your file locations, names and versions may differ to mine if you use Linux etc.
  • Olimex ARM-USB-TINY-H JTAG dongle
  • OpenOCD is used to Flash the STM32F103C8T6
  • Usb to 3.3v dongle for talking serial to Forth on the mini-stm32f103 board via a serial terminal. PL2303 chipsets are recommended being Unix friendly.
  • Picocom is used in this example as the serial terminal

The Hardware used

_images/mini-stm32F103C8T6-board-jtag-serial-1.jpg

Open a Root Xterm to run OpenOCD. Leave it running until MCU is flashed:

root@Gronk:/home/tp # openocd -f /usr/local/share/openocd/scripts/interface/ftdi/olimex-arm-usb-tiny-h.cfg -f /usr/local/share/openocd/scripts/target/stm32f1x.cfg
Open On-Chip Debugger 0.9.0 (2016-02-03-08:48)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "jtag". To override use 'transport select <transport>'.
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
jtag_ntrst_delay: 100
none separate
cortex_m reset_config sysresetreq
Info : clock speed 1000 kHz
Info : JTAG tap: stm32f1x.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32f1x.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints

Open a Telnet session to localhost 4444, which is how the user talks to OpenOCD

tp@Gronk:~/projects/programming-languages/forth/mecrisp-stellaris/mecrisp-stellaris-2.2.4/stm32f103-ra# telnet localhost 4444 
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger

> poll
background polling: on
TAP: stm32f1x.cpu (enabled)
target state: halted
target halted due to undefined, current mode: Thread 
xPSR: 00000000 pc: 00000000 msp: 00000000

> reset halt
JTAG tap: stm32f1x.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
JTAG tap: stm32f1x.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00003b6a msp: 0x20000330

Check how much Flash you have

>  flash probe 0     
device id = 0x20036410
flash size = 64kbytes
flash 'stm32f1x' found at 0x08000000

>  flash probe 1
flash bank 1 does not exist
flash bank '#1' is out of bounds

> flash probe 2
flash bank 2 does not exist
flash bank '#2' is out of bounds

>

Check if the Flash is erased

> flash erase_check 0
device id = 0x20036410
flash size = 64kbytes
successfully checked erase state
        #  0: 0x00000000 (0x400 1kB) not erased
        #  1: 0x00000400 (0x400 1kB) not erased
        #  2: 0x00000800 (0x400 1kB) erased
        #  3: 0x00000c00 (0x400 1kB) erased
        #  4: 0x00001000 (0x400 1kB) not erased
	... all not erased
        # 56: 0x0000e000 (0x400 1kB) not erased
        # 57: 0x0000e400 (0x400 1kB) erased
        # 58: 0x0000e800 (0x400 1kB) erased
        # 59: 0x0000ec00 (0x400 1kB) erased
	# 60: 0x0000f000 (0x400 1kB) erased
        # 61: 0x0000f400 (0x400 1kB) erased
        # 62: 0x0000f800 (0x400 1kB) erased
        # 63: 0x0000fc00 (0x400 1kB) erased
>

Mass erase all the Flash

> stm32f1x mass_erase 0
stm32x mass erase complete

Check the Flash is really erased

> flash erase_check 0  
successfully checked erase state
        #  0: 0x00000000 (0x400 1kB) erased
	... all erased
        # 63: 0x0000fc00 (0x400 1kB) erased
>

Flash Mecrisp-Stellaris onto the STM32F103C8T6

> flash write_image /home/tp/projects/programming-languages/forth/mecrisp-stellaris/mecrisp-stellaris-2.2.4/stm32f103-ra/mecrisp-stellaris-stm32f103.bin 0x08000000
wrote 20336 bytes from file /home/tp/projects/programming-languages/forth/mecrisp-stellaris/mecrisp-stellaris-2.2.4/stm32f103-ra/mecrisp-stellaris-stm32f103.bin in 0.652892s (30.418 KiB/s)
>

Check the Flash, how much space did Mecrisp-Stellaris use up ?

flash erase_check 0
successfully checked erase state
        #  0: 0x00000000 (0x400 1kB) not erased
        #  1: 0x00000400 (0x400 1kB) not erased
        #  2: 0x00000800 (0x400 1kB) not erased
        #  3: 0x00000c00 (0x400 1kB) not erased
        #  4: 0x00001000 (0x400 1kB) not erased
        #  5: 0x00001400 (0x400 1kB) not erased
        #  6: 0x00001800 (0x400 1kB) not erased
        #  7: 0x00001c00 (0x400 1kB) not erased
        #  8: 0x00002000 (0x400 1kB) not erased
        #  9: 0x00002400 (0x400 1kB) not erased
        # 10: 0x00002800 (0x400 1kB) not erased
        # 11: 0x00002c00 (0x400 1kB) not erased
        # 12: 0x00003000 (0x400 1kB) not erased
        # 13: 0x00003400 (0x400 1kB) not erased
        # 14: 0x00003800 (0x400 1kB) not erased
        # 15: 0x00003c00 (0x400 1kB) not erased
        # 16: 0x00004000 (0x400 1kB) not erased
        # 17: 0x00004400 (0x400 1kB) not erased
        # 18: 0x00004800 (0x400 1kB) not erased
        # 19: 0x00004c00 (0x400 1kB) not erased
        # 20: 0x00005000 (0x400 1kB) erased
	... all erased
        # 63: 0x0000fc00 (0x400 1kB) erased
>
If you want to Flash by calling OpenOCD from a script, instead of a open OpenOCD terminal and manual Telnet session, you can use this command instead.
  • This command flashes, verifies and exits, but does not erase any pre existing Forth Dictionary.
root@Gronk:/home/tp/projects/programming-languages/forth/mecrisp-stellaris/mecrisp-stellaris-2.2.4/stm32f103-ra # openocd -f /usr/local/share/openocd/scripts/interface/ftdi/olimex-arm-usb-tiny-h.cfg -f /usr/local/share/openocd/scriptsget/stm32f1x.cfg -c "program mecrisp-stellaris-stm32f103.bin verify exit 0x08000000"

Open On-Chip Debugger 0.9.0 (2016-02-03-08:48)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "jtag". To override use 'transport select <transport>'.
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
jtag_ntrst_delay: 100
none separate
cortex_m reset_config sysresetreq
Info : clock speed 1000 kHz
Info : JTAG tap: stm32f1x.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32f1x.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : JTAG tap: stm32f1x.cpu tap/device found: 0x3ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x3)
Info : JTAG tap: stm32f1x.bs tap/device found: 0x16410041 (mfg: 0x020, part: 0x6410, ver: 0x1)
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00004d92 msp: 0x2000038c
** Programming Started **
auto erase enabled
Info : device id = 0x20036410
Info : flash size = 64kbytes
wrote 20480 bytes from file mecrisp-stellaris-stm32f103.bin in 1.155165s (17.314 KiB/s)
** Programming Finished **
** Verify Started **
verified 20336 bytes in 0.311900s (63.672 KiB/s)
** Verified OK **
shutdown command invoked
Connect a terminal to the board and start talking in Forth.
  • The mini-stm32f103 board uses pins PA9 (TX) and PA10 (RX), and these will need to be wired to your usb to 3.3v cable or dongle.
  • OpenOCD terminals may need to be exited or the board reset switch pressed
tp@Gronk:~/projects/programming-languages/forth/mecrisp-stellaris/mecrisp-stellaris-2.2.4/stm32f103-ra# picocom -b 115200 /dev/cuaU5 --imap lfcrlf,crcrlf --omap delbs,crlf --send-cmd "ascii-xfr -s -l100"

picocom v2.1

port is        : /dev/cuaU5
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
stopbits are   : 1
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : ascii-xfr -s -l100
receive_cmd is : rz -vv -E
imap is        : crcrlf,lfcrlf,
omap is        : crlf,delbs,
emap is        : crcrlf,delbs,

Type [C-a] [C-h] to see available commands

Terminal ready
Mecrisp-Stellaris RA 2.2.4 for STM32F103 by Matthias Koch
  ok.
  ok.

Serial Bootloader

  • Most tedious method (imho)
  • The bootloader is stored in the internal boot ROM memory (system memory) of STM32 devices. It is programmed by ST during production. Its main task is to download the application program to the internal Flash memory through one of the available serial peripherals (USART, CAN, USB, I2C, SPI, etc.). A communication protocol is defined for each serial interface, with a compatible command set and sequences.
  • Selection is using the two “boot” pins on the MCU.
  • Requires a special program to be run on the PC.
  • Requires a USB port on the PC
  • I haven’t tried serial bootloading, as it lacks the many advantages of JTAG or SWD, but would be handy if no SWD or JTAG facility was on hand.

Warning

Requires a USB/3.3v cable or dongle for the PC. You can’t use RS232, if you do you may blow up your STM32 chip.

Software

Software Required Hardware URL
OpenOCD JTAG, SWD https://sourceforge.net/projects/openocd/
GDB over OpenOCD JTAG, SWD https://www.gnu.org/s/gdb/
st-flash SWD https://github.com/texane/stlink
st-flash SWD Freebsd, OpenBSD use the port or package ” devel/stlink “
STM32Loader 3.3v Serial, BOOT1=1,BOOT0=0 https://github.com/jsnyder/stm32loader

Hardware

Boot Pins

Pins BOOT0 and BOOT1 select how the STM32 starts
BOOT 1 BOOT 0 Boot mode
X 0 User Flash memory
0 1 System memory (bootloader)
1 1 Embedded SRAM