chiark / gitweb /
44414985318a3bcee99ebfd38173ea5bea18242e
[wiringPi.git] / wiringPi / wiringPiSPI.c
1 /*
2  * wiringPiSPI.c:
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/
8  *
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.
13  *
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.
18  *
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  ***********************************************************************
23  */
24
25
26 #include <stdint.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <linux/spi/spidev.h>
30
31 #include "wiringPiSPI.h"
32
33
34 // The SPI bus parameters
35 //      Variables as they need to be passed as pointers later on
36
37 static char       *spiDev0 = "/dev/spidev0.0" ;
38 static char       *spiDev1 = "/dev/spidev0.1" ;
39 static uint8_t     spiMode   = 0 ;
40 static uint8_t     spiBPW    = 8 ;
41 static uint16_t    spiDelay  = 0;
42
43 static uint32_t    spiSpeeds [2] ;
44 static int         spiFds [2] ;
45
46
47 /*
48  * wiringPiSPIGetFd:
49  *      Return the file-descriptor for the given channel
50  *********************************************************************************
51  */
52
53 int wiringPiSPIGetFd (int channel)
54 {
55   return spiFds [channel & 1] ;
56 }
57
58
59 /*
60  * wiringPiSPIDataRW:
61  *      Write and Read a block of data over the SPI bus.
62  *      Note the data ia being read into the transmit buffer, so will
63  *      overwrite it!
64  *      This is also a full-duplex operation.
65  *********************************************************************************
66  */
67
68 int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
69 {
70   struct spi_ioc_transfer spi ;
71
72   channel &= 1 ;
73
74   spi.tx_buf        = (unsigned long)data ;
75   spi.rx_buf        = (unsigned long)data ;
76   spi.len           = len ;
77   spi.delay_usecs   = spiDelay ;
78   spi.speed_hz      = spiSpeeds [channel] ;
79   spi.bits_per_word = spiBPW ;
80
81   return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
82 }
83
84
85 /*
86  * wiringPiSPISetup:
87  *      Open the SPI device, and set it up, etc.
88  *********************************************************************************
89  */
90
91 int wiringPiSPISetup (int channel, int speed)
92 {
93   int fd ;
94
95   channel &= 1 ;
96
97   if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
98     return -1 ;
99
100   spiSpeeds [channel] = speed ;
101   spiFds    [channel] = fd ;
102
103 // Set SPI parameters.
104 //      Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
105 //      copying example code I've seen online...
106
107   if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode)         < 0) return -1 ;
108   if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode)         < 0) return -1 ;
109
110   if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
111   if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
112
113   if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed)   < 0) return -1 ;
114   if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed)   < 0) return -1 ;
115
116   return fd ;
117 }