Enabling USB on a Blue Pill Sep 27, 2017

As you may have guessed from my last post, I’ll be using the Blue Pill STM32F103 µC board for some JET engine experiments. It’s available on eBay, etc (for less than $3).

With the USB console working very nicely in Mecrisp Forth now, this board plugs right in - no USB-to-serial adapter needed.

There’s just a li’l problem: getting started!

The trouble with these F103 boards is how to get Mecrisp on there in the first place. As described in probably dozens of posts on this weblog, there are numerous ways to get there - ehm… except that they tend to suffer from their own chicken-and-egg problems!

Since I’m tired of having to look up the solution every time I want to start using another Blue Pill (a 10-pack on eBay is cheaper than an Arduino Uno, go figure!), this post is meant to document a robust way to perform this first upload. If the Blue Pill gets bricked later on (not impossible, even with Mecrisp), then this recipe can be applied again. It works for me every time, and uses only off-the-shelf (eBay) parts:

Again: there are several other ways to re-flash an STM32 µC, but this one has the advantage of not requiring any soldering, bootstrap software, or other things which can get lost or become obsolete over time.

Install the stlink tool, see this README.

Plug in the ST-Link by itself, and enter the cmd “st-info --probe”. You should get:

$ st-info --probe
Found 1 stlink programmers 

Good, the software is ready.

Step 2: Blue Pill

Unplug the ST-Link, and connect it to the Blue Pill with four wires, as shown below:

Once double checked, plug it all back in.

Enter “st-info --probe” again:

$ st-info --probe
Found 1 stlink programmers
 serial: 513f7006483f50512744033f
openocd: "\x51\x3f\x70\x06\x48\x3f\x50\x51\x27\x44\x03\x3f"
  flash: 65536 (pagesize: 1024)
   sram: 20480
 chipid: 0x0410
  descr: F1 Medium-density device

Excellent, the hardware connection is ok!

Step 3: Re-flashing

The last step is to actually upload the firmware into the STM32F103:

st-flash --reset write usb-common.bin 0x8000000

Here is a full transcript of a successful run:

$ st-flash --reset write usb-common.bin 0x8000000
st-flash 1.4.0
2017-09-13T02:40:14 INFO src/common.c: Loading device parameters....
2017-09-13T02:40:14 INFO src/common.c: Device connected is: F1 Medium-density device, id 0x20036410
2017-09-13T02:40:14 INFO src/common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x10000 bytes (64 KiB) in pages of 1024 bytes
2017-09-13T02:40:14 INFO src/common.c: Attempting to write 47104 (0xb800) bytes to stm32 address: 134217728 (0x8000000)
Flash page at addr: 0x0800b400 erased
2017-09-13T02:40:16 INFO src/common.c: Finished erasing 46 pages of 1024 (0x400) bytes
2017-09-13T02:40:16 INFO src/common.c: Starting Flash write for VL/F0/F3/F1_XL core id
2017-09-13T02:40:16 INFO src/flash_loader.c: Successfully loaded flash loader in sram
 46/46 pages written
2017-09-13T02:40:17 INFO src/common.c: Starting verification of write complete
2017-09-13T02:40:18 INFO src/common.c: Flash written and verified! jolly good!

That’s it. Now plug in the Blue Pill via its micro-USB connector (if you leave the ST-Link attached and don’t power-cycle the Blue Pill, you must press its reset button!):

$ folie -r
Select the serial port:
  1: /dev/cu.Bluetooth-Incoming-Port
  2: /dev/cu.usbmodem3401AF11
Enter '!help' for additional help, or ctrl-d to quit.
[connected to cu.usbmodem3401AF11]
1 2 + . 3  ok.
hello 64 KB <g6u> 3401AF14 ram/flash: 16784 18408 free  ok.

(or use whatever terminal app you prefer…)

You can type “list” or “words” to see all the definitions available in this firmware.

Since this is USB 2.0, all communication is much faster than a 115,200 baud serial link.

Note that this version includes a bunch of utility code from the Embello project. You can safely type eraseflash to free up some flash space, because this word has been re-defined to protect the USB driver itself.

We’re good to go: Forth on an STM32F103!

PS. If you have problems starting Mecrisp, use “st-flash erase“ to completely wipe all flash memory, then upload again.

Weblog © Jean-Claude Wippler. Generated by Hugo.