!ARMForth32 on the Raspberry Pi

 
 
Home Page
 
 What is Forth?
 
 Why use Forth?
 
 !ARMForth32
 
Code Extensions
 
Examples
 
Downloads
 
More Information
 
 

Extensions to the Forth Dictionary (additional Words)

Code Extensions:

1. Floating Point. Many Forths expect the programmer to stick to integer arithmetic and use scaling factors to maintain precision, but the inclusion of floating point numbers makes the whole system more suitable to mathematical and scientific operations. Accordingly, floating point is now included in the main dictionary by using the facilities of the Floating Point Emulator. Single-precision values are used throughout (i.e. a 32-bit word contains the sign bit, mantissa and exponent) as otherwise an additional floating point stack would have been required plus some fairly fundamental changes to the basic arithmetical parts of the language. The precision and range are expected to be adequate for the likely applications to be chosen. There are numerous examples of the use of the floating point words (eg "FLOAT", "FIX", "FSIN" etc in the Downloads section)

2. Complex Numbers. A new data type for complex numbers has been defined using the words <BUILDS and DOES> (Note: <BUILDS is equivalent to CREATES for the later ANSI-Forth). Each element of the structure contains two words in order: the first being the real part and the second the imaginary part of the complex number. Mathematical manipulations such as multiplication, division and obtaining the complex conjugate have also been defined. Conversion to and from polar coordinates is included. Complex number operations are used in one of the projects discussed later - the Fast Fourier Transform (FFT). The complex numbers module is loaded separately from the core dictionary, as it is not required for every application.

3. Boolean Operators. The standard AND, NOT, OR etc apply to bit-wise checks between two parameters rather than 'Yes/No' logic i.e Boolean operators. The parameter in question is first made into a "1" or "0" and is then tested against a similarly defined value of a second parameter. The new words are lower-case equivalents of the upper-case bit-wise words in the main Forth dictionary (see the file "Boolean" in the Support directory of the downloaded language).

4. GPIO Control. In order to control the GPIO pins on the Pi, two methods were used:
1. the module "GPIO" supplied by: "Tank" at www.tankstage.co.uk. The Forth assembler was used to interface to the various SWI calls provided by this relocatable module - see the file "GPIO_pi" in the Support directory. It will be necessary to read the manual written by Rob Turner in order to understand this code.
2. the Broadcom SoC registers were accessed directly in Forth to select the desired GPIO pin number and to define its function and/or state - see the file "GPIOForth" in the Examples.GPIOForth directory.
The GPIO lines may also be configured as an IIC (or I2C) bus and/or an SPI bus, and there are examples of configuring these for use with ADCs . There are also examples of Forth code used to control stepper motors (full or half-step) and DC motors using hand-crafted Pulse Width Modulation (PWM). Either type of motor may be servo'd by a rotation potentiometer. The software also includes a method of ramping up and down the motor speed (using a simple formula) - examples in the Downloads section.
In the case of the IIC bus, a known fault in the Broadcom chip which exists when the device being accessed imposes a 'wait' state led to the devlopment of a "bit-banging" solution using Forth. The Forth code simulates the required behaviour of the IIC bus, and has the advantage that any two pins may be used, rather than being tied to the pins nominated by the Pi. The 'wait' state may also be accommodated quite easily.