This describes the portable bit-banged version of the SPI driver.
Each SPI transaction consists of the following steps:
>spito send a byte without reading the result
spi>to send a zero byte and return the result
>spi>to both send a byte and read back the result
Before using SPI, you need to call
spi-init to initialise the pins and
: 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
PA4 variable ssel \ pin used as slave select
MOSI constants should be defined before including
this driver, if you want to use SPI on other pins than the default
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
additional slave select:
PB0 ios! OMODE-PP PB0 io-mode!
Once configured, this will let you switch to that slave using “
PB0 ssel !”.