A GPIO Expander/Extender is a device that provides a designer the ability to implement additional inputs and outputs (I/O).

GPIO Expanders

MCP23017/MCP23S17

Link

class pidevices.MCP23x17

Class representing mcp23x17 chips.

Steps for interrupts:
  • Enable interrupt on pin through GPINTEN register

  • Define which type of signal will cause the interrupt through registers INTCON and DEFVAL

  • When an interrupt occur the bit in INT register is set.

  • The interrupt bit remains active until the intcap register(the value of gpio when the interrupt occured) or the gpio register is read.

  • The first interrupt event causes the port contents to becopied into the INTCAP register. Subsequent interruptconditions on the port will not cause an interrupt tooccur as long as the interrupt is not cleared by a readof INTCAP or GPIO.

Seq and banks:

The module supports byte mode which doesn’t increment the register counter. If has byte mode enable and bank 0 then the counter toggles between the two registers A,B.

set_pin_debounce(pin_num, value)

Set the debounce time for a pin.

Parameters
  • pin_num (str) – The pin number, it must be in the form of A_x or B_x.

  • value (int) – The debounce time in ms.

Raises

TypeError – Error when the type of value is not int.

set_int_handl_func(pin_num, func, *args)

Set interrupt handling function for a pin

Parameters
  • pin_num (str) – The pin number, it must be in the form of A_x or B_x.

  • func – The function to be called when the interrupt occur.

  • *args – The arguments of func.

poll_int_async(pin_nums)

Async polling of interrupt flags.

stop_poll_int_async()

Stop async polling

set_pin_dir(pin_num, function)

Set pin direction

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • function – Boolean, it could be 1 for input and 0 for output.

get_pin_dir(pin_num)

Get pin direction

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

set_pin_pol(pin_num, polarity)

Set pin polarity

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • polarity (boolean) – It could be 1 for reverse and 0 for same.

get_pin_pol(pin_num)

Get pin polarity

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

An integer indicating the polarity of the pin. 0 is for same and 1 for reverse.

set_pin_int(pin_num, interrupt)

Set pin interrupt on change.

In order to work the DEFVAL and INTCON registers must be set.

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • interrupt – Boolean representing the interrupt status of the pin.

get_pin_int(pin_num)

Get pin interrupt on change.

In order to work the DEFVAL and INTCON registers must be set.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

An integer indicating the interrupt status of the pin.

set_pin_def_val(pin_num, def_val)

Set pin default value for comparison.

The value of each bits will be compared with the value of the associate pin and if they are different then an interrupt will happen.

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • def_val – Int representing the compare value. Should be 0 or 1.

get_pin_def_val(pin_num)

Get pin default value for comparison.

The value of each bits will be compared with the value of the associate pin and if they are different then an interrupt will happen.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

Int representing the compare value. Should be 0 or 1.

set_pin_intcon(pin_num, value)

Set pin intcon value.

If the corresponding pin’s bit is set the the value is compared with the associate bit in the DEFVAL register. Else is compared against the previous value.

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • value – Int representing the value. Should be 0 or 1.

get_pin_intcon(pin_num)

Get pin intcon value.

If the corresponding pin’s bit is set the the value is compared with the associate bit in the DEFVAL register. Else is compared against the previous value.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

Int representing the value. Should be 0 or 1.

set_bank(value)

Set bank bit.

It changes the registers mapping. Currenty it only sets it to 0.

Parameters

value – Int represents the value.

get_bank()

Get bank bit.

It changes the registers mapping. Currenty it only sets it to 0.

Returns

Int represents the value.

set_mirror(value)

Set mirror bit.

If it is set the INTn pins are functionally OR’ed.

Parameters

value – Int represents the value.

get_mirror()

Get mirror bit.

If it is set the INTn pins are functionally OR’ed.

Returns

Int represents the value.

set_seqop(value)

Set SEQOP bit.

It changes the sequential operation. It is usefull for polling

Parameters

value – Int represents the value.

get_seqop()

Get SEQOP bit.

It changes the sequential operation. It is usefull for polling

Returns

Int represents the value.

set_disslw(value)

Set DISSLW bit.

It controls the slew rate of SDA pin

Parameters

value – Int represents the value.

get_disslw()

Get DISSLW bit.

It controls the slew rate of SDA pin

Returns

Int represents the value.

set_haen(value)

It is usefull only in the mcp23s17.

set_odr(value)

Set ODR bit.

It enables the int pin for open drain configuration. It overrides the INTPOL bit.

Parameters

value – Int represents the value.

get_odr()

Get ODR bit.

It enables the int pin for open drain configuration. It overrides the INTPOL bit.

Returns

Int represents the value.

set_intpol(value)

Set INTPOL bit.

It sets the polarity of the INT pin.

Parameters

value – Int represents the value.

get_intpol()

Get INTPOL bit.

It sets the polarity of the INT pin.

Returns

Int represents the value.

set_pin_pull_up(pin_num, pull)

Set the pull up of a pin.

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • pull (boolean) – It could be 0 for down and 1 for up.

get_pin_pull_up(pin_num)

Get the pull up of a pin.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

Int indicating the pin pull up resistor could be 0 for down and 1 for up.

get_intf(pin_num)

Get the pin interrupt flag.

It reflects if the pin caused the interrupt.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

The flag value.

get_mult_intf(pin_num)

Get the pin interrupt flag with more bytes

It reflects if the pin caused the interrupt.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

A list with the flag value.

get_intcap(pin_num)

Get the pin’s state when the interrupt occured.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

The flag value.

read(pin_num)

Read the pins state.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

The pin’s state.

write(pin_num, value)

Write to the pin

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • value – Int could be 0 or 1.

read_olat(pin_num)

Read the olat register.

Parameters

pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

write_olat(pin_num, value)

Write to the pin olat

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

  • value – Int could be 0 or 1.

class pidevices.MCP23017(bus, address)

Class representing mcp23017 chip

Parameters
  • bus (int) – The i2c bus

  • address (int) – The hardware defined address of the module.

property bus

The i2c bus.

property address

The address of the module. First it must be defined by hardware.

start()

Init hardware and os resources.

clear_ints()

Disable interrupts on every pin.

poll_int(pin_nums)

Poll the interrupt bit for the specified pin.

Parameters

pin_nums (list) – List with the pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number. See modules’s datasheet.

Returns

List of integers indicating if an interrupt occured at the specified pin.

wait_pin_for_edge(pin_num, timeout=None)

Wait for an edge signal on a pin.

Parameters
  • pin_num (str) – The pin number in format A_x or B_x, where A/B is the pin-chunk and x is the number.

  • timeout (int) – The time of waiting in ms. If it is none will wait until the edge signal occur. Defaults to None.

Returns

An integer indicating if the interrupt occured.

stop()

Free hardware and os resources.