spi.c 3.87 KB
#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>
/* memory management */
#include <sys/mman.h>
#include "spi.h"
#include <string.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

#undef DEBUG
//#define DEBUG
static uint8_t bits = 8;
//static uint32_t speed = 15000000;	// 9600;
//static uint32_t speed = 20000000;	// 9600;
static uint32_t speed = 10000000;
//static uint32_t speed = 100000;	// 9600;
//static uint16_t delay;
static void
pabort (const char *s)
{
  perror (s);
  abort ();
}

uint8_t
spi_put_multiple (int fd, uint8_t * tx, uint16_t tx_size,
		  uint8_t * rx, uint16_t rx_size)
{
  int ret;
  int nb = 2;
  if (rx_size == 0 || tx_size == 0)
    nb = 1;
  struct spi_ioc_transfer xfer[nb];
  nb = 0;
  memset (xfer, 0, sizeof (xfer));
  if (tx_size > 0)
    {
      xfer[nb].tx_buf = (unsigned long) tx;
      xfer[nb].len = tx_size;
      nb++;
    }
  if (rx_size > 0)
    {
      xfer[nb].rx_buf = (unsigned long) rx;
      xfer[nb].len = rx_size;
      nb++;
    }
  ret = ioctl (fd, SPI_IOC_MESSAGE (nb), xfer);
  return ret;
}

uint8_t
tr3 (int fd, uint8_t * val, uint8_t * rx, uint8_t size)
{
  int ret;

  struct spi_ioc_transfer xfer[2];
  memset (xfer, 0, sizeof (xfer));

  xfer[0].tx_buf = (unsigned long) val;
  xfer[0].len = size;
  xfer[1].rx_buf = (unsigned long) rx;
  xfer[1].len = size;

  ret = ioctl (fd, SPI_IOC_MESSAGE (2), xfer);
  if (ret < 1)
    {
      pabort ("can't send spi message");
    }
  return ret;
}

void
setMode (int fd, uint8_t mode)
{
  if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 0)
    pabort ("can't set mode");
  if (ioctl (fd, SPI_IOC_RD_MODE, &mode) < 0)
    pabort ("can't get mode");
}

uint8_t
spi_put (int fd, uint8_t valeur)
{
  int ret;
  uint8_t rx_buf[1], tx_buf[1];
  tx_buf[0] = valeur;
  struct spi_ioc_transfer xfer[2];
  memset (xfer, 0, sizeof (xfer));

  xfer[0].tx_buf = (unsigned long) tx_buf;
  xfer[0].rx_buf = (unsigned long) rx_buf;
  xfer[0].len = 1;
  ret = ioctl (fd, SPI_IOC_MESSAGE (1), xfer);
  if (ret < 1)
    {
      pabort ("can't send spi message");
    }
  return rx_buf[0];
}

int
configureSpi (char *device)
{
	printf("device(configureSpi)=%s\n",device);
  int fd = open (device, O_RDWR);
  if (fd < 0)
    pabort ("can't open device");

  setMode (fd, 0);
  /* bits per word */
  if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1)
    pabort ("can't set bits per word");
  if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1)
    pabort ("can't get bits per word");

  /* max speed hz */
  if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1)
    pabort ("can't set max speed hz");

  if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1)
    pabort ("can't get max speed hz");
//#ifdef DEBUG
  //printf("spi mode: %d\n", mode);
  //printf ("bits per word: %d\r\n", bits);
  //printf ("max speed: %lu Hz (%lu kHz)\r\n",
//	  (unsigned long) speed, ((unsigned long) speed) / 1000);
//#endif /* DEBUG */
  return fd;
}

int configureSpiFromFd (int fd)
{
  setMode (fd, 0);
  /* bits per word */
  if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1)
    pabort ("can't set bits per word");
  if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1)
    pabort ("can't get bits per word");

  /* max speed hz */
  if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1)
    pabort ("can't set max speed hz");

  if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1)
    pabort ("can't get max speed hz");
#ifdef DEBUG
//printf("spi mode: %d\n", mode);
printf ("bits per word: %d\r\n", bits);
printf ("max speed: %lu Hz (%lu kHz)\r\n",
        (unsigned long) speed, ((unsigned long) speed) / 1000);
#endif /* DEBUG */
  return EXIT_SUCCESS;
}