NumiX
Posts: 5
Joined: Sat Apr 27, 2019 2:35 pm

SPI: Problem with fast ADC sampling

Sat Apr 27, 2019 2:54 pm

Hey,

im working on a project where i need to sample analouge Signals as fast as possible with a Raspberry Pi. For this case i Use the 3 B+ model an the bcm2835 library with python. The generator puts out a perfect sine-wave at a freq. of a 10kHz.The Sampling looks quite good, but if you look closer you see that the sine-wave in not very accurate. For some reasons the samplnings has not the exact distance to each other. I thougt this is because the OS is doing some stuff in the background so i tried to activte DMA (i set the buffer size >96 bytes as mentioned here :viewtopic.php?t=216532 this should be enough) But it doesnt seem to work. Do you have any suggestions where this is coming from? Thanks in advance :)

Image

This is the Code

Code: Select all

#include <stdio.h>
#include <bcm2835.h>
int main(int argc, char const *argv[]) {
    if (!bcm2835_init())
    {
      printf("bcm2835_init failed. Are you running as root??\n");
      return 1;
    }
    if (!bcm2835_spi_begin())
    {
      printf("bcm2835_spi_begin failed. Are you running as root??\n");
      return 1;
    }
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE1);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16); // The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default
    
    char tx0[70] = {0,0};
    char rx[] =  {0,0};
    
    FILE *fp;
    fp = fopen( "file.txt" , "w" );
    for(int i=0; i<500; i++) {
      bcm2835_spi_transfernb(tx0,rx,2);
      fprintf(fp, "%02x%02x\n",rx[0],rx[1]);
    }
    fclose(fp);
    return 0;
}
Attachments
sceme.PNG
sceme.PNG (56.65 KiB) Viewed 422 times

NumiX
Posts: 5
Joined: Sat Apr 27, 2019 2:35 pm

Re: SPI: Problem with fast ADC sampling

Mon Apr 29, 2019 6:34 pm

Nobody can help? :|

User avatar
joan
Posts: 14097
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI: Problem with fast ADC sampling

Mon Apr 29, 2019 7:51 pm

There will be a random delay between each sample. Both the bcm2835_spi_transfernb(tx0,rx,2) and the fprintf(fp, "%02x%02x\n",rx[0],rx[1]) calls may be interrupted.

You are only transferring two bytes so DMA will make no difference. I think it is only used to safely fill the hardware FIFOs and they are probably at least 32 bytes anyhow.

NumiX
Posts: 5
Joined: Sat Apr 27, 2019 2:35 pm

Re: SPI: Problem with fast ADC sampling

Mon Apr 29, 2019 8:21 pm

Hm so you think DMA wont help me to get an uninterruptet samplingrate? Do you have any suggestions how to solve this problem?

User avatar
joan
Posts: 14097
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI: Problem with fast ADC sampling

Mon Apr 29, 2019 8:54 pm

DMA could help, but not the way you are trying.

See viewtopic.php?t=71089 for one method.

User avatar
mikronauts
Posts: 2710
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: SPI: Problem with fast ADC sampling

Mon Apr 29, 2019 11:02 pm

Due to Linux, and caching to a lesser degree, you cannot totally get rid of the jitter you are experiencing. The higher the sampling rate, the more apparent the jitter.

Add a deterministic high speed micro controller to do the sampling to a large buffer and have it transfer the data to the pi (some cortex m3's have high speed adc's)

You could try an I2S adc, or usb sound card input, and scale your signal to 0..1v
NumiX wrote:
Mon Apr 29, 2019 8:21 pm
Hm so you think DMA wont help me to get an uninterruptet samplingrate? Do you have any suggestions how to solve this problem?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

Return to “Interfacing (DSI, CSI, I2C, etc.)”