]> git.itanic.dy.fi Git - BME280_driver/blob - bme280_defs.h
Hide excess debug messages by default
[BME280_driver] / bme280_defs.h
1 /**\r
2 * Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.\r
3 *\r
4 * BSD-3-Clause\r
5 *\r
6 * Redistribution and use in source and binary forms, with or without\r
7 * modification, are permitted provided that the following conditions are met:\r
8 *\r
9 * 1. Redistributions of source code must retain the above copyright\r
10 *    notice, this list of conditions and the following disclaimer.\r
11 *\r
12 * 2. Redistributions in binary form must reproduce the above copyright\r
13 *    notice, this list of conditions and the following disclaimer in the\r
14 *    documentation and/or other materials provided with the distribution.\r
15 *\r
16 * 3. Neither the name of the copyright holder nor the names of its\r
17 *    contributors may be used to endorse or promote products derived from\r
18 *    this software without specific prior written permission.\r
19 *\r
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\r
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
31 * POSSIBILITY OF SUCH DAMAGE.\r
32 *\r
33 * @file       bme280_defs.h\r
34 * @date       2020-03-28\r
35 * @version    v3.5.0\r
36 *\r
37 */\r
38 \r
39 #ifndef BME280_DEFS_H_\r
40 #define BME280_DEFS_H_\r
41 \r
42 /********************************************************/\r
43 /* header includes */\r
44 #ifdef __KERNEL__\r
45 #include <linux/types.h>\r
46 #include <linux/kernel.h>\r
47 #else\r
48 #include <stdint.h>\r
49 #include <stddef.h>\r
50 #endif\r
51 \r
52 /********************************************************/\r
53 /*! @name       Common macros               */\r
54 /********************************************************/\r
55 \r
56 #if !defined(UINT8_C) && !defined(INT8_C)\r
57 #define INT8_C(x)    S8_C(x)\r
58 #define UINT8_C(x)   U8_C(x)\r
59 #endif\r
60 \r
61 #if !defined(UINT16_C) && !defined(INT16_C)\r
62 #define INT16_C(x)   S16_C(x)\r
63 #define UINT16_C(x)  U16_C(x)\r
64 #endif\r
65 \r
66 #if !defined(INT32_C) && !defined(UINT32_C)\r
67 #define INT32_C(x)   S32_C(x)\r
68 #define UINT32_C(x)  U32_C(x)\r
69 #endif\r
70 \r
71 #if !defined(INT64_C) && !defined(UINT64_C)\r
72 #define INT64_C(x)   S64_C(x)\r
73 #define UINT64_C(x)  U64_C(x)\r
74 #endif\r
75 \r
76 /**@}*/\r
77 /**\name C standard macros */\r
78 #ifndef NULL\r
79 #ifdef __cplusplus\r
80 #define NULL         0\r
81 #else\r
82 #define NULL         ((void *) 0)\r
83 #endif\r
84 #endif\r
85 \r
86 /********************************************************/\r
87 \r
88 #ifndef BME280_64BIT_ENABLE /*< Check if 64-bit integer (using BME280_64BIT_ENABLE) is enabled */\r
89 #ifndef BME280_32BIT_ENABLE /*< Check if 32-bit integer (using BME280_32BIT_ENABLE) is enabled */\r
90 #ifndef BME280_FLOAT_ENABLE /*< If any of the integer data types not enabled then enable BME280_FLOAT_ENABLE */\r
91 #define BME280_FLOAT_ENABLE\r
92 #endif\r
93 #endif\r
94 #endif\r
95 \r
96 #ifndef TRUE\r
97 #define TRUE                                      UINT8_C(1)\r
98 #endif\r
99 #ifndef FALSE\r
100 #define FALSE                                     UINT8_C(0)\r
101 #endif\r
102 \r
103 /**\r
104  * BME280_INTF_RET_TYPE is the read/write interface return type which can be overwritten by the build system.\r
105  */\r
106 #ifndef BME280_INTF_RET_TYPE\r
107 #define BME280_INTF_RET_TYPE                      int8_t\r
108 #endif\r
109 \r
110 /**\r
111  * The last error code from read/write interface is stored in the device structure as intf_rslt.\r
112  */\r
113 #ifndef BME280_INTF_RET_SUCCESS\r
114 #define BME280_INTF_RET_SUCCESS                   INT8_C(0)\r
115 #endif\r
116 \r
117 /**\name I2C addresses */\r
118 #define BME280_I2C_ADDR_PRIM                      UINT8_C(0x76)\r
119 #define BME280_I2C_ADDR_SEC                       UINT8_C(0x77)\r
120 \r
121 /**\name BME280 chip identifier */\r
122 #define BME280_CHIP_ID                            UINT8_C(0x60)\r
123 \r
124 /**\name Register Address */\r
125 #define BME280_CHIP_ID_ADDR                       UINT8_C(0xD0)\r
126 #define BME280_RESET_ADDR                         UINT8_C(0xE0)\r
127 #define BME280_TEMP_PRESS_CALIB_DATA_ADDR         UINT8_C(0x88)\r
128 #define BME280_HUMIDITY_CALIB_DATA_ADDR           UINT8_C(0xE1)\r
129 #define BME280_PWR_CTRL_ADDR                      UINT8_C(0xF4)\r
130 #define BME280_CTRL_HUM_ADDR                      UINT8_C(0xF2)\r
131 #define BME280_CTRL_MEAS_ADDR                     UINT8_C(0xF4)\r
132 #define BME280_CONFIG_ADDR                        UINT8_C(0xF5)\r
133 #define BME280_DATA_ADDR                          UINT8_C(0xF7)\r
134 \r
135 /**\name API success code */\r
136 #define BME280_OK                                 INT8_C(0)\r
137 \r
138 /**\name API error codes */\r
139 #define BME280_E_NULL_PTR                         INT8_C(-1)\r
140 #define BME280_E_DEV_NOT_FOUND                    INT8_C(-2)\r
141 #define BME280_E_INVALID_LEN                      INT8_C(-3)\r
142 #define BME280_E_COMM_FAIL                        INT8_C(-4)\r
143 #define BME280_E_SLEEP_MODE_FAIL                  INT8_C(-5)\r
144 #define BME280_E_NVM_COPY_FAILED                  INT8_C(-6)\r
145 \r
146 /**\name API warning codes */\r
147 #define BME280_W_INVALID_OSR_MACRO                INT8_C(1)\r
148 \r
149 /**\name Macros related to size */\r
150 #define BME280_TEMP_PRESS_CALIB_DATA_LEN          UINT8_C(26)\r
151 #define BME280_HUMIDITY_CALIB_DATA_LEN            UINT8_C(7)\r
152 #define BME280_P_T_H_DATA_LEN                     UINT8_C(8)\r
153 \r
154 /**\name Sensor power modes */\r
155 #define BME280_SLEEP_MODE                         UINT8_C(0x00)\r
156 #define BME280_FORCED_MODE                        UINT8_C(0x01)\r
157 #define BME280_NORMAL_MODE                        UINT8_C(0x03)\r
158 \r
159 /**\name Macro to combine two 8 bit data's to form a 16 bit data */\r
160 #define BME280_CONCAT_BYTES(msb, lsb)             (((uint16_t)msb << 8) | (uint16_t)lsb)\r
161 \r
162 #define BME280_SET_BITS(reg_data, bitname, data) \\r
163     ((reg_data & ~(bitname##_MSK)) | \\r
164      ((data << bitname##_POS) & bitname##_MSK))\r
165 #define BME280_SET_BITS_POS_0(reg_data, bitname, data) \\r
166     ((reg_data & ~(bitname##_MSK)) | \\r
167      (data & bitname##_MSK))\r
168 \r
169 #define BME280_GET_BITS(reg_data, bitname)        ((reg_data & (bitname##_MSK)) >> \\r
170                                                    (bitname##_POS))\r
171 #define BME280_GET_BITS_POS_0(reg_data, bitname)  (reg_data & (bitname##_MSK))\r
172 \r
173 /**\name Macros for bit masking */\r
174 #define BME280_SENSOR_MODE_MSK                    UINT8_C(0x03)\r
175 #define BME280_SENSOR_MODE_POS                    UINT8_C(0x00)\r
176 \r
177 #define BME280_CTRL_HUM_MSK                       UINT8_C(0x07)\r
178 #define BME280_CTRL_HUM_POS                       UINT8_C(0x00)\r
179 \r
180 #define BME280_CTRL_PRESS_MSK                     UINT8_C(0x1C)\r
181 #define BME280_CTRL_PRESS_POS                     UINT8_C(0x02)\r
182 \r
183 #define BME280_CTRL_TEMP_MSK                      UINT8_C(0xE0)\r
184 #define BME280_CTRL_TEMP_POS                      UINT8_C(0x05)\r
185 \r
186 #define BME280_FILTER_MSK                         UINT8_C(0x1C)\r
187 #define BME280_FILTER_POS                         UINT8_C(0x02)\r
188 \r
189 #define BME280_STANDBY_MSK                        UINT8_C(0xE0)\r
190 #define BME280_STANDBY_POS                        UINT8_C(0x05)\r
191 \r
192 /**\name Sensor component selection macros\r
193  * These values are internal for API implementation. Don't relate this to\r
194  * data sheet.\r
195  */\r
196 #define BME280_PRESS                              UINT8_C(1)\r
197 #define BME280_TEMP                               UINT8_C(1 << 1)\r
198 #define BME280_HUM                                UINT8_C(1 << 2)\r
199 #define BME280_ALL                                UINT8_C(0x07)\r
200 \r
201 /**\name Settings selection macros */\r
202 #define BME280_OSR_PRESS_SEL                      UINT8_C(1)\r
203 #define BME280_OSR_TEMP_SEL                       UINT8_C(1 << 1)\r
204 #define BME280_OSR_HUM_SEL                        UINT8_C(1 << 2)\r
205 #define BME280_FILTER_SEL                         UINT8_C(1 << 3)\r
206 #define BME280_STANDBY_SEL                        UINT8_C(1 << 4)\r
207 #define BME280_ALL_SETTINGS_SEL                   UINT8_C(0x1F)\r
208 \r
209 /**\name Oversampling macros */\r
210 #define BME280_NO_OVERSAMPLING                    UINT8_C(0x00)\r
211 #define BME280_OVERSAMPLING_1X                    UINT8_C(0x01)\r
212 #define BME280_OVERSAMPLING_2X                    UINT8_C(0x02)\r
213 #define BME280_OVERSAMPLING_4X                    UINT8_C(0x03)\r
214 #define BME280_OVERSAMPLING_8X                    UINT8_C(0x04)\r
215 #define BME280_OVERSAMPLING_16X                   UINT8_C(0x05)\r
216 \r
217 /**\name Measurement delay calculation macros  */\r
218 #define BME280_MEAS_OFFSET                        UINT16_C(1250)\r
219 #define BME280_MEAS_DUR                           UINT16_C(2300)\r
220 #define BME280_PRES_HUM_MEAS_OFFSET               UINT16_C(575)\r
221 #define BME280_MEAS_SCALING_FACTOR                UINT16_C(1000)\r
222 \r
223 /**\name Standby duration selection macros */\r
224 #define BME280_STANDBY_TIME_0_5_MS                (0x00)\r
225 #define BME280_STANDBY_TIME_62_5_MS               (0x01)\r
226 #define BME280_STANDBY_TIME_125_MS                (0x02)\r
227 #define BME280_STANDBY_TIME_250_MS                (0x03)\r
228 #define BME280_STANDBY_TIME_500_MS                (0x04)\r
229 #define BME280_STANDBY_TIME_1000_MS               (0x05)\r
230 #define BME280_STANDBY_TIME_10_MS                 (0x06)\r
231 #define BME280_STANDBY_TIME_20_MS                 (0x07)\r
232 \r
233 /**\name Filter coefficient selection macros */\r
234 #define BME280_FILTER_COEFF_OFF                   (0x00)\r
235 #define BME280_FILTER_COEFF_2                     (0x01)\r
236 #define BME280_FILTER_COEFF_4                     (0x02)\r
237 #define BME280_FILTER_COEFF_8                     (0x03)\r
238 #define BME280_FILTER_COEFF_16                    (0x04)\r
239 \r
240 #define BME280_STATUS_REG_ADDR                    (0xF3)\r
241 #define BME280_SOFT_RESET_COMMAND                 (0xB6)\r
242 #define BME280_STATUS_IM_UPDATE                   (0x01)\r
243 \r
244 /*!\r
245  * @brief Interface selection Enums\r
246  */\r
247 enum bme280_intf {\r
248     /*< SPI interface */\r
249     BME280_SPI_INTF,\r
250     /*< I2C interface */\r
251     BME280_I2C_INTF\r
252 };\r
253 \r
254 /*!\r
255  * @brief Type definitions\r
256  */\r
257 \r
258 /*!\r
259  * @brief Bus communication function pointer which should be mapped to\r
260  * the platform specific read functions of the user\r
261  *\r
262  * @param[in] reg_addr       : Register address from which data is read.\r
263  * @param[out] reg_data     : Pointer to data buffer where read data is stored.\r
264  * @param[in] len            : Number of bytes of data to be read.\r
265  * @param[in, out] intf_ptr  : Void pointer that can enable the linking of descriptors\r
266  *                                  for interface related call backs.\r
267  *\r
268  * @retval   0 -> Success.\r
269  * @retval Non zero value -> Fail.\r
270  *\r
271  */\r
272 typedef BME280_INTF_RET_TYPE (*bme280_read_fptr_t)(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr);\r
273 \r
274 /*!\r
275  * @brief Bus communication function pointer which should be mapped to\r
276  * the platform specific write functions of the user\r
277  *\r
278  * @param[in] reg_addr      : Register address to which the data is written.\r
279  * @param[in] reg_data     : Pointer to data buffer in which data to be written\r
280  *                            is stored.\r
281  * @param[in] len           : Number of bytes of data to be written.\r
282  * @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors\r
283  *                            for interface related call backs\r
284  *\r
285  * @retval   0   -> Success.\r
286  * @retval Non zero value -> Fail.\r
287  *\r
288  */\r
289 typedef BME280_INTF_RET_TYPE (*bme280_write_fptr_t)(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len,\r
290                                                     void *intf_ptr);\r
291 \r
292 /*!\r
293  * @brief Delay function pointer which should be mapped to\r
294  * delay function of the user\r
295  *\r
296  * @param[in] period              : Delay in microseconds.\r
297  * @param[in, out] intf_ptr       : Void pointer that can enable the linking of descriptors\r
298  *                                  for interface related call backs\r
299  *\r
300  */\r
301 typedef void (*bme280_delay_us_fptr_t)(uint32_t period, void *intf_ptr);\r
302 \r
303 /*!\r
304  * @brief Calibration data\r
305  */\r
306 struct bme280_calib_data\r
307 {\r
308     /*< Calibration coefficient for the temperature sensor */\r
309     uint16_t dig_t1;\r
310 \r
311     /*< Calibration coefficient for the temperature sensor */\r
312     int16_t dig_t2;\r
313 \r
314     /*< Calibration coefficient for the temperature sensor */\r
315     int16_t dig_t3;\r
316 \r
317     /*< Calibration coefficient for the pressure sensor */\r
318     uint16_t dig_p1;\r
319 \r
320     /*< Calibration coefficient for the pressure sensor */\r
321     int16_t dig_p2;\r
322 \r
323     /*< Calibration coefficient for the pressure sensor */\r
324     int16_t dig_p3;\r
325 \r
326     /*< Calibration coefficient for the pressure sensor */\r
327     int16_t dig_p4;\r
328 \r
329     /*< Calibration coefficient for the pressure sensor */\r
330     int16_t dig_p5;\r
331 \r
332     /*< Calibration coefficient for the pressure sensor */\r
333     int16_t dig_p6;\r
334 \r
335     /*< Calibration coefficient for the pressure sensor */\r
336     int16_t dig_p7;\r
337 \r
338     /*< Calibration coefficient for the pressure sensor */\r
339     int16_t dig_p8;\r
340 \r
341     /*< Calibration coefficient for the pressure sensor */\r
342     int16_t dig_p9;\r
343 \r
344     /*< Calibration coefficient for the humidity sensor */\r
345     uint8_t dig_h1;\r
346 \r
347     /*< Calibration coefficient for the humidity sensor */\r
348     int16_t dig_h2;\r
349 \r
350     /*< Calibration coefficient for the humidity sensor */\r
351     uint8_t dig_h3;\r
352 \r
353     /*< Calibration coefficient for the humidity sensor */\r
354     int16_t dig_h4;\r
355 \r
356     /*< Calibration coefficient for the humidity sensor */\r
357     int16_t dig_h5;\r
358 \r
359     /*< Calibration coefficient for the humidity sensor */\r
360     int8_t dig_h6;\r
361 \r
362     /*< Variable to store the intermediate temperature coefficient */\r
363     int32_t t_fine;\r
364 };\r
365 \r
366 /*!\r
367  * @brief bme280 sensor structure which comprises of temperature, pressure and\r
368  * humidity data\r
369  */\r
370 #ifdef BME280_FLOAT_ENABLE\r
371 struct bme280_data\r
372 {\r
373     /*< Compensated pressure */\r
374     double pressure;\r
375 \r
376     /*< Compensated temperature */\r
377     double temperature;\r
378 \r
379     /*< Compensated humidity */\r
380     double humidity;\r
381 };\r
382 #else\r
383 struct bme280_data\r
384 {\r
385     /*< Compensated pressure */\r
386     uint32_t pressure;\r
387 \r
388     /*< Compensated temperature */\r
389     int32_t temperature;\r
390 \r
391     /*< Compensated humidity */\r
392     uint32_t humidity;\r
393 };\r
394 #endif /*! BME280_USE_FLOATING_POINT */\r
395 \r
396 /*!\r
397  * @brief bme280 sensor structure which comprises of uncompensated temperature,\r
398  * pressure and humidity data\r
399  */\r
400 struct bme280_uncomp_data\r
401 {\r
402     /*< un-compensated pressure */\r
403     uint32_t pressure;\r
404 \r
405     /*< un-compensated temperature */\r
406     uint32_t temperature;\r
407 \r
408     /*< un-compensated humidity */\r
409     uint32_t humidity;\r
410 };\r
411 \r
412 /*!\r
413  * @brief bme280 sensor settings structure which comprises of mode,\r
414  * oversampling and filter settings.\r
415  */\r
416 struct bme280_settings\r
417 {\r
418     /*< pressure oversampling */\r
419     uint8_t osr_p;\r
420 \r
421     /*< temperature oversampling */\r
422     uint8_t osr_t;\r
423 \r
424     /*< humidity oversampling */\r
425     uint8_t osr_h;\r
426 \r
427     /*< filter coefficient */\r
428     uint8_t filter;\r
429 \r
430     /*< standby time */\r
431     uint8_t standby_time;\r
432 };\r
433 \r
434 /*!\r
435  * @brief bme280 device structure\r
436  */\r
437 struct bme280_dev\r
438 {\r
439     /*< Chip Id */\r
440     uint8_t chip_id;\r
441 \r
442     /*< Interface function pointer used to enable the device address for I2C and chip selection for SPI */\r
443     void *intf_ptr;\r
444 \r
445     /*< Interface Selection\r
446      * For SPI, intf = BME280_SPI_INTF\r
447      * For I2C, intf = BME280_I2C_INTF\r
448      * */\r
449     enum bme280_intf intf;\r
450 \r
451     /*< Read function pointer */\r
452     bme280_read_fptr_t read;\r
453 \r
454     /*< Write function pointer */\r
455     bme280_write_fptr_t write;\r
456 \r
457     /*< Delay function pointer */\r
458     bme280_delay_us_fptr_t delay_us;\r
459 \r
460     /*< Trim data */\r
461     struct bme280_calib_data calib_data;\r
462 \r
463     /*< Sensor settings */\r
464     struct bme280_settings settings;\r
465 \r
466     /*< Variable to store result of read/write function */\r
467     BME280_INTF_RET_TYPE intf_rslt;\r
468 };\r
469 \r
470 #endif /* BME280_DEFS_H_ */\r