drivers: sensor: Add OPT3001 light sensor driver

Add OPT3001 light sensor driver

Signed-off-by: Iosif Macesanu <iosif@actinius.com>
This commit is contained in:
Iosif Macesanu 2019-10-14 17:29:05 +02:00 committed by Maureen Helm
commit 24508a777d
9 changed files with 235 additions and 0 deletions

View file

@ -45,6 +45,7 @@ add_subdirectory_ifdef(CONFIG_MAX44009 max44009)
add_subdirectory_ifdef(CONFIG_MCP9808 mcp9808)
add_subdirectory_ifdef(CONFIG_MPU6050 mpu6050)
add_subdirectory_ifdef(CONFIG_MS5837 ms5837)
add_subdirectory_ifdef(CONFIG_OPT3001 opt3001)
add_subdirectory_ifdef(CONFIG_PMS7003 pms7003)
add_subdirectory_ifdef(CONFIG_QDEC_NRFX qdec_nrfx)
add_subdirectory_ifdef(CONFIG_TEMP_NRF5 nrf5)

View file

@ -117,6 +117,8 @@ source "drivers/sensor/ms5837/Kconfig"
source "drivers/sensor/nrf5/Kconfig"
source "drivers/sensor/opt3001/Kconfig"
source "drivers/sensor/pms7003/Kconfig"
source "drivers/sensor/qdec_nrfx/Kconfig"

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_OPT3001 opt3001.c)

View file

@ -0,0 +1,12 @@
# Kconfig - OPT3001 light sensor configuration options
#
# Copyright (c) 2019 Actinius
#
# SPDX-License-Identifier: Apache-2.0
#
menuconfig OPT3001
bool "OPT3001 Light Sensor"
depends on (I2C && HAS_DTS_I2C)
help
Enable driver for OPT3001 light sensors.

View file

