-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MCP23008.cpp
510 lines (442 loc) · 9.29 KB
/
MCP23008.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//
// FILE: MCP23008.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.4
// PURPOSE: Arduino library for I2C MCP23008 8 channel port expander
// DATE: 2019-10-12
// URL: https://github.com/RobTillaart/MCP23008
#include "MCP23008.h"
MCP23008::MCP23008(uint8_t address, TwoWire *wire)
: _address{address}, _wire{wire}
{}
bool MCP23008::begin(bool pullup)
{
// check connected
if (! isConnected()) return false;
// disable address increment (datasheet P20
// SEQOP: Sequential Operation mode bit
// 1 = Sequential operation disabled, address pointer does not increment.
// 0 = Sequential operation enabled, address pointer increments.
// if (! writeReg(MCP23x08_IOCR, MCP23008_IOCR_SEQOP)) return false;
if (pullup)
{
// Force INPUT_PULLUP
if (! writeReg(MCP23x08_PUR_A, 0xFF)) return false; // 0xFF == all UP
}
return true;
}
bool MCP23008::isConnected()
{
_wire->beginTransmission(_address);
if (_wire->endTransmission() != 0)
{
_error = MCP23008_I2C_ERROR;
return false;
}
_error = MCP23008_OK;
return true;
}
///////////////////////////////////////////////////////////////////
//
// single pin interface
//
// pin = 0..7
// mode = INPUT, OUTPUT, INPUT_PULLUP (= same as INPUT)
// do NOT use 0 or 1 for mode
bool MCP23008::pinMode1(uint8_t pin, uint8_t mode)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
if ((mode != INPUT) && (mode != INPUT_PULLUP) && (mode != OUTPUT))
{
_error = MCP23008_VALUE_ERROR;
return false;
}
uint8_t dataDirectionRegister = MCP23x08_DDR_A;
uint8_t val = readReg(dataDirectionRegister);
if (_error != MCP23008_OK)
{
return false;
}
uint8_t mask = 1 << pin;
// only work with valid
if ((mode == INPUT) || (mode == INPUT_PULLUP))
{
val |= mask;
}
else if (mode == OUTPUT)
{
val &= ~mask;
}
// other values won't change val ....
writeReg(dataDirectionRegister, val);
if (_error != MCP23008_OK)
{
return false;
}
return true;
}
// pin = 0..7
// value = LOW, HIGH
bool MCP23008::write1(uint8_t pin, uint8_t value)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
uint8_t IOR = MCP23x08_GPIO_A;
uint8_t val = readReg(IOR);
uint8_t pre = val;
if (_error != MCP23008_OK)
{
return false;
}
uint8_t mask = 1 << pin;
if (value)
{
val |= mask;
}
else
{
val &= ~mask;
}
// only write when changed.
if (pre != val)
{
writeReg(IOR, val);
if (_error != MCP23008_OK)
{
return false;
}
}
return true;
}
uint8_t MCP23008::read1(uint8_t pin)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return MCP23008_INVALID_READ;
}
uint8_t IOR = MCP23x08_GPIO_A;
uint8_t val = readReg(IOR);
if (_error != MCP23008_OK)
{
return MCP23008_INVALID_READ;
}
uint8_t mask = 1 << pin;
if (val & mask) return HIGH;
return LOW;
}
// pin = 0..7
// reversed = true or false
bool MCP23008::setPolarity(uint8_t pin, bool reversed)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
uint8_t inputPolarityRegister = MCP23x08_POL_A;
uint8_t val = readReg(inputPolarityRegister);
if (_error != MCP23008_OK)
{
return false;
}
uint8_t mask = 1 << pin;
if (reversed)
{
val |= mask;
}
else
{
val &= ~mask;
}
writeReg(inputPolarityRegister, val);
if (_error != MCP23008_OK)
{
return false;
}
return true;
}
bool MCP23008::getPolarity(uint8_t pin, bool &reversed)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
uint8_t inputPolarityRegister = MCP23x08_POL_A;
uint8_t val = readReg(inputPolarityRegister);
if (_error != MCP23008_OK)
{
return false;
}
uint8_t mask = 1 << pin;
reversed = (val & mask) > 0;
return true;
}
// pin = 0..7
// pullup = true or false
bool MCP23008::setPullup(uint8_t pin, bool pullup)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
uint8_t inputPullupRegister = MCP23x08_PUR_A;
uint8_t val = readReg(inputPullupRegister);
if (_error != MCP23008_OK)
{
return false;
}
uint8_t mask = 1 << pin;
if (pullup)
{
val |= mask;
}
else
{
val &= ~mask;
}
writeReg(inputPullupRegister, val);
if (_error != MCP23008_OK)
{
return false;
}
return true;
}
bool MCP23008::getPullup(uint8_t pin, bool &pullup)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
uint8_t inputPullupRegister = MCP23x08_PUR_A;
uint8_t val = readReg(inputPullupRegister);
if (_error != MCP23008_OK)
{
return false;
}
uint8_t mask = 1 << pin;
pullup = (val & mask) > 0;
return true;
}
///////////////////////////////////////////////////////////////////
//
// 8 pins interface
//
// whole register at once
// mask = 0x00..0xFF bit pattern
// bit 0 = output mode, bit 1 = input mode
bool MCP23008::pinMode8(uint8_t mask)
{
writeReg(MCP23x08_DDR_A, mask);
_error = MCP23008_OK;
return true;
}
bool MCP23008::write8(uint8_t value)
{
writeReg(MCP23x08_GPIO_A, value);
_error = MCP23008_OK;
return true;
}
int MCP23008::read8()
{
_error = MCP23008_OK;
return readReg(MCP23x08_GPIO_A);
}
// mask = 0..0xFF bit pattern
bool MCP23008::setPolarity8(uint8_t mask)
{
writeReg(MCP23x08_POL_A, mask);
if (_error != MCP23008_OK)
{
return false;
}
return true;
}
bool MCP23008::getPolarity8(uint8_t &mask)
{
mask = readReg(MCP23x08_POL_A);
if (_error != MCP23008_OK)
{
return false;
}
return true;
}
// mask = 0..0xFF bit pattern
bool MCP23008::setPullup8(uint8_t mask)
{
writeReg(MCP23x08_PUR_A, mask);
if (_error != MCP23008_OK)
{
return false;
}
return true;
}
bool MCP23008::getPullup8(uint8_t &mask)
{
mask = readReg(MCP23x08_PUR_A);
if (_error != MCP23008_OK)
{
return false;
}
return true;
}
///////////////////////////////////////////////////
//
// INTERRUPTS (experimental, see MCP23S17 - #40)
//
// TODO, catch writeReg errors
// TODO, MCP23x08_INT_MODE_ERROR?
// TODO, if register not changed no need to update?
// TODO, 8 bits optimize? more code vs speed?
//
// pin = 0..7, mode = { RISING, FALLING, CHANGE }
bool MCP23008::enableInterrupt(uint8_t pin, uint8_t mode)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
// right mode
uint8_t intcon = readReg(MCP23x08_INTCON_A);
if (mode == CHANGE)
{
// compare to previous value.
intcon &= ~(1 << pin);
}
else
{
uint8_t defval = readReg(MCP23x08_DEFVAL_A);
if (mode == RISING)
{
intcon |= (1 << pin);
defval &= ~(1 << pin); // RISING == compare to 0
}
else if (mode == FALLING)
{
intcon |= (1 << pin);
defval |= ~(1 << pin); // FALLING == compare to 1
}
writeReg(MCP23x08_DEFVAL_A, defval);
}
writeReg(MCP23x08_INTCON_A, intcon);
// enable interrupt
uint16_t value = readReg(MCP23x08_GPINTEN_A);
value |= (1 << pin);
return writeReg(MCP23x08_GPINTEN_A, value);
}
bool MCP23008::disableInterrupt(uint8_t pin)
{
if (pin > 7)
{
_error = MCP23008_PIN_ERROR;
return false;
}
// disable interrupt
uint16_t value = readReg(MCP23x08_GPINTEN_A);
value &= ~(1 << pin);
return writeReg(MCP23x08_GPINTEN_A, value);
}
// which pins caused the INT?
uint8_t MCP23008::getInterruptFlagRegister()
{
return readReg(MCP23x08_INTF_A);
}
uint8_t MCP23008::getInterruptCaptureRegister()
{
return readReg(MCP23x08_INTCAP_A);
}
// polarity: 0 = LOW, 1 = HIGH, 2 = NONE/ODR
bool MCP23008::setInterruptPolarity(uint8_t polarity)
{
if (polarity > 2) return false;
uint8_t reg = readReg(MCP23x08_IOCR);
reg &= ~(MCP23x08_IOCR_ODR | MCP23x08_IOCR_INTPOL);
// LOW is default set.
if (polarity == 2) reg |= MCP23x08_IOCR_ODR;
if (polarity == 1) reg |= MCP23x08_IOCR_INTPOL;
return writeReg(MCP23x08_IOCR, reg);
}
uint8_t MCP23008::getInterruptPolarity()
{
uint8_t reg = readReg(MCP23x08_IOCR);
if (reg & MCP23x08_IOCR_ODR) return 2;
if (reg & MCP23x08_IOCR_INTPOL) return 1;
return 0;
}
/////////////////////////////////////////////
//
// MISC
//
int MCP23008::lastError()
{
int e = _error;
_error = MCP23008_OK; // reset error after read.
return e;
}
bool MCP23008::enableControlRegister(uint8_t mask)
{
uint8_t reg = readReg(MCP23x08_IOCR);
reg |= mask;
return writeReg(MCP23x08_IOCR, reg);
}
bool MCP23008::disableControlRegister(uint8_t mask)
{
uint8_t reg = readReg(MCP23x08_IOCR);
reg &= ~mask;
return writeReg(MCP23x08_IOCR, reg);
}
////////////////////////////////////////////////////
//
// DEBUG
//
uint8_t MCP23008::getPinMode8()
{
return readReg(0);
}
////////////////////////////////////////////////////
//
// PROTECTED
//
bool MCP23008::writeReg(uint8_t reg, uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
if (_wire->endTransmission() != 0)
{
_error = MCP23008_I2C_ERROR;
return false;
}
_error = MCP23008_OK;
return true;
}
uint8_t MCP23008::readReg(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(reg);
if (_wire->endTransmission() != 0)
{
_error = MCP23008_I2C_ERROR;
return 0;
}
uint8_t n = _wire->requestFrom(_address, (uint8_t)1);
if (n != 1)
{
_error = MCP23008_I2C_ERROR;
return 0;
}
_error = MCP23008_OK;
return _wire->read();
}
// -- END OF FILE --