embello

Embedded software from JeeLabs

Embello Home Page
Hardware Documentation
Forth Library Documentation

SPI communication driver

This describes the portable bit-banged version of the SPI driver.

Each SPI transaction consists of the following steps:

Before using SPI, you need to call spi-init to initialise the pins and hardware device.

API

: spi-init ( -- )  \ set up bit-banged SPI
: +spi ( -- ) ssel @ ioc! ;  \ select SPI
: -spi ( -- ) ssel @ ios! ;  \ deselect SPI
: >spi ( c -- ) >spi> drop ;  \ write byte to SPI
: spi> ( -- c ) 0 >spi> ;  \ read byte from SPI
: >spi> ( c -- c )  \ bit-banged SPI, 8 bits

Variables

PA4 variable ssel  \ pin used as slave select

Constants

The SCLK, MISO, and MOSI constants should be defined before including this driver, if you want to use SPI on other pins than the default PA5, PA6, and PA7, respectively.

The ssel variable is used during each transaction. It defaults to PA4, but can be changed between transactions to connect to different slaves.

Note: only the ssel pin set when spi-init is called will be properly set up as GPIO output. To connect to addiitonal slave devices, you’ll need to initialise the other pins yourself, e.g. to use PB0 as additional slave select:

PB0 ios!  OMODE-PP PB0 io-mode!

Once configured, this will let you switch to that slave using “PB0 ssel !”.