forked from adafruit/Adafruit-MCP23017-Arduino-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_MCP23017.cpp
371 lines (324 loc) · 10.4 KB
/
Adafruit_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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*!
* @file Adafruit_MCP23017.cpp
*
* @mainpage Adafruit MCP23017 Library
*
* @section intro_sec Introduction
*
* This is a library for the MCP23017 i2c port expander
*
* These displays use I2C to communicate, 2 pins are required to
* interface
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section author Author
*
* Written by Limor Fried/Ladyada for Adafruit Industries.
*
* @section license License
*
* BSD license, all text above must be included in any redistribution
*/
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include "Adafruit_MCP23017.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// minihelper to keep Arduino backward compatibility
static inline void wiresend(uint8_t x, TwoWire *theWire) {
#if ARDUINO >= 100
theWire->write((uint8_t)x);
#else
theWire->send(x);
#endif
}
static inline uint8_t wirerecv(TwoWire *theWire) {
#if ARDUINO >= 100
return theWire->read();
#else
return theWire->receive();
#endif
}
/**
* Bit number associated to a give Pin
*/
uint8_t Adafruit_MCP23017::bitForPin(uint8_t pin) { return pin % 8; }
/**
* Register address, port dependent, for a given PIN
*/
uint8_t Adafruit_MCP23017::regForPin(uint8_t pin, uint8_t portAaddr,
uint8_t portBaddr) {
return (pin < 8) ? portAaddr : portBaddr;
}
/**
* Reads a given register
*/
uint8_t Adafruit_MCP23017::readRegister(uint8_t addr) {
// read the current GPINTEN
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(addr, _wire);
_wire->endTransmission();
_wire->requestFrom(MCP23017_ADDRESS | i2caddr, 1);
return wirerecv(_wire);
}
/**
* Writes a given register
*/
void Adafruit_MCP23017::writeRegister(uint8_t regAddr, uint8_t regValue) {
// Write the register
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(regAddr, _wire);
wiresend(regValue, _wire);
_wire->endTransmission();
}
/**
* Helper to update a single bit of an A/B register.
* - Reads the current register value
* - Writes the new register value
*/
void Adafruit_MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue,
uint8_t portAaddr,
uint8_t portBaddr) {
uint8_t regValue;
uint8_t regAddr = regForPin(pin, portAaddr, portBaddr);
uint8_t bit = bitForPin(pin);
regValue = readRegister(regAddr);
// set the value for the particular bit
bitWrite(regValue, bit, pValue);
writeRegister(regAddr, regValue);
}
////////////////////////////////////////////////////////////////////////////////
/*!
* Initializes the MCP23017 given its HW selected address, see datasheet for
* Address selection.
* @param addr Selected address
* @param theWire the I2C object to use, defaults to &Wire
*/
void Adafruit_MCP23017::begin(uint8_t addr, TwoWire *theWire) {
if (addr > 7) {
addr = 7;
}
i2caddr = addr;
_wire = theWire;
_wire->begin();
// set defaults!
// all inputs on port A and B
writeRegister(MCP23017_IODIRA, 0xff);
writeRegister(MCP23017_IODIRB, 0xff);
}
/**
* Initializes the default MCP23017, with 000 for the configurable part of the
* address
* @param theWire the I2C object to use, defaults to &Wire
*/
void Adafruit_MCP23017::begin(TwoWire *theWire) { begin(0, theWire); }
/**
* Changes the adress to which MCP23017 is talked to.
* Initializing all ports as input is optional.
* This enables the option to control multiple MCP23017 with one instance.
* Both adress syntax (i2x) = 0x20 or only the configurable part is supported.
* @param addr Selected address
* @param initAsInput If true initializing all ports as input, otherwise they
* stay as they are
*/
void Adafruit_MCP23017::changeAddress(uint8_t addr, bool initAsInput) {
// allow using both syntaxes, complete i2x address or address pin setting
if ((addr >= MCP23017_ADDRESS) && (addr < (MCP23017_ADDRESS + 8))) {
addr -= MCP23017_ADDRESS;
}
if (addr > 7) {
addr = 7;
}
i2caddr = addr;
if (initAsInput) {
// set defaults only if wanted.
// all inputs on port A and B
writeRegister(MCP23017_IODIRA, 0xff);
writeRegister(MCP23017_IODIRB, 0xff);
}
}
/**
* Changes the adress to which MCP23017 is talked to.
* This enables the option to control multiple MCP23017 with one instance.
* Both adress syntax (i2x) = 0x20 or only the configurable part is supported.
* @param addr Selected address
*/
void Adafruit_MCP23017::changeAddress(uint8_t addr) {
changeAddress(addr, false);
}
/**
* Sets the pin mode to either INPUT or OUTPUT
* @param p Pin to set
* @param d Mode to set the pin
*/
void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) {
updateRegisterBit(p, (d == INPUT), MCP23017_IODIRA, MCP23017_IODIRB);
}
/**
* Reads all 16 pins (port A and B) into a single 16 bits variable.
* @return Returns the 16 bit variable representing all 16 pins
*/
uint16_t Adafruit_MCP23017::readGPIOAB() {
uint16_t ba = 0;
uint8_t a;
// read the current GPIO output latches
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_GPIOA, _wire);
_wire->endTransmission();
_wire->requestFrom(MCP23017_ADDRESS | i2caddr, 2);
a = wirerecv(_wire);
ba = wirerecv(_wire);
ba <<= 8;
ba |= a;
return ba;
}
/**
* Read a single port, A or B, and return its current 8 bit value.
* @param b Decided what gpio to use. Should be 0 for GPIOA, and 1 for GPIOB.
* @return Returns the b bit value of the port
*/
uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) {
// read the current GPIO output latches
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
if (b == 0)
wiresend(MCP23017_GPIOA, _wire);
else {
wiresend(MCP23017_GPIOB, _wire);
}
_wire->endTransmission();
_wire->requestFrom(MCP23017_ADDRESS | i2caddr, 1);
return wirerecv(_wire);
}
/**
* Writes all the pins in one go. This method is very useful if you are
* implementing a multiplexed matrix and want to get a decent refresh rate.
*/
void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) {
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_GPIOA, _wire);
wiresend(ba & 0xFF, _wire);
wiresend(ba >> 8, _wire);
_wire->endTransmission();
}
/*!
* @brief Writes to a pin on the MCP23017
* @param pin Pin to write to
* @param d What to write to the pin
*/
void Adafruit_MCP23017::digitalWrite(uint8_t pin, uint8_t d) {
uint8_t gpio;
uint8_t bit = bitForPin(pin);
// read the current GPIO output latches
uint8_t regAddr = regForPin(pin, MCP23017_OLATA, MCP23017_OLATB);
gpio = readRegister(regAddr);
// set the pin and direction
bitWrite(gpio, bit, d);
// write the new GPIO
regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB);
writeRegister(regAddr, gpio);
}
/*!
* @brief Enables the pull-up resistor on the specified pin
* @param p Pin to set
* @param d Value to set the pin
*/
void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d) {
updateRegisterBit(p, d, MCP23017_GPPUA, MCP23017_GPPUB);
}
/*!
* @brief Reads the specified pin
* @param pin Pin to read
* @return Value of the pin
*/
uint8_t Adafruit_MCP23017::digitalRead(uint8_t pin) {
uint8_t bit = bitForPin(pin);
uint8_t regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB);
return (readRegister(regAddr) >> bit) & 0x1;
}
/**
* Configures the interrupt system. both port A and B are assigned the same
* configuration.
* @param mirroring Mirroring will OR both INTA and INTB pins.
* @param openDrain Opendrain will set the INT pin to value or open drain.
* @param polarity polarity will set LOW or HIGH on interrupt.
* Default values after Power On Reset are: (false, false, LOW)
* If you are connecting the INTA/B pin to arduino 2/3, you should configure the
* interupt handling as FALLING with the default configuration.
*/
void Adafruit_MCP23017::setupInterrupts(uint8_t mirroring, uint8_t openDrain,
uint8_t polarity) {
// configure the port A
uint8_t ioconfValue = readRegister(MCP23017_IOCONA);
bitWrite(ioconfValue, 6, mirroring);
bitWrite(ioconfValue, 2, openDrain);
bitWrite(ioconfValue, 1, polarity);
writeRegister(MCP23017_IOCONA, ioconfValue);
// Configure the port B
ioconfValue = readRegister(MCP23017_IOCONB);
bitWrite(ioconfValue, 6, mirroring);
bitWrite(ioconfValue, 2, openDrain);
bitWrite(ioconfValue, 1, polarity);
writeRegister(MCP23017_IOCONB, ioconfValue);
}
/**
* Set's up a pin for interrupt. uses arduino MODEs: CHANGE, FALLING, RISING.
*
* Note that the interrupt condition finishes when you read the information
* about the port / value that caused the interrupt or you read the port itself.
* Check the datasheet can be confusing.
* @param pin Pin to set
* @param mode Mode to set the pin
*
*/
void Adafruit_MCP23017::setupInterruptPin(uint8_t pin, uint8_t mode) {
// set the pin interrupt control (0 means change, 1 means compare against
// given value);
updateRegisterBit(pin, (mode != CHANGE), MCP23017_INTCONA, MCP23017_INTCONB);
// if the mode is not CHANGE, we need to set up a default value, different
// value triggers interrupt
// In a RISING interrupt the default value is 0, interrupt is triggered when
// the pin goes to 1. In a FALLING interrupt the default value is 1, interrupt
// is triggered when pin goes to 0.
updateRegisterBit(pin, (mode == FALLING), MCP23017_DEFVALA, MCP23017_DEFVALB);
// enable the pin for interrupt
updateRegisterBit(pin, HIGH, MCP23017_GPINTENA, MCP23017_GPINTENB);
}
/*!
* @brief Gets the last interrupt pin
* @return Returns the last interrupt pin
*/
uint8_t Adafruit_MCP23017::getLastInterruptPin() {
uint8_t intf;
// try port A
intf = readRegister(MCP23017_INTFA);
for (int i = 0; i < 8; i++)
if (bitRead(intf, i))
return i;
// try port B
intf = readRegister(MCP23017_INTFB);
for (int i = 0; i < 8; i++)
if (bitRead(intf, i))
return i + 8;
return MCP23017_INT_ERR;
}
/*!
* @brief Gets the value of the last interrupt pin
* @return Returns the value of the last interrupt pin
*/
uint8_t Adafruit_MCP23017::getLastInterruptPinValue() {
uint8_t intPin = getLastInterruptPin();
if (intPin != MCP23017_INT_ERR) {
uint8_t intcapreg = regForPin(intPin, MCP23017_INTCAPA, MCP23017_INTCAPB);
uint8_t bit = bitForPin(intPin);
return (readRegister(intcapreg) >> bit) & (0x01);
}
return MCP23017_INT_ERR;
}