From addf64349c6fcfcc14e7c1213ce239901c712a70 Mon Sep 17 00:00:00 2001 From: smlng Date: Wed, 31 May 2017 22:18:15 +0200 Subject: [PATCH] saul: add mpl3115a2 --- boards/pba-d-01-kw2x/Makefile.dep | 3 +- drivers/mpl3115a2/include/mpl3115a2_params.h | 4 +- drivers/mpl3115a2/mpl3115a2_saul.c | 70 +++++++++++++++++ sys/auto_init/auto_init.c | 4 + sys/auto_init/saul/auto_init_mpl3115a2.c | 82 ++++++++++++++++++++ 5 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 drivers/mpl3115a2/mpl3115a2_saul.c create mode 100644 sys/auto_init/saul/auto_init_mpl3115a2.c diff --git a/boards/pba-d-01-kw2x/Makefile.dep b/boards/pba-d-01-kw2x/Makefile.dep index 415f682bf..0e28cc990 100644 --- a/boards/pba-d-01-kw2x/Makefile.dep +++ b/boards/pba-d-01-kw2x/Makefile.dep @@ -7,6 +7,7 @@ ifneq (,$(filter saul_default,$(USEMODULE))) USEMODULE += hdc1000 USEMODULE += mag3110 USEMODULE += mma8x5x - USEMODULE += tmp006 + USEMODULE += mpl3115a2 USEMODULE += tcs37727 + USEMODULE += tmp006 endif diff --git a/drivers/mpl3115a2/include/mpl3115a2_params.h b/drivers/mpl3115a2/include/mpl3115a2_params.h index 8c9da2b87..b46d7b3f0 100644 --- a/drivers/mpl3115a2/include/mpl3115a2_params.h +++ b/drivers/mpl3115a2/include/mpl3115a2_params.h @@ -69,9 +69,7 @@ static const mpl3115a2_params_t mpl3115a2_params[] = */ static const saul_reg_info_t mpl3115a2_saul_info[] = { - { - .name = "mpl3115a2" - } + { .name = "mpl3115a2" }, }; #ifdef __cplusplus diff --git a/drivers/mpl3115a2/mpl3115a2_saul.c b/drivers/mpl3115a2/mpl3115a2_saul.c new file mode 100644 index 000000000..a2153035e --- /dev/null +++ b/drivers/mpl3115a2/mpl3115a2_saul.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2017 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_mpl3115a2 + * @{ + * + * @file + * @brief MPL3115A2 adaption to the RIOT actuator/sensor interface + * + * @author Sebastian Meiling + * + * @} + */ + +#include +#include + +#include "log.h" +#include "saul.h" +#include "mpl3115a2.h" + +static int read_pressure(const void *dev, phydat_t *res) +{ + uint8_t state; + uint32_t pressure; + if (mpl3115a2_read_pressure((const mpl3115a2_t *)dev, + &pressure, &state) != MPL3115A2_OK) { + LOG_ERROR("[SAUL] mpl3115a2_read_pressure failed!\n"); + return -1; + } + /* pressure values > 100000, so /100 for int16_t which matches unit hPA */ + res->val[0] = (int16_t)(pressure/100); + res->unit = UNIT_PA; + res->scale = 2; + + return 1; +} + +static int read_temperature(const void *dev, phydat_t *res) +{ + int16_t temperature; + if (mpl3115a2_read_temp((const mpl3115a2_t *)dev, &temperature) != MPL3115A2_OK) { + LOG_ERROR("[SAUL] mpl3115a2_read_temp failed!\n"); + return -1; + } + + res->val[0] = temperature; + res->unit = UNIT_TEMP_C; + res->scale = -1; + + return 1; +} + +const saul_driver_t mpl3115a2_pressure_saul_driver = { + .read = read_pressure, + .write = saul_notsup, + .type = SAUL_SENSE_PRESS, +}; + +const saul_driver_t mpl3115a2_temperature_saul_driver = { + .read = read_temperature, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP, +}; diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 65cc4c690..21f392cbe 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -287,6 +287,10 @@ void auto_init(void) extern void auto_init_mma8x5x(void); auto_init_mma8x5x(); #endif +#ifdef MODULE_MPL3115A2 + extern void auto_init_mpl3115a2(void); + auto_init_mpl3115a2(); +#endif #ifdef MODULE_SI70XX extern void auto_init_si70xx(void); auto_init_si70xx(); diff --git a/sys/auto_init/saul/auto_init_mpl3115a2.c b/sys/auto_init/saul/auto_init_mpl3115a2.c new file mode 100644 index 000000000..301d2591e --- /dev/null +++ b/sys/auto_init/saul/auto_init_mpl3115a2.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2017 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 auto_init_saul + * @{ + * + * @file + * @brief Auto initialization of MPL3115A2 pressure sensor + * + * @author Sebastian Meiling + * + * @} + */ + +#ifdef MODULE_MPL3115A2 + +#include "log.h" +#include "saul_reg.h" + +#include "mpl3115a2.h" +#include "mpl3115a2_params.h" + +/** + * @brief Define the number of configured sensors + */ +#define MPL3115A2_NUM (sizeof(mpl3115a2_params) / sizeof(mpl3115a2_params[0])) + +/** + * @brief Allocate memory for the device descriptors + */ +static mpl3115a2_t mpl3115a2_devs[MPL3115A2_NUM]; + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[MPL3115A2_NUM * 2]; + +/** + * @name Reference the driver struct + * @{ + */ +extern const saul_driver_t mpl3115a2_pressure_saul_driver; +extern const saul_driver_t mpl3115a2_temperature_saul_driver; +/** @} */ + +void auto_init_mpl3115a2(void) +{ + for (unsigned i = 0; i < MPL3115A2_NUM; i++) { + LOG_DEBUG("[auto_init_saul] initializing mpl3115a2 #%u\n", i); + + if ((mpl3115a2_init(&mpl3115a2_devs[i], &mpl3115a2_params[i]) | + mpl3115a2_set_active(&mpl3115a2_devs[i])) != MPL3115A2_OK) { + LOG_ERROR("[auto_init_saul] error initializing mpl3115a2 #%u\n", i); + continue; + } + + /* temperature */ + saul_entries[(i * 2)].dev = &(mpl3115a2_devs[i]); + saul_entries[(i * 2)].name = mpl3115a2_saul_info[i].name; + saul_entries[(i * 2)].driver = &mpl3115a2_temperature_saul_driver; + + /* atmospheric pressure */ + saul_entries[(i * 2) + 1].dev = &(mpl3115a2_devs[i]); + saul_entries[(i * 2) + 1].name = mpl3115a2_saul_info[i].name; + saul_entries[(i * 2) + 1].driver = &mpl3115a2_pressure_saul_driver; + + /* register to saul */ + saul_reg_add(&(saul_entries[(i * 2)])); + saul_reg_add(&(saul_entries[(i * 2) + 1])); + } +} + +#else +typedef int dont_be_pedantic; +#endif /* MODULE_MPL3115A2 */