How to achieve a high sampling speed using an ADC with Raspberry Pi?

时间:2018-03-25 21:01:57

标签: raspberry-pi3 spi adc

So, I am using the LTC 2366, 3 MSPS ADC and using the code given below, was able to achieve a sampling rate of about 380 KSPS.

#include <stdio.h>
#include <time.h>
#include <bcm2835.h>

int main(int argc, char**argv) {
    FILE *f_0 = fopen("adc_test.dat", "w");
    clock_t start, end;
    double time_taken;

    if (!bcm2835_init()) {
        return 1;
    }

    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
    bcm2835_spi_setClockDivider(32);
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
    int i;
    char buf_[0] = {0x01, (0x08|0)<<4, 0x00}; // really doesn't matter what this is
    char readBuf_0[2];
    start = clock();
    for (i=0; i<380000; i++) {
        bcm2835_spi_transfernb(buf_0, readBuf_0, 2);
        fprintf(f_0, "%d\n", (readBuf_0[0]<<6) + (readBuf_0[1]>>2));
    }
    end = clock();
    time_taken = ((double)(end-start)/CLOCKS_PER_SEC);
    printf("%f", (double)(time_taken));
    printf(" seconds \n");
    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}

This returns about 1 second every time.

When I used the exact same code with LTC 2315, I still get a sampling rate of about 380 KSPS. How come? First of all, why is the 3 MSPS ADC giving me only 380 KSPS and not something like 2 MSPS? Second, when I change the ADC to something that's about 70% faster, I get the same sampling rate, why is that? Is that the limit of the Pi? Any way of improving this to get at least 1 MSPS?

Thank you

1 个答案:

答案 0 :(得分:0)

我测试了一些Raspberry Pi SPI并发现spi有一些开销。在我的情况下,我尝试了pyspi,其中一个字节似乎至少需要15us,两个单词之间需要75us(参见these captures)。这比你测量的要慢,对你有好处!

增加SPI时钟会改变交换的长度,但不会改变开销。因此,关键时间不会改变,因为开销是限制因素。 380ksps意味着2.6个字节,可能接近你的开销?

提高ADC速度的更简单方法是使用并行ADC而不是串行 - 它有可能增加overall speed to 20Msps+