View topic - SPI BeagleBoneBlack

SPI BeagleBoneBlack

For discussion of realtime and/or embedded programming.

SPI BeagleBoneBlack

Postby EberhardBinder » Tue Jan 08, 2019 4:01 pm

Hello together,

I am trying to use an ADX345 acceleration sensor with QNX 7.0 for BeagleBoneBlack.

I am using the following code. I do not get error messages, but all I am able to read is 0.

The functions are from the spitest example. I get the message that a TSC-2046 is present.

I would be nice if anyone could give me some advice.

Regards,

Eberhard Binder


int tspi_getdrvinfo();

int tspi_getdev();

are

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/neutrino.h>
#include <hw/spi-master.h>
#include <hw/inout.h>
#include <math.h>    // wegen sqrt-operation
#include <devctl.h>
#include <unistd.h>
#include <string.h>


int tspi_getdrvinfo();

int tspi_getdev();

//From Table 19. of the ADXL345 Data sheet
#define DEVID          0x00   //Device ID
#define THRESH_TAP     0x1D   //Tap Threshold
#define OFSX           0x1E   //X-axis Offset
#define OFSY           0x1F   //Y-axis Offset
#define OFSZ           0x20   //Z-axis Offset
#define DUR            0x21   //Tap duration
#define LATENT         0x22   //Tap latency
#define WINDOW         0x23   //Tap window
#define THRESH_ACT     0x24   //Activity threshold
#define THRESH_INACT   0x25   //Threshold inactivity
#define TIME_INACT     0x26   //Inactivity time
#define ACT_INACT_CTL  0x27   //Axis enable control for activity and inactivity detection
#define THRESH_FF      0x28   //Free-fall threshold
#define TIME_FF        0x29   //Free-fall time
#define TAP_AXES       0x2A   //Axis control for single tap/double tap
#define ACT_TAP_STATUS 0x2B   //Source of single tap/double tap
#define BW_RATE        0x2C   //Data rate and power mode control
#define POWER_CTL      0x2D   //Power-saving features control
#define INT_ENABLE     0x2E   //Interrupt enable control
#define INT_MAP        0x2F   //Interrupt mapping control
#define INT_SOURCE     0x30   //Source of interrupts
#define DATA_FORMAT    0x31   //Data format control
#define DATAX0         0x32   //X-axis Data 0
#define DATAX1         0x33   //X-axis Data 1
#define DATAY0         0x34   //Y-axis Data 0
#define DATAY1         0x35   //Y-axis Data 1
#define DATAZ0         0x36   //Z-axis Data 0
#define DATAZ1         0x37   //Z-axis Data 1
#define FIFO_CTL       0x38   //FIFO control
#define FIFO_STATUS    0x39   //FIFO status

#define SLAVE_ID       0x53

// Defines for REGSTER 0x00 Device ID

#define DEVID_VALUE 0xE5

// Defines for REGSTER 0x2D POWER_CTL

// MEASURE         D3

#define MEASURE    0x08

// Defines for REGSTER 0x31 Data Format Control

// RANGE            D1 D0

#define RANGE_2G    0x00
#define RANGE_4G    0x01
#define RANGE_8G    0x02
#define RANGE_16G   0x03

// JUSTIFY          D2

#define JUSTIFY     0x04

// FULL RES         D3

#define FULL_RES    0x08

// READBIT           D7

#define READBIT      0x40

// MBBIT           D6

#define MBBIT      0x80


#define N         10

