pihwm  r1
A lightweight C library for Raspberry Pi hardware modules.
pi_spi.c
Go to the documentation of this file.
1 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdint.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/ioctl.h>
34 #include <linux/types.h>
35 #include <linux/spi/spidev.h>
36 
37 #include "pihwm.h"
38 
51 int
52 spi_init (uint8_t channel)
53 {
54  int fd;
55 
56  if ( check_kernel_module("spidev") < 0 )
57  {
58  debug ("[%s] Kernel module \"spidev\" not loaded.\n", __func__);
59  return -1;
60  }
61 
62  if ( check_kernel_module("spi_bcm2708") < 0 )
63  {
64  debug ("[%s] Kernel module \"spi_bcm2708\" not loaded.\n", __func__);
65  return -1;
66  }
67 
68  if ( channel == 0 )
69  {
70  fd = spi_init_name("/dev/spidev0.0");
71  }
72  else if ( channel == 1 )
73  {
74  fd = spi_init_name("/dev/spidev0.1");
75  }
76  else
77  {
78  debug ("[%s] Invalid SPI channel: %d\n", __func__, channel);
79  fd = -1;
80  }
81 
82  if ( fd < 0 )
83  {
84  debug ("[%s] Can't open SPI device.\n", __func__);
85  return -1;
86  }
87  else
88  {
89  // device open, config default values
90  if ( spi_config_default(fd) )
91  {
92  return fd;
93  }
94  else
95  {
96  debug ("[%s] Can't set default SPI config.\n", __func__);
97  return -1;
98  }
99  }
100 }
101 
111 int
112 spi_init_name (char *devname)
113 {
114  int fd = open (devname, O_RDWR);
115 
116  if ( fd < 0 )
117  {
118  debug("[%s] Can't open %s : %s\n", __func__, devname, strerror (errno));
119  return -1;
120  }
121  else
122  {
123  return fd;
124  }
125 }
126 
138 int
139 spi_config(int fd, uint8_t mode, uint8_t bits, uint32_t speed, uint16_t delay)
140 {
141  int ret;
142 
143  //spi mode
144  ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
145  if ( ret == -1 )
146  {
147  debug ("[%s] Can't set SPI mode.\n", __func__);
148  return -1;
149  }
150 
151  // bits per word
152  ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
153  if ( ret == -1 )
154  {
155  debug ("[%s] Can't set SPI bits per word.\n", __func__);
156  return -1;
157  }
158 
159  // max speed hz
160  ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
161  if ( ret == -1 )
162  {
163  debug ("[%s] Can't set SPI max speed.\n", __func__);
164  return -1;
165  }
166 
167  return 1;
168 }
169 
177 int
179 {
180  int ret;
181 
182  ret = spi_config (fd, SPI_DEFAULT_MODE, SPI_DEFAULT_BPW, SPI_DEFAULT_SPEED, SPI_DEFAULT_DELAY);
183 
184  return ret;
185 }
186 
197 int
198 spi_transfer (int fd, uint8_t txbuf[], uint8_t rxbuf[], uint8_t len)
199 {
200  int ret;
201 
202  struct spi_ioc_transfer transfer = {
203  .tx_buf = (unsigned long)txbuf,
204  .rx_buf = (unsigned long)rxbuf,
205  .len = len,
206  .delay_usecs = SPI_DEFAULT_DELAY,
207  .speed_hz = SPI_DEFAULT_SPEED,
208  .bits_per_word = SPI_DEFAULT_BPW,
209  };
210 
211  ret = ioctl(fd, SPI_IOC_MESSAGE(1), &transfer);
212  if ( ret < 1 )
213  {
214  debug ("[%s] Can't send SPI message.\n", __func__);
215  return -1;
216  }
217  else
218  {
219  return 1;
220  }
221 }
222 
int spi_config(int fd, uint8_t mode, uint8_t bits, uint32_t speed, uint16_t delay)
Configures the spidev interface.
Definition: pi_spi.c:139
int spi_transfer(int fd, uint8_t txbuf[], uint8_t rxbuf[], uint8_t len)
Initiates SPI transfers.
Definition: pi_spi.c:198
int spi_config_default(int fd)
Configures the spidev interface with default values.
Definition: pi_spi.c:178
int check_kernel_module(char *modulename)
Check if the kernel module specified is loaded.
Definition: pihwm.c:224
int spi_init_name(char *devname)
Initialises the spidev interface for the sysfs entry specified by the devname parameter.
Definition: pi_spi.c:112
int spi_init(uint8_t channel)
Initialises the spidev interface.
Definition: pi_spi.c:52
Header for general library functionality.