1 /*
2  * Copyright (c) 2018 Jan Van Winkel <jan.van_winkel@dxplore.eu>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __SENSOR_MS5837_H__
8 #define __SENSOR_MS5837_H__
9 
10 #include <zephyr/types.h>
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/i2c.h>
13 
14 #define MS5837_CMD_RESET 0x1E
15 
16 #define MS5837_CMD_CONV_P_256  0x40
17 #define MS5837_CMD_CONV_P_512  0x42
18 #define MS5837_CMD_CONV_P_1024 0x44
19 #define MS5837_CMD_CONV_P_2048 0x46
20 #define MS5837_CMD_CONV_P_4096 0x48
21 #define MS5837_CMD_CONV_P_8192 0x4A
22 
23 #define MS5837_CMD_CONV_T_256  0x50
24 #define MS5837_CMD_CONV_T_512  0x52
25 #define MS5837_CMD_CONV_T_1024 0x54
26 #define MS5837_CMD_CONV_T_2048 0x56
27 #define MS5837_CMD_CONV_T_4096 0x58
28 #define MS5837_CMD_CONV_T_8192 0x5A
29 
30 #define MS5837_CMD_CONV_READ_ADC 0x00
31 
32 #define MS5837_CMD_CONV_READ_CRC      0xA0
33 #define MS5837_CMD_CONV_READ_SENS_T1  0xA2
34 #define MS5837_CMD_CONV_READ_OFF_T1   0xA4
35 #define MS5837_CMD_CONV_READ_TCS      0xA6
36 #define MS5837_CMD_CONV_READ_TCO      0xA8
37 #define MS5837_CMD_CONV_READ_T_REF    0xAA
38 #define MS5837_CMD_CONV_READ_TEMPSENS 0xAC
39 
40 #define MS5837_ADC_READ_DELAY_256  1
41 #define MS5837_ADC_READ_DELAY_512  2
42 #define MS5837_ADC_READ_DELAY_1024 3
43 #define MS5837_ADC_READ_DELAY_2048 5
44 #define MS5837_ADC_READ_DELAY_4086 10
45 #define MS5837_ADC_READ_DELAY_8129 20
46 
47 enum ms5837_type {
48 	MS5837_02BA01 = 0x00,
49 	MS5837_02BA21 = 0x15,
50 	MS5837_30BA26 = 0x1A
51 };
52 
53 typedef void (*ms5837_compensate_func)(const struct device *dev,
54 				       const int32_t adc_temperature,
55 				       const int32_t adc_pressure);
56 
57 struct ms5837_data {
58 	/* Calibration values */
59 	uint16_t factory;
60 	uint16_t sens_t1;
61 	uint16_t off_t1;
62 	uint16_t tcs;
63 	uint16_t tco;
64 	uint16_t t_ref;
65 	uint16_t tempsens;
66 
67 	/* Measured values */
68 	int32_t pressure;
69 	int32_t temperature;
70 
71 	/* Conversion commands */
72 	uint8_t presure_conv_cmd;
73 	uint8_t temperature_conv_cmd;
74 
75 	/* Conversion delay in ms*/
76 	uint8_t presure_conv_delay;
77 	uint8_t temperature_conv_delay;
78 
79 	ms5837_compensate_func comp_func;
80 };
81 
82 struct ms5837_config {
83 	struct i2c_dt_spec i2c;
84 };
85 
86 #endif /* __SENSOR_MS5837_H__ */
87