271 of 318
ZBasic System Library 261 ZX Microcontroller Family 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 to the device. readCnt ByVal integral The number of bytes to read (0 – 65535). readData ByRef any type The variable in which to place the data read from the device. Discussion This routine allows you to send and/or receive data from a device connected to the processor’s SPI bus (on a 24-pin ZX device, the holes on the end of the 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 either 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: The chip select is asserted by setting the previously specified pin to the active level. The active level (typically logic zero) is specified by bit 6 in the flags parameter passed to OpenSPI(). 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 the device sends back is stored at readData(1). The same occurs for readData(2), etc. Finally, the chip select is deasserted by setting the previously specified pin to the inactive level. Whether you use writeData or readData or both depends on the particulars of the device you’re using. In some cases, you’ll 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 communicate 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 Double Speed 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 Call OpenSPI (1, 0, 12) odata(1) = &H06 odata(2) = &H00 Call SPICmd(1, 2, odata, 10, idata)