33 #include <sys/ioctl.h> 34 #include <linux/types.h> 35 #include <linux/spi/spidev.h> 58 debug (
"[%s] Kernel module \"spidev\" not loaded.\n", __func__);
64 debug (
"[%s] Kernel module \"spi_bcm2708\" not loaded.\n", __func__);
72 else if ( channel == 1 )
78 debug (
"[%s] Invalid SPI channel: %d\n", __func__, channel);
84 debug (
"[%s] Can't open SPI device.\n", __func__);
96 debug (
"[%s] Can't set default SPI config.\n", __func__);
114 int fd = open (devname, O_RDWR);
118 debug(
"[%s] Can't open %s : %s\n", __func__, devname, strerror (errno));
139 spi_config(
int fd, uint8_t mode, uint8_t bits, uint32_t speed, uint16_t delay)
144 ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
147 debug (
"[%s] Can't set SPI mode.\n", __func__);
152 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
155 debug (
"[%s] Can't set SPI bits per word.\n", __func__);
160 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
163 debug (
"[%s] Can't set SPI max speed.\n", __func__);
182 ret =
spi_config (fd, SPI_DEFAULT_MODE, SPI_DEFAULT_BPW, SPI_DEFAULT_SPEED, SPI_DEFAULT_DELAY);
202 struct spi_ioc_transfer transfer = {
203 .tx_buf = (
unsigned long)txbuf,
204 .rx_buf = (
unsigned long)rxbuf,
206 .delay_usecs = SPI_DEFAULT_DELAY,
207 .speed_hz = SPI_DEFAULT_SPEED,
208 .bits_per_word = SPI_DEFAULT_BPW,
211 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &transfer);
214 debug (
"[%s] Can't send SPI message.\n", __func__);
int spi_config(int fd, uint8_t mode, uint8_t bits, uint32_t speed, uint16_t delay)
Configures the spidev interface.
int spi_transfer(int fd, uint8_t txbuf[], uint8_t rxbuf[], uint8_t len)
Initiates SPI transfers.
int spi_config_default(int fd)
Configures the spidev interface with default values.
int check_kernel_module(char *modulename)
Check if the kernel module specified is loaded.
int spi_init_name(char *devname)
Initialises the spidev interface for the sysfs entry specified by the devname parameter.
int spi_init(uint8_t channel)
Initialises the spidev interface.
Header for general library functionality.