diff --git a/drivers/ltc4150/include/ltc4150_params.h b/drivers/ltc4150/include/ltc4150_params.h index b404148030..89d811be36 100644 --- a/drivers/ltc4150/include/ltc4150_params.h +++ b/drivers/ltc4150/include/ltc4150_params.h @@ -21,6 +21,7 @@ #include "board.h" #include "ltc4150.h" +#include "saul_reg.h" #ifdef __cplusplus extern "C" { @@ -60,6 +61,17 @@ extern "C" { #endif /**@}*/ +/** + * @name Set default SAUL info text for the LTC4150 + * @{ + */ +#ifndef LTC4150_SAULINFO +#define LTC4150_SAULINFO { .name = "LTC4150 charge" }, \ + { .name = "LTC4150 average current" } +#endif + +/**@}*/ + /** * @brief Configure LTC4150 devices */ @@ -68,6 +80,14 @@ static const ltc4150_params_t ltc4150_params[] = LTC4150_PARAMS }; +/** + * @brief Allocate and configure entries to the SAUL registry + */ +static const saul_reg_info_t ltc4150_saul_info[] = +{ + LTC4150_SAULINFO +}; + #ifdef __cplusplus } #endif diff --git a/drivers/ltc4150/ltc4150_saul.c b/drivers/ltc4150/ltc4150_saul.c new file mode 100644 index 0000000000..cfbfd588f9 --- /dev/null +++ b/drivers/ltc4150/ltc4150_saul.c @@ -0,0 +1,67 @@ +/* + * 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_ltc4150 + * @{ + * + * @file + * @brief SAUL adaption for LTC4150 devices + * + * @author Marian Buschsieweke + * + * @} + */ + +#include +#include +#include + +#include "phydat.h" +#include "saul.h" +#include "ltc4150.h" + +static int read_charge(const void *_dev, phydat_t *res) +{ + ltc4150_dev_t *dev = (ltc4150_dev_t *)_dev; + int32_t temp[3]; + + if (ltc4150_charge(dev, (uint32_t *)&temp[1], (uint32_t *)&temp[2]) == 0) { + res->scale = -3; + res->unit = UNIT_COULOMB; + temp[0] = temp[2] - temp[1]; + int dim = (dev->params.polarity != GPIO_UNDEF) ? 3 : 1; + phydat_fit(res, temp, (unsigned)dim); + return dim; + } + + return -ECANCELED; +} + +static int read_current(const void *dev, phydat_t *res) +{ + if (ltc4150_avg_current((ltc4150_dev_t *)dev, res->val) == 0) { + res->unit = UNIT_A; + res->scale = -4; + return 1; + } + + return -ECANCELED; +} + +const saul_driver_t ltc4150_saul_charge_driver = { + .read = read_charge, + .write = saul_notsup, + .type = SAUL_SENSE_CHARGE +}; + +const saul_driver_t ltc4150_saul_current_driver = { + .read = read_current, + .write = saul_notsup, + .type = SAUL_SENSE_CURRENT +}; diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index c47f486dc6..a5c45badec 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -413,6 +413,10 @@ void auto_init(void) extern void auto_init_lsm6dsl(void); auto_init_lsm6dsl(); #endif +#ifdef MODULE_LTC4150 + extern void auto_init_ltc4150(void); + auto_init_ltc4150(); + #endif #ifdef MODULE_MAG3110 extern void auto_init_mag3110(void); auto_init_mag3110(); diff --git a/sys/auto_init/saul/auto_init_ltc4150.c b/sys/auto_init/saul/auto_init_ltc4150.c new file mode 100644 index 0000000000..4d967a8e72 --- /dev/null +++ b/sys/auto_init/saul/auto_init_ltc4150.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2018 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 LTC4150 coulomb counter + * + * @author Marian Buschsieweke + * + * @} + */ + +#ifdef MODULE_LTC4150 + +#include "assert.h" +#include "log.h" +#include "saul_reg.h" +#include "ltc4150_params.h" +#include "ltc4150.h" + +/** + * @brief Define the number of configured sensors + */ +#define LTC4150_NUM (sizeof(ltc4150_params) / sizeof(ltc4150_params[0])) + +/** + * @brief Allocate memory for the device descriptors + */ +static ltc4150_dev_t ltc4150_devs[LTC4150_NUM]; + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[LTC4150_NUM * 2]; + +/** + * @brief Define the number of saul info + */ +#define LTC4150_INFO_NUM (sizeof(ltc4150_saul_info) / sizeof(ltc4150_saul_info[0])) + +/** + * @name Import SAUL endpoints + * @{ + */ +extern const saul_driver_t ltc4150_saul_charge_driver; +extern const saul_driver_t ltc4150_saul_current_driver; +/** @} */ + +void auto_init_ltc4150(void) +{ + assert(LTC4150_INFO_NUM == 2 * LTC4150_NUM); + + for (unsigned int i = 0; i < LTC4150_NUM; i++) { + LOG_DEBUG("[auto_init_saul] initializing ltc4150 #%u\n", i); + + if (ltc4150_init(<c4150_devs[i], <c4150_params[i])) { + LOG_ERROR("[auto_init_saul] error initializing ltc4150 #%u\n", i); + continue; + } + + saul_entries[i * 2 ].dev = &(ltc4150_devs[i]); + saul_entries[i * 2 ].name = ltc4150_saul_info[2 * i ].name; + saul_entries[i * 2 ].driver = <c4150_saul_charge_driver; + saul_entries[i * 2 + 1].dev = &(ltc4150_devs[i]); + saul_entries[i * 2 + 1].name = ltc4150_saul_info[2 * i + 1].name; + saul_entries[i * 2 + 1].driver = <c4150_saul_current_driver; + saul_reg_add(&(saul_entries[i * 2 ])); + saul_reg_add(&(saul_entries[i * 2 + 1])); + } +} + +#else +typedef int dont_be_pedantic; +#endif /* MODULE_LTC4150 */