WXK
2025-05-20 65f1d70842d83f92aa31d79c97e364b4b0fd9ab6
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (c) 2015-2016 Infineon Technologies AG
 *
 * Driver for Infineon DPS310 Digital Barometric Pressure Sensor
 *
 *
 */
 
#include "dps310.h"
 
/* Meaningful Default Configuration */
#define     IFX_DPS310_TEMPERATURE_OSR                  OSR_1
#define     IFX_DPS310_PRESSURE_OSR                     OSR_128
#define     IFX_DPS310_TEMPERATURE_MR                   TMP_MR_1
#define     IFX_DPS310_PRESSURE_MR                      PM_MR_1
 
/**/
 
static dps310_scaling_coeffs_e
dps310_get_scaling_coef (dps310_osr_e osr)
{
        dps310_scaling_coeffs_e scaling_coeff;
 
        switch (osr){
 
              case OSR_1:
                    scaling_coeff = OSR_SF_1;
                    break;
              case OSR_2:
                    scaling_coeff = OSR_SF_2;
                    break;
              case OSR_4:
                    scaling_coeff = OSR_SF_4;
                    break;
              case OSR_8:
                    scaling_coeff = OSR_SF_8;
                    break;
              case OSR_16:
                    scaling_coeff = OSR_SF_16;
                    break;
              case OSR_32:
                    scaling_coeff = OSR_SF_32;
                    break;
              case OSR_64:
                    scaling_coeff = OSR_SF_64;
                    break;
              case OSR_128:
                    scaling_coeff = OSR_SF_128;
                    break;
              default:
                     scaling_coeff = OSR_SF_1;
                     break;
        }
        return scaling_coeff;
}
 
static int dps310_read_calib_coeffs(struct dps310_state *drv_state)
 
