2014-10-06 18:50:24 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @ingroup drivers_isl29020
|
2014-10-06 18:50:24 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Device driver implementation for the ISL29020 light sensor
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2015-01-29 10:15:27 +01:00
|
|
|
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
2014-10-06 18:50:24 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "isl29020.h"
|
|
|
|
#include "isl29020-internal.h"
|
|
|
|
#include "periph/i2c.h"
|
|
|
|
|
2020-10-22 11:34:31 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2014-10-06 18:50:24 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
2018-02-28 18:03:43 +01:00
|
|
|
#define DEV_I2C (dev->params.i2c)
|
|
|
|
#define DEV_ADDR (dev->params.addr)
|
|
|
|
#define DEV_RANGE (dev->params.range)
|
|
|
|
#define DEV_MODE (dev->params.mode)
|
|
|
|
|
|
|
|
int isl29020_init(isl29020_t *dev, const isl29020_params_t *params)
|
2014-10-06 18:50:24 +02:00
|
|
|
{
|
2018-02-28 18:03:43 +01:00
|
|
|
dev->params = *params;
|
|
|
|
|
2014-10-06 18:50:24 +02:00
|
|
|
int res;
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t tmp;
|
2014-10-06 18:50:24 +02:00
|
|
|
|
|
|
|
/* initialize device descriptor */
|
2018-02-28 18:03:43 +01:00
|
|
|
dev->lux_fac = (float)((1 << (10 + (2 * DEV_RANGE))) - 1) / 0xffff;
|
2014-10-06 18:50:24 +02:00
|
|
|
|
2015-05-02 08:33:15 +02:00
|
|
|
/* acquire exclusive access to the bus */
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_acquire(DEV_I2C);
|
2014-10-06 18:50:24 +02:00
|
|
|
|
|
|
|
/* configure and enable the sensor */
|
2018-02-28 18:03:43 +01:00
|
|
|
tmp = (ISL29020_CMD_EN | ISL29020_CMD_MODE |
|
|
|
|
ISL29020_RES_INT_16 | DEV_RANGE | (DEV_MODE << 5));
|
2018-05-27 23:12:27 +02:00
|
|
|
res = i2c_write_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, tmp, 0);
|
2015-05-02 08:33:15 +02:00
|
|
|
/* release the bus for other threads */
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2018-05-27 23:12:27 +02:00
|
|
|
if (res < 0) {
|
2014-10-06 18:50:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 17:32:45 +02:00
|
|
|
int isl29020_read(const isl29020_t *dev)
|
2014-10-06 18:50:24 +02:00
|
|
|
{
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t low, high;
|
2014-10-06 18:50:24 +02:00
|
|
|
uint16_t res;
|
2015-05-02 08:35:38 +02:00
|
|
|
int ret;
|
2014-10-06 18:50:24 +02:00
|
|
|
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_acquire(DEV_I2C);
|
2015-05-02 08:33:15 +02:00
|
|
|
/* read lighting value */
|
2018-05-27 23:12:27 +02:00
|
|
|
ret = i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_LDATA, &low, 0);
|
|
|
|
ret += i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_HDATA, &high, 0);
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2018-05-27 23:12:27 +02:00
|
|
|
if (ret < 0) {
|
2014-10-06 18:50:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = (high << 8) | low;
|
|
|
|
DEBUG("ISL29020: Raw value: %i - high: %i, low: %i\n", res, high, low);
|
2015-05-02 08:33:15 +02:00
|
|
|
/* calculate and return the actual lux value */
|
2014-10-06 18:50:24 +02:00
|
|
|
return (int)(dev->lux_fac * res);
|
|
|
|
}
|
|
|
|
|
2017-06-20 17:32:45 +02:00
|
|
|
int isl29020_enable(const isl29020_t *dev)
|
2014-10-06 18:50:24 +02:00
|
|
|
{
|
|
|
|
int res;
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t tmp;
|
2014-10-06 18:50:24 +02:00
|
|
|
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_acquire(DEV_I2C);
|
2018-05-27 23:12:27 +02:00
|
|
|
res = i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, &tmp, 0);
|
|
|
|
if (res < 0) {
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2014-10-06 18:50:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp |= ISL29020_CMD_EN;
|
2018-05-27 23:12:27 +02:00
|
|
|
res = i2c_write_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, tmp, 0);
|
|
|
|
if (res < 0) {
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2014-10-06 18:50:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2014-10-06 18:50:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 17:32:45 +02:00
|
|
|
int isl29020_disable(const isl29020_t *dev)
|
2014-10-06 18:50:24 +02:00
|
|
|
{
|
|
|
|
int res;
|
2016-09-30 23:01:46 +02:00
|
|
|
uint8_t tmp;
|
2014-10-06 18:50:24 +02:00
|
|
|
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_acquire(DEV_I2C);
|
2018-05-27 23:12:27 +02:00
|
|
|
res = i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, &tmp, 0);
|
|
|
|
if (res < 0) {
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2014-10-06 18:50:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp &= ~(ISL29020_CMD_EN);
|
2018-05-27 23:12:27 +02:00
|
|
|
res = i2c_write_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, tmp, 0);
|
|
|
|
if (res < 0) {
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2014-10-06 18:50:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2018-02-28 18:03:43 +01:00
|
|
|
i2c_release(DEV_I2C);
|
2014-10-06 18:50:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|