What is it good for?
The project delivers configurable board support code for selected targets, and docs. Besides its modest size, the TG9541/STM8EF code has a long feature list. Using the code for embedded control applications is subject to new projects.
The code on GitHub can be used in many ways:
- for writing alternative firmware Chinese commodity boards (e.g. thermostats, DCDC converters, or relay boards)
- for embedded systems with an interactive shell (scriptable and extensible)
- for creating smart SPI, I2C, or RS232 smart sensors with a scripting shell, e.g. for RaspberryPi, Arduino, or ESP8266
- as an interactive environment for exploring the STM8 architecture
- for learning Forth. It's easy and fun - find out why in the text below!
- ...
Why a Forth for Cheap Chinese boards?
Because it's fun: cheap mass-produced imperfection is a playground for creativity :-)
Right now, the W1209 is my favorite target: it's a rather complete embedded control board with a UI at a very good price. It's as cheap as it looks, and the challenge is in it's imperfections: the guy who designed the board clearly didn't have a connected application in mind, and I had a lot of fun making it do things it was never intended to do.
There are challenges, like the lack of communication ports. The "sensor connector" can either be used for communicating, or for sensing. What if you need sensing and communication at the same time? Maybe the "update connector" can be used as a home brew field bus interface? A lot is possible with the right idea, and the right software!
Which target boards are supported?
Besides generic CORE target for STM8S003F3P6, there is currently support for the following boards:
- MINDEV STM8S103F3P6 Breakout Board or similar
- W1209 Thermostat Board (3 7S-LED, 3 keys, relay)
- W1401 Thermostat Board (3x2 7S-LED, 4 keys, relay, buzzer)
- C0135 Relay Board-4 (4 inputs, 1 key, 1 LED, 4 relays)
@Elliot Williams worked on using the ESP-14 as an IoT deviced (the ESP-14 is an ESP8266 with an STM8S003F3P6 in a ESP-12 package).
Programmable power supplies based on the XH-M188, and a cheap DC/DC converter are work in progress. There are also several types of STM8S003F3 based voltmeters that can be supported.
Read more about likely future targets below.
Why Forth?
Again, because it's fun!
Consider this:
- compared to other programming environments the core of Forth is easy to fully understand
- like Lisp, Forth has a REPL (Read-Evaluate-Print-Loop) which enables software testing in a way impossible with "Edit-Compile-Run-Debug" (e.g. Arduino)
- it's easy to build Domain Specific Languages (you can literally program the compiler!)
- the stack-centered "factoring" approach provides implicit data flow which leads to maintainable code
- Interpreter-compiler, basic OS functions fit in just 4K code :-)
Forth starts out as a stack machine with a tiny instruction set and minimal hardware requirements. It fits
in a few KiB, and the target, even a lowly µC, can even be used as the development system.
The Forth stack machine is a minimalistic VM on a standard CPU, but there are also hardware implementations (e.g. for FPGAs, or a 144 core Forth processor). The VM is ideal for fast context switching and Forth easily meets hard-real-time requirements. It's no surprise that Forth was used in
many NASA projects.
A Forth programmer is in control of all levels of problem
abstraction, a unique advantage in a world where layer on layer of 2nd
hand solutions leads to ever growing complexity (compilers, libraries, operating systems,
drivers,frameworks, IDEs... ). I'm convinced that "Thinking Forth" will make anybody a better programmer, not just in the
domain of embedded control!
Why STM8S003F3 or STM8S103F3?
Low-end "STM8S Value Line" STM8 µCs are very cheap (less than $0.25 in hobby quantities, $0.20@100pcs)....
Read more »
Thomas mentioned that a better file loader would be nice. Here is my attempt. Simple to start with, but obviously capable of being expanded with features later. It is in Python2 and runs from the command line of the host machine (mine is LinuxMint).
<code>
#!/usr/bin/env python2
import serial
import sys
import time
port = serial.Serial(
port='/dev/ttyACM0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=5)
if len(sys.argv) < 2:
print('Usage %s ... [fileN]' % (sys.argv[0]))
sys.exit()
def upload(path):
with open(path) as source:
for line in source.readlines():
time.sleep(0.2)
line = line.strip()
if not line: continue
if len(line) > 64:
raise 'Line is too long: %s' % (line)
print('\n\rsending: ' + line)
port.write(line)
port.write('\n\r')
chin = ''
response_buffer = []
while chin <> '\v':
response_buffer.append(chin)
while port.inWaiting() > 0:
chin = port.read(1)
response = ''.join(response_buffer)
sys.stdout.write(response)
for path in sys.argv[1:]:
print('Uploading %s' % path)
upload(path)
</code>
Usage: Save this code as a file (say named loadserial.py) and change its permissions to be executable (just the lines in between the code tags). I put loadserial.py in my local /bin folder. Edit loadserial.py so the port matches what you use when using a terminal console to connect to STM8 machine.
WARNING: I've just noticed that the indentation was inconsistently displayed, and python is indentation sensitive. So be very careful with just copy-and-paste. I'll put a copy of it up on RigTig's Big 3d Printer project here on hackaday.io.
Either put FILE on first line of the file to be sent, or type it into a terminal console and close it, then use a local command line interface thus: <code> filename file2send </code>. Enjoy!