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