Set up the device to use SPI (3-wire interface)
Description:
This is a function that sets the SPI subsystem attributes and the direction of the SPI lines. The SPI subsystem can be either a SPI master or a SPI slave. The InitSPICmd command sets the port lines appropriately for selection of either master or slave. Clock frequency, polarity, and phase are also set. SPI bytes are sent out MSB first. The SPI master clock can operate at up to 2 Mbits⁄s in master mode.
Command Syntax: (USBm.dll)
USBm_InitSPI( device, data )
The USBm_InitSPI function syntax has these parts:
Part | Description |
device | A zero-based index to address the appropriate USB device. |
data | SPI subsystem attribute data. |
Remarks:
The direction of the SPI port lines (A.5-MOSI, A.6-MISO, A.7-SCK, and, in the case of SPI slave operation, A.4-SS) is set based on the command to set the SPI subsystem to either master or slave.
When set as a master SPI device, A.5-MOSI and A.7-SCK are set as outputs. A.6-MISO is set to input. A.4-SS is unaffected.
When set as a slave SPI device, A.5-MOSI, A.7-SCK, and A.4-SS are set as inputs. A.6-MISO is set to output.
Turning off the SPI subsystem with the InitSPICmd command will still set the port direction of the SPI port lines as a SPI master. If you wish to turn off the SPI and use the SPI port lines, issue a DirectionACmd command after the InitSPICmd command.
The format of the data byte written to control the SPI subsystem attributes is divided into bit fields.
Bits 1 and 0 control the clock frequency: (applicable to the master mode)
00 = 2 Mbits⁄s
01 = 1 Mbits⁄s
10 = 500,000 bits⁄s
11 = 62,500 bits⁄s.
Bits 3 and 2 control the clock polarity and phase:
00 = clock starts⁄idles low, data sampled on falling edge (common SPI name: Mode 1)
01 = clock starts⁄idles low, data sampled on rising edge (common SPI name: Mode 0)
10 = clock starts⁄idles high, data sampled on rising edge (common SPI name: Mode 3)
11 = clock starts⁄idles high, data sampled on falling edge. (common SPI name: Mode 2)
Bits 5 and 4 control the SPI mode:
00 = SPI disabled
01 = SPI master
10 = SPI slave
11 should not be used.
Bits 7 and 6 should be written as 00.
Operation:
The U4x1 has a six byte buffer for SPI slave interaction. The SPI Slave Write command (from the PC host) places six bytes of data into this buffer. (Depending on the application, less than six bytes may be valid for that app.) These six bytes are the bytes that the PC host will transfer to the SPI master when the SPI master communicates. The index byte of the SPI Slave Write command, if set to 0, points to the first byte of the buffer as being the first byte to transmit to the master. Use only 0 for this revision of the firmware.
The host command SPI Slave Read command returns the contents of this six byte buffer.
The SPI master selects the SPI subsystem by bringing SS low. This allows SPI transfer from the master. The master transfers a byte into the U4x1 and receives the first byte in the buffer. The byte sent by the master moves into the first position of the buffer. In this way the buffer contents, from one to six bites, are moved to the master and replaced by the bytes sent by the master. No more than six bytes should be transmitted, as the buffer is only six bytes long. More bytes than this will result in data loss. The master should deselect the SPI subsystem by bringing SS high.
At this point the host PC should read the buffer (SPI Slave Read) and process the data. A SPI Slave Write will load additional data to the U4x1 and reset the index pointer to the start of the six byte buffer.
To optimize the transfer of data from the SPI master handshaking should be implemented using normal port I⁄O lines to indicate that the U4x1 is available for another access. This implementation will, naturally, be dependent on the application. The complexities of the handshake specific to the application as well as restrictions of the OS will determine the rate of data transfer.
VB Declaration
Public Declare Function USBm_InitSPI _ Lib “USBm.dll” _ (ByVal device As Byte, _ ByVal data As Byte) _ As Integer |
VB Example
USBm_InitSPI 6, &H10 |
This code fragment will initialize the SPI as a master, at 2 Mbits⁄s, and with the clock starting low and data valid on the falling edge.
C Prototype
int USBm_InitSPI( unsigned char device, unsigned char data ); |
C Example
RobotBASIC
usbm_InitSPI(ne_DeviceNumber,ne_Specs)
Returns true if successfull, false otherwise. It sets the attributes of the SPI system.
Raw Command Format:
Byte Number | Description |
0 | 14h: InitSPICmd |
1 | Byte Data |
2 | <not used> |
3 | <not used> |
4 | <not used> |
5 | <not used> |
6 | <not used> |
7 | <not used> |
Raw Command Format Details:
Byte 0 contains the command. Byte 1 is the SPI subsystem attribute data. Byte 2 through byte 7 are unused.
The direction of the SPI port lines (A.5-MOSI, A.6-MISO, A.7-SCK, and, in the case of SPI slave operation, A.4-SS) is set based on the command to set the SPI subsystem to either master or slave.
When set as a master SPI device, A.5-MOSI and A.7-SCK are set as outputs. A.6-MISO is set to input. A.4-SS is unaffected.
When set as a slave SPI device, A.5-MOSI, A.7-SCK, and A.4-SS are set as inputs. A.6-MISO is set to output.
Turning off the SPI subsystem with the InitSPICmd command will still set the port direction of the SPI port lines as a SPI master. If you wish to turn off the SPI and use the SPI port lines, issue a DirectionACmd command after the InitSPICmd command.
The format of the data byte written to control the SPI subsystem attributes is divided into bit fields.
Bits 1 and 0 control the clock frequency: (applicable to the master mode)
00 = 2 Mbits⁄s
01 = 1 Mbits⁄s
10 = 500,000 bits⁄s
11 = 62,500 bits⁄s.
Bits 3 and 2 control the clock polarity and phase:
00 = clock starts⁄idles low, data sampled on falling edge (common SPI name: Mode 1)
01 = clock starts⁄idles low, data sampled on rising edge (common SPI name: Mode 0)
10 = clock starts⁄idles high, data sampled on rising edge (common SPI name: Mode 3)
11 = clock starts⁄idles high, data sampled on falling edge. (common SPI name: Mode 2)
Bits 5 and 4 control the SPI mode:
00 = SPI disabled
01 = SPI master
10 = SPI slave
11 should not be used.
Bits 7 and 6 should be written as 00.
Raw Command Response Format:
Byte Number | Description |
0 | 14h: InitSPICmd |
1 | <not used> |
2 | <not used> |
3 | <not used> |
4 | <not used> |
5 | <not used> |
6 | <not used> |
7 | <not used> |
Raw Command Response Format Details:
Byte 0 contains the command. Byte 1 through byte 7 are unused.
Raw Command Example Usage:
14-10-00-00-00-00-00-00 will initialize the SPI as a master, at 2 Mbits⁄s, and with the clock starting low and data valid on the falling edge.