1

So i am using a trimmed down version of this code to do a spi transaction usin spidev. Here is my code

/*
 * spi-driver-speed.c
 * 2016-11-23
 * Public Domain
*/

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>

/*
spi-driver-speed.c

gcc -o spi-driver-speed spi-driver-speed.c

./spi-driver-speed [bytes [bps [loops] ] ]
*/



int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags)
{
   int i,fd;
   char  spiMode;
   char  spiBits  = 8;
   char dev[32];

   spiMode  = spiFlags & 3;
   spiBits  = 8;

   sprintf(dev, "/dev/spidev0.%d", spiChan);

   if ((fd = open(dev, O_RDWR)) < 0)
   {
      return -1;
   }

   if (ioctl(fd, SPI_IOC_WR_MODE, &spiMode) < 0)
   {
      close(fd);
      return -2;
   }

   if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spiBits) < 0)
   {
      close(fd);
      return -3;
   }

   if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spiBaud) < 0)
   {
      close(fd);
      return -4;
   }

   return fd;
}

int spiClose(int fd)
{
   return close(fd);
}

int spiXfer(int fd, unsigned speed, char *txBuf, char *rxBuf, unsigned count)
{
   int err;
   struct spi_ioc_transfer spi;

   memset(&spi, 0, sizeof(spi));

   spi.tx_buf        = (unsigned long)txBuf;
   spi.rx_buf        = (unsigned long)rxBuf;
   spi.len           = count;
   spi.speed_hz      = speed;
   spi.delay_usecs   = 0;
   spi.bits_per_word = 8;
   spi.cs_change     = 0;

   err = ioctl(fd, SPI_IOC_MESSAGE(1), &spi);

   return err;
}

char TXBuf[10] = {0xAB,0xAB,0xAB,0xAB,0xAB,0xAB,0xAB,0xAB,0xAB,0xAB};
char RXBuf[10] = {0};

int bytes = 10;
int speed = 5000000;


int main(int argc, char * argv[])
{
   int fd,er;

   fd = spiOpen(1, speed, 0);

   er = spiXfer(fd, speed, TXBuf, RXBuf, bytes);

   close(fd);


   printf("RX: %d %d %d %d %d %d %d %d %d %d\n",RXBuf[0],RXBuf[1],RXBuf[2],RXBuf[3],RXBuf[4],RXBuf[5],RXBuf[6],RXBuf[7],RXBuf[8],RXBuf[9]);
   printf("TX: %d %d %d %d %d %d %d %d %d %d\n",TXBuf[0],TXBuf[1],TXBuf[2],TXBuf[3],TXBuf[4],TXBuf[5],TXBuf[6],TXBuf[7],TXBuf[8],TXBuf[9]);
   printf("ER: %d\n",er);


}

but i do not see anything on RXBuf. Am i doing something wrong?

RX: 0 0 0 0 0 0 0 0 0 0
TX: 171 171 171 171 171 171 171 171 171 171
ER: 10

to prove that there is in fact a signal on the MISO here is a snap on the logic analyzer .

enter image description here

1

looks like spi.rx_buf in xfer needs a pointer to the array buffer. (unsigned long)rxBuf is not the way i have known to do it.

i suggest to use uintptr_t instead of unsigned long

spi.tx_buf        = (uintptr_t)txBuf;
spi.rx_buf        = (uintptr_t)rxBuf;

uintptr_t is a datatype meant specifically to hold pointers

1
  • Thank you it worked ! – DrakeJest Feb 27 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.