forked from saul-rodriguez/Qtraspberrylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp23017.cpp
335 lines (268 loc) · 7.08 KB
/
mcp23017.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "mcp23017.h"
#include <wiringPiI2C.h>
#include <wiringPi.h>
//Other definitions
//#include "hardware_conf.h"
Mcp23017::Mcp23017(QObject *parent) :
QObject(parent)
{
porta = 0;
portb = 0;
}
/*
* Function: open
*-----------------
* Open i2c connection with a MC23017 chip
*
* _addr: i2c address of the MC23017
*
* returns: 0 if the device was opened correctly
* -1 if the device was not opened correctly
*/
int Mcp23017::open(quint8 _addr)
{
fd = wiringPiI2CSetup(_addr);
qDebug("fd = %d",fd);
if (fd == -1) {
qDebug("Could not create a i2c handle fd= %d",fd);
return -1;
}
//Configure IOCON, disable sequential operation
int ret = wiringPiI2CWriteReg8(fd,MCP23017_IOCON,0x20);
qDebug("ret = %d",ret);
porta = 0;
portb = 0;
return 0;
}
/*
* Function: setTris
* ------------------
* Configures the 8 pins of port A/B as inputs/outputs.
*
* port: Either PORTA or PORTB
* tris: 8-bit mask [bit7,bit6,5bit,bit4,bit3,bit2,bit1,bit0].
* Inputs are "1", Outputs are "0"
*
* return: 0 if successful,
* -1 if the i2c failed,
* -2 if the port argument was wrong
*/
int Mcp23017::setTris(quint8 port, quint8 tris)
{
int ret;
if (port == PORTA) {
//Configure I/O portA
ret = wiringPiI2CWriteReg8(fd,MCP23017_IODIRA,tris);
qDebug("ret = %d",ret);
return ret;
} else if (port == PORTB) {
//Configure I/O portB
ret = wiringPiI2CWriteReg8(fd,MCP23017_IODIRB,tris);
qDebug("ret= %d",ret);
return ret;
}
return -2;
}
/*
* Function writePort
* ------------------
* Write 8 bits to port A or B
*
* port: Either PORTA or PORTB
* data: 8 bit data
*
* returns: 0 if the write was successful
* -1 if the i2c comm. failed
* -2 if the port argument was wrong
*/
int Mcp23017::writePort(quint8 port, quint8 data)
{
int ret;
if (port == PORTA) {
//Write to port A
porta = data;
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPIOA,data);
qDebug("ret = %d",ret);
return ret;
} else if (port == PORTB) {
//Write to port B
portb = data;
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPIOB,data);
qDebug("ret = %d",ret);
return ret;
}
return -2;
}
/*
* Function: writePin
* -------------------
* Writes to an individual pin in port A or B
*
* port: Either PORTA or PORTB
* pin: integer number from 0-7. It selects one of the pins
* of the port (port0 - port7)
* data: true or false (1 or 0)
*
* returns: 0 if the write was successful
* -1 if the i2c comm. was unsuccesful
* -2 if the port argument was wrong
*/
int Mcp23017::writePin(quint8 port, quint8 pin, quint8 data)
{
int ret;
quint8 aux;
if (port == PORTA) {
if (data == true) {
porta |= (1 << pin);
} else {
aux = ~(1 << pin);
porta &= aux;
}
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPIOA,porta);
return ret;
} else if (port == PORTB) {
if (data == true) {
portb |= (1 << pin);
} else {
aux = ~(1 << pin);
portb &= aux;
}
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPIOB,portb);
return ret;
}
return -2;
}
/*
* Function: setPullup
* -------------------
* Configures the 100k pull-up resistors for pins that are defined as inputs
*
* port: Either PORTA or PORTB
* pullup: 8-bit mask number [bit7,...,bit0]. The pull-up resistor is enabled
* when bitx = 1, and disabled when bitx = 0.
*
* returns: 0 if the configuration was successful
* -1 if the i2c comm. was unsuccesful
* -2 if the port argument is wrong
*
*/
int Mcp23017::setPullup(quint8 port, quint8 pullup)
{
int ret;
ret = -2;
if (port == PORTA) {
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPPUA,pullup);
} else if (port == PORTB) {
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPPUB,pullup);
}
return ret;
}
/*
* Function: readPort
* ------------------
* Reads the value of the 8 pins in a port.
*
* port: Either PORTA or PORTB
*
* returns: if successful, it returns the value of pins
* in the port [pin7,...,pin0] 0x00 to 0xff.
* -2 if the i2c comm. was unsuccessful
*
*/
int Mcp23017::readPort(quint8 port)
{
int ret;
ret = -2;
if (port == PORTA) {
ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOA);
porta = ret;
} else if (port == PORTB) {
ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOB);
portb = ret;
}
return ret;
}
/*
* Function: readPin
* ------------------
* Reads the digital value of a pin in a port
*
* port: Either PORTA or PORTB
* pin: number from 0-7 which represents the address of
* the pin the port [pin7,....,pin0]
*
* returns 1 or 0 if the read was sucessful
* -1 if the i2c comm. was unsuccessful
*/
int Mcp23017::readPin(quint8 port, quint8 pin)
{
int ret;
if (port == PORTA) {
ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOA);
if (ret & (1 << pin)) {
return 1;
} else {
return 0;
}
} else if (port == PORTB) {
ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOB);
if (ret & (1 << pin)) {
return 1;
} else {
return 0;
}
}
return -1;
}
/*
* Function: setISR
* -----------------
* Configure input pins that will produce interrupt-on-change. Interrupts pins INTA
* and INTB are separated. The output interrupt pins have active output drivers and are
* configured Active-low.
*
* port: Either PORTA or PORTB
* intmask: 8 bit mask number [bit7,...,bit0]. Interrupt enabled if bitx = 1.
* Interrupt disabled if bitx = 0.
*
* return: 0 if the last write was successful
* -1 if the i2c comm in the last write failed
* -2 if the port argument was wrong
*/
int Mcp23017::setISR(quint8 port, quint8 intmask)
{
int ret;
ret = -2;
if (port == PORTA) {
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPINTENA,intmask);
ret = wiringPiI2CWriteReg8(fd,MCP23017_INTCONA,0x00); //pin compared against previous value
} else if (port == PORTB){
ret = wiringPiI2CWriteReg8(fd,MCP23017_GPINTENB,intmask);
ret = wiringPiI2CWriteReg8(fd,MCP23017_INTCONB,0x00); //pin compared against previous value
}
return ret;
}
/*
* Virtual function: ISR
* This function is triggered from any interrupt service routine declared
* in mcp23017_isr.h. It is necessary that this function reads the gpiox or
* the intcapx register in order to re-enable the interrupt!
* The purpose of this virtual function is to glue any particular logic in a derived
* class.
*/
int Mcp23017::ISRA()
{
int ret;
ret = wiringPiI2CReadReg8(fd, MCP23017_INTCAPA);
qDebug("intcapa value = %X",ret);
emit interrupt_A(ret);
return ret;
}
int Mcp23017::ISRB()
{
int ret;
ret = wiringPiI2CReadReg8(fd, MCP23017_INTCAPB);
qDebug("intcapb value = %X",ret);
emit interrupt_B(ret);
return ret;
}