1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

drivers/ina2xx: Added SAUL integration

This commit is contained in:
Marian Buschsieweke 2019-04-05 13:49:37 +02:00
parent 3ebbec84c7
commit 0a0c1697d7
No known key found for this signature in database
GPG Key ID: 61F64C6599B1539F
4 changed files with 192 additions and 1 deletions

View File

@ -0,0 +1,86 @@
/*
* Copyright 2019 Otto-von-Guericke-Universität Magdeburg
*
* 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_ina2xx
* @{
*
* @file
* @brief SAUL adaption of the INA2XX driver
*
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*
* @}
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "phydat.h"
#include "saul.h"
#include "ina2xx.h"
static int read_current(const void *_dev, phydat_t *res)
{
ina2xx_t *dev = (ina2xx_t *)_dev;
int32_t current;
if (ina2xx_read_current(dev, &current) == 0) {
res->scale = -5;
res->unit = UNIT_A;
phydat_fit(res, &current, 1);
return 1;
}
return -ECANCELED;
}
static int read_power(const void *_dev, phydat_t *res)
{
ina2xx_t *dev = (ina2xx_t *)_dev;
uint32_t power;
if (ina2xx_read_power(dev, &power) == 0) {
res->unit = UNIT_W;
res->scale = -4;
phydat_fit(res, (int32_t *)&power, 1);
return 1;
}
return -ECANCELED;
}
static int read_voltage(const void *_dev, phydat_t *res)
{
ina2xx_t *dev = (ina2xx_t *)_dev;
if (ina2xx_read_bus(dev, (uint16_t *)&res->val[0]) >= 0) {
res->unit = UNIT_V;
res->scale = -3;
return 1;
}
return -ECANCELED;
}
const saul_driver_t ina2xx_saul_current_driver = {
.read = read_current,
.write = saul_notsup,
.type = SAUL_SENSE_CURRENT
};
const saul_driver_t ina2xx_saul_power_driver = {
.read = read_power,
.write = saul_notsup,
.type = SAUL_SENSE_POWER
};
const saul_driver_t ina2xx_saul_voltage_driver = {
.read = read_voltage,
.write = saul_notsup,
.type = SAUL_SENSE_VOLTAGE
};

View File

@ -21,6 +21,7 @@
#include "board.h"
#include "ina2xx.h"
#include "saul_reg.h"
#ifdef __cplusplus
extern "C" {
@ -92,6 +93,16 @@ extern "C" {
#endif
/**@}*/
/**
* @name Set default SAUL info for the INA2XX
* @{
*/
#ifndef INA2XX_SAULINFO
#define INA2XX_SAULINFO { .name = "INA2XX current" }, \
{ .name = "INA2XX power" }, \
{ .name = "INA2XX voltage" }
#endif
/**@}*/
/**
* @brief Configure INA2XX devices
@ -101,6 +112,14 @@ static const ina2xx_params_t ina2xx_params[] =
INA2XX_PARAMS
};
/**
* @brief Allocate and configure entries to the SAUL registry
*/
static const saul_reg_info_t ina2xx_saul_info[] =
{
INA2XX_SAULINFO
};
#ifdef __cplusplus
}
#endif

View File

@ -422,10 +422,14 @@ void auto_init(void)
extern void auto_init_hts221(void);
auto_init_hts221();
#endif
#ifdef MODULE_INA2XX
extern void auto_init_ina2xx(void);
auto_init_ina2xx();
#endif
#ifdef MODULE_INA3221
extern void auto_init_ina3221(void);
auto_init_ina3221();
#endif
#endif
#ifdef MODULE_IO1_XPLAINED
extern void auto_init_io1_xplained(void);
auto_init_io1_xplained();

View File

@ -0,0 +1,82 @@
/*
* Copyright (C) 2019 Otto-von-Guericke-Universität Magdeburg
*
* 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 sys_auto_init_saul
* @{
*
* @file
* @brief Auto initialization for INA2XX power/current monitors
*
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*
* @}
*/
#ifdef MODULE_INA2XX
#include "assert.h"
#include "log.h"
#include "saul_reg.h"
#include "ina2xx_params.h"
#include "ina2xx.h"
/**
* @brief Define the number of configured sensors
*/
#define INA2XX_NUM ARRAY_SIZE(ina2xx_params)
/**
* @brief Allocate memory for the device descriptors
*/
static ina2xx_t ina2xx_devs[INA2XX_NUM];
/**
* @brief Memory for the SAUL registry entries
*/
static saul_reg_t saul_entries[INA2XX_NUM * 3];
/**
* @brief Define the number of saul info
*/
#define INA2XX_INFO_NUM ARRAY_SIZE(ina2xx_saul_info)
/**
* @name Import SAUL endpoints
* @{
*/
extern const saul_driver_t ina2xx_saul_current_driver;
extern const saul_driver_t ina2xx_saul_power_driver;
extern const saul_driver_t ina2xx_saul_voltage_driver;
/** @} */
void auto_init_ina2xx(void)
{
assert(INA2XX_INFO_NUM == 3 * INA2XX_NUM);
for (unsigned int i = 0; i < INA2XX_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing ina2xx #%u\n", i);
if (ina2xx_init(&ina2xx_devs[i], &ina2xx_params[i])) {
LOG_ERROR("[auto_init_saul] error initializing ina2xx #%u\n", i);
continue;
}
for (unsigned int j = 0; j < 3; j++) {
saul_entries[i * 3 + j].dev = &(ina2xx_devs[i]);
saul_entries[i * 3 + j].name = ina2xx_saul_info[3 * i + j].name;
saul_entries[i * 3 + j].driver = &ina2xx_saul_power_driver;
saul_reg_add(&(saul_entries[i * 3 + j]));
}
}
}
#else
typedef int dont_be_pedantic;
#endif /* MODULE_INA2XX */