{
        s32 ret;
        u8 read_buffer[IFX_DPS310_COEF_LEN] = {0};
 
        if (drv_state == NULL)
            return -EINVAL;
 
        ret =  drv_state->io->read_block((u8)IFX_DPS310_COEF_REG_ADDR,
                                 (u8)IFX_DPS310_COEF_LEN, read_buffer);
 
        if ( ret != IFX_DPS310_COEF_LEN )
            return ret;
 
        drv_state->calib_coeffs.C0 = (read_buffer[0] << 4) + ((read_buffer[1] >>4) & 0x0F);
 
        if(drv_state->calib_coeffs.C0 > POW_2_11_MINUS_1)
            drv_state->calib_coeffs.C0 = drv_state->calib_coeffs.C0 - POW_2_12;
 
        drv_state->calib_coeffs.C1 = (read_buffer[2] + ((read_buffer[1] & 0x0F)<<8));
 
        if(drv_state->calib_coeffs.C1 > POW_2_11_MINUS_1)
            drv_state->calib_coeffs.C1 = drv_state->calib_coeffs.C1 - POW_2_12;
 
        drv_state->calib_coeffs.C00 = ((read_buffer[4]<<4) + (read_buffer[3]<<12)) + ((read_buffer[5]>>4) & 0x0F);
 
        if(drv_state->calib_coeffs.C00 > POW_2_19_MINUS_1)
            drv_state->calib_coeffs.C00 = drv_state->calib_coeffs.C00 -POW_2_20;
 
        drv_state->calib_coeffs.C10 = ((read_buffer[5] & 0x0F)<<16) + read_buffer[7] + (read_buffer[6]<<8);
 
        if(drv_state->calib_coeffs.C10 > POW_2_19_MINUS_1)
            drv_state->calib_coeffs.C10 = drv_state->calib_coeffs.C10 - POW_2_20;
 
        drv_state->calib_coeffs.C01 = (read_buffer[9] + (read_buffer[8]<<8));
 
        if(drv_state->calib_coeffs.C01 > POW_2_15_MINUS_1)
            drv_state->calib_coeffs.C01 = drv_state->calib_coeffs.C01 - POW_2_16;
 
        drv_state->calib_coeffs.C11 = (read_buffer[11] + (read_buffer[10]<<8));
 
        if(drv_state->calib_coeffs.C11 > POW_2_15_MINUS_1)
            drv_state->calib_coeffs.C11 = drv_state->calib_coeffs.C11 - POW_2_16;
 
        drv_state->calib_coeffs.C20 = (read_buffer[13] + (read_buffer[12]<<8));
 
        if(drv_state->calib_coeffs.C20 > POW_2_15_MINUS_1)
            drv_state->calib_coeffs.C20 = drv_state->calib_coeffs.C20 - POW_2_16;
 
        drv_state->calib_coeffs.C21 = (read_buffer[15] + (read_buffer[14]<<8));
 
        if(drv_state->calib_coeffs.C21 > POW_2_15_MINUS_1)
            drv_state->calib_coeffs.C21 = drv_state->calib_coeffs.C21 - POW_2_16;
 
        drv_state->calib_coeffs.C30 = (read_buffer[17] + (read_buffer[16]<<8));
 
        if(drv_state->calib_coeffs.C30 > POW_2_15_MINUS_1)
            drv_state->calib_coeffs.C30 = drv_state->calib_coeffs.C30 - POW_2_16;
 
        /* lets see which temperature diode is used for calibration and update state accordingly*/
        ret = drv_state->io->read_byte(IFX_DPS310_TMP_COEF_SRCE_REG_ADDR);
 
        if (ret < 0){
                return -EIO;
        }
        if ((ret >> IFX_DPS310_TMP_COEF_SRCE_REG_POS_MASK) & 1 ){
                drv_state->tmp_ext = TMP_EXT_MEMS;
        }
        else{
                drv_state->tmp_ext = TMP_EXT_ASIC;
        }
        return 0;
}
 
 
int dps310_resume(struct dps310_state *drv_state)
{
        s32 ret;
        if (drv_state == NULL)
            return -EINVAL;
 
        ret = drv_state->io->write_byte(IFX_DPS310_MEAS_CFG_REG_ADDR,
                                        (u8)DPS310_MODE_BACKGROUND_ALL);
        if (ret < 0)
                return -EIO;
 
        drv_state->dev_mode = DPS310_MODE_BACKGROUND_ALL;
 
        return 0;
 
}
 
 
int dps310_standby(struct dps310_state *drv_state)
{
        s32 ret;
        if (drv_state == NULL)
          return -EINVAL;
 
        ret = drv_state->io->write_byte(IFX_DPS310_MEAS_CFG_REG_ADDR,
                                       (u8)DPS310_MODE_IDLE);
        if (ret < 0)
                return -EIO;
 
        drv_state->dev_mode = DPS310_MODE_IDLE;
 
        return 0;
}
 
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)
{
 
        s32 ret;
        u8  config;
 
        if (drv_state == NULL)
            return -EINVAL;
 
       /* configure temperature measurements first*/
       /*Prepare a configuration word for TMP_CFG register*/
        config = (u8) temp_src;
 
        /*First Set the TMP_RATE[2:0] -> 6:4 */
        config |= ((u8)mr_temp);
 
       /*Set the TMP_PRC[3:0] -> 2:0 */
        config |= ((u8)osr_temp);
 
        ret = drv_state->io->write_byte(IFX_DPS310_TMP_CFG_REG_ADDR,
                                  config);
        if (ret < 0)
            return -EIO;
 
        /*Prepare a configuration word for PRS_CFG register*/
        /*First Set the PM_RATE[2:0] -> 6:4 */
        config = (u8) ( 0x00 ) | ((u8)mr_press);
 
        /*Set the PM_PRC[3:0] -> 3:0 */
        config |= ((u8)osr_press);
 
        ret = drv_state->io->write_byte(IFX_DPS310_PRS_CFG_REG_ADDR,
                                  config);
        if (ret < 0)
            return -EIO;
 
        /* always take configuration word from state*/
        config = drv_state->cfg_word;
 
        /*If oversampling rate for temperature is greater than 8 times, then set TMP_SHIFT bit in CFG_REG */
        if ((u8)osr_temp > (u8) OSR_8){
                config |= (u8) IFX_DPS310_CFG_TMP_SHIFT_EN_SET_VAL;
        }
 
        /*If oversampling rate for pressure is greater than 8 times, then set P_SHIFT bit in CFG_REG */
        if ((u8)osr_press > (u8) OSR_8){
                config |= (u8) IFX_DPS310_CFG_PRS_SHIFT_EN_SET_VAL;
        }
 
        /* write CFG_REG */
        ret =  drv_state->io->write_byte(IFX_DPS310_CFG_REG_ADDR,
                                  config);
        if (ret < 0)
            return -EIO;
 
       /*Update state accordingly with proper scaling factors based on oversampling rates*/
        drv_state->tmp_osr_scale_coeff = dps310_get_scaling_coef(osr_temp);
 
        drv_state->prs_osr_scale_coeff = dps310_get_scaling_coef(osr_press);
 
        drv_state->press_mr = mr_press;
 
        drv_state->temp_mr  = mr_temp;
 
        drv_state->temp_osr = osr_temp;
 
        drv_state->press_osr = osr_press;
 
        drv_state->tmp_ext  = temp_src;
 
        return 0;
 
}
 
 
int dps310_get_processed_data (struct dps310_state *drv_state,
                                      f64 *pressure,
                                      f64 *temperature)
{
        s32    ret;
        u8     read_buffer[IFX_DPS310_PSR_TMP_READ_LEN] = {0};
        f64    press_raw;
        f64    temp_raw;
 
        f64 temp_scaled;
        f64 temp_final;
        f64 press_scaled;
        f64 press_final;
 
        if (drv_state == NULL)
            return -EINVAL;
 
        ret = test_read_block(IFX_DPS310_PSR_TMP_READ_REG_ADDR,
                              IFX_DPS310_PSR_TMP_READ_LEN,
                              read_buffer);
 
        if (ret < IFX_DPS310_PSR_TMP_READ_LEN)
            return -EINVAL;
 
        press_raw = (read_buffer[2]) + (read_buffer[1]<<8) + (read_buffer[0] <<16);
        temp_raw  = (read_buffer[5]) + (read_buffer[4]<<8) + (read_buffer[3] <<16);
 
        if(temp_raw > POW_2_23_MINUS_1){
            temp_raw = temp_raw - POW_2_24;
        }
 
        if(press_raw > POW_2_23_MINUS_1){
            press_raw = press_raw - POW_2_24;
        }
 
        temp_scaled = (double)temp_raw / (double) (drv_state->tmp_osr_scale_coeff);
 
        temp_final =  (drv_state->calib_coeffs.C0 /2.0f) + drv_state->calib_coeffs.C1 * temp_scaled ;
 
        press_scaled = (double) press_raw / drv_state->prs_osr_scale_coeff;
 
        press_final = drv_state->calib_coeffs.C00 +
                      press_scaled *  (  drv_state->calib_coeffs.C10 + press_scaled *
                      ( drv_state->calib_coeffs.C20 + press_scaled * drv_state->calib_coeffs.C30 )  ) +
                      temp_scaled * drv_state->calib_coeffs.C01 +
                      temp_scaled * press_scaled * ( drv_state->calib_coeffs.C11 +
                                                      press_scaled * drv_state->calib_coeffs.C21 );
 
        press_final = press_final * 0.01f;    //to convert it into mBar
 
        *temperature = temp_final;
        *pressure    = press_final;  //press_final;
 
        return 0;
}
 
