
CForth for STM32G431, STM32G441 and STM32F042
CForth is a simple 32bit Forth interpreter system completely written in C language.
(no relationship to "Mitch Bradley's CForth")
The intention is to provide a straightforward user oriented Forth system,
specifically to organize interactive electronic control of art objects and installations or similar process control.
Primarily CForth was developed for the 32pin LQFP STM32G431KB and STM32G441KB processor.
I am a fan of these compact, high featured and easy to solder chips. Though originally bought for another project, the Corona 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 Amiga 68000, 68HC11 and AT90/ATmega (a recently updated version is published at this website: "AVISE = AVR VIrtual Stack Engine". Early 2000 I wrote a Windows console Forth 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.
This has been enhanced to a LQFP48 version for STM32G4xx processors. It is a simplified "Black Pill" compatible PCB layout. Because it is made with home-makers technologies, the USB connector, the reset button and the BOOT0 button could not be integrated on the PCB. Therefore it is called "Grey Pill".
All G4xx firmware versions include a combined RS-232/MIDI interface, an SPI interface, up to 4 PWM pulse outputs, motor drivers, an I2C interface and a random number generator. Furthermore, an optional DMX512 output is added.
All of these features are accessed directly with specialized CForth kernel operators (Forth "words").
Actually, a specific version for STM32G431 Nucleo32 completed with an easy to build support Veroboard PCB including DMX512 output has been added. A very similar Veroboard PCB with STM32G431 on DIL32 breakout board and all specific details and downloads are described in the link above. Firmware features of these boards are almost the same as the G431(LQFP32) version described here, except MIDI/RS-232 interface.
As a parallel project, firmware for STM32F042 and Veroboard compatible PCB layouts for 20pin TSSOP STM32F042F6 and 32 pin LQFP STM32F042K6 are developed. The 20 pin TSSOP version also works with STM32F070F6. Differing from it's data sheet, PB8 there works as normal I/O. Has been tested here with different CPU production dates, but cannot be guaranteed, of course.
Actually, a specific version for STM32F042 Nucleo32 has been completed. Some hints to configure this module for best use with CForth, see bottom of this page
The small memory size (Flash as well as SRAM) of the STM32F042 processors demands some restrictions concerning number of kernel operators, max. size of user programs, available I/O features.
To open this bottleneck a little bit, the threaded code for these processors is organized in a compact 8 bit array. This provides up to 3 times more Forth code compared with a straight 32bit word oriented code thread (which is used for the STM32G4 processors). The additional runtime overhead is moderate.
All F042 firmware versions include a RS-232 interace (except Nucleo), an SPI interface, a random number generator and optionally a MIDI interface or instead one or two PWM pulse outputs, optionally a motor driver.
All of these features are accessed directly with specialized CForth kernel operators (Forth "words").
Veroboard compatible PCB designs:


Both boards have an optional MIDI IN. The STM32G431 version is equipped with an optional DMX512 output and an optional switching regulator for external power supply.
CForth has been ported to other STM32 projects and processors. These projects are described on individual HTML pages:
Most essential features of the CForth kernel:
A detailled CForth Operating Manual (PDF file), and
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 actual firmware is programmed with "Segger Embedded Studio 5.44".
Firmware "CForth-G4xx-48.hex" for Veroboard compatible PCB, LQFP32, SysClock = 48MHz
Firmware "CForth-G4xx-144.hex" for Veroboard compatible PCB, LQFP32, SysClock = 144MHz
Firmware "CForth-G4xx-DMX48.hex"
Firmware "CForth-G4xx-DMX144.hex"
Firmware "CForth-G4xx-Grey48" (LQFP48 "Grey Pill")
Firmware "CForth-G4xx-Grey144.hex" (LQFP48 "Grey Pill")
Firmware "CForth-G4xx-GreyDMX48.hex" (LQFP48 "Grey Pill")
Firmware "CForth-G4xx-GreyDMX144.hex" (LQFP48 "Grey Pill")
Source code "CForth-G4xx-v63.zip"
This firmware has essentially the same features as the standard STM32G431 version.
Firmware versions for unmodified Nucleo boards are provided, too.
Minimal breadboard setup with STM32F042 Nucleo32 and simple support PCB..
specific "doAll" runtime operators are introduced for loops, conditionals, VARCONs. So I think, I have got an acceptably fast and rather easy to understand and to manage Forth system.
This way VARCONs 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) is displayed via terminal during runtime of the main thread. The background process can be switched off, to get approx. 25% higher execution speed.
Primitive startup example: Pin4(GND) is connected with the GND rail of the breadboard. The LED is connected with GND and via resistor with pin1(PA9). CForth command "9 oh <CR>" makes the LED ON, command "9 ol <CR>" makes is OFF. Details about commands and I/O numbering see CForth manual. The USB cable connects the Nucleo via virtual COM with termial software on the PC. More details about best Nucleo configuration, see below
A hardware DIY Construction Manual for STM32G431/G441 and STM32F042 (HTML file) is provided here.
The subsequently downloadable material is copyrighted (c)2020-25 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 (Segger GmbH, STM) 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.
(08 February 2025 - code improvement and minor bugfixes)
(08 February 2025 - code improvement and minor bugfixes)
with a set of DMX512 kernel operators for Veroboard compatible PCB, LQFP32, SysClock = 48MHz
(08 February 2025 - code improvement and minor bugfixes)
with a set of DMX512 kernel operators for Veroboard compatible PCB, LQFP32, SysClock = 144MHz
(08 February 2025 - code improvement and minor bugfixes)
for Black Pill compatible Veroboard PCB, LQFP48, SysClock = 48MHz
(08 February 2025 - code improvement and minor bugfixes)
for Black Pill compatible Veroboard PCB, LQFP48, SysClock = 144MHz
(08 February 2025 - code improvement and minor bugfixes)
with a set of DMX512 kernel operators for Black Pill compatible Veroboard PCB, LQFP48, SysClock = 48MHz
(08 February 2025 - code improvement and minor bugfixes)
with a set of DMX512 kernel operators for Black Pill compatible Veroboard PCB, LQFP48, SysClock = 144MHz
(08 February2025 - code improvement and minor bugfixes)
(state 08 February 2025) This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 988 kB).
Parts published by Segger GmbH 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-F042LQFP-pwm2.hex" for 32pin LQFP Veroboard compatible PCB, SysClock = 48MHz.
Kernel Ops for SPI and PWM1,PWM2 included. USART can operate as terminal or as serial I/O
(08 February 2025 - code improvement and minor bugfixes)
Firmware "CForth-F042-pwm2.hex" for 20pin TSSOP Veroboard compatible PCB, SysClock = 48MHz.
Kernel Ops for SPI and PWM1,PWM2 included. USART can operate as terminal or as serial I/O
(08 February 2025 - code improvement and minor bugfixes)
Firmware "CForth-F042LQFP-motor.hex" for 32pin LQFP Veroboard compatible PCB, SysClock = 48MHz.
Kernel Ops for SPI, stepper motor and PWM1 included. PWM2 not implemented. USART can operate as terminal or as serial I/O
(08 February 2025 - code improvement and minor bugfixes)
Firmware "CForth-F042-motor.hex" for 20pin TSSOP Veroboard compatible PCB, SysClock = 48MHz.
Kernel Ops for SPI, stepper motor and PWM1 included. PWM2 not implemented. USART can operate as terminal or as serial I/O
(08 February 2025 - code improvement and minor bugfixes)
Firmware "CForth-F042LQFP-midi.hex" for 32pin LQFP Veroboard compatible PCB, SysClock = 48MHz.
Can operate USB and the serial I/O as conventional RS-232 or as MIDI interface.
Instead no SPI, motor and PWM code implemented.
(08 February 2025 - code improvement and minor bugfixes)
Firmware "CForth-F042-midi.hex" for 20pin TSSOP Veroboard compatible PCB, SysClock = 48MHz
Can operate USB and the serial I/O as conventional RS-232 or as MIDI interface.
By jumper, this board can be started as a USB-to USART RS232 interface or into a USB-to conventional USART MIDI interface. Details see CForth Operating Manual p.14.
Instead no SPI, motor and PWM code implemented.
(0488 February 2025 - code improvement and minor bugfixes)
Firmware "CForth-F042-mini.hex" This special 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 this code is the same as "CForth-F042-midi.hex". SysClock = 48MHz.
By jumper, this board can be started as a USB-to USART RS232 interface or into a USB-to conventional USART MIDI interface. Details see CForth Operating Manual p.14.
(08 February 2025 - code improvement and minor bugfixes)
Source code "CForth-F042-v38.zip"
(state 08 February 2025) This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 371 kB).
Parts published by Segger GmbH 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-Nucleo-F042-HSI.hex" This firmware fits for an unmodified Nucleo32 module (default suggestion, but USART may be instable in some cases). SysClock = 48MHz
(08 February 2025 - first release)
Firmware "CForth-Nucleo-F042-HSE.hex" For this code, solder bridge SB4 must be connected. It is not necessary to unsolder SB6. To be used if HSI does not work properly. SysClock = 48MHz.
(08 February 2025 - - first release)
Source code "CForth-Nucleo-F042-v11.zip"
(state 08 February 2025) This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 431 kB).
Parts published by Segger GmbH 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.