yincheng.zhong
2024-03-01 e709bfae051d9888674f64e3d262ce7b7969db46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (c) 2015-2016 Infineon Technologies AG
 *
 * Driver for Infineon DPS310 Digital Barometric Pressure Sensor
 *
 *
 */
 
#ifndef DPS310_H_INCLUDED
#define DPS310_H_INCLUDED
#include <stdbool.h>
/* Attributes: Product identification and version */
 
#define     VENDOR_NAME                                 "Infineon"
#define     DRIVER_NAME                                 "IFXDD"
#define     DEVICE_NAME                                 "Digital Barometric Pressure Sensor"
#define     DEVICE_MODEL_NAME                           "DPS310"
#define     DEVICE_HW_VERSION                           1.0
#define     DRIVER_VERSION                              1.0
#define     DEVICE_PROD_REV_ID                          0x10
 
/* Attributes: Device performance :Pressure Sensing */
#define     IFX_DPS310_PROD_REV_ID_REG_ADDR             0x0D
#define     IFX_DPS310_PROD_REV_ID_LEN                  1
#define     IFX_DSPS310_PROD_REV_ID_VAL                 DEVICE_PROD_REV_ID
 
#define     IFX_DPS310_SOFT_RESET_REG_ADDR              0x0C
#define     IFX_DPS310_SOFT_RESET_REG_DATA              0x09
#define     IFX_DPS310_SOFT_RESET_REG_LEN               1
#define     IFX_DPS310_SOFT_RESET_VERIFY_REG_ADDR       0x06
 
#define     IFX_DPS310_COEF_REG_ADDR                    0x10
#define     IFX_DPS310_COEF_LEN                         18    // Length in bytes
 
#define     IFX_DPS310_TMP_COEF_SRCE_REG_ADDR           0x28
#define     IFX_DPS310_TMP_COEF_SRCE_REG_LEN            1    // Length in bytes
#define     IFX_DPS310_TMP_COEF_SRCE_REG_POS_MASK       7    // Length in bytes
 
#define     IFX_DPS310_PSR_TMP_READ_REG_ADDR            0x00
#define     IFX_DPS310_PSR_TMP_READ_LEN                 6
 
#define     IFX_DPS310_PRS_CFG_REG_ADDR                 0x06
#define     IFX_DPS310_PRS_CFG_REG_LEN                  1
 
#define     IFX_DPS310_TMP_CFG_REG_ADDR                 0x07
#define     IFX_DPS310_TMP_CFG_REG_LEN                  1
 
#define     IFX_DPS310_MEAS_CFG_REG_ADDR                0x08
#define     IFX_DPS310_MEAS_CFG_REG_LEN                 1
 
#define     IFX_DPS310_CFG_REG_ADDR                     0x09
#define     IFX_DPS310_CFG_REG_LEN                      1
 
#define     IFX_DPS310_CFG_TMP_SHIFT_EN_SET_VAL         0x08
#define     IFX_DPS310_CFG_PRS_SHIFT_EN_SET_VAL         0x04
 
 
#define     IFX_DPS310_FIFO_READ_REG_ADDR               0x00
#define     IFX_DPS310_FIFO_REG_READ_LEN                3
#define     IFX_DPS310_FIFO_BYTES_PER_ENTRY             3
 
#define     IFX_DPS310_FIFO_FLUSH_REG_ADDR              0x0C
#define     IFX_DPS310_FIFO_FLUSH_REG_VAL               0b1000000U
 
#define     IFX_DPS310_CFG_SPI_MODE_POS                 0
#define     IFX_DPS310_CFG_SPI_MODE_3_WIRE_VAL          1
#define     IFX_DPS310_CFG_SPI_MODE_4_WIRE_VAL          0
 
#define     IFX_DPS310_CFG_FIFO_ENABLE_POS              1
#define     IFX_DPS310_CFG_FIFO_ENABLE_VAL              1
#define     IFX_DPS310_CFG_FIFO_DISABLE_VAL             0
 
#define     IFX_DPS310_CFG_INTR_PRS_ENABLE_POS          4
#define     IFX_DPS310_CFG_INTR_PRS_ENABLE_VAL          1U
#define     IFX_DPS310_CFG_INTR_PRS_DISABLE_VAL         0U
 