int main(){

   int fd;

   spi_cfg_t spicfg;
   spi_drvinfo_t spidrvinfo;
   spi_devinfo_t spidevinfo;

   uint32_t device;

   unsigned char txBuf[N];
   unsigned char rxBuf[N];

   int len, clen, rlen;

   int DataX;

   int ret;

   int j;

   float a_x;

   tspi_getdrvinfo();

   tspi_getdev();

   fd = spi_open("/dev/spi0");

   if(fd < 0)
   {

      fprintf(stderr, "spi_open() failed\n");
      perror("spi_open() failed\n");
      exit(EXIT_FAILURE);

   }

   if (EOK != spi_getdrvinfo(fd, &spidrvinfo))
   {

      fprintf(stderr, "spi_getdrvinfo() call failed\n");
      perror("spi_getdrvinfo() failed\n");
      spi_close(fd);
      exit(EXIT_FAILURE);

   }
   else
   {

      fprintf(stdout, "spi_getdrvinfo: version %u\n", spidrvinfo.version);
      fprintf(stdout, "spi_getdrvinfo: name %s\n", spidrvinfo.name);
      fprintf(stdout, "spi_getdrvinfo: feature %u\n", spidrvinfo.feature);

   }

   if (EOK != spi_getdevinfo(fd, SPI_DEV_ID_NONE, &spidevinfo))
   {

        fprintf(stderr, "spi_getdevinfo() call failed\n");
        perror("spi_getevinfo() failed\n");
        spi_close(fd);
        exit(EXIT_FAILURE);

   }
   else
   {

        fprintf(stdout, "spi_getdevinfo: device %u\n", spidevinfo.device);
        fprintf(stdout, "spi_getdevinfo: name %s\n", spidevinfo.name);
        fprintf(stdout, "spi_getdevinfo: mode %u\n", spidevinfo.cfg.mode );
        fprintf(stdout, "spi_getdevinfo: clock rate %u\n", spidevinfo.cfg.clock_rate);

   }


   device = spidevinfo.device;

   if (EOK != spi_getdevinfo(fd, device | SPI_DEV_DEFAULT, &spidevinfo))
    {

         fprintf(stderr, "spi_getdevinfo() call failed\n");
         perror("spi_getevinfo() failed\n");
         spi_close(fd);
         exit(EXIT_FAILURE);

    }
    else
    {

         fprintf(stdout, "spi_getdevinfo: device %u\n", spidevinfo.device);
         fprintf(stdout, "spi_getdevinfo: name %s\n", spidevinfo.name);
         fprintf(stdout, "spi_getdevinfo: mode %u\n", spidevinfo.cfg.mode );
         fprintf(stdout, "spi_getdevinfo: clock rate %u\n", spidevinfo.cfg.clock_rate);

    }


   //spicfg.mode = ((8 & SPI_MODE_CHAR_LEN_MASK) | SPI_MODE_CKPOL_HIGH | SPI_MODE_CKPHASE_HALF | SPI_MODE_BODER_MSB);
   spicfg.mode = ((8 & SPI_MODE_CHAR_LEN_MASK) | SPI_MODE_CKPOL_HIGH | SPI_MODE_CKPHASE_HALF);
   spicfg.clock_rate = 500;

   if (EOK != spi_setcfg(fd, device | SPI_DEV_DEFAULT , &spicfg))
   {

      fprintf(stderr, "spi_setcfg() call failed\n");
      perror("spi_setcfg() failed\n");
      spi_close(fd);
      exit(EXIT_FAILURE);

   }

   if (EOK != spi_getdevinfo(fd, device | SPI_DEV_DEFAULT, &spidevinfo))
   {

      fprintf(stderr, "spi_getdevinfo() call failed\n");
      perror("spi_getevinfo() failed\n");
      spi_close(fd);
      exit(EXIT_FAILURE);

   }
   else
   {

      fprintf(stdout, "spi_getdevinfo: device %u\n", spidevinfo.device);
      fprintf(stdout, "spi_getdevinfo: name %s\n", spidevinfo.name);
      fprintf(stdout, "spi_getdevinfo: mode %u\n", spidevinfo.cfg.mode );
      fprintf(stdout, "spi_getdevinfo: clock rate %u\n", spidevinfo.cfg.clock_rate);

   }

   //memset(txBuf, 0, 2);
   //memset(rxBuf, 0, 2);

   memset(txBuf, 0, N);
   memset(rxBuf, 0, N);

   txBuf[0] = DEVID | READBIT;
   len = 1;

   ret = spi_write(fd, device | SPI_DEV_DEFAULT, (void *)txBuf, len);

   if (ret == -1)
   {

      fprintf(stderr, "POWER_CTL spi_write() call failed\n");
      perror("spi_write() failed\n");
      spi_close(fd);
      exit(EXIT_FAILURE);
   }
   else if (ret != len)
   {
      fprintf(stderr, "DEVID spi_write: len %u, ret %d\n", len, ret);
   }

   //memset(txBuf, 0, 2);
   //memset(rxBuf, 0, 2);

   memset(txBuf, 0, N);
   memset(rxBuf, 0, N);

   len = 1;

   ret = spi_read(fd, device | SPI_DEV_DEFAULT, rxBuf, len);

   if (ret == -1)
   {

      fprintf(stderr, "DEVID spi_read() call failed\n");
        perror("spi_read() failed\n");
        spi_close(fd);
      exit(EXIT_FAILURE);

   }
   else if (ret != len)
   {
      fprintf(stderr, "DEVID spi_read: len %u, ret %d\n", len, ret);
   }

   printf("DEVID %d %x\n", rxBuf[0], rxBuf[0]);

   memset(txBuf, 0, N);
   memset(rxBuf, 0, N);

   txBuf[0] = DEVID | READBIT;
   txBuf[1] = 0;
   len = 2;

   ret = spi_xchange(fd, device  | SPI_DEV_DEFAULT, (void *)txBuf, (void *)rxBuf, len);

   if (ret == -1)
   {

      fprintf(stderr, "DEVID spi_xchange() call failed\n");
      perror("spi_xchange() failed\n");
      spi_close(fd);
      exit(EXIT_FAILURE);

   }

   printf("DEVID %d %x\n", rxBuf[1], rxBuf[1]);

   memset(txBuf, 0, N);
   memset(rxBuf, 0, N);

   txBuf[0] = DEVID | READBIT;
   txBuf[1] = 0;
   clen = 1;
   rlen = 1;

   ret = spi_cmdread(fd, device | SPI_DEV_DEFAULT, (void *)txBuf, clen, (void *)rxBuf, rlen);

   if (ret == -1)
    {

       fprintf(stderr, "DEVID spi_cmdread() call failed\n");
       perror("spi_cmdread() failed\n");
       spi_close(fd);
       exit(EXIT_FAILURE);

    }
   printf("DEVID %d %x\n", rxBuf[0], rxBuf[0]);

   memset(txBuf, 0, N);
   memset(rxBuf, 0, N);

   txBuf[0] = DATA_FORMAT;
   txBuf[1] = RANGE_16G | FULL_RES;
   len = 2;

   ret = spi_write(fd, 0, (void *)txBuf, len);

   if (ret == -1)
   {

      fprintf(stderr, "DATA_FORMAT spi_write() call failed\n");
      perror("spi_write() failed\n");
      spi_close(fd);
      exit(EXIT_FAILURE);

   }
   else if (ret != len)
   {
      fprintf(stderr, "DATA_FORMAT spi_write: len %u, ret %d\n", len, ret);
   }

   memset(txBuf, 0, N);
   memset(rxBuf, 0, N);

   txBuf[0] = POWER_CTL;
   txBuf[1] = MEASURE;
   len = 2;

   ret = spi_write(fd, device | SPI_DEV_DEFAULT, (void *)txBuf, len);

   if (ret == -1)
   {

      fprintf(stderr, "POWER_CTL spi_write() call failed\n");
      perror("spi_write() failed\n");
      spi_close(fd);
      exit(EXIT_FAILURE);

   }
   else if (ret != len)
   {
      fprintf(stderr, "POWER_CTL spi_write: len %u, ret %d\n", len, ret);
   }


   while(1) {

     goto l1;

     memset(txBuf, 0, N);
     memset(rxBuf, 0, N);

      txBuf[0] = DATAX0 | READBIT;
      clen = 1;
      rlen = 1;

      ret = spi_cmdread(fd, device | SPI_DEV_DEFAULT, (void *)txBuf, clen, (void *)rxBuf, rlen);

      if (ret == -1)
     {

        fprintf(stderr, "DATAX0 spi_cmdread() call failed\n");
        perror("spi_cmdread() failed\n");
        spi_close(fd);
        exit(EXIT_FAILURE);

     }
     else if (ret != rlen)
     {
        fprintf(stderr, "DATAX0 spi_cmdread: len %u, ret %d\n", len, ret);
     }

      printf("DATAX0 %d\n", rxBuf[0]);

      DataX = rxBuf[0];

      memset(txBuf, 0, N);
      memset(rxBuf, 0, N);

     txBuf[0] = DATAX1 | READBIT;
     len = 1;

      ret = spi_write(fd, 0, (void *)txBuf, len);

     if (ret == -1)
     {

        fprintf(stderr, "DATAX1 spi_write() call failed\n");
        perror("spi_write() failed\n");
        spi_close(fd);
        exit(EXIT_FAILURE);

     }
     else if (ret != len)
     {
        fprintf(stderr, "DATAX1 spi_write: len %u, ret %d\n", len, ret);
     }

      ret = spi_read(fd, device | SPI_DEV_DEFAULT, (void *)rxBuf, len);

      if (ret == -1)
      {

         fprintf(stderr, "DATAX1 spi_read() call failed\n");
         perror("spi_read() failed\n");
         spi_close(fd);
         exit(EXIT_FAILURE);

      }
      else if (ret != len)
      {
         fprintf(stderr, "DATAX1 spi_read: len %u, ret %d\n", len, ret);
     }

      printf("DATAX1 %d\n", rxBuf[1]);

      DataX += rxBuf[1]<<8;

      printf("DataX %d\n", DataX);

      a_x = (float)DataX / 255.0;

      printf("a_x = %.2f\n", a_x);

l1:

      memset(txBuf, 0, N);
      memset(rxBuf, 0, N);

      txBuf[0] = DATAX0 | READBIT | MBBIT;
      clen = 1;
      rlen = 6;

      ret = spi_cmdread(fd, device | SPI_DEV_DEFAULT, (void *)txBuf, clen, (void *)rxBuf, rlen);

      printf("\nMessung, %d Bytes\n", ret);

      for(j = 0; j <= 5; j+=2)
         printf("%u %d\n", j, (rxBuf[j]<<8)+rxBuf[j+1]);

      usleep(2000000);


   }

   spi_close(fd);

}




