-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCP23S17.cpp
547 lines (507 loc) · 14.5 KB
/
MCP23S17.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Copyright (c) 2014, Majenko Technologies
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of Majenko Technologies nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <MCP23S17.h>
/*! The constructor takes three parameters. The first is an SPI class
* pointer. This is the address of an SPI object (either the default
* SPI object on the Arduino, or an object made using the DSPIx classes
* on the chipKIT). The second parameter is the chip select pin number
* to use when communicating with the chip. The third is the internal
* address number of the chip. This is controlled by the three Ax pins
* on the chip.
*
* Example:
*
*
* MCP23S17 myExpander(&SPI, 10, 0);
*
*/
#ifdef __PIC32MX__
MCP23S17::MCP23S17(DSPI *spi, uint8_t cs, uint8_t addr) {
#else
MCP23S17::MCP23S17(SPIClass *spi, uint8_t cs, uint8_t addr) {
#endif
_spi = spi;
_cs = cs;
_addr = addr;
_reg[IODIRA] = 0xFF;
_reg[IODIRB] = 0xFF;
_reg[IPOLA] = 0x00;
_reg[IPOLB] = 0x00;
_reg[GPINTENA] = 0x00;
_reg[GPINTENB] = 0x00;
_reg[DEFVALA] = 0x00;
_reg[DEFVALB] = 0x00;
_reg[INTCONA] = 0x00;
_reg[INTCONB] = 0x00;
_reg[IOCONA] = 0x18;
_reg[IOCONB] = 0x18;
_reg[GPPUA] = 0x00;
_reg[GPPUB] = 0x00;
_reg[INTFA] = 0x00;
_reg[INTFB] = 0x00;
_reg[INTCAPA] = 0x00;
_reg[INTCAPB] = 0x00;
_reg[GPIOA] = 0x00;
_reg[GPIOB] = 0x00;
_reg[OLATA] = 0x00;
_reg[OLATB] = 0x00;
}
#ifdef __PIC32MX__
MCP23S17::MCP23S17(DSPI &spi, uint8_t cs, uint8_t addr) {
#else
MCP23S17::MCP23S17(SPIClass &spi, uint8_t cs, uint8_t addr) {
#endif
_spi = &spi;
_cs = cs;
_addr = addr;
_reg[IODIRA] = 0xFF;
_reg[IODIRB] = 0xFF;
_reg[IPOLA] = 0x00;
_reg[IPOLB] = 0x00;
_reg[GPINTENA] = 0x00;
_reg[GPINTENB] = 0x00;
_reg[DEFVALA] = 0x00;
_reg[DEFVALB] = 0x00;
_reg[INTCONA] = 0x00;
_reg[INTCONB] = 0x00;
_reg[IOCONA] = 0x18;
_reg[IOCONB] = 0x18;
_reg[GPPUA] = 0x00;
_reg[GPPUB] = 0x00;
_reg[INTFA] = 0x00;
_reg[INTFB] = 0x00;
_reg[INTCAPA] = 0x00;
_reg[INTCAPB] = 0x00;
_reg[GPIOA] = 0x00;
_reg[GPIOB] = 0x00;
_reg[OLATA] = 0x00;
_reg[OLATB] = 0x00;
}
/*! The begin function performs the initial configuration of the IO expander chip.
* Not only does it set up the SPI communications, but it also configures the chip
* for address-based communication and sets the default parameters and registers
* to sensible values.
*
* Example:
*
* myExpander.begin();
*
*/
void MCP23S17::begin() {
_spi->begin();
::pinMode(_cs, OUTPUT);
::digitalWrite(_cs, HIGH);
uint8_t cmd = 0b01000000;
::digitalWrite(_cs, LOW);
_spi->transfer(cmd);
_spi->transfer(IOCONA);
_spi->transfer(0x18);
::digitalWrite(_cs, HIGH);
writeAll();
}
/*! This private function reads a value from the specified register on the chip and
* stores it in the _reg array for later usage.
*/
void MCP23S17::readRegister(uint8_t addr) {
if (addr > 21) {
return;
}
uint8_t cmd = 0b01000001 | ((_addr & 0b111) << 1);
::digitalWrite(_cs, LOW);
_spi->transfer(cmd);
_spi->transfer(addr);
_reg[addr] = _spi->transfer(0xFF);
::digitalWrite(_cs, HIGH);
}
/*! This private function writes the current value of a register (as stored in the
* _reg array) out to the register in the chip.
*/
void MCP23S17::writeRegister(uint8_t addr) {
if (addr > 21) {
return;
}
uint8_t cmd = 0b01000000 | ((_addr & 0b111) << 1);
::digitalWrite(_cs, LOW);
_spi->transfer(cmd);
_spi->transfer(addr);
_spi->transfer(_reg[addr]);
::digitalWrite(_cs, HIGH);
}
/*! This private function performs a bulk read on all the registers in the chip to
* ensure the _reg array contains all the correct current values.
*/
void MCP23S17::readAll() {
uint8_t cmd = 0b01000001 | ((_addr & 0b111) << 1);
::digitalWrite(_cs, LOW);
_spi->transfer(cmd);
_spi->transfer(0);
for (uint8_t i = 0; i < 22; i++) {
_reg[i] = _spi->transfer(0xFF);
}
::digitalWrite(_cs, HIGH);
}
/*! This private function performs a bulk write of all the data in the _reg array
* out to all the registers on the chip. It is mainly used during the initialisation
* of the chip.
*/
void MCP23S17::writeAll() {
uint8_t cmd = 0b01000000 | ((_addr & 0b111) << 1);
::digitalWrite(_cs, LOW);
_spi->transfer(cmd);
_spi->transfer(0);
for (uint8_t i = 0; i < 22; i++) {
_spi->transfer(_reg[i]);
}
::digitalWrite(_cs, HIGH);
}
/*! Just like the pinMode() function of the Arduino API, this function sets the
* direction of the pin. The first parameter is the pin nimber (0-15) to use,
* and the second parameter is the direction of the pin. There are standard
* Arduino macros for different modes which should be used. The supported macros are:
*
* * OUTPUT
* * INPUT
* * INPUT_PULLUP
*
* The INPUT_PULLUP mode enables the weak pullup that is available on any pin.
*
* Example:
*
* myExpander.pinMode(5, INPUT_PULLUP);
*/
void MCP23S17::pinMode(uint8_t pin, uint8_t mode) {
if (pin >= 16) {
return;
}
uint8_t dirReg = IODIRA;
uint8_t puReg = GPPUA;
if (pin >= 8) {
pin -= 8;
dirReg = IODIRB;
puReg = GPPUB;
}
switch (mode) {
case OUTPUT:
_reg[dirReg] &= ~(1<<pin);
writeRegister(dirReg);
break;
case INPUT:
case INPUT_PULLUP:
_reg[dirReg] |= (1<<pin);
writeRegister(dirReg);
if (mode == INPUT_PULLUP) {
_reg[puReg] |= (1<<pin);
} else {
_reg[puReg] &= ~(1<<pin);
}
writeRegister(puReg);
break;
}
}
/*! Like the Arduino API's namesake, this function will set an output pin to a specific
* value, either HIGH (1) or LOW (0). If the pin is currently set to an INPUT instead of
* an OUTPUT, then this function acts like the old way of enabling / disabling the pullup
* resistor, which pre-1.0.0 versions of the Arduino API used - i.e., set HIGH to enable the
* pullup, or LOW to disable it.
*
* Example:
*
* myExpander.digitalWrite(3, HIGH);
*/
void MCP23S17::digitalWrite(uint8_t pin, uint8_t value) {
if (pin >= 16) {
return;
}
uint8_t dirReg = IODIRA;
uint8_t puReg = GPPUA;
uint8_t latReg = OLATA;
if (pin >= 8) {
pin -= 8;
dirReg = IODIRB;
puReg = GPPUB;
latReg = OLATB;
}
uint8_t mode = (_reg[dirReg] & (1<<pin)) == 0 ? OUTPUT : INPUT;
switch (mode) {
case OUTPUT:
if (value == 0) {
_reg[latReg] &= ~(1<<pin);
} else {
_reg[latReg] |= (1<<pin);
}
writeRegister(latReg);
break;
case INPUT:
if (value == 0) {
_reg[puReg] &= ~(1<<pin);
} else {
_reg[puReg] |= (1<<pin);
}
writeRegister(puReg);
break;
}
}
/*! This will return the current state of a pin set to INPUT, or the last
* value written to a pin set to OUTPUT.
*
* Example:
*
* byte value = myExpander.digitalRead(4);
*/
uint8_t MCP23S17::digitalRead(uint8_t pin) {
if (pin >= 16) {
return 0;
}
uint8_t dirReg = IODIRA;
uint8_t portReg = GPIOA;
uint8_t latReg = OLATA;
if (pin >= 8) {
pin -= 8;
dirReg = IODIRB;
portReg = GPIOB;
latReg = OLATB;
}
uint8_t mode = (_reg[dirReg] & (1<<pin)) == 0 ? OUTPUT : INPUT;
switch (mode) {
case OUTPUT:
return _reg[latReg] & (1<<pin) ? HIGH : LOW;
case INPUT:
readRegister(portReg);
return _reg[portReg] & (1<<pin) ? HIGH : LOW;
}
return 0;
}
/*! This function returns the entire 8-bit value of a GPIO port. Note that
* only the bits which correspond to a GPIO pin set to INPUT are valid.
* Other pins should be ignored. The only parameter defines which port (A/B)
* to retrieve: 0 is port A and 1 (or anything other than 0) is port B.
*
* Example:
*
* byte portA = myExpander.readPort(0);
*/
uint8_t MCP23S17::readPort(uint8_t port) {
if (port == 0) {
readRegister(GPIOA);
return _reg[GPIOA];
} else {
readRegister(GPIOB);
return _reg[GPIOB];
}
}
/*! This is a full 16-bit version of the parameterised readPort function. This
* version reads the value of both ports and combines them into a single 16-bit
* value.
*
* Example:
*
* unsigned int value = myExpander.readPort();
*/
uint16_t MCP23S17::readPort() {
readRegister(GPIOA);
readRegister(GPIOB);
return (_reg[GPIOB] << 8) | _reg[GPIOA];
}
/*! This writes an 8-bit value to one of the two IO port banks (A/B) on the chip.
* The value is output direct to any pins on that bank that are set as OUTPUT. Any
* bits that correspond to pins set to INPUT are ignored. As with the readPort
* function the first parameter defines which bank to use (0 = A, 1+ = B).
*
* Example:
*
* myExpander.writePort(0, 0x55);
*/
void MCP23S17::writePort(uint8_t port, uint8_t val) {
if (port == 0) {
_reg[OLATA] = val;
writeRegister(OLATA);
} else {
_reg[OLATB] = val;
writeRegister(OLATB);
}
}
/*! This is the 16-bit version of the writePort function. This takes a single
* 16-bit value and splits it between the two IO ports, the upper half going to
* port B and the lower to port A.
*
* Example:
*
* myExpander.writePort(0x55AA);
*/
void MCP23S17::writePort(uint16_t val) {
_reg[OLATB] = val >> 8;
_reg[OLATA] = val & 0xFF;
writeRegister(OLATA);
writeRegister(OLATB);
}
/*! This enables the interrupt functionality of a pin. The interrupt type can be one of:
*
* * CHANGE
* * RISING
* * FALLING
*
* When an interrupt occurs the corresponding port's INT pin will be driven to it's configured
* level, and will remain there until either the port is read with a readPort or digitalRead, or the
* captured port status at the time of the interrupt is read using getInterruptValue.
*
* Example:
*
* myExpander.enableInterrupt(4, RISING);
*/
void MCP23S17::enableInterrupt(uint8_t pin, uint8_t type) {
if (pin >= 16) {
return;
}
uint8_t intcon = INTCONA;
uint8_t defval = DEFVALA;
uint8_t gpinten = GPINTENA;
if (pin >= 8) {
pin -= 8;
intcon = INTCONB;
defval = DEFVALB;
gpinten = GPINTENB;
}
switch (type) {
case CHANGE:
_reg[intcon] &= ~(1<<pin);
break;
case RISING:
_reg[intcon] |= (1<<pin);
_reg[defval] &= ~(1<<pin);
break;
case FALLING:
_reg[intcon] |= (1<<pin);
_reg[defval] |= (1<<pin);
break;
}
_reg[gpinten] |= (1<<pin);
writeRegister(intcon);
writeRegister(defval);
writeRegister(gpinten);
}
/*! This disables the interrupt functionality of a pin.
*
* Example:
*
* myExpander.disableInterrupt(4);
*/
void MCP23S17::disableInterrupt(uint8_t pin) {
if (pin >= 16) {
return;
}
uint8_t gpinten = GPINTENA;
if (pin >= 8) {
pin -= 8;
gpinten = GPINTENB;
}
_reg[gpinten] &= ~(1<<pin);
writeRegister(gpinten);
}
/*! The two IO banks can have their INT pins connected together.
* This enables you to monitor both banks with just one interrupt pin
* on the host microcontroller. Calling setMirror with a parameter of
* *true* will enable this feature. Calling it with *false* will disable
* it.
*
* Example:
*
* myExpander.setMirror(true);
*/
void MCP23S17::setMirror(boolean m) {
if (m) {
_reg[IOCONA] |= (1<<6);
_reg[IOCONB] |= (1<<6);
} else {
_reg[IOCONA] &= ~(1<<6);
_reg[IOCONB] &= ~(1<<6);
}
writeRegister(IOCONA);
}
/*! This function returns a 16-bit bitmap of the the pin or pins that have cause an interrupt to fire.
*
* Example:
*
* unsigned int pins = myExpander.getInterruptPins();
*/
uint16_t MCP23S17::getInterruptPins() {
readRegister(INTFA);
readRegister(INTFB);
return (_reg[INTFB] << 8) | _reg[INTFA];
}
/*! This returns a snapshot of the IO pin states at the moment the last interrupt occured. Reading
* this value clears the interrupt status (and hence the INT pins) for the whole chip.
* Until this value is read (or the current live port value is read) no further interrupts can
* be indicated.
*
* Example:
*
* unsigned int pinValues = myExpander.getInterruptPins();
*/
uint16_t MCP23S17::getInterruptValue() {
readRegister(INTCAPA);
readRegister(INTCAPB);
return (_reg[INTCAPB] << 8) | _reg[INTCAPA];
}
/*! This sets the "active" level for an interrupt. HIGH means the interrupt pin
* will go HIGH when an interrupt occurs, LOW means it will go LOW.
*
* Example:
*
* myExpander.setInterruptLevel(HIGH);
*/
void MCP23S17::setInterruptLevel(uint8_t level) {
if (level == LOW) {
_reg[IOCONA] &= ~(1<<1);
_reg[IOCONB] &= ~(1<<1);
} else {
_reg[IOCONA] |= (1<<1);
_reg[IOCONB] |= (1<<1);
}
writeRegister(IOCONA);
}
/*! Using this function it is possible to configure the interrupt output pins to be open
* drain. This means that interrupt pins from multiple chips can share the same interrupt
* pin on the host MCU. This causes the level set by setInterruptLevel to be ignored. A
* pullup resistor will be required on the host MCU's interrupt pin.
*
* Example:
*
* myExpander.setInterruptOD(true);
*/
void MCP23S17::setInterruptOD(boolean openDrain) {
if (openDrain) {
_reg[IOCONA] |= (1<<2);
_reg[IOCONB] |= (1<<2);
} else {
_reg[IOCONA] &= ~(1<<2);
_reg[IOCONB] &= ~(1<<2);
}
writeRegister(IOCONA);
}