#define     IFX_DPS310_CFG_INTR_TEMP_ENABLE_POS         5
#define     IFX_DPS310_CFG_INTR_TEMP_ENABLE_VAL         1U
#define     IFX_DPS310_CFG_INTR_TEMP_DISABLE_VAL        0U
 
#define     IFX_DPS310_CFG_INTR_FIFO_FULL_ENABLE_POS    6
#define     IFX_DPS310_CFG_INTR_FIFO_FULL_ENABLE_VAL    1U
#define     IFX_DPS310_CFG_INTR_FIFO_FULL_DISABLE_VAL   0U
 
#define     IFX_DPS310_CFG_INTR_LEVEL_TYP_SEL_POS       7
#define     IFX_DPS310_CFG_INTR_LEVEL_TYP_ACTIVE_H      1U
#define     IFX_DPS310_CFG_INTR_LEVEL_TYP_ACTIVE_L      0U
 
#define     IFX_DPS310_INTR_SOURCE_PRESSURE             0
#define     IFX_DPS310_INTR_SOURCE_TEMPERATURE          1
#define     IFX_DPS310_INTR_SOURCE_BOTH                 2
 
#define     IFX_DPS310_INTR_STATUS_REG_ADDR             0x0A
#define     IFX_DPS310_INTR_STATUS_REG_LEN              1
#define     IFX_DPS310_INTR_DISABLE_ALL                (uint8_t)0b10001111
 
#define     EINVAL                                      1
#define     EIO                                         2
 
#ifndef NULL
#define     NULL                                        ((void*)0)
#endif // NULL
 
 
/* _______________________________________________________ */
 
#define POW_2_23_MINUS_1    0x7FFFFF   //implies 2^23-1
#define POW_2_24            0x1000000
#define POW_2_15_MINUS_1    0x7FFF
#define POW_2_16            0x10000
#define POW_2_11_MINUS_1    0x7FF
#define POW_2_12            0x1000
#define POW_2_20            0x100000
#define POW_2_19_MINUS_1    524287
 
/* _______________________________________________________ */
 
/*Some aliases*/
typedef unsigned char       u8;
 
typedef char                s8;
 
typedef unsigned short      u16;
 
typedef short               s16;
 
typedef long                s32;
 
typedef    long long           s64;
 
//typedef    unsigned long       u32;
 
typedef    unsigned long long  u64;
 
typedef float               f32;
 
typedef double              f64;
 
//typedef u8                  bool;
 
#define false               0
#define true                1
 
/* Struct to hold calibration coefficients read from device*/
typedef struct
{
  /* calibration registers */
 
  s16     C0;    // 12bit
  s16     C1;    // 12bit
  s32    C00;    // 20bit
  s32   C10;    // 20bit
  s16     C01;    // 16bit
  s16    C11;    // 16bit
  s16    C20;    // 16bit
  s16    C21;    // 16bit
  s16    C30;    // 16bit
 
}dps310_cal_coeff_regs_s;
 
 
/* enum for seeting/getting device operating mode*/
 
typedef enum
    {DPS310_MODE_IDLE = 0,
  DPS310_MODE_COMMAND_PRESSURE       =  1,
  DPS310_MODE_COMMAND_TEMPERATURE    =  2,
  DPS310_MODE_BACKGROUND_PRESSURE    =  5,
  DPS310_MODE_BACKGROUND_TEMPERATURE =  6,
  DPS310_MODE_BACKGROUND_ALL         =  7
 
}dps310_operating_modes_e;
 
 
 
/* enum of scaling coefficients either Kp or Kt*/
typedef enum
{
    OSR_SF_1   = 524288,
    OSR_SF_2   = 1572864,
    OSR_SF_4   = 3670016,
    OSR_SF_8   = 7864320,
    OSR_SF_16  = 253952,
    OSR_SF_32  = 516096,
    OSR_SF_64  = 1040384,
    OSR_SF_128 = 2088960
 
} dps310_scaling_coeffs_e;
 
 
 
/* enum of oversampling rates for pressure and temperature*/
typedef enum
{
    OSR_1   = 0,
    OSR_2   = 1,
    OSR_4   = 2,
    OSR_8   = 3,
    OSR_16  = 4,
    OSR_32  = 5,
    OSR_64  = 6,
    OSR_128 = 7
 
} dps310_osr_e;
 
 
 