int dps310_init(struct dps310_state *drv_state, dps310_bus_connection *io)
{
        s32 ret;
 
        if (!drv_state){
            return -EINVAL;
        }
 
        if (!io){
            return -EINVAL;
        }
 
        drv_state->cfg_word = 0;
        drv_state->enable = 0;
       
        /*first verify chip by reading product and rev id*/
        ret = io->read_byte(IFX_DPS310_PROD_REV_ID_REG_ADDR);
 
        if (ret < 0){
            ret = -EIO;
                goto err_handler_iio;
        }
 
        if (ret != IFX_DSPS310_PROD_REV_ID_VAL){
            ret = -EINVAL;
            goto err_handler_iio;
        }
 
        /* attach bus connection instance to state*/
        drv_state->io = io;
 
        /* from here wait for about 40ms till calibration coefficients become available*/
        if (drv_state->io->delayms != NULL)
            drv_state->io->delayms(40);
 
        /* read now the calibration coeffs, temperature coef source and store in driver state*/
        ret = dps310_read_calib_coeffs(drv_state);
 
        if (ret < 0){
                goto err_handler_iio;
        }
 
        /* Now apply ADC Temperature gain settings*/
        /* First write valid signature on 0x0e and 0x0f
         * to unlock address 0x62 */
        drv_state->io->write_byte((u8)0x0e,(u8)0xa5);
        drv_state->io->write_byte((u8)0x0f,(u8)0x96);
        /*Then update high gain value for Temperature*/
        drv_state->io->write_byte((u8)0x62,(u8)0x02);
 
        /*Finally lock back the location 0x62*/
        drv_state->io->write_byte((u8)0x0e,(u8)0x00);
        drv_state->io->write_byte((u8)0x0f,(u8)0x00);
 
 
        /* configure sensor for default ODR settings*/
        ret = dps310_config(drv_state,
                            IFX_DPS310_TEMPERATURE_OSR,
                            IFX_DPS310_TEMPERATURE_MR,
                            IFX_DPS310_PRESSURE_OSR,
                            IFX_DPS310_PRESSURE_MR,
                            drv_state->tmp_ext);
        if (ret < 0){
            goto err_handler_iio;
        }
 
        /* activate sensor*/
        ret = dps310_resume(drv_state);
        if (ret < 0){
            goto err_handler_iio;
        }
 
        return 0;
 
err_handler_iio:
        return ret;
 
}