diff --git a/drivers/ad7746/ad7746_saul.c b/drivers/ad7746/ad7746_saul.c new file mode 100644 index 0000000000000000000000000000000000000000..1109379b5dcb4795c24f7e18372031e8dfcde062 --- /dev/null +++ b/drivers/ad7746/ad7746_saul.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2019 HAW Hamburg + * + * 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_ad7746 + * @{ + * + * @file + * @brief AD7746 adaption to the RIOT actuator/sensor interface + * + * @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de> + * + * @} + */ + +#include <string.h> +#include <stdio.h> + +#include "saul.h" +#include "ad7746.h" + +static int _read_cap(const void *dev, phydat_t *res) +{ + int val; + if (ad7746_read_capacitance_1((ad7746_t *)dev, &val)) { + return -ECANCELED; + } + + res->val[0] = val; + res->unit = UNIT_F; + res->scale = -15; + + return 1; +} + +static int _read_temp(const void *dev, phydat_t *res) +{ + int val; + int result; + do { + result = ad7746_read_temperature_int((ad7746_t *)dev, &val); + } while (result == AD7746_NODATA); + + if (result == AD7746_OK) { + res->val[0] = val; + res->unit = UNIT_TEMP_C; + res->scale = 0; + return 1; + } + else { + return -ECANCELED; + } +} + +static int _read_volt(const void *dev, phydat_t *res) +{ + int val; + int result; + do { + result = ad7746_read_voltage_in((ad7746_t *)dev, &val); + } while (result == AD7746_NODATA); + + if (result == AD7746_OK) { + res->val[0] = val; + res->unit = UNIT_V; + res->scale = -3; + return 1; + } + else { + return -ECANCELED; + } +} + +const saul_driver_t ad7746_saul_driver_cap = { + .read = _read_cap, + .write = saul_notsup, + .type = SAUL_SENSE_CAPACITANCE, +}; + +const saul_driver_t ad7746_saul_driver_temp = { + .read = _read_temp, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP +}; + +const saul_driver_t ad7746_saul_driver_volt = { + .read = _read_volt, + .write = saul_notsup, + .type = SAUL_SENSE_VOLTAGE +}; diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index d836794cd64df59ac22c1c4c881297e7f38392a1..11e19163957f35405433379019b61785d6883304 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -321,6 +321,10 @@ void auto_init(void) extern void auto_init_gpio(void); auto_init_gpio(); #endif +#ifdef MODULE_AD7746 + extern void auto_init_ad7746(void); + auto_init_ad7746(); +#endif #ifdef MODULE_ADCXX1C extern void auto_init_adcxx1c(void); auto_init_adcxx1c(); diff --git a/sys/auto_init/saul/auto_init_ad7746.c b/sys/auto_init/saul/auto_init_ad7746.c new file mode 100644 index 0000000000000000000000000000000000000000..9295107f7e8d2fcba436a494d90a8cd40f87a3a7 --- /dev/null +++ b/sys/auto_init/saul/auto_init_ad7746.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2019 HAW Hamburg + * + * 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 of AD7746 + * + * @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de> + * + * @} + */ + +#ifdef MODULE_AD7746 + +#include "assert.h" +#include "log.h" + +#include "saul_reg.h" +#include "ad7746.h" +#include "ad7746_params.h" + +/** + * @brief Define the number of configured sensors + */ +#define AD7746_NUM (sizeof(ad7746_params) / sizeof(ad7746_params[0])) + +/** + * @brief Allocate memory for the device descriptors. + */ +static ad7746_t ad7746_devs[AD7746_NUM]; + +/** + * @brief Memory for the SAUL registry entries. Each device provides + * 3 SAUL entries (capacitance, temperature and voltage) + */ +static saul_reg_t saul_entries[AD7746_NUM * 3]; + +/** + * @brief Define the number of saul info + */ +#define AD7746_INFO_NUM (sizeof(ad7746_saul_info) / sizeof(ad7746_saul_info[0])) + +/** + * @brief Reference the driver structs + * @{ + */ +extern saul_driver_t ad7746_saul_driver_cap; +extern saul_driver_t ad7746_saul_driver_temp; +extern saul_driver_t ad7746_saul_driver_volt; +/** @} */ + +void auto_init_ad7746(void) +{ + assert(AD7746_INFO_NUM == AD7746_NUM); + + for (unsigned i = 0; i < AD7746_NUM; i++) { + LOG_DEBUG("[auto_init_saul] initializing ad7746 #%d\n", i); + if (ad7746_init(&ad7746_devs[i], &ad7746_params[i]) < 0) { + LOG_ERROR("[auto_init_saul] error initializing ad7746 #%d\n", i); + continue; + } + /* add capacitance driver */ + saul_entries[i].dev = &(ad7746_devs[i]); + saul_entries[i].name = ad7746_saul_info[i].name; + saul_entries[i].driver = &ad7746_saul_driver_cap; + saul_reg_add(&(saul_entries[i])); + + /* add temperature driver */ + saul_entries[i + 1].dev = &(ad7746_devs[i]); + saul_entries[i + 1].name = ad7746_saul_info[i].name; + saul_entries[i + 1].driver = &ad7746_saul_driver_temp; + saul_reg_add(&(saul_entries[i + 1])); + + /* add voltage driver */ + saul_entries[i + 2].dev = &(ad7746_devs[i]); + saul_entries[i + 2].name = ad7746_saul_info[i].name; + saul_entries[i + 2].driver = &ad7746_saul_driver_volt; + saul_reg_add(&(saul_entries[i + 2])); + } +} + +#else +typedef int dont_be_pedantic; +#endif /* MODULE_AD7746 */