diff --git a/drivers/bmp180/bmp180.c b/drivers/bmp180/bmp180.c index 362ec9fca13b3e1b0982f5ade4955970ad6c6110..4ed56800ec55d30ca133c9bafbcab5c176c6e878 100644 --- a/drivers/bmp180/bmp180.c +++ b/drivers/bmp180/bmp180.c @@ -19,14 +19,22 @@ */ #include <math.h> + +#include "log.h" #include "bmp180.h" #include "bmp180_internals.h" +#include "bmp180_params.h" #include "periph/i2c.h" #include "xtimer.h" #define ENABLE_DEBUG (0) #include "debug.h" +/** + * @brief Allocation of memory for device descriptors + */ +bmp180_t bmp180_devs[BMP180_NUMOF]; + /* Internal function prototypes */ static int _read_ut(bmp180_t *dev, int32_t *ut); static int _read_up(bmp180_t *dev, int32_t *up); @@ -99,6 +107,21 @@ int bmp180_init(bmp180_t *dev, i2c_t i2c, uint8_t mode) return 0; } +void bmp180_auto_init(void) +{ + for (unsigned i = 0; i < BMP180_NUMOF; i++) { + if (bmp180_init(&bmp180_devs[i], bmp180_params[i].i2c_dev, bmp180_params[i].mode) < 0) { + LOG_ERROR("Unable to initialize BMP180 sensor #%i\n", i); + } +#ifdef MODULE_SAUL_REG + for (int j = 0; j < 2; j++) { + bmp180_saul_reg[i][j].dev = &bmp180_devs[i]; + saul_reg_add(&bmp180_saul_reg[i][j]); + } +#endif + } +} + int bmp180_read_temperature(bmp180_t *dev, int32_t *temperature) { int32_t ut, b5; diff --git a/drivers/bmp180/bmp180_saul.c b/drivers/bmp180/bmp180_saul.c new file mode 100644 index 0000000000000000000000000000000000000000..ef712650bfde498c5792e36da9c381884503788c --- /dev/null +++ b/drivers/bmp180/bmp180_saul.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2016 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_bmp180 + * @{ + * + * @file + * @brief SAUL adaption for BMP180 device + * + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> + * @author Alexandre Abadie <alexandre.abadie@inria.fr> + * + * @} + */ + +#include <string.h> + +#include "saul.h" +#include "bmp180.h" +#include "xtimer.h" + +static int32_t temperature, pressure; + +static int check_and_read_temperature(void *dev, phydat_t *res) +{ + bmp180_t *d = (bmp180_t *)dev; + + bmp180_read_temperature(d, &temperature); + res->val[0] = temperature; + res->unit = UNIT_TEMP_C; + res->scale = -1; + return 1; +} + +static int check_and_read_pressure(void *dev, phydat_t *res) +{ + bmp180_t *d = (bmp180_t *)dev; + + bmp180_read_pressure(d, &pressure); + + res->val[0] = pressure/10; + res->unit = UNIT_PA; + res->scale = 1; + return 1; +} + +static int read_temperature(void *dev, phydat_t *res) +{ + return check_and_read_temperature(dev, res); +} + +static int read_pressure(void *dev, phydat_t *res) +{ + return check_and_read_pressure(dev, res); +} + +const saul_driver_t bmp180_temperature_saul_driver = { + .read = read_temperature, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP +}; + +const saul_driver_t bmp180_pressure_saul_driver = { + .read = read_pressure, + .write = saul_notsup, + .type = SAUL_SENSE_PRESS +}; diff --git a/drivers/bmp180/include/bmp180_params.h b/drivers/bmp180/include/bmp180_params.h new file mode 100644 index 0000000000000000000000000000000000000000..65c2bc64d8354075bac3fbc73c53a47807d7295f --- /dev/null +++ b/drivers/bmp180/include/bmp180_params.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2016 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_bmp180 + * + * @{ + * @file + * @brief Default configuration for BMP180 + * + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> + * @author Alexandre Abadie <alexandre.abadie@inria.fr> + */ + +#ifndef BMP180_PARAMS_H +#define BMP180_PARAMS_H + +#include "board.h" +#include "bmp180.h" +#include "saul_reg.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Set default configuration parameters for the BMP180 + * @{ + */ +#ifndef BMP180_PARAM_I2C_DEV +#define BMP180_PARAM_I2C_DEV (0) +#endif +#ifndef BMP180_PARAM_MODE +#define BMP180_PARAM_MODE BMP180_ULTRALOWPOWER +#endif + +#define BMP180_PARAMS_DEFAULT {.i2c_dev = BMP180_PARAM_I2C_DEV, \ + .mode = BMP180_PARAM_MODE } +/**@}*/ + +/** + * @brief Configure BMP180 + */ +static const bmp180_params_t bmp180_params[] = +{ +#ifdef BMP180_PARAMS_BOARD + BMP180_PARAMS_BOARD, +#else + BMP180_PARAMS_DEFAULT, +#endif +}; + +/** + * @brief Get the number of configured BMP180 devices + */ +#define BMP180_NUMOF (sizeof(bmp180_params) / sizeof(bmp180_params[0])) + +#ifdef MODULE_SAUL_REG +/** + * @brief Allocate and configure entries to the SAUL registry + */ +saul_reg_t bmp180_saul_reg[][2] = +{ + { + { + .name = "bmp180-temp", + .driver = &bmp180_temperature_saul_driver + }, + { + .name = "bmp180-press", + .driver = &bmp180_pressure_saul_driver + }, + } +}; +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* BMP180_PARAMS_H */ +/** @} */ diff --git a/drivers/include/bmp180.h b/drivers/include/bmp180.h index 09a3f00299ee72f2b1a9ea0168e75a55c46c56c4..a03658b02356f8779e05d3367412224fff795374 100644 --- a/drivers/include/bmp180.h +++ b/drivers/include/bmp180.h @@ -21,6 +21,7 @@ #ifndef BMP180_H_ #define BMP180_H_ +#include "saul.h" #include "periph/i2c.h" #ifdef __cplusplus @@ -63,6 +64,28 @@ typedef struct { uint8_t oversampling; /**< Oversampling mode */ } bmp180_t; + +/** + * @brief Device initialization parameters + */ +typedef struct { + i2c_t i2c_dev; + uint8_t mode; +} bmp180_params_t; + +/** + * @brief export SAUL endpoints + * @{ + */ +extern const saul_driver_t bmp180_temperature_saul_driver; +extern const saul_driver_t bmp180_pressure_saul_driver; +/** @} */ + +/** + * @brief auto-initialize all configured BMP180 devices + */ +void bmp180_auto_init(void); + /** * @brief Initialize the given BMP180 device * diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 9ac9b6fd58db66c63824d287b6e1327df132f02e..5d50ff14f15e766a4d77bede86cc4703cfd5ee92 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -24,6 +24,10 @@ #include "config.h" #endif +#ifdef MODULE_BMP180 +#include "bmp180.h" +#endif + #ifdef MODULE_SHT11 #include "sht11.h" #endif @@ -109,6 +113,10 @@ void auto_init(void) DEBUG("Auto init rtc module.\n"); rtc_init(); #endif +#ifdef MODULE_BMP180 + DEBUG("Auto init BMP180 module.\n"); + bmp180_auto_init(); +#endif #ifdef MODULE_SHT11 DEBUG("Auto init SHT11 module.\n"); sht11_init();