Skip to content

Commit 24508a7

Browse files
Iosif MacesanuMaureenHelm
authored andcommitted
drivers: sensor: Add OPT3001 light sensor driver
Add OPT3001 light sensor driver Signed-off-by: Iosif Macesanu <[email protected]>
1 parent 8844856 commit 24508a7

File tree

9 files changed

+235
-0
lines changed

9 files changed

+235
-0
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ add_subdirectory_ifdef(CONFIG_MAX44009 max44009)
4545
add_subdirectory_ifdef(CONFIG_MCP9808 mcp9808)
4646
add_subdirectory_ifdef(CONFIG_MPU6050 mpu6050)
4747
add_subdirectory_ifdef(CONFIG_MS5837 ms5837)
48+
add_subdirectory_ifdef(CONFIG_OPT3001 opt3001)
4849
add_subdirectory_ifdef(CONFIG_PMS7003 pms7003)
4950
add_subdirectory_ifdef(CONFIG_QDEC_NRFX qdec_nrfx)
5051
add_subdirectory_ifdef(CONFIG_TEMP_NRF5 nrf5)

drivers/sensor/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ source "drivers/sensor/ms5837/Kconfig"
117117

118118
source "drivers/sensor/nrf5/Kconfig"
119119

120+
source "drivers/sensor/opt3001/Kconfig"
121+
120122
source "drivers/sensor/pms7003/Kconfig"
121123

122124
source "drivers/sensor/qdec_nrfx/Kconfig"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
5+
zephyr_library_sources_ifdef(CONFIG_OPT3001 opt3001.c)

drivers/sensor/opt3001/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Kconfig - OPT3001 light sensor configuration options
2+
#
3+
# Copyright (c) 2019 Actinius
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
8+
menuconfig OPT3001
9+
bool "OPT3001 Light Sensor"
10+
depends on (I2C && HAS_DTS_I2C)
11+
help
12+
Enable driver for OPT3001 light sensors.

drivers/sensor/opt3001/opt3001.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2019 Actinius
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <device.h>
8+
#include <drivers/i2c.h>
9+
#include <drivers/sensor.h>
10+
#include <logging/log.h>
11+
#include <misc/__assert.h>
12+
13+
#include "opt3001.h"
14+
15+
LOG_MODULE_REGISTER(opt3001, CONFIG_SENSOR_LOG_LEVEL);
16+
17+
static int opt3001_reg_read(struct opt3001_data *drv_data, u8_t reg,
18+
u16_t *val)
19+
{
20+
u8_t value[2];
21+
22+
if (i2c_burst_read(drv_data->i2c, DT_INST_0_TI_OPT3001_BASE_ADDRESS,
23+
reg, value, 2) != 0) {
24+
return -EIO;
25+
}
26+
27+
*val = ((u16_t)value[0] << 8) + value[1];
28+
29+
return 0;
30+
}
31+
32+
static int opt3001_reg_write(struct opt3001_data *drv_data, u8_t reg,
33+
u16_t val)
34+
{
35+
u8_t new_value[2];
36+
37+
new_value[0] = val >> 8;
38+
new_value[1] = val & 0xff;
39+
40+
u8_t tx_buf[3] = { reg, new_value[0], new_value[1] };
41+
42+
return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf),
43+
DT_INST_0_TI_OPT3001_BASE_ADDRESS);
44+
}
45+
46+
static int opt3001_reg_update(struct opt3001_data *drv_data, u8_t reg,
47+
u16_t mask, u16_t val)
48+
{
49+
u16_t old_val;
50+
u16_t new_val;
51+
52+
if (opt3001_reg_read(drv_data, reg, &old_val) != 0) {
53+
return -EIO;
54+
}
55+
56+
new_val = old_val & ~mask;
57+
new_val |= val & mask;
58+
59+
return opt3001_reg_write(drv_data, reg, new_val);
60+
}
61+
62+
static int opt3001_sample_fetch(struct device *dev, enum sensor_channel chan)
63+
{
64+
struct opt3001_data *drv_data = dev->driver_data;
65+
u16_t value;
66+
67+
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_LIGHT);
68+
69+
drv_data->sample = 0U;
70+
71+
if (opt3001_reg_read(drv_data, OPT3001_REG_RESULT, &value) != 0) {
72+
return -EIO;
73+
}
74+
75+
drv_data->sample = value;
76+
77+
return 0;
78+
}
79+
80+
static int opt3001_channel_get(struct device *dev, enum sensor_channel chan,
81+
struct sensor_value *val)
82+
{
83+
struct opt3001_data *drv_data = dev->driver_data;
84+
s32_t uval;
85+
86+
if (chan != SENSOR_CHAN_LIGHT) {
87+
return -ENOTSUP;
88+
}
89+
90+
/**
91+
* sample consists of 4 bits of exponent and 12 bits of mantissa
92+
* bits 15 to 12 are exponent bits
93+
* bits 11 to 0 are the mantissa bits
94+
*
95+
* lux is the integer obtained using the following formula:
96+
* (2^(exponent value)) * 0.01 * mantisa value
97+
*/
98+
uval = (1 << (drv_data->sample >> OPT3001_SAMPLE_EXPONENT_SHIFT))
99+
* (drv_data->sample & OPT3001_MANTISSA_MASK);
100+
val->val1 = uval / 100;
101+
val->val2 = (uval % 100) * 10000;
102+
103+
return 0;
104+
}
105+
106+
static const struct sensor_driver_api opt3001_driver_api = {
107+
.sample_fetch = opt3001_sample_fetch,
108+
.channel_get = opt3001_channel_get,
109+
};
110+
111+
static int opt3001_chip_init(struct device *dev)
112+
{
113+
struct opt3001_data *drv_data = dev->driver_data;
114+
u16_t value;
115+
116+
drv_data->i2c = device_get_binding(DT_INST_0_TI_OPT3001_BUS_NAME);
117+
if (drv_data->i2c == NULL) {
118+
LOG_ERR("Failed to get pointer to %s device!",
119+
DT_INST_0_TI_OPT3001_BUS_NAME);
120+
return -EINVAL;
121+
}
122+
123+
if (opt3001_reg_read(drv_data, OPT3001_REG_MANUFACTURER_ID,
124+
&value) != 0) {
125+
return -EIO;
126+
}
127+
128+
if (value != OPT3001_MANUFACTURER_ID_VALUE) {
129+
LOG_ERR("Bad manufacturer id 0x%x", value);
130+
return -ENOTSUP;
131+
}
132+
133+
if (opt3001_reg_read(drv_data, OPT3001_REG_DEVICE_ID,
134+
&value) != 0) {
135+
return -EIO;
136+
}
137+
138+
if (value != OPT3001_DEVICE_ID_VALUE) {
139+
LOG_ERR("Bad device id 0x%x", value);
140+
return -ENOTSUP;
141+
}
142+
143+
if (opt3001_reg_update(drv_data, OPT3001_REG_CONFIG,
144+
OPT3001_CONVERSION_MODE_MASK,
145+
OPT3001_CONVERSION_MODE_CONTINUOUS) != 0) {
146+
LOG_ERR("Failed to set mode to continuous conversion");
147+
return -EIO;
148+
}
149+
150+
return 0;
151+
}
152+
153+
int opt3001_init(struct device *dev)
154+
{
155+
if (opt3001_chip_init(dev) < 0) {
156+
return -EINVAL;
157+
}
158+
159+
return 0;
160+
}
161+
162+
static struct opt3001_data opt3001_drv_data;
163+
164+
DEVICE_AND_API_INIT(opt3001, DT_INST_0_TI_OPT3001_LABEL, opt3001_init,
165+
&opt3001_drv_data, NULL, POST_KERNEL,
166+
CONFIG_SENSOR_INIT_PRIORITY, &opt3001_driver_api);

