wiringpi2 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/ext/wiringpi/WiringPi/examples/blink.c +50 -0
  2. data/ext/wiringpi/WiringPi/examples/delayTest.c +107 -0
  3. data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
  4. data/ext/wiringpi/WiringPi/examples/gertboard.c +94 -0
  5. data/ext/wiringpi/WiringPi/examples/header.h +23 -0
  6. data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
  7. data/ext/wiringpi/WiringPi/examples/isr.c +99 -0
  8. data/ext/wiringpi/WiringPi/examples/lcd.c +129 -0
  9. data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
  10. data/ext/wiringpi/WiringPi/examples/okLed.c +83 -0
  11. data/ext/wiringpi/WiringPi/examples/piface.c +74 -0
  12. data/ext/wiringpi/WiringPi/examples/pwm.c +93 -0
  13. data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
  14. data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
  15. data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
  16. data/ext/wiringpi/WiringPi/examples/speed.c +123 -0
  17. data/ext/wiringpi/WiringPi/examples/test1.c +111 -0
  18. data/ext/wiringpi/WiringPi/examples/test2.c +58 -0
  19. data/ext/wiringpi/WiringPi/examples/tone.c +59 -0
  20. data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
  21. data/ext/wiringpi/WiringPi/gpio/gpio.c +1371 -0
  22. data/ext/wiringpi/WiringPi/wiringPi/ds1302.c +239 -0
  23. data/ext/wiringpi/WiringPi/wiringPi/ds1302.h +44 -0
  24. data/ext/wiringpi/WiringPi/wiringPi/gertboard.c +166 -0
  25. data/ext/wiringpi/WiringPi/wiringPi/gertboard.h +40 -0
  26. data/ext/wiringpi/WiringPi/wiringPi/lcd.c +380 -0
  27. data/ext/wiringpi/WiringPi/wiringPi/lcd.h +46 -0
  28. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +155 -0
  29. data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
  30. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
  31. data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
  32. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +195 -0
  33. data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
  34. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +236 -0
  35. data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
  36. data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
  37. data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
  38. data/ext/wiringpi/WiringPi/wiringPi/piFace.c +179 -0
  39. data/ext/wiringpi/WiringPi/wiringPi/piFace.h +32 -0
  40. data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +50 -0
  41. data/ext/wiringpi/WiringPi/wiringPi/piNes.c +113 -0
  42. data/ext/wiringpi/WiringPi/wiringPi/piNes.h +45 -0
  43. data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
  44. data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +130 -0
  45. data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +34 -0
  46. data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
  47. data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
  48. data/ext/wiringpi/WiringPi/wiringPi/softTone.c +121 -0
  49. data/ext/wiringpi/WiringPi/wiringPi/softTone.h +38 -0
  50. data/ext/wiringpi/WiringPi/wiringPi/sr595.c +108 -0
  51. data/ext/wiringpi/WiringPi/wiringPi/sr595.h +33 -0
  52. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +1664 -0
  53. data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +168 -0
  54. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +227 -0
  55. data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
  56. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +119 -0
  57. data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +35 -0
  58. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +213 -0
  59. data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
  60. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
  61. data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
  62. data/ext/wiringpi/extconf.rb +8 -0
  63. data/ext/wiringpi/wiringpi_wrap.c +4378 -0
  64. data/lib/wiringpi/event.rb +21 -0
  65. data/lib/wiringpi/gpio.rb +131 -0
  66. data/lib/wiringpi/i2c.rb +26 -0
  67. data/lib/wiringpi/mcp23x17.rb +31 -0
  68. data/lib/wiringpi/serial.rb +49 -0
  69. data/lib/wiringpi/spi.rb +15 -0
  70. data/lib/wiringpi2.rb +42 -0
  71. metadata +122 -0
