/!\ Vorläufig / Preliminary, work in progress /!\

STM32F103 Boards

From company STMicroelectronics there is a powerful low cost ARM®-based 32-bit MCU with 64 or 128 KB Flash, USB, CAN, 7 timers, 2 ADCs, 9 com. interfaces, named STM32F103x8 (64 KByte Flash ROM) and STM32F103xB (128 KByte Flash ROM).

It is interesting that some (most?) STM32F103x8 also have a 128 KByte Flash ROM, explained later.

There are 2 popular boards (3 - 5 EUR) on the market (Ebay, banggood.com, etc.):

Unfortunately boards delivered from China have usually very little documentation for the usage.

I like to explain the usage with the Arduino IDE (version 1.8.5). You find a here Links #4 a Arduino for STM32 Forum.

/!\ I am using the software under Mac OS 10.13.4. The usage should be similar to Linux.

/!\ A mouse click on a picture will expand the picture for better viewing.

/!\ A mouse click on a source program will show the listing in a new window with colour coding.


Blue Pill

A good description is at Wiki stm32duino Blue Pill.

A German description of the Blue Pill at Links #3.


Programming the STM32

  1. Programming with the STLink V2 interface, for ordering see at Banggood for the title "3.3V 5V XTW ST-LINK V2 STM8 / STM32 Simulator Programmer" (about 3 EUR). See the picture on the right, with the 4 wire cable to connect the STM32 board.

  2. With the STLink V2 interface you can use the software openOCD (On Chip Debugging) for debugging and programming.

  3. If the Boot Loader generic_boot20_pc13.bin is installed, you can program the STM32 via USB port with the Arduino IDE.

Following the different methods are explained in detail.

The STMLink V2 interface is connected with 4 wires to the STM32 board programming connector. It also supplies the 3V3 voltage.

# Install on Mac OS with Homebrew (version 1.5.0)
$ brew install stlink
# show manual of flash program
$ man st-flash

# Other checks:
$ st-info --probe
Found 1 stlink programmers
 serial: 543f6b06506650511649173f
openocd: "\x54\x3f\x6b\x06\x50\x66\x50\x51\x16\x49\x17\x3f"
  flash: 131072 (pagesize: 1024)
   sram: 20480
 chipid: 0x0410
  descr: F1 Medium-density device

$ st-info --flash
0x20000           # 131072 bytes

Another documentation for Linux at Links #2 from Roger Clark.

Boot Loader

The board is shipped with a Blink programm in the Flash-ROM, but it no Boot Loader programmed, which enumerates the USB port for Arduino programming and Serial I/O.

# Download Boot Loader file to actual directory, see at "Features" for the Link
# Set Jumper BOOT0 to 1
$ st-flash write generic_boot20_pc13.bin 0x8000000
st-flash 1.4.0
2018-05-02T06:33:00 INFO usb.c: -- exit_dfu_mode
2018-05-02T06:33:00 INFO common.c: Loading device parameters....
2018-05-02T06:33:00 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
2018-05-02T06:33:00 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x20000 bytes (128 KiB) in pages of 1024 bytes
2018-05-02T06:33:00 INFO common.c: Attempting to write 21140 (0x5294) bytes to stm32 address: 134217728 (0x8000000)
Flash page at addr: 0x08005000 erased
2018-05-02T06:33:00 INFO common.c: Finished erasing 21 pages of 1024 (0x400) bytes
2018-05-02T06:33:00 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id
2018-05-02T06:33:00 INFO flash_loader.c: Successfully loaded flash loader in sram
 21/21 pages written
2018-05-02T06:33:01 INFO common.c: Starting verification of write complete
2018-05-02T06:33:02 INFO common.c: Flash written and verified! jolly good!

# Remove STLink cable
# Set Jumper BOOT0 to 0

# Connect Micro USB cable to PC
# Check for USB device
$ ls /dev/cu.u*
# Now Arduino IDE programming is possible 


The openOCD software must be used with the STLink V2 interface. The first application is the check for 128KB Flash-ROM in the STM32F103x8 (from data sheet just 64 KB).

See at mikrocontroller.net for the details.

Program openOCD opens a Telnet port 4444 for remote controlling. Start with Terminal #1.

# Install openOCD software with Homebrew
$ brew install openOCD
Installing dependencies for open-ocd: libusb-compat, libftdi, hidapi
 /usr/local/Cellar/open-ocd/0.10.0: 632 files, 4.7MB

# Edit /usr/local/Cellar/open-ocd/0.10.0/share/openocd/scripts/target/stm32f1x.cfg
flash bank $_FLASHNAME stm32f1x 0 0x20000 0 0 $_TARGETNAME
adapter_khz 950

# Press Reset on the board

# Start openOCD Telnet server
$ openocd -f /usr/local/Cellar/open-ocd/0.10.0/share/openocd/scripts/interface/stlink-v2.cfg -f /usr/local/Cellar/open-ocd/0.10.0/share/openocd/scripts/target/stm32f1x.cfg
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 950 kHz
adapter_nsrst_delay: 100
none separate
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.247246
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
# waiting for connection via telnet
Info : accepting 'telnet' connection on tcp/4444
# reset halt
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x1ffff020 msp: 0x200001fc
# flash probe 0
Info : device id = 0x20036410
Info : ignoring flash probed value, using configured bank size
Info : flash size = 128kbytes
flash 'stm32f1x' found at 0x08000000
wrote 120832 bytes from file /Volumes/DAT/Users/rudi/STM32_JPG_121K.bin in 6.591493s (17.902 KiB/s)
dumped 131071 bytes in 2.795402s (45.789 KiB/s)

