Skip to content

Commit 63c459d

Browse files
authored
Merge pull request #41 from Red-M/master
Add SPI (MCP23SXX) devices
2 parents 05f0acc + f8ddbe0 commit 63c459d

File tree

11 files changed

+668
-16
lines changed

11 files changed

+668
-16
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Introduction
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx/actions/
1414
:alt: Build Status
1515

16-
CircuitPython module for the MCP23017 and MCP23008 I2C I/O extenders.
16+
CircuitPython module for the MCP23017/08 I2C and MCP23S17/08 SPI I/O extenders.
1717

1818
Dependencies
1919
=============

adafruit_mcp230xx/mcp23017.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,13 @@ def io_control(self):
253253
Bit 4 is whether SDA slew rate is enabled (1 = yes). Bit 5 is if I2C
254254
address pointer auto-increments (1 = no). Bit 6 is whether interrupt
255255
pins are internally connected (1 = yes). Bit 7 is whether registers
256-
are all in one bank (1 = no).
256+
are all in one bank (1 = no), this is silently ignored if set to ``1``.
257257
"""
258258
return self._read_u8(_MCP23017_IOCON)
259259

260260
@io_control.setter
261261
def io_control(self, val):
262+
val &= ~0x80
262263
self._write_u8(_MCP23017_IOCON, val)
263264

264265
@property

adafruit_mcp230xx/mcp230xx.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
22
# SPDX-FileCopyrightText: 2019 Carter Nelson
3+
# SPDX-FileCopyrightText: 2021 Red_M
34
#
45
# SPDX-License-Identifier: MIT
56

@@ -9,10 +10,10 @@
910
1011
CircuitPython module for the MCP23017 and MCP23008 I2C I/O extenders.
1112
12-
* Author(s): Tony DiCola
13+
* Author(s): Tony DiCola, Red_M (2021)
1314
"""
1415

15-
from adafruit_bus_device import i2c_device
16+
from .mcp23xxx import MCP23XXX
1617

1718
__version__ = "0.0.0-auto.0"
1819
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
@@ -24,41 +25,42 @@
2425

2526

2627
# pylint: disable=too-few-public-methods
27-
class MCP230XX:
28+
class MCP230XX(MCP23XXX):
2829
"""Base class for MCP230xx devices."""
2930

30-
def __init__(self, i2c, address):
31-
self._device = i2c_device.I2CDevice(i2c, address)
32-
3331
def _read_u16le(self, register):
3432
# Read an unsigned 16 bit little endian value from the specified 8-bit
3533
# register.
36-
with self._device as i2c:
34+
with self._device as bus_device:
3735
_BUFFER[0] = register & 0xFF
3836

39-
i2c.write_then_readinto(_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=3)
37+
bus_device.write_then_readinto(
38+
_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=3
39+
)
4040
return (_BUFFER[2] << 8) | _BUFFER[1]
4141

4242
def _write_u16le(self, register, val):
4343
# Write an unsigned 16 bit little endian value to the specified 8-bit
4444
# register.
45-
with self._device as i2c:
45+
with self._device as bus_device:
4646
_BUFFER[0] = register & 0xFF
4747
_BUFFER[1] = val & 0xFF
4848
_BUFFER[2] = (val >> 8) & 0xFF
49-
i2c.write(_BUFFER, end=3)
49+
bus_device.write(_BUFFER, end=3)
5050

5151
def _read_u8(self, register):
5252
# Read an unsigned 8 bit value from the specified 8-bit register.
53-
with self._device as i2c:
53+
with self._device as bus_device:
5454
_BUFFER[0] = register & 0xFF
5555

56-
i2c.write_then_readinto(_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=2)
56+
bus_device.write_then_readinto(
57+
_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=2
58+
)
5759
return _BUFFER[1]
5860

5961
def _write_u8(self, register, val):
6062
# Write an 8 bit value to the specified 8-bit register.
61-
with self._device as i2c:
63+
with self._device as bus_device:
6264
_BUFFER[0] = register & 0xFF
6365
_BUFFER[1] = val & 0xFF
64-
i2c.write(_BUFFER, end=2)
66+
bus_device.write(_BUFFER, end=2)

