-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mcp23s17.cpp
331 lines (261 loc) · 8.03 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
// MCP23S17 SPI 16-bit IO expander
// http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
// For the cmd, AAA is the 3-bit MCP23S17 device hardware address.
// Useful for letting up to 8 chips sharing same SPI Chip select
// #define MCP23S17_READ B0100AAA1
// #define MCP23S17_WRITE B0100AAA0
// The default SPI Control Register - SPCR = B01010000;
// interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
// sample on leading edge of clk,system clock/4 rate (fastest).
// Enable the digital pins 11-13 for SPI (the MOSI,MISO,SPICLK)
#include <SPI.h>
#include "mcp23s17.h"
#include <Wire.h>
// Note: You may need to take _RESET_ on the MCP23S17 low
// for a few hundred ms and then take (and hold) it high
// again before you begin to communicate with the chip.
//---------- constructor ----------------------------------------------------
MCP23S17::MCP23S17(uint8_t slave_select_pin)
{
SPI.begin();
setup_ss(slave_select_pin);
setup_device(0x00);
}
//#define ADDR_ENABLE (0b00001000) // Configuration register for MCP23S17, the only thing we change is enabling hardware addressing
MCP23S17::MCP23S17(uint8_t slave_select_pin, byte aaa_hw_addr)
{
SPI.begin();
// Set the aaa hardware address for this chip by tying the
// MCP23S17's pins (A0, A1, and A2) to either 5v or GND.
setup_ss(slave_select_pin);
SPI.setClockDivider(CLOCK_DIVIDER); // Sets the SPI bus speed
SPI.setBitOrder(MSBFIRST); // Sets SPI bus bit order (this is the default, setting it for good form!)
SPI.setDataMode(SPI_MODE0); // Sets the SPI bus timing mode (this is the default, setting it for good form!)
SPI.setClockDivider(0 ); // Sets the SPI bus speed
::digitalWrite(slave_select_pin, LOW);
SPI.transfer(0b01000000);
SPI.transfer(IOCON);
SPI.transfer(0b00001000);
::digitalWrite(slave_select_pin, HIGH);
::digitalWrite(slave_select_pin, LOW);
SPI.transfer(0b01001110);
SPI.transfer(IOCON);
SPI.transfer(0b00001000);
::digitalWrite(slave_select_pin, HIGH);
// We enable HAEN on all connected devices before we can address them individually
setup_device(0x07);
write_addr(IOCON, read_addr(IOCON)|HAEN);
setup_device(0x00);
write_addr(IOCON, read_addr(IOCON)|HAEN);
// Remember the hardware address for this chip
setup_device(aaa_hw_addr);
}
//------------------ protected -----------------------------------------------
uint16_t MCP23X17::byte2uint16(byte high_byte, byte low_byte)
{
return (uint16_t)high_byte<<8 | (uint16_t)low_byte;
}
byte MCP23X17::uint16_high_byte(uint16_t uint16)
{
return (byte)(uint16>>8);
}
byte MCP23X17::uint16_low_byte(uint16_t uint16)
{
return (byte)(uint16 & 0x00FF);
}
void MCP23S17::setup_ss(uint8_t slave_select_pin)
{
// Set slave select (Chip Select) pin for SPI Bus, and start high (disabled)
::pinMode(slave_select_pin,OUTPUT);
::digitalWrite(slave_select_pin,HIGH);
this->slave_select_pin = slave_select_pin;
}
void MCP23S17::setup_device(uint8_t aaa_hw_addr)
{
this->aaa_hw_addr = aaa_hw_addr;
this->read_cmd = B01000000 | aaa_hw_addr<<1 | 1<<0; // MCP23S17_READ = B0100AAA1
this->write_cmd = B01000000 | aaa_hw_addr<<1 | 0<<0; // MCP23S17_WRITE = B0100AAA0
write_addr(IOCON, read_addr(IOCON)|SEQOP); // no need to enable SEQOP if BANK=0
}
uint16_t MCP23S17::read_addr(byte addr)
{
byte low_byte;
byte high_byte;
::digitalWrite(slave_select_pin, LOW);
SPI.transfer(read_cmd);
SPI.transfer(addr);
low_byte = SPI.transfer(0x0/*dummy data for read*/);
high_byte = SPI.transfer(0x0/*dummy data for read*/);
::digitalWrite(slave_select_pin, HIGH);
return byte2uint16(high_byte,low_byte);
}
uint8_t MCP23S17::read_addr_byte(byte addr)
{
byte low_byte;
// ::digitalWrite(slave_select_pin, LOW);
PORTB &= ~(1<<PB0);
SPI.transfer(read_cmd);
SPI.transfer(addr);
low_byte = SPI.transfer(0x0/*dummy data for read*/);
// ::digitalWrite(slave_select_pin, HIGH);
PORTB |= (1<<PB0);
return low_byte;
}
void MCP23S17::write_addr(byte addr, uint16_t data)
{
::digitalWrite(slave_select_pin, LOW);
SPI.transfer(write_cmd);
SPI.transfer(addr);
SPI.transfer(uint16_low_byte(data));
SPI.transfer(uint16_high_byte(data));
::digitalWrite(slave_select_pin, HIGH);
}
void MCP23S17::write_addr_byte(byte addr, uint8_t data)
{
PORTB &= ~(1<<PB0);
//::digitalWrite(slave_select_pin, LOW);
SPI.transfer(write_cmd);
SPI.transfer(addr);
SPI.transfer(data);
// ::digitalWrite(slave_select_pin, HIGH);
PORTB |= (1<<PB0);
}
//---------- public ----------------------------------------------------
void MCP23X17::pinMode(bool mode)
{
uint16_t input_pins;
if(mode == INPUT)
input_pins = 0xFFFF;
else
input_pins = 0x0000;
write_addr(IODIR, input_pins);
}
void MCP23X17::portA(uint8_t value)
{
write_addr_byte(GPIOA,value);
}
void MCP23X17::portB(uint8_t value)
{
write_addr_byte(GPIOA,value);
}
uint8_t MCP23X17::portA(void)
{
return read_addr_byte(GPIOA);
}
uint8_t MCP23X17::portB(void)
{
return read_addr_byte(GPIOB);
}
void MCP23X17::port(uint16_t value)
{
write_addr(GPIO,value);
}
uint16_t MCP23X17::port()
{
return read_addr(GPIO);
}
void MCP23X17::ddrA(uint8_t val)
{
write_addr_byte(IODIRA,val);
}
void MCP23X17::ddrB(uint8_t val)
{
write_addr_byte(IODIRB,val);
}
void MCP23X17::pinMode(uint8_t pin, bool mode)
{
if(mode = INPUT)
write_addr(IODIR, read_addr(IODIR) | 1<<pin );
else
write_addr(IODIR, read_addr(IODIR) & ~(1<<pin) );
}
void MCP23X17::pinModeA(uint8_t pin, bool mode)
{
if(mode)
write_addr_byte(IODIRA, read_addr_byte(IODIRA) | 1<<pin );
else
write_addr_byte(IODIRA, read_addr_byte(IODIRA) & ~(1<<pin) );
}
void MCP23X17::pinA(uint8_t pin, bool mode)
{
if(mode)
write_addr_byte(GPIOA, read_addr_byte(GPIOA) | 1<<pin );
else
write_addr_byte(GPIOA, read_addr_byte(GPIOA) & ~(1<<pin) );
}
void MCP23X17::pinB(uint8_t pin, bool mode)
{
if(mode)
write_addr_byte(GPIOB, read_addr_byte(GPIOB) | 1<<pin );
else
write_addr_byte(GPIOB, read_addr_byte(GPIOB) & ~(1<<pin) );
}
void MCP23X17::pinModeB(uint8_t pin, bool mode)
{
if(mode)
write_addr_byte(IODIRB, read_addr_byte(IODIRB) | 1<<pin );
else
write_addr_byte(IODIRB, read_addr_byte(IODIRB) & ~(1<<pin) );
}
void MCP23X17::digitalWrite(uint8_t pin, bool value)
{
if(value)
write_addr(GPIO, read_addr(GPIO) | 1<<pin );
else
write_addr(GPIO, read_addr(GPIO) & ~(1<<pin) );
}
int MCP23X17::digitalRead(uint8_t pin)
{
(int)(read_addr(GPIO) & 1<<pin);
}
// ---------------------------------------------------------------
// MCP23017 I2C
// ---------------------------------------------------------------
MCP23017::MCP23017( byte aaa_hw_addr)
{
Wire.begin(); //start I2c as master
// We enable HAEN on all connected devices before we can address them individually
// setup_device(0x07);
// write_addr(IOCON, read_addr(IOCON)|HAEN);
// setup_device(0x00);
// write_addr(IOCON, read_addr(IOCON)|HAEN);
// Remember the hardware address for this chip
setup_device(aaa_hw_addr);
}
void MCP23017::setup_device(uint8_t aaa_hw_addr)
{
this->aaa_hw_addr = aaa_hw_addr;
this->mcp_addr = 0x20 | aaa_hw_addr; // MCP23017 = B0100AAA1, but shifted in wire
//write_addr(IOCON, read_addr(IOCON)|SEQOP); // no need to enable SEQOP if BANK=0
}
uint16_t MCP23017::read_addr(byte addr)
{
Wire.beginTransmission(mcp_addr);
Wire.write((uint8_t)addr);
Wire.endTransmission();
Wire.requestFrom((uint8_t)mcp_addr ,(uint8_t) 2 );
return ( Wire.read() ) | ( Wire.read() << 8);
}
uint8_t MCP23017::read_addr_byte(byte addr)
{
Wire.beginTransmission(mcp_addr);
Wire.write((uint8_t)addr);
Wire.endTransmission();
Wire.requestFrom((uint8_t)mcp_addr ,(uint8_t) 1 );
return Wire.read();
}
void MCP23017::write_addr(byte addr, uint16_t data)
{
Wire.beginTransmission(mcp_addr);
Wire.write((uint8_t)addr);
Wire.write((uint8_t)( data& 0xff));
Wire.write((uint8_t)( (data>>8)& 0xff));
Wire.endTransmission();
}
void MCP23017::write_addr_byte(byte addr, uint8_t data)
{
Wire.beginTransmission(mcp_addr);
Wire.write((uint8_t)addr);
Wire.write((uint8_t)( data & 0xff));
Wire.endTransmission();
}