33 #include <sys/ioctl.h>
34 #include <linux/types.h>
35 #include <linux/spi/spidev.h>
59 debug (
"[%s] Kernel module \"spidev\" not loaded.\n", __func__);
65 debug (
"[%s] Kernel module \"spi_bcm2708\" not loaded.\n", __func__);
71 fd = open(
"/dev/spidev0.0", O_RDWR);
73 else if ( channel == 1 )
75 fd = open(
"/dev/spidev0.1", O_RDWR);
79 debug (
"[%s] Invalid SPI channel: %d\n", __func__, channel);
85 debug (
"[%s] Can't open SPI device.\n", __func__);
97 debug (
"[%s] Can't set default SPI config.\n", __func__);
116 spi_config(
int fd, uint8_t mode, uint8_t bits, uint32_t speed, uint16_t delay)
121 ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
124 debug (
"[%s] Can't set SPI mode.\n", __func__);
129 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
132 debug (
"[%s] Can't set SPI bits per word.\n", __func__);
137 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
140 debug (
"[%s] Can't set SPI max speed.\n", __func__);
159 ret =
spi_config (fd, SPI_DEFAULT_MODE, SPI_DEFAULT_BPW, SPI_DEFAULT_SPEED, SPI_DEFAULT_DELAY);
179 struct spi_ioc_transfer transfer = {
180 .tx_buf = (
unsigned long)txbuf,
181 .rx_buf = (
unsigned long)rxbuf,
183 .delay_usecs = SPI_DEFAULT_DELAY,
184 .speed_hz = SPI_DEFAULT_SPEED,
185 .bits_per_word = SPI_DEFAULT_BPW,
188 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &transfer);
191 debug (
"[%s] Can't send SPI message.\n", __func__);