« Close

Datasheets and User Guides

App Notes

Software & Driver

 

5.2.17 - SPI

Sends and receives serial data using SPI synchronous communication.

Table 5.2.17-1. SPI Command Response

Command:    
Byte    
0 Checksum8  
1 0xF8  
2 4 + NumSPIWords 
3 0x3A  
4 Checksum16 (LSB) 
5 Checksum16 (MSB) 
6 SPIOptions  
    Bit 7: AutoCS
    Bit 6: DisableDirConfig
    Bits 1-0: SPIMode (0=A, 1=B, 2=C, 3=D)
7 SPIClockFactor  
8 Adv. Options Bits 2-0: Number of bits in the final byte.
    Where 0 = 8.
9 CSPinNum  
10 CLKPinNum  
11 MISOPinNum  
12 MOSIPinNum  
13 NumSPIBytesToTransfer 
14 SPIByte0  
... ...  
     
     
Response:    
Byte    
0 Checksum8  
1 0xF8  
2 1 + NumSPIWords 
3 0x3A  
4 Checksum16 (LSB) 
5 Checksum16 (MSB) 
6 Errorcode  
7 NumSPIBytesTransferred 
8 SPIByte0  
... ...  
  • NumSPIWords: This is the number of SPI bytes divided by 2. If the number of SPI bytes is odd, round up and add an extra zero to the packet.
  • SPIOptions: If AutoCS is true, the CS line is automatically driven low during the SPI communication and brought back high when done. If DisableDirConfig is true, this function does not set the direction of the lines, whereas if it is false the lines are configured as CS=output, CLK=output, MISO=input, and MOSI=output. SPIMode specifies the standard SPI mode as discussed below.
  • SPIClockFactor: Sets the frequency of the SPI clock according the following approximate formula: Frequency = 1000000/(10+10*(256-SPIClockFactor), where passing a value of 0 corresponds to a factor of 256, and thus a maximum frequency of about 100 kHz.
  • Advanced Options: Bit [2:0] allow the number of bits to be transfered during the final byte to be specified, where 0 = 8. This makes it possible to transfer any number of bits within packet size limitations. For example, to transfer 11 bits set the number of bytes to 2 and set bits [2:0] of the advanced options to 3. The LabJack will transfer a full byte of 8 bits, then transfer 3 more bits. The 3 most significant bits in the final byte will be sent and data read during those bits will be in the most significant bits of the final byte in the data. This feature requires firmware 1.21.
  • CS/CLK/MISO/MOSI -PinNum: Assigns which digital I/O line is used for each SPI line. Value passed is 0-19 corresponding to the normal digital I/O numbers as specified in Section 2.8.
  • NumSPIBytesToTransfer: Specifies how many SPI bytes will be transferred (1-50).

The initial state of SCK is set properly (CPOL), by this function, before CS (chip select) is brought low (final state is also set properly before CS is brought high again). If CS is being handled manually, outside of this function, care must be taken to make sure SCK is initially set to CPOL before asserting CS.

All standard SPI modes supported (A, B, C, and D).

Mode A: CPOL=0, CPHA=0

Mode B: CPOL=0, CPHA=1

Mode C: CPOL=1, CPHA=0

Mode D: CPOL=1, CPHA=1

If Clock Phase (CPHA) is 1, data is valid on the edge going to CPOL. If CPHA is 0, data is valid on the edge going away from CPOL. Clock Polarity (CPOL) determines the idle state of SCK.

Up to 50 bytes can be written/read. Communication is full duplex so 1 byte is read at the same time each byte is written.

11 comments

Hi Everyone:

The modes listed for the U6 above appear to be incorrect; they don't match those listed for the U3 or U9 and judging from hooking the U6 up to an o-scope they should in fact match the modes for U3/U9. I figured this out while trying to operate an IC in mode A above, only to figure out that I needed to use mode D to get correct operation of my device (then looked on a scope to realize the clock phase and polarity was what I wanted it to be when using mode D). Thanks!

 

Thank you for catching that.

Page updated.

 

 

how can I manually set or unset the CS pin?

Thanks a lot!

 

Just pointing out a minor error, byte 7 should be SPIClockFactor in the table.  Thanks!

Fixed.  Thanks.

The comment in the sample code in Section 4.3.10 mixes up modes A and D as well.

 

Thanks for catching that. The mode A comment has been fixed on that page.

kallesai's picture

Hi, is it possible to assign a chip select signal, other than a labjack CIO/MIO/FIO/EIO -signal? I'm using PCA9502 IO-expanders in my design and need to use the expander's outputs as chip select signals to AD-converters which are behind SPI. So I'm communicating from U6 through SPI to PCA9502 to get more chip-select signals in use.

I'm programming in Python and my worry is on u6.py SPI-function:

The CSPINNum should have the correct chip select signal assigned to. Will the SPI-function work, if I assign to CSPINNum the chip select(EIO4 in my design) for PCA9502 and then manually operate with PCA9502's extra chip select signals for the AD-converters?

Example:

Communicate to PCA9502 to get more chip selects:

device.spi(data, AutoCS=True, DisableDirConfig = False, SPIMode = 'A', CSPINNum = 14, CLKPinNum = 9, MISOPinNum = 10, MOSIPinNum = 8)

Then manually assign the expanders chip select down with "data"-parameter and use the U6 SPI function again. In other words, does the CSPINNum need to be a labjack pin?

The reason why I'm doing this, is because all U6 outputs are already in use.

LabJack Support's picture

The SPI function can only control the LabJack's DIO (CIO/MIO/FIO/EIO).

Your idea to manually control the chip select lines will work. To completely disable the CS control clear the auto-CS option bit. It may also be helpful to disable auto-direction control by setting DisableDirConfig high.

Let me know if you need more help.

kallesai's picture

Thanks for the quick response. I made some testing and verified that this works manually controlled. It required the setting of DisableDirConfig to high, just like you described. Otherwise it didn't work.