# to finish Telnet server press key CTRL C

Prepare a JPG picture with a size of about 120KB (< 128 KB), and rename to .bin (e.g. STM32_JPG_121K.bin).

Open Terminal #2 for control.

# type the commands after the ">" prompt.
# See in Terminal #1 for the answers.

$ brew install telnet # only once
$ telnet localhost 4444
Trying ::1...
telnet: connect to address ::1: Connection refused
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger

> reset halt
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x1ffff020 msp: 0x200001fc
> flash probe 0
> flash write_image erase /Volumes/DAT/Users/rudi/STM32_JPG_121K.bin 0x08000000
> dump_image /Volumes/DAT/Users/rudi/STM32_JPG_121K.jpg 0x08000000 0x1ffff
> exit

# check if dumped image is OK, then the Flash-ROM has 128Kb.

# Restore the Boot Loader

Arduino IDE

Install the STM32 addition:

Board: "Generic STM32F103C series" 
Variant: "STM32F103C8 (20k RAM. 64k Flash)"  or 128k
CPU speed (MHz): "72Mhz (Normal)"
Upload method: "STM32duino bootloader"
Optimize: "Smallest (default)"
Port: "/dev/cu.usbmodem14321 (Maple Mini)"

Hardware issue

The USB standard requires a 1.5 kΩ pullup resistor on D+, but this board is known to have a wrong value (R10 = 10K).

If the USB connection works in your case, it might be OK, otherwise change R10 to a SMD 0805 1.5 KOhm resistor.


Leaf Maple Mini

See the documentation at STM32duino.

The Maple Mini is the second of the two original STM32F103 boards made originally by LeafLabs. It was released in 2011. The original Maple Mini is no longer available, but several companies make clones, which has virtually identical functionality.

Access to Maple Mini

The Maple Mini clone should come with a flashed Boot Loader, and should enumerate via USB port.

Unfortunately not. What is next: try with STLink V2 to flash a bootloader. Also not possible.

Next an USB-UART converter on STM32 port RX1 and TX1, see Maple Mini bootloader flashing. Now I got access to the Maple Mini clone, but it did not flash, see:

Got NACK from device on command 0x43  
Can't initiate chip mass erase!
Failed to erase memory

An internet search brought Re: Can't initiate chip erase. You first have to Disable the flash read-protection, then you can flash the STM32. Now the handling in the working sequence with an 3V3 USB-UART converter:

# Connect at Maple Mini port BOOT1 J2,4 to GND
# USB-UART - Maple mini
  RXD  -     RX1 J1, 6
  TXD  -     TX1 J1, 7
  3V3  -     VCC
  GND  -     GND
# Note: RXD, TXD may be swapped, depending on the type of the USB-UART converter

# Bring the STM32 into flash mode (DFU upload mode):
  Press RESET + BOOT0 button -> release RESET -> release BOOT0

# Disable the flash read-protection
$ stm32flash  -k /dev/cu.SLAB_USBtoUART # device name may be different
stm32flash 0.5

Interface serial_posix: 57600 8E1
Version      : 0x22
Option 1     : 0x00
Option 2     : 0x00
Device ID    : 0x0410 (STM32F10xxx Medium-density)
- RAM        : 20KiB  (512b reserved by bootloader)
- Flash      : 128KiB (size first sector: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Read-UnProtecting flash

# Flash the Boot Loader, to allow access via USB port
$ stm32flash  -w maple_mini_boot20.bin /dev/cu.SLAB_USBtoUART
stm32flash 0.5
Using Parser : Raw BINARY
Interface serial_posix: 57600 8E1
Version      : 0x22
Option 1     : 0x00
Option 2     : 0x00
Device ID    : 0x0410 (STM32F10xxx Medium-density)
- RAM        : 20KiB  (512b reserved by bootloader)
- Flash      : 128KiB (size first sector: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Write to memory
Erasing memory
Wrote address 0x0800501c (100.00%) Done.

# remove all connections and plug in a Mini USB cable to the PC
# Check for USB port access
$ ls /dev/cu.u*
/dev/cu.usbmodem143141  # USB enumeration works now, OK

# Now the Arduino IDE access works

Arduino IDE

Install the STM32 addition:

Board: "Maple Mini" 
CPU speed (MHz): "72Mhz (Normal)"
Bootloader version: "Bootloader 2.0 (20K RAM, 120k Flash)"
Optimize: "Smallest (default)"
Port: "/dev/cu.usbmodem143141 (Maple Mini)"

  1. Arduino Software IDE download (Integrated Development Environment), actual version 1.8.5.

  2. Programming an STM32F103XXX with a generic "ST Link V2" programmer from Linux

  3. Keine bittere Pille - die Blue Pill mit ARM Cortex M3

  4. Wiki stm32duino Blue Pill documentation

  5. stm32duino Arduino Forum

  6. Explanation for the DFU upload mode

{i} Go to Top of the page

Contact Email

Please enter your Email address, if you expect an answer.

/!\ The entered Email address will not be published, or given away.

Comment this page
Are you human?

List of pages in this category:

-- RudolfReuter 2018-05-03 18:02:24

Go back to CategoryTechDoc or FrontPage