Communicate with (read⁄write) a SPI device
Description:
This is a function that is used to communicate with a slave SPI device. Zero to six bytes can be transferred in a single command. For each byte sent to a SPI slave device, a byte is returned. The returned bytes are in the response to the command
Command Syntax: (USBm.dll)
USBm_SPIMaster( device, countarray, dataarray )
The USBm_SPIMaster function syntax has these parts:
Part | Description |
device | A zero-based index to address the appropriate USB device. |
countarray | A data array to hold the number of bytes received (sent). Minimum array size of 1. |
dataarray | The function causes this array of variables to be changed to the data received from the SPI transfer. Minimum array size of 6. |
Remarks:
When the InitSPI function sets the SPI subsystem to be a master, only the SPI lines SCK, MISO, and MOSI are configured. A SPI device needs to be addressed with a slave select signal. Any remaining line of the U4xx can be set to be an output that controls the slave device SS input.
The slave must be selected prior to issuing the USBm_SPIMaster command and deselected afterward.
SPI will send out a byte and receive a byte at the same time. As the first clock pulse becomes valid, one bit of the output byte will appear on MOSI, while the state of MISO is shifted into the U4x1. Another clock pulse, another MOSI bit shifted out, another MISO bit shifted in. After 8 clock pulses, one byte is out from the master to the slave, one is shifted in from the slave to the master.
If you only need to shift bytes in, you can write “dummy” bytes out.
VB Declaration
Public Declare Function USBm_SPIMaster _ Lib “USBm.dll” _ (ByVal device As Byte, _ ByRef countarray As Byte, _ ByRef dataarray As Byte) _ As Integer |
VB Example
Dim dataarray(6) As Byte Dim countarray(1) As Byte dataarray(0) = &H55 countarray(0) = &H01 USBm_SPIMaster 0, countarray(0), dataarray(0) |
The slave must be selected (perhaps an active low CS) prior to issuing the SPIMaster function and deselected afterward.
This code fragment will shift a single byte (55h) out the SPI port.
C Prototype
int USBm_SPIMaster( unsigned char device, unsigned char *count, unsigned char *data ); |
C Example
RobotBASIC
usbm_SPIMaster(ne_DeviceNumber,se_DataBytes)
Returns a string of byte values inputted from the SPI master after it has read the corresponding number of bytes from the data string. Use ArrayStr() to extract the byte values and Char() to create the data string.
Raw Command Format:
Byte Number | Description |
0 | 15h: SPIMasterCmd |
1 | Number of Bytes (0-6) |
2 | Byte Data |
3 | Byte Data |
4 | Byte Data |
5 | Byte Data |
6 | Byte Data |
7 | Byte Data |
Raw Command Format Details:
Byte 0 contains the command. Byte 1 contains the number of bytes to send. The number of bytes can 0 to 6. Byte 2 through byte 7 are the transmitted bytes.
Raw Command Response Format:
Byte Number | Description |
0 | 15h: SPIMasterCmd |
1 | Number of Bytes (0-6) |
2 | Byte Data |
3 | Byte Data |
4 | Byte Data |
5 | Byte Data |
6 | Byte Data |
7 | Byte Data |
Raw Command Response Format Details:
Byte 0 contains the command. Byte 1 contains the number of bytes received (sent). Byte 0 through byte 7 contain the received data.
Raw Command Example Usage:
When the InitSPICmd command sets the SPI subsystem to be a master, only the SPI lines SCK, MISO, and MOSI are configured. A SPI device needs to be addressed with a slave select signal. Any remaining line of the U401 can be set to be an output that controls the slave device SS input.
The slave must be selected prior to issuing the SPIMasterCmd command and deselected afterward.
Issuing 15-01-55-00-00-00-00-00 will shift a single byte (55h) out the SPI port.