CForth Low Power for STM32G431(G441), STM32F042(F070), STM32L031 and STM32F030
The intention for this project is to get a tool for control dynamic art installations powered by small solar panels or other "weak" power sources using the Forth programming language (no relationship to "Mitch Bradley's CForth").
For this reason, primarily reduced versions of the CForth interpreter for STM32G431KB, STM32G441KB and STM32F042 and corresponding Verobard compatible PCB designs are developed.
LowPower Hardware:
First of all the CPU clock speed (STM32 AHB and APB bus) is reduced to about 1MHz (details see below).
A by-result is the 48MHz version for STM32F042. For the price of no-USB it offers more user code space and some more I/O features.
Additionally, attactive CForth Low Power versions for similar processor models are developed:
A new code version is implemented for STM32F030K6 processors (LQFP32), which is very similar to STM32F042. These processors are quite easily availble at low cost, but offer only 4kB SRAM. So this version is only useable for small projects (up to ca.50 user programmed code threads).
New experience has been made with STM32L031K6(LQFP32): this prototype uses only ca.0.2mA with LED OFF at 1.048 MHz clock (0.8mA at 4.194MHz clock). But it has to be noted, that supply current rises remarkably with ADC on (ca. +0.4mA), power via pull-up or pull-down (ca. +0,08mA) and fast pulse out via I/O pin (ca. +0.2mA at 1 kHz PWM, no load). Else this code is very similar to the STM32F042 version.
USB cannot be operated with the low-power clock frequencies. But this is not regarded to be harmful because USB uses a considerable amount of current and Flash memory (latter case is important for the STM32F042 version, there is a permanent struggle byte by byte to fit the code into Flash). The chosen system frequency allows standard RS-232 Baud Rates up to 115200. The RS-232 interface does not draw current except during data transfer. The RS-232 transmitter is extremely simplified, but has been tested with several standard RS-232 receivers.
Another critical aspect of supply current is the quality of the 3.3V regulator. After some tests, I have found out that MCP1702 (ca. 0.01mA ground pin current) is the best solution for most CForth Low Power target projects. But it is specified only up to 13V supply voltage.
A good alternative is LP2950ACZ-3.3 which is specified up to 20V input voltage. It uses about 0.2mA ground pin current, but increasing with higher load. It can be placed into MCP1702 PCB layout turned about 135 degrees counter clockwise. The middle pin of LP2059 is Ground instead of Vin at MCP1702. A pin compatible replacement for LP2950 is STM LE33A, but it uses ca 0.5mA ground pin current.
The LED should be a very efficient blue or white type. To save current, a special CForth kernel operator "LED" is introduced to switch the LED off when not needed. With 10kOhm serial resistor, a blue LED uses about 0.1mA.
Resume of CPU features - processors in RUN mode, current measured behind voltage regulator and LED OFF:
The STM32G431, STM23F042 and STM23F030 prototypes use ca.1.1 to 1.2mA (921kHz system freq. with 3.68MHz crystal). (Ca.1.6 to 1.9mA at 3.68MHz system freq. and ca.1.6 to 1.7mA at 921kHz system freq. with 14.74MHz crystal). The STM32L031 prototype uses only ca. 0.2mA with LED OFF at 1.048 MHz clock (0.8mA at 4.194MHz clock).
Though the STM32G431 surely is the most interesting processor, for the intended range of use the less current hungry STM32L031 or the more compact TSSOP20 STM32F042 seems to be more atttractive.
To get a more straight and better readable text,
---the differing low-power features of the STM32L031 version are described on a separate HTML page.
---and the differing low-power features of the STM32F030 version are described on a separate HTML page.
Next follows description of differing low-power features of the "original" STM32G431 and STMF042 codes:
Veroboard assembly prototypes.
left:STM32G431KBright:STM32F042K6
each soldered on 15.24mm DIL breakout board.
Another Veroboard layout for STM32F042K6 (LQFP32) is provided.
About CForth LowPower Firmware: (STM32G431/G441, STM32F042/F070)
The set of Kernel Operators and their features is almost the same as the standard CForth version, with lower execution speed of course.
In contrast to the straight 32bit code for STM32G431, the thread code storage for STM32F042 processors is compacted to 8bit format, which increases the maximum user Forth code up to 3 times compared with 32 bit code (details see CForth Operating Manual, p1-3).
The code for STM32F042 is 1:1 useable with STM32F070. The code for STM32G431 is 1:1 useable with STM32G441,LQFP32
The most essential differences against standard CForth:
- The basic CPU clock speed is 921.6kHz, switchable by Forth command to 3.6864MHz CPU clock or 14.7456MHz CPU clock - depending on the assembled crystal.
-- Furthermore 48MHz CPU Clock (switchable down to 3.0 MHz) (STM32F042 only, 8MHz crystal HSE). Latter one is not a low power version, may be regarded as a standard CForth version without USB. Supply current ca.12mA (ca.4mA at 3MHz). - STM32G431 and STM32F042 firmware (versions 3M68 and 14M):
Max FREQ is 9.216kHz. "9 FREQ" sets the output frequency to 1024Hz.
PWM can be only adjusted in the range 0...100. - STM32F042 firmware version 48M):
Max FREQ is 30.0kHz. "30 FREQ" sets the output frequency to 1000Hz.
PWM can be only adjusted in the range 0...100. (changed 05Jan24) - Max SPI clock rate is in the order of 50kHz (STM32G4xx)
(STM32F042 only ca 20 kHz - less efficient code due to lack of memory). - The STM32G4xx version supports I2C (PB7,PB8), but only rather low speed clock up to ca. 100kHz.
- An RS-232 interface is available with baud rates 9600,19200,38400,57600,115200.
MIDI and USB is not implemented. - For this reason, the Kernel Operators RX?, TX, TX, TXN, TX", SPLIT, VID, PID and CHANNEL are not included.
- As an option to save current, the Kernel Operator LED is added: LED ( mode --- ).
This operator can be compiled into User Threads for process signalling.
0 LED: LED is always OFF
1 LED: LED is always ON
2 LED: LED goes OFF for ca. 0.25 sec when a data byte is received via RS-232
3 LED = default: LED goes OFF for ca. 0.25 sec when a data byte is received or transmitted via RS-232 - The STM32F042 Low Power version supports 2 projects in Flash, because kernel code takes less Flash segments due to missing USB. Therefore SAVE and LOAD ask the Project number 0 (=Empty), 1, 2 like it is implemented in the G4xx version.
- The SLEEP command has beed added (Nov'23). Supply current in SLEEP mode is a few microAmp. Awake is triggered by a high-to low transient at PA3 or PA15 (changed Jan24) or USART Rx input.
- Though stepper motors surely are no low power devices, simple code for driving stepper motors as background process (external inverter with hiZ switch for 4 phases necessary) is provided. I use steppers from old disk drives together with small solar panels buffered with big electrolytic capacitors and small numbers of steps per capacitor discharge. Due to lack of memory for STM32F042 only available as alternative to PWM2.
Motor output is at PA0 and PA1 for all firmware versions.
A detailled CForth Operating Manual Standard version (PDF file), and
a DIY Construction Manual Low Power version (HTML 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-23 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.
Firmware "CForth-LP-G4xx-3M6.hex" for Veroboard compatible PCB, CPU Clock = 921.6kHz/3.6864MHz switchable. Uses 3.6864MHz crystal.
(09 April 2024 - bugfix)
Firmware "CForth-LP-G4xx-14m.hex" for Veroboard compatible PCB, CPU Clock = 921.6kHz/3.6864MHz/14.7456 switchable. Uses 14.7456MHz crystal.
(09 April 2024 - bugfix)
Source code "CForth-LP-G4xx-v11.zip"
(state 09 April 2024. This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 469 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-LP-F042-3m6pwm2.hex" for 20pin TSSOP Veroboard compatible PCB. CPU Clock = 921.6kHz/3.6864MHz switchable. Uses 3.6864MHz crystal. Code for PWM2.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042lqfp-3m6pwm2.hex" for 32pin LQFP Veroboard compatible PCB. CPU Clock = 921.6kHz/3.6864MHz switchable. Uses 3.6864MHz crystal. Code for PWM2.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042-3m6motor.hex" for 20pin TSSOP Veroboard compatible PCB. CPU Clock = 921.6kHz/3.6864MHz switchable. Uses 3.6864MHz crystal. Code for stepper motor.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042lqfp-3m6motor.hex" for 32pin LQFP Veroboard compatible PCB. CPU Clock = 921.6kHz/3.6864MHz switchable. Uses 3.6864MHz crystal. Code for stepper motor.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042-14pwm2.hex" for 20pin TSSOP Veroboard compatible PCB. CPU Clock = 921.6kHz/14.7456MHz switchable. Uses 14.7456MHz crystal. Code for PWM2.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042lqfp-14pwm2.hex" for 32pin LQFP Veroboard compatible PCB. CPU Clock = 921.6kHz/14.7456MHz switchable. Uses 14.7456MHz crystal. Code for PWM2.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042-14motor.hex" for 20pin TSSOP Veroboard compatible PCB. CPU Clock = 921.6kHz/14.7456MHz switchable. Uses 14.7456MHz crystal. Code for stepper motor.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042lqfp-14motor.hex" for 32pin LQFP Veroboard compatible PCB. CPU Clock = 921.6kHz/14.7456MHz switchable. Uses 14.7456MHz crystal. Code for stepper motor.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042-48pwm2.hex" for 20pin TSSOP Veroboard compatible PCB. Code for PWM2.
Designed for the same hardware as described here and the same firmware features but equiped with an 8.0MHz crystal and uses the processor built-in PLL to work with 48MHz/3MHz AHB and APB frequency.
CPU Clock = 48.00MHz/3.00MHz switchable. Default = 48MHz.
Provides more Flash and some more I/O features than the standard version.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042lqfp-48pwm2.hex" for 32pin LQFP Veroboard compatible PCB. Code for PWM2. Else general description as above.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042-48motor.hex" for 20pin TSSOP Veroboard compatible PCB. Code for stepper motor. Else general description as above.
(09 April 2024 - bugfix and minor improvements)
Firmware "CForth-LP-F042lqfp-48motor.hex" for 32pin LQFP Veroboard compatible PCB. Code for stepper motor. Else general description as above.
(09 April 2024 - bugfix and minor improvements)
Source code "CForth-LP-F042-v12.zip"
(state 09 April 2024). This source code is provided as complete "Segger Embedded Studio" project (ZIP file, 575kB).
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.
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.
* 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.