3 * Simplified SPI access routines
4 * Copyright (c) 2012 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with wiringPi.
21 * If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
30 #include <sys/ioctl.h>
31 #include <linux/spi/spidev.h>
35 #include "wiringPiSPI.h"
38 // The SPI bus parameters
39 // Variables as they need to be passed as pointers later on
41 const static char *spiDev0 = "/dev/spidev0.0" ;
42 const static char *spiDev1 = "/dev/spidev0.1" ;
43 const static uint8_t spiMode = 0 ;
44 const static uint8_t spiBPW = 8 ;
45 const static uint16_t spiDelay = 0 ;
47 static uint32_t spiSpeeds [2] ;
48 static int spiFds [2] ;
53 * Return the file-descriptor for the given channel
54 *********************************************************************************
57 int wiringPiSPIGetFd (int channel)
59 return spiFds [channel & 1] ;
65 * Write and Read a block of data over the SPI bus.
66 * Note the data ia being read into the transmit buffer, so will
68 * This is also a full-duplex operation.
69 *********************************************************************************
72 int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
74 struct spi_ioc_transfer spi ;
78 // Mentioned in spidev.h but not used in the original kernel documentation
81 memset (&spi, 0, sizeof (spi)) ;
83 spi.tx_buf = (unsigned long)data ;
84 spi.rx_buf = (unsigned long)data ;
86 spi.delay_usecs = spiDelay ;
87 spi.speed_hz = spiSpeeds [channel] ;
88 spi.bits_per_word = spiBPW ;
90 return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
96 * Open the SPI device, and set it up, etc.
97 *********************************************************************************
100 int wiringPiSPISetup (int channel, int speed)
106 if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
107 return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
109 spiSpeeds [channel] = speed ;
110 spiFds [channel] = fd ;
112 // Set SPI parameters.
113 // Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
114 // copying example code I've seen online...
116 if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0)
117 return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
119 if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
120 return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
122 if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
123 return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;