mcp23018.h

Helper module for controlling an MCP23018 I/O expander via I2C.  Depends on the i2chelper.h module.

NOTE: If the macro MCP23018_BANK_MODE is defined, the I/O expander is assumed to operate in banked mode (i.e. all registers for the same I/O port are located together in the expander’s register address space).  If MCP23018_BANK_MODE is NOT defined, the I/O expander is assumed to operate in paired mode (i.e. all registers of the same type are located together).  Consult your expander’s datasheet for details on the different addressing modes.

Summary
mcp23018.hHelper module for controlling an MCP23018 I/O expander via I2C.
Macros
MCP23018_MAX_PAIRThe largest valid register pair address.
MCP23018_MAX_BANKThe largest valid register bank address.
MCP23018_REG_MASKBit mask for the valid bits of an MCP23018 register address.
Enumerations
MCP23018 IOCON Bit NumbersAn MCP23018 can be reconfigured by setting these bits in the IOCON register.
MCP23018 Register AddressesThese registers are used to configure and perform I/O with an MCP23018 via I2C.
Variables
mcp23018_regThe type of MCP23018 register addresses.
Functions
mcp23018_is_valid_regRegister address validator.
mcp23018_begin_readStarts an asynchronous register read operation.
mcp23018_poll_readTests whether there is an ongoing I2C operation.
mcp23018_readPerforms a synchronous register read operation.
mcp23018_begin_writeStarts an asynchronous register write operation.
mcp23018_writePerforms a synchronous register write operation.

Macros

MCP23018_MAX_PAIR

The largest valid register pair address.  Constant macro.

MCP23018_MAX_BANK

The largest valid register bank address.  Constant macro.

MCP23018_REG_MASK

Bit mask for the valid bits of an MCP23018 register address.  Constant macro.

Enumerations

MCP23018 IOCON Bit Numbers

An MCP23018 can be reconfigured by setting these bits in the IOCON register.

MCP23018_INTCCinterrupt clearing control, 0: clear on GPIO read, 1: clear on INTCAP read
MCP23018_INTPOLinterrupt output polarity, 0: active-low, 1: active-high
MCP23018_ODRopen-drain interrupt output enable, 0: active driver, 1: open-drain
MCP23018_SEQOPsequential operation disable, 0: address auto-incremented, 1: address retained
MCP23018_MIRRORmirrored interrupt output enable, 0: outputs separate, 1: outputs mirrored
MCP23018_BANKregister bank mode enable, 0: registers paired, 1: registers in banks

MCP23018 Register Addresses

These registers are used to configure and perform I/O with an MCP23018 via I2C.

NOTE: The IODIRx registers are set to all ones (i.e. all inputs) on startup/reset.  All other registers are zeroed on startup/reset.

NOTE: Entries where the register name ends with an ‘x’ describe two registers, one for the ‘A’ port and one for the ‘B’ port, named correspondingly.

MCP23018_IODIRx[RW] I/O direction, 0: output, 1: input
MCP23018_IPOLx[RW] input polarity, 0: same, 1: inverted
MCP23018_GPINTENx[RW] interrupt trigger enable, 0: disabled, 1: enabled
MCP23018_DEFVALx[RW] reference value for level-triggered interrupts
MCP23018_INTCONx[RW] interrupt mode select, 0: edge-triggered, 1: level-triggered
MCP23018_IOCON[RW] configuration register
MCP23018_GPPUx[RW] I/O pull-up enable, 0: disabled, 1: enabled
MCP23018_INTFx[R] interrupt flag register, 0: no interrupt, 1: interrupt pending
MCP23018_INTCAPx[R] interrupt capture register
MCP23018_GPIOx[RW] I/O port register
MCP23018_OLATx[RW] output latch register
MCP23018_NOT_A_REGguaranteed to NOT be a valid register address

Variables

mcp23018_reg

typedef uint8_t mcp23018_reg

The type of MCP23018 register addresses.

NOTE: Bits that are set to zero in MCP23018_REG_MASK are zero in all valid register addresses.

Functions

mcp23018_is_valid_reg

uint8_t mcp23018_is_valid_reg(mcp23018_reg r)

Register address validator.

Parameters

rA register address.

