1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/mcp47xx: SAUL integration

This commit is contained in:
Gunar Schorcht 2021-12-06 23:50:31 +01:00
parent 499cbb9062
commit 033fa00b9f
3 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,43 @@
/*
* 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_mcp47xx
* @brief MCP47xx adaption to the RIOT actuator/sensor interface
* @author Gunar Schorcht <gunar@schorcht.net>
* @file
*/
#if MODULE_SAUL
#include <string.h>
#include "saul.h"
#include "mcp47xx.h"
extern mcp47xx_t mcp47xx_devs[];
static int set(const void *dev, phydat_t *data)
{
const mcp47xx_saul_dac_params_t *p = (const mcp47xx_saul_dac_params_t *)dev;
mcp47xx_dac_set(&mcp47xx_devs[p->dev], p->channel, (uint16_t)data->val[0]);
return 1;
}
static int get(const void *dev, phydat_t *data)
{
const mcp47xx_saul_dac_params_t *p = (const mcp47xx_saul_dac_params_t *)dev;
mcp47xx_dac_get(&mcp47xx_devs[p->dev], p->channel, (uint16_t*)&data->val[0]);
return 1;
}
const saul_driver_t mcp47xx_dac_saul_driver = {
.read = get,
.write = set,
.type = SAUL_ACT_DIMMER
};
#endif /* MODULE_SAUL */

View File

@ -0,0 +1,90 @@
/*
* 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_mcp47xx
* @ingroup sys_auto_init_saul
* @brief Auto initialization of Microchip MCP47xx DAC with I2C interface
* @author Gunar Schorcht <gunar@schorcht.net>
* @file
* @{
*/
#include "assert.h"
#include "log.h"
#include "saul_reg.h"
#include "saul/periph.h"
#include "mcp47xx.h"
#include "mcp47xx_params.h"
/**
* @brief Number of configured MCP47xx DAC devices
*/
#define MCP47XX_NUM (sizeof(mcp47xx_params) / sizeof(mcp47xx_params[0]))
/**
* @brief Number of configured SAUL MCP47xx DAC channels
*/
#define MCP47XX_SAUL_DAC_NUMOF (sizeof(mcp47xx_saul_dac_params) / \
sizeof(mcp47xx_saul_dac_params[0]))
/**
* @brief Number of saul info
*/
#define MCP47XX_INFO_NUM (sizeof(mcp47xx_saul_info) / \
sizeof(mcp47xx_saul_info[0]))
/**
* @brief Allocate the memory for the MCP47xx DAC device descriptors
*/
mcp47xx_t mcp47xx_devs[MCP47XX_NUM];
/**
* @brief Allocate the memory for MCP47xx DAC SAUL registry entries
*/
static saul_reg_t mcp47xx_saul_reg_entries[MCP47XX_SAUL_DAC_NUMOF];
/**
* @brief Reference the MCP47xx DAC input mode driver struct
*/
extern saul_driver_t mcp47xx_dac_in_saul_driver;
/**
* @brief Reference to the MCP47xx DAC output mode driver struct
*/
extern saul_driver_t mcp47xx_dac_saul_driver;
void auto_init_mcp47xx(void)
{
for (unsigned int i = 0; i < MCP47XX_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing MCP47xx DAC dev #%u\n", i);
mcp47xx_init(&mcp47xx_devs[i], &mcp47xx_params[i]);
}
for (unsigned int i = 0; i < MCP47XX_SAUL_DAC_NUMOF; i++) {
const mcp47xx_saul_dac_params_t *p = &mcp47xx_saul_dac_params[i];
LOG_DEBUG("[auto_init_saul] initializing MCP47xx DAC channel #%u\n", i);
/* check the MCP47xx device index */
assert(p->dev < MCP47XX_NUM);
mcp47xx_saul_reg_entries[i].dev = (void *)p;
mcp47xx_saul_reg_entries[i].name = p->name;
mcp47xx_saul_reg_entries[i].driver = &mcp47xx_dac_saul_driver;
/* initialize the MCP47xx pin */
mcp47xx_dac_init(&mcp47xx_devs[p->dev], p->channel);
mcp47xx_dac_set(&mcp47xx_devs[p->dev], p->channel, p->initial);
/* add to registry */
saul_reg_add(&(mcp47xx_saul_reg_entries[i]));
}
}

View File

@ -195,6 +195,10 @@ void saul_init_devs(void)
extern void auto_init_mag3110(void);
auto_init_mag3110();
}
if (IS_USED(MODULE_MCP47XX)) {
extern void auto_init_mcp47xx(void);
auto_init_mcp47xx();
}
if (IS_USED(MODULE_MHZ19)) {
extern void auto_init_mhz19(void);
auto_init_mhz19();