Welcome to mcp2210-python’s documentation!

class mcp2210.Mcp2210(serial_number, vendor_id=1240, product_id=222, immediate_gpio_update=True)

This class is used to interface with the MCP2210.

Parameters
  • serial_number (str) – The serial number of the device to connect to (a 10 digit string)

  • vendor_id (int) – The vendor ID of the device (defaults to 0x04d8)

  • product_id (int) – The product ID of the device (defaults to 0x00de)

  • immediate_gpio_update (bool) – If True, immediately send any GPIO configuration changes to the device

A usage example is below.

import time
from mcp2210 import Mcp2210, Mcp2210GpioDesignation, Mcp2210GpioDirection

# To use this example code:
#   connect LEDs to pins 0-4
#   connect MISO to MOSI on the MCP2210 breakout board.

# You can also connect either VCC or GND to pins 5-8.
# Note that when unconnected the pins will read as False.

# connect to the device by serial number
mcp = Mcp2210(serial_number="0000992816")

# this only needs to happen once
# if you don't call this, the device will use the existing settings
mcp.configure_spi_timing(chip_select_to_data_delay=0,
                         last_data_byte_to_cs=0,
                         delay_between_bytes=0)

# set all pins as GPIO
for i in range(9):
    mcp.set_gpio_designation(i, Mcp2210GpioDesignation.GPIO)

# set lower GPIOs to output
for i in range(0, 5):
    mcp.set_gpio_direction(i, Mcp2210GpioDirection.OUTPUT)

# set upper GPIOs to input
for i in range(5, 9):
    mcp.set_gpio_direction(i, Mcp2210GpioDirection.INPUT)
    print("Pin {}:".format(i), mcp.get_gpio_value(i))

# flash an LED
mcp.set_gpio_output_value(0, False)
for i in range(3):
    mcp.set_gpio_output_value(0, True)
    time.sleep(0.5)
    mcp.set_gpio_output_value(0, False)
    time.sleep(0.5)

# LED slider
counter = 0
for _ in range(20):
    counter += 1
    counter %= 5
    for i in range(5):
        mcp.set_gpio_output_value(i, counter == i)
    time.sleep(0.2)

# turn all LEDs off
for i in range(0, 5):
    mcp.set_gpio_output_value(i, False)

# set pin 4 as CS, and transmit the bytes 0 through to 255 inclusive over SPI
mcp.set_gpio_designation(4, Mcp2210GpioDesignation.CHIP_SELECT)
tx_data = bytes(range(256))
rx_data = mcp.spi_exchange(tx_data, cs_pin_number=4)

# as MOSI is connected to MISO, check that the data matches what we sent
assert rx_data == tx_data
configure_spi_timing(chip_select_to_data_delay=None, last_data_byte_to_cs=None, delay_between_bytes=None)

Configure the timing parameters for an SPI transaction. All delays are in 100 microsecond steps. If these delays are not needed, they can be set to 0. Note that there will still be some minimal delays between the events due to the way the device works (typically about 30-40us).

If this function is never called, the delays will be whatever the device was configured with previously.

If None is passed as a parameter, the value will remain unchanged.

Parameters
  • chip_select_to_data_delay (Optional[int]) – Delay between CS assert and first data byte

  • last_data_byte_to_cs (Optional[int]) – Delay between the last data byte and CS de-assert

  • delay_between_bytes (Optional[int]) – Delay between bytes

get_gpio_value(pin_number)

Read the value of a GPIO input.

Parameters

pin_number (int) – The number of the pin of interest

Return type

bool

Returns

The state of the pin

gpio_update()

Updates any GPIO direction/output levels and reads the latest GPIO input data. If the immediate_gpio_update was set to True when this class was constructed, there is no need to call this function. Otherwise, it needs to be called manually each time any GPIO configuration is changed, or when the GPIO inputs are to be read.

set_gpio_designation(pin_number, designation)

