
7 changed files with 286 additions and 0 deletions
@ -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 <alexandre.abadie@inria.fr> |
||||
*/ |
||||
|
||||
#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 */ |
||||
/** @} */ |
@ -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 <alexandre.abadie@inria.fr> |
||||
*/ |
||||
|
||||
#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 */ |
||||
/** @} */ |
@ -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 <alexandre.abadie@inria.fr> |
||||
* |
||||
* @} |
||||
*/ |
||||
|
||||
#include <math.h> |
||||
|
||||
#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; |
||||
} |
@ -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 <alexandre.abadie@inria.fr> |
||||
* |
||||
* @} |
||||
*/ |
||||
|
||||
#include <string.h> |
||||
|
||||
#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 |
||||
}; |
Loading…
Reference in new issue