228
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.
For an SPI channel that is opened with an non-zero rxDelay parameter specified (see OpenSPI()), a
delay is implemented prior to each SPI cycle for which the data read is placed in the readData buffer,
i.e., the third phase described above. The delay value specified is interpreted as the number of cycles of
the SPI clock frequency (but ignoring the SPI2X configuration bit). Of course, during the delay time the
SPI clock signal (SCK) will be idle. This delay is useful when communicating with slave devices that must
compute data values to return, for example, a ZX-24n operating in SPI slave mode.
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))