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