int tspi_getdrvinfo()
{
   spi_drvinfo_t   info;
   int            i, fd;

   fprintf(stderr, "\n########### spi_getdrvinfo() test ###########\n");

   /*
    * Open
    */

   if ((fd = spi_open("/dev/spi0")) < 0) {
      fprintf(stderr, "SPI open failed, fd = %d errno = %d\n", fd, errno);
      strerror(errno);
      return(-1);
   }

   i = spi_getdrvinfo(fd, &info);
   if (i == EOK) {
      fprintf(stderr, "version     = %x\n", info.version);
      fprintf(stderr, "driver name = %s\n", info.name);
      fprintf(stderr, "feature     = %x\n", info.feature);
   }
   else
      fprintf(stderr, "get driver info failed\n");

   spi_close(fd);

   return 0;
}




int tspi_getdev()
{
   spi_devinfo_t   info;
   int            i, fd, dev;

   fprintf(stderr, "\n########### spi_getdevinfo() test ###########\n");

   /*
    * Open
    */
   if ((fd = spi_open("/dev/spi0")) < 0) {
      fprintf(stderr, "SPI open failed, fd = %d errno = %d\n", fd, errno);
      strerror(errno);
      return(-1);
   }


   fprintf(stderr, "\n----- Detect all devices info -----\n\n");
   dev = 0;
   while (1) {
        fprintf(stderr, "----- get  device 0x%04x info -----\n", dev);

      i = spi_getdevinfo(fd, dev, &info);
      if (i == EOK) {
         fprintf(stderr, "device     = %x\n", info.device);
         fprintf(stderr, "name       = %s\n", info.name);
         fprintf(stderr, "mode       = %x\n", info.cfg.mode);
         fprintf(stderr, "clock_rate = %d\n", info.cfg.clock_rate);
         dev = info.device & SPI_DEV_ID_MASK;
      }
      else {
         break;
      }
        dev++;
   }

   fprintf(stderr, "----- get next device 0x%04x info -----\n", 0xFFFF);
   dev = SPI_DEV_ID_NONE;
   while (1) {
      i = spi_getdevinfo(fd, dev, &info);
      if (i == EOK) {
         fprintf(stderr, "device     = %x\n", info.device);
         fprintf(stderr, "name       = %s\n", info.name);
         fprintf(stderr, "mode       = %x\n", info.cfg.mode);
         fprintf(stderr, "clock_rate = %d\n", info.cfg.clock_rate);
         dev = info.device & SPI_DEV_ID_MASK;
      }
      else {
         fprintf(stderr, "get devinfo failed\n");
         break;
      }
   }

   spi_close(fd);

   return 0;
}

EberhardBinder
New Member
 
Posts: 1
Joined: Mon Jan 07, 2019 4:51 pm

Re: SPI BeagleBoneBlack

Postby nico04 » Fri Jan 11, 2019 8:02 am

Hi,

I can't see any ADX345 chip in BeagleBoneBlack schematics.
I suppose you use an add-on board. What are the schematics of this board ? How is it connected on the BeagleBoneBlack board ?

Nicolas
nico04
Senior Member
 
Posts: 125
Joined: Wed Sep 29, 2010 9:59 am
Location: France


Return to Realtime and Embedded

Who is online

Users browsing this forum: No registered users and 3 guests

cron