drivers/sensor/opt3001/opt3001.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2019 Actinius
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_DRIVERS_SENSOR_OPT3001_H_
8+
#define ZEPHYR_DRIVERS_SENSOR_OPT3001_H_
9+
10+
#include <misc/util.h>
11+
12+
#define OPT3001_REG_RESULT 0x00
13+
#define OPT3001_REG_CONFIG 0x01
14+
#define OPT3001_REG_MANUFACTURER_ID 0x7E
15+
#define OPT3001_REG_DEVICE_ID 0x7F
16+
17+
#define OPT3001_MANUFACTURER_ID_VALUE 0x5449
18+
#define OPT3001_DEVICE_ID_VALUE 0x3001
19+
20+
#define OPT3001_CONVERSION_MODE_MASK (BIT(10) | BIT(9))
21+
#define OPT3001_CONVERSION_MODE_CONTINUOUS (BIT(10) | BIT(9))
22+
23+
#define OPT3001_SAMPLE_EXPONENT_SHIFT 12
24+
#define OPT3001_MANTISSA_MASK 0xfff
25+
26+
struct opt3001_data {
27+
struct device *i2c;
28+
u16_t sample;
29+
};
30+
31+
#endif /* _SENSOR_OPT3001_ */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2019, Actinius
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
title: Texas Instruments OPT3001 Ambient light sensor
5+
6+
description: >
7+
This is a representation of the Texas Instruments OPT3001 Ambient light sensor
8+
9+
compatible: "ti,opt3001"
10+
11+
include: i2c-device.yaml

tests/drivers/build_all/dts_fixup.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@
264264
#define DT_INST_0_TI_HDC_BASE_ADDRESS 0
265265
#endif
266266

267+
#ifndef DT_INST_0_TI_OPT3001_LABEL
268+
#define DT_INST_0_TI_OPT3001_LABEL ""
269+
#define DT_INST_0_TI_OPT3001_BASE_ADDRESS 0x00
270+
#define DT_INST_0_TI_OPT3001_BUS_NAME ""
271+
#endif
272+
267273
#ifndef DT_INST_0_TI_TMP116_LABEL
268274
#define DT_INST_0_TI_TMP116_LABEL ""
269275
#define DT_INST_0_TI_TMP116_BASE_ADDRESS 0

tests/drivers/build_all/sensors_i_z.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CONFIG_MAX30101=y
2424
CONFIG_MAX44009=y
2525
CONFIG_MCP9808=y
2626
CONFIG_MPU6050=y
27+
CONFIG_OPT3001=y
2728
CONFIG_SHT3XD=y
2829
CONFIG_SI7006=y
2930
CONFIG_SI7060=y

0 commit comments

Comments
 (0)