2010-02-12 17:04:38     Spidev for AD7793

Document created by Aaronwu Employee on Aug 20, 2013
Version 1Show Document
  • View in full screen mode

2010-02-12 17:04:38     Spidev for AD7793

Charles Xavier (UNITED STATES)

Message: 86039   

 

Hello All,

 

I'm having a bit of a problem trying to use spidev with the AD7793 analog to digital converter.

 

I'm using spidev1.1 and I'm able to successfully open this device.  However, I'm trying to read the identification register and getting back all 0's.  In the AD7793 specs, the recommended procedure for reading one of the registers is to do a reset of the AD7793 first.  A reset consists of writing 32 consecutive one's to the device.  In other words, 32 SCLK pulses need to be initiated.  I think I'm doing this correctly but perhaps I'm overlooking something.  After the reset I try reading to the Identification register which is at offset 0x60.  Here is a simplified version of the code:  Any help would be greatly appreciated.

 

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <fcntl.h>

#include <string.h>

 

#include <sys/ioctl.h>

#include <sys/types.h>

#include <sys/stat.h>

 

#include <linux/types.h>

#include <linux/spi/spidev.h>

 

const char *name = "/dev/spidev1.1"

 

static void send_byte(int fd, char cmd)

{

   struct spi_ioc_transfer   xfer[1];

   unsigned char buf[2], reset, *bp;

   int status, len;

 

   /*  First let's reset the AD7793 */

   reset = 0xFFFFFFFF;

   len = 4;

 

   memset(xfer, 0, sizeof xfer);

   memset(buf, 0, sizeof buf);

 

   buf[0] = reset;

 

   xfer[0].tx_buf = (__u64) buf;

   xfer[0].rx_buf = (__u64) buf;

   xfer[0].len = len;

 

   status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);

   if (status < 0)

   {

      perror("SPI_IOC_MESSAGE");

      return;

   }

 

   /*  Now let's read the AD7793 identification register */

 

   cmd = 0x60;

   len = 1;

 

   memset(xfer, 0, sizeof xfer);

   memset(buf, 0, sizeof buf);

 

   buf[0] = cmd;

 

   xfer[0].tx_buf = (__u64) buf;

   xfer[0].rx_buf = (__u64) buf;

   xfer[0].len = len;

 

   status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);

   if (status < 0)

   {

      perror("SPI_IOC_MESSAGE");

      return;

   } 

   printf("TX Byte:  %02x RX Byte:  %02x\n", cmd, buf[0]);

}

 

 

int main()

{

   int      cmd = 0;  // This is actually hardcoded

   int      fd;

 

   fd = open(name, O_RDWR);

   if (fd < 0) {

      perror("open");

      return 1;

   }

 

   send_byte(fd, cmd);

 

   close(fd);

   return 0;

}

QuoteReplyEditDelete

 

 

2010-02-12 17:15:51     Re: Spidev for AD7793

Mike Frysinger (UNITED STATES)

Message: 86040   

 

the CS needs to be held low for those 32bits though and i doubt that's what you're doing

Attachments

    Outcomes