Skip to content

Commit

Permalink
drivers/mcp23x17: add support for MCP23x17 I/O expanders
Browse files Browse the repository at this point in the history
  • Loading branch information
gschorcht committed Dec 5, 2021
1 parent 0646862 commit 297b513
Show file tree
Hide file tree
Showing 9 changed files with 1,677 additions and 0 deletions.
4 changes: 4 additions & 0 deletions drivers/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ ifneq (,$(filter mrf24j40m%,$(USEMODULE)))
USEMODULE += mrf24j40
endif

ifneq (,$(filter mcp23%17 mcp23x17_%,$(USEMODULE)))
USEMODULE += mcp23x17
endif

ifneq (,$(filter mtd_%,$(USEMODULE)))
USEMODULE += mtd
endif
Expand Down
689 changes: 689 additions & 0 deletions drivers/include/mcp23x17.h

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions drivers/mcp23x17/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
38 changes: 38 additions & 0 deletions drivers/mcp23x17/Makefile.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FEATURES_REQUIRED += periph_gpio

ifneq (,$(filter mcp23s17,$(USEMODULE)))
USEMODULE += mcp23x17_spi
endif

ifneq (,$(filter mcp23017,$(USEMODULE)))
USEMODULE += mcp23x17_i2c
endif

ifneq (,$(filter mcp23x17_i2c,$(USEMODULE)))
FEATURES_REQUIRED += periph_i2c
endif

ifneq (,$(filter mcp23x17_spi,$(USEMODULE)))
FEATURES_REQUIRED += periph_spi
endif

_MCP23X17_IRQ_MODULE := $(filter mcp23x17_irq_%,$(USEMODULE))
ifneq (,$(_MCP23X17_IRQ_MODULE))
# pull in the correspondant event_thread_<priority> module
USEMODULE += $(_MCP23X17_IRQ_MODULE:mcp23x17_irq_%=event_thread_%)
USEMODULE += mcp23x17_irq
else
ifneq (,$(filter mcp23x17_irq,$(USEMODULE)))
# pull in the event_thread_medium module by default if mcp23x17_irq is used.
USEMODULE += mcp23x17_irq_medium
endif
endif

