-
Notifications
You must be signed in to change notification settings - Fork 29
/
mcp23s17.cpp
315 lines (267 loc) · 6.88 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
#include <stdio.h>
#include <inttypes.h>
#include <Arduino.h>
#include "mcp23s17.h"
#include <../SPI/SPI.h>//this chip needs SPI
mcp23s17::mcp23s17(){
#if defined (SPI_HAS_TRANSACTION)
_spiTransactionsSpeed = MAXSPISPEED;//set to max supported speed (in relation to chip and CPU)
#else
_spiTransactionsSpeed = 0;
#endif
}
//return 255 if the choosed pin has no INT, otherwise return INT number
//if there's support for SPI transactions it will use SPI.usingInterrupt(intNum);
//to prevent problems from interrupt
/*USE:
int intNumber = mcp.getInterruptNumber(gpio_int_pin);
if (intNumber < 255){
attachInterrupt(intNumber, keypress, FALLING);//attack interrupt
} else {
Serial.println("sorry, pin has no INT capabilities!");
}
*/
int mcp23s17::getInterruptNumber(byte pin) {
int intNum = digitalPinToInterrupt(pin);
if (intNum != NOT_AN_INTERRUPT) {
#if defined (SPI_HAS_TRANSACTION)
SPI.usingInterrupt(intNum);
#endif
return intNum;
}
return 255;
}
void mcp23s17::setSPIspeed(uint32_t spispeed){
#if defined (SPI_HAS_TRANSACTION)
if (spispeed > 0){
if (spispeed > MAXSPISPEED) {
_spiTransactionsSpeed = MAXSPISPEED;
} else {
_spiTransactionsSpeed = spispeed;
}
} else {
_spiTransactionsSpeed = 0;//disable SPItransactons
}
#else
_spiTransactionsSpeed = 0;
#endif
}
mcp23s17::mcp23s17(const uint8_t csPin,const uint8_t haenAdrs){
_spiTransactionsSpeed = 0;
postSetup(csPin,haenAdrs);
}
mcp23s17::mcp23s17(const uint8_t csPin,const uint8_t haenAdrs,uint32_t spispeed){
postSetup(csPin,haenAdrs,spispeed);
}
void mcp23s17::postSetup(const uint8_t csPin,const uint8_t haenAdrs,uint32_t spispeed){
#if defined (SPI_HAS_TRANSACTION)
if (spispeed > 0) setSPIspeed(spispeed);
#endif
_cs = csPin;
if (haenAdrs > 0x19 && haenAdrs < 0x28){//HAEN works between 0x20...0x27
_adrs = haenAdrs;
_useHaen = 1;
} else {
_adrs = 0;
_useHaen = 0;
}
_readCmd = (_adrs << 1) | 1;
_writeCmd = _adrs << 1;
//setup register values for this chip
IOCON = 0x0A;
IODIR = 0x00;
GPPU = 0x0C;
GPIO = 0x12;
GPINTEN = 0x04;
IPOL = 0x02;
DEFVAL = 0x06;
INTF = 0x0E;
INTCAP = 0x10;
OLAT = 0x14;
INTCON = 0x08;
}
void mcp23s17::begin(bool protocolInitOverride) {
if (!protocolInitOverride){
SPI.begin();
#if defined (SPI_HAS_TRANSACTION)
if (_spiTransactionsSpeed == 0){//do not use SPItransactons
SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz (half speed)
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
}
#else//do not use SPItransactons
SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz (half speed)
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
#endif
}
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
delay(100);
_useHaen == 1 ? writeByte(IOCON,0b00101000) : writeByte(IOCON,0b00100000);
_gpioDirection = 0xFFFF;//all in
_gpioState = 0xFFFF;//all low
}
uint16_t mcp23s17::readAddress(byte addr){
startSend(1);
SPI.transfer(addr);
#if !defined(__SAM3X8E__) && ((ARDUINO >= 160) || (TEENSYDUINO > 121))
uint16_t temp = SPI.transfer16(0x0);
endSend();
return temp;
#else
byte low_byte = SPI.transfer(0x0);
byte high_byte = SPI.transfer(0x0);
endSend();
uint16_t temp = low_byte | (high_byte << 8);
return temp;
#endif
}
void mcp23s17::gpioPinMode(uint16_t mode){
if (mode == INPUT){
_gpioDirection = 0xFFFF;
} else if (mode == OUTPUT){
_gpioDirection = 0x0000;
_gpioState = 0x0000;
} else {
_gpioDirection = mode;
}
writeWord(IODIR,_gpioDirection);
}
void mcp23s17::gpioPinMode(uint8_t pin, bool mode){
if (pin < 16){//0...15
mode == INPUT ? _gpioDirection |= (1 << pin) :_gpioDirection &= ~(1 << pin);
writeWord(IODIR,_gpioDirection);
}
}
void mcp23s17::gpioPort(uint16_t value){
if (value == HIGH){
_gpioState = 0xFFFF;
} else if (value == LOW){
_gpioState = 0x0000;
} else {
_gpioState = value;
}
writeWord(GPIO,_gpioState);
}
void mcp23s17::gpioPort(byte lowByte, byte highByte){
//_gpioState = byte2word(highByte,lowByte);
//_gpioState = lowByte | (highByte << 8);
_gpioState = highByte | (lowByte << 8);
writeWord(GPIO,_gpioState);
}
uint16_t mcp23s17::readGpioPort(){
return readAddress(GPIO);
}
uint16_t mcp23s17::readGpioPortFast(){
return _gpioState;
}
void mcp23s17::portPullup(uint16_t data) {
if (data == HIGH){
_gpioState = 0xFFFF;
} else if (data == LOW){
_gpioState = 0x0000;
} else {
_gpioState = data;
}
writeWord(GPPU, _gpioState);
}
void mcp23s17::gpioDigitalWrite(uint8_t pin, bool value){
if (pin < 16){//0...15
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
writeWord(GPIO,_gpioState);
}
}
void mcp23s17::gpioDigitalWriteFast(uint8_t pin, bool value){
if (pin < 16){//0...15
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
}
}
void mcp23s17::gpioPortUpdate(){
writeWord(GPIO,_gpioState);
}
int mcp23s17::gpioDigitalRead(uint8_t pin){
if (pin < 16) return (int)(readAddress(GPIO) & 1 << pin);
return 0;
}
int mcp23s17::gpioDigitalReadFast(uint8_t pin){
int temp = 0;
if (pin < 16) temp = bitRead(_gpioState,pin);
return temp;
}
uint8_t mcp23s17::gpioRegisterReadByte(byte reg){
uint8_t data = 0;
startSend(1);
SPI.transfer(reg);
data = SPI.transfer(0);
endSend();
return data;
}
uint16_t mcp23s17::gpioRegisterReadWord(byte reg){
uint16_t data = 0;
startSend(1);
SPI.transfer(reg);
#if !defined(__SAM3X8E__) && ((ARDUINO >= 160) || (TEENSYDUINO > 121))
data = SPI.transfer16(0);
#else
data = SPI.transfer(0);
data = SPI.transfer(0) << 8;
#endif
endSend();
return data;
}
void mcp23s17::gpioRegisterWriteByte(byte reg,byte data,bool both){
if (!both){
writeByte(reg,(byte)data);
} else {
startSend(0);
SPI.transfer(reg);
SPI.transfer(data);
SPI.transfer(data);
endSend();
}
}
void mcp23s17::gpioRegisterWriteWord(byte reg,word data){
writeWord(reg,(word)data);
}
/* ------------------------------ Low Level ----------------*/
void mcp23s17::writeByte(byte addr, byte data){
startSend(0);
SPI.transfer(addr);
SPI.transfer(data);
endSend();
}
void mcp23s17::writeWord(byte addr, uint16_t data){
startSend(0);
SPI.transfer(addr);
#if !defined(__SAM3X8E__) && ((ARDUINO >= 160) || (TEENSYDUINO > 121))
SPI.transfer16(data);
#else
//SPI.transfer(word2lowByte(data));
//SPI.transfer(word2highByte(data));
SPI.transfer(data >> 8);
SPI.transfer(data & 0xFF);
#endif
endSend();
}
void mcp23s17::startSend(bool mode){
#if defined (SPI_HAS_TRANSACTION)
if (_spiTransactionsSpeed > 0) SPI.beginTransaction(SPISettings(_spiTransactionsSpeed, MSBFIRST, SPI_MODE0));
#endif
#if defined(__FASTWRITE)
digitalWriteFast(_cs, LOW);
#else
digitalWrite(_cs, LOW);
#endif
mode == 1 ? SPI.transfer(_readCmd) : SPI.transfer(_writeCmd);
}
void mcp23s17::endSend(){
#if defined(__FASTWRITE)
digitalWriteFast(_cs, HIGH);
#else
digitalWrite(_cs, HIGH);
#endif
#if defined (SPI_HAS_TRANSACTION)
if (_spiTransactionsSpeed > 0) SPI.endTransaction();
#endif
}