Forth over USB on STM32F103 Feb 2017

Mecrisp Forth and its GitHub mirror make a wonderful interactive environment for a range of powerful ARM-based 32-bit µCs. There are a gazillion low-cost STM32F103-based boards available on eBay and AliExpress, such as this one, for example - often called the “Blue Pill”:

But… out of the box, Mecrisp Forth only supports a serial port as console.

Wouldn’t it be nice if we could load an extended version of Mecrisp Forth onto this board, which does console I/O over USB? After all, the connector is there!

If you’ve been following this weblog for a while, you know that such a project was started quite some time ago, based on Eckhart Köppen’s work, and that it hit some snags along the way.

Well, the good news is that all the major bugs have been fixed!

You can now install a USB-enabled version of Mecrisp Forth on a number of different F103-based boards, and it’s quite easy to adapt the code for new boards. The differences are only about how USB enumeration is triggered: some boards needs a specific GPIO pin to be pulled high or low, before USB can be used. Other boards have a fixed 1.5 kΩ pull-up resistor on board and need a different trick in the code.

You can find all the pre-compiled USB images in the suf/ directory of Embello on GitHub - where “suf” stands for “Serial USB Forth”. On many boards, usb-common.hex should work.

The result is very effective: just plug in the board and a serial USB device will appear, to which you can connect using Folie or picocom. This should work on Windows, MacOS, Linux, etc.

There are a few differences to be aware of when using the USB-enabled builds:

Note that when SerPlus is not in the loop, you need to start Folie with the -r (raw) flag.

The connection speed is considerably higher than serial, but what is even more important is that the driver implements bi-directional throttling (i.e. back-pressure): you can send a large Forth source file to the board, and it won’t overrun the interpreter loop. Similarly, you can send output back at maximum speed without losing a single byte.

A quick test, sending some 65 KB of Forth source code to a Blue Pill took 8 seconds. That’s 8 KB/sec, and around 250 lines/second. It may not sound like much, but it’s considerably faster than the ok-prompt-throttled rate you will get with a 115,200 baud USART-based connection.

And as will be described in the next article, Mecrisp Forth does some quite amazing things while it’s reading in all those source lines…

Weblog © Jean-Claude Wippler. Generated by Hugo.