CForth for STM32G431, STM32F042 and STM32L476

CForth is a Forth interpreter system completely written in C language.
The intention is to make a straightforward user oriented Forth system,
specifically to organize interactive electronic control of art objects and installations or similar process control.

Primarily CForth is developed for a 32pin LQFP STM32G431KB processor.
I am a fan of these compact, high featured and easy to solder chips. Though originally bought for another project, the lockout time stimulated me to start this Forth development. My first Forth about 1992 was e-Forth with an 80C31 processor. Later I programmed some Forth for 68HC11 and AT90/ATmega. My last Forth interest was early 2000 a Windows console version written in C, which served as a first template for the actual project.

Because it is complicated to etch PCBs without appropriate tools, a Veroboard compatible PCB layout was developed (processor soldered on DIL breakout board like Adafruit 1163), which is presented here.
An experimental small CForth based USB/DMX512 controller is presented at another place of this website.
All firmware versions include an SPI interface, two PWM pulse outputs, an I2C interface and a random number generator.
Furthermore, an optional DMX512 output for the Veroboard PCB version is added.
The useability of the MIDI interface is improved with the actual firmware (as of Dec.2020).
All of these features are handled with specialized CForth kernel operators (Forth "words").

As a side product, firmware and a Veroboard compatible PCB layout for 20pin TSSOP STM32F042F6 is developed too. It also works with STM32F070F6.
The small memory size (Flash as well as SRAM) demands some restrictions concerning kernel operators and max. size of user programs.
To open this bottleneck a little bit, a specialized firmware compilation without RS-232 terminal but more I/O features and another one with the improved MIDI interface but less I/O features but is provided. Latter firmware is also suitable for the small USB/serial interface described at another place of this website. Native I2C support is impossible with STM32F042F6 because the assigned pins are used for USB.
A minimal Breadboard setup is shown below.

Another side product is a CForth version for STM32L476 Nucleo-64 with ST-LINK cut off. It is optimized for a hardware design described at another place of this website. It has essentially the same features as the standard STM32G431 version, inclusive MIDI I/O. A basic firmware version for an unmodified STM32L476 Nucleo is provided, too.

Last not least a reduced version for an unmodified STM32G431 Nucleo-64 is derived.
But I am very disappointed about this Nucleo, it is an absolute downgrade of the Nucleo concept. Because the ST-LINK unit cannot be cut off and be used as a standalone programmer, it's effectively a playaround gadget and not useful for real projects. If I had known before, I would not have ordered it. Because the firmware modification for this board is quite easy, it is supported too with basic features.

New: For STM32G431 and STM32F042 low power versions are added on this website

Veroboard compatible PCB designs
Veroboard compatible PCB designs for CForth with STM32G431(LQFP32) and STM32F042(TSSOP20)
Both boards have an optional MIDI IN,
the STM32G431 version is equipped with a DMX512 output and a switching regulator for external power supply.

Most essential features of the CForth kernel:

  • A basic set of standard Forth kernel operators ("words") is implemented
  • No attempt is made to comply with established Forth standards. Nevertheless, all typical features of most Forth dialects are supported.
  • No attempt is made to "program Forth by Forth". At the expensive of code length everything is programmed on rather flat subroutine nesting.
  • Where standard Forth systems compile sniblets of assembler code to manage runtime behaviour,
    I have introduced several specific "doAll" runtime operators for loops, conditionals, VARiables and CONSTants. So I think, I have got an acceptably fast and easy to understand and to manage Forth system.
  • Loops are organized more "C-programming style" than in traditional Forth.
  • A simple background process for debugging and parameter optimization is implemented: selected CForth kernel operators can be called via Terminal while another CForth thread is executing.
    This way constants and variables of the main thread can be modified externally during runtime for example, which allows interactive flow control of the executing thread.
    Single step execution can be switched on and off at any time and some debug information (like memory content) can be displayed via terminal during runtime of the main thread. The background process can be switched off, to get approx. 40% higher execution speed.
  • Any Forth User thread can be replaced by a new programmed one without "ripping" up the compiled code memory and complile it new. Change can be undone without reprogramming. This may be useful when a good idea comes later. (Not implemented in the STM32F042 MIDI firmware)
  • Line-end delimiter is "Carriage Return" (ASCII 13) only. To modify it into "Line Feed" (ASCII 10), exchange the numeric values of CRR and LFF in source file "globaldefines.h" and recompile.
F042 Breadboard minimal
Minimal breadboard setup with STM32F042. Colors of wires see schematic. The reset button is replaced by a yellow wire, which is plugged into the ground rail to "Connect" ST-Link and removed when "Connect" is done for firmware programming and CForth operation.
Most times C2 and C3 are not necessary for this processor type.
Operation and CForth programming is done via USB virtual COM port. Additional components may be added, see DIY manual.

A detailled Forth Operating Manual (PDF file), and
a DIY Construction Manual (PDF file) is provided here.

A good tool for Forth source code upload is my DTerm Windows terminal software. It is surely not the best terminal emulator, but has been developed over the years for best fitness with my actual projects.

