From 53d84cf17b0fbae998ab323a6d1cbc5b22c7476e Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Wed, 21 Aug 2019 09:11:26 +0200 Subject: [PATCH 1/3] sys/auto_init: Add modules for sensors & actuators - Added module auto_init_actuators_sensors to auto-initialize actuator and sensor drivers - Auto-initialization for actuator/sensor driver `foo` will be implemented in submodule `auto_init_actuators_sensors_foo` - SAUL registration is also performed by that hook, if `auto_init_saul` is used - Pseudo-module `auto_init_sensors_actuators_default` is used to pull in sensor and actuator auto_init submodules for all drivers used - Made `auto_init_saul` depend on `auto_init_sensors_actuators_default` --- makefiles/pseudomodules.inc.mk | 1 + sys/Makefile.dep | 10 ++++-- sys/auto_init/Makefile | 4 +++ sys/auto_init/actuators_sensors/Makefile | 7 ++++ .../auto_init_actuators_sensors.c | 27 ++++++++++++++++ sys/auto_init/actuators_sensors/doc.txt | 32 +++++++++++++++++++ sys/auto_init/auto_init.c | 6 ++++ 7 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 sys/auto_init/actuators_sensors/Makefile create mode 100644 sys/auto_init/actuators_sensors/auto_init_actuators_sensors.c create mode 100644 sys/auto_init/actuators_sensors/doc.txt diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index a887731975ba..e2a093e4fa10 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -308,6 +308,7 @@ PSEUDOMODULES += test_utils_interactive_sync_shell # All auto_init modules are pseudomodules PSEUDOMODULES += auto_init_% +NO_PSEUDOMODULES += auto_init_actuators_sensors NO_PSEUDOMODULES += auto_init_can NO_PSEUDOMODULES += auto_init_loramac NO_PSEUDOMODULES += auto_init_multimedia diff --git a/sys/Makefile.dep b/sys/Makefile.dep index c732189ddd80..6ff09b22be08 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -8,8 +8,6 @@ ifneq (,$(filter arduino,$(USEMODULE))) FEATURES_REQUIRED += periph_uart SKETCH_MODULE ?= arduino_sketches USEMODULE += $(SKETCH_MODULE) - USEMODULE += fmt - USEMODULE += xtimer endif ifneq (,$(filter arduino_pwm,$(FEATURES_USED))) @@ -24,6 +22,14 @@ ifneq (,$(filter congure_test,$(USEMODULE))) USEMODULE += fmt endif +ifneq (,$(filter auto_init_actuators_sensors_%,$(filter-out auto_init_actuators_sensors_default,$(USEMODULE)))) + USEMODULE += auto_init_actuators_sensors +endif + +ifneq (,$(filter auto_init_saul,$(USEMODULE))) + USEMODULE += auto_init_actuators_sensors_default +endif + ifneq (,$(filter eepreg,$(USEMODULE))) FEATURES_REQUIRED += periph_eeprom endif diff --git a/sys/auto_init/Makefile b/sys/auto_init/Makefile index f1ac62ab8a13..3f2e496ab2e9 100644 --- a/sys/auto_init/Makefile +++ b/sys/auto_init/Makefile @@ -1,3 +1,7 @@ +ifneq (,$(filter auto_init_actuators_sensors,$(USEMODULE))) + DIRS += actuators_sensors +endif + ifneq (,$(filter gnrc_netif_init,$(USEMODULE))) DIRS += netif endif diff --git a/sys/auto_init/actuators_sensors/Makefile b/sys/auto_init/actuators_sensors/Makefile new file mode 100644 index 000000000000..9c23d0b0ea70 --- /dev/null +++ b/sys/auto_init/actuators_sensors/Makefile @@ -0,0 +1,7 @@ +MODULE = auto_init_actuators_sensors + +SRC := $(MODULE).c + +SUBMODULES := 1 + +include $(RIOTBASE)/Makefile.base diff --git a/sys/auto_init/actuators_sensors/auto_init_actuators_sensors.c b/sys/auto_init/actuators_sensors/auto_init_actuators_sensors.c new file mode 100644 index 000000000000..b4d3b2ac14b1 --- /dev/null +++ b/sys/auto_init/actuators_sensors/auto_init_actuators_sensors.c @@ -0,0 +1,27 @@ +/* + * Copyright 2020 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_actuators_sensors + * + * @{ + * @file + * @brief Implementation of the auto initialization of actuators and sensors + * + * @author Marian Buschsieweke + * + * @} + */ +#include +#include + +#include "auto_init.h" + +void auto_init_actuators_sensors(void) +{ +} diff --git a/sys/auto_init/actuators_sensors/doc.txt b/sys/auto_init/actuators_sensors/doc.txt new file mode 100644 index 000000000000..ff11d6143fc1 --- /dev/null +++ b/sys/auto_init/actuators_sensors/doc.txt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2020 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. + */ + +/** + * @defgroup sys_auto_init_actuators_sensors Actuator & Sensor Auto Initialisation + * @ingroup sys_auto_init + * @brief Auto initialisation of actuator and sensor drivers + * + * This module contains auto init functions for actuator and sensor drivers. + * + * # Architecture / Usage + * Each actuator/sensor driver "foo" capable of auto initialization must provide + * the (pseudo) module `auto_init_actuators_sensors_foo` that provides the + * auto initialization. The module `auto_init_actuators_sensors` provides the + * function `void auto_init_actuators_sensors(void)` that will call the auto + * initialisation hooks of the individual actuator and sensor drivers. + * + * Note that the auto initialization of each actuator and sensor driver must + * be individually enabled to allow the user maximum flexibility. However, + * the pseudo module `auto_init_sensors_actuators_default` will pull in the auto + * initialization hooks of all used actuator and sensor drivers. + * + * # SAUL Integration + * Each actuator driver and sensor driver auto-init hook must also register the + * SAUL endpoints (if present) if and only if the module `auto_init_saul` is + * used. + */ diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index bad383816f90..8fa4770dd85c 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -213,6 +213,12 @@ void auto_init(void) auto_init_sht1x(); } + if (IS_USED(MODULE_AUTO_INIT_ACTUATORS_SENSORS)) { + LOG_DEBUG("Auto init sensors / actuators.\n"); + extern void auto_init_actuators_sensors(void); + auto_init_actuators_sensors(); + } + if (IS_USED(MODULE_AUTO_INIT_SAUL)) { LOG_DEBUG("Auto init SAUL.\n"); extern void saul_init_devs(void); From 3c1ec3d84383bb39665c133d2ac33501dd83b91d Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Sun, 9 Feb 2020 22:08:01 +0100 Subject: [PATCH 2/3] makefiles: auto_init_sensors_actuators_default as default module By default, auto initialize all used sensor and actuator drivers. --- makefiles/defaultmodules.inc.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/makefiles/defaultmodules.inc.mk b/makefiles/defaultmodules.inc.mk index f0bc0f9bad47..19756a9fa751 100644 --- a/makefiles/defaultmodules.inc.mk +++ b/makefiles/defaultmodules.inc.mk @@ -1,6 +1,7 @@ DEFAULT_MODULE += board cpu core core_init core_msg core_panic sys DEFAULT_MODULE += auto_init +DEFAULT_MODULE += auto_init_actuators_sensors_default # Initialize all used peripherals by default DEFAULT_MODULE += periph_init From 4f426f701f5311f0b98f2fa30f10e4e5860763c3 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 15 Jul 2019 20:39:17 +0200 Subject: [PATCH 3/3] sys/auto_init: Adapted SHT1x auto_init - Moved auto initialization of the SHT1X driver to module auto_init_actuators_sensors - Added sht1x_get_dev() helper to retrieve auto-initialized device - Adapted shell commands of the SHT1X to depend on auto_init_actuators_sensors_sht1x and use sht1x_get_dev() --- drivers/include/sht1x.h | 63 ++++++---------- drivers/sht1x/Makefile.dep | 3 + drivers/sht1x/include/sht1x_params.h | 7 +- drivers/sht1x/include/sht1x_types.h | 82 +++++++++++++++++++++ sys/Makefile.dep | 4 ++ sys/auto_init/actuators_sensors/sht1x.c | 96 +++++++++++++++++++++++++ sys/auto_init/auto_init.c | 6 -- sys/shell/commands/sc_sht1x.c | 18 ++--- 8 files changed, 221 insertions(+), 58 deletions(-) create mode 100644 drivers/sht1x/include/sht1x_types.h create mode 100644 sys/auto_init/actuators_sensors/sht1x.c diff --git a/drivers/include/sht1x.h b/drivers/include/sht1x.h index b8c7bd871973..192c5fe3b35a 100644 --- a/drivers/include/sht1x.h +++ b/drivers/include/sht1x.h @@ -26,57 +26,40 @@ #include #include +#include "sht1x_types.h" +#include "sht1x_params.h" + #ifdef __cplusplus extern "C" { #endif +#ifdef MODULE_AUTO_INIT_ACTUATORS_SENSORS_SHT1X /** - * @brief Possible configuration (=status byte) values of the SHT10/11/15 + * @brief Array of all auto-initialized SHT1X device descriptors * - * These values can be or'ed together to get the configuration. - */ -typedef enum { - /** Use 8/12 bit resolution instead of 12/14 bit for temp/hum */ - SHT1X_CONF_LOW_RESOLUTION = 0x01, - /** Don't upload calibration data to register to safe 10 millisec */ - SHT1X_CONF_SKIP_CALIBRATION = 0x02, - /** Waste 8mA at 5V to increase the sensor temperature up to 10°C */ - SHT1X_CONF_ENABLE_HEATER = 0x04, - /** Skip the CRC check (and reading the CRC byte) to safe time */ - SHT1X_CONF_SKIP_CRC = 0x08, -} sht1x_conf_t; - -/** - * @brief Possible values for Vdd (measured temperature depends on it) + * @note Only available if module @ref sys_auto_init_actuators_sensors_sht1x + * is used */ -typedef enum { - SHT1X_VDD_5_0V = 0, - SHT1X_VDD_4_0V = 1, - SHT1X_VDD_3_5V = 2, - SHT1X_VDD_3_0V = 3, - SHT1X_VDD_2_5V = 4, -} sht1x_vdd_t; +extern sht1x_dev_t sht1x_devs[SHT1X_NUMOF]; /** - * @brief SHT10/11/15 temperature humidity sensor + * @brief If `sht1x` is auto-initialized, device descriptors can be retrieved by number + * @param num Number of the SHT1X device to retrieve the device descriptor of + * @return The device descriptor of the device with number @p num + * @retval `NULL` @p num is out of range + * + * @note Only available if module @ref sys_auto_init_actuators_sensors_sht1x + * is used */ -typedef struct { - gpio_t clk; /**< GPIO connected to the clock pin of the SHT1X */ - gpio_t data; /**< GPIO connected to the data pin of the SHT1X */ - int16_t temp_off; /**< Offset to add to the measured temperature */ - int16_t hum_off; /**< Offset to add to the measured humidity */ - uint8_t conf; /**< Status byte (containing configuration) of the SHT1X */ - uint8_t vdd; /**< Supply voltage of the SHT1X (as sht1x_vdd_t) */ -} sht1x_dev_t; +static inline sht1x_dev_t * sht1x_get_dev(unsigned num) +{ + if (num >= SHT1X_NUMOF) { + return NULL; + } -/** - * @brief Parameters required to set up the SHT10/11/15 device driver - */ -typedef struct { - gpio_t clk; /**< GPIO connected to the clock pin of the SHT1X */ - gpio_t data; /**< GPIO connected to the data pin of the SHT1X */ - sht1x_vdd_t vdd; /**< The supply voltage of the SHT1X */ -} sht1x_params_t; + return &sht1x_devs[num]; +} +#endif /* MODULE_AUTO_INIT_ACTUATORS_SENSORS_SHT1X */ /** * @brief Initialize the SHT10/11/15 sensor diff --git a/drivers/sht1x/Makefile.dep b/drivers/sht1x/Makefile.dep index 8525f848a7c3..b177adcca7cc 100644 --- a/drivers/sht1x/Makefile.dep +++ b/drivers/sht1x/Makefile.dep @@ -1,2 +1,5 @@ FEATURES_REQUIRED += periph_gpio USEMODULE += xtimer +ifneq (,$(filter auto_init_sensors_actuators_default,$(USEMODULE))) + USEMODULE += auto_init_actuators_sensors_sht1x +endif diff --git a/drivers/sht1x/include/sht1x_params.h b/drivers/sht1x/include/sht1x_params.h index 02b94c784de4..70a8f16fd029 100644 --- a/drivers/sht1x/include/sht1x_params.h +++ b/drivers/sht1x/include/sht1x_params.h @@ -20,7 +20,7 @@ #define SHT1X_PARAMS_H #include "board.h" -#include "sht1x.h" +#include "sht1x_types.h" #include "saul_reg.h" #ifdef __cplusplus @@ -85,6 +85,11 @@ static const saul_reg_info_t sht1x_saul_info[] = SHT1X_SAULINFO }; +/** + * @brief Number of SHT1x devices + */ +#define SHT1X_NUMOF (sizeof(sht1x_params) / sizeof(sht1x_params[0])) + #ifdef __cplusplus } #endif diff --git a/drivers/sht1x/include/sht1x_types.h b/drivers/sht1x/include/sht1x_types.h new file mode 100644 index 000000000000..4dbb43a62520 --- /dev/null +++ b/drivers/sht1x/include/sht1x_types.h @@ -0,0 +1,82 @@ +/* + * 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_sht1x + * + * @{ + * @file + * @brief Data types of SHT10/SHT11/SHT15 Device Driver + * + * @author Marian Buschsieweke + */ + +#ifndef SHT1X_TYPES_H +#define SHT1X_TYPES_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Possible configuration (=status byte) values of the SHT10/11/15 + * + * These values can be or'ed together to get the configuration. + */ +typedef enum { + /** Use 8/12 bit resolution instead of 12/14 bit for temp/hum */ + SHT1X_CONF_LOW_RESOLUTION = 0x01, + /** Don't upload calibration data to register to safe 10 millisec */ + SHT1X_CONF_SKIP_CALIBRATION = 0x02, + /** Waste 8mA at 5V to increase the sensor temperature up to 10°C */ + SHT1X_CONF_ENABLE_HEATER = 0x04, + /** Skip the CRC check (and reading the CRC byte) to safe time */ + SHT1X_CONF_SKIP_CRC = 0x08, +} sht1x_conf_t; + +/** + * @brief Possible values for Vdd (measured temperature depends on it) + */ +typedef enum { + SHT1X_VDD_5_0V = 0, + SHT1X_VDD_4_0V = 1, + SHT1X_VDD_3_5V = 2, + SHT1X_VDD_3_0V = 3, + SHT1X_VDD_2_5V = 4, +} sht1x_vdd_t; + +/** + * @brief SHT10/11/15 temperature humidity sensor + */ +typedef struct { + gpio_t clk; /**< GPIO connected to the clock pin of the SHT1X */ + gpio_t data; /**< GPIO connected to the data pin of the SHT1X */ + int16_t temp_off; /**< Offset to add to the measured temperature */ + int16_t hum_off; /**< Offset to add to the measured humidity */ + uint8_t conf; /**< Status byte (containing configuration) of the SHT1X */ + uint8_t vdd; /**< Supply voltage of the SHT1X (as sht1x_vdd_t) */ +} sht1x_dev_t; + +/** + * @brief Parameters required to set up the SHT10/11/15 device driver + */ +typedef struct { + gpio_t clk; /**< GPIO connected to the clock pin of the SHT1X */ + gpio_t data; /**< GPIO connected to the data pin of the SHT1X */ + sht1x_vdd_t vdd; /**< The supply voltage of the SHT1X */ +} sht1x_params_t; + +#ifdef __cplusplus +} +#endif + +#endif /* SHT1X_TYPES_H */ +/** @} */ diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 6ff09b22be08..cbe9ef6bd763 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -611,6 +611,10 @@ ifneq (,$(filter shell_commands,$(USEMODULE))) USEMODULE += netif USEMODULE += ipv6_addr endif + + ifneq (,$(filter sht1x,$(USEMODULE))) + USEMODULE += auto_init_actuators_sensors_sht1x + endif endif ifneq (,$(filter posix_semaphore,$(USEMODULE))) diff --git a/sys/auto_init/actuators_sensors/sht1x.c b/sys/auto_init/actuators_sensors/sht1x.c new file mode 100644 index 000000000000..418622b722a8 --- /dev/null +++ b/sys/auto_init/actuators_sensors/sht1x.c @@ -0,0 +1,96 @@ +/* + * Copyright 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. + * + */ + +/* + * @defgroup sys_auto_init_actuators_sensors_sht1x Auto-initialization for SHT1X sensors + * @ingroup sys_auto_init_actuators_sensors + * + * This sub-module contains the auto-initialization hook for SHT1X temperature + * and humidity sensors. + * + * @{ + * + * @file + * @brief Auto initialization for SHT1X temperature/humidity sensors + * + * @author Marian Buschsieweke + * + * @} + */ + +#include "log.h" +#include "sht1x_params.h" +#include "sht1x.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +/** + * @brief Allocate memory for the device descriptors + */ +sht1x_dev_t sht1x_devs[SHT1X_NUMOF]; + +#ifdef MODULE_AUTO_INIT_SAUL + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[SHT1X_NUMOF * 2]; + +/** + * @name Import SAUL endpoints + * @{ + */ +extern const saul_driver_t sht1x_saul_temp_driver; +extern const saul_driver_t sht1x_saul_hum_driver; +/** @} */ + +#endif /* MODULE_AUTO_INIT_SAUL */ + +static void sht1x_error(unsigned int num, const char *reason) +{ + LOG_ERROR("[auto_init] error initializing SHT10/SHT11/SHT15 sensor " + "#%u: %s\n", num, reason); +} + +void auto_init_sht1x(void) +{ + for (unsigned int i = 0; i < SHT1X_NUMOF; i++) { + DEBUG("[auto_init_sht1x] Initializing SHT1X sensor #%u\n", i); + + switch (sht1x_init(&sht1x_devs[i], &sht1x_params[i])) { + case 0: + break; + case -EIO: + sht1x_error(i, "Failed to initialize GPIOs"); + continue; + case -EINVAL: + sht1x_error(i, "Invalid configuration for VDD"); + continue; + case -EPROTO: + sht1x_error(i, "Reset command not acknowledged"); + continue; + default: + /* Should not happen, but better safe than sorry */ + sht1x_error(i, "?"); + continue; + } + +#ifdef MODULE_AUTO_INIT_SAUL + saul_entries[(i * 2) ].dev = &(sht1x_devs[i]); + saul_entries[(i * 2) + 1].dev = &(sht1x_devs[i]); + saul_entries[(i * 2) ].name = sht1x_saul_info[(i * 2) ].name; + saul_entries[(i * 2) + 1].name = sht1x_saul_info[(i * 2) + 1].name; + saul_entries[(i * 2) ].driver = &sht1x_saul_temp_driver; + saul_entries[(i * 2) + 1].driver = &sht1x_saul_hum_driver; + saul_reg_add(&(saul_entries[(i * 2) ])); + saul_reg_add(&(saul_entries[(i * 2) + 1])); +#endif /* MODULE_AUTO_INIT_SAUL */ + } +} diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 8fa4770dd85c..bad383816f90 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -213,12 +213,6 @@ void auto_init(void) auto_init_sht1x(); } - if (IS_USED(MODULE_AUTO_INIT_ACTUATORS_SENSORS)) { - LOG_DEBUG("Auto init sensors / actuators.\n"); - extern void auto_init_actuators_sensors(void); - auto_init_actuators_sensors(); - } - if (IS_USED(MODULE_AUTO_INIT_SAUL)) { LOG_DEBUG("Auto init SAUL.\n"); extern void saul_init_devs(void); diff --git a/sys/shell/commands/sc_sht1x.c b/sys/shell/commands/sc_sht1x.c index 79e1bf04c204..cdbb448df60f 100644 --- a/sys/shell/commands/sc_sht1x.c +++ b/sys/shell/commands/sc_sht1x.c @@ -27,23 +27,19 @@ #include "sht1x.h" #include "sht1x_params.h" -#define SHT1X_NUM ARRAY_SIZE(sht1x_params) - -extern sht1x_dev_t sht1x_devs[SHT1X_NUM]; - static sht1x_dev_t *get_dev(int argc, char **argv) { switch (argc) { case 1: - return &sht1x_devs[0]; + return sht1x_get_dev(0); case 2: { - int pos = atoi(argv[1]); - if ((pos < 0) || (pos >= (int)SHT1X_NUM)) { - printf("No SHT10/SHT11/SHT15 device with number %i\n", pos); - return NULL; + unsigned num = (unsigned)atoi(argv[1]); + sht1x_dev_t *dev = sht1x_get_dev(num); + if (!dev) { + printf("No SHT10/SHT11/SHT15 device with number %u\n", num); } - return &sht1x_devs[pos]; + return dev; } default: break; @@ -215,7 +211,7 @@ int _sht_config_handler(int argc, char **argv) return -1; } dev_num = atoi(argv[i]); - if ((dev_num < 0) || (dev_num >= (int)SHT1X_NUM)) { + if ((dev_num < 0) || (dev_num >= (int)SHT1X_NUMOF)) { printf("No SHT10/11/15 sensor with number %i\n", dev_num); return -1; }