LPC2xxx GPIO utilities





The code in ARM\LPC-GPIO.fth provides utility words for accessing the GPIO/FIO pins on a bit by bit basis. The code is written for ease of use rather than performance.




Configuration

The equates in this block are defaults used if the equates have not been defined before this file is compiled.

1 equ useFIOs?  \ -- flag
Use the FIO access reather than the legacy GPIO access. Note that on the Cortex-M3 devices, legacy access is not supported. FIO (Fast I/O) is much faster. Unless you are using LPC21xx/22xx devices before the /O1 releases, leave useFIOs set to 1.

: initFIOs      \ --
To use the FIOs on LPC21xx and LPC22xx parts, SCS bits 0 and 1 must be set. This is done at power up.

: initFIOs      \ --
To use the FIOs on LPC23xx and LPC24xx parts, SCS bit 0 must be set. This is done at power up.

_FIO0 equ PortBase      \ -- addr
Base address of port definitions.

$20 equ /PortBlock      \ -- len
Number of bytes per port for peripheral registers.

5 equ #PortShift        \ -- u
Number of bits to shift to offset by /PortBlock.

FIOSET equ xIOSET       \ -- u
Offset to port set register

FIOCLR equ xIOCLR       \ -- u
Offset to port clear register

FIOPIN equ xIOPIN       \ -- u
Offset to port pin register

FIODIR equ xIODIR       \ -- u
Offset to port pin register




Defining I/O pins

FIO/GPIO acess is defined using a bit number. Bits 0..31 form P0.0 to P0.31, bits 32..63 form P1.0 to P1.31 and so on.

: PIO:          \ port# bit# -- ; -- iobit#
Define a port I/O bit by name. For example

  2 11 PIO: P2.11



IO pin access

: setPin        \ iobit# --
Set the pin high.

: clrPin        \ iobit# --
Set the pin low.

: getPin        \ iobit# -- 0/1
Read the pin state.




IO pin configuration

: isPinsel      \ sel# iobit# --
Set the pin configuration 0..3. By default, nearly all pins are in I/O pin mode after reset.

: isPinmode     \ mode# iobit# --
Set the pin mode 0..3. Not available for CPUs that do not have PINMODE registers.

0 constant PulledUp     \ -- mode#
Used as the mode# with isPinMode above to define how the pin operates as an input.

2 constant NotPulled    \ -- mode#
Used as the mode# with isPinMode above to define how the pin operates as an input.

3 constant PulledDown   \ -- mode#
Used as the mode# with isPinMode above to define how the pin operates as an input.

: isInput       \ iobit# --
Set the pin as an input. Selects GPIO mode.

: isOutput      \ iobit# --
Set the pin as an input. Selects GPIO mode.




Test code for LPC-E2468

decimal

4 16 pio: led2
4 17 pio: led1

: tled1         \ --
  led1 isOutput
  begin
    led1 clrPin  200 ms
    led1 setPin  200 ms
  key? until
;

: tled2         \ --
  led2 isOutput
  begin
    led2 clrPin  200 ms
    led2 setPin  200 ms
  key? until
;



Test code for LPC-P2148

decimal

0 15 pio: but1
0 16 pio: but2

\ but1 getPin .
\ but2 getPin .