Hi,
I have once again checked the same today. But surprisingly the delay got reduced.
time delay b/w CS falling to first SPI clock = 14 micro seconds
time delay b/w SPI last clock to CS rising = 24 micro seconds
If the delay is reduced further, the data acquisition speed can be increased.
The code:
#include "libsoc_gpio.h"
#include "libsoc_debug.h"
#include"libsoc_file.h"
#include "libsoc_spi.h"
#define CS 146
#define RST 147
#define SPI_DEVICE 3
void error()
{
printf("error\n");
}
int main(void)
{
uint8_t wdata[4],rdata[4];
gpio *cs,*rst; //chip select and reset
spi* spi_dev = libsoc_spi_init(SPI_DEVICE, 0);
if (!spi_dev)
{
printf("Failed to get spidev device!\n");
return EXIT_FAILURE;
}
//For SPI
libsoc_spi_set_mode(spi_dev, MODE_0);
libsoc_spi_get_mode(spi_dev);
libsoc_spi_set_speed(spi_dev, 10000000); //10MHz
libsoc_spi_get_speed(spi_dev);
libsoc_spi_set_bits_per_word(spi_dev,BITS_8);
// Request gpios
cs = libsoc_gpio_request(CS, LS_GPIO_SHARED);
rst = libsoc_gpio_request(RST, LS_GPIO_SHARED);
if (cs == NULL || rst == NULL)
{
error();
}
// Set direction to OUTPUT
libsoc_gpio_set_direction(cs, OUTPUT);
if (libsoc_gpio_get_direction(cs) != OUTPUT)
{
printf("Failed to set cs direction to OUTPUT\n");
error();
}
libsoc_gpio_set_direction(rst, OUTPUT);
if (libsoc_gpio_get_direction(rst) != OUTPUT)
{
printf("Failed to set rst direction to OUTPUT\n");
error();
}
// Set reset always HIGH. It goes to the active low reset pin of ADC.
libsoc_gpio_set_level(rst, LOW);
usleep(250);
libsoc_gpio_set_level(rst, HIGH);
//softwre rst of adc
wdata[0]=0x85; wdata[1]=0x00;
libsoc_gpio_set_level(cs, HIGH);
usleep(1);
libsoc_gpio_set_level(cs, LOW);
libsoc_spi_rw(spi_dev,wdata,rdata,2);
libsoc_gpio_set_level(cs, HIGH);
usleep(1);
//feature select of adc - ADC Configuration
wdata[0]=0x03; wdata[1]=0x03; wdata[2]=0x00;
libsoc_gpio_set_level(cs, LOW);
libsoc_spi_rw(spi_dev,wdata,rdata,3);
libsoc_gpio_set_level(cs, HIGH);
usleep(1);
//to set range for all channel - ADC Configuration
for(int i=0;i<4;i++)
{
libsoc_gpio_set_level(cs, LOW);
wdata[0]=(((0x05+i)<<1) | 1); wdata[1]=0x05; wdata[2]=0x00;
libsoc_spi_rw(spi_dev,wdata,rdata,3);
libsoc_gpio_set_level(cs, HIGH);
usleep(1);
}
// to get back to auto mode in adc - ADC Configuration
libsoc_gpio_set_level(cs, LOW);
wdata[0]=(0x01<<1)|1; wdata[1]=0x0f; wdata[2]=0x00;
libsoc_spi_rw(spi_dev,wdata,rdata,4);
libsoc_gpio_set_level(cs, HIGH);
usleep(1);
// to auto rst of adc - ADC Configuration
libsoc_gpio_set_level(cs, LOW);
wdata[0]=0xA0; wdata[1]=0x00; wdata[2]=0x00; wdata[3]=0x00;
libsoc_spi_rw(spi_dev,wdata,rdata,4);
libsoc_gpio_set_level(cs, HIGH);
usleep(1);
//data acquistion loop
while(1)
{
time_t start, end;
double *arr;
int n=1;
arr=(double*)malloc(n*sizeof(double));
start=time(NULL);
end=start+5; //to acquire data for 5 sec
while(start<=end)
{
for(int i=0;i<4;i++) //to take data from 4 ch of adc in auto sequence
{
libsoc_gpio_set_level(cs, LOW);
libsoc_spi_read(spi_dev,rdata,4);
libsoc_gpio_set_level(cs, HIGH);
arr[n-1]=(10.24/65536)*((rdata[2]<<8) | rdata[3]);
n=n+1;
arr=(double*)realloc(arr,n*sizeof(double));
}
start=time(NULL);
}
for(int i=0;i<n;i++)
{
printf("arr[%d] is %f\n",i,arr[i]);
}
printf("The size of arr for 5 sec is %d\n",n);
free(arr);
break;
}
return 0;
}
Another issue that we are facing is that the Data Acquisition is not happening at constant speed. As in the code we are continuously capturing data for 5 sec. But if I run the code each time the size of the array is different. The size of the array is like 10925, 18402, 5589 etc. How to control the speed of the data acquisition? Whether the loop interval between each iteration varies based upon the OS background operations? Or is it the problem of the time() function that we have used to figure out the time interval?
Thank You for your time.