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:
plug the SerPlus into USB, and attach the Blue Pill on which you want to install Forth
launch Folie, enter “
!upload” (or “
!u”) to see a list of built-in firmware images:
!upload These firmware images are built-in: 1: F103-BMP 50096b crc:F87A 2: F103-Blink 724b crc:4967 3: F103-Mecrisp 20500b crc:A585 4: F103-SerPlus 7052b crc:7DD0 Use '!u <n>' to upload a specific one.
now enter this command, making sure the number matches what you see in your list:
(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:
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.
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…