diff --git a/drivers/ds75lx/ds75lx_saul.c b/drivers/ds75lx/ds75lx_saul.c new file mode 100644 index 0000000000..9905bd7566 --- /dev/null +++ b/drivers/ds75lx/ds75lx_saul.c @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2019 Inria + * + * 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_ds75lx + * @{ + * + * @file + * @brief SAUL adaption for DS75LX device + * + * @author Alexandre Abadie + * + * @} + */ + +#include + +#include "saul.h" +#include "ds75lx.h" + +static int read_temperature(const void *dev, phydat_t *res) +{ + ds75lx_wakeup((const ds75lx_t *)dev); + if (ds75lx_read_temperature((const ds75lx_t *)dev, &res->val[0]) != DS75LX_OK) { + ds75lx_shutdown((const ds75lx_t *)dev); + return -ECANCELED; + } + ds75lx_shutdown((const ds75lx_t *)dev); + + res->unit = UNIT_TEMP_C; + res->scale = -2; + return 1; +} + +const saul_driver_t ds75lx_temperature_saul_driver = { + .read = read_temperature, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP +}; diff --git a/drivers/ds75lx/include/ds75lx_params.h b/drivers/ds75lx/include/ds75lx_params.h index 8999261bed..4ea3809d9c 100644 --- a/drivers/ds75lx/include/ds75lx_params.h +++ b/drivers/ds75lx/include/ds75lx_params.h @@ -47,6 +47,9 @@ extern "C" { .addr = DS75LX_PARAM_I2C_ADDR, \ .resolution = DS75LX_PARAM_RESOLUTION } #endif +#ifndef DS75LX_SAUL_INFO +#define DS75LX_SAUL_INFO { .name = "ds75lx" } +#endif /**@}*/ /** @@ -57,6 +60,14 @@ static const ds75lx_params_t ds75lx_params[] = DS75LX_PARAMS }; +/** + * @brief Configure SAUL registry entries + */ +static const saul_reg_info_t ds75lx_saul_info[] = +{ + DS75LX_SAUL_INFO +}; + #ifdef __cplusplus } #endif diff --git a/drivers/include/ds75lx.h b/drivers/include/ds75lx.h index a728a23461..31fe414a6c 100644 --- a/drivers/include/ds75lx.h +++ b/drivers/include/ds75lx.h @@ -9,8 +9,10 @@ /** * @defgroup drivers_ds75lx Maxim DS75LX temperature sensor * @ingroup drivers_sensors + * @ingroup drivers_saul * @brief Device driver interface for the Maxim DS75LX temperature sensor * + * This driver provides @ref drivers_saul capabilities. * @{ * * @file diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index e94d530d75..92335a3259 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -365,6 +365,10 @@ void auto_init(void) extern void auto_init_ds18(void); auto_init_ds18(); #endif +#ifdef MODULE_DS75LX + extern void auto_init_ds75lx(void); + auto_init_ds75lx(); +#endif #ifdef MODULE_FXOS8700 extern void auto_init_fxos8700(void); auto_init_fxos8700(); diff --git a/sys/auto_init/saul/auto_init_ds75lx.c b/sys/auto_init/saul/auto_init_ds75lx.c new file mode 100644 index 0000000000..e80e273a9c --- /dev/null +++ b/sys/auto_init/saul/auto_init_ds75lx.c @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2019 Inria + * + * 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 DS75LX driver. + * + * @author Alexandre Abadie + * + * @} + */ + +#ifdef MODULE_DS75LX + +#include "assert.h" +#include "log.h" +#include "saul_reg.h" +#include "ds75lx.h" +#include "ds75lx_params.h" + +/** + * @brief Define the number of configured sensors + */ +#define DS75LX_NUM (sizeof(ds75lx_params) / sizeof(ds75lx_params[0])) + +/** + * @brief Allocation of memory for device descriptors + */ +static ds75lx_t ds75lx_devs[DS75LX_NUM]; + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[DS75LX_NUM]; + +/** + * @brief Define the number of saul info + */ +#define DS75LX_INFO_NUM (sizeof(ds75lx_saul_info) / sizeof(ds75lx_saul_info[0])) + +/** + * @name Reference the driver structs. + * @{ + */ +extern const saul_driver_t ds75lx_temperature_saul_driver; +/** @} */ + +void auto_init_ds75lx(void) +{ + assert(DS75LX_INFO_NUM == DS75LX_NUM); + + for (unsigned i = 0; i < DS75LX_NUM; i++) { + LOG_DEBUG("[auto_init_saul] initializing ds75lx #%u\n", i); + + if (ds75lx_init(&ds75lx_devs[i], + &ds75lx_params[i]) != DS75LX_OK) { + LOG_ERROR("[auto_init_saul] error initializing ds75lx #%u\n", i); + continue; + } + + saul_entries[i].dev = &(ds75lx_devs[i]); + saul_entries[i].name = ds75lx_saul_info[i].name; + saul_entries[i].driver = &ds75lx_temperature_saul_driver; + + /* register to saul */ + saul_reg_add(&(saul_entries[i])); + } +} +#else +typedef int dont_be_pedantic; +#endif /* MODULE_DS75LX */