1 /*
2  * Copyright (c) 2021 Leonard Pollak
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_SHT4X_SHT4X_H_
8 #define ZEPHYR_DRIVERS_SENSOR_SHT4X_SHT4X_H_
9 
10 #include <zephyr/device.h>
11 
12 #define SHT4X_CMD_READ_SERIAL	0x89
13 #define SHT4X_CMD_RESET		0x94
14 
15 #define SHT4X_RESET_WAIT_MS	1
16 
17 #define SHT4X_HEATER_POWER_IDX_MAX	3
18 #define SHT4X_HEATER_DURATION_IDX_MAX	2
19 
20 /*
21  * CRC parameters were taken from the
22  * "Checksum Calculation" section of the datasheet.
23  */
24 #define SHT4X_CRC_POLY		0x31
25 #define SHT4X_CRC_INIT		0xFF
26 
27 struct sht4x_config {
28 	struct i2c_dt_spec bus;
29 	uint8_t repeatability;
30 };
31 
32 struct sht4x_data {
33 	uint16_t t_sample;
34 	uint16_t rh_sample;
35 	uint8_t heater_power;
36 	uint8_t heater_duration;
37 };
38 
39 static const uint8_t measure_cmd[3] = {
40 	 0xE0, 0xF6, 0xFD
41 };
42 
43 static const uint16_t measure_wait_us[3] = {
44 	1700, 4500, 8200
45 };
46 
47 /*
48  * heater specifics
49  *
50  * power:
51  * High power heater pulse -> ~200 mW  @3.3V
52  * Medium power heater pulse -> ~110 mW  @3.3V
53  * Low power heater pulse -> ~20 mW  @3.3V
54  *
55  * duration:
56  * Long heater pulse -> 1.1s
57  * Short heater pulse -> 0.11s
58  */
59 
60 static const int8_t heater_cmd[SHT4X_HEATER_POWER_IDX_MAX][SHT4X_HEATER_DURATION_IDX_MAX] = {
61 	{ 0x39, 0x32 },
62 	{ 0x2F, 0x24 },
63 	{ 0x1E, 0x15 }
64 };
65 
66 static const uint32_t heater_wait_ms[SHT4X_HEATER_DURATION_IDX_MAX] = {
67 	1000, 100
68 };
69 
70 #endif /* ZEPHYR_DRIVERS_SENSOR_SHT4X_SHT4X_H_ */
71