Let it burn, then press CTRL-C Dec 2016

We’re about to try out Forth on real hardware. The implementation used here at JeeLabs is Mecrisp Forth by Mattias Koch. It’s fully open source (GPL3), it’s well-supported, it’s robust, it’s available on a range of platforms, and it’s documented (very concisely, as usual in Forth).

There’s a firmware image of Mecrisp Forth for F103 built into Folie, which makes it easy to get started. If you’ve been using Folie, you’re just 3 steps away from installing Forth on a Blue Pill. It’s particularly effortless if you’ve made yourself a SerPlus, as described in a recent article:

(Note: If you only see dots appearing, reset the target Blue Pill to kick the upload into action)

With other USB-serial adapters, you’ll need to put the Blue Pill in boot mode and restore/reset once the upload finishes. Apart from that, the process should be the same as described above. Don’t forget to specify the “-r” flag to Folie when not using the Telnet-aware SerPlus.

Here is a transcript of the entire process when using a SerPlus:

$ folie
Folie v2.7-1-g94cba5e
Select the serial port:
  1: /dev/cu.Bluetooth-Incoming-Port
  2: /dev/cu.usbmodem3430DC31
? 2
Enter '!help' for additional help, or ctrl-d to quit.
[connected to /dev/cu.usbmodem3430DC31]
!u 3
  20500b .+V22 #0410 R .W .E writing: 81/81 done.
Mecrisp-Stellaris RA 2.3.1 for STM32F103 by Matthias Koch
Erase block at  00005004  from Flash
Finished. Reset Mecrisp-Stellaris RA 2.3.1 for STM32F103 by Matthias Koch

The erase and duplicate welcome greeting only appear just after being re-flashed. After that, pressing reset (or hitting CTRL-C when using SerPlus) will show the above greeting once.

We’re in business! - Forth is now at your command (prompt) …

Go ahead, try a few things. Type this text (hitting ENTER sends it to the F103):

1 2 + . <enter>

Note that in Forth, the prompt is called “ok.” and that it usually appears at the end of the previous line. Also note how output shows up after the command, i.e. on the same line.

A good word to learn and use often, is “.s”, which displays the contents of the data stack:

.s <enter>
1 2 .s <enter>
+ .s <enter>
. <enter>
.s <enter>

Or all on one line:

.s 1 .s 2 .s + .s . .s <enter>

Here’s how to produce a huge list of all the words Forth currently knows about:

words <enter>

Sample output, shortened for the sake of brevity (the full output is hundreds of lines long):

Address: 00000150 [...] Name: --- Mecrisp-Stellaris Core ---
Address: 0000058C [...] Name: 2dup
Address: 000005B0 [...] Name: 2drop
Address: 000005CE [...] Name: 2swap
[...]
Address: 00004D4A [...] Name: irq-tim7
Address: 00004D72 [...] Name: irq-usbfs
Address: 00004D9A [...] Name: --- Flash Dictionary ---
 ok.

Now is a good time to revisit the Forth in 7 easy steps article and to browse through Mecrisp’s online glossary. Go ahead, try it - if the µC stops working, simply reset it to get a prompt back.

To get some feel for Forth – once you know the basics – check out Sam Falvo’s 1-hour video. He tackles a problem which is not so relevant for embedded computing, but his explanations, programming style, and way of creating a solution are good examples of advanced Forth use.

One thing you’ll notice is that there are no definitions for controlling GPIO pins, turning LEDs on and off, or for any of the built-on hardware peripherals such as I2C and SPI. The Mecrisp Forth core is just the base system: a command line, all the essential words needed to code in Forth, plus some more which are useful but not specific to the F103 ARM microcontroller.

There’s a lot packed into this Mecrisp core, including an on-the-fly compiler to ARM machine code with some fairly advanced optimisations such as constant folding and register allocation. But for convenience, we’ll first need to load a bit more Forth code in flash. Coming up next…

Weblog © Jean-Claude Wippler. Generated by Hugo.