From 65d1b77338610a12e13bb814aa8411f7f0641a3c Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Mon, 27 Feb 2017 21:56:23 +0100 Subject: [PATCH] drivers/veml6070: initial implementation --- drivers/Makefile.dep | 4 + drivers/Makefile.include | 3 + drivers/include/veml6070.h | 89 ++++++++++++++++++++++ drivers/veml6070/Makefile | 1 + drivers/veml6070/include/veml6070_params.h | 72 +++++++++++++++++ drivers/veml6070/veml6070.c | 76 ++++++++++++++++++ drivers/veml6070/veml6070_saul.c | 41 ++++++++++ 7 files changed, 286 insertions(+) create mode 100644 drivers/include/veml6070.h create mode 100644 drivers/veml6070/Makefile create mode 100644 drivers/veml6070/include/veml6070_params.h create mode 100644 drivers/veml6070/veml6070.c create mode 100644 drivers/veml6070/veml6070_saul.c diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 531a6b7c6a..558ff0e35a 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -184,6 +184,10 @@ ifneq (,$(filter srf08,$(USEMODULE))) USEMODULE += xtimer endif +ifneq (,$(filter veml6070,$(USEMODULE))) + FEATURES_REQUIRED += periph_i2c +endif + ifneq (,$(filter w5100,$(USEMODULE))) USEMODULE += netdev2_eth USEMODULE += luid diff --git a/drivers/Makefile.include b/drivers/Makefile.include index 9af613263e..07eaa78bd8 100644 --- a/drivers/Makefile.include +++ b/drivers/Makefile.include @@ -100,3 +100,6 @@ endif ifneq (,$(filter sdcard_spi,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/drivers/sdcard_spi/include endif +ifneq (,$(filter veml6070,$(USEMODULE))) + USEMODULE_INCLUDES += $(RIOTBASE)/drivers/veml6070/include +endif diff --git a/drivers/include/veml6070.h b/drivers/include/veml6070.h new file mode 100644 index 0000000000..26f802ecc2 --- /dev/null +++ b/drivers/include/veml6070.h @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2017 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. + */ + +/** + * @defgroup drivers_veml6070 VEML6070 + * @ingroup drivers_sensors + * @brief Device driver interface for the VEML6070 UV sensor + * @{ + * + * @file + * @brief Device driver interface for the VEML6070 UV sensor. + * + * @author Alexandre Abadie + */ + +#ifndef VEML6070_H +#define VEML6070_H + +#include "saul.h" +#include "periph/i2c.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Integration times + */ +typedef enum veml6070_integrationtime { + VEML6070_HALF_T = 0, /**< 1/2 T integration time */ + VEML6070_1_T, /**< 1 T integration time */ + VEML6070_2_T, /**< 2 T integration time */ + VEML6070_4_T, /**< 4 T integration time */ +} veml6070_itime_t; + +/** + * @brief Status and error return codes + */ +enum { + VEML6070_OK = 0, /**< Everything was fine */ + VEML6070_ERR_I2C /**< Error initializing the I2C bus */ +}; + +/** + * @brief Device initialization parameters + */ +typedef struct { + i2c_t i2c_dev; /**< I2C device which is used */ + veml6070_itime_t itime; /**< Integration time */ +} veml6070_params_t; + +/** + * @brief Device descriptor for the VEML6070 sensor + */ +typedef struct { + veml6070_params_t params; /**< Device parameters */ +} veml6070_t; + +/** + * @brief Initialize the given VEML6070 device + * + * @param[out] dev Initialized device descriptor of VEML6070 device + * @param[in] params The parameters for the VEML6070 device (integration time) + * + * @return VEML6070_OK on success + * @return VEML6070_ERR_I2C if given I2C is not enabled in board config + */ +int veml6070_init(veml6070_t *dev, const veml6070_params_t * params); + +/** + * @brief Read UV indice from the given VEML6070 device + * + * @param[in] dev Device descriptor of VEML6070 device to read from + * + * @return UV indice + */ +uint16_t veml6070_read_uv(veml6070_t *dev); + +#ifdef __cplusplus +} +#endif + +#endif /* VEML6070_H */ +/** @} */ diff --git a/drivers/veml6070/Makefile b/drivers/veml6070/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/drivers/veml6070/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/veml6070/include/veml6070_params.h b/drivers/veml6070/include/veml6070_params.h new file mode 100644 index 0000000000..ef96b6d1b4 --- /dev/null +++ b/drivers/veml6070/include/veml6070_params.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017 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_veml6070 + * + * @{ + * @file + * @brief Default configuration for VEML6070 + * + * @author Alexandre Abadie + */ + +#ifndef VEML6070_PARAMS_H +#define VEML6070_PARAMS_H + +#include "board.h" +#include "veml6070.h" +#include "saul_reg.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Set default configuration parameters for the VEML6070 + * @{ + */ +#ifndef VEML6070_PARAM_I2C_DEV +#define VEML6070_PARAM_I2C_DEV I2C_DEV(0) +#endif +#ifndef VEML6070_PARAM_ITIME +#define VEML6070_PARAM_ITIME VEML6070_1_T +#endif + +#define VEML6070_PARAMS_DEFAULT { .i2c_dev = VEML6070_PARAM_I2C_DEV, \ + .itime = VEML6070_PARAM_ITIME } +/**@}*/ + +/** + * @brief Configure VEML6070 + */ +static const veml6070_params_t veml6070_params[] = +{ +#ifdef VEML6070_PARAMS_BOARD + VEML6070_PARAMS_BOARD, +#else + VEML6070_PARAMS_DEFAULT, +#endif +}; + +/** + * @brief Configure SAUL registry entries + */ +static const saul_reg_info_t veml6070_saul_reg_info[] = +{ + { + .name = "veml6070-uv" + } +}; + +#ifdef __cplusplus +} +#endif + +#endif /* VEML6070_PARAMS_H */ +/** @} */ diff --git a/drivers/veml6070/veml6070.c b/drivers/veml6070/veml6070.c new file mode 100644 index 0000000000..32fa79dd28 --- /dev/null +++ b/drivers/veml6070/veml6070.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2017 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_veml6070 + * @{ + * + * @file + * @brief Device driver implementation for the VEML6070 UV sensor. + * + * @author Alexandre Abadie + * + * @} + */ + +#include + +#include "log.h" +#include "veml6070.h" +#include "veml6070_params.h" +#include "periph/i2c.h" +#include "xtimer.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +#define VEML6070_ADDRH (0x39) +#define VEML6070_ADDRL (0x38) + +/*---------------------------------------------------------------------------* + * VEML6070 Core API * + *---------------------------------------------------------------------------*/ + +int veml6070_init(veml6070_t *dev, const veml6070_params_t * params) +{ + dev->params = *params; + + /* Initialize I2C interface */ + if (i2c_init_master(dev->params.i2c_dev, I2C_SPEED_NORMAL)) { + DEBUG("[Error] I2C device not enabled\n"); + return -VEML6070_ERR_I2C; + } + + /* Acquire exclusive access */ + i2c_acquire(dev->params.i2c_dev); + + i2c_write_byte(dev->params.i2c_dev, VEML6070_ADDRL, + (uint8_t)(dev->params.itime << 2) | 0x02); + + /* Release I2C device */ + i2c_release(dev->params.i2c_dev); + + return VEML6070_OK; +} + +uint16_t veml6070_read_uv(veml6070_t *dev) +{ + /* Acquire exclusive access */ + i2c_acquire(dev->params.i2c_dev); + + uint8_t buffer[2]; + i2c_read_byte(dev->params.i2c_dev, VEML6070_ADDRL, &buffer[0]); + i2c_read_byte(dev->params.i2c_dev, VEML6070_ADDRH, &buffer[1]); + + uint16_t uv = (uint16_t)(buffer[1] << 8) | buffer[0]; + + /* Release I2C device */ + i2c_release(dev->params.i2c_dev); + + return uv; +} diff --git a/drivers/veml6070/veml6070_saul.c b/drivers/veml6070/veml6070_saul.c new file mode 100644 index 0000000000..7ba6dd2fcd --- /dev/null +++ b/drivers/veml6070/veml6070_saul.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2017 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_veml6070 + * @{ + * + * @file + * @brief SAUL adaption for VEML6070 UV sensor + * + * @author Alexandre Abadie + * + * @} + */ + +#include + +#include "saul.h" +#include "veml6070.h" +#include "xtimer.h" + +static int read_uv(void *dev, phydat_t *res) +{ + veml6070_t *d = (veml6070_t *)dev; + + res->val[0] = veml6070_read_uv(d); + res->unit = UNIT_NONE; + res->scale = -1; + return 1; +} + +const saul_driver_t veml6070_uv_saul_driver = { + .read = read_uv, + .write = saul_notsup, + .type = SAUL_SENSE_ANY +};