@ -0,0 +1,166 @@
/*
* Copyright (c) 2019 Actinius
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <drivers/i2c.h>
#include <drivers/sensor.h>
#include <logging/log.h>
#include <misc/__assert.h>
#include "opt3001.h"
LOG_MODULE_REGISTER(opt3001, CONFIG_SENSOR_LOG_LEVEL);
static int opt3001_reg_read(struct opt3001_data *drv_data, u8_t reg,
u16_t *val)
{
u8_t value[2];
if (i2c_burst_read(drv_data->i2c, DT_INST_0_TI_OPT3001_BASE_ADDRESS,
reg, value, 2) != 0) {
return -EIO;
}
*val = ((u16_t)value[0] << 8) + value[1];
return 0;
}
static int opt3001_reg_write(struct opt3001_data *drv_data, u8_t reg,
u16_t val)
{
u8_t new_value[2];
new_value[0] = val >> 8;
new_value[1] = val & 0xff;
u8_t tx_buf[3] = { reg, new_value[0], new_value[1] };
return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf),
DT_INST_0_TI_OPT3001_BASE_ADDRESS);
}
static int opt3001_reg_update(struct opt3001_data *drv_data, u8_t reg,
u16_t mask, u16_t val)
{
u16_t old_val;
u16_t new_val;
if (opt3001_reg_read(drv_data, reg, &old_val) != 0) {
return -EIO;
}
new_val = old_val & ~mask;
new_val |= val & mask;
return opt3001_reg_write(drv_data, reg, new_val);
}
static int opt3001_sample_fetch(struct device *dev, enum sensor_channel chan)
{
struct opt3001_data *drv_data = dev->driver_data;
u16_t value;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_LIGHT);
drv_data->sample = 0U;
if (opt3001_reg_read(drv_data, OPT3001_REG_RESULT, &value) != 0) {
return -EIO;
}
drv_data->sample = value;
return 0;
}
static int opt3001_channel_get(struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
struct opt3001_data *drv_data = dev->driver_data;
s32_t uval;
if (chan != SENSOR_CHAN_LIGHT) {
return -ENOTSUP;
}
/**
* sample consists of 4 bits of exponent and 12 bits of mantissa
* bits 15 to 12 are exponent bits
* bits 11 to 0 are the mantissa bits
*
* lux is the integer obtained using the following formula:
* (2^(exponent value)) * 0.01 * mantisa value
*/
uval = (1 << (drv_data->sample >> OPT3001_SAMPLE_EXPONENT_SHIFT))
* (drv_data->sample & OPT3001_MANTISSA_MASK);
val->val1 = uval / 100;
val->val2 = (uval % 100) * 10000;
return 0;
}
static const struct sensor_driver_api opt3001_driver_api = {
.sample_fetch = opt3001_sample_fetch,
.channel_get = opt3001_channel_get,
};
static int opt3001_chip_init(struct device *dev)
{
struct opt3001_data *drv_data = dev->driver_data;
u16_t value;
drv_data->i2c = device_get_binding(DT_INST_0_TI_OPT3001_BUS_NAME);
if (drv_data->i2c == NULL) {
LOG_ERR("Failed to get pointer to %s device!",
DT_INST_0_TI_OPT3001_BUS_NAME);
return -EINVAL;
}
if (opt3001_reg_read(drv_data, OPT3001_REG_MANUFACTURER_ID,
&value) != 0) {
return -EIO;
}
if (value != OPT3001_MANUFACTURER_ID_VALUE) {
LOG_ERR("Bad manufacturer id 0x%x", value);
return -ENOTSUP;
}
if (opt3001_reg_read(drv_data, OPT3001_REG_DEVICE_ID,
&value) != 0) {
return -EIO;
}
if (value != OPT3001_DEVICE_ID_VALUE) {
LOG_ERR("Bad device id 0x%x", value);
return -ENOTSUP;
}
if (opt3001_reg_update(drv_data, OPT3001_REG_CONFIG,
OPT3001_CONVERSION_MODE_MASK,
OPT3001_CONVERSION_MODE_CONTINUOUS) != 0) {
LOG_ERR("Failed to set mode to continuous conversion");
return -EIO;
}
return 0;
}
int opt3001_init(struct device *dev)
{
if (opt3001_chip_init(dev) < 0) {
return -EINVAL;
}
return 0;
}
static struct opt3001_data opt3001_drv_data;
DEVICE_AND_API_INIT(opt3001, DT_INST_0_TI_OPT3001_LABEL, opt3001_init,
&opt3001_drv_data, NULL, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &opt3001_driver_api);

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2019 Actinius
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_SENSOR_OPT3001_H_
#define ZEPHYR_DRIVERS_SENSOR_OPT3001_H_
#include <misc/util.h>
#define OPT3001_REG_RESULT 0x00
#define OPT3001_REG_CONFIG 0x01
#define OPT3001_REG_MANUFACTURER_ID 0x7E
#define OPT3001_REG_DEVICE_ID 0x7F
#define OPT3001_MANUFACTURER_ID_VALUE 0x5449
#define OPT3001_DEVICE_ID_VALUE 0x3001
#define OPT3001_CONVERSION_MODE_MASK (BIT(10) | BIT(9))
#define OPT3001_CONVERSION_MODE_CONTINUOUS (BIT(10) | BIT(9))
#define OPT3001_SAMPLE_EXPONENT_SHIFT 12
#define OPT3001_MANTISSA_MASK 0xfff
struct opt3001_data {
struct device *i2c;
u16_t sample;
};
#endif /* _SENSOR_OPT3001_ */

View file

@ -0,0 +1,11 @@
# Copyright (c) 2019, Actinius
# SPDX-License-Identifier: Apache-2.0
title: Texas Instruments OPT3001 Ambient light sensor
description: >
This is a representation of the Texas Instruments OPT3001 Ambient light sensor
compatible: "ti,opt3001"
include: i2c-device.yaml

View file

@ -264,6 +264,12 @@
#define DT_INST_0_TI_HDC_BASE_ADDRESS 0
#endif
#ifndef DT_INST_0_TI_OPT3001_LABEL
#define DT_INST_0_TI_OPT3001_LABEL ""
#define DT_INST_0_TI_OPT3001_BASE_ADDRESS 0x00
#define DT_INST_0_TI_OPT3001_BUS_NAME ""
#endif
#ifndef DT_INST_0_TI_TMP116_LABEL
#define DT_INST_0_TI_TMP116_LABEL ""
#define DT_INST_0_TI_TMP116_BASE_ADDRESS 0

View file

@ -24,6 +24,7 @@ CONFIG_MAX30101=y
CONFIG_MAX44009=y
CONFIG_MCP9808=y
CONFIG_MPU6050=y
CONFIG_OPT3001=y
CONFIG_SHT3XD=y
CONFIG_SI7006=y
CONFIG_SI7060=y