Library
#include <SPI.bas>
Interface
optional #defines-
SPIclkNEGATIVE
SPIpreSample
TERMINATE_ON_0_ONLY -- ignore CR,LF as special characters
SUB SPIIN (CSpin, INpin, CLKpin, OUTpin, LSBfirst,
OUTcnt, BYREF OUTlist as STRING, INcnt, BYREF INlist as STRING)
SUB SPIOUT (CSpin, CLKpin, OUTpin, LSBfirst, OUTcnt,
BYREF OUTlist AS STRING)
SUB SPIBI (CSpin, INpin, CLKpin, OUTpin,
LSBfirst, BIcnt, BYREF OUTlist as STRING, BYREF INlist as STRING)
Description
These libraries are written for the ARM being the
master, with possible multiple slaves selected by different CS lines.
LSBfirst selects the bit order for the SPI
routines.
A #define is used to set clock mode #define
SPIclkNEGATIVE will invert the normally low clock. To use a normally high
clock this #define must be placed before the #include <SPI.bas>
Another #define can be used to sample data before the
clock, #define SPIpreSample. The default case is to sample data after each
clock.
SPIIN supports the loosely defined serial protocol used
by a variety of manufacturers. The desired device is selected by asserting
CSpin LOW. If there is no
CSpin , the value should be
set to -1.
In the simplest case, INpin is used to input data clocked by
CLKpin, to fill the INlist. (OUTcnt will be 0 and
OUTlist empty)
In bi-directional cases, OUTcnt bytes of OUTlist will
be output on OUTpin before reading the INlist. OUTcnt
may be -1 and OUTlist empty. If OUTcnt is 0, then
OUTlist bytes will be sent until a value of 0 is found (the 0 will not
be sent). An empty OUTlist can be represented by "".
It is also allowable to have INpin equal to OUTpin, in
which case that pin will be driven for the OUTlist and then converted
to an input for INlist.
INlist will be filled with INcnt bytes. If
INcnt is 0 then the INlist will be filled with bytes until a
0, CR or LF character is received. Note that no bounds checking is
performed on the input, and if a 0, CR, or LF is never received then this
routine will hang.
Data is shifted in MSB first and each element of the InputList is
filled with a byte of data. The LSBfirst can be used to change the bit order.
Data is shifted in at 330 Kbits/sec
SPIOUT supports the loosely defined serial protocol
used by a variety of manufacturers. The desired device is selected by
asserting
CS_pin LOW. If there is no
CS_pin, the value
should be set to -1.
In the simplest case, out_pin is used to output data clocked by
clk_pin, from the OutputList.
OutputList can contain a list of constants, variables,
"constant-string" or stringame$ without
a count. The latter will send out bytes starting from
stringname$(0) until a 0 byte is read. The 0 is not shifted out,
if that is required either a count should be specified so as to include
the 0.
Data is shifted out MSB first and each element of the OutputList is
treated as a byte. The LSBfirst can be used to change the bit order.
Data
is shifted out at 300 Kbits/sec
SPIBI supports the loosely defined serial protocol used
by a variety of manufacturers. The desired device is selected by asserting
CS_pin LOW. If there is no
CS_pin , the value should be
set to -1.
SPIBI will shift out1, out2, out3 bytes out on
out_pin while reading 3 or more bytes into the InputList
from in_pin. For each bit the clk_pin will be pulsed.
Data is shifted in/out MSB first. The LSBfirst can be used to change the bit
order.
Data is shifted in/out at 220 Kbits/sec
Example
#include <SPI.bas>
,,,
DIM
shortResponse(20) as string
' microMega FPU uses MSB
first -- positive clock
shortResponse=
chr(&HFF)+chr(&HFF)+chr(&HFF)+chr(&HFF)+chr(&HFF)+chr(&HFF)+chr(&HFF)+chr(&HFF)+chr(&HFF)+chr(&HFF)
SPIOUT (-1,14,15, 0, 11, shortResponse) ' reset FPU
WAIT (10)
shortResponse= chr(&HF0)
SPIOUT (-1,14,15, 0, 1,
shortResponse) ' sync
FPU
save_time = TIMER
while ((TIMER - save_time) <
15) ' wait 15 uSec
loop
SPIIN (-1,14,15, 0, 0,"",
1,
shortResponse) '
get 1 byte status back
if
(shortResponse(0) <> &H5C ) then
print " No FPU
found", status
end
endif
print "FPU
found"
shortResponse= chr(&HF3)
SPIOUT (-1,14,15,
0, 1,
shortResponse) '
get version
INPUT
(14)
' allow FPU to drive this bi-directional line
while
(IN(14))
' wait for FPU to drive that line low
loop
shortResponse=
chr(&HF2)
SPIOUT (-1,14,15, 0, 1,
shortResponse) '
get string
save_time = TIMER
while ((TIMER - save_time) <
15) ' wait 15 uSec
loop
SPIIN (-1,14,15, 0, 0,"",
0, shortResponse) ' get a 0 terminated string back
print "version =
"; shortResponse;
For an example of an SPI device that uses non-byte oriented command see SHIFTIN, SHIFTOUT example.