Returns

A true value if and only if the argument is a valid MCP23018 register address in the selected addressing mode (i.e. paired or banked).

mcp23018_begin_read

i2c_state mcp23018_begin_read(i2c_slave_addr addr,
mcp23018_reg r)

Starts an asynchronous register read operation.  This function delegates to i2chelper_request and completion of the register read can be detected as described in the documentation of that function.

Parameters

addr7-bit I2C slave address of the target device.  The address MUST be stored in the 7 least significant bits of the argument and MUST NOT include an R/W control bit.
rAddress of the register to read.

Returns

I2C_ACTIVE if and only if the register read operation was successfully started, otherwise an error code.

mcp23018_poll_read

i2c_state mcp23018_poll_read(uint8_t *v)

Tests whether there is an ongoing I2C operation.  If there is no ongoing operation, this function will output the most recently read MCP23018 register value.

CAUTION: This function does not check whether any MCP23018 register read operation has actually been started.

Parameters

vPointer to memory where the most recently read register value should be stored.  This output parameter is updated only when the returned i2c_state is I2C_READY.

Returns

An i2c_state value representing the current state of the I2C module.

mcp23018_read

i2c_state mcp23018_read(i2c_slave_addr addr,
mcp23018_reg r,
uint8_t *v)

Performs a synchronous register read operation.

Parameters

addr7-bit I2C slave address of the target device.  The address MUST be stored in the 7 least significant bits of the argument and MUST NOT include an R/W control bit.
rAddress of the register to read.
vPointer to memory where the result of the read register operation should be stored.  This output parameter is updated only when the returned i2c_state is I2C_READY.

Returns

I2C_READY if and only if the register read operation was successfully performed, otherwise an error code.

mcp23018_begin_write

i2c_state mcp23018_begin_write(i2c_slave_addr addr,
mcp23018_reg r,
uint8_t v)

Starts an asynchronous register write operation.  This function delegates to i2chelper_request and completion of the register write can be detected as described in the documentation of that function.

Parameters

addr7-bit I2C slave address of the target device.  The address MUST be stored in the 7 least significant bits of the argument and MUST NOT include an R/W control bit.
rAddress of the register to write to.
vValue to write to the target register.

Returns

I2C_ACTIVE if and only if the register read operation was successfully started, otherwise an error code.

mcp23018_write

i2c_state mcp23018_write(i2c_slave_addr addr,
mcp23018_reg r,
uint8_t v)

Performs a synchronous register write operation.

Parameters

addr7-bit I2C slave address of the target device.  The address MUST be stored in the 7 least significant bits of the argument and MUST NOT include an R/W control bit.
rAddress of the register to write to.
vValue to write to the target register.

Returns

I2C_READY if and only if the register write operation was successfully performed, otherwise an error code.

typedef uint8_t mcp23018_reg
The type of MCP23018 register addresses.
uint8_t mcp23018_is_valid_reg(mcp23018_reg r)
Register address validator.
i2c_state mcp23018_begin_read(i2c_slave_addr addr,
mcp23018_reg r)
Starts an asynchronous register read operation.
i2c_state mcp23018_poll_read(uint8_t *v)
Tests whether there is an ongoing I2C operation.
i2c_state mcp23018_read(i2c_slave_addr addr,
mcp23018_reg r,
uint8_t *v)
Performs a synchronous register read operation.
i2c_state mcp23018_begin_write(i2c_slave_addr addr,
mcp23018_reg r,
uint8_t v)
Starts an asynchronous register write operation.
i2c_state mcp23018_write(i2c_slave_addr addr,
mcp23018_reg r,
uint8_t v)
Performs a synchronous register write operation.
Asynchronous, interrupt-driven I2C (aka TWI) helper module.
Bit mask for the valid bits of an MCP23018 register address.
i2c_state i2chelper_request(i2c_slave_addr addr,
uint8_t n_out,
volatile const uint8_t *bfr_out,
uint8_t n_in,
volatile uint8_t *bfr_in)
Initiates an I2C request operation consisting of a transmit phase followed by a receive phase.
module is currently performing an operation
typedef uint8_t i2c_state
The type of state and result codes for the I2C module.
module is idle and ready to begin a new operation
Close