eForth for STM8S 

Chen-Hanson Ting, 7/19/2010


1.	STM8S Discovery Board

The STM8S Discovery Board from STMicroelectronics Corp. is a very convenient single board computer
 which allows you to evaluate STM8S chip and explore all its capabilities.
It is amazing that ST makes it available for less than $10.
Such generosity deserves a convenient operating system which an average user can interact with
 the CPU and develop applications.
STM8eForth is such a system.
I am donating it to the public domain.
You can use it and distribute it freely.
You can also use it to develop applications for commercial purposes without any restriction.  

2.	Running STM8eForth

STM8eForth system is distributed in the form of an assembly file stm8ef.asm.
It is fully tested on STVD IDE which can be downloaded from www.st.com/stm8s-discovery.
Install STVD on your computer.  Create a new project.  Add stm8sef.asm to this project,
build the project, and use the debugger to program the resulting binary image to Discovery Board.
To interact with the eForth system downloaded to Discovery Board, you have to connect the UART2 port
 in STM8S to a COM port on PC.
The Discovery web page also distributes a Hyperterminal sample project which shows you haw to make
 the connection.
You have to open Hyperterminal application under Windows to communicate with STM8S,
 using RS232 communication protocol at 9600 baud, 8 data bits, 1 stop bit, and no parity.

Once STM8S chip on Discovery Board and Hyperterminal on PC are properly set up and connected,
you can type FORTH commands and STM8S with execute them.
  Here are samples of conversation I copied from Hyperterminal screen:

When STM8S is turned on, it displays the sign-on message:

stm8eForth v2.1
 ok
  ok

If you press the Return key, eForth will echo "ok" with a linefeed.

Here is the universal first program you write on any new computer:

 : TEST1 CR ." HELLO, WORLD!" ; ok
 TEST1
HELLO, WORLD! ok

Here is a program testing the IF-ELSE-THEN conditional structure:

 : TEST2 IF 123 ELSE 456 THEN . ; ok
 0 TEST2 456 ok
 1 TEST2 123 ok

Here is a program testing a loop structure:

 : TEST3 10 FOR R@ . NEXT ; ok
 TEST3 10 9 8 7 6 5 4 3 2 1 0 ok

Here is a program testing how fast this computer can do empty loops.
It takes eForth about 2 seconds to do 10 million empty loops.
It is not bad for a high level language to run loops like this.

 : TEST4 100 FOR 10000 FOR NEXT NEXT ; ok
 TEST4 ok

Now is the fun stuff.
I can turn the LED on Discovery Board easily by writing directly to the Output Data Register of Port D.
Bit 0 in this register controls the LED.
PD_ODR port address is $500F.
C! is the FORTH command which write a byte to a memory or register location.
HEX specifies that all subsequent numbers will be entered and displayed in hexadecimal.
"1 500F C!" writes a 1 to bit 0 of register PD_ODR at 500F, which turns off the LED.
"0 500F C!" clear this bit and turns the LED on..

 HEX ok
 1 500F C! ok
 0 500F C! ok

Here is a program which turns on the beeper on Discovery Board.
Beeper in STM8S is connected to Bit 4 of Port D.
However, this happens only if set the Option bit AFR7.
This is a long story I love to tell.
But now for short, when in the debugger, enable this beeper alternate function by clicking:
	Menu->Debug Instrument->MCU Configuration-AFR7
After AFR7 is thus enabled,
 you can write into the beeper control-status register BEEP_CSR at $50F3 to run the beeper.
Writing $3E enables 1 KHz beep.
Writing $7E enables 2 KHz beep.
Writing $FE enables 4 KHz beep.
Writing $1E stops beeper.

 3E 50F3 C! ok
 7E 50F3 C! ok
 FE 50F3 C! ok
 1E 50F3 C! ok

The other important FORTH command is C@ to examine the contents of a memory location or a register.
 For example, to read BEEP_CSR:

 50F3 C@
 1E ok

