Skip to content

Add SPI (MCP23SXX) devices #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Introduction
:target: https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx/actions/
:alt: Build Status

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

Dependencies
=============
Expand Down
3 changes: 2 additions & 1 deletion adafruit_mcp230xx/mcp23017.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,13 @@ def io_control(self):
Bit 4 is whether SDA slew rate is enabled (1 = yes). Bit 5 is if I2C
address pointer auto-increments (1 = no). Bit 6 is whether interrupt
pins are internally connected (1 = yes). Bit 7 is whether registers
are all in one bank (1 = no).
are all in one bank (1 = no), this is silently ignored if set to ``1``.
"""
return self._read_u8(_MCP23017_IOCON)

@io_control.setter
def io_control(self, val):
val &= ~0x80
self._write_u8(_MCP23017_IOCON, val)

@property
Expand Down
30 changes: 16 additions & 14 deletions adafruit_mcp230xx/mcp230xx.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Carter Nelson
# SPDX-FileCopyrightText: 2021 Red_M
#
# SPDX-License-Identifier: MIT

Expand All @@ -9,10 +10,10 @@

CircuitPython module for the MCP23017 and MCP23008 I2C I/O extenders.

* Author(s): Tony DiCola
* Author(s): Tony DiCola, Red_M (2021)
"""

from adafruit_bus_device import i2c_device
from .mcp23xxx import MCP23XXX

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


# pylint: disable=too-few-public-methods
class MCP230XX:
class MCP230XX(MCP23XXX):
"""Base class for MCP230xx devices."""

def __init__(self, i2c, address):
self._device = i2c_device.I2CDevice(i2c, address)

def _read_u16le(self, register):
# Read an unsigned 16 bit little endian value from the specified 8-bit
# register.
with self._device as i2c:
with self._device as bus_device:
_BUFFER[0] = register & 0xFF

i2c.write_then_readinto(_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=3)
bus_device.write_then_readinto(
_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=3
)
return (_BUFFER[2] << 8) | _BUFFER[1]

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

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

i2c.write_then_readinto(_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=2)
bus_device.write_then_readinto(
_BUFFER, _BUFFER, out_end=1, in_start=1, in_end=2
)
return _BUFFER[1]

def _write_u8(self, register, val):
# Write an 8 bit value to the specified 8-bit register.
with self._device as i2c:
with self._device as bus_device:
_BUFFER[0] = register & 0xFF
_BUFFER[1] = val & 0xFF
i2c.write(_BUFFER, end=2)
bus_device.write(_BUFFER, end=2)
94 changes: 94 additions & 0 deletions adafruit_mcp230xx/mcp23s08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Carter Nelson
# SPDX-FileCopyrightText: 2021 Red_M
#
# SPDX-License-Identifier: MIT

"""
`MCP23S08`
====================================================

CircuitPython module for the MCP23S08 I2C I/O extenders.

* Author(s): Tony DiCola, Romy Bompart (2020), Red_M (2021)
"""

from micropython import const
from .mcp23sxx import MCP23SXX
from .digital_inout import DigitalInOut

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP23Sxx.git"

_MCP23S08_ADDRESS = const(0x20)
_MCP23S08_IODIR = const(0x00)
_MCP23S08_IPOL = const(0x01)
_MCP23S08_GPINTEN = const(0x02)
_MCP23S08_DEFVAL = const(0x03)
_MCP23S08_INTCON = const(0x04)
_MCP23S08_IOCON = const(0x05)
_MCP23S08_GPPU = const(0x06)
_MCP23S08_INTF = const(0x07)
_MCP23S08_INTCAP = const(0x08)
_MCP23S08_GPIO = const(0x09)

# pylint: disable=too-many-arguments
class MCP23S08(MCP23SXX):
"""Supports MCP23S08 instance on specified I2C bus and optionally
at the specified I2C address.
"""

def __init__(
self, spi, chip_select, address=_MCP23S08_ADDRESS, reset=True, baudrate=100000
):
super().__init__(spi, address, chip_select, baudrate=baudrate)
# For user information
self.address = address
if reset:
# Reset to all inputs with no pull-ups and no inverted polarity.
self.iodir = 0xFF
self.gppu = 0x00
self._write_u8(_MCP23S08_IPOL, 0x00)

@property
def gpio(self):
"""The raw GPIO output register. Each bit represents the
output value of the associated pin (0 = low, 1 = high), assuming that
pin has been configured as an output previously.
"""
return self._read_u8(_MCP23S08_GPIO)

@gpio.setter
def gpio(self, val):
self._write_u8(_MCP23S08_GPIO, val)

@property
def iodir(self):
"""The raw IODIR direction register. Each bit represents
direction of a pin, either 1 for an input or 0 for an output mode.
"""
return self._read_u8(_MCP23S08_IODIR)

@iodir.setter
def iodir(self, val):
self._write_u8(_MCP23S08_IODIR, val)

@property
def gppu(self):
"""The raw GPPU pull-up register. Each bit represents
if a pull-up is enabled on the specified pin (1 = pull-up enabled,
0 = pull-up disabled). Note pull-down resistors are NOT supported!
"""
return self._read_u8(_MCP23S08_GPPU)

@gppu.setter
def gppu(self, val):
self._write_u8(_MCP23S08_GPPU, val)

def get_pin(self, pin):
"""Convenience function to create an instance of the DigitalInOut class
pointing at the specified pin of this MCP23S08 device.
"""
if not 0 <= pin <= 7:
raise ValueError("Pin number must be 0-7.")
return DigitalInOut(pin, self)
Loading