Close

STM8 eForth Servo Demo Revisited

A project log for eForth for cheap STM8S gadgets

Turn cheap modules from AliExpress into interactive development kits!

thomasThomas 11/17/2017 at 21:170 Comments

Some time ago Manfred Mahlow, the author or e4thcom, and I discussed how to use the e4thcom #include and #require feature for building STM8 eForth applications with the help of a library. One of the challenges when writing an interactive Forth for a µC with relatively little memory is the space the dictionary occupies. Especially symbols for hardware registers consume a lot of memory even if they're only used once (e.g. for initializing a peripheral).

STM8 eForth solves this problem with temporary dictionary entries in RAM which can be removed after appropriate words for using the peripherals have been built.

Here is an example:

\ STM8 eForth interactive RC-servo demo

\ set compilation target for STARTTEMP /  ENDTEMP
: TARGET NVM ;

#require hw/pwm.fs
#require OPT!

#include STARTTEMP

\res MCU: STM8S103
\res export OPT2

TARGET

  \ set option bits to enable PC5 TIM1_CC1, and PC6 TIM1_CC2
  : OptInit ( -- )
    OPT2 DUP C@ $01 OR SWAP OPT!
  ;
  \ timer1 init to 1MHz clock and 20ms period
  : ServoInit ( -- )
    15 T1PwmInit
    20000 T1Reload
    \ init servos to 50%
    1500 PWM1  \ PC6 pin 16
    1500 PWM2  \ PC7 pin 17
    1500 PWM3  \ PC3 pin 13
  ;
ENDTEMP

 The STARTTEMP - TARGET - ENDTEMP structures Forth code into part that resolves dependencies or defines temporary words, and a TARGET part that contains the words that should remain in the dictionary. ENDTEMP finally removes temporary words from RAM. STARTTEMP uses the word MARKER which means that the dependencies can be nested.

Mr. Mahlow also published a new version of e4thcom 0.6.3 that has improved support for "resource files". These can now reside in a search path folder (e.g. the mcu folder). It's also possible to create an arbitrary search path with up to 3 directories.

Discussions