adafruit_mcp230xx/mcp23s08.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2019 Carter Nelson
3+
# SPDX-FileCopyrightText: 2021 Red_M
4+
#
5+
# SPDX-License-Identifier: MIT
6+
7+
"""
8+
`MCP23S08`
9+
====================================================
10+
11+
CircuitPython module for the MCP23S08 I2C I/O extenders.
12+
13+
* Author(s): Tony DiCola, Romy Bompart (2020), Red_M (2021)
14+
"""
15+
16+
from micropython import const
17+
from .mcp23sxx import MCP23SXX
18+
from .digital_inout import DigitalInOut
19+
20+
__version__ = "0.0.0-auto.0"
21+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP23Sxx.git"
22+
23+
_MCP23S08_ADDRESS = const(0x20)
24+
_MCP23S08_IODIR = const(0x00)
25+
_MCP23S08_IPOL = const(0x01)
26+
_MCP23S08_GPINTEN = const(0x02)
27+
_MCP23S08_DEFVAL = const(0x03)
28+
_MCP23S08_INTCON = const(0x04)
29+
_MCP23S08_IOCON = const(0x05)
30+
_MCP23S08_GPPU = const(0x06)
31+
_MCP23S08_INTF = const(0x07)
32+
_MCP23S08_INTCAP = const(0x08)
33+
_MCP23S08_GPIO = const(0x09)
34+
35+
# pylint: disable=too-many-arguments
36+
class MCP23S08(MCP23SXX):
37+
"""Supports MCP23S08 instance on specified I2C bus and optionally
38+
at the specified I2C address.
39+
"""
40+
41+
def __init__(
42+
self, spi, chip_select, address=_MCP23S08_ADDRESS, reset=True, baudrate=100000
43+
):
44+
super().__init__(spi, address, chip_select, baudrate=baudrate)
45+
# For user information
46+
self.address = address
47+
if reset:
48+
# Reset to all inputs with no pull-ups and no inverted polarity.
49+
self.iodir = 0xFF
50+
self.gppu = 0x00
51+
self._write_u8(_MCP23S08_IPOL, 0x00)
52+
53+
@property
54+
def gpio(self):
55+
"""The raw GPIO output register. Each bit represents the
56+
output value of the associated pin (0 = low, 1 = high), assuming that
57+
pin has been configured as an output previously.
58+
"""
59+
return self._read_u8(_MCP23S08_GPIO)
60+
61+
@gpio.setter
62+
def gpio(self, val):
63+
self._write_u8(_MCP23S08_GPIO, val)
64+
65+
@property
66+
def iodir(self):
67+
"""The raw IODIR direction register. Each bit represents
68+
direction of a pin, either 1 for an input or 0 for an output mode.
69+
"""
70+
return self._read_u8(_MCP23S08_IODIR)
71+
72+
@iodir.setter
73+
def iodir(self, val):
74+
self._write_u8(_MCP23S08_IODIR, val)
75+
76+
@property
77+
def gppu(self):
78+
"""The raw GPPU pull-up register. Each bit represents
79+
if a pull-up is enabled on the specified pin (1 = pull-up enabled,
80+
0 = pull-up disabled). Note pull-down resistors are NOT supported!
81+
"""
82+
return self._read_u8(_MCP23S08_GPPU)
83+
84+
@gppu.setter
85+
def gppu(self, val):
86+
self._write_u8(_MCP23S08_GPPU, val)
87+
88+
def get_pin(self, pin):
89+
"""Convenience function to create an instance of the DigitalInOut class
90+
pointing at the specified pin of this MCP23S08 device.
91+
"""
92+
if not 0 <= pin <= 7:
93+
raise ValueError("Pin number must be 0-7.")
94+
return DigitalInOut(pin, self)

0 commit comments

Comments
 (0)