|Online Development Notebook > Index > Programming Overview > Device Commands > SPIMaster|
SPIMaster - Communicate with (read⁄write) a SPI device
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:
|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.|
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 in to 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.
|Public Declare Function USBm_SPIMaster _|
Lib "USBm.dll" _
(ByVal device As Byte, _
ByRef countarray As Byte, _
ByRef dataarray As Byte) _
| 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.
|int USBm_SPIMaster( unsigned char device, unsigned char *count, unsigned char *data );|
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:
|1||Number of Bytes (0-6)|
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:
|1||Number of Bytes (0-6)|
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.
Hardware: U401 USB Interface U421 USB Interface U451 USB Interface
Programming: USBm DLL Programming Download Files
Application Notes: U4x1 Application Notes Misc Applications and Information FAQ
While every effort has been made to make sure that the information posted on this site is correct, the author can not be held liable for any damages whatsoever for losses as a result of the application of this information. Use this information at your own risk.
USBmicro can design your custom and semi-custom USB product. Email about USB design can be directed to " Robert " at usbmicro.com.
Copyright © USBmicro, L.L.C., 2002-2010