Alan Johnstone
Posts: 48
Joined: Tue Jan 08, 2013 4:35 pm

spidev xfer2 v xfer

Wed Apr 03, 2013 7:01 pm

I am using spidev to transfer data between my Rpi and Arduino with the RPi as Master.
I understand from using help(spidev) that xfer transfers the list of data given
raising and lowering the ss line inbetween each byte while xfer2 lowers the ss line at the begining
of the data transmission and raises it at the end of the transmission of all the data in the list.

When I look at what I am getting on the Arduino it would seem that both versions are acting like xfer2
ie doing the transfer with one lowering of the SS line. This is important to me as I am using interrupts on the SS line to detect a new set of data.

Has anybody else seen this or have I misunderstood the description? I can find no other documentation.

As an additional question does anybody know how to set the delay between bytes when data is sent
using the described xfer2 method?

User avatar
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: spidev xfer2 v xfer

Wed Apr 03, 2013 8:41 pm

Bill Greathouse seems to know about these things Alan. Pretty sure I've read somewhere in one of the add-on boards sections, something where it said that xfer2 doesn't release the spi line between transfers but xfer does. Hopefully Bill (BGreat) will pop in here soon and throw some light on it. :D

I'm glad you asked this question because I'm interested in the answer too. :D
Alex Eames RasPi.TV, RasP.iO

Alan Johnstone
Posts: 48
Joined: Tue Jan 08, 2013 4:35 pm

Re: spidev xfer2 v xfer

Thu Apr 04, 2013 8:38 am

Looking in more detail at the output in the Arduino I am forced to the conclusion that:

1 xfer and xfer2 do exactly the same thing with a list of data.
2 They both bring the SS line LOW and send ALL the data in the list and then bring the
SS line up HIGH
3 THEN they both repeat the SS LOW/SS HIGH sending no data.

It was the last bit that was perplexing me.

User avatar
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: spidev xfer2 v xfer

Thu Apr 04, 2013 1:10 pm

I will look at this again, but I do remember my initial testing showed the CSx was not released between transactions when using xfer2 unless another SPI device was accessed, the SPI connection was closed, or an xfer transaction was initiated. I will not be able to look at this in depth until this weekend. In the meantime, you could simply use a GPIO pin to manually control your peripheral selection with any timing you desire. Just program as an output, set low before transmission and set high when you have completed.


Alan Johnstone
Posts: 48
Joined: Tue Jan 08, 2013 4:35 pm

Re: spidev xfer2 v xfer

Sat Apr 13, 2013 7:43 pm

I found this on a google site
Some new update after a day, the code r = spi.xfer2([1,(8+adcnum)<<4,0])
could be altered like this r = spi.xfer2(
  • ,speed_hz,delay_usecs,bits_per_word) if you put 0 for delay_usecs nothing happens it still 220us delay
    if i put 1000 in delay_usecs the delay is 1220us.
    This means that the pi is deciding when SPI get to communicate. Howe to work around this i still don't know

    So I tried it and it and xfer2() does accept these arguments.

    Whether they are doing anything or not is anothe matter.

    I tested my setup which is getting 12 bytes of data from the Arduino along with a one byte checksum

    Here are the results for % errors at different speeds using a small sample of 100 tries each

    speed 150,000 14.5 %
    speed 250,000 12.2 %
    speed 1,000,000 20.0 %

    Here are the results using a speed of 250,00hz and different udelay

    udelay 220 12.2 %
    udelay 440 10.7 %
    udelay 880 16.6 %

    The figures look to be nearly the same within the experimantal error of the test (eg doing the 880 delay again I got 13.9%)

    So unless anybody can tell me how to reduce the error rate I will live with it and get on with life.


Posts: 1
Joined: Thu Oct 10, 2013 4:33 pm

Re: spidev xfer2 v xfer

Thu Oct 10, 2013 4:48 pm

I hooked up a logic analyzer to the Raspberry PI SPI interface and captured several waveforms showing the SPI bus activity vs the SPI python commands. Just confirming that xfer2 and xfer do indeed look/operate the same. Also detailed what the speed_hertz and delay_us command options actually do. Raspberry PI SPI waveforms are at


Return to “Python”