C Forth for Teensy 3.1

C Forth for Teensy 3.1 has moved to my different repository after rebasing against Mitch Bradley's repository. More work is needed to merge both Mitch's and my Teensy support.

20th October 2016

Since the article below, work on C Forth has proceeded, and the latest C Forth can be found in Mitch Bradley's repository. My repository linked below is much older.

19th April 2016

For people using 64-bit Linux as builder, I've added 32-bit
dependencies in the article below. Thanks to Oliver Ve&ccaron;erník.

11th September 2014

C Forth is an implementation of Forth by Mitch Bradley, optimised for
embedded use in semi-constrained systems such as System-on-Chip
processors. See git.

The Teensy 3.1 is a Freescale MK20DX256 ARM Cortex-M4 with a Nuvoton
MINI54 ARM Cortex-M0 management controller. Paul Stoffregen maintains
a build environment, which can be used with or without an IDE. See git.

An initial build of C Forth, without removing unnecessary features,
used 51 kB out of the available 256 kB FLASH. It requires UART0 at
115200 baud. Source is available in git.

The Teensy 3.1 modules were purchased from OSH Park, but
are also available from Sparkfun and Little Bird Electronics.

Once loaded into the program Flash, C Forth is running whenever the
module is powered. Without any prior configuration, C Forth will
display the system prompt "ok" on the serial port or via USB
serial.

Attach the Teensy 3.1 via USB, and run a terminal emulator. The baud
rate is 115200 baud. For instance, use the Screen program on
Linux:

screen /dev/ttyACM0 115200

Press enter. The "ok" prompt should appear.

ok

Or, connect a 3.3V serial adapter to Teensy pins RX1 (0) and TX1 (1).
C Forth sends output to both TX1 and USB ACM, and accepts input from
both RX1 and USB ACM.

Blinking the LED

The Teensy 3.1 has an LED attached to port 13. Let's light this LED
manually.

First, set the port as an output:

ok #1 #13 m!

This pushes the decimal number one to the stack, then pushes the
decimal number thirteen, then calls the predefined m! (mode store) word which is the equivalent of
the Arduino pinMode function. Both numbers are consumed from the
stack, nothing is left on it.

Now, turn on the port:

ok #1 #13 p!

This pushes the decimal number one to the stack, then pushes the
decimal number thirteen, then calls the predefined p! (pin store) word which is the equivalent of the
Arduino digitalWrite function. Nothing is left on the stack.

Now, turn it off:

ok #0 #13 p!

Notice how #1 is used to turn it on, and #0 is used to turn it off,
but that #13 is used each time.

Word

Stack effect

Meaning

m!

( mode pin# -- )

mode store, set the mode of a pin,zero is input, one is output, two is input with pullup

The non-volatile buffer is run on power up. Now, when the power is
lost and regained, the module will begin blinking the LED
immediately. Serial is no longer required.

If pin 13 is grounded, C Forth will not run the non-volatile buffer.
You can use this to recover from a mistake that would otherwise
prevent further use.

The size of the non-volatile buffer is limited to 2048 bytes, and
there is much more room in the Program Flash memory, so for larger
programs it is easier to extend C Forth. The
non-volatile buffer also has limited endurance, and is harder to edit,
so for best results, extend C Forth.