I'm trying to interface with MCP3002 ADC using SPI and it's working nicely except for the amount of the samples I'm able to collect per second. I'm using WiringPi and the code is very simple:
wiringPiSPISetup(0, 2 * 1000 * 1000);
unsigned char buf[2];
for (;;) {
buf[0] = 104; // x start_bit single_mode ch1 msbf xxx
buf[1] = 0;
wiringPiSPIDataRW(0, buf, 2);
// convert buf to an int and print to stdout
}
The problem is that regardless of the clock speed set, the wiringPiSPIDataRW
call takes about 70us which limits the speed of the whole system to 14k samples / sec.
Has anyone had better luck with SPI on Raspberry Pi with very short messages?
The MCP3002 can conservatively handle 80k samples at 3.3V which then assuming some extra clock cycles for asserting the CS line should run at 1.6Mhz clock and perform a RW call in 12.5 usec. Is then the speed limited by WiringPi overhead?
EDIT: I tried using spidev directly using SPI_IOC_MESSAGE
and the speed is the same so it's not WiringPi. I forgot to mention I'm using a slightly older Raspberry board, v1 rev.B
if I'm not mistaken. lsmod
shows that spi_bcm2835
is loaded, so that should be fine. Using SPI_IOC_MESSAGE
with an array of 128 messages gets the speed up to 45k samples / sec, but that's about as high as it will go.
I appreciate any pointers. Thanks.