1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_SHT3XD_SHT3XD_H_
8 #define ZEPHYR_DRIVERS_SENSOR_SHT3XD_SHT3XD_H_
9 
10 #include <zephyr/device.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/drivers/i2c.h>
14 
15 #define SHT3XD_CMD_FETCH                0xE000
16 #define SHT3XD_CMD_ART                  0x2B32
17 #define SHT3XD_CMD_READ_STATUS          0xF32D
18 #define SHT3XD_CMD_CLEAR_STATUS         0x3041
19 
20 #define SHT3XD_CMD_WRITE_TH_HIGH_SET    0x611D
21 #define SHT3XD_CMD_WRITE_TH_HIGH_CLEAR  0x6116
22 #define SHT3XD_CMD_WRITE_TH_LOW_SET     0x610B
23 #define SHT3XD_CMD_WRITE_TH_LOW_CLEAR   0x6100
24 
25 #if CONFIG_SHT3XD_REPEATABILITY_LOW
26 	#define SHT3XD_REPEATABILITY_IDX        0
27 #elif CONFIG_SHT3XD_REPEATABILITY_MEDIUM
28 	#define SHT3XD_REPEATABILITY_IDX        1
29 #elif CONFIG_SHT3XD_REPEATABILITY_HIGH
30 	#define SHT3XD_REPEATABILITY_IDX        2
31 #endif
32 
33 #if CONFIG_SHT3XD_MPS_05
34 	#define SHT3XD_MPS_IDX          0
35 #elif CONFIG_SHT3XD_MPS_1
36 	#define SHT3XD_MPS_IDX          1
37 #elif CONFIG_SHT3XD_MPS_2
38 	#define SHT3XD_MPS_IDX          2
39 #elif CONFIG_SHT3XD_MPS_4
40 	#define SHT3XD_MPS_IDX          3
41 #elif CONFIG_SHT3XD_MPS_10
42 	#define SHT3XD_MPS_IDX          4
43 #endif
44 
45 #define SHT3XD_CLEAR_STATUS_WAIT_USEC   1000
46 
47 struct sht3xd_config {
48 	struct i2c_dt_spec bus;
49 
50 #ifdef CONFIG_SHT3XD_TRIGGER
51 	struct gpio_dt_spec alert_gpio;
52 #endif /* CONFIG_SHT3XD_TRIGGER */
53 };
54 
55 struct sht3xd_data {
56 	uint16_t t_sample;
57 	uint16_t rh_sample;
58 
59 #ifdef CONFIG_SHT3XD_TRIGGER
60 	const struct device *dev;
61 	struct gpio_callback alert_cb;
62 
63 	uint16_t t_low;
64 	uint16_t t_high;
65 	uint16_t rh_low;
66 	uint16_t rh_high;
67 
68 	sensor_trigger_handler_t handler;
69 	const struct sensor_trigger *trigger;
70 
71 #if defined(CONFIG_SHT3XD_TRIGGER_OWN_THREAD)
72 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_SHT3XD_THREAD_STACK_SIZE);
73 	struct k_sem gpio_sem;
74 	struct k_thread thread;
75 #elif defined(CONFIG_SHT3XD_TRIGGER_GLOBAL_THREAD)
76 	struct k_work work;
77 #endif
78 
79 #endif /* CONFIG_SHT3XD_TRIGGER */
80 };
81 
82 #ifdef CONFIG_SHT3XD_TRIGGER
83 int sht3xd_write_command(const struct device *dev, uint16_t cmd);
84 
85 int sht3xd_write_reg(const struct device *dev, uint16_t cmd, uint16_t val);
86 
87 int sht3xd_attr_set(const struct device *dev,
88 		    enum sensor_channel chan,
89 		    enum sensor_attribute attr,
90 		    const struct sensor_value *val);
91 
92 int sht3xd_trigger_set(const struct device *dev,
93 		       const struct sensor_trigger *trig,
94 		       sensor_trigger_handler_t handler);
95 
96 int sht3xd_init_interrupt(const struct device *dev);
97 #endif
98 
99 #endif /* ZEPHYR_DRIVERS_SENSOR_SHT3XD_SHT3XD_H_ */
100