> What’s New ? <

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.

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

Flashing Methods

Difficulty Score:-

  • 1 - fastest and simplest, only useful on boards with Boot Option Jumpers, such as the massive range of cheap Chinese STM32F103C8T6 Cortex-M3 boards
  • 2 - fast and complex, required for any STM32F Discovery boards as they don’t have Boot Option Jumpers
  • 3 - slower and complex
  • 4 - slower and complex, incredibly useful for debugging with ‘C’ or Assembly applications. Not needed For Mecrisp-Stellaris use as Forth does it’s own debugging.
Difficulty Flashing Method Software Required Hardware URL
3 SWD OpenOCD JTAG or SWD https://sourceforge.net/projects/openocd/
4 GDB GDB over OpenOCD JTAG or SWD https://www.gnu.org/s/gdb/
2 St-flash st-flash SWD https://github.com/texane/stlink
2 St-flash (FreeBSD) st-flash (FreeBSD) SWD Freebsd, OpenBSD use the port or package ” devel/stlink “
1 Serial Bootloader stm32loader.py 3.3v USB Serial Dongle stm32loader.py

Serial Bootloader

Note

See www.st.com/resource/en/application_note/cd00167594.pdf for which serial ports support bootloading on your STM32F MCU.

Your board will need the two Boot Option Jumpers present, but not all boards have them.

Howto flash Mecrisp-Stellaris using the Bootloader

  1. Download the latest Mecrisp-Stellaris tarball: http://mecrisp.sourceforge.net/
  2. Extract the binary for your MCU, in this case it’s mecrisp-stellaris-2.3.9a/stm32f103-ra/mecrisp-stellaris-stm32f103.bin
  3. Hook up your 3.3v USB Serial Dongle to the USART 1 (only USART-1 supports the serial bootloader on a STM32F103xx MCU) pins on your cheap Chinese target board . You can usually power the board from the dongle.
  4. Set the boot pin jumpers. Boot 0 to HIGH or 1. BOOT 1 will stay LOW or 0.
  5. Run something like the command below to flash Mecrisp-Stellaris onto your target :-
  6. Set both boot pins to LOW, reset the power to the target
  7. Fire up your serial terminal and talk Forth to your MCU/Board.
stm32loader.py -p /dev/cuaU16 -evw mecrisp-stellaris-stm32f103.bin
Reading data from mecrisp-stellaris-stm32f103.bin
Bootloader version 0x22
Chip id 0x410, STM32F1, performance, medium-density
Writing 20352 bytes to start address 0x8000000
Write 256 bytes at 0x8000000
Write 256 bytes at 0x8000100
Write 256 bytes at 0x8000200
...
Read 256 bytes at 0x8004F00
Verification OK
Mecrisp-Stellaris RA 2.3.9 for STM32F103 by Matthias Koch
 ok.
 ok.
 ok.

Details:

  • The serial bootloader is stored in the internal boot ROM memory (system memory) of STM32 devices. It is programmed by ST during production and cant be wiped or overwritten. 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” jumpers on the MCU board. Set Boot 1 = LOW (0) and Boot 2 = HIGH (1) before using the bootloader. After bootloading set them both to LOW (0) to run the binary you just uploaded and power cycle the board.
  • A ‘bootloader’ program needs to be run on the PC, I’ve used stm32loader.py without any problems on a Shenzhen LC mini STM32FC8T6 board from China.
  • Which USART do I use on a chip/board with multiple USARTS ? See STM Application note cd00167594 for your MCU. Mecrisp-Stellaris usually uses USART-1
  • See this table for serial port pins and connections to find your most likely Bootloader port connections.
  • After Mecrisp-Stellaris has uploaded, set the two boot jumpers to LOW and reset the power. You should then be able to use a serial terminal to talk Forth to the board using exactly the same serial port on the target board.
  • Mecrisp-Stellaris takes 6 seconds to upload its 23KB image with stm32loader.py and seems to work everytime on a cheap $9 Shenzhen LC mini STM32FC8T6 board from China.

Warning

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

Boot Pin Functions

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

SWD

  • 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

Howto flash Mecrisp-Stellaris using SWD

To be written ..

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

Howto flash Mecrisp-Stellaris using JTAG

  • Board: 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.