Designates a pin as either GPIO, chip select or alternate function.

Parameters
  • pin_number (int) – The number of the pin of interest

  • designation (Mcp2210GpioDesignation) – The designation to set

set_gpio_direction(pin_number, direction)

Configures a GPIO as either input or output

Parameters
  • pin_number (int) – The number of the pin of interest

  • direction (Mcp2210GpioDirection) – The direction to set

set_gpio_output_value(pin_number, value)

Sets the value of a GPIO output

Parameters
  • pin_number (int) – The number of the pin of interest

  • value (bool) – The value to write to the pin

set_spi_mode(mode)

Sets the SPI mode of the device

Parameters

mode (int) – the SPI mode (0, 1, 2 or 3)

spi_exchange(payload, cs_pin_number)

Performs an SPI exchange. Note that the pin corresponding to the number provided must already be designated as a CS pin using set_gpio_designation().

Parameters
  • payload (bytes) – The bytes to send in the transaction

  • cs_pin_number (int) – The pin number which is to be used as CS.

Return type

bytes

Returns

The bytes which were received as part of the exchange

class mcp2210.Mcp2210AccessControl(value)

MCP2210 password state. Currently unused and only present for compatibility.

NOT_PROTECTED = 0
PASSWORD_PROTECTED = 64
PERMANENTLY_LOCKED = 128
exception mcp2210.Mcp2210CommandFailedException

Indicates that the device either reported a command failure, or returned some unexpected data.

exception mcp2210.Mcp2210CommandResponseDesyncException

The MCP2210 returned a command response that didn’t correspond to a request.

class mcp2210.Mcp2210CommandResult(value)

Results codes for the MCP2210.

SPI_DATA_NOT_ACCEPTED = 247
SUCCESS = 0
TRANSFER_IN_PROGRESS = 248
class mcp2210.Mcp2210Commands(value)

Command codes for the MCP2210

GET_GPIO_PIN_DIRECTION = 51
GET_GPIO_PIN_VALUE = 49
GET_GPIO_SETTINGS = 32
GET_SPI_TRANSFER_SETTINGS = 65
GET_STATUS = 16
REQUEST_SPI_BUS_RELEASE = 128
SET_GPIO_PIN_DIRECTION = 50
SET_GPIO_PIN_VALUE = 48
SET_GPIO_SETTINGS = 33
SET_SPI_TRANSFER_SETTINGS = 64
TRANSFER_SPI_DATA = 66
class mcp2210.Mcp2210GpioDesignation(value)

The designations which can be applied to a pin.

CHIP_SELECT = 1
DEDICATED_FUNCTION = 2
GPIO = 0
class mcp2210.Mcp2210GpioDirection(value)

GPIO direction codes.

INPUT = 1
OUTPUT = 0
class mcp2210.Mcp2210InterruptCountingMode(value)

MCP2210 interrupt counting modes. Currently unused and only present for compatibility.

COUNT_FALLING_EDGES = 1
COUNT_HIGH_PULSES = 4
COUNT_LOW_PULSES = 3
COUNT_RISING_EDGES = 2
NO_COUNTING = 0
exception mcp2210.Mcp2210SpiBusLockedException

An external master is locking the SPI bus.

class mcp2210.Mcp2210SpiTransferStatus(value)

Codes which indicate the current state of an SPI transfer.

SPI_TRANSFER_COMPLETE = 16
SPI_TRANSFER_PENDING_NO_RECEIVED_DATA = 32
SPI_TRANSFER_PENDING_RECEIVED_DATA_AVAILABLE = 48
exception mcp2210.Mcp2210UsbBusyException

A USB transaction is in progress, so the MCP2210 cannot execute the command.

mcp2210.bytes_to_hex_string(data)

Converts a bytes object into a string of hex characters. For example, b”\x00\x01” becomes “00 01”.

Parameters

data (bytes) – bytes object

Return type

str

Returns

a hex string of the bytes

Indices and tables