230
SPICmd
Type
Subroutine
Invocation
SPICmd(channel, writeCnt, writeData, readCnt, readData)
Parameter
Method
Type
Description
channel
ByVal
Byte
The SPI channel number (1-4).
writeCnt
ByVal
integral
The number of bytes to write (0 65535).
writeData
ByRef
any type
The variable containing the data to write.
readCnt
ByVal
integral
The number of bytes to read (0 65535).
readData
ByRef
any type
The variable in which to place the data read.
Discussion
The routine allows you to send and/or receive data from a device connected to the processors SPI bus
(the holes on the end of the ZX device between pins 1 and 24). The specified channel must have been
previously opened with a call to OpenSPI(). If the channel has not been opened, the results are
undefined.
If both writeCnt and readCnt are zero the routine returns immediately without doing anything. You
may specify the value 0 for writeData or readData if no data is being provided. If the value of
readCnt exceeds the size of the readData variable, the additional bytes will be written to subsequent
memory locations, possibly with undesirable results.
The execution of the SPI command occurs in four phases:
Chip select is asserted by setting the previously specified pin to a logic zero level.
If the writeCnt parameter is non-zero, the data bytes at writeData are written sequentially to
the SPI interface. The data returned by the SPI device during this phase is discarded.
If the readCnt parameter is non-zero, the existing data beginning at readData are written to the
SPI device and the returned bytes are stored sequentially in the specified variable. That is, the
byte at readData(1) is sent to the device and the byte that it sends back is stored at
readData(1). The same occurs for readData(2), etc.
Finally, chip select is deasserted by setting the previously specified pin to a logic one level.
Whether you use writeData or readData or both depends on the particulars of the device youre
using. In some cases, youll need to populate readData and in other cases not. Careful study of the
datasheet of the target device will be required to determine how SPICmd() can be used to interface with
it.
Example
Dim odata(1 to 2) as Byte, idata(1 to 10) as Byte
Odata(1) = &H06
Odata(2) = &H00
Call SPICmd(1, 2, odata(1), 10, idata(1))
In this example idata is not initialized before calling SPICmd(). If your SPI device needs specific data
written to it during the read phase idata would need to be initialized before the call.