]> git.itanic.dy.fi Git - BME280_driver/blob - bme280.c
d1f460ef31ae2c8468e8e120dbe7826d40b8fbad
[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  *      dig_H4    |  0xE4 and 0xE5   | from 4 : 11 to 0: 3
634  *      dig_H5    |  0xE5 and 0xE6   | from 0 : 3 to 4: 11
635  *      dig_H6    |         0xE7     | from 0 to 7
636  *
637  *      @return results of bus communication function
638  *      @retval 0 -> Success
639  *      @retval -1 -> Error
640  *
641  *
642 */
643 BME280_RETURN_FUNCTION_TYPE bme280_get_calib_param(void)
644 {
645         /* used to return the communication result*/
646         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
647         u8 a_data_u8[BME280_CALIB_DATA_SIZE] = {
648         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         BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
655         BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
656         BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
657         /* check the p_bme280 structure pointer as NULL*/
658         if (p_bme280 == BME280_NULL) {
659                 return E_BME280_NULL_PTR;
660                 } else {
661                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
662                         p_bme280->dev_addr,
663                         BME280_TEMPERATURE_CALIB_DIG_T1_LSB_REG,
664                         a_data_u8,
665                         BME280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH);
666
667                         p_bme280->cal_param.dig_T1 = (u16)(((
668                         (u16)((u8)a_data_u8[
669                         BME280_TEMPERATURE_CALIB_DIG_T1_MSB])) <<
670                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
671                         | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T1_LSB]);
672                         p_bme280->cal_param.dig_T2 = (s16)(((
673                         (s16)((s8)a_data_u8[
674                         BME280_TEMPERATURE_CALIB_DIG_T2_MSB])) <<
675                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
676                         | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T2_LSB]);
677                         p_bme280->cal_param.dig_T3 = (s16)(((
678                         (s16)((s8)a_data_u8[
679                         BME280_TEMPERATURE_CALIB_DIG_T3_MSB])) <<
680                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
681                         | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T3_LSB]);
682                         p_bme280->cal_param.dig_P1 = (u16)(((
683                         (u16)((u8)a_data_u8[
684                         BME280_PRESSURE_CALIB_DIG_P1_MSB])) <<
685                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
686                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P1_LSB]);
687                         p_bme280->cal_param.dig_P2 = (s16)(((
688                         (s16)((s8)a_data_u8[
689                         BME280_PRESSURE_CALIB_DIG_P2_MSB])) <<
690                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
691                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P2_LSB]);
692                         p_bme280->cal_param.dig_P3 = (s16)(((
693                         (s16)((s8)a_data_u8[
694                         BME280_PRESSURE_CALIB_DIG_P3_MSB])) <<
695                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
696                         | a_data_u8[
697                         BME280_PRESSURE_CALIB_DIG_P3_LSB]);
698                         p_bme280->cal_param.dig_P4 = (s16)(((
699                         (s16)((s8)a_data_u8[
700                         BME280_PRESSURE_CALIB_DIG_P4_MSB])) <<
701                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
702                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P4_LSB]);
703                         p_bme280->cal_param.dig_P5 = (s16)(((
704                         (s16)((s8)a_data_u8[
705                         BME280_PRESSURE_CALIB_DIG_P5_MSB])) <<
706                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
707                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P5_LSB]);
708                         p_bme280->cal_param.dig_P6 = (s16)(((
709                         (s16)((s8)a_data_u8[
710                         BME280_PRESSURE_CALIB_DIG_P6_MSB])) <<
711                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
712                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P6_LSB]);
713                         p_bme280->cal_param.dig_P7 = (s16)(((
714                         (s16)((s8)a_data_u8[
715                         BME280_PRESSURE_CALIB_DIG_P7_MSB])) <<
716                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
717                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P7_LSB]);
718                         p_bme280->cal_param.dig_P8 = (s16)(((
719                         (s16)((s8)a_data_u8[
720                         BME280_PRESSURE_CALIB_DIG_P8_MSB])) <<
721                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
722                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P8_LSB]);
723                         p_bme280->cal_param.dig_P9 = (s16)(((
724                         (s16)((s8)a_data_u8[
725                         BME280_PRESSURE_CALIB_DIG_P9_MSB])) <<
726                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
727                         | a_data_u8[BME280_PRESSURE_CALIB_DIG_P9_LSB]);
728                         p_bme280->cal_param.dig_H1 =
729                         a_data_u8[BME280_HUMIDITY_CALIB_DIG_H1];
730                         com_rslt += p_bme280->BME280_BUS_READ_FUNC(
731                         p_bme280->dev_addr,
732                         BME280_HUMIDITY_CALIB_DIG_H2_LSB_REG, a_data_u8,
733                         BME280_HUMIDITY_CALIB_DATA_LENGTH);
734                         p_bme280->cal_param.dig_H2 = (s16)(((
735                         (s16)((s8)a_data_u8[
736                         BME280_HUMIDITY_CALIB_DIG_H2_MSB])) <<
737                         BME280_SHIFT_BIT_POSITION_BY_08_BITS)
738                         | a_data_u8[BME280_HUMIDITY_CALIB_DIG_H2_LSB]);
739                         p_bme280->cal_param.dig_H3 =
740                         a_data_u8[BME280_HUMIDITY_CALIB_DIG_H3];
741                         p_bme280->cal_param.dig_H4 = (s16)(((
742                         (s16)((s8)a_data_u8[
743                         BME280_HUMIDITY_CALIB_DIG_H4_MSB])) <<
744                         BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
745                         (((u8)BME280_MASK_DIG_H4) &
746                         a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB]));
747                         p_bme280->cal_param.dig_H5 = (s16)(((
748                         (s16)((s8)a_data_u8[
749                         BME280_HUMIDITY_CALIB_DIG_H5_MSB])) <<
750                         BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
751                         (a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB] >>
752                         BME280_SHIFT_BIT_POSITION_BY_04_BITS));
753                         p_bme280->cal_param.dig_H6 =
754                         (s8)a_data_u8[BME280_HUMIDITY_CALIB_DIG_H6];
755                 }
756         return com_rslt;
757 }
758 /*!
759  *      @brief This API is used to get
760  *      the temperature oversampling setting in the register 0xF4
761  *      bits from 5 to 7
762  *
763  *      value               |   Temperature oversampling
764  * ---------------------|---------------------------------
765  *      0x00                | Skipped
766  *      0x01                | BME280_OVERSAMP_1X
767  *      0x02                | BME280_OVERSAMP_2X
768  *      0x03                | BME280_OVERSAMP_4X
769  *      0x04                | BME280_OVERSAMP_8X
770  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
771  *
772  *
773  *  @param v_value_u8 : The value of temperature over sampling
774  *
775  *
776  *
777  *      @return results of bus communication function
778  *      @retval 0 -> Success
779  *      @retval -1 -> Error
780  *
781  *
782 */
783 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_temperature(
784 u8 *v_value_u8)
785 {
786         /* used to return the communication result*/
787         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
788         u8 v_data_u8 = BME280_INIT_VALUE;
789         /* check the p_bme280 structure pointer as NULL*/
790         if (p_bme280 == BME280_NULL) {
791                 return E_BME280_NULL_PTR;
792                 } else {
793                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
794                         p_bme280->dev_addr,
795                         BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
796                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
797                         *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
798                         BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE);
799
800                         p_bme280->oversamp_temperature = *v_value_u8;
801                 }
802         return com_rslt;
803 }
804 /*!
805  *      @brief This API is used to set
806  *      the temperature oversampling setting in the register 0xF4
807  *      bits from 5 to 7
808  *
809  *      value               |   Temperature oversampling
810  * ---------------------|---------------------------------
811  *      0x00                | Skipped
812  *      0x01                | BME280_OVERSAMP_1X
813  *      0x02                | BME280_OVERSAMP_2X
814  *      0x03                | BME280_OVERSAMP_4X
815  *      0x04                | BME280_OVERSAMP_8X
816  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
817  *
818  *
819  *  @param v_value_u8 : The value of temperature over sampling
820  *
821  *
822  *
823  *      @return results of bus communication function
824  *      @retval 0 -> Success
825  *      @retval -1 -> Error
826  *
827  *
828 */
829 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_temperature(
830 u8 v_value_u8)
831 {
832         /* used to return the communication result*/
833         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
834         u8 v_data_u8 = BME280_INIT_VALUE;
835         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
836         u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
837         u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
838         /* check the p_bme280 structure pointer as NULL*/
839         if (p_bme280 == BME280_NULL) {
840                 return E_BME280_NULL_PTR;
841                 } else {
842                         v_data_u8 = p_bme280->ctrl_meas_reg;
843                         v_data_u8 =
844                         BME280_SET_BITSLICE(v_data_u8,
845                         BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE, v_value_u8);
846                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
847                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
848                                 com_rslt += bme280_set_soft_rst();
849                                 p_bme280->delay_msec(BME280_3MS_DELAY);
850                                 /* write previous value
851                                 of configuration register*/
852                                 v_pre_config_value_u8 = p_bme280->config_reg;
853                                 com_rslt += bme280_write_register(
854                                         BME280_CONFIG_REG,
855                                 &v_pre_config_value_u8,
856                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
857                                 /* write previous value
858                                 of humidity oversampling*/
859                                 v_pre_ctrl_hum_value_u8 =
860                                 p_bme280->ctrl_hum_reg;
861                                 com_rslt += bme280_write_register(
862                                         BME280_CTRL_HUMIDITY_REG,
863                                 &v_pre_ctrl_hum_value_u8,
864                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
865                                 /* write previous and updated value
866                                 of configuration register*/
867                                 com_rslt += bme280_write_register(
868                                         BME280_CTRL_MEAS_REG,
869                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
870                         } else {
871                                 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
872                                 p_bme280->dev_addr,
873                                 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
874                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
875                         }
876                                 p_bme280->oversamp_temperature = v_value_u8;
877                                 /* read the control measurement register value*/
878                                 com_rslt = bme280_read_register(
879                                         BME280_CTRL_MEAS_REG,
880                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
881                                 p_bme280->ctrl_meas_reg = v_data_u8;
882                                 /* read the control humidity register value*/
883                                 com_rslt += bme280_read_register(
884                                         BME280_CTRL_HUMIDITY_REG,
885                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
886                                 p_bme280->ctrl_hum_reg = v_data_u8;
887                                 /* read the control
888                                 configuration register value*/
889                                 com_rslt += bme280_read_register(
890                                         BME280_CONFIG_REG,
891                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
892                                 p_bme280->config_reg = v_data_u8;
893                 }
894         return com_rslt;
895 }
896 /*!
897  *      @brief This API is used to get
898  *      the pressure oversampling setting in the register 0xF4
899  *      bits from 2 to 4
900  *
901  *      value              | Pressure oversampling
902  * --------------------|--------------------------
903  *      0x00               | Skipped
904  *      0x01               | BME280_OVERSAMP_1X
905  *      0x02               | BME280_OVERSAMP_2X
906  *      0x03               | BME280_OVERSAMP_4X
907  *      0x04               | BME280_OVERSAMP_8X
908  *      0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
909  *
910  *
911  *  @param v_value_u8 : The value of pressure oversampling
912  *
913  *
914  *
915  *      @return results of bus communication function
916  *      @retval 0 -> Success
917  *      @retval -1 -> Error
918  *
919  *
920 */
921 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_pressure(
922 u8 *v_value_u8)
923 {
924         /* used to return the communication result*/
925         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
926         u8 v_data_u8 = BME280_INIT_VALUE;
927         /* check the p_bme280 structure pointer as NULL*/
928         if (p_bme280 == BME280_NULL) {
929                 return E_BME280_NULL_PTR;
930                 } else {
931                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
932                         p_bme280->dev_addr,
933                         BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
934                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
935                         *v_value_u8 = BME280_GET_BITSLICE(
936                         v_data_u8,
937                         BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE);
938
939                         p_bme280->oversamp_pressure = *v_value_u8;
940                 }
941         return com_rslt;
942 }
943 /*!
944  *      @brief This API is used to set
945  *      the pressure oversampling setting in the register 0xF4
946  *      bits from 2 to 4
947  *
948  *      value              | Pressure oversampling
949  * --------------------|--------------------------
950  *      0x00               | Skipped
951  *      0x01               | BME280_OVERSAMP_1X
952  *      0x02               | BME280_OVERSAMP_2X
953  *      0x03               | BME280_OVERSAMP_4X
954  *      0x04               | BME280_OVERSAMP_8X
955  *      0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
956  *
957  *
958  *  @param v_value_u8 : The value of pressure oversampling
959  *
960  *
961  *
962  *      @return results of bus communication function
963  *      @retval 0 -> Success
964  *      @retval -1 -> Error
965  *
966  *
967 */
968 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_pressure(
969 u8 v_value_u8)
970 {
971         /* used to return the communication result*/
972         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
973         u8 v_data_u8 = BME280_INIT_VALUE;
974         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
975         u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
976         u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
977         /* check the p_bme280 structure pointer as NULL*/
978         if (p_bme280 == BME280_NULL) {
979                 return E_BME280_NULL_PTR;
980                 } else {
981                         v_data_u8 = p_bme280->ctrl_meas_reg;
982                         v_data_u8 =
983                         BME280_SET_BITSLICE(v_data_u8,
984                         BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE, v_value_u8);
985                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
986                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
987                                 com_rslt += bme280_set_soft_rst();
988                                 p_bme280->delay_msec(BME280_3MS_DELAY);
989                                 /* write previous value of
990                                 configuration register*/
991                                 v_pre_config_value_u8 = p_bme280->config_reg;
992                                 com_rslt = bme280_write_register(
993                                         BME280_CONFIG_REG,
994                                 &v_pre_config_value_u8,
995                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
996                                 /* write previous value of
997                                 humidity oversampling*/
998                                 v_pre_ctrl_hum_value_u8 =
999                                 p_bme280->ctrl_hum_reg;
1000                                 com_rslt += bme280_write_register(
1001                                         BME280_CTRL_HUMIDITY_REG,
1002                                 &v_pre_ctrl_hum_value_u8,
1003                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1004                                 /* write previous and updated value of
1005                                 control measurement register*/
1006                                 bme280_write_register(
1007                                         BME280_CTRL_MEAS_REG,
1008                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1009                         } else {
1010                                 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1011                                 p_bme280->dev_addr,
1012                                 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
1013                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1014                         }
1015                                 p_bme280->oversamp_pressure = v_value_u8;
1016                                 /* read the control measurement register value*/
1017                                 com_rslt = bme280_read_register(
1018                                         BME280_CTRL_MEAS_REG,
1019                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1020                                 p_bme280->ctrl_meas_reg = v_data_u8;
1021                                 /* read the control humidity register value*/
1022                                 com_rslt += bme280_read_register(
1023                                         BME280_CTRL_HUMIDITY_REG,
1024                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1025                                 p_bme280->ctrl_hum_reg = v_data_u8;
1026                                 /* read the control
1027                                 configuration register value*/
1028                                 com_rslt += bme280_read_register(
1029                                         BME280_CONFIG_REG,
1030                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1031                                 p_bme280->config_reg = v_data_u8;
1032                 }
1033         return com_rslt;
1034 }
1035 /*!
1036  *      @brief This API is used to get
1037  *      the humidity oversampling setting in the register 0xF2
1038  *      bits from 0 to 2
1039  *
1040  *      value               | Humidity oversampling
1041  * ---------------------|-------------------------
1042  *      0x00                | Skipped
1043  *      0x01                | BME280_OVERSAMP_1X
1044  *      0x02                | BME280_OVERSAMP_2X
1045  *      0x03                | BME280_OVERSAMP_4X
1046  *      0x04                | BME280_OVERSAMP_8X
1047  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
1048  *
1049  *
1050  *  @param  v_value_u8 : The value of humidity over sampling
1051  *
1052  *
1053  *
1054  *      @return results of bus communication function
1055  *      @retval 0 -> Success
1056  *      @retval -1 -> Error
1057  *
1058  *
1059 */
1060 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_humidity(
1061 u8 *v_value_u8)
1062 {
1063         /* used to return the communication result*/
1064         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1065         u8 v_data_u8 = BME280_INIT_VALUE;
1066         /* check the p_bme280 structure pointer as NULL*/
1067         if (p_bme280 == BME280_NULL) {
1068                 return E_BME280_NULL_PTR;
1069                 } else {
1070                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1071                         p_bme280->dev_addr,
1072                         BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1073                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1074                         *v_value_u8 = BME280_GET_BITSLICE(
1075                         v_data_u8,
1076                         BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY);
1077
1078                         p_bme280->oversamp_humidity = *v_value_u8;
1079                 }
1080         return com_rslt;
1081 }
1082 /*!
1083  *      @brief This API is used to set
1084  *      the humidity oversampling setting in the register 0xF2
1085  *      bits from 0 to 2
1086  *
1087  *      value               | Humidity oversampling
1088  * ---------------------|-------------------------
1089  *      0x00                | Skipped
1090  *      0x01                | BME280_OVERSAMP_1X
1091  *      0x02                | BME280_OVERSAMP_2X
1092  *      0x03                | BME280_OVERSAMP_4X
1093  *      0x04                | BME280_OVERSAMP_8X
1094  *      0x05,0x06 and 0x07  | BME280_OVERSAMP_16X
1095  *
1096  *
1097  *  @param  v_value_u8 : The value of humidity over sampling
1098  *
1099  *
1100  *
1101  * @note The "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1102  * register sets the humidity
1103  * data acquisition options of the device.
1104  * @note changes to this registers only become
1105  * effective after a write operation to
1106  * "BME280_CTRL_MEAS_REG" register.
1107  * @note In the code automated reading and writing of
1108  *      "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1109  * @note register first set the
1110  * "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1111  *  and then read and write
1112  *  the "BME280_CTRL_MEAS_REG" register in the function.
1113  *
1114  *
1115  *      @return results of bus communication function
1116  *      @retval 0 -> Success
1117  *      @retval -1 -> Error
1118  *
1119  *
1120 */
1121 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_humidity(
1122 u8 v_value_u8)
1123 {
1124         /* used to return the communication result*/
1125         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1126         u8 v_data_u8 = BME280_INIT_VALUE;
1127         u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1128         u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1129         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1130         /* check the p_bme280 structure pointer as NULL*/
1131         if (p_bme280 == BME280_NULL) {
1132                 return E_BME280_NULL_PTR;
1133                 } else {
1134                         /* write humidity oversampling*/
1135                         v_data_u8 = p_bme280->ctrl_hum_reg;
1136                         v_data_u8 =
1137                         BME280_SET_BITSLICE(v_data_u8,
1138                         BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY, v_value_u8);
1139                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1140                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1141                                 com_rslt += bme280_set_soft_rst();
1142                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1143                                 /* write previous value of
1144                                 configuration register*/
1145                                 v_pre_config_value_u8 = p_bme280->config_reg;
1146                                 com_rslt += bme280_write_register(
1147                                         BME280_CONFIG_REG,
1148                                 &v_pre_config_value_u8,
1149                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1150                                 /* write the value of control humidity*/
1151                                 com_rslt += bme280_write_register(
1152                                         BME280_CTRL_HUMIDITY_REG,
1153                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1154                                 /* write previous value of
1155                                 control measurement register*/
1156                                 pre_ctrl_meas_value =
1157                                 p_bme280->ctrl_meas_reg;
1158                                 com_rslt += bme280_write_register(
1159                                         BME280_CTRL_MEAS_REG,
1160                                 &pre_ctrl_meas_value,
1161                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1162                         } else {
1163                                 com_rslt +=
1164                                 p_bme280->BME280_BUS_WRITE_FUNC(
1165                                 p_bme280->dev_addr,
1166                                 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1167                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1168                                 /* Control humidity write will effective only
1169                                 after the control measurement register*/
1170                                 pre_ctrl_meas_value =
1171                                 p_bme280->ctrl_meas_reg;
1172                                 com_rslt += bme280_write_register(
1173                                         BME280_CTRL_MEAS_REG,
1174                                 &pre_ctrl_meas_value,
1175                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1176                         }
1177                         p_bme280->oversamp_humidity = v_value_u8;
1178                         /* read the control measurement register value*/
1179                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1180                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1181                         p_bme280->ctrl_meas_reg = v_data_u8;
1182                         /* read the control humidity register value*/
1183                         com_rslt += bme280_read_register(
1184                         BME280_CTRL_HUMIDITY_REG,
1185                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1186                         p_bme280->ctrl_hum_reg = v_data_u8;
1187                         /* read the control configuration register value*/
1188                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1189                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1190                         p_bme280->config_reg = v_data_u8;
1191                 }
1192         return com_rslt;
1193 }
1194 /*!
1195  *      @brief This API used to get the
1196  *      Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1197  *
1198  *
1199  *
1200  *      @param v_power_mode_u8 : The value of power mode
1201  *  value           |    mode
1202  * -----------------|------------------
1203  *      0x00            | BME280_SLEEP_MODE
1204  *      0x01 and 0x02   | BME280_FORCED_MODE
1205  *      0x03            | BME280_NORMAL_MODE
1206  *
1207  *      @return results of bus communication function
1208  *      @retval 0 -> Success
1209  *      @retval -1 -> Error
1210  *
1211  *
1212 */
1213 BME280_RETURN_FUNCTION_TYPE bme280_get_power_mode(u8 *v_power_mode_u8)
1214 {
1215         /* used to return the communication result*/
1216         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1217         u8 v_mode_u8r = BME280_INIT_VALUE;
1218         /* check the p_bme280 structure pointer as NULL*/
1219         if (p_bme280 == BME280_NULL) {
1220                 return E_BME280_NULL_PTR;
1221                 } else {
1222                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1223                         p_bme280->dev_addr,
1224                         BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1225                         &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1226                         *v_power_mode_u8 = BME280_GET_BITSLICE(v_mode_u8r,
1227                         BME280_CTRL_MEAS_REG_POWER_MODE);
1228                 }
1229         return com_rslt;
1230 }
1231 /*!
1232  *      @brief This API used to set the
1233  *      Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1234  *
1235  *
1236  *
1237  *      @param v_power_mode_u8 : The value of power mode
1238  *  value           |    mode
1239  * -----------------|------------------
1240  *      0x00            | BME280_SLEEP_MODE
1241  *      0x01 and 0x02   | BME280_FORCED_MODE
1242  *      0x03            | BME280_NORMAL_MODE
1243  *
1244  *      @return results of bus communication function
1245  *      @retval 0 -> Success
1246  *      @retval -1 -> Error
1247  *
1248  *
1249 */
1250 BME280_RETURN_FUNCTION_TYPE bme280_set_power_mode(u8 v_power_mode_u8)
1251 {
1252         /* used to return the communication result*/
1253         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1254         u8 v_mode_u8r = BME280_INIT_VALUE;
1255         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1256         u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1257         u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1258         u8 v_data_u8 = BME280_INIT_VALUE;
1259         /* check the p_bme280 structure pointer as NULL*/
1260         if (p_bme280 == BME280_NULL) {
1261                 return E_BME280_NULL_PTR;
1262                 } else {
1263                         if (v_power_mode_u8 <= BME280_NORMAL_MODE) {
1264                                 v_mode_u8r = p_bme280->ctrl_meas_reg;
1265                                 v_mode_u8r =
1266                                 BME280_SET_BITSLICE(v_mode_u8r,
1267                                 BME280_CTRL_MEAS_REG_POWER_MODE,
1268                                 v_power_mode_u8);
1269                                 com_rslt = bme280_get_power_mode(
1270                                         &v_prev_pow_mode_u8);
1271                                 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1272                                         com_rslt += bme280_set_soft_rst();
1273                                         p_bme280->delay_msec(BME280_3MS_DELAY);
1274                                         /* write previous value of
1275                                         configuration register*/
1276                                         v_pre_config_value_u8 =
1277                                         p_bme280->config_reg;
1278                                         com_rslt = bme280_write_register(
1279                                                 BME280_CONFIG_REG,
1280                                         &v_pre_config_value_u8,
1281                                         BME280_GEN_READ_WRITE_DATA_LENGTH);
1282                                         /* write previous value of
1283                                         humidity oversampling*/
1284                                         v_pre_ctrl_hum_value_u8 =
1285                                         p_bme280->ctrl_hum_reg;
1286                                         com_rslt += bme280_write_register(
1287                                         BME280_CTRL_HUMIDITY_REG,
1288                                         &v_pre_ctrl_hum_value_u8,
1289                                         BME280_GEN_READ_WRITE_DATA_LENGTH);
1290                                         /* write previous and updated value of
1291                                         control measurement register*/
1292                                         com_rslt += bme280_write_register(
1293                                         BME280_CTRL_MEAS_REG,
1294                                         &v_mode_u8r,
1295                                         BME280_GEN_READ_WRITE_DATA_LENGTH);
1296                                 } else {
1297                                         com_rslt =
1298                                         p_bme280->BME280_BUS_WRITE_FUNC(
1299                                         p_bme280->dev_addr,
1300                                         BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1301                                         &v_mode_u8r,
1302                                         BME280_GEN_READ_WRITE_DATA_LENGTH);
1303                                 }
1304                                 /* read the control measurement register value*/
1305                                 com_rslt = bme280_read_register(
1306                                         BME280_CTRL_MEAS_REG,
1307                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1308                                 p_bme280->ctrl_meas_reg = v_data_u8;
1309                                 /* read the control humidity register value*/
1310                                 com_rslt += bme280_read_register(
1311                                         BME280_CTRL_HUMIDITY_REG,
1312                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1313                                 p_bme280->ctrl_hum_reg = v_data_u8;
1314                                 /* read the config register value*/
1315                                 com_rslt += bme280_read_register(
1316                                         BME280_CONFIG_REG,
1317                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1318                                 p_bme280->config_reg = v_data_u8;
1319                         } else {
1320                         com_rslt = E_BME280_OUT_OF_RANGE;
1321                         }
1322                 }
1323         return com_rslt;
1324 }
1325 /*!
1326  * @brief Used to reset the sensor
1327  * The value 0xB6 is written to the 0xE0
1328  * register the device is reset using the
1329  * complete power-on-reset procedure.
1330  * @note Soft reset can be easily set using bme280_set_softreset().
1331  * @note Usage Hint : bme280_set_softreset()
1332  *
1333  *
1334  *      @return results of bus communication function
1335  *      @retval 0 -> Success
1336  *      @retval -1 -> Error
1337  *
1338  *
1339 */
1340 BME280_RETURN_FUNCTION_TYPE bme280_set_soft_rst(void)
1341 {
1342         /* used to return the communication result*/
1343         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1344         u8 v_data_u8 = BME280_SOFT_RESET_CODE;
1345         /* check the p_bme280 structure pointer as NULL*/
1346         if (p_bme280 == BME280_NULL) {
1347                 return E_BME280_NULL_PTR;
1348                 } else {
1349                         com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1350                         p_bme280->dev_addr,
1351                         BME280_RST_REG, &v_data_u8,
1352                         BME280_GEN_READ_WRITE_DATA_LENGTH);
1353                 }
1354         return com_rslt;
1355 }
1356 /*!
1357  *      @brief This API used to get the sensor
1358  *      SPI mode(communication type) in the register 0xF5 bit 0
1359  *
1360  *
1361  *
1362  *      @param v_enable_disable_u8 : The value of SPI enable
1363  *      value  | Description
1364  * --------|--------------
1365  *   0     | Disable
1366  *   1     | Enable
1367  *
1368  *
1369  *
1370  *      @return results of bus communication function
1371  *      @retval 0 -> Success
1372  *      @retval -1 -> Error
1373  *
1374  *
1375 */
1376 BME280_RETURN_FUNCTION_TYPE bme280_get_spi3(u8 *v_enable_disable_u8)
1377 {
1378         /* used to return the communication result*/
1379         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1380         u8 v_data_u8 = BME280_INIT_VALUE;
1381         /* check the p_bme280 structure pointer as NULL*/
1382         if (p_bme280 == BME280_NULL) {
1383                 return E_BME280_NULL_PTR;
1384                 } else {
1385                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1386                         p_bme280->dev_addr,
1387                         BME280_CONFIG_REG_SPI3_ENABLE__REG,
1388                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1389                         *v_enable_disable_u8 = BME280_GET_BITSLICE(
1390                         v_data_u8,
1391                         BME280_CONFIG_REG_SPI3_ENABLE);
1392                 }
1393         return com_rslt;
1394 }
1395 /*!
1396  *      @brief This API used to set the sensor
1397  *      SPI mode(communication type) in the register 0xF5 bit 0
1398  *
1399  *
1400  *
1401  *      @param v_enable_disable_u8 : The value of SPI enable
1402  *      value  | Description
1403  * --------|--------------
1404  *   0     | Disable
1405  *   1     | Enable
1406  *
1407  *
1408  *
1409  *      @return results of bus communication function
1410  *      @retval 0 -> Success
1411  *      @retval -1 -> Error
1412  *
1413  *
1414 */
1415 BME280_RETURN_FUNCTION_TYPE bme280_set_spi3(u8 v_enable_disable_u8)
1416 {
1417         /* used to return the communication result*/
1418         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1419         u8 v_data_u8 = BME280_INIT_VALUE;
1420         u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1421         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1422         u8 v_pre_ctrl_hum_value_u8 =  BME280_INIT_VALUE;
1423         /* check the p_bme280 structure pointer as NULL*/
1424         if (p_bme280 == BME280_NULL) {
1425                 return E_BME280_NULL_PTR;
1426                 } else {
1427                         v_data_u8 = p_bme280->config_reg;
1428                         v_data_u8 =
1429                         BME280_SET_BITSLICE(v_data_u8,
1430                         BME280_CONFIG_REG_SPI3_ENABLE, v_enable_disable_u8);
1431                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1432                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1433                                 com_rslt += bme280_set_soft_rst();
1434                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1435                                 /* write previous and updated value of
1436                                 configuration register*/
1437                                 com_rslt += bme280_write_register(
1438                                         BME280_CONFIG_REG,
1439                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1440                                 /* write previous value of
1441                                 humidity oversampling*/
1442                                 v_pre_ctrl_hum_value_u8 =
1443                                 p_bme280->ctrl_hum_reg;
1444                                 com_rslt +=  bme280_write_register(
1445                                         BME280_CTRL_HUMIDITY_REG,
1446                                 &v_pre_ctrl_hum_value_u8,
1447                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1448                                 /* write previous value of
1449                                 control measurement register*/
1450                                 pre_ctrl_meas_value =
1451                                 p_bme280->ctrl_meas_reg;
1452                                 com_rslt += bme280_write_register(
1453                                         BME280_CTRL_MEAS_REG,
1454                                 &pre_ctrl_meas_value,
1455                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1456                         } else {
1457                                 com_rslt =
1458                                 p_bme280->BME280_BUS_WRITE_FUNC(
1459                                 p_bme280->dev_addr,
1460                                 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1461                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1462                         }
1463                         /* read the control measurement register value*/
1464                         com_rslt += bme280_read_register(
1465                                 BME280_CTRL_MEAS_REG,
1466                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1467                         p_bme280->ctrl_meas_reg = v_data_u8;
1468                         /* read the control humidity register value*/
1469                         com_rslt += bme280_read_register(
1470                                 BME280_CTRL_HUMIDITY_REG,
1471                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1472                         p_bme280->ctrl_hum_reg = v_data_u8;
1473                         /* read the control configuration register value*/
1474                         com_rslt += bme280_read_register(
1475                                 BME280_CONFIG_REG,
1476                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1477                         p_bme280->config_reg = v_data_u8;
1478                 }
1479         return com_rslt;
1480 }
1481 /*!
1482  *      @brief This API is used to reads filter setting
1483  *      in the register 0xF5 bit 3 and 4
1484  *
1485  *
1486  *
1487  *      @param v_value_u8 : The value of IIR filter coefficient
1488  *
1489  *      value       |   Filter coefficient
1490  * -------------|-------------------------
1491  *      0x00        | BME280_FILTER_COEFF_OFF
1492  *      0x01        | BME280_FILTER_COEFF_2
1493  *      0x02        | BME280_FILTER_COEFF_4
1494  *      0x03        | BME280_FILTER_COEFF_8
1495  *      0x04        | BME280_FILTER_COEFF_16
1496  *
1497  *      @return results of bus communication function
1498  *      @retval 0 -> Success
1499  *      @retval -1 -> Error
1500  *
1501  *
1502 */
1503 BME280_RETURN_FUNCTION_TYPE bme280_get_filter(u8 *v_value_u8)
1504 {
1505         /* used to return the communication result*/
1506         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1507         u8 v_data_u8 = BME280_INIT_VALUE;
1508         /* check the p_bme280 structure pointer as NULL*/
1509         if (p_bme280 == BME280_NULL) {
1510                 return E_BME280_NULL_PTR;
1511                 } else {
1512                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1513                         p_bme280->dev_addr,
1514                         BME280_CONFIG_REG_FILTER__REG,
1515                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1516                         *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
1517                         BME280_CONFIG_REG_FILTER);
1518                 }
1519         return com_rslt;
1520 }
1521 /*!
1522  *      @brief This API is used to write filter setting
1523  *      in the register 0xF5 bit 3 and 4
1524  *
1525  *
1526  *
1527  *      @param v_value_u8 : The value of IIR filter coefficient
1528  *
1529  *      value       |   Filter coefficient
1530  * -------------|-------------------------
1531  *      0x00        | BME280_FILTER_COEFF_OFF
1532  *      0x01        | BME280_FILTER_COEFF_2
1533  *      0x02        | BME280_FILTER_COEFF_4
1534  *      0x03        | BME280_FILTER_COEFF_8
1535  *      0x04        | BME280_FILTER_COEFF_16
1536  *
1537  *      @return results of bus communication function
1538  *      @retval 0 -> Success
1539  *      @retval -1 -> Error
1540  *
1541  *
1542 */
1543 BME280_RETURN_FUNCTION_TYPE bme280_set_filter(u8 v_value_u8)
1544 {
1545         /* used to return the communication result*/
1546         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1547         u8 v_data_u8 = BME280_INIT_VALUE;
1548         u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1549         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1550         u8 v_pre_ctrl_hum_value_u8 =  BME280_INIT_VALUE;
1551         /* check the p_bme280 structure pointer as NULL*/
1552         if (p_bme280 == BME280_NULL) {
1553                 return E_BME280_NULL_PTR;
1554                 } else {
1555                         v_data_u8 = p_bme280->config_reg;
1556                         v_data_u8 =
1557                         BME280_SET_BITSLICE(v_data_u8,
1558                         BME280_CONFIG_REG_FILTER, v_value_u8);
1559                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1560                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1561                                 com_rslt += bme280_set_soft_rst();
1562                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1563                                 /* write previous and updated value of
1564                                 configuration register*/
1565                                 com_rslt += bme280_write_register(
1566                                         BME280_CONFIG_REG,
1567                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1568                                 /* write previous value of
1569                                 humidity oversampling*/
1570                                 v_pre_ctrl_hum_value_u8 =
1571                                 p_bme280->ctrl_hum_reg;
1572                                 com_rslt += bme280_write_register(
1573                                         BME280_CTRL_HUMIDITY_REG,
1574                                 &v_pre_ctrl_hum_value_u8,
1575                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1576                                 /* write previous value of
1577                                 control measurement register*/
1578                                 pre_ctrl_meas_value =
1579                                 p_bme280->ctrl_meas_reg;
1580                                 com_rslt += bme280_write_register(
1581                                         BME280_CTRL_MEAS_REG,
1582                                 &pre_ctrl_meas_value,
1583                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1584                         } else {
1585                                 com_rslt =
1586                                 p_bme280->BME280_BUS_WRITE_FUNC(
1587                                 p_bme280->dev_addr,
1588                                 BME280_CONFIG_REG_FILTER__REG,
1589                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1590                         }
1591                         /* read the control measurement register value*/
1592                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1593                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1594                         p_bme280->ctrl_meas_reg = v_data_u8;
1595                         /* read the control humidity register value*/
1596                         com_rslt += bme280_read_register(
1597                         BME280_CTRL_HUMIDITY_REG,
1598                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1599                         p_bme280->ctrl_hum_reg = v_data_u8;
1600                         /* read the configuration register value*/
1601                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1602                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1603                         p_bme280->config_reg = v_data_u8;
1604                 }
1605         return com_rslt;
1606 }
1607 /*!
1608  *      @brief This API used to Read the
1609  *      standby duration time from the sensor in the register 0xF5 bit 5 to 7
1610  *
1611  *      @param v_standby_durn_u8 : The value of standby duration time value.
1612  *  value       | standby duration
1613  * -------------|-----------------------
1614  *    0x00      | BME280_STANDBY_TIME_1_MS
1615  *    0x01      | BME280_STANDBY_TIME_63_MS
1616  *    0x02      | BME280_STANDBY_TIME_125_MS
1617  *    0x03      | BME280_STANDBY_TIME_250_MS
1618  *    0x04      | BME280_STANDBY_TIME_500_MS
1619  *    0x05      | BME280_STANDBY_TIME_1000_MS
1620  *    0x06      | BME280_STANDBY_TIME_2000_MS
1621  *    0x07      | BME280_STANDBY_TIME_4000_MS
1622  *
1623  *
1624  *      @return results of bus communication function
1625  *      @retval 0 -> Success
1626  *      @retval -1 -> Error
1627  *
1628  *
1629 */
1630 BME280_RETURN_FUNCTION_TYPE bme280_get_standby_durn(u8 *v_standby_durn_u8)
1631 {
1632         /* used to return the communication result*/
1633         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1634         u8 v_data_u8 = BME280_INIT_VALUE;
1635         /* check the p_bme280 structure pointer as NULL*/
1636         if (p_bme280 == BME280_NULL) {
1637                 return E_BME280_NULL_PTR;
1638                 } else {
1639                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1640                         p_bme280->dev_addr,
1641                         BME280_CONFIG_REG_TSB__REG,
1642                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1643                         *v_standby_durn_u8 = BME280_GET_BITSLICE(
1644                         v_data_u8, BME280_CONFIG_REG_TSB);
1645                 }
1646         return com_rslt;
1647 }
1648 /*!
1649  *      @brief This API used to write the
1650  *      standby duration time from the sensor in the register 0xF5 bit 5 to 7
1651  *
1652  *      @param v_standby_durn_u8 : The value of standby duration time value.
1653  *  value       | standby duration
1654  * -------------|-----------------------
1655  *    0x00      | BME280_STANDBY_TIME_1_MS
1656  *    0x01      | BME280_STANDBY_TIME_63_MS
1657  *    0x02      | BME280_STANDBY_TIME_125_MS
1658  *    0x03      | BME280_STANDBY_TIME_250_MS
1659  *    0x04      | BME280_STANDBY_TIME_500_MS
1660  *    0x05      | BME280_STANDBY_TIME_1000_MS
1661  *    0x06      | BME280_STANDBY_TIME_2000_MS
1662  *    0x07      | BME280_STANDBY_TIME_4000_MS
1663  *
1664  *      @note Normal mode comprises an automated perpetual
1665  *      cycling between an (active)
1666  *      Measurement period and an (inactive) standby period.
1667  *      @note The standby time is determined by
1668  *      the contents of the register t_sb.
1669  *      Standby time can be set using BME280_STANDBY_TIME_125_MS.
1670  *
1671  *      @note Usage Hint : bme280_set_standby_durn(BME280_STANDBY_TIME_125_MS)
1672  *
1673  *
1674  *
1675  *      @return results of bus communication function
1676  *      @retval 0 -> Success
1677  *      @retval -1 -> Error
1678  *
1679  *
1680 */
1681 BME280_RETURN_FUNCTION_TYPE bme280_set_standby_durn(u8 v_standby_durn_u8)
1682 {
1683         /* used to return the communication result*/
1684         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1685         u8 v_data_u8 = BME280_INIT_VALUE;
1686         u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1687         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1688         u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1689         /* check the p_bme280 structure pointer as NULL*/
1690         if (p_bme280 == BME280_NULL) {
1691                 return E_BME280_NULL_PTR;
1692                 } else {
1693                         v_data_u8 = p_bme280->config_reg;
1694                         v_data_u8 =
1695                         BME280_SET_BITSLICE(v_data_u8,
1696                         BME280_CONFIG_REG_TSB, v_standby_durn_u8);
1697                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1698                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1699                                 com_rslt += bme280_set_soft_rst();
1700                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1701                                 /* write previous and updated value of
1702                                 configuration register*/
1703                                 com_rslt += bme280_write_register(
1704                                         BME280_CONFIG_REG,
1705                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1706                                 /* write previous value of
1707                                 humidity oversampling*/
1708                                 v_pre_ctrl_hum_value_u8 =
1709                                 p_bme280->ctrl_hum_reg;
1710                                 com_rslt += bme280_write_register(
1711                                         BME280_CTRL_HUMIDITY_REG,
1712                                 &v_pre_ctrl_hum_value_u8,
1713                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1714                                 /* write previous value of control
1715                                 measurement register*/
1716                                 pre_ctrl_meas_value =
1717                                 p_bme280->ctrl_meas_reg;
1718                                 com_rslt += bme280_write_register(
1719                                         BME280_CTRL_MEAS_REG,
1720                                 &pre_ctrl_meas_value,
1721                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1722                         } else {
1723                                 com_rslt =
1724                                 p_bme280->BME280_BUS_WRITE_FUNC(
1725                                 p_bme280->dev_addr,
1726                                 BME280_CONFIG_REG_TSB__REG,
1727                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1728                         }
1729                         /* read the control measurement register value*/
1730                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1731                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1732                         p_bme280->ctrl_meas_reg = v_data_u8;
1733                         /* read the control humidity register value*/
1734                         com_rslt += bme280_read_register(
1735                         BME280_CTRL_HUMIDITY_REG,
1736                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1737                         p_bme280->ctrl_hum_reg = v_data_u8;
1738                         /* read the configuration register value*/
1739                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1740                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1741                         p_bme280->config_reg = v_data_u8;
1742                 }
1743         return com_rslt;
1744 }
1745 /*
1746  * @brief Writes the working mode to the sensor
1747  *
1748  *
1749  *
1750  *
1751  *  @param v_work_mode_u8 : Mode to be set
1752  *  value    | Working mode
1753  * ----------|--------------------
1754  *   0       | BME280_ULTRALOWPOWER_MODE
1755  *   1       | BME280_LOWPOWER_MODE
1756  *   2       | BME280_STANDARDRESOLUTION_MODE
1757  *   3       | BME280_HIGHRESOLUTION_MODE
1758  *   4       | BME280_ULTRAHIGHRESOLUTION_MODE
1759  *
1760  *      @return results of bus communication function
1761  *      @retval 0 -> Success
1762  *      @retval -1 -> Error
1763  *
1764  *
1765 */
1766 /*BME280_RETURN_FUNCTION_TYPE bme280_set_work_mode(u8 v_work_mode_u8)
1767 {
1768 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1769 u8 v_data_u8 = BME280_INIT_VALUE;
1770 if (p_bme280 == BME280_NULL) {
1771         return E_BME280_NULL_PTR;
1772 } else {
1773         if (v_work_mode_u8 <= BME280_ULTRAHIGHRESOLUTION_MODE) {
1774                 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1775                         p_bme280->dev_addr,     BME280_CTRL_MEAS_REG,
1776                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1777                 if (com_rslt == SUCCESS) {
1778                         switch (v_work_mode_u8) {
1779                         case BME280_ULTRALOWPOWER_MODE:
1780                                 p_bme280->oversamp_temperature =
1781                                 BME280_ULTRALOWPOWER_OSRS_T;
1782                                 p_bme280->osrs_p =
1783                                 BME280_ULTRALOWPOWER_OSRS_P;
1784                                 break;
1785                         case BME280_LOWPOWER_MODE:
1786                                 p_bme280->oversamp_temperature =
1787                                 BME280_LOWPOWER_OSRS_T;
1788                                 p_bme280->osrs_p = BME280_LOWPOWER_OSRS_P;
1789                                 break;
1790                         case BME280_STANDARDRESOLUTION_MODE:
1791                                 p_bme280->oversamp_temperature =
1792                                 BME280_STANDARDRESOLUTION_OSRS_T;
1793                                 p_bme280->osrs_p =
1794                                 BME280_STANDARDRESOLUTION_OSRS_P;
1795                                 break;
1796                         case BME280_HIGHRESOLUTION_MODE:
1797                                 p_bme280->oversamp_temperature =
1798                                 BME280_HIGHRESOLUTION_OSRS_T;
1799                                 p_bme280->osrs_p = BME280_HIGHRESOLUTION_OSRS_P;
1800                                 break;
1801                         case BME280_ULTRAHIGHRESOLUTION_MODE:
1802                                 p_bme280->oversamp_temperature =
1803                                 BME280_ULTRAHIGHRESOLUTION_OSRS_T;
1804                                 p_bme280->osrs_p =
1805                                 BME280_ULTRAHIGHRESOLUTION_OSRS_P;
1806                                 break;
1807                         }
1808                         v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1809                                 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE,
1810                                 p_bme280->oversamp_temperature);
1811                         v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1812                                 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE,
1813                                 p_bme280->osrs_p);
1814                         com_rslt += p_bme280->BME280_BUS_WRITE_FUNC(
1815                                 p_bme280->dev_addr,     BME280_CTRL_MEAS_REG,
1816                                 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1817                 }
1818         } else {
1819                 com_rslt = E_BME280_OUT_OF_RANGE;
1820         }
1821 }
1822 return com_rslt;
1823 }*/
1824 /*!
1825  * @brief This API used to read uncompensated
1826  * temperature,pressure and humidity in forced mode
1827  *
1828  *
1829  *      @param v_uncom_pressure_s32: The value of uncompensated pressure
1830  *      @param v_uncom_temperature_s32: The value of uncompensated temperature
1831  *      @param v_uncom_humidity_s32: The value of uncompensated humidity
1832  *
1833  *
1834  *      @return results of bus communication function
1835  *      @retval 0 -> Success
1836  *      @retval -1 -> Error
1837  *
1838  *
1839 */
1840 BME280_RETURN_FUNCTION_TYPE
1841 bme280_get_forced_uncomp_pressure_temperature_humidity(
1842 s32 *v_uncom_pressure_s32,
1843 s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32)
1844 {
1845         /* used to return the communication result*/
1846         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1847         u8 v_data_u8 = BME280_INIT_VALUE;
1848         u8 v_waittime_u8 = BME280_INIT_VALUE;
1849         u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1850         u8 v_mode_u8r = BME280_INIT_VALUE;
1851         u8 pre_ctrl_config_value = BME280_INIT_VALUE;
1852         u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1853         /* check the p_bme280 structure pointer as NULL*/
1854         if (p_bme280 == BME280_NULL) {
1855                 return E_BME280_NULL_PTR;
1856                 } else {
1857                         v_mode_u8r = p_bme280->ctrl_meas_reg;
1858                         v_mode_u8r =
1859                         BME280_SET_BITSLICE(v_mode_u8r,
1860                         BME280_CTRL_MEAS_REG_POWER_MODE, BME280_FORCED_MODE);
1861                         com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1862                         if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1863                                 com_rslt += bme280_set_soft_rst();
1864                                 p_bme280->delay_msec(BME280_3MS_DELAY);
1865                                 /* write previous and updated value of
1866                                 configuration register*/
1867                                 pre_ctrl_config_value = p_bme280->config_reg;
1868                                 com_rslt += bme280_write_register(
1869                                         BME280_CONFIG_REG,
1870                                 &pre_ctrl_config_value,
1871                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1872                                 /* write previous value of
1873                                 humidity oversampling*/
1874                                 v_pre_ctrl_hum_value_u8 =
1875                                 p_bme280->ctrl_hum_reg;
1876                                 com_rslt += bme280_write_register(
1877                                         BME280_CTRL_HUMIDITY_REG,
1878                                 &v_pre_ctrl_hum_value_u8,
1879                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1880                                 /* write the force mode  */
1881                                 com_rslt += bme280_write_register(
1882                                         BME280_CTRL_MEAS_REG,
1883                                 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1884                         } else {
1885                                 /* write previous value of
1886                                 humidity oversampling*/
1887                                 v_pre_ctrl_hum_value_u8 =
1888                                 p_bme280->ctrl_hum_reg;
1889                                 com_rslt += bme280_write_register(
1890                                         BME280_CTRL_HUMIDITY_REG,
1891                                 &v_pre_ctrl_hum_value_u8,
1892                                 BME280_GEN_READ_WRITE_DATA_LENGTH);
1893                                 /* write the force mode  */
1894                                 com_rslt += bme280_write_register(
1895                                         BME280_CTRL_MEAS_REG,
1896                                 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1897                         }
1898                         bme280_compute_wait_time(&v_waittime_u8);
1899                         p_bme280->delay_msec(v_waittime_u8);
1900                         /* read the force-mode value of pressure
1901                         temperature and humidity*/
1902                         com_rslt +=
1903                         bme280_read_uncomp_pressure_temperature_humidity(
1904                         v_uncom_pressure_s32, v_uncom_temperature_s32,
1905                         v_uncom_humidity_s32);
1906
1907                         /* read the control humidity register value*/
1908                         com_rslt += bme280_read_register(
1909                         BME280_CTRL_HUMIDITY_REG,
1910                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1911                         p_bme280->ctrl_hum_reg = v_data_u8;
1912                         /* read the configuration register value*/
1913                         com_rslt += bme280_read_register(BME280_CONFIG_REG,
1914                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1915                         p_bme280->config_reg = v_data_u8;
1916
1917                         /* read the control measurement register value*/
1918                         com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1919                         &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1920                         p_bme280->ctrl_meas_reg = v_data_u8;
1921                 }
1922         return com_rslt;
1923 }
1924 /*!
1925  * @brief
1926  *      This API write the data to
1927  *      the given register
1928  *
1929  *
1930  *      @param v_addr_u8 -> Address of the register
1931  *      @param v_data_u8 -> The data from the register
1932  *      @param v_len_u8 -> no of bytes to read
1933  *
1934  *
1935  *      @return results of bus communication function
1936  *      @retval 0 -> Success
1937  *      @retval -1 -> Error
1938  *
1939  *
1940  */
1941 BME280_RETURN_FUNCTION_TYPE bme280_write_register(u8 v_addr_u8,
1942 u8 *v_data_u8, u8 v_len_u8)
1943 {
1944         /* used to return the communication result*/
1945         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1946         /* check the p_bme280 structure pointer as NULL*/
1947         if (p_bme280 == BME280_NULL) {
1948                 return E_BME280_NULL_PTR;
1949                 } else {
1950                         com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1951                         p_bme280->dev_addr,
1952                         v_addr_u8, v_data_u8, v_len_u8);
1953                 }
1954         return com_rslt;
1955 }
1956 /*!
1957  * @brief
1958  *      This API reads the data from
1959  *      the given register
1960  *
1961  *
1962  *      @param v_addr_u8 -> Address of the register
1963  *      @param v_data_u8 -> The data from the register
1964  *      @param v_len_u8 -> no of bytes to read
1965  *
1966  *
1967  *      @return results of bus communication function
1968  *      @retval 0 -> Success
1969  *      @retval -1 -> Error
1970  *
1971  *
1972  */
1973 BME280_RETURN_FUNCTION_TYPE bme280_read_register(u8 v_addr_u8,
1974 u8 *v_data_u8, u8 v_len_u8)
1975 {
1976         /* used to return the communication result*/
1977         BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1978         /* check the p_bme280 structure pointer as NULL*/
1979         if (p_bme280 == BME280_NULL) {
1980                 return E_BME280_NULL_PTR;
1981                 } else {
1982                         com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1983                         p_bme280->dev_addr,
1984                         v_addr_u8, v_data_u8, v_len_u8);
1985                 }
1986         return com_rslt;
1987 }
1988 #ifdef BME280_ENABLE_FLOAT
1989 /*!
1990  * @brief Reads actual temperature from uncompensated temperature
1991  * @note returns the value in Degree centigrade
1992  * @note Output value of "51.23" equals 51.23 DegC.
1993  *
1994  *
1995  *
1996  *  @param v_uncom_temperature_s32 : value of uncompensated temperature
1997  *
1998  *
1999  *
2000  *  @return  Return the actual temperature in floating point
2001  *
2002 */
2003 double bme280_compensate_temperature_double(s32 v_uncom_temperature_s32)
2004 {
2005         double v_x1_u32 = BME280_INIT_VALUE;
2006         double v_x2_u32 = BME280_INIT_VALUE;
2007         double temperature = BME280_INIT_VALUE;
2008
2009         v_x1_u32  = (((double)v_uncom_temperature_s32) / 16384.0 -
2010         ((double)p_bme280->cal_param.dig_T1) / 1024.0) *
2011         ((double)p_bme280->cal_param.dig_T2);
2012         v_x2_u32  = ((((double)v_uncom_temperature_s32) / 131072.0 -
2013         ((double)p_bme280->cal_param.dig_T1) / 8192.0) *
2014         (((double)v_uncom_temperature_s32) / 131072.0 -
2015         ((double)p_bme280->cal_param.dig_T1) / 8192.0)) *
2016         ((double)p_bme280->cal_param.dig_T3);
2017         p_bme280->cal_param.t_fine = (s32)(v_x1_u32 + v_x2_u32);
2018         temperature  = (v_x1_u32 + v_x2_u32) / 5120.0;
2019
2020
2021         return temperature;
2022 }
2023 /*!
2024  * @brief Reads actual pressure from uncompensated pressure
2025  * @note Returns pressure in Pa as double.
2026  * @note Output value of "96386.2"
2027  * equals 96386.2 Pa = 963.862 hPa.
2028  *
2029  *
2030  *  @param v_uncom_pressure_s32 : value of uncompensated pressure
2031  *
2032  *
2033  *  @return  Return the actual pressure in floating point
2034  *
2035 */
2036 double bme280_compensate_pressure_double(s32 v_uncom_pressure_s32)
2037 {
2038         double v_x1_u32 = BME280_INIT_VALUE;
2039         double v_x2_u32 = BME280_INIT_VALUE;
2040         double pressure = BME280_INIT_VALUE;
2041
2042         v_x1_u32 = ((double)p_bme280->cal_param.t_fine /
2043         2.0) - 64000.0;
2044         v_x2_u32 = v_x1_u32 * v_x1_u32 *
2045         ((double)p_bme280->cal_param.dig_P6) / 32768.0;
2046         v_x2_u32 = v_x2_u32 + v_x1_u32 *
2047         ((double)p_bme280->cal_param.dig_P5) * 2.0;
2048         v_x2_u32 = (v_x2_u32 / 4.0) +
2049         (((double)p_bme280->cal_param.dig_P4) * 65536.0);
2050         v_x1_u32 = (((double)p_bme280->cal_param.dig_P3) *
2051         v_x1_u32 * v_x1_u32 / 524288.0 +
2052         ((double)p_bme280->cal_param.dig_P2) * v_x1_u32) / 524288.0;
2053         v_x1_u32 = (1.0 + v_x1_u32 / 32768.0) *
2054         ((double)p_bme280->cal_param.dig_P1);
2055         pressure = 1048576.0 - (double)v_uncom_pressure_s32;
2056         /* Avoid exception caused by division by zero */
2057         if ((v_x1_u32 > 0) || (v_x1_u32 < 0))
2058                 pressure = (pressure - (v_x2_u32 / 4096.0)) * 6250.0 / v_x1_u32;
2059         else
2060                 return BME280_INVALID_DATA;
2061         v_x1_u32 = ((double)p_bme280->cal_param.dig_P9) *
2062         pressure * pressure / 2147483648.0;
2063         v_x2_u32 = pressure * ((double)p_bme280->cal_param.dig_P8) / 32768.0;
2064         pressure = pressure + (v_x1_u32 + v_x2_u32 +
2065         ((double)p_bme280->cal_param.dig_P7)) / 16.0;
2066
2067         return pressure;
2068 }
2069 /*!
2070  * @brief Reads actual humidity from uncompensated humidity
2071  * @note returns the value in relative humidity (%rH)
2072  * @note Output value of "42.12" equals 42.12 %rH
2073  *
2074  *  @param v_uncom_humidity_s32 : value of uncompensated humidity
2075  *
2076  *
2077  *
2078  *  @return Return the actual humidity in floating point
2079  *
2080 */
2081 double bme280_compensate_humidity_double(s32 v_uncom_humidity_s32)
2082 {
2083         double var_h = BME280_INIT_VALUE;
2084
2085         var_h = (((double)p_bme280->cal_param.t_fine) - 76800.0);
2086         if ((var_h > 0) || (var_h < 0))
2087                 var_h = (v_uncom_humidity_s32 -
2088                 (((double)p_bme280->cal_param.dig_H4) * 64.0 +
2089                 ((double)p_bme280->cal_param.dig_H5) / 16384.0 * var_h))*
2090                 (((double)p_bme280->cal_param.dig_H2) / 65536.0 *
2091                 (1.0 + ((double) p_bme280->cal_param.dig_H6)
2092                 / 67108864.0 * var_h * (1.0 + ((double)
2093                 p_bme280->cal_param.dig_H3) / 67108864.0 * var_h)));
2094         else
2095                 return BME280_INVALID_DATA;
2096         var_h = var_h * (1.0 - ((double)
2097         p_bme280->cal_param.dig_H1)*var_h / 524288.0);
2098         if (var_h > 100.0)
2099                 var_h = 100.0;
2100         else if (var_h < 0.0)
2101                 var_h = 0.0;
2102         return var_h;
2103
2104 }
2105 #endif
2106 #if defined(BME280_ENABLE_INT64) && defined(BME280_64BITSUPPORT_PRESENT)
2107 /*!
2108  * @brief Reads actual pressure from uncompensated pressure
2109  * @note Returns the value in Pa as unsigned 32 bit
2110  * integer in Q24.8 format (24 integer bits and
2111  * 8 fractional bits).
2112  * @note Output value of "24674867"
2113  * represents 24674867 / 256 = 96386.2 Pa = 963.862 hPa
2114  *
2115  *
2116  *
2117  *  @param  v_uncom_pressure_s32 : value of uncompensated temperature
2118  *
2119  *
2120  *  @return Return the actual pressure in u32
2121  *
2122 */
2123 u32 bme280_compensate_pressure_int64(s32 v_uncom_pressure_s32)
2124 {
2125         s64 v_x1_s64r = BME280_INIT_VALUE;
2126         s64 v_x2_s64r = BME280_INIT_VALUE;
2127         s64 pressure = BME280_INIT_VALUE;
2128
2129         v_x1_s64r = ((s64)p_bme280->cal_param.t_fine)
2130         - 128000;
2131         v_x2_s64r = v_x1_s64r * v_x1_s64r *
2132         (s64)p_bme280->cal_param.dig_P6;
2133         v_x2_s64r = v_x2_s64r + ((v_x1_s64r *
2134         (s64)p_bme280->cal_param.dig_P5)
2135         << BME280_SHIFT_BIT_POSITION_BY_17_BITS);
2136         v_x2_s64r = v_x2_s64r +
2137         (((s64)p_bme280->cal_param.dig_P4)
2138         << BME280_SHIFT_BIT_POSITION_BY_35_BITS);
2139         v_x1_s64r = ((v_x1_s64r * v_x1_s64r *
2140         (s64)p_bme280->cal_param.dig_P3)
2141         >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2142         ((v_x1_s64r * (s64)p_bme280->cal_param.dig_P2)
2143         << BME280_SHIFT_BIT_POSITION_BY_12_BITS);
2144         v_x1_s64r = (((((s64)1)
2145         << BME280_SHIFT_BIT_POSITION_BY_47_BITS) + v_x1_s64r)) *
2146         ((s64)p_bme280->cal_param.dig_P1)
2147         >> BME280_SHIFT_BIT_POSITION_BY_33_BITS;
2148         pressure = 1048576 - v_uncom_pressure_s32;
2149         /* Avoid exception caused by division by zero */
2150         if (v_x1_s64r != BME280_INIT_VALUE)
2151                 #if defined __KERNEL__
2152                         pressure = div64_s64((((pressure
2153                         << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2154                         * 3125), v_x1_s64r);
2155                 #else
2156                         pressure = (((pressure
2157                         << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2158                         * 3125) / v_x1_s64r;
2159                 #endif
2160         else
2161                 return BME280_INVALID_DATA;
2162         v_x1_s64r = (((s64)p_bme280->cal_param.dig_P9) *
2163         (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS) *
2164         (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS))
2165         >> BME280_SHIFT_BIT_POSITION_BY_25_BITS;
2166         v_x2_s64r = (((s64)p_bme280->cal_param.dig_P8) *
2167         pressure) >> BME280_SHIFT_BIT_POSITION_BY_19_BITS;
2168         pressure = (((pressure + v_x1_s64r +
2169         v_x2_s64r) >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2170         (((s64)p_bme280->cal_param.dig_P7)
2171         << BME280_SHIFT_BIT_POSITION_BY_04_BITS));
2172
2173         return (u32)pressure;
2174 }
2175 /*!
2176  * @brief Reads actual pressure from uncompensated pressure
2177  * @note Returns the value in Pa.
2178  * @note Output value of "12337434"
2179  * @note represents 12337434 / 128 = 96386.2 Pa = 963.862 hPa
2180  *
2181  *
2182  *
2183  *  @param v_uncom_pressure_s32 : value of uncompensated pressure
2184  *
2185  *
2186  *  @return the actual pressure in u32
2187  *
2188 */
2189 u32 bme280_compensate_pressure_int64_twentyfour_bit_output(
2190 s32 v_uncom_pressure_s32)
2191 {
2192         u32 pressure = BME280_INIT_VALUE;
2193
2194         pressure = bme280_compensate_pressure_int64(
2195         v_uncom_pressure_s32);
2196         pressure = (u32)(pressure >> BME280_SHIFT_BIT_POSITION_BY_01_BIT);
2197         return pressure;
2198 }
2199 #endif
2200 /*!
2201  * @brief Computing waiting time for sensor data read
2202  *
2203  *
2204  *
2205  *
2206  *  @param v_delaytime_u8 : The value of delay time for force mode
2207  *
2208  *
2209  *      @retval 0 -> Success
2210  *
2211  *
2212  */
2213 BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8
2214 *v_delaytime_u8)
2215 {
2216         /* used to return the communication result*/
2217         BME280_RETURN_FUNCTION_TYPE com_rslt = SUCCESS;
2218
2219         *v_delaytime_u8 = (T_INIT_MAX +
2220         T_MEASURE_PER_OSRS_MAX *
2221         (((1 <<
2222         p_bme280->oversamp_temperature)
2223         >> BME280_SHIFT_BIT_POSITION_BY_01_BIT)
2224         + ((1 << p_bme280->oversamp_pressure)
2225         >> BME280_SHIFT_BIT_POSITION_BY_01_BIT) +
2226         ((1 << p_bme280->oversamp_humidity)
2227         >> BME280_SHIFT_BIT_POSITION_BY_01_BIT))
2228         + ((p_bme280->oversamp_pressure > 0) ?
2229         T_SETUP_PRESSURE_MAX : 0) +
2230         ((p_bme280->oversamp_humidity > 0) ?
2231         T_SETUP_HUMIDITY_MAX : 0) + 15) / 16;
2232         return com_rslt;
2233 }