-
Notifications
You must be signed in to change notification settings - Fork 2
/
BDN_MCP23S17.py
304 lines (249 loc) · 9.04 KB
/
BDN_MCP23S17.py
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
#!/usr/bin/python
import spidev
class MCP23S17(object):
"""This class provides an abstraction of the GPIO expander MCP23S17
for the Raspberry Pi.
It is depndent on the Python packages spidev, which can
be get from https://pypi.python.org/pypi/spidev.
"""
DIR_INPUT = 1
DIR_OUTPUT = 0
PULLUP_ENABLED = 1
PULLUP_DISABLED = 0
LEVEL_LOW = 0
LEVEL_HIGH = 1
"""Register addresses (ICON.BANK = 0) as documentined in the technical data sheet at
http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
"""
MCP23S17_IODIRA = 0x00
MCP23S17_IODIRB = 0x01
MCP23S17_IPOLA = 0x02
MCP23S17_IPOLB = 0x03
MCP23S17_GPIOA = 0x12
MCP23S17_GPIOB = 0x13
MCP23S17_OLATA = 0x14
MCP23S17_OLATB = 0x15
MCP23S17_IOCON = 0x0A
MCP23S17_GPPUA = 0x0C
MCP23S17_GPPUB = 0x0D
"""Bit field flags as documentined in the technical data sheet at
http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
"""
IOCON_UNUSED = 0x01
IOCON_INTPOL = 0x02
IOCON_ODR = 0x04
IOCON_HAEN = 0x08
IOCON_DISSLW = 0x10
IOCON_SEQOP = 0x20
IOCON_MIRROR = 0x40
IOCON_BANK_MODE = 0x80
IOCON_INIT = 0x00 # IOCON_BANK_MODE = 0, IOCON_HAEN = 0 address pins disabled
MCP23S17_CMD_WRITE = 0x40
MCP23S17_CMD_READ = 0x41
def __init__(self, bus=0, ce=0, deviceID=0x00):
"""Constructor
Initializes all attributes with 0.
Keyword arguments:
bus -- The SPI bus number
ce -- The chip-enable number for the SPI
deviceID -- The device ID of the component, i.e., the hardware address (default 0.0)
"""
self.spi = spidev.SpiDev()
self.bus = bus
self.ce = ce
self.deviceID = deviceID
self._GPIOA = 0x00
self._GPIOB = 0x00
self._IODIRA = 0xFF
self._IODIRB = 0xFF
self._GPPUA = 0x00
self._GPPUB = 0x00
self.isInitialized = False
def open(self):
"""Initializes the MCP23S17 with hardware-address access
and sequential operations mode.
"""
self.spi.open(self.bus, self.ce)
self.spi.max_speed_hz = 10000000
self.isInitialized = True
self._writeRegister(MCP23S17.MCP23S17_IOCON, MCP23S17.IOCON_INIT)
def close(self):
"""Closes the SPI connection that the MCP23S17 component is using.
"""
self.spi.close()
self.isInitialized = False
def setPullupMode(self, pin, mode):
"""Enables or disables the pull-up mode for input pins.
Parameters:
pin -- The pin index (0 - 15)
mode -- The pull-up mode (MCP23S17.PULLUP_ENABLED, MCP23S17.PULLUP_DISABLED)
"""
assert(pin < 16)
assert((mode == MCP23S17.PULLUP_ENABLED)
or (mode == MCP23S17.PULLUP_DISABLED))
assert(self.isInitialized)
if (pin < 8):
register = MCP23S17.MCP23S17_GPPUA
data = self._GPPUA
noshifts = pin
else:
register = MCP23S17.MCP23S17_GPPUB
noshifts = pin & 0x07
data = self._GPPUB
if (mode == MCP23S17.PULLUP_ENABLED):
data |= (1 << noshifts)
else:
data &= (~(1 << noshifts))
self._writeRegister(register, data)
if (pin < 8):
self._GPPUA = data
else:
self._GPPUB = data
def setDirection(self, pin, direction):
"""Sets the direction for a given pin.
Parameters:
pin -- The pin index (0 - 15)
direction -- The direction of the pin (MCP23S17.DIR_INPUT, MCP23S17.DIR_OUTPUT)
"""
assert (pin < 16)
assert ((direction == MCP23S17.DIR_INPUT)
or (direction == MCP23S17.DIR_OUTPUT))
assert(self.isInitialized)
if (pin < 8):
register = MCP23S17.MCP23S17_IODIRA
data = self._IODIRA
noshifts = pin
else:
register = MCP23S17.MCP23S17_IODIRB
noshifts = pin & 0x07
data = self._IODIRB
if (direction == MCP23S17.DIR_INPUT):
data |= (1 << noshifts)
else:
data &= (~(1 << noshifts))
self._writeRegister(register, data)
if (pin < 8):
self._IODIRA = data
else:
self._IODIRB = data
def digitalRead(self, pin):
"""Reads the logical level of a given pin.
Parameters:
pin -- The pin index (0 - 15)
Returns:
- MCP23S17.LEVEL_LOW, if the logical level of the pin is low,
- MCP23S17.LEVEL_HIGH, otherwise.
"""
assert(self.isInitialized)
assert (pin < 16)
if (pin < 8):
self._GPIOA = self._readRegister(MCP23S17.MCP23S17_GPIOA)
if ((self._GPIOA & (1 << pin)) != 0):
return MCP23S17.LEVEL_HIGH
else:
return MCP23S17.LEVEL_LOW
else:
self._GPIOB = self._readRegister(MCP23S17.MCP23S17_GPIOB)
pin &= 0x07
if ((self._GPIOB & (1 << pin)) != 0):
return MCP23S17.LEVEL_HIGH
else:
return MCP23S17.LEVEL_LOW
def digitalWrite(self, pin, level):
"""Sets the level of a given pin.
Parameters:
pin -- The pin idnex (0 - 15)
level -- The logical level to be set (MCP23S17.LEVEL_LOW, MCP23S17.LEVEL_HIGH)
"""
assert(self.isInitialized)
assert(pin < 16)
assert((level == MCP23S17.LEVEL_HIGH) or (level == MCP23S17.LEVEL_LOW))
if (pin < 8):
register = MCP23S17.MCP23S17_GPIOA
data = self._GPIOA
noshifts = pin
else:
register = MCP23S17.MCP23S17_GPIOB
noshifts = pin & 0x07
data = self._GPIOB
if (level == MCP23S17.LEVEL_HIGH):
data |= (1 << noshifts)
else:
data &= (~(1 << noshifts))
self._writeRegister(register, data)
if (pin < 8):
self._GPIOA = data
else:
self._GPIOB = data
def setDirPORTA(self, data):
assert(self.isInitialized)
self._writeRegister(MCP23S17.MCP23S17_IODIRA, data)
self._IODIRA = data
def setDirPORTB(self, data):
assert(self.isInitialized)
self._writeRegister(MCP23S17.MCP23S17_IODIRB, data)
self._IODIRA = data
def setPullupPORTA(self, data):
assert(self.isInitialized)
self._writeRegister(MCP23S17.MCP23S17_GPPUA, data)
self._GPPUA = data
def setPullupPORTB(self, data):
assert(self.isInitialized)
self._writeRegister(MCP23S17.MCP23S17_GPPUB, data)
self._GPPUB = data
def readPORTA(self):
assert(self.isInitialized)
data = self._readRegister(MCP23S17.MCP23S17_GPIOA)
self._GPIOA = data
return data
def readPORTB(self):
assert(self.isInitialized)
data = self._readRegister(MCP23S17.MCP23S17_GPIOB)
self._GPIOB = data
return data
def writePORTA(self,data):
assert(self.isInitialized)
self._writeRegister(MCP23S17.MCP23S17_GPIOA, data)
self._GPIOA = data
def writePORTB(self,data):
assert(self.isInitialized)
self._writeRegister(MCP23S17.MCP23S17_GPIOB, data)
self._GPIOB = data
def writeGPIO(self, data):
"""Sets the data port value for all pins.
Parameters:
data - The 16-bit value to be set.
"""
assert(self.isInitialized)
self._GPIOA = (data & 0xFF)
self._GPIOB = (data >> 8)
self._writeRegisterWord(MCP23S17.MCP23S17_GPIOA, data)
def readGPIO(self):
"""Reads the data port value of all pins.
Returns:
- The 16-bit data port value
"""
assert(self.isInitialized)
data = self._readRegisterWord(MCP23S17.MCP23S17_GPIOA)
self._GPIOA = (data & 0xFF)
self._GPIOB = (data >> 8)
return data
def _writeRegister(self, register, value):
assert(self.isInitialized)
command = MCP23S17.MCP23S17_CMD_WRITE | ((self.deviceID) << 1)
self.spi.xfer2([command, register, value])
def _readRegister(self, register):
assert(self.isInitialized)
command = MCP23S17.MCP23S17_CMD_READ | ((self.deviceID) << 1)
data = self.spi.xfer2([command, register, 0])
return data[2]
def _readRegisterWord(self, register):
assert(self.isInitialized)
buffer = [0, 0]
buffer[0] = self._readRegister(register)
buffer[1] = self._readRegister(register + 1)
return ((buffer[1] << 8) | buffer[0])
def _writeRegisterWord(self, register, data):
assert(self.isInitialized)
self._writeRegister(register, data & 0xFF)
self._writeRegister(register + 1, data >> 8)