TomiL
Posts: 2
Joined: Fri May 03, 2013 12:20 pm

Custom time interval between SPI characters

Thu Mar 17, 2016 5:34 pm

I have SPI slave device connected to my RaspberryPi 1 (revison "000e", model B, manufactured by Sony, SPI IC family = BCM2708, implementation BCM2835). My device vendor suggest to interface the device in this way: first send command byte (0x03), wait for at least 10mS and then send data byte (0x04). I tried to do this with python (via py-spidev library), but when using 2 separate "spi.xfer()" command (or "spi.xfer2()"; btw, they do the same) I get ChipSelect assert between them:

Code: Select all

import spidev
import time

...
spi = spidev.SpiDev()
spi.open(0,0)
spi.mode = 0b01
resp1 = spi.xfer([0x03], 500, 10000)
resp2 = spi.xfer([0x04], 500, 10000)
...
On oscilloscope it looks like that:
SPI_xfer2.png
2 single "xfer" commands
SPI_xfer2.png (5.35 KiB) Viewed 854 times
Then I tried with array of commands:

Code: Select all

...
resp = spi.xfer([0x03, 0x04], 500, 10000)
...
but than I can not get required 10mS gap between sequential command and data byte:
SPI_linux.png
The place where I need custom interval
SPI_linux.png (5.92 KiB) Viewed 854 times
How can I extend time gap between SPI commands without CS assert (red marker)? Do I need to modify C library ("spidev_module.c" or even BCM2708 driver) to accomplished that or can that be done some other way?

User avatar
joan
Posts: 14472
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Custom time interval between SPI characters

Thu Mar 17, 2016 7:35 pm

It may be simplest if you just control the slave select line yourself. Choose a spare GPIO for your own slave select and connect it to your device. Keep it high normally. Set it low before and high after your SPI commands.

TheRayTracer
Posts: 51
Joined: Tue Oct 13, 2015 12:23 am
Location: Australia

Re: Custom time interval between SPI characters

Thu Mar 17, 2016 10:51 pm

According to the documentation: http://tightdev.net/SpiDev_Doc.pdf

There is a subtle difference between the use of xfer() and xfer2(). xfer() - "CS will be released and reactivated between blocks" and xfer2() - "CS will be held active between blocks". In your case I would recommend to use xfer2(). I would also recommend following Joan's advice to use a spare GPIO pin to drive the CS on your slave. This way you can manually hold it active and have full control over it.

TomiL
Posts: 2
Joined: Fri May 03, 2013 12:20 pm

Re: Custom time interval between SPI characters

Fri Mar 18, 2016 11:29 am

Joan, I will definitely try your proposed solution.

TheRayTracer, I did investigate difference between .xfer() and .xfer2() in documentation, but in practice these command do the same, if the one try to send array of commands (I checked it on oscilloscope). Btw, this has been already reported a few times by different sources, e.g. viewtopic.php?t=39384&p=323721 (look at post by drdavros).

Return to “Interfacing (DSI, CSI, I2C, etc.)”