Skip to content

Commit

Permalink
drivers/mcp23x17: SAUL integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gschorcht committed Dec 5, 2021
1 parent 2bdeef5 commit 5f7b03c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drivers/mcp23x17/include/mcp23x17_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ extern "C" {
#define MCP23X17_SAUL_GPIO_PARAMS { \
.dev = 0, \
.gpio = { \
.name = "PA0 Input", \
.name = "MCP23x17_0 PA0 Input", \
.pin = MCP23X17_GPIO_PIN(0, 0), \
.mode = GPIO_IN, \
.flags = 0, \
Expand All @@ -133,7 +133,7 @@ extern "C" {
{ \
.dev = 0, \
.gpio = { \
.name = "PB5 Output", \
.name = "MCP23x17_0 PB5 Output", \
.pin = MCP23X17_GPIO_PIN(1, 5), \
.mode = GPIO_OUT, \
.flags = SAUL_GPIO_INIT_CLEAR, \
Expand Down
105 changes: 105 additions & 0 deletions drivers/saul/init_devs/auto_init_mcp23x17.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (C) 2018 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
* @ingroup sys_auto_init_saul
* @brief Auto initialization for Microchip MCP23x17 I/O expanders
* @author Gunar Schorcht <gunar@schorcht.net>
* @file
* @{
*/

#if MODULE_MCP23X17 && MODULE_SAUL_GPIO

#include "assert.h"
#include "log.h"

#include "saul_reg.h"
#include "saul/periph.h"

#include "mcp23x17.h"
#include "mcp23x17_params.h"

/**
* @brief Number of configured MCP23x17 I/O expander devices
*/
#define MCP23X17_NUM ARRAY_SIZE(mcp23x17_params)

/**
* @brief Number of configured SAUL MCP23x17 I/O pins
*/
#define MCP23X17_SAUL_GPIO_NUMOF ARRAY_SIZE(mcp23x17_saul_gpio_params)

/**
* @brief Number of saul info
*/
#define MCP23X17_INFO_NUM ARRAY_SIZE(mcp23x17_saul_info)

/**
* @brief Allocate the memory for the MCP23x17 I/O expander device descriptors
*/
mcp23x17_t mcp23x17_devs[MCP23X17_NUM];

/**
* @brief Allocate the memory for MCP23x17 I/O expander SAUL registry entries
*/
static saul_reg_t mcp23x17_saul_reg_entries[MCP23X17_SAUL_GPIO_NUMOF];

/**
* @brief Reference the MCP23x17 I/O expander input mode driver struct
*/
extern saul_driver_t mcp23x17_gpio_in_saul_driver;

/**
* @brief Reference to the MCP23x17 I/O expander output mode driver struct
*/
extern saul_driver_t mcp23x17_gpio_out_saul_driver;

void auto_init_mcp23x17(void)
{
for (unsigned int i = 0; i < MCP23X17_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing MCP23x17 I/O expander dev #%u\n", i);
mcp23x17_init(&mcp23x17_devs[i], &mcp23x17_params[i]);
}

for (unsigned int i = 0; i < MCP23X17_SAUL_GPIO_NUMOF; i++) {
const mcp23x17_saul_gpio_params_t *p = &mcp23x17_saul_gpio_params[i];

LOG_DEBUG("[auto_init_saul] initializing MCP23x17 GPIO #%u\n", i);

/* check the MCP23x17 device index */
assert(p->dev < MCP23X17_NUM);
/* check the MCP23x17 device index */
assert(p->gpio.pin < MCP23X17_GPIO_PIN_NUM);

mcp23x17_saul_reg_entries[i].dev = (void *)p;
mcp23x17_saul_reg_entries[i].name = p->gpio.name;
if ((p->gpio.mode == GPIO_IN) ||
(p->gpio.mode == GPIO_IN_PD) ||
(p->gpio.mode == GPIO_IN_PU)) {
mcp23x17_saul_reg_entries[i].driver = &mcp23x17_gpio_in_saul_driver;
}
else {
mcp23x17_saul_reg_entries[i].driver = &mcp23x17_gpio_out_saul_driver;
}
/* initialize the MCP23x17 pin */
mcp23x17_gpio_init(&mcp23x17_devs[p->dev], p->gpio.pin, p->gpio.mode);
/* set initial pin MCP23x17 state if configured */
if (p->gpio.flags & (SAUL_GPIO_INIT_CLEAR | SAUL_GPIO_INIT_SET)) {
phydat_t s;
s.val[0] = (p->gpio.flags & SAUL_GPIO_INIT_SET);
mcp23x17_saul_reg_entries[i].driver->write(p, &s);
}
/* add to registry */
saul_reg_add(&(mcp23x17_saul_reg_entries[i]));
}
}
#else
typedef int dont_be_pedantic;
#endif /* MODULE_MCP23x17 && MODULE_SAUL_GPIO */
4 changes: 4 additions & 0 deletions drivers/saul/init_devs/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ void saul_init_devs(void)
extern void auto_init_mag3110(void);
auto_init_mag3110();
}
if (IS_USED(MODULE_MCP23X17)) {
extern void auto_init_mcp23x17(void);
auto_init_mcp23x17();
}
if (IS_USED(MODULE_MHZ19)) {
extern void auto_init_mhz19(void);
auto_init_mhz19();
Expand Down

0 comments on commit 5f7b03c

Please sign in to comment.