Skip to content

STM8 eForth Idle Task

Thomas edited this page Jun 9, 2020 · 8 revisions

Idle Task

STM8 eForth can run code when the console is idle. It's almost as easy to use as the Background Task but it runs in the console context whenever the console is waiting for an input character from ?KEY.

Possible applications are fast protocol handlers, interpreters, state machines, and tasks with best-effort fast response in embedded systems. It's even possible to use the interpreter in an Idle Task with the STM8 eForth library word EVALUATE. For example it's possible to evaluate strings sent from a guest, e.g. via a wireless interface.

The following table gives an overview of Background Task and Idle Task properties:

Property Background Task Idle Task
Max run-time 1ms 1ms - (Background Task run-time)
Min rate 1/5ms 1/(Idle Task run-time)
Max rate 1/5ms 1/(console execution time)
Stack dedicated shared, must be balanced
I/O board I/O console
PAD dedicated shared
Interpreter words can't be used using EVALUATE

Note: in order not to drop characters from the Forth console user input care must be taken not to exceed 1ms run-time (1ms at the default UART setting of 9600 baud or 2ms at 4800 baud). This time interval includes the run-time of higher priority tasks (e.g. Background Task or interrupts). If the runtime of the idle task approaches or exceeds 1ms, or whatever the character rate of serial IO is, buffered UART RX should be applied.

Here is an Idle Task example:

#require 'IDLE   
\ note: 'IDLE is "$0066 CONSTANT 'IDLE" (fixed memory address)

VARIABLE tally

: idletask ( -- ) 
   \ idle code must be stack neutral!
   tally @ 1+ DUP TALLY ! $F000 AND 0= OUT!
   ;

' idletask 'IDLE !  \   set IDLE execution vector to word idletask

When the console waits for input, idletask makes a board outputs flash (e.g. LED). An STM8 with 16MHz clock flashes the LED about 34 times per minute. This means that one IDLE loop iteration takes about 8.65µs = 34s/60/65536.

IDLE feature was added in issue #187 (Release 2.2.22). One of the first applications was a MODBUS protocol handler.

Clone this wiki locally