by
Mikael Nordman
FlashForth is a standalone native Forth operating system
implemented on the Microchip 8-bit PIC18F and 16-bit PIC24, 30, 33
and the Atmel Atmega microcontroller families.
FF
(FlashForth) allows you to write and debug complex real-time
applications. The complete system including the compiler is executing
on the microcontroller.
A Forth interpreter, compiler,
assembler, multitasker and user definable interrupts are provided.
A computer with a terminal emulator is used for communicating
with FF via a serial or USB link.
The Forth source files are
edited and saved on the computer and uploaded to the uC as Forth
source code.
All uC registers and memories can be read and
written from the command line.
When the application is ready,
the application word address can be stored in the turnkey vector, and
your autonomous embedded application has been set up.
FlashForth
is mostly compatible with the ANS'94 standard. This guide describes
the differences, and features not covered by the ANS'94 standard.
FlashForth is licensed according to the Gnu Public License v3.
If you feel that FlashForth has been useful for you, please contribute with a donation.
You can now try four different FF boards via Telnet !
PIC18F2620 @ 12 MHz using a 38400 baud UART.
telnet -l pic18f2620 flashforth.dlinkddns.com
PIC24HJ128 @ 55 MHz using a 38400 baud UART.
telnet -l pic24hj128 flashforth.dlinkddns.com
ATMEGA328p @ 8 MHz using a 38400 baud UART.
telnet -l atmega328p flashforth.dlinkddns.com
ATMEGA128 @ 16 MHz using a 38400 baud UART.
telnet -l atmega128 flashforth.dlinkddns.com
The password is ff . End the session with control-c .
If you get stuck you can warm start the board with control-o . Use control-h for backspace.
The excellent Elements of FlashForth tutorial is essential for a basic understanding of how to use FlashForth.
It is brought to you by Peter Jacobs of the University of Queensland.
Peter Jacobs (University of Queensland) has written two excellent FlashForth tutorials and a reference sheet.
Elements of FlashForth tutorial
FlashForth 3.8 Tutorial
FlashForth 3.8 Reference Sheet
Here are a few general sources for learning Forth.
Pforth tutorial.
Programming Forth by Stephen Pelc.
Starting Forth by Leo Brodie.
The ANS Forth Standard.
Support can be obtained via the MAILING LIST
Register HERE to the mailing list.
Many thanks to Joe Ennis, W7NET, and Pete Zawasky, AG7C, for
pushing FlashForth hard and bugging me with trouble reports.
Thanks
to Igor, OM1ZZ for contributing a proto board and two PICs for the 24
and 33 PIC series.
Thanks to Brian Howell of WCU for contributing
a PicKit2 and a PIC18F87J11 board.
Western Carolina University, Kimmel School, Electrical and
Computer Engineering Technology
is using FlashForth with PIC18F
and dsPIC30F for teaching microcontroller and DSP concepts.
University of Queensland.
PZEF Company.
IBHessler.
And many more.
Here you can see a bot that Joe Ennis was building. It is running
FlashForth.
http://www.youtube.com/results?search_query=flashforth&search_type=
FF can be downloaded from
http://www.sourceforge.net/projects/flashforth
.
The git repository contains the latest developments.
The git
repository can be cloned with the git command:git
clone
git://flashforth.git.sourceforge.net/gitroot/flashforth/flashforth
Download FF for PIC18. Expand the downloaded archive into a folder. Follow the instructions in install.txt
At least the following PIC processors are able to run FlashForth 3.8.
- 18f242, 18f442, 18f252, 18f452
-
18f248, 18f258, 18f448, 18f458
-
18f2455, 18f2550, 18f4455, 18f4550
- 18f2420, 18f2520, 18f4420,
18f4420
- 18f2525, 18f2620, 18f4525, 18f4620
- 18f6527,
18f6622, 18f6627, 18f6722
- 18f8527, 18f8622, 18f8627, 18f8722
-
18f2458, 18f2553, 18f4458, 18f4553
- 18f2480, 18f2580, 18f4480,
18f4580
- 18f2423, 18f2523, 18f4423, 18f4523
- 18f2585,
18f2680, 18f4585, 18f4680
- 18f2682, 18f2685, 18f4682, 18f4685
-
18f14k22, 18f14k50
- 18f24K20, 18f25k20, 18f26k20
- 18f44k20,
18f45k20, 18f26k20
Here is the wordlist for FF3.8 and PIC18.
Download FF for PIC 24-30-33. Expand the downloaded archive into a folder.
Select the processor type in the IDE. Update relevant include file with clock configuration and frequency and other settings.
Compile the project and program the HEX file to the PIC with your favourite device programmer.
FF 4.8 has been run at least on dsPIC30F4012, dsPIC30F4013, dsPIC33FJ128GP802 and pic24HJ128GP502
FF 4.8 should work on all 16-bit PIC chips with enough flash(>=24Kbytes) and enough ram.
Here is the wordlist for FF4.8 and PIC24-30-33.
Download FF for Atmega.
Select the uC type by including the correct include file, e.g.
m128def.inc for Atmega 128.
Configure the clock and baudrate
according to your hardware and preferences.
This is done in the
configuration file, config.inc.
Compile the code with AVR Studio and program the hex file with a
suitable JTAG or ICSP programmer to your chip.
The fuses must be
set to BOOTSIZE=1024 words and the BOOTRESET should be active.
The Atmega has been tested on Atmega 128 and 328 sofar.
It
should also work on Atmegas 168 and 644.
Users report that FlashForth works on the Arduino Duemilanove 328p.
Here is the wordlist of FF for Atmega.
FF is case sensitive. All FF core words are written in lowercase letters and hex numbers must be entered in lower case also.
Although the FF core words are in lower case, in documentation the FF core words may be written in UPPERCASE to make it clear that a Forth word is referred to.
Forth source code can be interpreted and compiled by loading it via a terminal emulator.
FF 3.8 supports terminal communication via UART or USB serial
emulation.
The USB serial emulation requires that you use a PIC
chip with inbuilt USB tranceiver.
The default UART setting is 38400, 1, N, XON/XOFF. It is mandatory to enable inband flow control (XON/XOFF). When FF stores data in flash, the chip will stop responding for up to 20 milliseconds. XOFF will prevent the terminal emulator from sending characters to FF while data is being stored into flash.
CTS/RTS (HW flow control) is available as a compilation option.
USB serial emulation flow control is handled by the USB protocol. Use HW flow control in the terminal emulator.
I am using minicom with linux and it works OK without any extra TX delays. Forth source files can be sent to the PIC using the 'send ascii file' (CTRL-A S) function.
With Windows I use TeraTerm. It also works OK.
FlashForth recognises CRLF or only CR as end of line in ACCEPT. LF and CR are not echoed by ACCEPT
U1- and U2- can be used for disabling flow control if the end application can not support flow control on the serial interface.
Normally communication with the PC and writing to flash works very reliably, but...
If to you see a vertical bar '|' output from FlashForth, it means that the UART RX interrupt buffer has overflowed.
It is usually caused by the PC reacting slowly on XOFF.
'setserial
/dev/ttyS0 low_latency' improves the situation on Linux.
On
Windows, disabling the UART buffers improves the situation.
Another
alternative is to use TeraTerm with an intercharacter delay of a few
milliseconds.
Increasing the UART RX interrupt buffer size and sending XOFF for a small buffer fill level can also improve the situation.
If you see an extra '~' output it means that a serial framing or overrun error has occurred.
If you see a '^' output from FlashForth, it means that the verification of a program memory write has failed. FlashForth will try to write the same buffer only once, then an warm start will be made.
If you always get a write error when compiling Forth words, try adjusting outer_loop and inner_loop constants. For example some early versions of the PIC18f2520 family require (2,32) instead of the documented (1,64). See Microchip errata page for details of your particular chip.
Please note that the chips 18F252, 18F452, 18F258, 18F458, may not
work reliably if you jump over the $3fff address border.
So these
chips are not recommended for FlashForth if your program grows over
the $3fff border.
This is a fault in the PIC chip, not in
FlashForth. The other 18Fxxxx chips do not have any such errata.
The PIC memory is mapped by FF:
PIC18F:
FLASH is mapped to $0000
- $ebff
EEPROM is mapped to $ec00 -
$efff
RAM is mapped to
$f000 - $ffff
dsPIC30, dsPIC33, PIC24:
RAM is mapped
to $0000 - $PFL-1
FLASH
is mapped to $PFL - $fbff NOTE: PFLash is
determined in the configuration files.
EEPROM
is mapped to $fc00 - $ffff
Atmega:
RAM
is mapped to $0000 – (RAMSIZE-1)
EEPROM is
mapped to RAMSIZE – (EEPROMSIZE-1)
FLASH is mapped to
(0xffff – FLASHSIZE + 1) - 0xffff
In FlashForth data space is allocated by CREATE ALLOT VARIABLE , C, VALUE DEFER .
The words RAM EEPROM FLASH sets the data area from which the succeeding allocation will be made.
@ ! and other memory access words can be used transparently with all types of memory.
For the FF3.8 the mapping penalty is 33 % (4 instruction cycles) for ram stores and fetches. For the FF4.8 the mapping penalty for ram access is ~50% (3 instruction cycles).
\ A variable in ram
ram
variable var1
\ A variable in eeprom
eeprom
variable var2
It is not recommended to create variables in eeprom unless these are updated fairly seldom.
Data areas in flash are normally used for constant data and constant execution vectors.
Flash can be written to 10000 times until it may fail. Eeprom can be written to 100000 times until it may fail.
Compile a word which creates character arrays in the current data space.
: carray ( n “name” -- ) create allot does> + ;
Create a 20 character array in eeprom called CALIBRATE.
eeprom decimal 20 carray calibrate
ram
It is good to always set the data space context back to ram after flash or eeprom has been used.
Compile a word which creates indexed cell arrays.
decimal
: array create
cells allot does> swap 2* + ;
ram 20 array
cnt \ Creates the array cnt ( size 20
cells )
1233 10 cnt ! \
Store 1233 in table index 10
10 cnt @ \
Fetch from index 10
The words MSET MCLR MTST BSET BCLR BTST can only address RAM. These words are typically used for setting and clearing bits in registers.
The Forth interpreter is a normal Forth interpreter. It parses words delimited by space and tries to find the word in the dictionary. TAB is ignored. If the word is found it is executed, if not, FF tries to convert it to a number according to the current base (or base prefix), and put the number on the stack. If that fails ABORT is called.
The interpreter can only be used by the OPERATOR task, not by the background tasks.
After interpreting a line, QUIT prints Ok and executes the deferred word PROMPT. If you don't want to see the info from .ST, you can re vector PROMPT to for example CHARS which does nothing.
' chars is prompt
' .st
is prompt
FF is a 16-bit Forth and the single precision math operations are consequently 16-bit.
FF also supports double precision 32-bit math.
UM/MOD, M+ and UM* are used as base for the extra 32-bit double precision math words that can be loaded from math.txt.
For working with 48 and 64 bit numbers there are words in qmath.txt.
The words UT* UT/ UT/* have 48 bits precision.
The words UQ* UQ/MOD QM+ D>Q have 64 bits precision.
FF supports single precision 16-bit and double precision 32-bit number conversion.
Double precision numbers are identified by a trailing dot.
Input numbers can be prefixed by % # $ to achieve binary decimal and hexadecimal number conversion without changing BASE.
Output numbers are always converted according to BASE.
FF is a subroutine threaded Forth with native code generation.
Code is always compiled to flash memory. The PICs can execute code only from flash.
Literals are compiled as native code.
DUP and 0= before IF WHILE UNTIL are optimized away.
All the structured conditional words generate native code.
: and ] puts FF in compilation state. ; ;I and [ enters the interpreter state.
The maximum word name length is 15 characters.
Words that should not be interpreted have a 'compile only' bit in the header. Interpreting these words will result in an ABORT and restarting the interpreter, that is jumping to QUIT.
The compiler performs tail call -> goto optimisation at the end of a word.
It is possible to call the word beeing currently defined.
If
the word is the last word before ';' it will result in a branch back
to beginning of the word.
If the current word is called earlier,
it will result in recursion.
FF can compile location independent assembler primitives as inline code. Some of these words have the inline bit set in the word header.
Individual words can be inlined by prefixing the word with INLINE.
: newswap inline swap ;
When compiling a new word that should be inlined automatically, the inline flag can be set with the word INLINED.
: 1+
[ Sminus w, a, swapf, ] \ Decrement stack pointer with one
[ Splus f, a, infsnz, ] \ Add lower byte, skip next instruction
if the result was nonzero
[ Srw
f, a, incf, ] \ Add high byte
; inlined \ Set
the inline header flag
On the FF3.8 the following words are always inlined by the compiler.
[i i] drop p+ cwd r@ r> >r rdrop false true 1 leave cell chars di ei
On FF4.8 the following words are always inlined by the compiler.
cwd ivt aivt [i i] ei di u1txq u1rxq
drop over >r r> r@
invert negate 1+ 2+ 1- 2- 2* 2/
!p>r r>p p+ p2+ >body
cell cell+ cells char+
chars 2drop 0 1 nip nfa>lfa leave
rdrop bl ticks
cpu_clk false true + - and or xor !p @p
On FF3.8 the following words can be prefixed with INLINE.
mset mclr lshift rshift sp@ swap
over rot dup + m+ - and or
xor invert 1+ 1- 2+ 2* 2/ !p @p
p++ p2+ ticks
On FF4.8 the following words can be prefixed with INLINE.
mset mclr mset bset bclr lshift
rshift sp! sp@ swap rot m+
um* um/mod u/mod m* sm/rem /mod
mod /
Inline literals are fast and do not take too much space. With CON
you can define a constant which is compiled as native code, it does
not use DOCREATE. If you follow the definition with inlined,
the compiler will later compile the constant as an inline literal.
34 con thirtyfour inlined
: native-inline-34 thirtyfour ;
The core dictionary is always searched before the user dictionary.
It is not possible to redefine existing words. These measures have
been taken to make the system more robust and to make it possible to
recover to the basic state, without the need to flash the chip again.
It also makes the code clearer, since there will not be two words
with the same name.
In order to recover to an earlier dictionary and memory allocation state, use MARKER. Always before defining new words define a marker. Otherwise you may need to return to an earlier marker or to say EMPTY which will empty the dictionary and reset all memory allocations to default values. A marker will restore TURNKEY, DP and LATEST. IRQ is not affected.
FORGET can be used to forget a user word, but FORGET can only adjust the FLASH DP. This means that allotted EEPROM or RAM will not be reclaimed if you use FORGET.
Note that TURNKEY, DP, LATEST are stored in flash, but their respective addresses point to ram. These variables are cached in ram during interpretation of a input line and also during compilation state. This makes compilations run faster, and there will be less wear of the eeprom.
Since FF refuses to redefine words, certain words, typically one line definitions, can be compiled from several source files. The first compilation is accepted, and the others rejected. This is quite practical for having some short definition in many files, so that you can compile exactly the words one or more applications needs.
For example i2c_base.txt and task-test.txt both have defined PORTC, but PORTC will be compiled only once, even if both files are loaded to FF.
The word FL- can be used for disabling writes to flash and eeprom. It is useful for making sure that no writes to flash or eeprom occur.
After processor reset a check is made to see if a turnkey word
should be executed. If the eeprom value TURNKEY contains a nonzero
value, it must be an address of a valid user word. Unless the user
presses ESC within the turnkey timeout, the user word is executed. If
ESC is pressed, the user word will not execute. Instead the forth
interpreter is entered.
'
my_application is turnkey
If your TURNKEY word is crashing, press ESC and as a first command give:
false is turnkey
This will disable the TURNKEY and allow you to make corrections.
This chapter describes the interrupt handling on the PIC18 series
Interrupts are also supported on the PIC24-30-33 and the Atmega. There are example files in the downloads.
Interrupt routines can be written in assembly or in Forth.
The
Forth interrupt word has its own parameter stack of 8 cells. A
FF interrupt word has to be ended with ;I .
There are certain
limitations to which registers and Forth words can be used in an
interrupt word.
It is not recommended to store to flash or
eeprom in an interrupt routine.
The following words are safe
to use inside the Forth interrupt routine.
Words defined by:
variable
con
constant
: create does> ;
value
defer
The following words can be compiled into a Forth
interrupt word :
@ ! c@ c! mset
mclr mtst lshift rshift exit execute @ex
cell aligned
cell+ cells char+ chars 2@ 2! 2drop 2over
umin umax 0 1
drop swap over rot nip tuck >r r> r@ dup abs
+ - and
or xor invert negate 1+ 1- 2+ 2- 2* 2/ +! within
<>
0= 0< = < > u< u> nip tuck ?negate min max false
true
um* * um/mod u/mod u/ u*/mod / cwd m+ cmove place
The
following immediate words can be used in a interrupt word :
['] [char] [ ] literal is if else then begin while repeat until for
next
The following registers are saved on the return
stack by [I and restored by I].
Sreg(=FSR0),
TBLPTRL, TBLPTRH, TABLAT.
Treg (=FSR1) and PCLATH are
always saved before the interrupt vector (IRQ) is called, and
restored by ;I
This makes it possible to use the above Forth words in the
interrupt word.
Below is a interrupt word which counts the
total number of interrupts.
ram
variable irq_counter
: my_irq
[i
irq_counter
@
1+
irq_counter
!
i]
;i
The
following way to store the variable address as an inline literal, and
inlining 1+, gives faster code.
:
my_irq
[i
[
irq_counter literal ] @
inline
1+
[ irq_counter literal ]
!
i]
;i
To
activate the interrupt you store the interrupt word xt into the
interrupt vector IRQ.
' my_irq
is irq
IRQ is cleared at warm start, so to enable
the interrupt word
at startup, a initialization word must be
used.
: irq_init ['] my_irq is
irq ;
' irq_init is turnkey
The above
example is a simple example. To use individual interrupt sources the
interrupt enable bits and flag bits must be used.
See
servo.txt for an example for a complete
servo control routine that uses a timer and interrupts to control 4
servo channels.
Below is the interrupt counter implemented in
assembly
$28 as3
incf,
( f d a -- )
$48 as3 infsnz,
( f d a -- )
: lfsr, ( k f -- )
4 lshift over 8 rshift $f and or $ee00 or i, $ff and $f000 or i, ;
1 con f,
\ Destination File
0 con a,
\ Force Access Bank
1 con Treg
$ffe6 con Tplus \ Treg (FSR1) is interrupt safe
ram variable irq_counter
\
Interrupt routine written in assembly
: my_irq
[ irq_counter Treg lfsr, ]
[ Tplus f, a,
infsnz, ]
[ Tplus f,
a, incf, ]
;i
NOTE:
By going to compile state before end-of-line, there will be less
writes to FLASH and EEPROM and the compilation process will go
faster.
FF can execute background tasks concurrently with the operator task.
The task switching is made cooperatively by executing PAUSE. PAUSE is executed in I/O words KEY and EMIT so that background tasks can run while the console is waiting for input or queuing for output. PAUSE is also executed in FIND so that compilation does not disturb the real time too much. MS executes PAUSE while it waits for the specified delay to pass.
If IDLE_MODE in the configuration file is enabled, the PIC will
enter the idle powersaving mode in PAUSE.
If you want to allow the
PIC to go into idle mode in your own I/O or looping words use the
word IDLE. If not, use the word BUSY.
The idle powersaving mode is
entered in PAUSE if all tasks have been marked idle with the word
IDLE.
If one or more tasks are busy, the idle mode will not be
entered.
An interrupt will exit the idle mode and the PIC will run
until the next time idle mode is entered.
The percentage of time that the PIC is busy can be read by the LOAD word. The integration interval is 256 milliseconds.
The words for tasking can be loaded from task.txt.
The user area lives in ram. The user area is initialized from the task definition in flash.
TASK: creates a new task and defines the stack sizes and the
additional user area size and the tibsize.
Tibsize can be set to
zero for background tasks, except if numeric output is used.
The
end of the TIB is shared with the HOLD buffer. A task that uses “.
U. <# # #s #>” etc., will
need a small TIB for number
formatting.
Each task has its own PAD which starts at end of TIB. When allocating ram you must allot space for the PAD if it is being used.
The FF kernel does not use PAD. So if you want to use PAD in the OPERATOR task it is up to you to allot space for PAD. This non-standard behavior exists to save ram.
TINIT initializes a task with the XT of the task loop. It also initializes the task user area.
RUN makes the task run. It inserts the task in the round-robin linked list.
END ends a task. It removes the task from the round robin linked list.
SINGLE ends all tasks except the operator task.
TASKS lists all running tasks.
The tasking commands may only be executed from the operator task.
KEY, KEY?, EMIT can be deferred and used in a background task to interact for example with a keyboard and a LCD display.
\ Task loop for displaying data on the LCD display
: lcd_display ( -- )
lcd_init
['] lcd_emit 'emit ! \ Use LCD emit
hex
begin
#00 lcd_at \ Position cursor at beginning of first line
." Ticks: " ticks u.4 \ Display the current number of ticks
again
;
10 20 20 0 task: lcd_task \ tibsize stacksize rsize addsize --
' lcd_display lcd_task tinit
lcd_task runI have always found DO..LOOP cumbersome to use. I wanted to separate the loop count and the index handling. Therefore in FF 3.0 FOR..NEXT and the P register were introduced.
FOR..NEXT loops exactly the amount of times specified ( also 0 ) .
: star
[char] * emit ; ok <$,ram>
star *ok <@,ram>
: stars for star next ; ok <$,ram>
10
stars ****************ok <$,ram>
0 stars ok
<$,ram>
The loop count is held on top of the return stack and it can be
fetched by R@. LEAVE sets the loop count to 0, so that NEXT will
terminate the loop.
: test
#10
for
r@ . r@ 4 =
if
leave
then
next
; ok
test
9 8 7 6 5 4 ok
If you EXIT a FOR..NEXT loop you must drop the loop count with
RDROP
: test
#10
for
r@
4 =
if
rdrop
exit
then
r@
.
next
; ok
test
9 8 7 6 5 ok
The P register can be used as a variable or as a pointer. It can be used in conjunction with FOR..NEXT or at any other time.
!P>R pushes the current P value on the return stack and sets a new value to P.
In a definition !P>R and R>P should always be used to allow proper nesting of words.
R>P pops a value into P from the return stack.
!P sets a new value into P. Use !P only from the command line, or between !P>R and R>P in a definition.
@P lets you fetch the value of P.
P+ increments P by one.
P2+ increments P by two.
P++ ( n -- ) adds n to P.
P@ P! PC@ PC! are used to access memory via the pointer.
Always remember to balance the return stack in all branches of your code.
\ CMOVE src dst u -- copy u bytes
from src to dst
\ The source address is kept on the
parameter stack.
\ The destination address is kept in the P
register.
: cmove
swap !p>r
for
c@+
pc! p+
next
r>p drop
;
The PIC hardware stack is used as the Forth return stack.
The
FSR0 register is used as the parameter stack pointer. It is called S
in the assembler code.
FSR1 is used as a temporary pointer and as
temporary storage. It is called T in the assembler code.
FSR2 is
used as a temporary pointer and as temporary storage, Its called A in
the assembler code.
FSR2 is not interrupt safe, but it is used in
words that would normally not be used in an interrupt word.
PCL,
PCLATH, TBLPTRL TBLPRH are used for accessing flash memory. PCLATU
and TBLPTRU must be zero at all times.
The return stack pointer is W15.
The parameter stack pointer
is W14.
The P register uses W13
Assembly words use W0..W3.
In
addition SKIP, SCAN, N= use W4 and W5. [I does not save W4 and W5, so
those words cannot be used in an interrupt routine.
SP: The return stack pointer
Y : The parameter stack pointer
X,Z: Temporary data and pointers
r24,r25: Cached TOS value
r22,r23: Internal flags
r20,R21: The P register
All other registers are used internally by FlashForth.
ff18_usb.asm is the
FlashForth core assembly file for the PIC18Fxxxx processor series.
p18fxxxx.cfg
The
master configuration file. It includes the p18f-main.cfg
file the processor specific file depending on the selected
processor type. For example p18f2420-2520-4420-4520.cfg
asm.txt
The
assembler can be loaded from asm.txt. Look in blink.txt, i2c_base.txt
and irq.txt for examples how to use the assembler words.
MSET
MCLR MTST can be used for bit-manipulation but using inline assembly
is much faster.
blink.txt
blink is a simple word showing how to access the PIC hardware
by using inline assembly.
case.txt
, case-test.txt
Contains
CASE OF ENDOF ENDCASE.
core.txt
Contains EVALUATE FORGET DUMP FILL ERASE BLANKS ?DUP PICK
ct.txt , ct-test.txt
Contains
a word for making condition tables. The idea is to use this instead
of CASE statements. Look in seen.txt for an
example. There the see word has been implemented using a condition
table.
Help words a and a help file. Load first help.txt. Then
just load helpwords.txt
i2c_base.txt
i2c_24aa1025.txt i2c_tcn75.txt
i2c_ds1307.txt
I2C
words for accessing a serial eeprom, a temperature sensor and a RTC.
irq.txt
Contains a
counter for the total number of interrupts. A very simple interrupt
routine. Both assembly and Forth versions are shown as examples.
jt.txt , jt-test.txt
Contains
a word for making jump tables. This can be used instead of CASE
statements to execute words corresponding to a certain value.
A better jump table with a much clearer structure. Design by Pete Zawasky.
Here you can find 32-, 48- and 64-bit math words.
pic18f_regs.txt
Some
PIC register adresses are defined here. Thanks to Andrew Smith.
see.txt
The SEE word
decompiles forth words. SEE also decompiles the assembler
instructions bcf, bsf, btfsc, btfss, return, bra, bz and goto.
servo.txt ,
servo-test.txt
Contains a
servo pulse solution as an example of how to set up a interrupt
routine using Forth. It can control 4 servos on port B. The interrupt
routine uses TIMER0 interupts to time the servo pulses.
task.txt
, task-test.txt ,
8blink.txt
Contains the words
TASK:, TINIT, RUN, END, SINGLE, TASKS for task handling.
task-test.txt contain some led blinking background tasks.
8blink.txt contains tasks for blinking 8 leds in 8 separate
background tasks.
words38.txt
words48.txt
These file
contain the word lists.
warm
FlashForth V3.4 PIC18F258
ESC
decimal ok<#,ram>
255 ok<#,ram>255
$ff ok<#,ram>255 255
%11111111 ok<#,ram>255 255 255
bin ok<%,ram>11111111 11111111 11111111
hex ok<$,ram>ff ff ff
2drop drop ok<$,ram>
words
marker
p2+ pc@ @p m? b? rdrop leave next
for in, inline repeat while again until begin
else then if until, again, begin, else, then,
if, not, nc, nz, z, br? true false
dump .s words >pr .id ms ticks s0
latest state bl 2- ['] -@ ; :noname
: ] [ does> postpone create cr [char]
( char ' abort" ?abort ?abort? abort prompt
quit .st inlined immediate shb interpret 'source >in
tib ti# number? >number sign? digit? find immed?
(f) c>n n>c @+ c@+ place cmove word
parse \ /string source user base pad hp
task rcnt ssave rsave ulink bin hex decimal
. u.r u. sign #> #s # >digit
<# hold up min max ?negate tuck nip
/ u*/mod u/ * u/mod um/mod um* ukey?
ukey uemit p++ p+ pc! p! p@ r>p
!p>r !p u> u< > < = 0<
0= <> within +! 2/ 2* >body 2+
1- 1+ negate invert xor or and -
m+ + abs dup r@ r> >r rot
over swap drop allot ." s" type accept
1 umax umin spaces space 2dup 2drop 2!
2@ cf, chars char+ cells cell+ aligned align
cell c, , here dp ram eeprom flash
c@ @ c! ! sp@ con constant variable
@ex execute key? key emit cold warm btfss,
btfsc, bsf, bcf, bra, rcall, call, goto, br3
br2 as1 as3 rshift lshift ic, i, operator
cpu_clk mtst mclr mset iflush pause turnkey is
to defer value cwd literal irq ;i di
ei scan skip n= rx1? rx1 tx1 i]
[i andlw, movf, w, a, exit ok<$,ram>
\ Compile a word which creates indexed cell arrays in current data memory.
: array create cells allot does> swap 2* + ; ok<$,ram>
\ Create an array with elements in program flash
flash #10 array flash-array ok<$,flash>
\ Get the address of element 0
0 flash-array hex ok<#,ram>30b6
\ Create an array with elements in eeprom
eeprom #30 array eeprom-array ok<$,eeprom>30b6
0 eeprom-array ok<$,ram>30b6 ec0c
\ Create an array with elements in ram
ram $20 array ram-array ok<#,ram>30b6 ec0c
0 ram-array ok<$,ram>30b6 ec0c f42a
2drop drop ok<$,ram>
\ move 10 cells
0 flash-array 0 ram-array #10 cells cmove ok<$,ram>
0 flash-array 10 dump
30b6 :f0 f1 f2 ff ff ff ff ff ff ff ff ff ff ff ff ff ................ ok<$,ram>
0 ram-array 10 dump
f42a :f0 f1 f2 ff ff ff ff ff ff ff ff ff ff ff ff ff ................ ok<$,ram>
\ Define a task loop that toggles PORTC outputs based on the
\ bitmask, delay determines the toggle period.
\ delay and bitmask are user variables to make it possible
\ to use the same task loop in many tasks, so that
\ each task can have it's own bitmask and delay values.
\ The compilation of the task definition words is not shown in this example
-lblink -lblink?
marker -lblink ok<$,ram>
decimal ok<#,ram>
$ff82 constant portc ok<#,ram>
$ff94 constant trisc ok<#,ram>
$2 user bitmask \ The bitmask ok<#,ram>
$4 user delay \ The delay time in milliseconds ok<#,ram>
ok<#,ram>
: lblink
bitmask c@ trisc mclr
begin
delay @ ms
bitmask c@ portc mset
delay @ ms
bitmask c@ portc mclr
again
; ok<#,ram>
\ Define the first task
flash $0 $10 $10 $4 task: tblink ok<#,ram>
\ Define a word that initialises tblink
: tblink-init
['] lblink tblink tinit
$1 tblink bitmask his !
$100 tblink delay his !
; ok<#,ram>
\ Initialise the tblink task
tblink-init ok<#,ram>
\ Run the the tblink task
tblink run ok<#,ram>
\ tblink is running in the background while tblink1 is compiled
\ Define, init and run the second task
flash $0 $10 $10 $4 task: tblink1 ok<#,ram>
: tblink1-init
['] lblink tblink1 tinit
$4 tblink1 bitmask his !
$60 tblink1 delay his !
; ok<#,ram>
tblink1-init ok<#,ram>
tblink1 run ok<#,ram>
\ Wait for 3000 milliseconds
3000 ms ok<#,ram>
\ End both tasks
single ok<#,ram>
\ Wait for 2000 milliseconds
2000 ms ok<#,ram>
\ Make both tasks start after a warm start or power on
\ Define a word that initialises and runs both tasks
: blink2 tblink-init tblink1-init tblink run tblink1 run ; ok
\ Test that blink2 works
blink2 ok<#,ram>
\ Wait 4096 milliseconds
$1000 ms ok<#,ram>
\ End both background tasks again.
single ok<#,ram>
\Store the execution vector of blink2 in the turnkey vector in eeprom
' blink2 is turnkey ok<#,ram>
\ Make a warm start
warm
FlashForth V3.4 PIC18F258
ESC
\ Now the leds should be blinking unless you pressed ESC.
\ The compilation of the see word is not shown in this example.
\ Decompile the blink2 word.
see blink2
291c dfb9 rcall tblink-init
291e dfe3 rcall tblink1-init
2920 dfa8 rcall tblink
2922 defb rcall run
2924 dfd0 rcall tblink1
2926 def9 rcall run
2928 0012 return
ok<$,ram>
tasks operator tblink1 tblink ok<$,ram>warm FlashForth V4.3 on dsPIC30F (C) Mikael Nordman GPL V3 ESC flash ok <$,flash> eeprom ok <$,eeprom> ram ok <$,ram> decimal ok <#,ram> bin ok <%,ram> hex ok <$,ram> see see 41b0 0007 faad rcall ' 41b2 0007 f61f rcall cr 41b4 0007 f7c3 rcall hex 41b6 0078 0f3e mov.w [W14++], [W14] \ DUP 41b8 0007 fe2e rcall u.4 41ba 0078 0f3e mov.w [W14++], [W14] \ DUP 41bc 0007 f1c4 rcall cf@ 41be 0007 fe2b rcall u.4 41c0 0007 fe2a rcall u.4 41c2 0007 ffd8 rcall (see) 41c4 0007 f616 rcall cr 41c6 00e0 001e cp0 [W14] \ IF also DUP 41c8 003a fff6 bra nz, 41b6 \ IF also 0= 41ca 0057 0762 sub W14, 2, W14 \ DROP 41cc 0006 0000 return ok <$,ram> see ms 3aa2 0007 fff9 rcall ticks 3aa4 0007 f7e7 rcall + 3aa6 0007 f488 rcall pause 3aa8 0078 0f3e mov.w [W14++], [W14] \ DUP 3aaa 0007 fff5 rcall ticks 3aac 0007 f7ef rcall - 3aae 0007 f83a rcall 0< 3ab0 00e0 002e cp0 [W14--] \ IF 3ab2 0032 fff9 bra z, 3aa6 \ IF 3ab4 0057 0762 sub W14, 2, W14 \ DROP 3ab6 0006 0000 return ok <$,ram> see t1go 441c 0024 3fe0 mov 43fe , W0 \ literal for tloop address 441e 0078 2f00 mov.w W0, [++W14] 4420 0007 ffe3 rcall t1 4422 0007 ff2e rcall tinit 4424 0007 ffe1 rcall t1 4426 0007 ff63 rcall run 4428 0006 0000 return ok <$,ram> tasks operator t1 ok <$,ram> 1 ok <$,ram>1 2 ok <$,ram>1 2 34 ok <$,ram>1 2 34 + ok <$,ram>1 36 - ok <$,ram>ffcb . -35 ok <$,ram>