Downloads:
The subsequently downloadable material is copyrighted (c)2020 by Wolfgang Schemmert.
Assembly of the devices, programming and use of the software is permitted for free by everybody for any purpose ("freeware"). For commercial use, restrictions of third-party software contributors must be respected.
All information is based on best knowledge, but "as is" and without any warranty. Any responsibility is excluded. Use for dangerous, life-threatening and medical applications is forbidden.

The actual firmware is programmed with "Segger Embedded Studio 4.52". Ports to "Embitz 1.11" are available on demand.

PCB layout for the "STM32G431 board" PCB (1:1 TIF format. BOTTOM layer as well as component placement view are "from component side", i.e. BOTTOM layer shown mirrored "through the PCB" (as needed for PCB production))

PCB layout for the "STM32F042 board" PCB (1:1 TIF format. BOTTOM layer as well as component placement view are "from component side", i.e. BOTTOM layer shown mirrored "through the PCB" (as needed for PCB production))

Firmware "CForth-G431-48.hex" for Veroboard compatible PCB, SysClock = 48MHz
(Version 5.51 , 01 January 2021 - bugfix save BaudRate in Flash)

Firmware "CForth-G431-noRS232.hex" for Veroboard compatible PCB, SysClock = 48MHz
Compact version without RS-232 and MIDI interface but a few more I/O
(Version 5.51 , 01 January 2021 - same code as version 5.50)

Firmware "CForth-G431-144.hex" for Veroboard compatible PCB, SysClock = 144MHz
(Version 5.51 , 01 January 2021 - bugfix save BaudRate in Flash)

Firmware "CForth-G431-DMX48.hex"
with a set of DMX512 kernel operators for Veroboard compatible PCB, SysClock = 48MHz
(Version 5.51 , 01 January 2021 - bugfix save BaudRate in Flash)

Firmware "CForth-G431-DMX144.hex"
with a set of DMX512 kernel operators for Veroboard compatible PCB, SysClock = 144MHz
(Version 5.51 , 01 January 2021 - bugfix save BaudRate in Flash)

Source code "CForth-G431.zip" (state 01 January 2021)
This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 652 kB. Parts published by Segger are under license of Segger&|Rowley, parts published by STM are under license of STM, parts programmed by me are provided under GNU GPL3 license.)

Firmware "CForth-F042.hex" for Veroboard compatible PCB, SysClock = 48MHz
(Version 3.25 , 28 December 2020 - minor improvements, new reference hardware)

Firmware "CForth-F042-noRS232.hex" compact version without RS-232 interface but a few more I/O and PWM output. SysClock = 48MHz
(Version 3.25 , 28 December 2020 - minor improvements, new reference hardware)

Firmware "CForth-F042-midi.hex" can operate the serial I/O as conventional RS-232 or as MIDI interface. Instead no SPI and PWM output is implemented. SysClock = 48MHz
(Version 3.25 , 28 December 2020 - minor improvements, new reference hardware)

Firmware "CForth-F042-mini.hex" This firmware fits for the small USB/serial interface, the USART Tx level is not inverted, because this board is equipped with an external CMOS inverter and 5V level shifter. Else the code is the same as "CForth-F042-midi.hex". SysClock = 48MHz.
(Version 2.25 , 28 December 2020 - minor improvements)

Source code "CForth-F042.zip" (state 28 December 2020)
This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 383 kB. Parts published by Segger are under license of Segger&|Rowley, parts published by STM are under license of STM, parts programmed by me are provided under GNU GPL3 license.)

Firmware "CForth-L476-cutoff.hex" for a Nucleo board with the ST-LINK module cut off.
The L476 on-chip USB and USART is supported for communication
(Version 1.22 , 29 December 2020 - minor improvements)

Firmware "CForth-NucleoL476-orig.hex"
reduced version for an original unmodified STM32 Nucleo L476 board
(Version 1.22 , 29 December 2020 - minor improvements)

Source code "CForth-L476.zip" (state 29 December 2020)
This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 411 kB. Parts published by Segger are under license of Segger&|Rowley, parts published by STM are under license of STM, parts programmed by me are provided under GNU GPL3 license.)

Firmware "CForth-NucleoG431-48.hex", SysClock = 48MHz
reduced version for an unmodified STM32 Nucleo G431 board
(Version 1.8 , 28 December 2020 - minor improvements)

Firmware "CForth-NucleoG431-144.hex", SysClock = 144MHz
reduced version for an unmodified STM32 Nucleo G431 board
(Version 1.8 , 28 December 2020 - minor improvements)

Source code "CForth-NucleoG431.zip" (state 28 December 2020)
This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 386 kB. Parts published by Segger are under license of Segger&|Rowley, parts published by STM are under license of STM, parts programmed by me are provided under GNU GPL3 license.)

These projects are programmed directly on register level, no external libraries (except "cmsis", startup code and Segger system code), no external drivers or HAL are used.



* State of information January 2021.
* Right of technical modifications reserved. Provided 'as is' - without any warranty. Any responsibility is excluded.
* This description is for information only. No product specifications are assured in juridical sense.
* Trademarks and product names cited in this text are property of their respective owners.