]> git.itanic.dy.fi Git - BME280_driver/blob - bme280.c
b55511046b7a262c250385244dc0e8a03c522096
[BME280_driver] / bme280.c
1 /*
2 ****************************************************************************
3 * Copyright (C) 2013 - 2014 Bosch Sensortec GmbH
4 *
5 * bme280.c
6 * Date: 2014/12/12
7 * Revision: 2.0.3(Pressure and Temperature compensation code revision is 1.1
8 *               and Humidity compensation code revision is 1.0)
9 *
10 * Usage: Sensor Driver file for BME280 sensor
11 *
12 ****************************************************************************
13 * License:
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are met:
17 *
18 *   Redistributions of source code must retain the above copyright
19 *   notice, this list of conditions and the following disclaimer.
20 *
21 *   Redistributions in binary form must reproduce the above copyright
22 *   notice, this list of conditions and the following disclaimer in the
23 *   documentation and/or other materials provided with the distribution.
24 *
25 *   Neither the name of the copyright holder nor the names of the
26 *   contributors may be used to endorse or promote products derived from
27 *   this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY
35 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
36 * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
37 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
42 * ANY WAY OUT OF THE USE OF THIS
43 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
44 *
45 * The information provided is believed to be accurate and reliable.
46 * The copyright holder assumes no responsibility
47 * for the consequences of use
48 * of such information nor for any infringement of patents or
49 * other rights of third parties which may result from its use.
50 * No license is granted by implication or otherwise under any patent or
51 * patent rights of the copyright holder.
52 **************************************************************************/
53
54 #include "bme280.h"
55 static struct bme280_t *p_bme280; /**< pointer to BME280 */
56
57 /*!
58  *      @brief This function is used for initialize
59  *      the bus read and bus write functions
60  *  and assign the chip id and I2C address of the BME280 sensor
61  *      chip id is read in the register 0xD0 bit from 0 to 7
62  *
63  *       @param bme280 structure pointer.
64  *
65  *      @note While changing the parameter of the bme280_t
66  *      @note consider the following point:
67  *      Changing the reference value of the parameter
68  *      will changes the local copy or local reference
69  *      make sure your changes will not
70  *      affect the reference value of the parameter
71  *      (Better case don't change the reference value of the parameter)
72  *
73  *
74  *
75  *
76  *      @return results of bus communication function
77  *      @retval 0 -> Success
78  *      @retval -1 -> Error
79  *
80  *
81 */
82 BME280_RETURN_FUNCTION_TYPE bme280_init(struct bme280_t *bme280)
83 {
84         /* used to return the communication result*/
85         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
86         u8 v_data_u8 = BME280_ZERO_U8X;
87         p_bme280 = bme280;
88         /* assign BME280 ptr */
89         com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr,
90         BME280_CHIP_ID_REG, &v_data_u8, BME280_ONE_U8X);
91         /* read Chip Id */
92         p_bme280->chip_id = v_data_u8;
93
94         bme280_get_calib_param();
95         /* readout bme280 calibparam structure */
96         return com_rslt;
97 }
98 /*!
99  *      @brief This API is used to read uncompensated temperature
100  *      in the registers 0xFA, 0xFB and 0xFC
101  *      @note 0xFA -> MSB -> bit from 0 to 7
102  *      @note 0xFB -> LSB -> bit from 0 to 7
103  *      @note 0xFC -> LSB -> bit from 4 to 7
104  *
105  * @param v_uncomp_temperature_s32 : The value of uncompensated temperature
106  *
107  *
108  *
109  *      @return results of bus communication function
110  *      @retval 0 -> Success
111  *      @retval -1 -> Error
112  *
113  *
114 */
115 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_temperature(
116 s32 *v_uncomp_temperature_s32)
117 {
118         /* used to return the communication result*/
119         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
120         /* Array holding the MSB and LSb value
121         a_data_u8r[0] - Temperature MSB
122         a_data_u8r[1] - Temperature LSB
123         a_data_u8r[2] - Temperature LSB
124         */
125         u8 a_data_u8r[ARRAY_SIZE_THREE] = {
126         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X};
127         /* check the p_bme280 structure pointer as NULL*/
128         if (p_bme280 == BME280_NULL) {
129                 return E_BME280_NULL_PTR;
130                 } else {
131                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
132                         p_bme280->dev_addr,
133                         BME280_TEMPERATURE_MSB_REG,
134                         a_data_u8r, BME280_THREE_U8X);
135                         *v_uncomp_temperature_s32 = (s32)(((
136                         (u32) (a_data_u8r[INDEX_ZERO]))
137                         << SHIFT_LEFT_12_POSITION) |
138                         (((u32)(a_data_u8r[INDEX_ONE]))
139                         << SHIFT_LEFT_4_POSITION)
140                         | ((u32)a_data_u8r[INDEX_TWO] >>
141                         SHIFT_RIGHT_4_POSITION));
142                 }
143         return com_rslt;
144 }
145 /*!
146  * @brief Reads actual temperature from uncompensated temperature
147  * @note Returns the value in 0.01 degree Centigrade
148  * Output value of "5123" equals 51.23 DegC.
149  *
150  *
151  *
152  *  @param  v_uncomp_temperature_s32 : value of uncompensated temperature
153  *
154  *
155  *  @return Returns the actual temperature
156  *
157 */
158 s32 bme280_compensate_T_int32(s32 v_uncomp_temperature_s32)
159 {
160         s32 v_x1_u32r = BME280_ZERO_U8X;
161         s32 v_x2_u32r = BME280_ZERO_U8X;
162         s32 temperature = BME280_ZERO_U8X;
163
164         v_x1_u32r  = ((((v_uncomp_temperature_s32
165         >> SHIFT_RIGHT_3_POSITION) - ((s32)
166         p_bme280->cal_param.dig_T1 << SHIFT_LEFT_1_POSITION))) *
167         ((s32)p_bme280->cal_param.dig_T2))
168         >> SHIFT_RIGHT_11_POSITION;
169         v_x2_u32r  = (((((v_uncomp_temperature_s32
170         >> SHIFT_RIGHT_4_POSITION) -
171         ((s32)p_bme280->cal_param.dig_T1))
172         * ((v_uncomp_temperature_s32 >> SHIFT_RIGHT_4_POSITION) -
173         ((s32)p_bme280->cal_param.dig_T1)))
174         >> SHIFT_RIGHT_12_POSITION) *
175         ((s32)p_bme280->cal_param.dig_T3))
176         >> SHIFT_RIGHT_14_POSITION;
177         p_bme280->cal_param.t_fine = v_x1_u32r + v_x2_u32r;
178         temperature  = (p_bme280->cal_param.t_fine
179         * BME280_FIVE_U8X + BME280_ONE_TWENTY_EIGHT_U8X)
180         >> SHIFT_RIGHT_8_POSITION;
181         return temperature;
182 }
183 /*!
184  * @brief Reads actual temperature from uncompensated temperature
185  * @note Returns the value with 500LSB/DegC centred around 24 DegC
186  * output value of "5123" equals(5123/500)+24 = 34.246DegC
187  *
188  *
189  *  @param v_uncomp_temperature_s32: value of uncompensated temperature
190  *
191  *
192  *
193  *  @return Return the actual temperature as s16 output
194  *
195 */
196 s16 bme280_compensate_T_int32_sixteen_bit_output(
197 s32 v_uncomp_temperature_s32)
198 {
199         s16 temperature = BME280_ZERO_U8X;
200         bme280_compensate_T_int32(v_uncomp_temperature_s32);
201         temperature  = (s16)((((
202         p_bme280->cal_param.t_fine
203         - BME280_TEMP_1_2_2_8_8_0_DATA)
204         * BME280_TWENTY_FIVE_U8X)
205         + BME280_ONE_TWENTY_EIGHT_U8X)
206         >> SHIFT_RIGHT_8_POSITION);
207
208         return temperature;
209 }
210 /*!
211  *      @brief This API is used to read uncompensated pressure.
212  *      in the registers 0xF7, 0xF8 and 0xF9
213  *      @note 0xF7 -> MSB -> bit from 0 to 7
214  *      @note 0xF8 -> LSB -> bit from 0 to 7
215  *      @note 0xF9 -> LSB -> bit from 4 to 7
216  *
217  *
218  *
219  *      @param v_uncomp_pressure_s32 : The value of uncompensated pressure
220  *
221  *
222  *
223  *      @return results of bus communication function
224  *      @retval 0 -> Success
225  *      @retval -1 -> Error
226  *
227  *
228 */
229 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure(
230 s32 *v_uncomp_pressure_s32)
231 {
232         /* used to return the communication result*/
233         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
234         /* Array holding the MSB and LSb value
235         a_data_u8[0] - Pressure MSB
236         a_data_u8[1] - Pressure LSB
237         a_data_u8[2] - Pressure LSB
238         */
239         u8 a_data_u8[ARRAY_SIZE_THREE] = {
240         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X};
241         /* check the p_bme280 structure pointer as NULL*/
242         if (p_bme280 == BME280_NULL) {
243                 return E_BME280_NULL_PTR;
244                 } else {
245                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
246                         p_bme280->dev_addr,
247                         BME280_PRESSURE_MSB_REG,
248                         a_data_u8, BME280_THREE_U8X);
249                         *v_uncomp_pressure_s32 = (s32)((
250                         ((u32)(a_data_u8[INDEX_ZERO]))
251                         << SHIFT_LEFT_12_POSITION) |
252                         (((u32)(a_data_u8[INDEX_ONE]))
253                         << SHIFT_LEFT_4_POSITION) |
254                         ((u32)a_data_u8[INDEX_TWO] >>
255                         SHIFT_RIGHT_4_POSITION));
256                 }
257         return com_rslt;
258 }
259 /*!
260  * @brief Reads actual pressure from uncompensated pressure
261  * @note Returns the value in Pascal(Pa)
262  * Output value of "96386" equals 96386 Pa =
263  * 963.86 hPa = 963.86 millibar
264  *
265  *
266  *
267  *  @param v_uncomp_pressure_s32 : value of uncompensated pressure
268  *
269  *
270  *
271  *  @return Return the actual pressure output as u32
272  *
273 */
274 u32 bme280_compensate_P_int32(s32 v_uncomp_pressure_s32)
275 {
276         s32 v_x1_u32 = BME280_ZERO_U8X;
277         s32 v_x2_u32 = BME280_ZERO_U8X;
278         u32 v_pressure_u32 = BME280_ZERO_U8X;
279
280         v_x1_u32 = (((s32)p_bme280->cal_param.t_fine)
281         >> SHIFT_RIGHT_1_POSITION) -
282         (s32)BME280_PRESSURE_6_4_0_0_0_DATA;
283         v_x2_u32 = (((v_x1_u32 >> SHIFT_RIGHT_2_POSITION)
284         * (v_x1_u32 >> SHIFT_RIGHT_2_POSITION))
285         >> SHIFT_RIGHT_11_POSITION) *
286         ((s32)p_bme280->cal_param.dig_P6);
287         v_x2_u32 = v_x2_u32 + ((v_x1_u32 *
288         ((s32)p_bme280->cal_param.dig_P5))
289         << SHIFT_LEFT_1_POSITION);
290         v_x2_u32 = (v_x2_u32 >> SHIFT_RIGHT_2_POSITION) +
291         (((s32)p_bme280->cal_param.dig_P4)
292         << SHIFT_LEFT_16_POSITION);
293         v_x1_u32 = (((p_bme280->cal_param.dig_P3
294         * (((v_x1_u32 >> SHIFT_RIGHT_2_POSITION) *
295         (v_x1_u32 >> SHIFT_RIGHT_2_POSITION))
296         >> SHIFT_RIGHT_13_POSITION)) >> SHIFT_RIGHT_3_POSITION) +
297         ((((s32)p_bme280->cal_param.dig_P2) *
298         v_x1_u32) >> SHIFT_RIGHT_1_POSITION))
299         >> SHIFT_RIGHT_18_POSITION;
300         v_x1_u32 = ((((BME280_PRESSURE_3_2_7_6_8_DATA + v_x1_u32)) *
301         ((s32)p_bme280->cal_param.dig_P1))
302         >> SHIFT_RIGHT_15_POSITION);
303         v_pressure_u32 =
304         (((u32)(((s32)BME280_PRESSURE_1_0_4_8_5_7_6_DATA)
305         - v_uncomp_pressure_s32) -
306         (v_x2_u32 >> SHIFT_RIGHT_12_POSITION)))
307         * BME280_PRESSURE_3_1_2_5_DATA;
308         if (v_pressure_u32
309         < BME280_HEX_PRESSURE_8_0_0_0_0_0_0_0_DATA)
310                 /* Avoid exception caused by division by zero */
311                 if (v_x1_u32 != BME280_ZERO_U8X)
312                         v_pressure_u32 =
313                         (v_pressure_u32 << SHIFT_LEFT_1_POSITION) /
314                         ((u32)v_x1_u32);
315                 else
316                         return BME280_ZERO_U8X;
317         else
318                 /* Avoid exception caused by division by zero */
319                 if (v_x1_u32 != BME280_ZERO_U8X)
320                         v_pressure_u32 = (v_pressure_u32
321                         / (u32)v_x1_u32) * BME280_TWO_U8X;
322                 else
323                         return BME280_ZERO_U8X;
324
325                 v_x1_u32 = (((s32)p_bme280->cal_param.dig_P9) *
326                 ((s32)(((v_pressure_u32 >> SHIFT_RIGHT_3_POSITION)
327                 * (v_pressure_u32 >> SHIFT_RIGHT_3_POSITION))
328                 >> SHIFT_RIGHT_13_POSITION)))
329                 >> SHIFT_RIGHT_12_POSITION;
330                 v_x2_u32 = (((s32)(v_pressure_u32
331                 >> SHIFT_RIGHT_2_POSITION)) *
332                 ((s32)p_bme280->cal_param.dig_P8))
333                 >> SHIFT_RIGHT_13_POSITION;
334                 v_pressure_u32 = (u32)((s32)v_pressure_u32 +
335                 ((v_x1_u32 + v_x2_u32 + p_bme280->cal_param.dig_P7)
336                 >> SHIFT_RIGHT_4_POSITION));
337
338         return v_pressure_u32;
339 }
340 /*!
341  *      @brief This API is used to read uncompensated humidity.
342  *      in the registers 0xF7, 0xF8 and 0xF9
343  *      @note 0xFD -> MSB -> bit from 0 to 7
344  *      @note 0xFE -> LSB -> bit from 0 to 7
345  *
346  *
347  *
348  *      @param v_uncomp_humidity_s32 : The value of uncompensated humidity
349  *
350  *
351  *
352  *      @return results of bus communication function
353  *      @retval 0 -> Success
354  *      @retval -1 -> Error
355  *
356  *
357 */
358 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_humidity(
359 s32 *v_uncomp_humidity_s32)
360 {
361         /* used to return the communication result*/
362         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
363         /* Array holding the MSB and LSb value
364         a_data_u8[0] - Humidity MSB
365         a_data_u8[1] - Humidity LSB
366         */
367         u8 a_data_u8[ARRAY_SIZE_TWO] = {
368         BME280_ZERO_U8X, BME280_ZERO_U8X};
369         /* check the p_bme280 structure pointer as NULL*/
370         if (p_bme280 == BME280_NULL) {
371                 return E_BME280_NULL_PTR;
372                 } else {
373                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
374                         p_bme280->dev_addr,
375                         BME280_HUMIDITY_MSB_REG, a_data_u8, BME280_TWO_U8X);
376                         *v_uncomp_humidity_s32 = (s32)(
377                         (((u32)(a_data_u8[INDEX_ZERO]))
378                         << SHIFT_LEFT_8_POSITION)|
379                         ((u32)(a_data_u8[INDEX_ONE])));
380                 }
381         return com_rslt;
382 }
383 /*!
384  * @brief Reads actual humidity from uncompensated humidity
385  * @note Returns the value in %rH as unsigned 32bit integer
386  * in Q22.10 format(22 integer 10 fractional bits).
387  * @note An output value of 42313
388  * represents 42313 / 1024 = 41.321 %rH
389  *
390  *
391  *
392  *  @param  v_uncomp_humidity_s32: value of uncompensated humidity
393  *
394  *  @return Return the actual relative humidity output as u32
395  *
396 */
397 u32 bme280_compensate_H_int32(s32 v_uncomp_humidity_s32)
398 {
399         s32 v_x1_u32;
400         v_x1_u32 = (p_bme280->cal_param.t_fine
401         - ((s32)BME280_HUMIDITY_7_6_8_0_0_DATA));
402         v_x1_u32 = (((((v_uncomp_humidity_s32
403         << SHIFT_LEFT_14_POSITION) -
404         (((s32)p_bme280->cal_param.dig_H4)
405         << SHIFT_LEFT_20_POSITION) -
406         (((s32)p_bme280->cal_param.dig_H5) * v_x1_u32)) +
407         ((s32)BME280_HUMIDITY_1_6_3_8_4_DATA))
408         >> SHIFT_RIGHT_15_POSITION) *
409         (((((((v_x1_u32 *
410         ((s32)p_bme280->cal_param.dig_H6))
411         >> SHIFT_RIGHT_10_POSITION) *
412         (((v_x1_u32 * ((s32)p_bme280->cal_param.dig_H3))
413         >> SHIFT_RIGHT_11_POSITION) +
414         ((s32)BME280_HUMIDITY_3_2_7_6_8_DATA)))
415         >> SHIFT_RIGHT_10_POSITION) +
416         ((s32)BME280_HUMIDITY_2_0_9_7_1_5_2_DATA)) *
417         ((s32)p_bme280->cal_param.dig_H2)
418         + BME280_HUMIDITY_8_1_9_2_DATA)
419         >> SHIFT_RIGHT_14_POSITION));
420         v_x1_u32 = (v_x1_u32 - (((((v_x1_u32
421         >> SHIFT_RIGHT_15_POSITION) *
422         (v_x1_u32 >> SHIFT_RIGHT_15_POSITION))
423         >> SHIFT_RIGHT_7_POSITION) *
424         ((s32)p_bme280->cal_param.dig_H1))
425         >> SHIFT_RIGHT_4_POSITION));
426         v_x1_u32 = (v_x1_u32 < BME280_ZERO_U8X
427         ? BME280_ZERO_U8X : v_x1_u32);
428         v_x1_u32 =
429         (v_x1_u32 > BME280_HUMIDITY_4_1_9_4_3_0_4_0_0_DATA ?
430         BME280_HUMIDITY_4_1_9_4_3_0_4_0_0_DATA : v_x1_u32);
431         return (u32)(v_x1_u32 >> SHIFT_RIGHT_12_POSITION);
432 }
433 /*!
434  * @brief Reads actual humidity from uncompensated humidity
435  * @note Returns the value in %rH as unsigned 16bit integer
436  * @note An output value of 42313
437  * represents 42313/512 = 82.643 %rH
438  *
439  *
440  *
441  *  @param v_uncomp_humidity_s32: value of uncompensated humidity
442  *
443  *
444  *  @return Return the actual relative humidity output as u16
445  *
446 */
447 u16 bme280_compensate_H_int32_sixteen_bit_output(s32 v_uncomp_humidity_s32)
448 {
449         u32 v_x1_u32;
450         u16 v_x2_u32;
451         v_x1_u32 =  bme280_compensate_H_int32(v_uncomp_humidity_s32);
452         v_x2_u32 = (u16)(v_x1_u32 >> SHIFT_RIGHT_1_POSITION);
453         return v_x2_u32;
454 }
455 /*!
456  * @brief This API used to read uncompensated
457  * pressure,temperature and humidity
458  *
459  *
460  *
461  *
462  *  @param  v_uncomp_pressure_s32: The value of uncompensated pressure.
463  *  @param  v_uncomp_temperature_s32: The value of uncompensated temperature
464  *  @param  v_uncomp_humidity_s32: The value of uncompensated humidity.
465  *
466  *
467  *
468  *      @return results of bus communication function
469  *      @retval 0 -> Success
470  *      @retval -1 -> Error
471  *
472  *
473 */
474 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure_temperature_humidity(
475 s32 *v_uncomp_pressure_s32,
476 s32 *v_uncomp_temperature_s32, s32 *v_uncomp_humidity_s32)
477 {
478         /* used to return the communication result*/
479         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
480         /* Array holding the MSB and LSb value of
481         a_data_u8[0] - Pressure MSB
482         a_data_u8[1] - Pressure LSB
483         a_data_u8[1] - Pressure LSB
484         a_data_u8[1] - Temperature MSB
485         a_data_u8[1] - Temperature LSB
486         a_data_u8[1] - Temperature LSB
487         a_data_u8[1] - Humidity MSB
488         a_data_u8[1] - Humidity LSB
489         */
490         u8 a_data_u8[ARRAY_SIZE_EIGHT] = {
491         BME280_ZERO_U8X, BME280_ZERO_U8X,
492         BME280_ZERO_U8X, BME280_ZERO_U8X,
493         BME280_ZERO_U8X, BME280_ZERO_U8X,
494         BME280_ZERO_U8X, BME280_ZERO_U8X};
495         /* check the p_bme280 structure pointer as NULL*/
496         if (p_bme280 == BME280_NULL) {
497                 return E_BME280_NULL_PTR;
498                 } else {
499                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
500                         p_bme280->dev_addr,
501                         BME280_PRESSURE_MSB_REG,
502                         a_data_u8, BME280_EIGHT_U8X);
503                         /*Pressure*/
504                         *v_uncomp_pressure_s32 = (s32)((
505                         ((u32)(a_data_u8[INDEX_ZERO]))
506                         << SHIFT_LEFT_12_POSITION) |
507                         (((u32)(a_data_u8[INDEX_ONE]))
508                         << SHIFT_LEFT_4_POSITION) |
509                         ((u32)a_data_u8[INDEX_TWO] >>
510                         SHIFT_RIGHT_4_POSITION));
511
512                         /* Temperature */
513                         *v_uncomp_temperature_s32 = (s32)(((
514                         (u32) (a_data_u8[INDEX_THREE]))
515                         << SHIFT_LEFT_12_POSITION) |
516                         (((u32)(a_data_u8[INDEX_FOUR]))
517                         << SHIFT_LEFT_4_POSITION)
518                         | ((u32)a_data_u8[INDEX_FIVE]
519                         >> SHIFT_RIGHT_4_POSITION));
520
521                         /*Humidity*/
522                         *v_uncomp_humidity_s32 = (s32)((
523                         ((u32)(a_data_u8[INDEX_SIX]))
524                         << SHIFT_LEFT_8_POSITION)|
525                         ((u32)(a_data_u8[INDEX_SEVEN])));
526                 }
527         return com_rslt;
528 }
529 /*!
530  * @brief This API used to read true pressure, temperature and humidity
531  *
532  *
533  *
534  *
535  *      @param  v_pressure_u32 : The value of compensated pressure.
536  *      @param  v_temperature_s32 : The value of compensated temperature.
537  *      @param  v_humidity_u32 : The value of compensated humidity.
538  *
539  *
540  *      @return results of bus communication function
541  *      @retval 0 -> Success
542  *      @retval -1 -> Error
543  *
544  *
545 */
546 BME280_RETURN_FUNCTION_TYPE bme280_read_pressure_temperature_humidity(
547 u32 *v_pressure_u32, s32 *v_temperature_s32, u32 *v_humidity_u32)
548 {
549         /* used to return the communication result*/
550         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
551         s32 v_uncomp_pressure_s32 = BME280_ZERO_U8X;
552         s32 v_uncom_temperature_s32 = BME280_ZERO_U8X;
553         s32 v_uncom_humidity_s32 = BME280_ZERO_U8X;
554         /* check the p_bme280 structure pointer as NULL*/
555         if (p_bme280 == BME280_NULL) {
556                 return E_BME280_NULL_PTR;
557                 } else {
558                         /* read the uncompensated pressure,
559                         temperature and humidity*/
560                         com_rslt =
561                         bme280_read_uncomp_pressure_temperature_humidity(
562                         &v_uncomp_pressure_s32, &v_uncom_temperature_s32,
563                         &v_uncom_humidity_s32);
564                         /* read the true pressure, temperature and humidity*/
565                         *v_temperature_s32 = bme280_compensate_T_int32(
566                         v_uncom_temperature_s32);
567                         *v_pressure_u32 = bme280_compensate_P_int32(
568                         v_uncomp_pressure_s32);
569                         *v_humidity_u32 = bme280_compensate_H_int32(
570                         v_uncom_humidity_s32);
571                 }
572         return com_rslt;
573 }
574 /*!
575  *      @brief This API is used to
576  *      calibration parameters used for calculation in the registers
577  *
578  *  parameter | Register address |   bit
579  *------------|------------------|----------------
580  *      dig_T1    |  0x88 and 0x89   | from 0 : 7 to 8: 15
581  *      dig_T2    |  0x8A and 0x8B   | from 0 : 7 to 8: 15
582  *      dig_T3    |  0x8C and 0x8D   | from 0 : 7 to 8: 15
583  *      dig_P1    |  0x8E and 0x8F   | from 0 : 7 to 8: 15
584  *      dig_P2    |  0x90 and 0x91   | from 0 : 7 to 8: 15
585  *      dig_P3    |  0x92 and 0x93   | from 0 : 7 to 8: 15
586  *      dig_P4    |  0x94 and 0x95   | from 0 : 7 to 8: 15
587  *      dig_P5    |  0x96 and 0x97   | from 0 : 7 to 8: 15
588  *      dig_P6    |  0x98 and 0x99   | from 0 : 7 to 8: 15
589  *      dig_P7    |  0x9A and 0x9B   | from 0 : 7 to 8: 15
590  *      dig_P8    |  0x9C and 0x9D   | from 0 : 7 to 8: 15
591  *      dig_P9    |  0x9E and 0x9F   | from 0 : 7 to 8: 15
592  *      dig_H1    |         0xA1     | from 0 to 7
593  *      dig_H2    |  0xE1 and 0xE2   | from 0 : 7 to 8: 15
594  *      dig_H3    |         0xE3     | from 0 to 7
595  *
596  *      @return results of bus communication function
597  *      @retval 0 -> Success
598  *      @retval -1 -> Error
599  *
600  *
601 */
602 BME280_RETURN_FUNCTION_TYPE bme280_get_calib_param()
603 {
604         /* used to return the communication result*/
605         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
606         u8 a_data_u8[ARRAY_SIZE_TWENTY_SIX] = {
607         BME280_ZERO_U8X, BME280_ZERO_U8X,
608         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
609         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
610         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
611         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
612         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
613         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
614         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
615         BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X};
616         /* check the p_bme280 structure pointer as NULL*/
617         if (p_bme280 == BME280_NULL) {
618                 return E_BME280_NULL_PTR;
619                 } else {
620                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
621                         p_bme280->dev_addr,
622                         BME280_DIG_T1_LSB_REG,
623                         a_data_u8, BME280_TWENTY_SIX_U8X);
624
625                         p_bme280->cal_param.dig_T1 = (u16)(((
626                         (u16)((u8)a_data_u8[INDEX_ONE])) <<
627                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_ZERO]);
628                         p_bme280->cal_param.dig_T2 = (s16)(((
629                         (s16)((s8)a_data_u8[INDEX_THREE])) <<
630                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWO]);
631                         p_bme280->cal_param.dig_T3 = (s16)(((
632                         (s16)((s8)a_data_u8[INDEX_FIVE])) <<
633                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_FOUR]);
634                         p_bme280->cal_param.dig_P1 = (u16)(((
635                         (u16)((u8)a_data_u8[INDEX_SEVEN])) <<
636                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_SIX]);
637                         p_bme280->cal_param.dig_P2 = (s16)(((
638                         (s16)((s8)a_data_u8[INDEX_NINE])) <<
639                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_EIGHT]);
640                         p_bme280->cal_param.dig_P3 = (s16)(((
641                         (s16)((s8)a_data_u8[INDEX_ELEVEN])) <<
642                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TEN]);
643                         p_bme280->cal_param.dig_P4 = (s16)(((
644                         (s16)((s8)a_data_u8[INDEX_THIRTEEN])) <<
645                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWELVE]);
646                         p_bme280->cal_param.dig_P5 = (s16)(((
647                         (s16)((s8)a_data_u8[INDEX_FIVETEEN])) <<
648                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_FOURTEEN]);
649                         p_bme280->cal_param.dig_P6 = (s16)(((
650                         (s16)((s8)a_data_u8[INDEX_SEVENTEEN])) <<
651                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_SIXTEEN]);
652                         p_bme280->cal_param.dig_P7 = (s16)(((
653                         (s16)((s8)a_data_u8[INDEX_NINETEEN])) <<
654                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_EIGHTEEN]);
655                         p_bme280->cal_param.dig_P8 = (s16)(((
656                         (s16)((s8)a_data_u8[INDEX_TWENTY_ONE])) <<
657                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWENTY]);
658                         p_bme280->cal_param.dig_P9 = (s16)(((
659                         (s16)((s8)a_data_u8[INDEX_TWENTY_THREE])) <<
660                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWENTY_TWO]);
661                         p_bme280->cal_param.dig_H1 =
662                         a_data_u8[INDEX_TWENTY_FIVE];
663                         com_rslt += p_bme280->BME280_BUS_READ_FUNC(
664                         p_bme280->dev_addr,
665                         BME280_DIG_H2_LSB_REG, a_data_u8, BME280_SEVEN_U8X);
666                         p_bme280->cal_param.dig_H2 = (s16)(((
667                         (s16)((s8)a_data_u8[INDEX_ONE])) <<
668                         SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_ZERO]);
669                         p_bme280->cal_param.dig_H3 = a_data_u8[INDEX_TWO];
670                         p_bme280->cal_param.dig_H4 = (s16)(((
671                         (s16)((s8)a_data_u8[INDEX_THREE])) <<
672                         SHIFT_LEFT_4_POSITION) |
673                         (((u8)BME280_HEX_CALIB_0_F_DATA)
674                         & a_data_u8[INDEX_FOUR]));
675                         p_bme280->cal_param.dig_H5 = (s16)(((
676                         (s16)((s8)a_data_u8[INDEX_FIVE])) <<
677                         SHIFT_LEFT_4_POSITION) | (a_data_u8[INDEX_FOUR] >>
678                         SHIFT_RIGHT_4_POSITION));
679                         p_bme280->cal_param.dig_H6 = (s8)a_data_u8[INDEX_SIX];
680                 }
681         return com_rslt;
682 }
683 /*!
684  *      @brief This API is used to get
685  *      the temperature oversampling setting in the register 0xF4
686  *      bits from 5 to 7
687  *
688  *      value               |   Temperature oversampling
689  * ---------------------|---------------------------------
690  *      0x00                | Skipped
691  *      0x01                | BME280_OVERSAMP_1X
692  *      0x02                | BME280_OVERSAMP_2X
693  *      0x03                | BME280_OVERSAMP_4X
694  *      0x04                | BME280_OVERSAMP_8X
695  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
696  *
697  *
698  *  @param v_value_u8 : The value of temperature over sampling
699  *
700  *
701  *
702  *      @return results of bus communication function
703  *      @retval 0 -> Success
704  *      @retval -1 -> Error
705  *
706  *
707 */
708 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_temperature(
709 u8 *v_value_u8)
710 {
711         /* used to return the communication result*/
712         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
713         u8 v_data_u8 = BME280_ZERO_U8X;
714         /* check the p_bme280 structure pointer as NULL*/
715         if (p_bme280 == BME280_NULL) {
716                 return E_BME280_NULL_PTR;
717                 } else {
718                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
719                         p_bme280->dev_addr,
720                         BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
721                         &v_data_u8, BME280_ONE_U8X);
722                         *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
723                         BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE);
724
725                         p_bme280->oversamp_temperature = *v_value_u8;
726                 }
727         return com_rslt;
728 }
729 /*!
730  *      @brief This API is used to set
731  *      the temperature oversampling setting in the register 0xF4
732  *      bits from 5 to 7
733  *
734  *      value               |   Temperature oversampling
735  * ---------------------|---------------------------------
736  *      0x00                | Skipped
737  *      0x01                | BME280_OVERSAMP_1X
738  *      0x02                | BME280_OVERSAMP_2X
739  *      0x03                | BME280_OVERSAMP_4X
740  *      0x04                | BME280_OVERSAMP_8X
741  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
742  *
743  *
744  *  @param v_value_u8 : The value of temperature over sampling
745  *
746  *
747  *
748  *      @return results of bus communication function
749  *      @retval 0 -> Success
750  *      @retval -1 -> Error
751  *
752  *
753 */
754 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_temperature(
755 u8 v_value_u8)
756 {
757         /* used to return the communication result*/
758         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
759         u8 v_data_u8 = BME280_ZERO_U8X;
760         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
761         u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
762         u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
763         /* check the p_bme280 structure pointer as NULL*/
764         if (p_bme280 == BME280_NULL) {
765                 return E_BME280_NULL_PTR;
766                 } else {
767                         v_data_u8 = p_bme280->ctrl_meas_reg;
768                         v_data_u8 =
769                         BME280_SET_BITSLICE(v_data_u8,
770                         BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE, v_value_u8);
771                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
772                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
773                                 com_rslt += bme280_set_soft_rst();
774                                 p_bme280->delay_msec(BME280_3MS_DELAY);
775                                 /* write previous value
776                                 of configuration register*/
777                                 v_pre_config_value_u8 = p_bme280->config_reg;
778                                 com_rslt += bme280_write_register(
779                                         BME280_CONFIG_REG,
780                                 &v_pre_config_value_u8, BME280_ONE_U8X);
781                                 /* write previous value
782                                 of humidity oversampling*/
783                                 v_pre_ctrl_hum_value_u8 =
784                                 p_bme280->ctrl_hum_reg;
785                                 com_rslt += bme280_write_register(
786                                         BME280_CTRL_HUMIDITY_REG,
787                                 &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
788                                 /* write previous and updated value
789                                 of configuration register*/
790                                 com_rslt += bme280_write_register(
791                                         BME280_CTRL_MEAS_REG,
792                                 &v_data_u8, BME280_ONE_U8X);
793                         } else {
794                                 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
795                                 p_bme280->dev_addr,
796                                 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
797                                 &v_data_u8, BME280_ONE_U8X);
798                         }
799                                 p_bme280->oversamp_temperature = v_value_u8;
800                                 /* read the control measurement register value*/
801                                 com_rslt = bme280_read_register(
802                                         BME280_CTRL_MEAS_REG,
803                                 &v_data_u8, BME280_ONE_U8X);
804                                 p_bme280->ctrl_meas_reg = v_data_u8;
805                                 /* read the control humidity register value*/
806                                 com_rslt += bme280_read_register(
807                                         BME280_CTRL_HUMIDITY_REG,
808                                 &v_data_u8, BME280_ONE_U8X);
809                                 p_bme280->ctrl_hum_reg = v_data_u8;
810                                 /* read the control
811                                 configuration register value*/
812                                 com_rslt += bme280_read_register(
813                                         BME280_CONFIG_REG,
814                                 &v_data_u8, BME280_ONE_U8X);
815                                 p_bme280->config_reg = v_data_u8;
816                 }
817         return com_rslt;
818 }
819 /*!
820  *      @brief This API is used to get
821  *      the pressure oversampling setting in the register 0xF4
822  *      bits from 2 to 4
823  *
824  *      value              | Pressure oversampling
825  * --------------------|--------------------------
826  *      0x00               | Skipped
827  *      0x01               | BME280_OVERSAMP_1X
828  *      0x02               | BME280_OVERSAMP_2X
829  *      0x03               | BME280_OVERSAMP_4X
830  *      0x04               | BME280_OVERSAMP_8X
831  *      0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
832  *
833  *
834  *  @param v_value_u8 : The value of pressure oversampling
835  *
836  *
837  *
838  *      @return results of bus communication function
839  *      @retval 0 -> Success
840  *      @retval -1 -> Error
841  *
842  *
843 */
844 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_pressure(
845 u8 *v_value_u8)
846 {
847         /* used to return the communication result*/
848         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
849         u8 v_data_u8 = BME280_ZERO_U8X;
850         /* check the p_bme280 structure pointer as NULL*/
851         if (p_bme280 == BME280_NULL) {
852                 return E_BME280_NULL_PTR;
853                 } else {
854                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
855                         p_bme280->dev_addr,
856                         BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
857                         &v_data_u8, BME280_ONE_U8X);
858                         *v_value_u8 = BME280_GET_BITSLICE(
859                         v_data_u8,
860                         BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE);
861
862                         p_bme280->oversamp_pressure = *v_value_u8;
863                 }
864         return com_rslt;
865 }
866 /*!
867  *      @brief This API is used to set
868  *      the pressure oversampling setting in the register 0xF4
869  *      bits from 2 to 4
870  *
871  *      value              | Pressure oversampling
872  * --------------------|--------------------------
873  *      0x00               | Skipped
874  *      0x01               | BME280_OVERSAMP_1X
875  *      0x02               | BME280_OVERSAMP_2X
876  *      0x03               | BME280_OVERSAMP_4X
877  *      0x04               | BME280_OVERSAMP_8X
878  *      0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
879  *
880  *
881  *  @param v_value_u8 : The value of pressure oversampling
882  *
883  *
884  *
885  *      @return results of bus communication function
886  *      @retval 0 -> Success
887  *      @retval -1 -> Error
888  *
889  *
890 */
891 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_pressure(
892 u8 v_value_u8)
893 {
894         /* used to return the communication result*/
895         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
896         u8 v_data_u8 = BME280_ZERO_U8X;
897         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
898         u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
899         u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
900         /* check the p_bme280 structure pointer as NULL*/
901         if (p_bme280 == BME280_NULL) {
902                 return E_BME280_NULL_PTR;
903                 } else {
904                         v_data_u8 = p_bme280->ctrl_meas_reg;
905                         v_data_u8 =
906                         BME280_SET_BITSLICE(v_data_u8,
907                         BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE, v_value_u8);
908                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
909                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
910                                 com_rslt += bme280_set_soft_rst();
911                                 p_bme280->delay_msec(BME280_3MS_DELAY);
912                                 /* write previous value of
913                                 configuration register*/
914                                 v_pre_config_value_u8 = p_bme280->config_reg;
915                                 com_rslt = bme280_write_register(
916                                         BME280_CONFIG_REG,
917                                 &v_pre_config_value_u8, BME280_ONE_U8X);
918                                 /* write previous value of
919                                 humidity oversampling*/
920                                 v_pre_ctrl_hum_value_u8 =
921                                 p_bme280->ctrl_hum_reg;
922                                 com_rslt += bme280_write_register(
923                                         BME280_CTRL_HUMIDITY_REG,
924                                 &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
925                                 /* write previous and updated value of
926                                 control measurement register*/
927                                 bme280_write_register(
928                                         BME280_CTRL_MEAS_REG,
929                                 &v_data_u8, BME280_ONE_U8X);
930                         } else {
931                                 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
932                                 p_bme280->dev_addr,
933                                 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
934                                 &v_data_u8, BME280_ONE_U8X);
935                         }
936                                 p_bme280->oversamp_pressure = v_value_u8;
937                                 /* read the control measurement register value*/
938                                 com_rslt = bme280_read_register(
939                                         BME280_CTRL_MEAS_REG,
940                                 &v_data_u8, BME280_ONE_U8X);
941                                 p_bme280->ctrl_meas_reg = v_data_u8;
942                                 /* read the control humidity register value*/
943                                 com_rslt += bme280_read_register(
944                                         BME280_CTRL_HUMIDITY_REG,
945                                 &v_data_u8, BME280_ONE_U8X);
946                                 p_bme280->ctrl_hum_reg = v_data_u8;
947                                 /* read the control
948                                 configuration register value*/
949                                 com_rslt += bme280_read_register(
950                                         BME280_CONFIG_REG,
951                                 &v_data_u8, BME280_ONE_U8X);
952                                 p_bme280->config_reg = v_data_u8;
953                 }
954         return com_rslt;
955 }
956 /*!
957  *      @brief This API is used to get
958  *      the humidity oversampling setting in the register 0xF2
959  *      bits from 0 to 2
960  *
961  *      value               | Humidity oversampling
962  * ---------------------|-------------------------
963  *      0x00                | Skipped
964  *      0x01                | BME280_OVERSAMP_1X
965  *      0x02                | BME280_OVERSAMP_2X
966  *      0x03                | BME280_OVERSAMP_4X
967  *      0x04                | BME280_OVERSAMP_8X
968  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
969  *
970  *
971  *  @param  v_value_u8 : The value of humidity over sampling
972  *
973  *
974  *
975  *      @return results of bus communication function
976  *      @retval 0 -> Success
977  *      @retval -1 -> Error
978  *
979  *
980 */
981 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_humidity(
982 u8 *v_value_u8)
983 {
984         /* used to return the communication result*/
985         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
986         u8 v_data_u8 = BME280_ZERO_U8X;
987         /* check the p_bme280 structure pointer as NULL*/
988         if (p_bme280 == BME280_NULL) {
989                 return E_BME280_NULL_PTR;
990                 } else {
991                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
992                         p_bme280->dev_addr,
993                         BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY__REG,
994                         &v_data_u8, BME280_ONE_U8X);
995                         *v_value_u8 = BME280_GET_BITSLICE(
996                         v_data_u8,
997                         BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY);
998
999                         p_bme280->oversamp_humidity = *v_value_u8;
1000                 }
1001         return com_rslt;
1002 }
1003 /*!
1004  *      @brief This API is used to set
1005  *      the humidity oversampling setting in the register 0xF2
1006  *      bits from 0 to 2
1007  *
1008  *      value               | Humidity oversampling
1009  * ---------------------|-------------------------
1010  *      0x00                | Skipped
1011  *      0x01                | BME280_OVERSAMP_1X
1012  *      0x02                | BME280_OVERSAMP_2X
1013  *      0x03                | BME280_OVERSAMP_4X
1014  *      0x04                | BME280_OVERSAMP_8X
1015  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
1016  *
1017  *
1018  *  @param  v_value_u8 : The value of humidity over sampling
1019  *
1020  *
1021  *
1022  * @note The "BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY"
1023  * register sets the humidity
1024  * data acquisition options of the device.
1025  * @note changes to this registers only become
1026  * effective after a write operation to
1027  * "BME280_CTRL_MEAS_REG" register.
1028  * @note In the code automated reading and writing of
1029  *      "BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY"
1030  * @note register first set the
1031  * "BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY"
1032  *  and then read and write
1033  *  the "BME280_CTRL_MEAS_REG" register in the function.
1034  *
1035  *
1036  *      @return results of bus communication function
1037  *      @retval 0 -> Success
1038  *      @retval -1 -> Error
1039  *
1040  *
1041 */
1042 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_humidity(
1043 u8 v_value_u8)
1044 {
1045         /* used to return the communication result*/
1046         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1047         u8 v_data_u8 = BME280_ZERO_U8X;
1048         u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
1049         u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
1050         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
1051         /* check the p_bme280 structure pointer as NULL*/
1052         if (p_bme280 == BME280_NULL) {
1053                 return E_BME280_NULL_PTR;
1054                 } else {
1055                         /* write humidity oversampling*/
1056                         v_data_u8 = p_bme280->ctrl_hum_reg;
1057                         v_data_u8 =
1058                         BME280_SET_BITSLICE(v_data_u8,
1059                         BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY, v_value_u8);
1060                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1061                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1062                                 com_rslt += bme280_set_soft_rst();
1063                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1064                                 /* write previous value of
1065                                 configuration register*/
1066                                 v_pre_config_value_u8 = p_bme280->config_reg;
1067                                 com_rslt += bme280_write_register(
1068                                         BME280_CONFIG_REG,
1069                                 &v_pre_config_value_u8, BME280_ONE_U8X);
1070                                 /* write the value of control humidity*/
1071                                 com_rslt += bme280_write_register(
1072                                         BME280_CTRL_HUMIDITY_REG,
1073                                 &v_data_u8, BME280_ONE_U8X);
1074                                 /* write previous value of
1075                                 control measurement register*/
1076                                 pre_ctrl_meas_value =
1077                                 p_bme280->ctrl_meas_reg;
1078                                 com_rslt += bme280_write_register(
1079                                         BME280_CTRL_MEAS_REG,
1080                                 &pre_ctrl_meas_value, BME280_ONE_U8X);
1081                         } else {
1082                                 com_rslt +=
1083                                 p_bme280->BME280_BUS_WRITE_FUNC(
1084                                 p_bme280->dev_addr,
1085                                 BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY__REG,
1086                                 &v_data_u8, BME280_ONE_U8X);
1087                                 /* Control humidity write will effective only
1088                                 after the control measurement register*/
1089                                 pre_ctrl_meas_value =
1090                                 p_bme280->ctrl_meas_reg;
1091                                 com_rslt += bme280_write_register(
1092                                         BME280_CTRL_MEAS_REG,
1093                                 &pre_ctrl_meas_value, BME280_ONE_U8X);
1094                         }
1095                         p_bme280->oversamp_humidity = v_value_u8;
1096                         /* read the control measurement register value*/
1097                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1098                         &v_data_u8, BME280_ONE_U8X);
1099                         p_bme280->ctrl_meas_reg = v_data_u8;
1100                         /* read the control humidity register value*/
1101                         com_rslt += bme280_read_register(
1102                         BME280_CTRL_HUMIDITY_REG,
1103                         &v_data_u8, BME280_ONE_U8X);
1104                         p_bme280->ctrl_hum_reg = v_data_u8;
1105                         /* read the control configuration register value*/
1106                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1107                         &v_data_u8, BME280_ONE_U8X);
1108                         p_bme280->config_reg = v_data_u8;
1109                 }
1110         return com_rslt;
1111 }
1112 /*!
1113  *      @brief This API used to get the
1114  *      Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1115  *
1116  *
1117  *
1118  *      @param v_power_mode_u8 : The value of power mode
1119  *  value           |    mode
1120  * -----------------|------------------
1121  *      0x00            | BME280_SLEEP_MODE
1122  *      0x01 and 0x02   | BME280_FORCED_MODE
1123  *      0x03            | BME280_NORMAL_MODE
1124  *
1125  *      @return results of bus communication function
1126  *      @retval 0 -> Success
1127  *      @retval -1 -> Error
1128  *
1129  *
1130 */
1131 BME280_RETURN_FUNCTION_TYPE bme280_get_power_mode(u8 *v_power_mode_u8)
1132 {
1133         /* used to return the communication result*/
1134         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1135         u8 v_mode_u8r = BME280_ZERO_U8X;
1136         /* check the p_bme280 structure pointer as NULL*/
1137         if (p_bme280 == BME280_NULL) {
1138                 return E_BME280_NULL_PTR;
1139                 } else {
1140                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1141                         p_bme280->dev_addr,
1142                         BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1143                         &v_mode_u8r, BME280_ONE_U8X);
1144                         *v_power_mode_u8 = BME280_GET_BITSLICE(v_mode_u8r,
1145                         BME280_CTRL_MEAS_REG_POWER_MODE);
1146                 }
1147         return com_rslt;
1148 }
1149 /*!
1150  *      @brief This API used to set the
1151  *      Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1152  *
1153  *
1154  *
1155  *      @param v_power_mode_u8 : The value of power mode
1156  *  value           |    mode
1157  * -----------------|------------------
1158  *      0x00            | BME280_SLEEP_MODE
1159  *      0x01 and 0x02   | BME280_FORCED_MODE
1160  *      0x03            | BME280_NORMAL_MODE
1161  *
1162  *      @return results of bus communication function
1163  *      @retval 0 -> Success
1164  *      @retval -1 -> Error
1165  *
1166  *
1167 */
1168 BME280_RETURN_FUNCTION_TYPE bme280_set_power_mode(u8 v_power_mode_u8)
1169 {
1170         /* used to return the communication result*/
1171         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1172         u8 v_mode_u8r = BME280_ZERO_U8X;
1173         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
1174         u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
1175         u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
1176         u8 v_data_u8 = BME280_ZERO_U8X;
1177         /* check the p_bme280 structure pointer as NULL*/
1178         if (p_bme280 == BME280_NULL) {
1179                 return E_BME280_NULL_PTR;
1180                 } else {
1181                         if (v_power_mode_u8 < BME280_FOUR_U8X) {
1182                                 v_mode_u8r = p_bme280->ctrl_meas_reg;
1183                                 v_mode_u8r =
1184                                 BME280_SET_BITSLICE(v_mode_u8r,
1185                                 BME280_CTRL_MEAS_REG_POWER_MODE,
1186                                 v_power_mode_u8);
1187                                 com_rslt = bme280_get_power_mode(
1188                                         &v_prev_pow_mode_u8);
1189                                 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1190                                         com_rslt += bme280_set_soft_rst();
1191                                         p_bme280->delay_msec(BME280_3MS_DELAY);
1192                                         /* write previous value of
1193                                         configuration register*/
1194                                         v_pre_config_value_u8 =
1195                                         p_bme280->config_reg;
1196                                         com_rslt = bme280_write_register(
1197                                                 BME280_CONFIG_REG,
1198                                         &v_pre_config_value_u8, BME280_ONE_U8X);
1199                                         /* write previous value of
1200                                         humidity oversampling*/
1201                                         v_pre_ctrl_hum_value_u8 =
1202                                         p_bme280->ctrl_hum_reg;
1203                                         com_rslt += bme280_write_register(
1204                                         BME280_CTRL_HUMIDITY_REG,
1205                                         &v_pre_ctrl_hum_value_u8,
1206                                         BME280_ONE_U8X);
1207                                         /* write previous and updated value of
1208                                         control measurement register*/
1209                                         com_rslt += bme280_write_register(
1210                                         BME280_CTRL_MEAS_REG,
1211                                         &v_mode_u8r, BME280_ONE_U8X);
1212                                 } else {
1213                                         com_rslt =
1214                                         p_bme280->BME280_BUS_WRITE_FUNC(
1215                                         p_bme280->dev_addr,
1216                                         BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1217                                         &v_mode_u8r, BME280_ONE_U8X);
1218                                 }
1219                                 /* read the control measurement register value*/
1220                                 com_rslt = bme280_read_register(
1221                                         BME280_CTRL_MEAS_REG,
1222                                 &v_data_u8, BME280_ONE_U8X);
1223                                 p_bme280->ctrl_meas_reg = v_data_u8;
1224                                 /* read the control humidity register value*/
1225                                 com_rslt += bme280_read_register(
1226                                         BME280_CTRL_HUMIDITY_REG,
1227                                 &v_data_u8, BME280_ONE_U8X);
1228                                 p_bme280->ctrl_hum_reg = v_data_u8;
1229                                 /* read the config register value*/
1230                                 com_rslt += bme280_read_register(
1231                                         BME280_CONFIG_REG,
1232                                 &v_data_u8, BME280_ONE_U8X);
1233                                 p_bme280->config_reg = v_data_u8;
1234                         } else {
1235                         com_rslt = E_BME280_OUT_OF_RANGE;
1236                         }
1237                 }
1238         return com_rslt;
1239 }
1240 /*!
1241  * @brief Used to reset the sensor
1242  * The value 0xB6 is written to the 0xE0
1243  * register the device is reset using the
1244  * complete power-on-reset procedure.
1245  * @note Soft reset can be easily set using bme280_set_softreset().
1246  * @note Usage Hint : bme280_set_softreset()
1247  *
1248  *
1249  *      @return results of bus communication function
1250  *      @retval 0 -> Success
1251  *      @retval -1 -> Error
1252  *
1253  *
1254 */
1255 BME280_RETURN_FUNCTION_TYPE bme280_set_soft_rst()
1256 {
1257         /* used to return the communication result*/
1258         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1259         u8 v_data_u8 = BME280_SOFT_RESET_CODE;
1260         /* check the p_bme280 structure pointer as NULL*/
1261         if (p_bme280 == BME280_NULL) {
1262                 return E_BME280_NULL_PTR;
1263                 } else {
1264                         com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1265                         p_bme280->dev_addr,
1266                         BME280_RST_REG, &v_data_u8, BME280_ONE_U8X);
1267                 }
1268         return com_rslt;
1269 }
1270 /*!
1271  *      @brief This API used to get the sensor
1272  *      SPI mode(communication type) in the register 0xF5 bit 0
1273  *
1274  *
1275  *
1276  *      @param v_enable_disable_u8 : The value of SPI enable
1277  *      value  | Description
1278  * --------|--------------
1279  *   0     | Disable
1280  *   1     | Enable
1281  *
1282  *
1283  *
1284  *      @return results of bus communication function
1285  *      @retval 0 -> Success
1286  *      @retval -1 -> Error
1287  *
1288  *
1289 */
1290 BME280_RETURN_FUNCTION_TYPE bme280_get_spi3(u8 *v_enable_disable_u8)
1291 {
1292         /* used to return the communication result*/
1293         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1294         u8 v_data_u8 = BME280_ZERO_U8X;
1295         /* check the p_bme280 structure pointer as NULL*/
1296         if (p_bme280 == BME280_NULL) {
1297                 return E_BME280_NULL_PTR;
1298                 } else {
1299                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1300                         p_bme280->dev_addr,
1301                         BME280_CONFIG_REG_SPI3_ENABLE__REG,
1302                         &v_data_u8, BME280_ONE_U8X);
1303                         *v_enable_disable_u8 = BME280_GET_BITSLICE(
1304                         v_data_u8,
1305                         BME280_CONFIG_REG_SPI3_ENABLE);
1306                 }
1307         return com_rslt;
1308 }
1309 /*!
1310  *      @brief This API used to set the sensor
1311  *      SPI mode(communication type) in the register 0xF5 bit 0
1312  *
1313  *
1314  *
1315  *      @param v_enable_disable_u8 : The value of SPI enable
1316  *      value  | Description
1317  * --------|--------------
1318  *   0     | Disable
1319  *   1     | Enable
1320  *
1321  *
1322  *
1323  *      @return results of bus communication function
1324  *      @retval 0 -> Success
1325  *      @retval -1 -> Error
1326  *
1327  *
1328 */
1329 BME280_RETURN_FUNCTION_TYPE bme280_set_spi3(u8 v_enable_disable_u8)
1330 {
1331         /* used to return the communication result*/
1332         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1333         u8 v_data_u8 = BME280_ZERO_U8X;
1334         u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
1335         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
1336         u8 v_pre_ctrl_hum_value_u8 =  BME280_ZERO_U8X;
1337         /* check the p_bme280 structure pointer as NULL*/
1338         if (p_bme280 == BME280_NULL) {
1339                 return E_BME280_NULL_PTR;
1340                 } else {
1341                         v_data_u8 = p_bme280->config_reg;
1342                         v_data_u8 =
1343                         BME280_SET_BITSLICE(v_data_u8,
1344                         BME280_CONFIG_REG_SPI3_ENABLE, v_enable_disable_u8);
1345                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1346                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1347                                 com_rslt += bme280_set_soft_rst();
1348                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1349                                 /* write previous and updated value of
1350                                 configuration register*/
1351                                 com_rslt += bme280_write_register(
1352                                         BME280_CONFIG_REG,
1353                                 &v_data_u8, BME280_ONE_U8X);
1354                                 /* write previous value of
1355                                 humidity oversampling*/
1356                                 v_pre_ctrl_hum_value_u8 =
1357                                 p_bme280->ctrl_hum_reg;
1358                                 com_rslt +=  bme280_write_register(
1359                                         BME280_CTRL_HUMIDITY_REG,
1360                                 &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
1361                                 /* write previous value of
1362                                 control measurement register*/
1363                                 pre_ctrl_meas_value =
1364                                 p_bme280->ctrl_meas_reg;
1365                                 com_rslt += bme280_write_register(
1366                                         BME280_CTRL_MEAS_REG,
1367                                 &pre_ctrl_meas_value, BME280_ONE_U8X);
1368                         } else {
1369                                 com_rslt =
1370                                 p_bme280->BME280_BUS_WRITE_FUNC(
1371                                 p_bme280->dev_addr,
1372                                 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1373                                 &v_data_u8, BME280_ONE_U8X);
1374                         }
1375                         /* read the control measurement register value*/
1376                         com_rslt += bme280_read_register(
1377                                 BME280_CTRL_MEAS_REG,
1378                         &v_data_u8, BME280_ONE_U8X);
1379                         p_bme280->ctrl_meas_reg = v_data_u8;
1380                         /* read the control humidity register value*/
1381                         com_rslt += bme280_read_register(
1382                                 BME280_CTRL_HUMIDITY_REG,
1383                         &v_data_u8, BME280_ONE_U8X);
1384                         p_bme280->ctrl_hum_reg = v_data_u8;
1385                         /* read the control configuration register value*/
1386                         com_rslt += bme280_read_register(
1387                                 BME280_CONFIG_REG,
1388                         &v_data_u8, BME280_ONE_U8X);
1389                         p_bme280->config_reg = v_data_u8;
1390                 }
1391         return com_rslt;
1392 }
1393 /*!
1394  *      @brief This API is used to reads filter setting
1395  *      in the register 0xF5 bit 3 and 4
1396  *
1397  *
1398  *
1399  *      @param v_value_u8 : The value of IIR filter coefficient
1400  *
1401  *      value       |   Filter coefficient
1402  * -------------|-------------------------
1403  *      0x00        | BME280_FILTER_COEFF_OFF
1404  *      0x01        | BME280_FILTER_COEFF_2
1405  *      0x02        | BME280_FILTER_COEFF_4
1406  *      0x03        | BME280_FILTER_COEFF_8
1407  *      0x04        | BME280_FILTER_COEFF_16
1408  *
1409  *      @return results of bus communication function
1410  *      @retval 0 -> Success
1411  *      @retval -1 -> Error
1412  *
1413  *
1414 */
1415 BME280_RETURN_FUNCTION_TYPE bme280_get_filter(u8 *v_value_u8)
1416 {
1417         /* used to return the communication result*/
1418         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1419         u8 v_data_u8 = BME280_ZERO_U8X;
1420         /* check the p_bme280 structure pointer as NULL*/
1421         if (p_bme280 == BME280_NULL) {
1422                 return E_BME280_NULL_PTR;
1423                 } else {
1424                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1425                         p_bme280->dev_addr,
1426                         BME280_CONFIG_REG_FILTER__REG,
1427                         &v_data_u8, BME280_ONE_U8X);
1428                         *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
1429                         BME280_CONFIG_REG_FILTER);
1430                 }
1431         return com_rslt;
1432 }
1433 /*!
1434  *      @brief This API is used to write filter setting
1435  *      in the register 0xF5 bit 3 and 4
1436  *
1437  *
1438  *
1439  *      @param v_value_u8 : The value of IIR filter coefficient
1440  *
1441  *      value       |   Filter coefficient
1442  * -------------|-------------------------
1443  *      0x00        | BME280_FILTER_COEFF_OFF
1444  *      0x01        | BME280_FILTER_COEFF_2
1445  *      0x02        | BME280_FILTER_COEFF_4
1446  *      0x03        | BME280_FILTER_COEFF_8
1447  *      0x04        | BME280_FILTER_COEFF_16
1448  *
1449  *      @return results of bus communication function
1450  *      @retval 0 -> Success
1451  *      @retval -1 -> Error
1452  *
1453  *
1454 */
1455 BME280_RETURN_FUNCTION_TYPE bme280_set_filter(u8 v_value_u8)
1456 {
1457         /* used to return the communication result*/
1458         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1459         u8 v_data_u8 = BME280_ZERO_U8X;
1460         u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
1461         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
1462         u8 v_pre_ctrl_hum_value_u8 =  BME280_ZERO_U8X;
1463         /* check the p_bme280 structure pointer as NULL*/
1464         if (p_bme280 == BME280_NULL) {
1465                 return E_BME280_NULL_PTR;
1466                 } else {
1467                         v_data_u8 = p_bme280->config_reg;
1468                         v_data_u8 =
1469                         BME280_SET_BITSLICE(v_data_u8,
1470                         BME280_CONFIG_REG_FILTER, v_value_u8);
1471                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1472                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1473                                 com_rslt += bme280_set_soft_rst();
1474                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1475                                 /* write previous and updated value of
1476                                 configuration register*/
1477                                 com_rslt += bme280_write_register(
1478                                         BME280_CONFIG_REG,
1479                                 &v_data_u8, BME280_ONE_U8X);
1480                                 /* write previous value of
1481                                 humidity oversampling*/
1482                                 v_pre_ctrl_hum_value_u8 =
1483                                 p_bme280->ctrl_hum_reg;
1484                                 com_rslt += bme280_write_register(
1485                                         BME280_CTRL_HUMIDITY_REG,
1486                                 &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
1487                                 /* write previous value of
1488                                 control measurement register*/
1489                                 pre_ctrl_meas_value =
1490                                 p_bme280->ctrl_meas_reg;
1491                                 com_rslt += bme280_write_register(
1492                                         BME280_CTRL_MEAS_REG,
1493                                 &pre_ctrl_meas_value, BME280_ONE_U8X);
1494                         } else {
1495                                 com_rslt =
1496                                 p_bme280->BME280_BUS_WRITE_FUNC(
1497                                 p_bme280->dev_addr,
1498                                 BME280_CONFIG_REG_FILTER__REG,
1499                                 &v_data_u8, BME280_ONE_U8X);
1500                         }
1501                         /* read the control measurement register value*/
1502                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1503                         &v_data_u8, BME280_ONE_U8X);
1504                         p_bme280->ctrl_meas_reg = v_data_u8;
1505                         /* read the control humidity register value*/
1506                         com_rslt += bme280_read_register(
1507                         BME280_CTRL_HUMIDITY_REG,
1508                         &v_data_u8, BME280_ONE_U8X);
1509                         p_bme280->ctrl_hum_reg = v_data_u8;
1510                         /* read the configuration register value*/
1511                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1512                         &v_data_u8, BME280_ONE_U8X);
1513                         p_bme280->config_reg = v_data_u8;
1514                 }
1515         return com_rslt;
1516 }
1517 /*!
1518  *      @brief This API used to Read the
1519  *      standby duration time from the sensor in the register 0xF5 bit 5 to 7
1520  *
1521  *      @param v_standby_durn_u8 : The value of standby duration time value.
1522  *  value       | standby duration
1523  * -------------|-----------------------
1524  *    0x00      | BME280_STANDBY_TIME_1_MS
1525  *    0x01      | BME280_STANDBY_TIME_63_MS
1526  *    0x02      | BME280_STANDBY_TIME_125_MS
1527  *    0x03      | BME280_STANDBY_TIME_250_MS
1528  *    0x04      | BME280_STANDBY_TIME_500_MS
1529  *    0x05      | BME280_STANDBY_TIME_1000_MS
1530  *    0x06      | BME280_STANDBY_TIME_2000_MS
1531  *    0x07      | BME280_STANDBY_TIME_4000_MS
1532  *
1533  *
1534  *      @return results of bus communication function
1535  *      @retval 0 -> Success
1536  *      @retval -1 -> Error
1537  *
1538  *
1539 */
1540 BME280_RETURN_FUNCTION_TYPE bme280_get_standby_durn(u8 *v_standby_durn_u8)
1541 {
1542         /* used to return the communication result*/
1543         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1544         u8 v_data_u8 = BME280_ZERO_U8X;
1545         /* check the p_bme280 structure pointer as NULL*/
1546         if (p_bme280 == BME280_NULL) {
1547                 return E_BME280_NULL_PTR;
1548                 } else {
1549                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1550                         p_bme280->dev_addr,
1551                         BME280_CONFIG_REG_TSB__REG,
1552                         &v_data_u8, BME280_ONE_U8X);
1553                         *v_standby_durn_u8 = BME280_GET_BITSLICE(
1554                         v_data_u8, BME280_CONFIG_REG_TSB);
1555                 }
1556         return com_rslt;
1557 }
1558 /*!
1559  *      @brief This API used to write the
1560  *      standby duration time from the sensor in the register 0xF5 bit 5 to 7
1561  *
1562  *      @param v_standby_durn_u8 : The value of standby duration time value.
1563  *  value       | standby duration
1564  * -------------|-----------------------
1565  *    0x00      | BME280_STANDBY_TIME_1_MS
1566  *    0x01      | BME280_STANDBY_TIME_63_MS
1567  *    0x02      | BME280_STANDBY_TIME_125_MS
1568  *    0x03      | BME280_STANDBY_TIME_250_MS
1569  *    0x04      | BME280_STANDBY_TIME_500_MS
1570  *    0x05      | BME280_STANDBY_TIME_1000_MS
1571  *    0x06      | BME280_STANDBY_TIME_2000_MS
1572  *    0x07      | BME280_STANDBY_TIME_4000_MS
1573  *
1574  *      @note Normal mode comprises an automated perpetual
1575  *      cycling between an (active)
1576  *      Measurement period and an (inactive) standby period.
1577  *      @note The standby time is determined by
1578  *      the contents of the register t_sb.
1579  *      Standby time can be set using BME280_STANDBY_TIME_125_MS.
1580  *
1581  *      @note Usage Hint : bme280_set_standby_durn(BME280_STANDBY_TIME_125_MS)
1582  *
1583  *
1584  *
1585  *      @return results of bus communication function
1586  *      @retval 0 -> Success
1587  *      @retval -1 -> Error
1588  *
1589  *
1590 */
1591 BME280_RETURN_FUNCTION_TYPE bme280_set_standby_durn(u8 v_standby_durn_u8)
1592 {
1593         /* used to return the communication result*/
1594         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1595         u8 v_data_u8 = BME280_ZERO_U8X;
1596         u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
1597         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
1598         u8 v_pre_ctrl_hum_value_u8 =  BME280_ZERO_U8X;
1599         /* check the p_bme280 structure pointer as NULL*/
1600         if (p_bme280 == BME280_NULL) {
1601                 return E_BME280_NULL_PTR;
1602                 } else {
1603                         v_data_u8 = p_bme280->config_reg;
1604                         v_data_u8 =
1605                         BME280_SET_BITSLICE(v_data_u8,
1606                         BME280_CONFIG_REG_TSB, v_standby_durn_u8);
1607                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1608                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1609                                 com_rslt += bme280_set_soft_rst();
1610                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1611                                 /* write previous and updated value of
1612                                 configuration register*/
1613                                 com_rslt += bme280_write_register(
1614                                         BME280_CONFIG_REG,
1615                                 &v_data_u8, BME280_ONE_U8X);
1616                                 /* write previous value of
1617                                 humidity oversampling*/
1618                                 v_pre_ctrl_hum_value_u8 =
1619                                 p_bme280->ctrl_hum_reg;
1620                                 com_rslt += bme280_write_register(
1621                                         BME280_CTRL_HUMIDITY_REG,
1622                                 &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
1623                                 /* write previous value of control
1624                                 measurement register*/
1625                                 pre_ctrl_meas_value =
1626                                 p_bme280->ctrl_meas_reg;
1627                                 com_rslt += bme280_write_register(
1628                                         BME280_CTRL_MEAS_REG,
1629                                 &pre_ctrl_meas_value, BME280_ONE_U8X);
1630                         } else {
1631                                 com_rslt =
1632                                 p_bme280->BME280_BUS_WRITE_FUNC(
1633                                 p_bme280->dev_addr,
1634                                 BME280_CONFIG_REG_TSB__REG,
1635                                 &v_data_u8, BME280_ONE_U8X);
1636                         }
1637                         /* read the control measurement register value*/
1638                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1639                         &v_data_u8, BME280_ONE_U8X);
1640                         p_bme280->ctrl_meas_reg = v_data_u8;
1641                         /* read the control humidity register value*/
1642                         com_rslt += bme280_read_register(
1643                         BME280_CTRL_HUMIDITY_REG,
1644                         &v_data_u8, BME280_ONE_U8X);
1645                         p_bme280->ctrl_hum_reg = v_data_u8;
1646                         /* read the configuration register value*/
1647                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1648                         &v_data_u8, BME280_ONE_U8X);
1649                         p_bme280->config_reg = v_data_u8;
1650                 }
1651         return com_rslt;
1652 }
1653 /*
1654  * @brief Writes the working mode to the sensor
1655  *
1656  *
1657  *
1658  *
1659  *  @param v_work_mode_u8 : Mode to be set
1660  *  value    | Working mode
1661  * ----------|--------------------
1662  *   0       | BME280_ULTRALOWPOWER_MODE
1663  *   1       | BME280_LOWPOWER_MODE
1664  *   2       | BME280_STANDARDRESOLUTION_MODE
1665  *   3       | BME280_HIGHRESOLUTION_MODE
1666  *   4       | BME280_ULTRAHIGHRESOLUTION_MODE
1667  *
1668  *      @return results of bus communication function
1669  *      @retval 0 -> Success
1670  *      @retval -1 -> Error
1671  *
1672  *
1673 */
1674 /*BME280_RETURN_FUNCTION_TYPE bme280_set_work_mode(u8 v_work_mode_u8)
1675 {
1676 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1677 u8 v_data_u8 = BME280_ZERO_U8X;
1678 if (p_bme280 == BME280_NULL) {
1679         return E_BME280_NULL_PTR;
1680 } else {
1681         if (v_work_mode_u8 <= BME280_FOUR_U8X) {
1682                 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1683                         p_bme280->dev_addr,     BME280_CTRL_MEAS_REG,
1684                         &v_data_u8, BME280_ONE_U8X);
1685                 if (com_rslt == SUCCESS) {
1686                         switch (v_work_mode_u8) {
1687                         case BME280_ULTRALOWPOWER_MODE:
1688                                 p_bme280->oversamp_temperature =
1689                                 BME280_ULTRALOWPOWER_OSRS_T;
1690                                 p_bme280->osrs_p =
1691                                 BME280_ULTRALOWPOWER_OSRS_P;
1692                                 break;
1693                         case BME280_LOWPOWER_MODE:
1694                                 p_bme280->oversamp_temperature =
1695                                 BME280_LOWPOWER_OSRS_T;
1696                                 p_bme280->osrs_p = BME280_LOWPOWER_OSRS_P;
1697                                 break;
1698                         case BME280_STANDARDRESOLUTION_MODE:
1699                                 p_bme280->oversamp_temperature =
1700                                 BME280_STANDARDRESOLUTION_OSRS_T;
1701                                 p_bme280->osrs_p =
1702                                 BME280_STANDARDRESOLUTION_OSRS_P;
1703                                 break;
1704                         case BME280_HIGHRESOLUTION_MODE:
1705                                 p_bme280->oversamp_temperature =
1706                                 BME280_HIGHRESOLUTION_OSRS_T;
1707                                 p_bme280->osrs_p = BME280_HIGHRESOLUTION_OSRS_P;
1708                                 break;
1709                         case BME280_ULTRAHIGHRESOLUTION_MODE:
1710                                 p_bme280->oversamp_temperature =
1711                                 BME280_ULTRAHIGHRESOLUTION_OSRS_T;
1712                                 p_bme280->osrs_p =
1713                                 BME280_ULTRAHIGHRESOLUTION_OSRS_P;
1714                                 break;
1715                         }
1716                         v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1717                                 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE,
1718                                 p_bme280->oversamp_temperature);
1719                         v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1720                                 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE,
1721                                 p_bme280->osrs_p);
1722                         com_rslt += p_bme280->BME280_BUS_WRITE_FUNC(
1723                                 p_bme280->dev_addr,     BME280_CTRL_MEAS_REG,
1724                                 &v_data_u8, BME280_ONE_U8X);
1725                 }
1726         } else {
1727                 com_rslt = E_BME280_OUT_OF_RANGE;
1728         }
1729 }
1730 return com_rslt;
1731 }*/
1732 /*!
1733  * @brief This API used to read uncompensated
1734  * temperature,pressure and humidity in forced mode
1735  *
1736  *
1737  *      @param v_uncom_pressure_s32: The value of uncompensated pressure
1738  *      @param v_uncom_temperature_s32: The value of uncompensated temperature
1739  *      @param v_uncom_humidity_s32: The value of uncompensated humidity
1740  *
1741  *
1742  *      @return results of bus communication function
1743  *      @retval 0 -> Success
1744  *      @retval -1 -> Error
1745  *
1746  *
1747 */
1748 BME280_RETURN_FUNCTION_TYPE
1749 bme280_get_forced_uncomp_pressure_temperature_humidity(
1750 s32 *v_uncom_pressure_s32,
1751 s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32)
1752 {
1753         /* used to return the communication result*/
1754         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1755         u8 v_data_u8 = BME280_ZERO_U8X;
1756         u8 v_waittime_u8r = BME280_ZERO_U8X;
1757         u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
1758         u8 v_mode_u8r = BME280_ZERO_U8X;
1759         u8 pre_ctrl_config_value = BME280_ZERO_U8X;
1760         u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
1761         /* check the p_bme280 structure pointer as NULL*/
1762         if (p_bme280 == BME280_NULL) {
1763                 return E_BME280_NULL_PTR;
1764                 } else {
1765                         v_mode_u8r = p_bme280->ctrl_meas_reg;
1766                         v_mode_u8r =
1767                         BME280_SET_BITSLICE(v_mode_u8r,
1768                         BME280_CTRL_MEAS_REG_POWER_MODE, BME280_FORCED_MODE);
1769                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1770                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1771                                 com_rslt += bme280_set_soft_rst();
1772                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1773                                 /* write previous and updated value of
1774                                 configuration register*/
1775                                 pre_ctrl_config_value = p_bme280->config_reg;
1776                                 com_rslt += bme280_write_register(
1777                                         BME280_CONFIG_REG,
1778                                 &pre_ctrl_config_value, BME280_ONE_U8X);
1779                                 /* write previous value of
1780                                 humidity oversampling*/
1781                                 v_pre_ctrl_hum_value_u8 =
1782                                 p_bme280->ctrl_hum_reg;
1783                                 com_rslt += bme280_write_register(
1784                                         BME280_CTRL_HUMIDITY_REG,
1785                                 &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
1786                                 /* write the force mode  */
1787                                 com_rslt += bme280_write_register(
1788                                         BME280_CTRL_MEAS_REG,
1789                                 &v_mode_u8r, BME280_ONE_U8X);
1790                         } else {
1791                                 /* write previous value of
1792                                 humidity oversampling*/
1793                                 v_pre_ctrl_hum_value_u8 =
1794                                 p_bme280->ctrl_hum_reg;
1795                                 com_rslt += bme280_write_register(
1796                                         BME280_CTRL_HUMIDITY_REG,
1797                                 &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
1798                                 /* write the force mode  */
1799                                 com_rslt += bme280_write_register(
1800                                         BME280_CTRL_MEAS_REG,
1801                                 &v_mode_u8r, BME280_ONE_U8X);
1802                         }
1803                         bme280_compute_wait_time(&v_waittime_u8r);
1804                         p_bme280->delay_msec(v_waittime_u8r);
1805                         /* read the force-mode value of pressure
1806                         temperature and humidity*/
1807                         com_rslt +=
1808                         bme280_read_uncomp_pressure_temperature_humidity(
1809                         v_uncom_pressure_s32, v_uncom_temperature_s32,
1810                         v_uncom_humidity_s32);
1811
1812                         /* read the control humidity register value*/
1813                         com_rslt += bme280_read_register(
1814                         BME280_CTRL_HUMIDITY_REG,
1815                         &v_data_u8, BME280_ONE_U8X);
1816                         p_bme280->ctrl_hum_reg = v_data_u8;
1817                         /* read the configuration register value*/
1818                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1819                         &v_data_u8, BME280_ONE_U8X);
1820                         p_bme280->config_reg = v_data_u8;
1821
1822                         /* read the control measurement register value*/
1823                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1824                         &v_data_u8, BME280_ONE_U8X);
1825                         p_bme280->ctrl_meas_reg = v_data_u8;
1826                 }
1827         return com_rslt;
1828 }
1829 /*!
1830  * @brief
1831  *      This API write the data to
1832  *      the given register
1833  *
1834  *
1835  *      @param v_addr_u8 -> Address of the register
1836  *      @param v_data_u8 -> The data from the register
1837  *      @param v_len_u8 -> no of bytes to read
1838  *
1839  *
1840  *      @return results of bus communication function
1841  *      @retval 0 -> Success
1842  *      @retval -1 -> Error
1843  *
1844  *
1845  */
1846 BME280_RETURN_FUNCTION_TYPE bme280_write_register(u8 v_addr_u8,
1847 u8 *v_data_u8, u8 v_len_u8)
1848 {
1849         /* used to return the communication result*/
1850         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1851         /* check the p_bme280 structure pointer as NULL*/
1852         if (p_bme280 == BME280_NULL) {
1853                 return E_BME280_NULL_PTR;
1854                 } else {
1855                         com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1856                         p_bme280->dev_addr,
1857                         v_addr_u8, v_data_u8, v_len_u8);
1858                 }
1859         return com_rslt;
1860 }
1861 /*!
1862  * @brief
1863  *      This API reads the data from
1864  *      the given register
1865  *
1866  *
1867  *      @param v_addr_u8 -> Address of the register
1868  *      @param v_data_u8 -> The data from the register
1869  *      @param v_len_u8 -> no of bytes to read
1870  *
1871  *
1872  *      @return results of bus communication function
1873  *      @retval 0 -> Success
1874  *      @retval -1 -> Error
1875  *
1876  *
1877  */
1878 BME280_RETURN_FUNCTION_TYPE bme280_read_register(u8 v_addr_u8,
1879 u8 *v_data_u8, u8 v_len_u8)
1880 {
1881         /* used to return the communication result*/
1882         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1883         /* check the p_bme280 structure pointer as NULL*/
1884         if (p_bme280 == BME280_NULL) {
1885                 return E_BME280_NULL_PTR;
1886                 } else {
1887                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1888                         p_bme280->dev_addr,
1889                         v_addr_u8, v_data_u8, v_len_u8);
1890                 }
1891         return com_rslt;
1892 }
1893 #ifdef BME280_ENABLE_FLOAT
1894 /*!
1895  * @brief Reads actual temperature from uncompensated temperature
1896  * @note returns the value in Degree centigrade
1897  * @note Output value of "51.23" equals 51.23 DegC.
1898  *
1899  *
1900  *
1901  *  @param v_uncom_temperature_s32 : value of uncompensated temperature
1902  *
1903  *
1904  *
1905  *  @return  Return the actual temperature in floating point
1906  *
1907 */
1908 double bme280_compensate_T_double(s32 v_uncom_temperature_s32)
1909 {
1910         double v_x1_u32 = BME280_ZERO_U8X;
1911         double v_x2_u32 = BME280_ZERO_U8X;
1912         double temperature = BME280_ZERO_U8X;
1913
1914         v_x1_u32  = (((double)v_uncom_temperature_s32)
1915         / BME280_FLOAT_TRUE_TEMP_1_6_3_8_4_DATA -
1916         ((double)p_bme280->cal_param.dig_T1)
1917         / BME280_FLOAT_TRUE_TEMP_1_0_2_4_DATA) *
1918         ((double)p_bme280->cal_param.dig_T2);
1919         v_x2_u32  = ((((double)v_uncom_temperature_s32)
1920         / BME280_FLOAT_TRUE_TEMP_1_3_1_0_7_2_DATA -
1921         ((double)p_bme280->cal_param.dig_T1)
1922         / BME280_FLOAT_TRUE_TEMP_8_1_9_2_DATA) *
1923         (((double)v_uncom_temperature_s32) /
1924         BME280_FLOAT_TRUE_TEMP_1_3_1_0_7_2_DATA -
1925         ((double)p_bme280->cal_param.dig_T1) /
1926         BME280_FLOAT_TRUE_TEMP_8_1_9_2_DATA)) *
1927         ((double)p_bme280->cal_param.dig_T3);
1928         p_bme280->cal_param.t_fine = (s32)(v_x1_u32 + v_x2_u32);
1929         temperature  = (v_x1_u32 + v_x2_u32) /
1930         BME280_FLOAT_TRUE_TEMP_5_1_2_0_DATA;
1931
1932
1933         return temperature;
1934 }
1935 /*!
1936  * @brief Reads actual pressure from uncompensated pressure
1937  * @note Returns pressure in Pa as double.
1938  * @note Output value of "96386.2"
1939  * equals 96386.2 Pa = 963.862 hPa.
1940  *
1941  *
1942  *  @param v_uncom_pressure_s32 : value of uncompensated pressure
1943  *
1944  *
1945  *  @return  Return the actual pressure in floating point
1946  *
1947 */
1948 double bme280_compensate_P_double(s32 v_uncom_pressure_s32)
1949 {
1950         double v_x1_u32 = BME280_ZERO_U8X;
1951         double v_x2_u32 = BME280_ZERO_U8X;
1952         double pressure = BME280_ZERO_U8X;
1953
1954         v_x1_u32 = ((double)p_bme280->cal_param.t_fine /
1955         BME280_FLAOT_TRUE_PRESSURE_2_DATA) -
1956         BME280_FLAOT_TRUE_PRESSURE_6_4_0_0_0_DATA;
1957         v_x2_u32 = v_x1_u32 * v_x1_u32 *
1958         ((double)p_bme280->cal_param.dig_P6) /
1959         BME280_FLAOT_TRUE_PRESSURE_3_2_7_6_8_DATA;
1960         v_x2_u32 = v_x2_u32 + v_x1_u32 *
1961         ((double)p_bme280->cal_param.dig_P5) *
1962         BME280_FLAOT_TRUE_PRESSURE_2_DATA;
1963         v_x2_u32 = (v_x2_u32 / BME280_FLAOT_TRUE_PRESSURE_4_DATA) +
1964         (((double)p_bme280->cal_param.dig_P4) *
1965         BME280_FLAOT_TRUE_PRESSURE_6_5_5_3_6_DATA);
1966         v_x1_u32 = (((double)p_bme280->cal_param.dig_P3) *
1967         v_x1_u32 * v_x1_u32
1968         / BME280_FLAOT_TRUE_PRESSURE_5_2_4_2_8_8_DATA +
1969         ((double)p_bme280->cal_param.dig_P2) * v_x1_u32) /
1970         BME280_FLAOT_TRUE_PRESSURE_5_2_4_2_8_8_DATA;
1971         v_x1_u32 = (BME280_FLAOT_TRUE_PRESSURE_1_DATA + v_x1_u32
1972         / BME280_FLAOT_TRUE_PRESSURE_3_2_7_6_8_DATA) *
1973         ((double)p_bme280->cal_param.dig_P1);
1974         pressure = BME280_FLAOT_TRUE_PRESSURE_1_0_4_8_5_7_6_DATA
1975         - (double)v_uncom_pressure_s32;
1976         /* Avoid exception caused by division by zero */
1977         if (v_x1_u32 != BME280_ZERO_U8X)
1978                 pressure = (pressure - (v_x2_u32
1979                 / BME280_FLAOT_TRUE_PRESSURE_4_0_9_6_DATA))
1980                 * BME280_FLAOT_TRUE_PRESSURE_6_2_5_0_DATA / v_x1_u32;
1981         else
1982                 return BME280_ZERO_U8X;
1983         v_x1_u32 = ((double)p_bme280->cal_param.dig_P9) *
1984         pressure * pressure /
1985         BME280_FLAOT_TRUE_PRESSURE_2_1_4_7_4_8_3_6_4_8_DATA;
1986         v_x2_u32 = pressure * ((double)p_bme280->cal_param.dig_P8)
1987         / BME280_FLAOT_TRUE_PRESSURE_3_2_7_6_8_DATA;
1988         pressure = pressure + (v_x1_u32 + v_x2_u32 +
1989         ((double)p_bme280->cal_param.dig_P7))
1990         / BME280_FLAOT_TRUE_PRESSURE_1_6_DATA;
1991
1992         return pressure;
1993 }
1994 /*!
1995  * @brief Reads actual humidity from uncompensated humidity
1996  * @note returns the value in relative humidity (%rH)
1997  * @note Output value of "42.12" equals 42.12 %rH
1998  *
1999  *  @param v_uncom_humidity_s32 : value of uncompensated humidity
2000  *
2001  *
2002  *
2003  *  @return Return the actual humidity in floating point
2004  *
2005 */
2006 double bme280_compensate_H_double(s32 v_uncom_humidity_s32)
2007 {
2008         double var_h = BME280_ZERO_U8X;
2009         var_h = (((double)p_bme280->cal_param.t_fine)
2010         - BME280_TRUE_HUMIDITY_7_6_8_0_0_DATA);
2011         if (var_h != BME280_ZERO_U8X)
2012                 var_h = (v_uncom_humidity_s32 -
2013                 (((double)p_bme280->cal_param.dig_H4)
2014                 * BME280_TRUE_HUMIDITY_6_4_DATA +
2015                 ((double)p_bme280->cal_param.dig_H5)
2016                 / BME280_TRUE_HUMIDITY_1_6_3_8_4_DATA * var_h))*
2017                 (((double)p_bme280->cal_param.dig_H2)
2018                 /BME280_TRUE_HUMIDITY_6_5_5_3_6_DATA *
2019                 (BME280_TRUE_HUMIDITY_1_DATA + ((double)
2020                 p_bme280->cal_param.dig_H6)
2021                 / BME280_TRUE_HUMIDITY_6_7_1_0_8_8_6_4_DATA
2022                 * var_h * (BME280_TRUE_HUMIDITY_1_DATA + ((double)
2023                 p_bme280->cal_param.dig_H3)
2024                 / BME280_TRUE_HUMIDITY_6_7_1_0_8_8_6_4_DATA * var_h)));
2025         else
2026                 return BME280_ZERO_U8X;
2027         var_h = var_h * (BME280_TRUE_HUMIDITY_1_DATA - ((double)
2028         p_bme280->cal_param.dig_H1)*var_h
2029         / BME280_TRUE_HUMIDITY_5_2_4_2_8_8_DATA);
2030         if (var_h > BME280_TRUE_HUMIDITY_1_0_0_DATA)
2031                 var_h = BME280_TRUE_HUMIDITY_1_0_0_DATA;
2032         else if (var_h < BME280_TRUE_HUMIDITY_0_DATA)
2033                 var_h = BME280_TRUE_HUMIDITY_0_DATA;
2034         return var_h;
2035
2036 }
2037 #endif
2038 #if defined(BME280_ENABLE_INT64) && defined(BME280_64BITSUPPORT_PRESENT)
2039 /*!
2040  * @brief Reads actual pressure from uncompensated pressure
2041  * @note Returns the value in Pa as unsigned 32 bit
2042  * integer in Q24.8 format (24 integer bits and
2043  * 8 fractional bits).
2044  * @note Output value of "24674867"
2045  * represents 24674867 / 256 = 96386.2 Pa = 963.862 hPa
2046  *
2047  *
2048  *
2049  *  @param  v_uncom_pressure_s32 : value of uncompensated temperature
2050  *
2051  *
2052  *  @return Return the actual pressure in u32
2053  *
2054 */
2055 u32 bme280_compensate_P_int64(s32 v_uncom_pressure_s32)
2056 {
2057         s64 v_x1_s64r = BME280_ZERO_U8X;
2058         s64 v_x2_s64r = BME280_ZERO_U8X;
2059         s64 pressure = BME280_ZERO_U8X;
2060         v_x1_s64r = ((s64)p_bme280->cal_param.t_fine)
2061         - BME280_TRUE_PRESSURE_1_2_8_0_0_0_DATA;
2062         v_x2_s64r = v_x1_s64r * v_x1_s64r *
2063         (s64)p_bme280->cal_param.dig_P6;
2064         v_x2_s64r = v_x2_s64r + ((v_x1_s64r *
2065         (s64)p_bme280->cal_param.dig_P5)
2066         << SHIFT_LEFT_17_POSITION);
2067         v_x2_s64r = v_x2_s64r +
2068         (((s64)p_bme280->cal_param.dig_P4)
2069         << SHIFT_LEFT_35_POSITION);
2070         v_x1_s64r = ((v_x1_s64r * v_x1_s64r *
2071         (s64)p_bme280->cal_param.dig_P3)
2072         >> SHIFT_RIGHT_8_POSITION) +
2073         ((v_x1_s64r * (s64)p_bme280->cal_param.dig_P2)
2074         << SHIFT_LEFT_12_POSITION);
2075         v_x1_s64r = (((((s64)BME280_ONE_U8X)
2076         << SHIFT_LEFT_47_POSITION) + v_x1_s64r)) *
2077         ((s64)p_bme280->cal_param.dig_P1)
2078         >> SHIFT_RIGHT_33_POSITION;
2079         pressure = BME280_TRUE_PRESSURE_1_0_4_8_5_7_6_DATA
2080         - v_uncom_pressure_s32;
2081         /* Avoid exception caused by division by zero */
2082         if (v_x1_s64r != BME280_ZERO_U8X)
2083                 #if defined __KERNEL__
2084                         pressure = div64_s64((((pressure
2085                         << SHIFT_LEFT_31_POSITION) - v_x2_s64r)
2086                         * BME280_TRUE_PRESSURE_3_1_2_5_DATA),
2087                         v_x1_s64r);
2088                 #else
2089                         pressure = (((pressure
2090                         << SHIFT_LEFT_31_POSITION) - v_x2_s64r)
2091                         * BME280_TRUE_PRESSURE_3_1_2_5_DATA) / v_x1_s64r;
2092                 #endif
2093         else
2094                 return BME280_ZERO_U8X;
2095         v_x1_s64r = (((s64)p_bme280->cal_param.dig_P9) *
2096         (pressure >> SHIFT_RIGHT_13_POSITION) *
2097         (pressure >> SHIFT_RIGHT_13_POSITION))
2098         >> SHIFT_RIGHT_25_POSITION;
2099         v_x2_s64r = (((s64)p_bme280->cal_param.dig_P8) *
2100         pressure) >> SHIFT_RIGHT_19_POSITION;
2101         pressure = (((pressure + v_x1_s64r +
2102         v_x2_s64r) >> SHIFT_RIGHT_8_POSITION) +
2103         (((s64)p_bme280->cal_param.dig_P7)
2104         << SHIFT_LEFT_4_POSITION));
2105
2106         return (u32)pressure;
2107 }
2108 /*!
2109  * @brief Reads actual pressure from uncompensated pressure
2110  * @note Returns the value in Pa.
2111  * @note Output value of "12337434"
2112  * @note represents 12337434 / 128 = 96386.2 Pa = 963.862 hPa
2113  *
2114  *
2115  *
2116  *  @param v_uncom_pressure_s32 : value of uncompensated pressure
2117  *
2118  *
2119  *  @return the actual pressure in u32
2120  *
2121 */
2122 u32 bme280_compensate_P_int64_twentyfour_bit_output(s32 v_uncom_pressure_s32)
2123 {
2124         u32 pressure = BME280_ZERO_U8X;
2125         pressure = bme280_compensate_P_int64(v_uncom_pressure_s32);
2126         pressure = (u32)(pressure >> SHIFT_RIGHT_1_POSITION);
2127         return pressure;
2128 }
2129 #endif
2130 /*!
2131  * @brief Computing waiting time for sensor data read
2132  *
2133  *
2134  *
2135  *
2136  *  @param v_delaytime_u8 : The value of delay time for force mode
2137  *
2138  *
2139  *      @retval 0 -> Success
2140  *
2141  *
2142  */
2143 BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8
2144 *v_delaytime_u8)
2145 {
2146         /* used to return the communication result*/
2147         BME280_RETURN_FUNCTION_TYPE com_rslt = SUCCESS;
2148
2149         *v_delaytime_u8 = (T_INIT_MAX +
2150         T_MEASURE_PER_OSRS_MAX *
2151         (((BME280_ONE_U8X
2152         << p_bme280->oversamp_temperature)
2153         >> SHIFT_RIGHT_1_POSITION) +
2154         ((BME280_ONE_U8X << p_bme280->oversamp_pressure)
2155         >> SHIFT_RIGHT_1_POSITION) +
2156         ((BME280_ONE_U8X << p_bme280->oversamp_humidity)
2157         >> SHIFT_RIGHT_1_POSITION))+
2158         (p_bme280->oversamp_pressure ?
2159         T_SETUP_PRESSURE_MAX : BME280_ZERO_U8X) +
2160         (p_bme280->oversamp_humidity ?
2161         T_SETUP_HUMIDITY_MAX : BME280_ZERO_U8X)
2162         + BME280_FIVETEEN_U8X) / BME280_SIXTEEN_U8X;
2163         return com_rslt;
2164 }