wiringpi 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/wiringpi/WiringPi/devLib/ds1302.c +240 -0
- data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
- data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.c +495 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +673 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.c +165 -0
- data/ext/wiringpi/{serial.h → WiringPi/devLib/maxdetect.h} +14 -15
- data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
- data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +178 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.c +113 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/7segments.c +221 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/buttons.c +83 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/gertboard.c +96 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/record.c +60 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/temperature.c +78 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/voltmeter.c +73 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/vumeter.c +152 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/blink.c +59 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/buttons.c +103 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/ladder.c +337 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/metro.c +111 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/motor.c +120 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/reaction.c +194 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow0.c +51 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow1.c +258 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piglow.c +176 -0
- data/ext/wiringpi/WiringPi/examples/blink.c +48 -0
- data/ext/wiringpi/WiringPi/examples/blink12.c +111 -0
- data/ext/wiringpi/WiringPi/examples/blink12drcs.c +125 -0
- data/ext/wiringpi/WiringPi/examples/blink6drcs.c +115 -0
- data/ext/wiringpi/WiringPi/examples/blink8.c +57 -0
- data/ext/wiringpi/WiringPi/examples/clock.c +201 -0
- data/ext/wiringpi/WiringPi/examples/delayTest.c +107 -0
- data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
- data/ext/wiringpi/WiringPi/examples/header.h +23 -0
- data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
- data/ext/wiringpi/WiringPi/examples/isr.c +110 -0
- data/ext/wiringpi/WiringPi/examples/lcd-adafruit.c +347 -0
- data/ext/wiringpi/WiringPi/examples/lcd.c +286 -0
- data/ext/wiringpi/WiringPi/examples/lowPower.c +68 -0
- data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
- data/ext/wiringpi/WiringPi/examples/okLed.c +82 -0
- data/ext/wiringpi/WiringPi/examples/pwm.c +58 -0
- data/ext/wiringpi/WiringPi/examples/q2w/binary.c +79 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink-io.c +61 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink.c +50 -0
- data/ext/wiringpi/WiringPi/examples/q2w/bright.c +59 -0
- data/ext/wiringpi/WiringPi/examples/q2w/button.c +63 -0
- data/ext/wiringpi/WiringPi/examples/q2w/volts.c +62 -0
- data/ext/wiringpi/WiringPi/examples/rht03.c +69 -0
- data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
- data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
- data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
- data/ext/wiringpi/WiringPi/examples/softPwm.c +89 -0
- data/ext/wiringpi/WiringPi/examples/softTone.c +54 -0
- data/ext/wiringpi/WiringPi/examples/speed.c +95 -0
- data/ext/wiringpi/WiringPi/examples/spiSpeed.c +118 -0
- data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
- data/ext/wiringpi/WiringPi/gpio/gpio.c +1351 -0
- data/ext/wiringpi/WiringPi/gpio/pins.c +33 -0
- data/ext/wiringpi/WiringPi/gpio/readall.c +334 -0
- data/ext/wiringpi/WiringPi/gpio/version.h +1 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +201 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.c +81 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +189 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +236 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +110 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +90 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
- data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +166 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.c +109 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +2007 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +224 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +232 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +136 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +36 -0
- data/ext/wiringpi/{serial.c → WiringPi/wiringPi/wiringSerial.c} +38 -29
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
- data/ext/wiringpi/{wiringShift.c → WiringPi/wiringPi/wiringShift.c} +3 -4
- data/ext/wiringpi/{wiringShift.h → WiringPi/wiringPi/wiringShift.h} +5 -5
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.c +731 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.h +26 -0
- data/ext/wiringpi/extconf.rb +7 -1
- data/ext/wiringpi/wiringpi_wrap.c +1741 -218
- data/lib/wiringpi.rb +24 -222
- data/lib/wiringpi/event.rb +21 -0
- data/lib/wiringpi/gpio.rb +139 -0
- data/lib/wiringpi/i2c.rb +26 -0
- data/lib/wiringpi/mcp23x17.rb +31 -0
- data/lib/wiringpi/serial.rb +49 -0
- data/lib/wiringpi/spi.rb +15 -0
- metadata +146 -22
- data/ext/wiringpi/wiringPi.c +0 -729
- data/ext/wiringpi/wiringPi.h +0 -69
@@ -0,0 +1,224 @@
|
|
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
|
+
#ifndef __WIRING_PI_H__
|
25
|
+
#define __WIRING_PI_H__
|
26
|
+
|
27
|
+
// Handy defines
|
28
|
+
|
29
|
+
// Deprecated
|
30
|
+
#define NUM_PINS 17
|
31
|
+
|
32
|
+
#define WPI_MODE_PINS 0
|
33
|
+
#define WPI_MODE_GPIO 1
|
34
|
+
#define WPI_MODE_GPIO_SYS 2
|
35
|
+
#define WPI_MODE_PHYS 3
|
36
|
+
#define WPI_MODE_PIFACE 4
|
37
|
+
#define WPI_MODE_UNINITIALISED -1
|
38
|
+
|
39
|
+
// Pin modes
|
40
|
+
|
41
|
+
#define INPUT 0
|
42
|
+
#define OUTPUT 1
|
43
|
+
#define PWM_OUTPUT 2
|
44
|
+
#define GPIO_CLOCK 3
|
45
|
+
#define SOFT_PWM_OUTPUT 4
|
46
|
+
#define SOFT_TONE_OUTPUT 5
|
47
|
+
#define PWM_TONE_OUTPUT 6
|
48
|
+
|
49
|
+
#define LOW 0
|
50
|
+
#define HIGH 1
|
51
|
+
|
52
|
+
// Pull up/down/none
|
53
|
+
|
54
|
+
#define PUD_OFF 0
|
55
|
+
#define PUD_DOWN 1
|
56
|
+
#define PUD_UP 2
|
57
|
+
|
58
|
+
// PWM
|
59
|
+
|
60
|
+
#define PWM_MODE_MS 0
|
61
|
+
#define PWM_MODE_BAL 1
|
62
|
+
|
63
|
+
// Interrupt levels
|
64
|
+
|
65
|
+
#define INT_EDGE_SETUP 0
|
66
|
+
#define INT_EDGE_FALLING 1
|
67
|
+
#define INT_EDGE_RISING 2
|
68
|
+
#define INT_EDGE_BOTH 3
|
69
|
+
|
70
|
+
// Pi model types and version numbers
|
71
|
+
// Intended for the GPIO program Use at your own risk.
|
72
|
+
|
73
|
+
#define PI_MODEL_UNKNOWN 0
|
74
|
+
#define PI_MODEL_A 1
|
75
|
+
#define PI_MODEL_B 2
|
76
|
+
#define PI_MODEL_BP 3
|
77
|
+
#define PI_MODEL_CM 4
|
78
|
+
#define PI_MODEL_AP 5
|
79
|
+
#define PI_MODEL_2 6
|
80
|
+
|
81
|
+
#define PI_VERSION_UNKNOWN 0
|
82
|
+
#define PI_VERSION_1 1
|
83
|
+
#define PI_VERSION_1_1 2
|
84
|
+
#define PI_VERSION_1_2 3
|
85
|
+
#define PI_VERSION_2 4
|
86
|
+
|
87
|
+
#define PI_MAKER_UNKNOWN 0
|
88
|
+
#define PI_MAKER_EGOMAN 1
|
89
|
+
#define PI_MAKER_SONY 2
|
90
|
+
#define PI_MAKER_QISDA 3
|
91
|
+
#define PI_MAKER_MBEST 4
|
92
|
+
|
93
|
+
extern const char *piModelNames [7] ;
|
94
|
+
extern const char *piRevisionNames [5] ;
|
95
|
+
extern const char *piMakerNames [5] ;
|
96
|
+
|
97
|
+
|
98
|
+
// Intended for the GPIO program Use at your own risk.
|
99
|
+
|
100
|
+
// Threads
|
101
|
+
|
102
|
+
#define PI_THREAD(X) void *X (void *dummy)
|
103
|
+
|
104
|
+
// Failure modes
|
105
|
+
|
106
|
+
#define WPI_FATAL (1==1)
|
107
|
+
#define WPI_ALMOST (1==2)
|
108
|
+
|
109
|
+
|
110
|
+
// wiringPiNodeStruct:
|
111
|
+
// This describes additional device nodes in the extended wiringPi
|
112
|
+
// 2.0 scheme of things.
|
113
|
+
// It's a simple linked list for now, but will hopefully migrate to
|
114
|
+
// a binary tree for efficiency reasons - but then again, the chances
|
115
|
+
// of more than 1 or 2 devices being added are fairly slim, so who
|
116
|
+
// knows....
|
117
|
+
|
118
|
+
struct wiringPiNodeStruct
|
119
|
+
{
|
120
|
+
int pinBase ;
|
121
|
+
int pinMax ;
|
122
|
+
|
123
|
+
int fd ; // Node specific
|
124
|
+
unsigned int data0 ; // ditto
|
125
|
+
unsigned int data1 ; // ditto
|
126
|
+
unsigned int data2 ; // ditto
|
127
|
+
unsigned int data3 ; // ditto
|
128
|
+
|
129
|
+
void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
|
130
|
+
void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
|
131
|
+
int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
|
132
|
+
void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
133
|
+
void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
134
|
+
int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
|
135
|
+
void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
|
136
|
+
|
137
|
+
struct wiringPiNodeStruct *next ;
|
138
|
+
} ;
|
139
|
+
|
140
|
+
extern struct wiringPiNodeStruct *wiringPiNodes ;
|
141
|
+
|
142
|
+
|
143
|
+
// Function prototypes
|
144
|
+
// c++ wrappers thanks to a comment by Nick Lott
|
145
|
+
// (and others on the Raspberry Pi forums)
|
146
|
+
|
147
|
+
#ifdef __cplusplus
|
148
|
+
extern "C" {
|
149
|
+
#endif
|
150
|
+
|
151
|
+
// Data
|
152
|
+
|
153
|
+
// Internal
|
154
|
+
|
155
|
+
extern int wiringPiFailure (int fatal, const char *message, ...) ;
|
156
|
+
|
157
|
+
// Core wiringPi functions
|
158
|
+
|
159
|
+
extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
|
160
|
+
extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
|
161
|
+
|
162
|
+
extern int wiringPiSetup (void) ;
|
163
|
+
extern int wiringPiSetupSys (void) ;
|
164
|
+
extern int wiringPiSetupGpio (void) ;
|
165
|
+
extern int wiringPiSetupPhys (void) ;
|
166
|
+
|
167
|
+
extern void pinModeAlt (int pin, int mode) ;
|
168
|
+
extern void pinMode (int pin, int mode) ;
|
169
|
+
extern void pullUpDnControl (int pin, int pud) ;
|
170
|
+
extern int digitalRead (int pin) ;
|
171
|
+
extern void digitalWrite (int pin, int value) ;
|
172
|
+
extern void pwmWrite (int pin, int value) ;
|
173
|
+
extern int analogRead (int pin) ;
|
174
|
+
extern void analogWrite (int pin, int value) ;
|
175
|
+
|
176
|
+
// PiFace specifics
|
177
|
+
// (Deprecated)
|
178
|
+
|
179
|
+
extern int wiringPiSetupPiFace (void) ;
|
180
|
+
extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
|
181
|
+
|
182
|
+
// On-Board Raspberry Pi hardware specific stuff
|
183
|
+
|
184
|
+
extern int piBoardRev (void) ;
|
185
|
+
extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ;
|
186
|
+
extern int wpiPinToGpio (int wpiPin) ;
|
187
|
+
extern int physPinToGpio (int physPin) ;
|
188
|
+
extern void setPadDrive (int group, int value) ;
|
189
|
+
extern int getAlt (int pin) ;
|
190
|
+
extern void pwmToneWrite (int pin, int freq) ;
|
191
|
+
extern void digitalWriteByte (int value) ;
|
192
|
+
extern void pwmSetMode (int mode) ;
|
193
|
+
extern void pwmSetRange (unsigned int range) ;
|
194
|
+
extern void pwmSetClock (int divisor) ;
|
195
|
+
extern void gpioClockSet (int pin, int freq) ;
|
196
|
+
|
197
|
+
// Interrupts
|
198
|
+
// (Also Pi hardware specific)
|
199
|
+
|
200
|
+
extern int waitForInterrupt (int pin, int mS) ;
|
201
|
+
extern int wiringPiISR (int pin, int mode, void (*function)(void)) ;
|
202
|
+
|
203
|
+
// Threads
|
204
|
+
|
205
|
+
extern int piThreadCreate (void *(*fn)(void *)) ;
|
206
|
+
extern void piLock (int key) ;
|
207
|
+
extern void piUnlock (int key) ;
|
208
|
+
|
209
|
+
// Schedulling priority
|
210
|
+
|
211
|
+
extern int piHiPri (const int pri) ;
|
212
|
+
|
213
|
+
// Extras from arduino land
|
214
|
+
|
215
|
+
extern void delay (unsigned int howLong) ;
|
216
|
+
extern void delayMicroseconds (unsigned int howLong) ;
|
217
|
+
extern unsigned int millis (void) ;
|
218
|
+
extern unsigned int micros (void) ;
|
219
|
+
|
220
|
+
#ifdef __cplusplus
|
221
|
+
}
|
222
|
+
#endif
|
223
|
+
|
224
|
+
#endif
|
@@ -0,0 +1,232 @@
|
|
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 <errno.h>
|
52
|
+
#include <string.h>
|
53
|
+
#include <fcntl.h>
|
54
|
+
#include <sys/ioctl.h>
|
55
|
+
|
56
|
+
#include "wiringPi.h"
|
57
|
+
#include "wiringPiI2C.h"
|
58
|
+
|
59
|
+
// I2C definitions
|
60
|
+
|
61
|
+
#define I2C_SLAVE 0x0703
|
62
|
+
#define I2C_SMBUS 0x0720 /* SMBus-level access */
|
63
|
+
|
64
|
+
#define I2C_SMBUS_READ 1
|
65
|
+
#define I2C_SMBUS_WRITE 0
|
66
|
+
|
67
|
+
// SMBus transaction types
|
68
|
+
|
69
|
+
#define I2C_SMBUS_QUICK 0
|
70
|
+
#define I2C_SMBUS_BYTE 1
|
71
|
+
#define I2C_SMBUS_BYTE_DATA 2
|
72
|
+
#define I2C_SMBUS_WORD_DATA 3
|
73
|
+
#define I2C_SMBUS_PROC_CALL 4
|
74
|
+
#define I2C_SMBUS_BLOCK_DATA 5
|
75
|
+
#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
|
76
|
+
#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
|
77
|
+
#define I2C_SMBUS_I2C_BLOCK_DATA 8
|
78
|
+
|
79
|
+
// SMBus messages
|
80
|
+
|
81
|
+
#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
|
82
|
+
#define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
|
83
|
+
|
84
|
+
// Structures used in the ioctl() calls
|
85
|
+
|
86
|
+
union i2c_smbus_data
|
87
|
+
{
|
88
|
+
uint8_t byte ;
|
89
|
+
uint16_t word ;
|
90
|
+
uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC
|
91
|
+
} ;
|
92
|
+
|
93
|
+
struct i2c_smbus_ioctl_data
|
94
|
+
{
|
95
|
+
char read_write ;
|
96
|
+
uint8_t command ;
|
97
|
+
int size ;
|
98
|
+
union i2c_smbus_data *data ;
|
99
|
+
} ;
|
100
|
+
|
101
|
+
static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data)
|
102
|
+
{
|
103
|
+
struct i2c_smbus_ioctl_data args ;
|
104
|
+
|
105
|
+
args.read_write = rw ;
|
106
|
+
args.command = command ;
|
107
|
+
args.size = size ;
|
108
|
+
args.data = data ;
|
109
|
+
return ioctl (fd, I2C_SMBUS, &args) ;
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
/*
|
114
|
+
* wiringPiI2CRead:
|
115
|
+
* Simple device read
|
116
|
+
*********************************************************************************
|
117
|
+
*/
|
118
|
+
|
119
|
+
int wiringPiI2CRead (int fd)
|
120
|
+
{
|
121
|
+
union i2c_smbus_data data ;
|
122
|
+
|
123
|
+
if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
|
124
|
+
return -1 ;
|
125
|
+
else
|
126
|
+
return data.byte & 0xFF ;
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
/*
|
131
|
+
* wiringPiI2CReadReg8: wiringPiI2CReadReg16:
|
132
|
+
* Read an 8 or 16-bit value from a regsiter on the device
|
133
|
+
*********************************************************************************
|
134
|
+
*/
|
135
|
+
|
136
|
+
int wiringPiI2CReadReg8 (int fd, int reg)
|
137
|
+
{
|
138
|
+
union i2c_smbus_data data;
|
139
|
+
|
140
|
+
if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data))
|
141
|
+
return -1 ;
|
142
|
+
else
|
143
|
+
return data.byte & 0xFF ;
|
144
|
+
}
|
145
|
+
|
146
|
+
int wiringPiI2CReadReg16 (int fd, int reg)
|
147
|
+
{
|
148
|
+
union i2c_smbus_data data;
|
149
|
+
|
150
|
+
if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
|
151
|
+
return -1 ;
|
152
|
+
else
|
153
|
+
return data.word & 0xFFFF ;
|
154
|
+
}
|
155
|
+
|
156
|
+
|
157
|
+
/*
|
158
|
+
* wiringPiI2CWrite:
|
159
|
+
* Simple device write
|
160
|
+
*********************************************************************************
|
161
|
+
*/
|
162
|
+
|
163
|
+
int wiringPiI2CWrite (int fd, int data)
|
164
|
+
{
|
165
|
+
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
/*
|
170
|
+
* wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
|
171
|
+
* Write an 8 or 16-bit value to the given register
|
172
|
+
*********************************************************************************
|
173
|
+
*/
|
174
|
+
|
175
|
+
int wiringPiI2CWriteReg8 (int fd, int reg, int value)
|
176
|
+
{
|
177
|
+
union i2c_smbus_data data ;
|
178
|
+
|
179
|
+
data.byte = value ;
|
180
|
+
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
|
181
|
+
}
|
182
|
+
|
183
|
+
int wiringPiI2CWriteReg16 (int fd, int reg, int value)
|
184
|
+
{
|
185
|
+
union i2c_smbus_data data ;
|
186
|
+
|
187
|
+
data.word = value ;
|
188
|
+
return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
/*
|
193
|
+
* wiringPiI2CSetupInterface:
|
194
|
+
* Undocumented access to set the interface explicitly - might be used
|
195
|
+
* for the Pi's 2nd I2C interface...
|
196
|
+
*********************************************************************************
|
197
|
+
*/
|
198
|
+
|
199
|
+
int wiringPiI2CSetupInterface (const char *device, int devId)
|
200
|
+
{
|
201
|
+
int fd ;
|
202
|
+
|
203
|
+
if ((fd = open (device, O_RDWR)) < 0)
|
204
|
+
return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
|
205
|
+
|
206
|
+
if (ioctl (fd, I2C_SLAVE, devId) < 0)
|
207
|
+
return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
|
208
|
+
|
209
|
+
return fd ;
|
210
|
+
}
|
211
|
+
|
212
|
+
|
213
|
+
/*
|
214
|
+
* wiringPiI2CSetup:
|
215
|
+
* Open the I2C device, and regsiter the target device
|
216
|
+
*********************************************************************************
|
217
|
+
*/
|
218
|
+
|
219
|
+
int wiringPiI2CSetup (const int devId)
|
220
|
+
{
|
221
|
+
int rev ;
|
222
|
+
const char *device ;
|
223
|
+
|
224
|
+
rev = piBoardRev () ;
|
225
|
+
|
226
|
+
if (rev == 1)
|
227
|
+
device = "/dev/i2c-0" ;
|
228
|
+
else
|
229
|
+
device = "/dev/i2c-1" ;
|
230
|
+
|
231
|
+
return wiringPiI2CSetupInterface (device, devId) ;
|
232
|
+
}
|