FlashForth: Hardware Abstract Layer (HAL)

3 minute read

UPDATED: This page contains a both a Forth hardware abstraction level (HAL) much like the m328def.inc file for AVR programming in C and an example of debouncing buttons.

New Words

This HAL provides the words:

I/O Words

The Arduino pins are defined by bit port so one can use the following commands using the Arduino pin number. This is true for pins D0-D13.

high    ( bit port -- )     set a Arduino pin high
low     ( bit port -- )     set a Arduino pin low
toggle  ( bit port -- )     toggle output value on an Arduino pin
output  ( bit port -- )     set Arduino pin as output
input   ( bit port -- )     set a Arduino pin as input
pullup  ( bit port -- )     set Arduino pin as input_pullup
read    ( bit port -- f )   read a Arduino pin, returns bit value

For example:

D3 output \ set Arduino Uno pin 3 as output
D3 high   \ set Arduino Uno pin 3 high

Checking Memory

.mem_left ( -- )      shows memory remaining for all three types
.mem_left 
 flash: 21878 
eeprom: 1012 
   ram: 1552

Turnkey

To set a word to run on boot, you use the word turnkey in this manner:

' start is turnkey

In this example, the word start would run once boot has completed. To quit this behavior, press restart on the Uno board then the Esc key on your keyboard. This returns to the standard Forth interpreter. To ensure the turnkey word is turned off enter (or enter the word falkey, which is the same thing.):

false is turnkey

Timing

us ( n -- )           blocking delay in n microseconds

Hardware Debounce

See the examples buttons and fsm to understand how to use Timer 0 for debouncing buttons. The debounce code is in the HAL, buttons demonstrates how to use the words.

Forth HAL

empty

%10000000 constant BIT7   \ bit 07  
%01000000 constant BIT6   \ bit 06  
%00100000 constant BIT5   \ bit 05  
%00010000 constant BIT4   \ bit 04  
%00001000 constant BIT3   \ bit 03  
%00000100 constant BIT2   \ bit 02  
%00000010 constant BIT1   \ bit 01  
%00000001 constant BIT0   \ bit 00  

\ Arduino Board Pins, reference using Board Pins
\ not AVR registers if possible
\ Pins referenced by nn, where nn is the Arduino board pin number
BIT5 ddrc 2constant A5 \ Board Connector A5 PC5
BIT4 ddrc 2constant A4 \ Board Connector A4 PC4
BIT3 ddrc 2constant A3 \ Board Connector A3 PC3
BIT2 ddrc 2constant A2 \ Board Connector A2 PC2
BIT1 ddrc 2constant A1 \ Board Connector A1 PC1
BIT0 ddrc 2constant A0 \ Board Connector A0 PC0
BIT5 ddrb 2constant LED \ Board Connector 13 PB5
BIT5 ddrb 2constant D13 \ Board Connector 13 PB5
BIT4 ddrb 2constant D12 \ Board Connector 12 PB4
BIT3 ddrb 2constant D11 \ Board Connector 11 PB3 PWM OC2A
BIT2 ddrb 2constant D10 \ Board Connector 10 PB2 PWM OC1B
BIT1 ddrb 2constant D9  \ Board Connector  9 PB1 PWM OC1A
BIT0 ddrb 2constant D8  \ Board Connector  8 PB0 
BIT7 ddrd 2constant D7  \ Board Connector  7 PD7 
BIT6 ddrd 2constant D6  \ Board Connector  6 PD6 PWM OC0A
BIT5 ddrd 2constant D5  \ Board Connector  5 PD5 PWM OC0B
BIT4 ddrd 2constant D4  \ Board Connector  4 PD4 
BIT3 ddrd 2constant D3  \ Board Connector  3 PD3 PWM OC2B
BIT2 ddrd 2constant D2  \ Board Connector  2 PD2 
BIT1 ddrd 2constant D1  \ Board Connector  1 PD1 
BIT0 ddrd 2constant D0  \ Board Connector  0 PD0

\ PRIM: primitives for initializing port and set high or low output
: high ( bit port -- ) 1 + mset ;  \ set a pin high
: low ( bit port -- ) 1 + mclr ;  \ set a pin low
: tog ( bit port -- ) 1 - mset ; \ toggle output value
: output ( bit port -- ) mset ;  \ set pin as output
: input ( bit port -- ) mclr ;  \ set a pin as input
: pullup ( bit port -- ) 2dup input high ; \ set pin as input_pullup
: read ( bit port -- f ) 1 - c@ and ; \ read a pin, returns bit value
: on high ;
: off low ;

-end_ports
marker -end_ports

Comments powered by Talkyard.