@@ -0,0 +1,168 @@
1
+ /*
2
+ * wiringPi:
3
+ * Arduino compatable (ish) Wiring library for the Raspberry Pi
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 published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (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 License
20
+ * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
21
+ ***********************************************************************
22
+ */
23
+
24
+ // Handy defines
25
+
26
+ // Deprecated
27
+ #define NUM_PINS 17
28
+
29
+ #define WPI_MODE_PINS 0
30
+ #define WPI_MODE_GPIO 1
31
+ #define WPI_MODE_GPIO_SYS 2
32
+ #define WPI_MODE_PHYS 3
33
+ #define WPI_MODE_PIFACE 4
34
+ #define WPI_MODE_UNINITIALISED -1
35
+
36
+ // Pin modes
37
+
38
+ #define INPUT 0
39
+ #define OUTPUT 1
40
+ #define PWM_OUTPUT 2
41
+ #define GPIO_CLOCK 3
42
+
43
+ #define LOW 0
44
+ #define HIGH 1
45
+
46
+ // Pull up/down/none
47
+
48
+ #define PUD_OFF 0
49
+ #define PUD_DOWN 1
50
+ #define PUD_UP 2
51
+
52
+ // PWM
53
+
54
+ #define PWM_MODE_MS 0
55
+ #define PWM_MODE_BAL 1
56
+
57
+ // Interrupt levels
58
+
59
+ #define INT_EDGE_SETUP 0
60
+ #define INT_EDGE_FALLING 1
61
+ #define INT_EDGE_RISING 2
62
+ #define INT_EDGE_BOTH 3
63
+
64
+ // Threads
65
+
66
+ #define PI_THREAD(X) void *X (void *dummy)
67
+
68
+ // wiringPiNodeStruct:
69
+ // This describes additional device nodes in the extended wiringPi
70
+ // 2.0 scheme of things.
71
+ // It's a simple linked list for now, but will hopefully migrate to
72
+ // a binary tree for efficiency reasons - but then again, the chances
73
+ // of more than 1 or 2 devices being added are fairly slim, so who
74
+ // knows....
75
+
76
+ struct wiringPiNodeStruct
77
+ {
78
+ int pinBase ;
79
+ int pinMax ;
80
+
81
+ int fd ; // Node specific
82
+ unsigned int data0 ; // ditto
83
+ unsigned int data1 ; // ditto
84
+ unsigned int data2 ; // ditto
85
+ unsigned int data3 ; // ditto
86
+
87
+ void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
88
+ void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
89
+ int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
90
+ void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
91
+ void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
92
+ int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
93
+ void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
94
+
95
+ struct wiringPiNodeStruct *next ;
96
+ } ;
97
+
98
+
99
+ // Function prototypes
100
+ // c++ wrappers thanks to a comment by Nick Lott
101
+ // (and others on the Raspberry Pi forums)
102
+
103
+ #ifdef __cplusplus
104
+ extern "C" {
105
+ #endif
106
+
107
+
108
+ // Core wiringPi functions
109
+
110
+ extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
111
+
112
+ extern int wiringPiSetup (void) ;
113
+ extern int wiringPiSetupSys (void) ;
114
+ extern int wiringPiSetupGpio (void) ;
115
+ extern int wiringPiSetupPhys (void) ;
116
+
117
+ extern void pinMode (int pin, int mode) ;
118
+ extern void pullUpDnControl (int pin, int pud) ;
119
+ extern int digitalRead (int pin) ;
120
+ extern void digitalWrite (int pin, int value) ;
121
+ extern void pwmWrite (int pin, int value) ;
122
+ extern int analogRead (int pin) ;
123
+ extern void analogWrite (int pin, int value) ;
124
+
125
+ // PiFace specifics
126
+ // (Deprecated)
127
+
128
+ extern int wiringPiSetupPiFace (void) ;
129
+ extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
130
+
131
+ // On-Board Raspberry Pi hardware specific stuff
132
+
133
+ extern int piBoardRev (void) ;
134
+ extern int wpiPinToGpio (int wpiPin) ;
135
+ extern void setPadDrive (int group, int value) ;
136
+ extern int getAlt (int pin) ;
137
+ extern void digitalWriteByte (int value) ;
138
+ extern void pwmSetMode (int mode) ;
139
+ extern void pwmSetRange (unsigned int range) ;
140
+ extern void pwmSetClock (int divisor) ;
141
+ extern void gpioClockSet (int pin, int freq) ;
142
+
143
+ // Interrupts
144
+ // (Also Pi hardware specific)
145
+
146
+ extern int waitForInterrupt (int pin, int mS) ;
147
+ extern int wiringPiISR (int pin, int mode, void (*function)(void)) ;
148
+
149
+ // Threads
150
+
151
+ extern int piThreadCreate (void *(*fn)(void *)) ;
152
+ extern void piLock (int key) ;
153
+ extern void piUnlock (int key) ;
154
+
155
+ // Schedulling priority
156
+
157
+ extern int piHiPri (int pri) ;
158
+
159
+ // Extras from arduino land
160
+
161
+ extern void delay (unsigned int howLong) ;
162
+ extern void delayMicroseconds (unsigned int howLong) ;
163
+ extern unsigned int millis (void) ;
164
+ extern unsigned int micros (void) ;
165
+
166
+ #ifdef __cplusplus
167
+ }
168
+ #endif
@@ -0,0 +1,227 @@
1
+ /*
2
+ * wiringPiI2C.c:
3
+ * Simplified I2C access routines
4
+ * Copyright (c) 2013 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
+ * Notes:
27
+ * The Linux I2C code is actually the same (almost) as the SMBus code.
28
+ * SMBus is System Management Bus - and in essentially I2C with some
29
+ * additional functionality added, and stricter controls on the electrical
30
+ * specifications, etc. however I2C does work well with it and the
31
+ * protocols work over both.
32
+ *
33
+ * I'm directly including the SMBus functions here as some Linux distros
34
+ * lack the correct header files, and also some header files are GPLv2
35
+ * rather than the LGPL that wiringPi is released under - presumably because
36
+ * originally no-one expected I2C/SMBus to be used outside the kernel -
37
+ * however enter the Raspberry Pi with people now taking directly to I2C
38
+ * devices without going via the kernel...
39
+ *
40
+ * This may ultimately reduce the flexibility of this code, but it won't be
41
+ * hard to maintain it and keep it current, should things change.
42
+ *
43
+ * Information here gained from: kernel/Documentation/i2c/dev-interface
44
+ * as well as other online resources.
45
+ *********************************************************************************
46
+ */
47
+
48
+ #include <stdio.h>
49
+ #include <stdlib.h>
50
+ #include <stdint.h>
51
+ #include <fcntl.h>
52
+ #include <sys/ioctl.h>
53
+
54
+ #include "wiringPi.h"
55
+ #include "wiringPiI2C.h"
56
+
57
+ // I2C definitions
58
+
59
+ #define I2C_SLAVE 0x0703
60
+ #define I2C_SMBUS 0x0720 /* SMBus-level access */
61
+
62
+ #define I2C_SMBUS_READ 1
63
+ #define I2C_SMBUS_WRITE 0
64
+
65
+ // SMBus transaction types
66
+
67
+ #define I2C_SMBUS_QUICK 0
68
+ #define I2C_SMBUS_BYTE 1
69
+ #define I2C_SMBUS_BYTE_DATA 2
70
+ #define I2C_SMBUS_WORD_DATA 3
71
+ #define I2C_SMBUS_PROC_CALL 4
72
+ #define I2C_SMBUS_BLOCK_DATA 5
73
+ #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
74
+ #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
75
+ #define I2C_SMBUS_I2C_BLOCK_DATA 8
76
+
77
+ // SMBus messages
78
+
79
+ #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
80
+ #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
81
+
82
+ // Structures used in the ioctl() calls
83
+
84
+ union i2c_smbus_data
85
+ {
86
+ uint8_t byte ;
87
+ uint16_t word ;
88
+ uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC
89
+ } ;
90
+
91
+ struct i2c_smbus_ioctl_data
92
+ {
93
+ char read_write ;
94
+ uint8_t command ;
95
+ int size ;
96
+ union i2c_smbus_data *data ;
97
+ } ;
98
+
99
+ static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data)
100
+ {
101
+ struct i2c_smbus_ioctl_data args ;
102
+
103
+ args.read_write = rw ;
104
+ args.command = command ;
105
+ args.size = size ;
106
+ args.data = data ;
107
+ return ioctl (fd, I2C_SMBUS, &args) ;
108
+ }
109
+
110
+
111
+ /*
112
+ * wiringPiI2CRead:
113
+ * Simple device read
114
+ *********************************************************************************
115
+ */
116
+
117
+ int wiringPiI2CRead (int fd)
118
+ {
119
+ union i2c_smbus_data data ;
120
+
121
+ if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
122
+ return -1 ;
123
+ else
124
+ return data.byte & 0xFF ;
125
+ }
126
+
127
+
128
+ /*
129
+ * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
130
+ * Read an 8 or 16-bit value from a regsiter on the device
131
+ *********************************************************************************
132
+ */
133
+
134
+ int wiringPiI2CReadReg8 (int fd, int reg)
135
+ {
136
+ union i2c_smbus_data data;
137
+
138
+ if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data))
139
+ return -1 ;
140
+ else
141
+ return data.byte & 0xFF ;
142
+ }
143
+
144
+ int wiringPiI2CReadReg16 (int fd, int reg)
145
+ {
146
+ union i2c_smbus_data data;
147
+
148
+ if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
149
+ return -1 ;
150
+ else
151
+ return data.byte & 0xFF ;
152
+ }
153
+
154
+
155
+ /*
156
+ * wiringPiI2CWrite:
157
+ * Simple device write
158
+ *********************************************************************************
159
+ */
160
+
161
+ int wiringPiI2CWrite (int fd, int data)
162
+ {
163
+ return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
164
+ }
165
+
166
+
167
+ /*
168
+ * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
169
+ * Write an 8 or 16-bit value to the given register
170
+ *********************************************************************************
171
+ */
172
+
173
+ int wiringPiI2CWriteReg8 (int fd, int reg, int value)
174
+ {
175
+ union i2c_smbus_data data ;
176
+
177
+ data.byte = value ;
178
+ return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
179
+ }
180
+
181
+ int wiringPiI2CWriteReg16 (int fd, int reg, int value)
182
+ {
183
+ union i2c_smbus_data data ;
184
+
185
+ data.word = value ;
186
+ return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
187
+ }
188
+
189
+
190
+ /*
191
+ * wiringPiI2CSetup:
192
+ * Open the I2C device, and regsiter the target device
193
+ *********************************************************************************
194
+ */
195
+
196
+ int wiringPiI2CSetupInterface (char *device, int devId)
197
+ {
198
+ int fd ;
199
+
200
+ if ((fd = open (device, O_RDWR)) < 0)
201
+ return -1 ;
202
+
203
+ if (ioctl (fd, I2C_SLAVE, devId) < 0)
204
+ return -1 ;
205
+
206
+ return fd ;
207
+ }
208
+
209
+
210
+ int wiringPiI2CSetup (int devId)
211
+ {
212
+ int rev ;
213
+ char *device ;
214
+
215
+ if ((rev = piBoardRev ()) < 0)
216
+ {
217
+ fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
218
+ exit (1) ;
219
+ }
220
+
221
+ if (rev == 1)
222
+ device = "/dev/i2c-0" ;
223
+ else
224
+ device = "/dev/i2c-1" ;
225
+
226
+ return wiringPiI2CSetupInterface (device, devId) ;
227
+ }
@@ -0,0 +1,42 @@
1
+ /*
2
+ * wiringPiI2C.h:
3
+ * Simplified I2C access routines
4
+ * Copyright (c) 2013 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
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ extern int wiringPiI2CRead (int fd) ;
30
+ extern int wiringPiI2CReadReg8 (int fd, int reg) ;
31
+ extern int wiringPiI2CReadReg16 (int fd, int reg) ;
32
+
33
+ extern int wiringPiI2CWrite (int fd, int data) ;
34
+ extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ;
35
+ extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ;
36
+
37
+ extern int wiringPiI2CSetupInterface (char *device, int devId) ;
38
+ extern int wiringPiI2CSetup (int devId) ;
39
+
40
+ #ifdef __cplusplus
41
+ }
42
+ #endif
@@ -0,0 +1,119 @@
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 "wiringPi.h"
32
+
33
+ #include "wiringPiSPI.h"
34
+
35
+
36
+ // The SPI bus parameters
37
+ // Variables as they need to be passed as pointers later on
38
+
39
+ static char *spiDev0 = "/dev/spidev0.0" ;
40
+ static char *spiDev1 = "/dev/spidev0.1" ;
41
+ static uint8_t spiMode = 0 ;
42
+ static uint8_t spiBPW = 8 ;
43
+ static uint16_t spiDelay = 0;
44
+
45
+ static uint32_t spiSpeeds [2] ;
46
+ static int spiFds [2] ;
47
+
48
+
49
+ /*
50
+ * wiringPiSPIGetFd:
51
+ * Return the file-descriptor for the given channel
52
+ *********************************************************************************
53
+ */
54
+
55
+ int wiringPiSPIGetFd (int channel)
56
+ {
57
+ return spiFds [channel & 1] ;
58
+ }
59
+
60
+
61
+ /*
62
+ * wiringPiSPIDataRW:
63
+ * Write and Read a block of data over the SPI bus.
64
+ * Note the data ia being read into the transmit buffer, so will
65
+ * overwrite it!
66
+ * This is also a full-duplex operation.
67
+ *********************************************************************************
68
+ */
69
+
70
+ int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
71
+ {
72
+ struct spi_ioc_transfer spi ;
73
+
74
+ channel &= 1 ;
75
+
76
+ spi.tx_buf = (unsigned long)data ;
77
+ spi.rx_buf = (unsigned long)data ;
78
+ spi.len = len ;
79
+ spi.delay_usecs = spiDelay ;
80
+ spi.speed_hz = spiSpeeds [channel] ;
81
+ spi.bits_per_word = spiBPW ;
82
+
83
+ return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
84
+ }
85
+
86
+
87
+ /*
88
+ * wiringPiSPISetup:
89
+ * Open the SPI device, and set it up, etc.
90
+ *********************************************************************************
91
+ */
92
+
93
+ int wiringPiSPISetup (int channel, int speed)
94
+ {
95
+ int fd ;
96
+
97
+ channel &= 1 ;
98
+
99
+ if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
100
+ return -1 ;
101
+
102
+ spiSpeeds [channel] = speed ;
103
+ spiFds [channel] = fd ;
104
+
105
+ // Set SPI parameters.
106
+ // Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
107
+ // copying example code I've seen online...
108
+
109
+ if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) return -1 ;
110
+ if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) return -1 ;
111
+
112
+ if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
113
+ if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
114
+
115
+ if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return -1 ;
116
+ if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) return -1 ;
117
+
118
+ return fd ;
119
+ }