ifneq (,$(filter mcp23x17_irq,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio_irq
endif

ifneq (,$(filter mcp23x17_reset,$(USEMODULE)))
USEMODULE += ztimer
USEMODULE += ztimer_msec
endif
12 changes: 12 additions & 0 deletions drivers/mcp23x17/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# variants of MCP27x17
PSEUDOMODULES += mcp23017
PSEUDOMODULES += mcp23s17
PSEUDOMODULES += mcp23x17_i2c
PSEUDOMODULES += mcp23x17_spi
PSEUDOMODULES += mcp23x17_irq
PSEUDOMODULES += mcp23x17_irq_medium
PSEUDOMODULES += mcp23x17_irq_highest
PSEUDOMODULES += mcp23x17_reset

USEMODULE_INCLUDES_mcp23x17 := $(LAST_MAKEFILEDIR)/include
USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_mcp23x17)
174 changes: 174 additions & 0 deletions drivers/mcp23x17/include/mcp23x17_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright (C) 2021 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup drivers_mcp23x17
* @{
*
* @file
* @brief Default configuration for Microchip MCP23x17 I/O expanders
*
* @author Gunar Schorcht <gunar@schorcht.net>
*/

#ifndef MCP23X17_PARAMS_H
#define MCP23X17_PARAMS_H

#include "board.h"
#include "mcp23x17.h"
#include "saul_reg.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @name Set default hardware configuration parameters
* @{
*/

#if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN

#ifndef MCP23X17_PARAM_SPI_ADDR
/**
* Default hardware address, if the SPI interface is used.
* The use of hardware addressing also for SPI devices allows the use of
* up to eight SPI devices with the same CS signal.
*/
#define MCP23X17_PARAM_SPI_ADDR (0)
#endif

#ifndef MCP23X17_PARAM_SPI_DEV
/** Default SPI device, if the SPI interface is used */
#define MCP23X17_PARAM_SPI_DEV (SPI_DEV(0))
#endif

#ifndef MCP23X17_PARAM_SPI_CLK
/** Default SPI clock frequency, if the SPI interface is used */
#define MCP23X17_PARAM_SPI_CLK (SPI_CLK_10MHZ)
#endif

#ifndef MCP23X17_PARAM_SPI_CS
/** Default SPI CS signal, if the SPI interface is used */
#define MCP23X17_PARAM_SPI_CS (GPIO_PIN(0, 0))
#endif

#ifndef MCP23X17_PARAM_SPI_INT
/** Default MCU pin for INTA and INTB signal of the default SPI device */
#define MCP23X17_PARAM_SPI_INT (GPIO_PIN(0, 1))
#endif

#endif /* MODULE_MCP23X17_SPI || DOXYGEN */

#if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN

#ifndef MCP23X17_PARAM_I2C_ADDR
/** Default hardware address, if the I2C interface is used */
#define MCP23X17_PARAM_I2C_ADDR (0)
#endif

#ifndef MCP23X17_PARAM_I2C_DEV
/** Default I2C device, if the I2C interface is used */
#define MCP23X17_PARAM_I2C_DEV (I2C_DEV(0))
#endif

#ifndef MCP23X17_PARAM_I2C_INT
/** Default MCU pin for INTA and INTB signal of the default I2C device */
#define MCP23X17_PARAM_I2C_INT (GPIO_PIN(0, 2))
#endif

#endif /* MODULE_MCP23X17_I2C || DOXYGEN */

#ifndef MCP23X17_PARAM_RESET_PIN
/** MCU pin for RESET signal is undefinded by default */
#define MCP23X17_PARAM_RESET_PIN (GPIO_UNDEF)
#endif

#if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
#ifndef MCP23X17_SPI_PARAMS
/** Default device parameters, if SPI interface is used */
#define MCP23X17_SPI_PARAMS { \
.addr = MCP23X17_PARAM_SPI_ADDR, \
.int_pin = MCP23X17_PARAM_SPI_INT, \
.reset_pin = MCP23X17_PARAM_RESET_PIN, \
.if_params.type = MCP23X17_SPI, \
.if_params.spi.dev = MCP23X17_PARAM_SPI_DEV, \
.if_params.spi.cs = MCP23X17_PARAM_SPI_CS, \
.if_params.spi.clk = MCP23X17_PARAM_SPI_CLK, \
}
#endif /* MCP23X17_SPI_PARAMS */
#endif /* MODULE_MCP23X17_SPI || DOXYGEN */

#if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
#ifndef MCP23X17_I2C_PARAMS
/** Default device parameters, if I2C interface is used */
#define MCP23X17_I2C_PARAMS { \
.addr = MCP23X17_PARAM_I2C_ADDR, \
.int_pin = MCP23X17_PARAM_I2C_INT, \
.reset_pin = MCP23X17_PARAM_RESET_PIN, \
.if_params.type = MCP23X17_I2C, \
.if_params.i2c.dev = MCP23X17_PARAM_I2C_DEV, \
}
#endif /* MCP23X17_I2C_PARAMS */
#endif /* MODULE_MCP23X17_I2C || DOXYGEN */

#if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN

#ifndef MCP23X17_SAUL_GPIO_PARAMS
/** Example for mapping expander pins to SAUL */
#define MCP23X17_SAUL_GPIO_PARAMS { \
.dev = 0, \
.gpio = { \
.name = "PA0 Input", \
.pin = MCP23X17_GPIO_PIN(0, 0), \
.mode = GPIO_IN, \
.flags = 0, \
} \
}, \
{ \
.dev = 0, \
.gpio = { \
.name = "PB5 Output", \
.pin = MCP23X17_GPIO_PIN(1, 5), \
.mode = GPIO_OUT, \
.flags = SAUL_GPIO_INIT_CLEAR, \
} \
},
#endif /* MCP23X17_SAUL_GPIO_PARAMS */
#endif /* MODULE_SAUL_GPIO || DOXYGEN */
/**@}*/

/**
* @brief Allocation of MCP23x17 configuration
*/
static const mcp23x17_params_t mcp23x17_params[] =
{
#if IS_USED(MODULE_MCP23X17_SPI) || DOXYGEN
MCP23X17_SPI_PARAMS,
#endif
#if IS_USED(MODULE_MCP23X17_I2C) || DOXYGEN
MCP23X17_I2C_PARAMS,
#endif
};

#if IS_USED(MODULE_SAUL_GPIO) || DOXYGEN
/**
* @brief Additional meta information to keep in the SAUL registry
*/
static const mcp23x17_saul_gpio_params_t mcp23x17_saul_gpio_params[] =
{
MCP23X17_SAUL_GPIO_PARAMS
};
#endif /* MODULE_SAUL_GPIO || DOXYGEN */

#ifdef __cplusplus
}
#endif

