Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
apds9960.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Intel Corporation
3 * Copyright (c) 2018 PHYTEC Messtechnik GmbH
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_DRIVERS_SENSOR_APDS9960_APDS9960_H_
9#define ZEPHYR_DRIVERS_SENSOR_APDS9960_APDS9960_H_
10
11#include <zephyr/drivers/gpio.h>
12#include <zephyr/drivers/i2c.h>
13
14#define APDS9960_ENABLE_REG 0x80
15#define APDS9960_ENABLE_GEN BIT(6)
16#define APDS9960_ENABLE_PIEN BIT(5)
17#define APDS9960_ENABLE_AIEN BIT(4)
18#define APDS9960_ENABLE_WEN BIT(3)
19#define APDS9960_ENABLE_PEN BIT(2)
20#define APDS9960_ENABLE_AEN BIT(1)
21#define APDS9960_ENABLE_PON BIT(0)
22
23#define APDS9960_ATIME_REG 0x81
24#define APDS9960_WTIME_REG 0x83
25#define APDS9960_INT_AILTL_REG 0x84
26#define APDS9960_INT_AILTH_REG 0x85
27#define APDS9960_INT_AIHTL_REG 0x86
28#define APDS9960_INT_AIHTH_REG 0x87
29#define APDS9960_PILT_REG 0x89
30#define APDS9960_PIHT_REG 0x8B
31
32#define APDS9960_PERS_REG 0x8C
33#define APDS9960_PERS_PPERS (BIT(4) | BIT(5) | BIT(6) | BIT(7))
34#define APDS9960_APERS_MASK (BIT(0) | BIT(1) | BIT(2) | BIT(3))
35
36#define APDS9960_CONFIG1_REG 0x8D
37#define APDS9960_CONFIG1_WLONG BIT(1)
38
39#define APDS9960_PPULSE_REG 0x8E
40#define APDS9960_PPULSE_LENGTH_4US 0
41#define APDS9960_PPULSE_LENGTH_8US BIT(6)
42#define APDS9960_PPULSE_LENGTH_16US BIT(7)
43#define APDS9960_PPULSE_LENGTH_32US (BIT(7) | BIT(6))
44
45#define APDS9960_CONTROL_REG 0x8F
46#define APDS9960_CONTROL_LDRIVE (BIT(6) | BIT(7))
47#define APDS9960_CONTROL_PGAIN (BIT(3) | BIT(2))
48#define APDS9960_CONTROL_AGAIN (BIT(0) | BIT(1))
49/* LED Drive values */
50#define APDS9960_LED_DRIVE_100MA 0
51#define APDS9960_LED_DRIVE_50MA BIT(6)
52#define APDS9960_LED_DRIVE_25MA BIT(7)
53#define APDS9960_LED_DRIVE_12_5MA (BIT(6) | BIT(7))
54/* Proximity Gain (PGAIN) values */
55#define APDS9960_PGAIN_1X 0
56#define APDS9960_PGAIN_2X BIT(2)
57#define APDS9960_PGAIN_4X BIT(3)
58#define APDS9960_PGAIN_8X (BIT(2) | BIT(3))
59/* ALS Gain (AGAIN) values */
60#define APDS9960_AGAIN_1X 0
61#define APDS9960_AGAIN_4X BIT(0)
62#define APDS9960_AGAIN_16X BIT(1)
63#define APDS9960_AGAIN_64X (BIT(0) | BIT(1))
64
65#define APDS9960_CONFIG2_REG 0x90
66#define APDS9960_CONFIG2_CPSIEN BIT(6)
67#define APDS9960_CONFIG2_PSIEN BIT(7)
68/* LED Boost values */
69#define APDS9960_PLED_BOOST_100 0
70#define APDS9960_PLED_BOOST_150 BIT(4)
71#define APDS9960_PLED_BOOST_200 BIT(5)
72#define APDS9960_PLED_BOOST_300 (BIT(5) | BIT(4))
73
74#define APDS9960_ID_REG 0x92
75/* Acceptable device IDs */
76#define APDS9960_ID_1 0xAB
77#define APDS9960_ID_2 0x9C
78
79#define APDS9960_STATUS_REG 0x93
80#define APDS9960_STATUS_CPSAT BIT(7)
81#define APDS9960_STATUS_PGSAT BIT(6)
82#define APDS9960_STATUS_PINT BIT(5)
83#define APDS9960_STATUS_AINT BIT(4)
84#define APDS9960_STATUS_GINT BIT(2)
85#define APDS9960_STATUS_PVALID BIT(1)
86#define APDS9960_STATUS_AVALID BIT(0)
87
88#define APDS9960_CDATAL_REG 0x94
89#define APDS9960_CDATAH_REG 0x95
90#define APDS9960_RDATAL_REG 0x96
91#define APDS9960_RDATAH_REG 0x97
92#define APDS9960_GDATAL_REG 0x98
93#define APDS9960_GDATAH_REG 0x99
94#define APDS9960_BDATAL_REG 0x9A
95#define APDS9960_BDATAH_REG 0x9B
96#define APDS9960_PDATA_REG 0x9C
97
98#define APDS9960_POFFSET_UR_REG 0x9D
99#define APDS9960_POFFSET_DL_REG 0x9E
100
101#define APDS9960_CONFIG3_REG 0x9F
102#define APDS9960_CONFIG3_PCMP BIT(5)
103#define APDS9960_CONFIG3_SAI BIT(4)
104#define APDS9960_CONFIG3_PMSK_U BIT(3)
105#define APDS9960_CONFIG3_PMSK_D BIT(2)
106#define APDS9960_CONFIG3_PMSK_L BIT(1)
107#define APDS9960_CONFIG3_PMSK_R BIT(0)
108
109#define APDS9960_GPENTH_REG 0xA0
110#define APDS9960_GEXTH_REG 0xA1
111
112#define APDS9960_GCONFIG1_REG 0xA2
113#define APDS9960_GCONFIG1_GFIFOTH (BIT(7) | BIT(6))
114#define APDS9960_GCONFIG1_GEXMSK (BIT(5) | BIT(4) | BIT(3) | BIT(2))
115#define APDS9960_GCONFIG1_GEXPERS (BIT(1) | BIT(0))
116
117#define APDS9960_GCONFIG2_REG 0xA3
118#define APDS9960_GCONFIG2_GGAIN (BIT(6) | BIT(5))
119#define APDS9960_GCONFIG2_GLDRIVE (BIT(4) | BIT(3))
120#define APDS9960_GCONFIG2_WTIME (BIT(2) | BIT(1) | BIT(0))
121/* Gesture Gain (GGAIN) values */
122#define APDS9960_GGAIN_1X 0
123#define APDS9960_GGAIN_2X BIT(5)
124#define APDS9960_GGAIN_4X BIT(6)
125#define APDS9960_GGAIN_8X (BIT(6) | BIT(5))
126/* Gesture LED Drive Strength values */
127#define APDS9960_LED_GDRIVE_100MA 0
128#define APDS9960_LED_GDRIVE_50MA BIT(3)
129#define APDS9960_LED_GDRIVE_25MA BIT(4)
130#define APDS9960_LED_GDRIVE_12_5MA (BIT(4) | BIT(3))
131/* Gesture wait time values */
132#define APDS9960_GWTIME_0MS 0
133#define APDS9960_GWTIME_2_8MS 1
134#define APDS9960_GWTIME_5_6MS 2
135#define APDS9960_GWTIME_8_4MS 3
136#define APDS9960_GWTIME_14_0MS 4
137#define APDS9960_GWTIME_22_4MS 5
138#define APDS9960_GWTIME_30_8MS 6
139#define APDS9960_GWTIME_39_2MS 7
140
141#define APDS9960_GOFFSET_U_REG 0xA4
142#define APDS9960_GOFFSET_D_REG 0xA5
143#define APDS9960_GOFFSET_L_REG 0xA7
144#define APDS9960_GOFFSET_R_REG 0xA9
145
146#define APDS9960_GPULSE_REG 0xA6
147#define APDS9960_GPULSE_GPLEN (BIT(7) | BIT(6))
148#define APDS9960_GPULSE_GPULSE (BIT(5) | BIT(4) | BIT(3) |\
149 BIT(2) | BIT(1) | BIT(0))
150/* Gesture Pulse Length values */
151#define APDS9960_GPLEN_0US 0
152#define APDS9960_GPLEN_8US BIT(6)
153#define APDS9960_GPLEN_16US BIT(7)
154#define APDS9960_GPLEN_32US (BIT(7) | BIT(6))
155
156#define APDS9960_GCONFIG3_REG 0xAA
157#define APDS9960_GCONFIG3_GDIMS (BIT(1) | BIT(0))
158
159/* Gesture Registers */
160#define APDS9960_GCONFIG4_REG 0xAB
161#define APDS9960_GCONFIG4_GFIFO_CLR BIT(2)
162#define APDS9960_GCONFIG4_GIEN BIT(1)
163#define APDS9960_GCONFIG4_GMODE BIT(0)
164
165#define APDS9960_GFLVL_REG 0xAE
166
167#define APDS9960_GSTATUS_REG 0xAF
168#define APDS9960_GSTATUS_GFOV BIT(1)
169#define APDS9960_GSTATUS_GVALID BIT(0)
170
171#define APDS9960_IFORCE_REG 0xE4
172#define APDS9960_PICLEAR_REG 0xE5
173#define APDS9960_CICLEAR_REG 0xE6
174#define APDS9960_AICLEAR_REG 0xE7
175
176#define APDS9960_GFIFO_U_REG 0xFC
177#define APDS9960_GFIFO_D_REG 0xFD
178#define APDS9960_GFIFO_L_REG 0xFE
179#define APDS9960_GFIFO_R_REG 0xFF
180
181/* Default values */
182#define APDS9960_DEFAULT_ATIME 219
183#define APDS9960_DEFAULT_WTIME 255
184#define APDS9960_DEFAULT_CONFIG1 0x60
185#define APDS9960_DEFAULT_PERS BIT(4)
186#define APDS9960_DEFAULT_CONFIG2 (BIT(6) | BIT(0))
187#define APDS9960_DEFAULT_GESTURE_PPULSE 0x89
188#define APDS9960_DEFAULT_POFFSET_UR 0
189#define APDS9960_DEFAULT_POFFSET_DL 0
190#define APDS9960_DEFAULT_LDRIVE APDS9960_LED_DRIVE_100MA
191
192#ifdef CONFIG_APDS9960_TRIGGER
193#define APDS9960_DEFAULT_PILT 0
194#define APDS9960_DEFAULT_PIHT 50
195#define APDS9960_DEFAULT_AILT 10
196#define APDS9960_DEFAULT_AIHT 1000
197#define APDS9960_DEFAULT_CONFIG3 0
198#else
199#define APDS9960_DEFAULT_PILT 0
200#define APDS9960_DEFAULT_PIHT 1
201#define APDS9960_DEFAULT_AILT 0xFFFF
202#define APDS9960_DEFAULT_AIHT 0
203#define APDS9960_DEFAULT_CONFIG3 APDS9960_CONFIG3_SAI
204#endif
205
206#define APDS9960_DEFAULT_GPENTH 40
207#define APDS9960_DEFAULT_GEXTH 30
208#define APDS9960_DEFAULT_GCONF1 0x40
209#define APDS9960_DEFAULT_GGAIN APDS9960_GGAIN_4X
210#define APDS9960_DEFAULT_GLDRIVE APDS9960_LED_DRIVE_100MA
211#define APDS9960_DEFAULT_GWTIME APDS9960_GWTIME_2_8MS
212#define APDS9960_DEFAULT_GOFFSET 0
213#define APDS9960_DEFAULT_GPULSE 0xC9
214#define APDS9960_DEFAULT_GCONF3 0
215
216/* Polling Wait Times (ms) */
217#define APDS9960_DEFAULT_WAIT_TIME 2.78
218#define APDS9960_MAX_WAIT_TIME 10000
219
224
227#ifdef CONFIG_APDS9960_FETCH_MODE_INTERRUPT
228 struct gpio_dt_spec int_gpio;
229#endif
234#ifdef CONFIG_APDS9960_ENABLE_GESTURE
235 struct apds9960_gesture_setup gesture_config;
236#endif
237};
238
239/* apds9960 specific channels */
243
251
254 struct k_work work;
255 const struct device *dev;
259
260#ifdef CONFIG_APDS9960_TRIGGER
261 sensor_trigger_handler_t p_th_handler;
262 const struct sensor_trigger *p_th_trigger;
263#elif CONFIG_APDS9960_FETCH_MODE_INTERRUPT
264 struct k_sem data_sem;
265#endif
266};
267
268#ifdef CONFIG_APDS9960_FETCH_MODE_INTERRUPT
269static inline void apds9960_setup_int(const struct apds9960_config *cfg,
270 bool enable)
271{
272 unsigned int flags = enable
275
276 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, flags);
277}
278#endif
279
280#ifdef CONFIG_APDS9960_TRIGGER
281void apds9960_work_cb(struct k_work *work);
282
283int apds9960_attr_set(const struct device *dev,
284 enum sensor_channel chan,
285 enum sensor_attribute attr,
286 const struct sensor_value *val);
287
288int apds9960_trigger_set(const struct device *dev,
289 const struct sensor_trigger *trig,
291#endif /* CONFIG_APDS9960_TRIGGER */
292
293#endif /* ZEPHYR_DRIVERS_SENSOR_APDS9960_APDS9960_H_*/
apds9960_gesture
Definition apds9960.h:244
@ APDS9960_GESTURE_UP
Definition apds9960.h:246
@ APDS9960_GESTURE_DOWN
Definition apds9960.h:247
@ APDS9960_GESTURE_LEFT
Definition apds9960.h:248
@ APDS9960_GESTURE_NONE
Definition apds9960.h:245
@ APDS9960_GESTURE_RIGHT
Definition apds9960.h:249
sensor_channel_apds9960
Definition apds9960.h:240
@ SENSOR_CHAN_APDS9960_GESTURE
Definition apds9960.h:241
Main header file for GPIO driver API.
Main header file for I2C (Inter-Integrated Circuit) driver API.
static int gpio_pin_interrupt_configure_dt(const struct gpio_dt_spec *spec, gpio_flags_t flags)
Configure pin interrupts from a gpio_dt_spec.
Definition gpio.h:972
#define GPIO_INT_EDGE_TO_ACTIVE
Configures GPIO interrupt to be triggered on pin state change to logical level 1 and enables it.
Definition gpio.h:210
#define GPIO_INT_DISABLE
Disables GPIO pin interrupt.
Definition gpio.h:108
sensor_attribute
Sensor attribute types.
Definition sensor.h:342
void(* sensor_trigger_handler_t)(const struct device *dev, const struct sensor_trigger *trigger)
Callback API upon firing of a trigger.
Definition sensor.h:422
sensor_channel
Sensor channels.
Definition sensor.h:65
@ SENSOR_CHAN_PRIV_START
This and higher values are sensor specific.
Definition sensor.h:247
flags
Definition parser.h:97
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Definition apds9960.h:225
uint8_t pgain
Definition apds9960.h:230
uint8_t again
Definition apds9960.h:231
struct i2c_dt_spec i2c
Definition apds9960.h:226
uint8_t pled_boost
Definition apds9960.h:233
uint8_t ppcount
Definition apds9960.h:232
Definition apds9960.h:252
const struct device * dev
Definition apds9960.h:255
struct k_work work
Definition apds9960.h:254
enum apds9960_gesture gesture
Definition apds9960.h:258
uint8_t pdata
Definition apds9960.h:257
struct gpio_callback gpio_cb
Definition apds9960.h:253
uint16_t sample_crgb[4]
Definition apds9960.h:256
Definition apds9960.h:220
int proximity
Definition apds9960.h:221
int ir_difference
Definition apds9960.h:222
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
GPIO callback structure.
Definition gpio.h:748
Container for GPIO pin information specified in devicetree.
Definition gpio.h:296
Complete I2C DT information.
Definition i2c.h:78
Semaphore structure.
Definition kernel.h:3609
A structure used to submit work.
Definition kernel.h:4505
Sensor trigger spec.
Definition sensor.h:332
Representation of a sensor readout value.
Definition sensor.h:55