I wrote some Python to read data from an SPI A/D converter MCP3208 using:

dat = spi.xfer([0x06, 0x00, 0x00])

for the first channel. The variable dat contains the data which I need to shift and convert to voltage value. All OK so far.

Now I like to read not just a single sample, but say 1024 samples at once. I could achieve this by sending the above command in a for loop, but then the timing between samples is not really guaranteed.

I can not find a way to accomplish this in a single xfer command, because it seems that xfer keeps CS signal active during the transfer of bytes in the list above, no matter how many they are.

The CS behaviour I need would be like:

CS: active active active *release* active active active *release* …

all in the same xfer command.

It seems that xfer2 is not of much help here either, particularly since it also seems to have a bug.

any ideas? :-)

  • You don't really ask a question. Perhaps source another ADC which can do the timing and buffering of readings. Perhaps use a separate micro to do the readings. Perhaps bit bang SPI with DMA to get accurately timed readings.
    – joan
    Sep 16, 2016 at 8:45
  • thanks for quick reply. I think the ADC is quite ok. My point is about the timing between two consequent xfer calls. Please also see the comment below.
    – xaratustra
    Sep 16, 2016 at 9:06
  • You can't get accurately timed samples with the Linux SPI driver. You will need to bit bang SPI using DMA to do what you apparently want on the Pi.
    – joan
    Sep 16, 2016 at 9:25

1 Answer 1


I'm not sure what you mean by Now I like to read not just a single sample, but say 1024 samples at once ? the device does not have a buffer holding multiple readings ... it just sends the latest sample when requested.

If you want 1024 samples then (as you say) put the command in a loop.

Not sure what you mean about timing not being guaranteed. Each command is independent and should get you a reading (the MCP3208 can sample at up to 100ks/s.

This might help from the py-spidev library implementation :

    "xfer([values]) -> [values]\n\n"
    "Perform SPI transaction.\n"
    "CS will be released and reactivated between blocks.\n"
    "delay specifies delay in usec between blocks.\n");
  • Thanks for fast reply. No matter what the clock speed is, sending the above 6 0 0 gives me back 3 bytes containing one ADC sample. Now if I want to go for say 10ksps, then I need to shift 10000 such 6 0 0s using the same number of xfer commands in a loop. While within an xfer command the timing between 6 0 0 is dominated by the SPI clock speed setting, the timing between two xfer commands may is not guaranteed by the SPI clock speed in a loop. So I need a kind of xfer that can transfer say 10000 6 0 0s within the same command.
    – xaratustra
    Sep 16, 2016 at 9:06
  • For that kind of granular timing control you might need to consider a microcontroller instead. Even if you could modify the py-spidev library to suit your needs the .xfer command is going to suffer from interrupts and other OS tasks that are taking place, so still no guarantee of accurate timing... Sep 16, 2016 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.