From afb5e043bef006df8ff57eb7eeed03313669cb19 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Sun, 28 Nov 2021 12:22:00 +0100 Subject: [PATCH] drivers/vl6180x: add SAUL integration --- drivers/saul/init_devs/auto_init_vl6180x.c | 78 ++++++++++++++++++++++ drivers/saul/init_devs/init.c | 4 ++ drivers/vl6180x/vl6180x_saul.c | 64 ++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 drivers/saul/init_devs/auto_init_vl6180x.c create mode 100644 drivers/vl6180x/vl6180x_saul.c diff --git a/drivers/saul/init_devs/auto_init_vl6180x.c b/drivers/saul/init_devs/auto_init_vl6180x.c new file mode 100644 index 0000000000..e68da5a7c9 --- /dev/null +++ b/drivers/saul/init_devs/auto_init_vl6180x.c @@ -0,0 +1,78 @@ +/* + * 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_vl6180x + * @ingroup sys_auto_init_saul + * @brief Auto initialization of ST VL6180X Ranging and Ambient Light Sensor + * @author Gunar Schorcht + * @file + */ + +#ifdef MODULE_VL6180X + +#include "assert.h" +#include "log.h" +#include "saul_reg.h" +#include "vl6180x.h" +#include "vl6180x_params.h" + +/** + * @brief Define the number of configured sensors + */ +#define VL6180X_NUM ARRAY_SIZE(vl6180x_params) + +/** + * @brief Allocate memory for the device descriptors + */ +static vl6180x_t vl6180x_devs[VL6180X_NUM]; + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[VL6180X_NUM * 2]; + +/** + * @brief Define the number of saul info + */ +#define VL6180X_INFO_NUM ARRAY_SIZE(vl6180x_saul_info) + +/** + * @brief Reference the driver structs + */ +extern saul_driver_t vl6180x_saul_als_driver; +extern saul_driver_t vl6180x_saul_rng_driver; + +void auto_init_vl6180x(void) +{ + assert(VL6180X_INFO_NUM == VL6180X_NUM); + + for (unsigned i = 0; i < VL6180X_NUM; i++) { + LOG_DEBUG("[auto_init_saul] initializing vl6180x #%u\n", i); + + if (vl6180x_init(&vl6180x_devs[i], &vl6180x_params[i]) != VL6180X_OK) { + LOG_ERROR("[auto_init_saul] error initializing vl6180x #%u\n", i); + continue; + } + + saul_entries[(i * 2)].dev = &(vl6180x_devs[i]); + saul_entries[(i * 2)].name = vl6180x_saul_info[i].name; + saul_entries[(i * 2)].driver = &vl6180x_saul_als_driver; + saul_reg_add(&(saul_entries[(i * 2)])); + + saul_entries[(i * 2) + 1].dev = &(vl6180x_devs[i]); + saul_entries[(i * 2) + 1].name = vl6180x_saul_info[i].name; + saul_entries[(i * 2) + 1].driver = &vl6180x_saul_rng_driver; + saul_reg_add(&(saul_entries[(i * 2) + 1])); + } +} + +#else +typedef int dont_be_pedantic; +#endif /* MODULE_VL6180X */ diff --git a/drivers/saul/init_devs/init.c b/drivers/saul/init_devs/init.c index 6c5116e110..55b26e96ad 100644 --- a/drivers/saul/init_devs/init.c +++ b/drivers/saul/init_devs/init.c @@ -335,4 +335,8 @@ void saul_init_devs(void) extern void auto_init_veml6070(void); auto_init_veml6070(); } + if (IS_USED(MODULE_VL6180X)) { + extern void auto_init_vl6180x(void); + auto_init_vl6180x(); + } } diff --git a/drivers/vl6180x/vl6180x_saul.c b/drivers/vl6180x/vl6180x_saul.c new file mode 100644 index 0000000000..1b6242744d --- /dev/null +++ b/drivers/vl6180x/vl6180x_saul.c @@ -0,0 +1,64 @@ +/* + * 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_vl6180x + * @brief VL6180X adaption to the RIOT actuator/sensor interface + * @author Gunar Schorcht + * @file + */ + +#include + +#include "saul.h" +#include "vl6180x.h" + +#if IS_USED(MODULE_VL6180X_ALS) + +static int read_als(const void *dev, phydat_t *res) +{ + if (vl6180x_als_data_ready((vl6180x_t*)dev) == VL6180X_OK && + vl6180x_als_read((vl6180x_t*)dev, NULL, + (uint16_t*)&res->val[0]) == VL6180X_OK) { + res->unit = UNIT_LUX; + res->scale = 0; + return 1; + } + return -ECANCELED; +} + +const saul_driver_t vl6180x_saul_als_driver = { + .read = read_als, + .write = saul_write_notsup, + .type = SAUL_SENSE_LIGHT, +}; + +#endif /* IS_USED(MODULE_VL6180X_ALS) */ + +#if IS_USED(MODULE_VL6180X_RNG) + +static int read_rng(const void *dev, phydat_t *res) +{ + uint8_t mm; + if (vl6180x_rng_data_ready((vl6180x_t*)dev) == VL6180X_OK) { + vl6180x_rng_read((vl6180x_t*)dev, &mm); + res->val[0] = mm; + res->unit = UNIT_M; + res->scale = -3; + return 1; + } + return -ECANCELED; +} + +const saul_driver_t vl6180x_saul_rng_driver = { + .read = read_rng, + .write = saul_write_notsup, + .type = SAUL_SENSE_DISTANCE, +}; + +#endif /* IS_USED(MODULE_VL6180X_RNG) */