1

I managed to successfuly read packets from MCP3208 ADC using SPI on Raspberry Pi - I can do that both with spidev.h library or with WiringPi.

The problem is that when I want much higher speed, I can preload - say - 1000 queries into an array (each query is 3 bytes) and fire it all up at once with wiringPiSPIDataRW function. In theory I should get 1000 samples as fast as possible, but unfortunately it doesn't work like that.

According to the datasheet, before each read (query) I need to set up chip select to high for at least one clock cycle. Unfortunately I don't seem to have any control over the chip select pin while the read/write sequence is fired, so the question is - is there some mode that will fire the chip select every n bytes?

Or if not, what other fast ways are there to read data from SPI?

Without reading multiple bytes in a single read/write sequence, it gets pretty slow - about 60kHz and I need more than that.

1

Both wiringPi and spidev will be using the underlying Linux SPI driver.

At a guess you are using the Pi3B as other Pi models will probably top out at 20ksps.

My pigpio and the bcm2835 library use their own driver which will probably allow much higher sample rates.

For a metric see http://abyz.me.uk/rpi/pigpio/faq.html#How_fast_is_SPI

  • Does it mean that when I call a single read in a loop it will be much faster than the equivalent with Linux SPI driver? Thanks a lot for the link - I've been looking for something like this for a long time and I found nothing! From the plots I can see that the speed is almost the same as with linux spi driver at 3 bytes per sample and 2MHz – Max Walczak Mar 12 '18 at 11:34
  • 1
    I have not tested multiple SPI requests with the kernel driver. There is a spi.cs_change field in the C IOCTL. Perhaps edit the wiringPi source wiringPiSPI.c and change the value to 1 and then rebuild the library. That might then toggle the chip select at each transaction. – joan Mar 12 '18 at 11:42
  • Can you elaborate on how is the cs_change executed? If I have spi.len of 128 bits and set bits per word to 16, will it turn CS on and off every 16 bits being transferred? That is exactly what I would want to achieve. – Max Walczak Mar 12 '18 at 12:18
  • 1
    You need to look at the spi_ioc_transfer structure. Each member is one SPI transaction. The cs_change will presumably set the CS change around that transaction. You pass an array of those structures to the SPI transfer. If this is not how you/wiringPi are handling multiple transactions then I've wasted your time (although it's quite easy to write your own version in C, wiringPi is a thin wrapper around the SPI driver). – joan Mar 12 '18 at 12:47
  • This seems to solve my problem quite exactly! Thank you a lot for your time and effort. – Max Walczak Mar 12 '18 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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