#endif /* MCP23X17_PARAMS_H */
/** @} */
82 changes: 82 additions & 0 deletions drivers/mcp23x17/include/mcp23x17_regs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2021 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup drivers_mcp23x17
* @brief Register definitions for Microchip MCP23x17 I/O expanders
* @author Gunar Schorcht <gunar@schorcht.net>
* @file
* @{
*/

#ifndef MCP23X17_REGS_H
#define MCP23X17_REGS_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* @name Register addresses (IOCON.BANK = 0)
* @{
*/
#define MCP23X17_REG_IODIR (0x00) /**< I/O direction register pair */
#define MCP23X17_REG_IODIRA (0x00) /**< I/O direction register port A */
#define MCP23X17_REG_IODIRB (0x01) /**< I/O direction register port B */
#define MCP23X17_REG_IPOL (0x02) /**< Input polarity port register pair */
#define MCP23X17_REG_IPOLA (0x02) /**< Input polarity port register port A */
#define MCP23X17_REG_IPOLB (0x03) /**< Input polarity port register port B */
#define MCP23X17_REG_GPINTEN (0x04) /**< Interupt-on-change enabled register pair */
#define MCP23X17_REG_GPINTENA (0x04) /**< Interupt-on-change enabled register port A */
#define MCP23X17_REG_GPINTENB (0x05) /**< Interupt-on-change enabled register port B */
#define MCP23X17_REG_DEFVAL (0x06) /**< Default value register pair */
#define MCP23X17_REG_DEFVALA (0x06) /**< Default value register port A */
#define MCP23X17_REG_DEFVALB (0x07) /**< Default value register port B */
#define MCP23X17_REG_INTCON (0x08) /**< Interupt-on-change config register pair */
#define MCP23X17_REG_INTCONA (0x08) /**< Interupt-on-change config register port A */
#define MCP23X17_REG_INTCONB (0x09) /**< Interupt-on-change config register port B */
#define MCP23X17_REG_IOCON (0x0A) /**< I/O expander config register pair */
#define MCP23X17_REG_IOCONA (0x0A) /**< I/O expander config register port A */
#define MCP23X17_REG_IOCONB (0x0B) /**< I/O expander config register port B */
#define MCP23X17_REG_GPPU (0x0C) /**< GPIO pull-up resistor register pair */
#define MCP23X17_REG_GPPUA (0x0C) /**< GPIO pull-up resistor register port A */
#define MCP23X17_REG_GPPUB (0x0D) /**< GPIO pull-up resistor register port B */
#define MCP23X17_REG_INTF (0x0E) /**< Interrupt flag register pair */
#define MCP23X17_REG_INTFA (0x0E) /**< Interrupt flag register port A */
#define MCP23X17_REG_INTFB (0x0F) /**< Interrupt flag register port B */
#define MCP23X17_REG_INTCAP (0x10) /**< Interrupt capture register pair */
#define MCP23X17_REG_INTCAPA (0x10) /**< Interrupt capture register port A */
#define MCP23X17_REG_INTCAPB (0x11) /**< Interrupt capture register port B */
#define MCP23X17_REG_GPIO (0x12) /**< GPIO port register pair */
#define MCP23X17_REG_GPIOA (0x12) /**< GPIO port register port A */
#define MCP23X17_REG_GPIOB (0x13) /**< GPIO port register port B */
#define MCP23X17_REG_OLAT (0x14) /**< Output latch register pair */
#define MCP23X17_REG_OLATA (0x14) /**< Output latch register port A */
#define MCP23X17_REG_OLATB (0x15) /**< Output latch register port B */
/** @} */

/**
* @name Register structure definitions
* @{
*/
/* IOCONA/IOCONB registers */
#define MCP23X17_IOCON_BANK (0x80) /**< Registered separated into different banks */
#define MCP23X17_IOCON_MIRROR (0x40) /**< INT pins internally connected */
#define MCP23X17_IOCON_SEQOP (0x20) /**< Sequential option disabled */
#define MCP23X17_IOCON_DISSLW (0x10) /**< Slew rate control for SDA disabled */
#define MCP23X17_IOCON_HAEN (0x08) /**< Hardware address enabled */
#define MCP23X17_IOCON_ODR (0x04) /**< Open-drain output (overrides INTPOL) */
#define MCP23X17_IOCON_INTPOL (0x02) /**< INT signals are active high */
/** @} */

#ifdef __cplusplus
}
#endif

#endif /* MCP23X17_REGS_H */
/** @} */
Loading

0 comments on commit 297b513

Please sign in to comment.