/* enum of measurement rates for pressure*/
 
typedef enum
{
    PM_MR_1   = 0,
    PM_MR_2   = 0x10,
    PM_MR_4   = 0x20,
    PM_MR_8   = 0x30,
    PM_MR_16  = 0x40,
    PM_MR_32  = 0x50,
    PM_MR_64  = 0x60,
    PM_MR_128 = 0x70
 
} dps310_pm_rate_e;
 
 
 
/* enum of measurement rates for temperature*/
 
typedef enum
{
    TMP_MR_1   = 0,
    TMP_MR_2   = 0x10,
    TMP_MR_4   = 0x20,
    TMP_MR_8   = 0x30,
    TMP_MR_16  = 0x40,
    TMP_MR_32  = 0x50,
    TMP_MR_64  = 0x60,
    TMP_MR_128 = 0x70
 
} dps310_tmp_rate_e;
 
/* enum of oversampling and measurement rates*/
 
typedef enum
 
{
    TMP_EXT_ASIC = 0x00,
    TMP_EXT_MEMS = 0x80,
 
}dps310_temperature_src_e;
 
 
/*Please update callbacks for bus communication
* callbacks are protocol agnostic/abstract so
* as to wrap around I2C or SPI low level protocols
*/
 
typedef struct {
 
        /*Provide a wrapper for single byte read/write and multi byte read
        * all callbacks return negative values to indicate error
        * however, read_byte must return the content in case of successful read
        * and read_block shall return number of bytes read successfully
        * For write_byte non zero return value shall indicate successful write
        */
        s16 (*read_byte)(u8 address);
 
        s16 (*read_block)(u8 address, u8 length, u8 *read_buffer);
 
        s16 (*write_byte)(u8 address, u8 data);
 
        /*It is expected to provide a wrapper for incorporating delay
        * the delay shall be in milliseconds. This is required as
        * after powering up the sensor, it takes 40ms until fused
        * calibration coefficients are ready to read.
        * in case this delay is handled appropriately by caller by other mechanism
        * please set this callback to NULL
        */
        void (*delayms)(u8 duration);
 
}dps310_bus_connection;
 
 
struct dps310_state {
 
        dps310_scaling_coeffs_e   tmp_osr_scale_coeff;                    /* Temperature scaling coefficient*/
        dps310_scaling_coeffs_e   prs_osr_scale_coeff;                    /* Pressure scaling coefficient*/
        dps310_cal_coeff_regs_s   calib_coeffs;                           /* Calibration coefficients index */
        dps310_operating_modes_e  dev_mode;                               /* Current operating mode of device */
        dps310_pm_rate_e          press_mr;                  /* Current measurement readout rate (ODR) for pressure */
        dps310_tmp_rate_e         temp_mr;                  /* Current measurement readout rate (ODR) for temperature */
        dps310_osr_e              temp_osr;                  /* Current oversampling rate (OSR) for temperature */
        dps310_osr_e              press_osr;                  /* Current oversampling rate (OSR) for pressure */
        dps310_temperature_src_e  tmp_ext;                                /* Temperature ASIC or MEMS. Should always be set MEMS*/
        u8                        cfg_word;                               /* Keep the contents of CFG register as it gets configured
                                                                            to avoid excessive bus transactions */
    bool                           enable;
    dps310_bus_connection         *io;                                    /*To access bus communication call backs */
};
 
 
/* public function prototypes */
int
dps310_init
(
 struct dps310_state *drv_state,
 dps310_bus_connection *io
);
 
 
int dps310_get_processed_data
(
 struct dps310_state *drv_state,
 f64 *pressure,
 f64 *temperature
 );
 
 
int
dps310_config
(
 struct dps310_state *drv_state,
 dps310_osr_e osr_temp,
 dps310_tmp_rate_e mr_temp,
 dps310_osr_e osr_press,
 dps310_pm_rate_e mr_press,
 dps310_temperature_src_e temp_src
 );
 
int
dps310_standby
(
 struct dps310_state *drv_state
);
 
int
dps310_resume
(
 struct dps310_state *drv_state
);
int BarInit(void);
float GetPressAndHeight(void);
#endif // DPS310_H_INCLUDED