.. index:: flashing, saving, stlink, st-link, jtag, telnet, serial bootloader, openocd, boot pins > :ref:`What's New ?` < .. _flashing-methods: Flashing Mecrisp-Stellaris onto a STM32: Methods ================================================ 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 :ref:`Quickstart page` ? .. _stm32f0discovery: .. _methods-table: Flashing Methods ---------------- Difficulty Score, 1 is easiest. +---------------------+---------------------+---------------------+-----------------------------------------+-------------------------------------------------------------------------------+ | Difficulty | Flashing Method | Software | Required Hardware | URL | +=====================+=====================+=====================+=========================================+===============================================================================+ | 1 | Serial Bootloader | stm32loader.py | :ref:`3.3v USB Serial Dongle` | :download:`stm32loader.py` | +---------------------+---------------------+---------------------+-----------------------------------------+-------------------------------------------------------------------------------+ | 1 | St-flash (FreeBSD) | st-flash (FreeBSD) | SWD | Freebsd, OpenBSD use the port or package " devel/stlink " | +---------------------+---------------------+---------------------+-----------------------------------------+-------------------------------------------------------------------------------+ | 1 | df-util | df-util | USB | http://dfu-util.sourceforge.net/ | +---------------------+---------------------+---------------------+-----------------------------------------+-------------------------------------------------------------------------------+ | 2 | SWD | OpenOCD | JTAG or SWD | https://sourceforge.net/projects/openocd/ | +---------------------+---------------------+---------------------+-----------------------------------------+-------------------------------------------------------------------------------+ | 3 | GDB | GDB over OpenOCD | JTAG or SWD | https://www.gnu.org/s/gdb/ | +---------------------+---------------------+---------------------+-----------------------------------------+-------------------------------------------------------------------------------+ .. warning:: stm32loader.py does not seem to erase at all. The '-e Erase' option seems to have no effect with stm32f103, stm32L053 etc. This is a fatal problem when flashing a new version of Mecrosp-Stellaris to your MCU. Use SWD, df-util or JTAG instead. See this url for more information: https://community.st.com/s/question/0D50X00009Xkex4/unable-to-erase-flash-stm32l05-using-bootloader-via-spi-cmds .. note:: use STM32FLASH instead, it is a Debian package: stm32flash -j /dev/ttyUSB0 ; stm32flash -k /dev/ttyUSB0 ; stm32flash -e 0 -w /path/to/mecrisp.bin /dev/ttyUSB0 .. _serial-bootloader: 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 one or two :ref:`Boot Option Jumpers`. On some Discovery boards only BOOT0 may be present as BOOT1 is internally tied to logic high inside the MCU and not available externally. 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-stm32fxxx.bin 3) Hook up your :ref:`3.3v USB Serial Dongle` to the USART 1 (only USART-1 supports the serial bootloader on a STM32F103xx (PA9-TX, PA10-RX) pins on your :ref:`cheap Chinese target board` . You can usually power the board from the dongle. 4) Set the :ref:`boot pin` jumpers. Boot 0 to HIGH or 1. BOOT 1 will stay LOW or 0. 5) Run something like this command " python ./stm32loader.py -p /dev/cuaU0 -evw ./mecrisp-stellaris-stm32fxxx.bin " 6) Set both boot pins to LOW, reset the power to the target 7) Fire up your :ref:`serial terminal` and talk Forth to your MCU/Board. Special note to use the Bootloader on a Discovery Board ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Fast and simple, requires the use of :ref:`Boot Option Jumpers`, stm32loader.py and a serial 3.3v/USB dongle. Note the STM32F0 Discovery Board has a BOOT0 pin next to a VDD pin. A - E below describe how to use the inbuilt STM32F0 Serial Bootloader on a STM32F0 Discovery board, but many other Discovery boards have the same Bootloader Jumper Methods. A) Have a serial connection to PA9/10 (This is the default Mecrisp-Forth serial connection) B) Fit 0.1" pitch Option Jumper on the STM32F0 Discovery Board between 'BOOT0' and 'VDD'. Borrow one from the nearby ST-LINK jumper pins. C) Press the STM32F0 Discovery Board RESET button D) Run the following CLI command "python ./stm32loader.py -p /dev/cuaU0 -evw ./mecrisp-stellaris-stm32f051.bin" E) Remove the Option Jumper from 'BOOT0' and 'VDD' and replace it on the ST-LINK header. DONE! Faster than SWD and easy, no need to find a usb cable etc. .. _stm32loader: A Unix friendly Bootloader: stm32loader.py ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Author: Ivan A-R * With hacky error recovery by Gordon Williams * Project page: http://tuxotronic.org/wiki/projects/stm32loader * The version here :download:`stm32loader.py` was kindly cleaned up by McKinnon Ryall vatrat.dev@gmail.com. :: # python ./stm32loader.py -h Usage: ./stm32loader.py [-hqVewvrX] [-l length] [-p port] [-b baud] [-a addr] [file.bin] -h This help -q Quiet -V Verbose -e Erase -w Write -v Verify -X Reset after -r Read -l length Length of read -p port Serial port (default: first USB-like port in /dev) -b baud Baud speed (default: 115200) -a addr Target address -s n Skip writing N bytes from beginning of the binary (does not affect start address) -k Change PCLK frequency to make USB stable on Espruino 1v43 bootloaders ./stm32loader.py -e -w -v example/main.bin :: 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 :ref:`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 :ref:`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 :ref:`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 :ref:`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-jumpers: 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 | +--------+--------+--------------------------------+ Saving a on chip binary ----------------------- Saving 645kB of flash to "saved-binary.bin" :: ./stm32loader.py -r -a 0x08000000 -l 0x10000 -p /dev/ttyUSB0 saved-binary.bin .. _stlink-howto: Stlink ------ Howto flash Mecrisp-Stellaris using Stlink ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1) Download the latest Mecrisp-Stellaris tarball: http://mecrisp.sourceforge.net/ 2) Extract the binary for your MCU, in my case it's: mecrisp-stellaris-stm32f051.bin 3) Install Stlink * FreeBsd and OpenBsd: devel/stlink * Linux: You may need to compile it with Linux Debian as they don't seem to have a stlink.deb. See https://github.com/texane/stlink 4) Connect your Discovery board to your PC using a usb cable, a led for the board power should light. **Note: Root or sudo will be required for the erase and write steps below** 5) Erase the flash on the board by running .. code-block:: none st-flash erase It's advised to press the reset button on the board before the next step. 6) Now flash the board with the Mecrisp-Stellaris binary, remember to use the binary for *your* MCU .. code-block:: none st-flash write mecrisp-stellaris-stm32f051.bin 0x08000000 7) Connect a 3.3v-USB dongle to your PC and make sure it works in a serial terminal emulator such as "Picocom", "Minicom", "Cutecom" etc by connecting the 3.3v TX and RX wires and observing that keys are echoed to the screen when hit. See this :ref:`table` for which pins to use for your board/chip. Connection config is: 115200 baud, 1 stop bit, no parity bit, and no handshaking, no xon/off. Below is a picocom example but your /dev/cuaU0 device will probably be different to mine. .. code-block:: none picocom -b 115200 /dev/cuaU0 –imap lfcrlf,crcrlf –omap delbs,crlf 8) Connect the TX, RX and GND wires from the 3.3v-USB dongle to your Discovery board, which pins are used depends on the board/MCU so check the pin numbers in the "Connecting a Serial Terminal" Section in this documentation. 9) When all is working, pressing the reset button on your Discovery Board will result in a similar message being sent to your screen. .. code-block:: none Mecrisp-Stellaris 2.3.6 with M0 core for STM32F051 by Matthias Koch FINISHED! at this point Mecrisp-Stellaris Forth is running on your Microcomputer. While Picocom is great for a quick test, I recommend that you use :ref:`E4thcom` as it is specifically made for programming and uploading files to a Forth Micocomputer. .. index:: df-util, Unhandled Interrupt 00000003 .. _df-util: DF-UTIL ------- Df-util is a host side implementation of the DFU 1.0 and DFU 1.1 specifications of the USB forum. DFU is intended to download and upload firmware to/from devices connected over USB. It ranges from small devices like micro-controller boards to mobile phones. Using dfu-util you can download firmware to your DFU-enabled device or upload firmware from it. Example usage ^^^^^^^^^^^^^ Flashing a .dfu (special DfuSe format) file to the device ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: $ dfu-util -a 0 -D /path/to/dfuse-image.dfu Reading out 1 KB of flash starting at address 0x8000000 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: $ dfu-util -a 0 -s 0x08000000:1024 -U newfile.bin Flashing a binary file to address 0x8004000 of device memory and ask the device to leave DFU mode ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: $ dfu-util -a 0 -s 0x08004000:leave -D /path/to/image.bin Unhandled Interrupt 00000003 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. warning:: I have observed that failure to fully erase the Flash memory prior to flashing Mecrisp-Stellaris often results in endless "Unhandled Interrupt 00000003" messages, this can happen with df-util because the erase-flash command is undocumented for fear of people bricking their devices. How To Erase Using Df-util ^^^^^^^^^^^^^^^^^^^^^^^^^^ https://forum.mystorm.uk/t/how-to-clear-all-flash/373 (erikolson) Here's how I flash with erase: :: dfu-util -a 0 -d 0483:df11 -s 0x8000000:mass-erase:force -D mecrisp-stellaris-stm32l433.bin -t 1024 You can "upload" the flash to your desktop to see if you do have unwanted flash content: :: #!/bin/bash # verify upload rm current.bin dfu-util -a 0 -d 0483:df11 -s 0x8000000 -U current.bin od -x mecrisp-stellaris-stm32l433.bin > in.txt od -x current.bin > out.txt sdiff -w 216 -s in.txt out.txt | more This is also useful if you suspect corruption which happens if the upload is not a 4-byte multiple. 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 .. image:: mini-stm32F103C8T6-board-jtag-serial-1.jpg Open a Root Xterm to run OpenOCD. Leave it running until MCU is flashed: .. literalinclude:: openocd-jtag-f103-example.txt Open a Telnet session to localhost 4444, which is how the user talks to OpenOCD .. literalinclude:: openocd-jtag-f103-example-telnet-4444.txt Check how much Flash you have .. literalinclude:: openocd-jtag-f103-example-flash-probes.txt Check if the Flash is erased .. literalinclude:: openocd-jtag-f103-example-flash-erase-check-0.txt Mass erase all the Flash .. literalinclude:: openocd-jtag-f103-example-stm32f1x-mass_erase_0.txt Check the Flash is really erased .. literalinclude:: openocd-jtag-f103-example-flash-erase-check-0-erased.txt Flash Mecrisp-Stellaris onto the STM32F103C8T6 .. literalinclude:: openocd-jtag-f103-example-flash-mecrisp-stellaris.txt Check the Flash, how much space did Mecrisp-Stellaris use up ? .. literalinclude:: openocd-jtag-f103-example-flash-erase-check-0-after-mecrisp-stellaris-flash.txt 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. .. literalinclude:: openocd-jtag-f103-example-flash-with-script.txt 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 .. literalinclude:: openocd-jtag-f103-example-mecrisp-stellaris-running.txt .. seealso:: PROBLEMS ? :ref:`See This Page`