wiringpi2 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/wiringpi/WiringPi/examples/blink.c +50 -0
- data/ext/wiringpi/WiringPi/examples/delayTest.c +107 -0
- data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
- data/ext/wiringpi/WiringPi/examples/gertboard.c +94 -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 +99 -0
- data/ext/wiringpi/WiringPi/examples/lcd.c +129 -0
- data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
- data/ext/wiringpi/WiringPi/examples/okLed.c +83 -0
- data/ext/wiringpi/WiringPi/examples/piface.c +74 -0
- data/ext/wiringpi/WiringPi/examples/pwm.c +93 -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/speed.c +123 -0
- data/ext/wiringpi/WiringPi/examples/test1.c +111 -0
- data/ext/wiringpi/WiringPi/examples/test2.c +58 -0
- data/ext/wiringpi/WiringPi/examples/tone.c +59 -0
- data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
- data/ext/wiringpi/WiringPi/gpio/gpio.c +1371 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds1302.c +239 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds1302.h +44 -0
- data/ext/wiringpi/WiringPi/wiringPi/gertboard.c +166 -0
- data/ext/wiringpi/WiringPi/wiringPi/gertboard.h +40 -0
- data/ext/wiringpi/WiringPi/wiringPi/lcd.c +380 -0
- data/ext/wiringpi/WiringPi/wiringPi/lcd.h +46 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +155 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -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 +195 -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/piFace.c +179 -0
- data/ext/wiringpi/WiringPi/wiringPi/piFace.h +32 -0
- data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +50 -0
- data/ext/wiringpi/WiringPi/wiringPi/piNes.c +113 -0
- data/ext/wiringpi/WiringPi/wiringPi/piNes.h +45 -0
- data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +130 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +34 -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 +121 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.h +38 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.c +108 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +1664 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +168 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +227 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +119 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +213 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
- data/ext/wiringpi/extconf.rb +8 -0
- data/ext/wiringpi/wiringpi_wrap.c +4378 -0
- data/lib/wiringpi/event.rb +21 -0
- data/lib/wiringpi/gpio.rb +131 -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
- data/lib/wiringpi2.rb +42 -0
- metadata +122 -0
@@ -0,0 +1,155 @@
|
|
1
|
+
/*
|
2
|
+
* mcp23008.c:
|
3
|
+
* Extend wiringPi with the MCP 23008 I2C GPIO expander chip
|
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
|
+
#include <stdio.h>
|
26
|
+
#include <pthread.h>
|
27
|
+
|
28
|
+
#include "wiringPi.h"
|
29
|
+
#include "wiringPiI2C.h"
|
30
|
+
#include "mcp23x0817.h"
|
31
|
+
|
32
|
+
#include "mcp23008.h"
|
33
|
+
|
34
|
+
|
35
|
+
/*
|
36
|
+
* myPinMode:
|
37
|
+
*********************************************************************************
|
38
|
+
*/
|
39
|
+
|
40
|
+
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
41
|
+
{
|
42
|
+
int mask, old, ddr ;
|
43
|
+
|
44
|
+
pin -= node->pinBase ;
|
45
|
+
ddr = MCP23x08_IODIR ;
|
46
|
+
mask = 1 << pin ;
|
47
|
+
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
|
48
|
+
|
49
|
+
if (mode == OUTPUT)
|
50
|
+
old &= (~mask) ;
|
51
|
+
else
|
52
|
+
old |= mask ;
|
53
|
+
|
54
|
+
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
/*
|
59
|
+
* myPullUpDnControl:
|
60
|
+
*********************************************************************************
|
61
|
+
*/
|
62
|
+
|
63
|
+
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
64
|
+
{
|
65
|
+
int mask, old, pud ;
|
66
|
+
|
67
|
+
pin -= node->pinBase ;
|
68
|
+
pud = MCP23x08_GPPU ;
|
69
|
+
mask = 1 << pin ;
|
70
|
+
|
71
|
+
old = wiringPiI2CReadReg8 (node->fd, pud) ;
|
72
|
+
|
73
|
+
if (mode == PUD_UP)
|
74
|
+
old |= mask ;
|
75
|
+
else
|
76
|
+
old &= (~mask) ;
|
77
|
+
|
78
|
+
wiringPiI2CWriteReg8 (node->fd, pud, old) ;
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
/*
|
83
|
+
* myDigitalWrite:
|
84
|
+
*********************************************************************************
|
85
|
+
*/
|
86
|
+
|
87
|
+
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
88
|
+
{
|
89
|
+
int bit, old ;
|
90
|
+
|
91
|
+
pin -= node->pinBase ;
|
92
|
+
bit = 1 << (pin & 7) ;
|
93
|
+
|
94
|
+
old = node->data2 ;
|
95
|
+
if (value == LOW)
|
96
|
+
old &= (~bit) ;
|
97
|
+
else
|
98
|
+
old |= bit ;
|
99
|
+
|
100
|
+
wiringPiI2CWriteReg8 (node->fd, MCP23x08_GPIO, old) ;
|
101
|
+
node->data2 = old ;
|
102
|
+
}
|
103
|
+
|
104
|
+
|
105
|
+
/*
|
106
|
+
* myDigitalRead:
|
107
|
+
*********************************************************************************
|
108
|
+
*/
|
109
|
+
|
110
|
+
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
111
|
+
{
|
112
|
+
int mask, value, gpio ;
|
113
|
+
|
114
|
+
pin -= node->pinBase ;
|
115
|
+
gpio = MCP23x08_GPIO ;
|
116
|
+
mask = 1 << pin ;
|
117
|
+
|
118
|
+
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
|
119
|
+
|
120
|
+
if ((value & mask) == 0)
|
121
|
+
return LOW ;
|
122
|
+
else
|
123
|
+
return HIGH ;
|
124
|
+
}
|
125
|
+
|
126
|
+
|
127
|
+
/*
|
128
|
+
* mcp23008Setup:
|
129
|
+
* Create a new instance of an MCP23008 I2C GPIO interface. We know it
|
130
|
+
* has 16 pins, so all we need to know here is the I2C address and the
|
131
|
+
* user-defined pin base.
|
132
|
+
*********************************************************************************
|
133
|
+
*/
|
134
|
+
|
135
|
+
int mcp23008Setup (int pinBase, int i2cAddress)
|
136
|
+
{
|
137
|
+
int fd ;
|
138
|
+
struct wiringPiNodeStruct *node ;
|
139
|
+
|
140
|
+
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
141
|
+
return fd ;
|
142
|
+
|
143
|
+
wiringPiI2CWriteReg8 (fd, MCP23x08_IOCON, IOCON_INIT) ;
|
144
|
+
|
145
|
+
node = wiringPiNewNode (pinBase, 16) ;
|
146
|
+
|
147
|
+
node->fd = fd ;
|
148
|
+
node->pinMode = myPinMode ;
|
149
|
+
node->pullUpDnControl = myPullUpDnControl ;
|
150
|
+
node->digitalRead = myDigitalRead ;
|
151
|
+
node->digitalWrite = myDigitalWrite ;
|
152
|
+
node->data2 = wiringPiI2CReadReg8 (fd, MCP23x08_OLAT) ;
|
153
|
+
|
154
|
+
return 0 ;
|
155
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
/*
|
2
|
+
* 23008.h:
|
3
|
+
* Extend wiringPi with the MCP 23008 I2C GPIO expander chip
|
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 mcp23008Setup (int pinBase, int i2cAddress) ;
|
30
|
+
|
31
|
+
#ifdef __cplusplus
|
32
|
+
}
|
33
|
+
#endif
|
@@ -0,0 +1,195 @@
|
|
1
|
+
/*
|
2
|
+
* mcp23017.c:
|
3
|
+
* Extend wiringPi with the MCP 23017 I2C GPIO expander chip
|
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
|
+
#include <stdio.h>
|
26
|
+
#include <pthread.h>
|
27
|
+
|
28
|
+
#include "wiringPi.h"
|
29
|
+
#include "wiringPiI2C.h"
|
30
|
+
#include "mcp23x0817.h"
|
31
|
+
|
32
|
+
#include "mcp23017.h"
|
33
|
+
|
34
|
+
|
35
|
+
/*
|
36
|
+
* myPinMode:
|
37
|
+
*********************************************************************************
|
38
|
+
*/
|
39
|
+
|
40
|
+
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
41
|
+
{
|
42
|
+
int mask, old, ddr ;
|
43
|
+
|
44
|
+
pin -= node->pinBase ;
|
45
|
+
|
46
|
+
if (pin < 8) // Bank A
|
47
|
+
ddr = MCP23x17_IODIRA ;
|
48
|
+
else
|
49
|
+
{
|
50
|
+
ddr = MCP23x17_IODIRB ;
|
51
|
+
pin &= 0x07 ;
|
52
|
+
}
|
53
|
+
|
54
|
+
mask = 1 << pin ;
|
55
|
+
old = wiringPiI2CReadReg8 (node->fd, ddr) ;
|
56
|
+
|
57
|
+
if (mode == OUTPUT)
|
58
|
+
old &= (~mask) ;
|
59
|
+
else
|
60
|
+
old |= mask ;
|
61
|
+
|
62
|
+
wiringPiI2CWriteReg8 (node->fd, ddr, old) ;
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
/*
|
67
|
+
* myPullUpDnControl:
|
68
|
+
*********************************************************************************
|
69
|
+
*/
|
70
|
+
|
71
|
+
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
72
|
+
{
|
73
|
+
int mask, old, pud ;
|
74
|
+
|
75
|
+
pin -= node->pinBase ;
|
76
|
+
|
77
|
+
if (pin < 8) // Bank A
|
78
|
+
pud = MCP23x17_GPPUA ;
|
79
|
+
else
|
80
|
+
{
|
81
|
+
pud = MCP23x17_GPPUB ;
|
82
|
+
pin &= 0x07 ;
|
83
|
+
}
|
84
|
+
|
85
|
+
mask = 1 << pin ;
|
86
|
+
old = wiringPiI2CReadReg8 (node->fd, pud) ;
|
87
|
+
|
88
|
+
if (mode == PUD_UP)
|
89
|
+
old |= mask ;
|
90
|
+
else
|
91
|
+
old &= (~mask) ;
|
92
|
+
|
93
|
+
wiringPiI2CWriteReg8 (node->fd, pud, old) ;
|
94
|
+
}
|
95
|
+
|
96
|
+
|
97
|
+
/*
|
98
|
+
* myDigitalWrite:
|
99
|
+
*********************************************************************************
|
100
|
+
*/
|
101
|
+
|
102
|
+
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
103
|
+
{
|
104
|
+
int bit, old ;
|
105
|
+
|
106
|
+
pin -= node->pinBase ; // Pin now 0-15
|
107
|
+
|
108
|
+
bit = 1 << (pin & 7) ;
|
109
|
+
|
110
|
+
if (pin < 8) // Bank A
|
111
|
+
{
|
112
|
+
old = node->data2 ;
|
113
|
+
|
114
|
+
if (value == LOW)
|
115
|
+
old &= (~bit) ;
|
116
|
+
else
|
117
|
+
old |= bit ;
|
118
|
+
|
119
|
+
wiringPiI2CWriteReg8 (node->fd, MCP23x17_GPIOA, old) ;
|
120
|
+
node->data2 = old ;
|
121
|
+
}
|
122
|
+
else // Bank B
|
123
|
+
{
|
124
|
+
old = node->data3 ;
|
125
|
+
|
126
|
+
if (value == LOW)
|
127
|
+
old &= (~bit) ;
|
128
|
+
else
|
129
|
+
old |= bit ;
|
130
|
+
|
131
|
+
wiringPiI2CWriteReg8 (node->fd, MCP23x17_GPIOB, old) ;
|
132
|
+
node->data3 = old ;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
|
137
|
+
/*
|
138
|
+
* myDigitalRead:
|
139
|
+
*********************************************************************************
|
140
|
+
*/
|
141
|
+
|
142
|
+
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
143
|
+
{
|
144
|
+
int mask, value, gpio ;
|
145
|
+
|
146
|
+
pin -= node->pinBase ;
|
147
|
+
|
148
|
+
if (pin < 8) // Bank A
|
149
|
+
gpio = MCP23x17_GPIOA ;
|
150
|
+
else
|
151
|
+
{
|
152
|
+
gpio = MCP23x17_GPIOB ;
|
153
|
+
pin &= 0x07 ;
|
154
|
+
}
|
155
|
+
|
156
|
+
mask = 1 << pin ;
|
157
|
+
value = wiringPiI2CReadReg8 (node->fd, gpio) ;
|
158
|
+
|
159
|
+
if ((value & mask) == 0)
|
160
|
+
return LOW ;
|
161
|
+
else
|
162
|
+
return HIGH ;
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
/*
|
167
|
+
* mcp23017Setup:
|
168
|
+
* Create a new instance of an MCP23017 I2C GPIO interface. We know it
|
169
|
+
* has 16 pins, so all we need to know here is the I2C address and the
|
170
|
+
* user-defined pin base.
|
171
|
+
*********************************************************************************
|
172
|
+
*/
|
173
|
+
|
174
|
+
int mcp23017Setup (int pinBase, int i2cAddress)
|
175
|
+
{
|
176
|
+
int fd ;
|
177
|
+
struct wiringPiNodeStruct *node ;
|
178
|
+
|
179
|
+
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
|
180
|
+
return fd ;
|
181
|
+
|
182
|
+
wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, IOCON_INIT) ;
|
183
|
+
|
184
|
+
node = wiringPiNewNode (pinBase, 16) ;
|
185
|
+
|
186
|
+
node->fd = fd ;
|
187
|
+
node->pinMode = myPinMode ;
|
188
|
+
node->pullUpDnControl = myPullUpDnControl ;
|
189
|
+
node->digitalRead = myDigitalRead ;
|
190
|
+
node->digitalWrite = myDigitalWrite ;
|
191
|
+
node->data2 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA) ;
|
192
|
+
node->data3 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATB) ;
|
193
|
+
|
194
|
+
return 0 ;
|
195
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
/*
|
2
|
+
* 23017.h:
|
3
|
+
* Extend wiringPi with the MCP 23017 I2C GPIO expander chip
|
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 mcp23017Setup (int pinBase, int i2cAddress) ;
|
30
|
+
|
31
|
+
#ifdef __cplusplus
|
32
|
+
}
|
33
|
+
#endif
|
@@ -0,0 +1,195 @@
|
|
1
|
+
/*
|
2
|
+
* mcp23s08.c:
|
3
|
+
* Extend wiringPi with the MCP 23s08 SPI GPIO expander chip
|
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
|
+
#include <stdio.h>
|
26
|
+
#include <stdint.h>
|
27
|
+
|
28
|
+
#include "wiringPi.h"
|
29
|
+
#include "wiringPiSPI.h"
|
30
|
+
#include "mcp23x0817.h"
|
31
|
+
|
32
|
+
#include "mcp23s08.h"
|
33
|
+
|
34
|
+
#define MCP_SPEED 4000000
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
/*
|
39
|
+
* writeByte:
|
40
|
+
* Write a byte to a register on the MCP23s08 on the SPI bus.
|
41
|
+
*********************************************************************************
|
42
|
+
*/
|
43
|
+
|
44
|
+
static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data)
|
45
|
+
{
|
46
|
+
uint8_t spiData [4] ;
|
47
|
+
|
48
|
+
spiData [0] = CMD_WRITE | ((devId & 7) << 1) ;
|
49
|
+
spiData [1] = reg ;
|
50
|
+
spiData [2] = data ;
|
51
|
+
|
52
|
+
wiringPiSPIDataRW (spiPort, spiData, 3) ;
|
53
|
+
}
|
54
|
+
|
55
|
+
/*
|
56
|
+
* readByte:
|
57
|
+
* Read a byte from a register on the MCP23s08 on the SPI bus.
|
58
|
+
*********************************************************************************
|
59
|
+
*/
|
60
|
+
|
61
|
+
static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
|
62
|
+
{
|
63
|
+
uint8_t spiData [4] ;
|
64
|
+
|
65
|
+
spiData [0] = CMD_READ | ((devId & 7) << 1) ;
|
66
|
+
spiData [1] = reg ;
|
67
|
+
|
68
|
+
wiringPiSPIDataRW (spiPort, spiData, 3) ;
|
69
|
+
|
70
|
+
return spiData [2] ;
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
/*
|
75
|
+
* myPinMode:
|
76
|
+
*********************************************************************************
|
77
|
+
*/
|
78
|
+
|
79
|
+
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
|
80
|
+
{
|
81
|
+
int mask, old, ddr ;
|
82
|
+
|
83
|
+
pin -= node->pinBase ;
|
84
|
+
ddr = MCP23x08_IODIR ;
|
85
|
+
mask = 1 << pin ;
|
86
|
+
old = readByte (node->data0, node->data1, ddr) ;
|
87
|
+
|
88
|
+
if (mode == OUTPUT)
|
89
|
+
old &= (~mask) ;
|
90
|
+
else
|
91
|
+
old |= mask ;
|
92
|
+
|
93
|
+
writeByte (node->data0, node->data1, ddr, old) ;
|
94
|
+
}
|
95
|
+
|
96
|
+
|
97
|
+
/*
|
98
|
+
* myPullUpDnControl:
|
99
|
+
*********************************************************************************
|
100
|
+
*/
|
101
|
+
|
102
|
+
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
|
103
|
+
{
|
104
|
+
int mask, old, pud ;
|
105
|
+
|
106
|
+
pin -= node->pinBase ;
|
107
|
+
pud = MCP23x08_GPPU ;
|
108
|
+
mask = 1 << pin ;
|
109
|
+
|
110
|
+
old = readByte (node->data0, node->data1, pud) ;
|
111
|
+
|
112
|
+
if (mode == PUD_UP)
|
113
|
+
old |= mask ;
|
114
|
+
else
|
115
|
+
old &= (~mask) ;
|
116
|
+
|
117
|
+
writeByte (node->data0, node->data1, pud, old) ;
|
118
|
+
}
|
119
|
+
|
120
|
+
|
121
|
+
/*
|
122
|
+
* myDigitalWrite:
|
123
|
+
*********************************************************************************
|
124
|
+
*/
|
125
|
+
|
126
|
+
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
127
|
+
{
|
128
|
+
int bit, old ;
|
129
|
+
|
130
|
+
pin -= node->pinBase ;
|
131
|
+
bit = 1 << pin ;
|
132
|
+
|
133
|
+
old = node->data2 ;
|
134
|
+
if (value == LOW)
|
135
|
+
old &= (~bit) ;
|
136
|
+
else
|
137
|
+
old |= bit ;
|
138
|
+
|
139
|
+
writeByte (node->data0, node->data1, MCP23x08_GPIO, old) ;
|
140
|
+
node->data2 = old ;
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
/*
|
145
|
+
* myDigitalRead:
|
146
|
+
*********************************************************************************
|
147
|
+
*/
|
148
|
+
|
149
|
+
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
|
150
|
+
{
|
151
|
+
int mask, value, gpio ;
|
152
|
+
|
153
|
+
pin -= node->pinBase ;
|
154
|
+
gpio = MCP23x08_GPIO ;
|
155
|
+
mask = 1 << pin ;
|
156
|
+
|
157
|
+
value = readByte (node->data0, node->data1, gpio) ;
|
158
|
+
|
159
|
+
if ((value & mask) == 0)
|
160
|
+
return LOW ;
|
161
|
+
else
|
162
|
+
return HIGH ;
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
/*
|
167
|
+
* mcp23s08Setup:
|
168
|
+
* Create a new instance of an MCP23s08 SPI GPIO interface. We know it
|
169
|
+
* has 16 pins, so all we need to know here is the SPI address and the
|
170
|
+
* user-defined pin base.
|
171
|
+
*********************************************************************************
|
172
|
+
*/
|
173
|
+
|
174
|
+
int mcp23s08Setup (int pinBase, int spiPort, int devId)
|
175
|
+
{
|
176
|
+
int x ;
|
177
|
+
struct wiringPiNodeStruct *node ;
|
178
|
+
|
179
|
+
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
|
180
|
+
return x ;
|
181
|
+
|
182
|
+
writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
|
183
|
+
|
184
|
+
node = wiringPiNewNode (pinBase, 16) ;
|
185
|
+
|
186
|
+
node->data0 = spiPort ;
|
187
|
+
node->data1 = devId ;
|
188
|
+
node->pinMode = myPinMode ;
|
189
|
+
node->pullUpDnControl = myPullUpDnControl ;
|
190
|
+
node->digitalRead = myDigitalRead ;
|
191
|
+
node->digitalWrite = myDigitalWrite ;
|
192
|
+
node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ;
|
193
|
+
|
194
|
+
return 0 ;
|
195
|
+
}
|