FabGL
ESP32 Display Controller and Graphics Library
MCP23S17.h
Go to the documentation of this file.
1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27
28#pragma once
29
30
31
32#include "driver/spi_master.h"
33
34#include "fabutils.h"
35
36
37
47namespace fabgl {
48
49
50
51#define MCP_SPI_FREQ 10000000 // it seems to work up to 23000000!! (but datasheet specifies 10000000)
52#define MCP_DMACHANNEL 2
53
54
55#define MCP_MAXDEVICES 2
56
57
58#define MCP_PORTA 0
59#define MCP_PORTB 1
60
61
62#define MCP_A0 0
63#define MCP_A1 1
64#define MCP_A2 2
65#define MCP_A3 3
66#define MCP_A4 4
67#define MCP_A5 5
68#define MCP_A6 6
69#define MCP_A7 7
70
71#define MCP_B0 8
72#define MCP_B1 9
73#define MCP_B2 10
74#define MCP_B3 11
75#define MCP_B4 12
76#define MCP_B5 13
77#define MCP_B6 14
78#define MCP_B7 15
79
80
81// bank 0 registers (A = reg + 0, B = reg + 1)
82#define MCP_IODIR 0x00
83#define MCP_IPOL 0x02
84#define MCP_GPINTEN 0x04
85#define MCP_DEFVAL 0x06
86#define MCP_INTCON 0x08
87#define MCP_IOCON 0x0A
88#define MCP_GPPU 0x0C
89#define MCP_INTF 0x0E
90#define MCP_INTCAP 0x10
91#define MCP_GPIO 0x12
92#define MCP_OLAT 0x14
93
94
95// bank 1 registers (A = reg + 0, B = reg + 0x10)
96#define MCP_BNK1_IODIR 0x00
97#define MCP_BNK1_IPOL 0x01
98#define MCP_BNK1_GPINTEN 0x02
99#define MCP_BNK1_DEFVAL 0x03
100#define MCP_BNK1_INTCON 0x04
101#define MCP_BNK1_IOCON 0x05
102#define MCP_BNK1_GPPU 0x06
103#define MCP_BNK1_INTF 0x07
104#define MCP_BNK1_INTCAP 0x08
105#define MCP_BNK1_GPIO 0x09
106#define MCP_BNK1_OLAT 0x0A
107
108
109// IOCON bits
110#define MCP_IOCON_BANK 0x80 // Controls how the registers are addressed (0 = bank0)
111#define MCP_IOCON_MIRROR 0x40 // INT Pins Mirror bit (1 = mirrored)
112#define MCP_IOCON_SEQOP 0x20 // Sequential Operation mode bit (1 = not increment)
113#define MCP_IOCON_DISSLW 0x10 // Slew Rate control bit for SDA output (I2C only)
114#define MCP_IOCON_HAEN 0x08 // Hardware Address Enable bit
115#define MCP_IOCON_ODR 0x04 // Configures the INT pin as an open-drain output (1 = open-drain)
116#define MCP_IOCON_INTPOL 0x02 // This bit sets the polarity of the INT output pin (1 = active-high)
117
118
119#define MCP_GPIO2REG(basereg, gpio) ((basereg) + ((gpio) >> 3))
120#define MCP_GPIO2MASK(gpio) (1 << ((gpio) & 7))
121
122
126enum class MCPDir {
127 Input,
128 Output
129};
130
131
135enum class MCPIntTrigger {
138};
139
140
155class MCP23S17 {
156
157public:
158
159 MCP23S17();
160 ~MCP23S17();
161
162
164
165
176 bool begin(int MISO = -1, int MOSI = -1, int CLK = -1, int CS = -1, int CSActiveState = -1, int host = HSPI_HOST);
177
181 void end();
182
188 bool available() { return m_SPIDevHandle != nullptr; }
189
208 bool initDevice(uint8_t hwAddr);
209
210
212
213
230 void writeReg(uint8_t addr, uint8_t value, uint8_t hwAddr = 0);
231
240 uint8_t readReg(uint8_t addr, uint8_t hwAddr = 0);
241
249 void writeReg16(uint8_t addr, uint16_t value, uint8_t hwAddr = 0);
250
259 uint16_t readReg16(uint8_t addr, uint8_t hwAddr = 0);
260
261
263
264
271 void enableINTMirroring(bool value, uint8_t hwAddr = 0);
272
279 void enableINTOpenDrain(bool value, uint8_t hwAddr = 0);
280
287 void setINTActiveHigh(bool value, uint8_t hwAddr = 0);
288
296 bool getINTActiveHigh(uint8_t hwAddr = 0) { return readReg(MCP_IOCON, hwAddr) & MCP_IOCON_INTPOL; }
297
298
300
301
314 void setPortDir(int port, uint8_t value, uint8_t hwAddr = 0) { writeReg(MCP_IODIR + port, value, hwAddr); }
315
324 uint8_t getPortDir(int port, uint8_t hwAddr = 0) { return readReg(MCP_IODIR + port, hwAddr); }
325
333 void setPortInputPolarity(int port, uint8_t value, uint8_t hwAddr = 0) { writeReg(MCP_IPOL + port, value, hwAddr); }
334
347 void enablePortPullUp(int port, uint8_t value, uint8_t hwAddr = 0) { writeReg(MCP_GPPU + port, value, hwAddr); }
348
357 uint8_t getPortPullUp(int port, uint8_t hwAddr = 0) { return readReg(MCP_GPPU + port, hwAddr); }
358
371 void writePort(int port, uint8_t value, uint8_t hwAddr = 0) { writeReg(MCP_OLAT + port, value, hwAddr); }
372
386 uint8_t readPort(int port, uint8_t hwAddr = 0) { return readReg(MCP_GPIO + port, hwAddr); }
387
399 void writePort16(uint16_t value, uint8_t hwAddr = 0) { writeReg16(MCP_OLAT, value, hwAddr); }
400
413 uint16_t readPort16(uint8_t hwAddr = 0) { return readReg16(MCP_GPIO, hwAddr); }
414
415
417
418
432 void configureGPIO(int gpio, MCPDir dir, bool pullup = false, uint8_t hwAddr = 0);
433
445 void writeGPIO(int gpio, bool value, uint8_t hwAddr = 0);
446
459 bool readGPIO(int gpio, uint8_t hwAddr = 0);
460
461
463
464
486 void enableInterrupt(int gpio, MCPIntTrigger trigger, bool defaultValue = false, uint8_t hwAddr = 0);
487
498 void disableInterrupt(int gpio, uint8_t hwAddr = 0);
499
500
502
503
512 uint8_t getPortIntFlags(int port, uint8_t hwAddr = 0) { return readReg(MCP_INTF + port, hwAddr); }
513
522 uint8_t getPortIntCaptured(int port, uint8_t hwAddr = 0) { return readReg(MCP_INTCAP + port, hwAddr); }
523
524
526
527
541 void writePort(int port, void const * buffer, size_t length, uint8_t hwAddr = 0);
542
557 void readPort(int port, void * buffer, size_t length, uint8_t hwAddr = 0);
558
559
560private:
561
562 bool SPIBegin(int CSActiveState);
563 void SPIEnd();
564
565 gpio_num_t m_MISO;
566 gpio_num_t m_MOSI;
567 gpio_num_t m_CLK;
568 gpio_num_t m_CS;
569 spi_host_device_t m_SPIHost;
570
571 spi_device_handle_t m_SPIDevHandle;
572
573 // cached registers
574 uint8_t m_IOCON[MCP_MAXDEVICES];
575};
576
577
578
579
580
581
582
583
584
585} // fabgl namespace
586
void configureGPIO(int gpio, MCPDir dir, bool pullup=false, uint8_t hwAddr=0)
Configure a pin direction and pullup.
Definition: MCP23S17.cpp:258
void setPortInputPolarity(int port, uint8_t value, uint8_t hwAddr=0)
Sets input polarity.
Definition: MCP23S17.h:333
void writeGPIO(int gpio, bool value, uint8_t hwAddr=0)
Sets output status of a pin.
Definition: MCP23S17.cpp:273
void writePort(int port, uint8_t value, uint8_t hwAddr=0)
Sets status of output pins of specified port.
Definition: MCP23S17.h:371
void enableINTMirroring(bool value, uint8_t hwAddr=0)
Enables/disables INTs pins mirroring.
Definition: MCP23S17.cpp:240
void setPortDir(int port, uint8_t value, uint8_t hwAddr=0)
Sets port direction.
Definition: MCP23S17.h:314
void writeReg16(uint8_t addr, uint16_t value, uint8_t hwAddr=0)
Writes 16 bit value to two consecutive registers.
Definition: MCP23S17.cpp:201
uint8_t getPortIntFlags(int port, uint8_t hwAddr=0)
Reads interrupt flags for the specified port.
Definition: MCP23S17.h:512
bool available()
Determines MCP23S17 availability.
Definition: MCP23S17.h:188
uint8_t getPortIntCaptured(int port, uint8_t hwAddr=0)
Reads status of input port when last interrupt has been triggered.
Definition: MCP23S17.h:522
void writePort16(uint16_t value, uint8_t hwAddr=0)
Sets status of output pins of combined port A and B.
Definition: MCP23S17.h:399
uint16_t readPort16(uint8_t hwAddr=0)
Gets status of input pins of combined port A and B.
Definition: MCP23S17.h:413
uint16_t readReg16(uint8_t addr, uint8_t hwAddr=0)
Reads 16 bit value from two consecutive registers.
Definition: MCP23S17.cpp:218
void writeReg(uint8_t addr, uint8_t value, uint8_t hwAddr=0)
Writes 8 bit value to an internal register.
Definition: MCP23S17.cpp:162
bool initDevice(uint8_t hwAddr)
Initializes additional MCP23S17 devices connected to the same SPI bus but with a different hardware a...
Definition: MCP23S17.cpp:101
void setINTActiveHigh(bool value, uint8_t hwAddr=0)
Sets the polarity of the INT pins.
Definition: MCP23S17.cpp:252
bool getINTActiveHigh(uint8_t hwAddr=0)
Gets the polarity of the INT pins.
Definition: MCP23S17.h:296
void end()
Deinitializes MCP23S17 driver.
Definition: MCP23S17.cpp:113
bool begin(int MISO=-1, int MOSI=-1, int CLK=-1, int CS=-1, int CSActiveState=-1, int host=HSPI_HOST)
Initializes MCP23S17 driver.
Definition: MCP23S17.cpp:53
void enableINTOpenDrain(bool value, uint8_t hwAddr=0)
Enables/disables the INT pin open-drain.
Definition: MCP23S17.cpp:246
uint8_t getPortDir(int port, uint8_t hwAddr=0)
Gets port direction.
Definition: MCP23S17.h:324
uint8_t getPortPullUp(int port, uint8_t hwAddr=0)
Gets port pull-ups.
Definition: MCP23S17.h:357
void enableInterrupt(int gpio, MCPIntTrigger trigger, bool defaultValue=false, uint8_t hwAddr=0)
Enables interrupt on the specific pin.
Definition: MCP23S17.cpp:288
void enablePortPullUp(int port, uint8_t value, uint8_t hwAddr=0)
Enables/disables port pull-ups.
Definition: MCP23S17.h:347
uint8_t readReg(uint8_t addr, uint8_t hwAddr=0)
Reads 8 bit value from an internal register.
Definition: MCP23S17.cpp:179
uint8_t readPort(int port, uint8_t hwAddr=0)
Gets status of input pins of specified port.
Definition: MCP23S17.h:386
void disableInterrupt(int gpio, uint8_t hwAddr=0)
Disables any interrupt on the specified pin.
Definition: MCP23S17.cpp:305
bool readGPIO(int gpio, uint8_t hwAddr=0)
Reads input status of a pin.
Definition: MCP23S17.cpp:282
MCP23S17 driver.
Definition: MCP23S17.h:155
This file contains some utility classes and functions.
MCPDir
Represents GPIO directioon.
Definition: MCP23S17.h:126
MCPIntTrigger
Represents interrupt trigger mode.
Definition: MCP23S17.h:135