AnsweredAssumed Answered

AD7124-4 Activated all channels but able to read only even channels

Question asked by kprimerose on Feb 10, 2018
Latest reply on Mar 3, 2018 by kprimerose

Hello all,I am using AD7124-4 Precision ADC using SPI Communication with orangePI(Zero) . I activated all channels (0 to 7) by channel configuration and I am just checking status(active or not ) of channel by using simple 0x40 command.I am getting only odd channels as active channel via CH_ACTIVE.I tried my best to solve but can't make correct.Can anyone help me? I have paste my code below.JellenieR

In same manner ,in arduino(uno) I am getting CH_ACTIVE values properly for each channels.

 

---------------------------------------------------------------------------------------------------------------------------------------------------------
/*
spidevlib.c - A user-space program to comunicate using spidev.
Gustavo Zamboni
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>

int buf[10];
int buf2[10];
// extern int com_serial, file;
// extern int failcount;

struct spi_ioc_transfer xfer[2];


//////////
// Init SPIdev
//////////
int spi_init(char filename[40])
{
int file;
__u8 mode=0, lsb, bits;
__u32 speed=2500000;

if ((file = open(filename,O_RDWR)) < 0)
{
printf("Failed to open the bus.");
/* ERROR HANDLING; you can check errno to see what went wrong */
//com_serial=0;
exit(1);
}

///////////////
// Verifications
///////////////
//possible modes: mode |= SPI_LOOP; mode |= SPI_CPHA; mode |= SPI_CPOL; mode |= SPI_CS_HIGH; mode |= SPI_3WIRE; mode |= SPI_NO_CS; mode |= SPI_READY;
//multiple possibilities using |

// mode |= SPI_LSB_FIRST;

if (ioctl(file, SPI_IOC_WR_MODE, &mode)<0)
{
perror("can't set spi mode");
//return;
}


if (ioctl(file, SPI_IOC_RD_MODE, &mode) < 0)
{
perror("SPI rd_mode");
// return;
}
if (ioctl(file, SPI_IOC_RD_LSB_FIRST, &lsb) < 0)
{
perror("SPI rd_lsb_fist");
// return;
}
// //sunxi supports only 8 bits

if (ioctl(file, SPI_IOC_WR_BITS_PER_WORD, (__u8[1]){8})<0)
{
perror("can't set bits per word");
// return;
}

if (ioctl(file, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
{
perror("SPI bits_per_word");
// return;
}

if (ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0)
{
perror("can't set max speed hz");
//return;
}

if (ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
{
perror("SPI max_speed_hz");
// return;
}


printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n",filename, mode, bits, 0 ? "(lsb first) " : "", speed);

// xfer[0].tx_buf = (unsigned long)buf;
// xfer[0].len = 3; /* Length of command to write*/
xfer[0].cs_change = 0; /* Keep CS activated */
xfer[0].delay_usecs = 0, //delay in us
xfer[0].speed_hz = 2500000, //speed
xfer[0].bits_per_word = 8, // bites per word 8

// xfer[1].rx_buf = (unsigned long) buf2;
// xfer[1].len = 1; /* Length of Data to read */
xfer[1].cs_change = 0; /* Keep CS activated */
// xfer[0].delay_usecs = 0;
// xfer[0].speed_hz = 2500000;
// xfer[0].bits_per_word = 8;

return file;

}



void spi_read(int add1,int nbytes,int file)
{
int status;
buf[0] = add1;

xfer[0].tx_buf = (unsigned long)buf;
xfer[0].len = 1; /* Length of command to write*/
xfer[1].rx_buf = (unsigned long) buf2;
xfer[1].len = nbytes; /* Length of Data to read */
status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
if (status < 0)
{
perror("SPI_IOC_MESSAGE");
// return;
}
// printf("env: %02x \n", buf[0]);
// printf("read: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);

for (int ret = 0; ret < 4; ret++) {
if (!(ret % 6))
puts("");
printf("%d ", buf2[ret]);
}
}

void spi_write(int add,int d1,int d2,int nbytes,int file)
{
unsigned char buf[32], buf2[32];
int status;

buf[0] = add;
buf[1] = d1;
buf[2] = d2;

xfer[0].tx_buf = (unsigned long)buf;
xfer[0].len = 3; /* Length of command to write*/
status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);
if (status < 0)
{
perror("SPI_IOC_MESSAGE");
}
printf("write: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
//printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);
}

void main()
{
/* code */
int file;

file=spi_init("/dev/spidev1.0"); //dev

//usleep(125000);

spi_write(0x09,0x80,0x13,2,file); usleep(10000);
spi_write(0x0a,0x80,0x33,2,file); usleep(10000);
spi_write(0x0b,0x80,0x53,2,file); usleep(10000);
spi_write(0x0c,0x80,0x73,2,file); usleep(10000);
spi_write(0x0d,0x80,0x93,2,file); usleep(10000);
spi_write(0x0e,0x80,0xa3,2,file); usleep(10000);
spi_write(0x0f,0x80,0xc3,2,file); usleep(10000);
// spi_write(0x01,0x00,0x00,2,file);

usleep(125000);
//while(1)
//{
// spi_read(0x49,0x00,3,file); //reading the address 0xE60E
// spi_read(0x4a,0x00,3,file);
// spi_read(0x4b,0x00,3,file);
//}
while(1)
{
spi_read(0x40,1,file);
usleep(125000);
}


close(file);


}

------------------------------------------------------------------------------------------------------------------------------------------------------------

Outcomes