]> git.itanic.dy.fi Git - BME280_driver/blob - bme280_defs.h
Merge pull request #77 from kni/master
[BME280_driver] / bme280_defs.h
1 /**\r
2  * Copyright (C) 2018 - 2019 Bosch Sensortec GmbH\r
3  *\r
4  * Redistribution and use in source and binary forms, with or without\r
5  * modification, are permitted provided that the following conditions are met:\r
6  *\r
7  * Redistributions of source code must retain the above copyright\r
8  * notice, this list of conditions and the following disclaimer.\r
9  *\r
10  * Redistributions in binary form must reproduce the above copyright\r
11  * notice, this list of conditions and the following disclaimer in the\r
12  * documentation and/or other materials provided with the distribution.\r
13  *\r
14  * Neither the name of the copyright holder nor the names of the\r
15  * contributors may be used to endorse or promote products derived from\r
16  * this software without specific prior written permission.\r
17  *\r
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\r
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR\r
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
22  * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER\r
23  * OR CONTRIBUTORS BE LIABLE FOR ANY\r
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,\r
25  * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,\r
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
31  * ANY WAY OUT OF THE USE OF THIS\r
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\r
33  *\r
34  * The information provided is believed to be accurate and reliable.\r
35  * The copyright holder assumes no responsibility\r
36  * for the consequences of use\r
37  * of such information nor for any infringement of patents or\r
38  * other rights of third parties which may result from its use.\r
39  * No license is granted by implication or otherwise under any patent or\r
40  * patent rights of the copyright holder.\r
41  *\r
42  * @file    bme280_defs.h\r
43  * @date    26 Aug 2019\r
44  * @version 3.3.7\r
45  * @brief\r
46  *\r
47  */\r
48 \r
49 /*! @file bme280_defs.h\r
50  * @brief Sensor driver for BME280 sensor\r
51  */\r
52 \r
53 /*!\r
54  * @defgroup BME280 SENSOR API\r
55  * @brief\r
56  */\r
57 #ifndef BME280_DEFS_H_\r
58 #define BME280_DEFS_H_\r
59 \r
60 /********************************************************/\r
61 /* header includes */\r
62 #ifdef __KERNEL__\r
63 #include <linux/types.h>\r
64 #include <linux/kernel.h>\r
65 #else\r
66 #include <stdint.h>\r
67 #include <stddef.h>\r
68 #endif\r
69 \r
70 /********************************************************/\r
71 /*! @name       Common macros               */\r
72 /********************************************************/\r
73 \r
74 #if !defined(UINT8_C) && !defined(INT8_C)\r
75 #define INT8_C(x)   S8_C(x)\r
76 #define UINT8_C(x)  U8_C(x)\r
77 #endif\r
78 \r
79 #if !defined(UINT16_C) && !defined(INT16_C)\r
80 #define INT16_C(x)  S16_C(x)\r
81 #define UINT16_C(x) U16_C(x)\r
82 #endif\r
83 \r
84 #if !defined(INT32_C) && !defined(UINT32_C)\r
85 #define INT32_C(x)  S32_C(x)\r
86 #define UINT32_C(x) U32_C(x)\r
87 #endif\r
88 \r
89 #if !defined(INT64_C) && !defined(UINT64_C)\r
90 #define INT64_C(x)  S64_C(x)\r
91 #define UINT64_C(x) U64_C(x)\r
92 #endif\r
93 \r
94 /**@}*/\r
95 /**\name C standard macros */\r
96 #ifndef NULL\r
97 #ifdef __cplusplus\r
98 #define NULL 0\r
99 #else\r
100 #define NULL ((void *) 0)\r
101 #endif\r
102 #endif\r
103 \r
104 /********************************************************/\r
105 \r
106 #ifndef BME280_FLOAT_ENABLE\r
107 \r
108 /* #define BME280_FLOAT_ENABLE */\r
109 #endif\r
110 \r
111 #ifndef BME280_FLOAT_ENABLE\r
112 #ifndef BME280_64BIT_ENABLE\r
113 #define BME280_64BIT_ENABLE\r
114 #endif\r
115 #endif\r
116 \r
117 #ifndef TRUE\r
118 #define TRUE                              UINT8_C(1)\r
119 #endif\r
120 #ifndef FALSE\r
121 #define FALSE                             UINT8_C(0)\r
122 #endif\r
123 \r
124 /**\name I2C addresses */\r
125 #define BME280_I2C_ADDR_PRIM              UINT8_C(0x76)\r
126 #define BME280_I2C_ADDR_SEC               UINT8_C(0x77)\r
127 \r
128 /**\name BME280 chip identifier */\r
129 #define BME280_CHIP_ID                    UINT8_C(0x60)\r
130 \r
131 /**\name Register Address */\r
132 #define BME280_CHIP_ID_ADDR               UINT8_C(0xD0)\r
133 #define BME280_RESET_ADDR                 UINT8_C(0xE0)\r
134 #define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88)\r
135 #define BME280_HUMIDITY_CALIB_DATA_ADDR   UINT8_C(0xE1)\r
136 #define BME280_PWR_CTRL_ADDR              UINT8_C(0xF4)\r
137 #define BME280_CTRL_HUM_ADDR              UINT8_C(0xF2)\r
138 #define BME280_CTRL_MEAS_ADDR             UINT8_C(0xF4)\r
139 #define BME280_CONFIG_ADDR                UINT8_C(0xF5)\r
140 #define BME280_DATA_ADDR                  UINT8_C(0xF7)\r
141 \r
142 /**\name API success code */\r
143 #define BME280_OK                         INT8_C(0)\r
144 \r
145 /**\name API error codes */\r
146 #define BME280_E_NULL_PTR                 INT8_C(-1)\r
147 #define BME280_E_DEV_NOT_FOUND            INT8_C(-2)\r
148 #define BME280_E_INVALID_LEN              INT8_C(-3)\r
149 #define BME280_E_COMM_FAIL                INT8_C(-4)\r
150 #define BME280_E_SLEEP_MODE_FAIL          INT8_C(-5)\r
151 #define BME280_E_NVM_COPY_FAILED          INT8_C(-6)\r
152 \r
153 /**\name API warning codes */\r
154 #define BME280_W_INVALID_OSR_MACRO        INT8_C(1)\r
155 \r
156 /**\name Macros related to size */\r
157 #define BME280_TEMP_PRESS_CALIB_DATA_LEN  UINT8_C(26)\r
158 #define BME280_HUMIDITY_CALIB_DATA_LEN    UINT8_C(7)\r
159 #define BME280_P_T_H_DATA_LEN             UINT8_C(8)\r
160 \r
161 /**\name Sensor power modes */\r
162 #define BME280_SLEEP_MODE                 UINT8_C(0x00)\r
163 #define BME280_FORCED_MODE                UINT8_C(0x01)\r
164 #define BME280_NORMAL_MODE                UINT8_C(0x03)\r
165 \r
166 /**\name Macro to combine two 8 bit data's to form a 16 bit data */\r
167 #define BME280_CONCAT_BYTES(msb, lsb)            (((uint16_t)msb << 8) | (uint16_t)lsb)\r
168 \r
169 #define BME280_SET_BITS(reg_data, bitname, data) \\r
170     ((reg_data & ~(bitname##_MSK)) | \\r
171      ((data << bitname##_POS) & bitname##_MSK))\r
172 #define BME280_SET_BITS_POS_0(reg_data, bitname, data) \\r
173     ((reg_data & ~(bitname##_MSK)) | \\r
174      (data & bitname##_MSK))\r
175 \r
176 #define BME280_GET_BITS(reg_data, bitname)       ((reg_data & (bitname##_MSK)) >> \\r
177                                                   (bitname##_POS))\r
178 #define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))\r
179 \r
180 /**\name Macros for bit masking */\r
181 #define BME280_SENSOR_MODE_MSK      UINT8_C(0x03)\r
182 #define BME280_SENSOR_MODE_POS      UINT8_C(0x00)\r
183 \r
184 #define BME280_CTRL_HUM_MSK         UINT8_C(0x07)\r
185 #define BME280_CTRL_HUM_POS         UINT8_C(0x00)\r
186 \r
187 #define BME280_CTRL_PRESS_MSK       UINT8_C(0x1C)\r
188 #define BME280_CTRL_PRESS_POS       UINT8_C(0x02)\r
189 \r
190 #define BME280_CTRL_TEMP_MSK        UINT8_C(0xE0)\r
191 #define BME280_CTRL_TEMP_POS        UINT8_C(0x05)\r
192 \r
193 #define BME280_FILTER_MSK           UINT8_C(0x1C)\r
194 #define BME280_FILTER_POS           UINT8_C(0x02)\r
195 \r
196 #define BME280_STANDBY_MSK          UINT8_C(0xE0)\r
197 #define BME280_STANDBY_POS          UINT8_C(0x05)\r
198 \r
199 /**\name Sensor component selection macros\r
200  * These values are internal for API implementation. Don't relate this to\r
201  * data sheet.\r
202  */\r
203 #define BME280_PRESS                UINT8_C(1)\r
204 #define BME280_TEMP                 UINT8_C(1 << 1)\r
205 #define BME280_HUM                  UINT8_C(1 << 2)\r
206 #define BME280_ALL                  UINT8_C(0x07)\r
207 \r
208 /**\name Settings selection macros */\r
209 #define BME280_OSR_PRESS_SEL        UINT8_C(1)\r
210 #define BME280_OSR_TEMP_SEL         UINT8_C(1 << 1)\r
211 #define BME280_OSR_HUM_SEL          UINT8_C(1 << 2)\r
212 #define BME280_FILTER_SEL           UINT8_C(1 << 3)\r
213 #define BME280_STANDBY_SEL          UINT8_C(1 << 4)\r
214 #define BME280_ALL_SETTINGS_SEL     UINT8_C(0x1F)\r
215 \r
216 /**\name Oversampling macros */\r
217 #define BME280_NO_OVERSAMPLING      UINT8_C(0x00)\r
218 #define BME280_OVERSAMPLING_1X      UINT8_C(0x01)\r
219 #define BME280_OVERSAMPLING_2X      UINT8_C(0x02)\r
220 #define BME280_OVERSAMPLING_4X      UINT8_C(0x03)\r
221 #define BME280_OVERSAMPLING_8X      UINT8_C(0x04)\r
222 #define BME280_OVERSAMPLING_16X     UINT8_C(0x05)\r
223 \r
224 /**\name Standby duration selection macros */\r
225 #define BME280_STANDBY_TIME_0_5_MS  (0x00)\r
226 #define BME280_STANDBY_TIME_62_5_MS (0x01)\r
227 #define BME280_STANDBY_TIME_125_MS  (0x02)\r
228 #define BME280_STANDBY_TIME_250_MS  (0x03)\r
229 #define BME280_STANDBY_TIME_500_MS  (0x04)\r
230 #define BME280_STANDBY_TIME_1000_MS (0x05)\r
231 #define BME280_STANDBY_TIME_10_MS   (0x06)\r
232 #define BME280_STANDBY_TIME_20_MS   (0x07)\r
233 \r
234 /**\name Filter coefficient selection macros */\r
235 #define BME280_FILTER_COEFF_OFF     (0x00)\r
236 #define BME280_FILTER_COEFF_2       (0x01)\r
237 #define BME280_FILTER_COEFF_4       (0x02)\r
238 #define BME280_FILTER_COEFF_8       (0x03)\r
239 #define BME280_FILTER_COEFF_16      (0x04)\r
240 \r
241 #define BME280_STATUS_REG_ADDR      (0xF3)\r
242 #define BME280_SOFT_RESET_COMMAND   (0xB6)\r
243 #define BME280_STATUS_IM_UPDATE     (0x01)\r
244 \r
245 /*!\r
246  * @brief Interface selection Enums\r
247  */\r
248 enum bme280_intf {\r
249     /*! SPI interface */\r
250     BME280_SPI_INTF,\r
251 \r
252     /*! I2C interface */\r
253     BME280_I2C_INTF\r
254 };\r
255 \r
256 /*!\r
257  * @brief Type definitions\r
258  */\r
259 typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len);\r
260 typedef void (*bme280_delay_fptr_t)(uint32_t period);\r
261 \r
262 /*!\r
263  * @brief Calibration data\r
264  */\r
265 struct bme280_calib_data\r
266 {\r
267     /**\r
268      * @ Trim Variables\r
269      */\r
270 \r
271     /**@{*/\r
272     uint16_t dig_T1;\r
273     int16_t dig_T2;\r
274     int16_t dig_T3;\r
275     uint16_t dig_P1;\r
276     int16_t dig_P2;\r
277     int16_t dig_P3;\r
278     int16_t dig_P4;\r
279     int16_t dig_P5;\r
280     int16_t dig_P6;\r
281     int16_t dig_P7;\r
282     int16_t dig_P8;\r
283     int16_t dig_P9;\r
284     uint8_t dig_H1;\r
285     int16_t dig_H2;\r
286     uint8_t dig_H3;\r
287     int16_t dig_H4;\r
288     int16_t dig_H5;\r
289     int8_t dig_H6;\r
290     int32_t t_fine;\r
291 \r
292     /**@}*/\r
293 };\r
294 \r
295 /*!\r
296  * @brief bme280 sensor structure which comprises of temperature, pressure and\r
297  * humidity data\r
298  */\r
299 #ifdef BME280_FLOAT_ENABLE\r
300 struct bme280_data\r
301 {\r
302     /*! Compensated pressure */\r
303     double pressure;\r
304 \r
305     /*! Compensated temperature */\r
306     double temperature;\r
307 \r
308     /*! Compensated humidity */\r
309     double humidity;\r
310 };\r
311 #else\r
312 struct bme280_data\r
313 {\r
314     /*! Compensated pressure */\r
315     uint32_t pressure;\r
316 \r
317     /*! Compensated temperature */\r
318     int32_t temperature;\r
319 \r
320     /*! Compensated humidity */\r
321     uint32_t humidity;\r
322 };\r
323 #endif /* BME280_USE_FLOATING_POINT */\r
324 \r
325 /*!\r
326  * @brief bme280 sensor structure which comprises of uncompensated temperature,\r
327  * pressure and humidity data\r
328  */\r
329 struct bme280_uncomp_data\r
330 {\r
331     /*! un-compensated pressure */\r
332     uint32_t pressure;\r
333 \r
334     /*! un-compensated temperature */\r
335     uint32_t temperature;\r
336 \r
337     /*! un-compensated humidity */\r
338     uint32_t humidity;\r
339 };\r
340 \r
341 /*!\r
342  * @brief bme280 sensor settings structure which comprises of mode,\r
343  * oversampling and filter settings.\r
344  */\r
345 struct bme280_settings\r
346 {\r
347     /*! pressure oversampling */\r
348     uint8_t osr_p;\r
349 \r
350     /*! temperature oversampling */\r
351     uint8_t osr_t;\r
352 \r
353     /*! humidity oversampling */\r
354     uint8_t osr_h;\r
355 \r
356     /*! filter coefficient */\r
357     uint8_t filter;\r
358 \r
359     /*! standby time */\r
360     uint8_t standby_time;\r
361 };\r
362 \r
363 /*!\r
364  * @brief bme280 device structure\r
365  */\r
366 struct bme280_dev\r
367 {\r
368     /*! Chip Id */\r
369     uint8_t chip_id;\r
370 \r
371     /*! Device Id */\r
372     uint8_t dev_id;\r
373 \r
374     /*! SPI/I2C interface */\r
375     enum bme280_intf intf;\r
376 \r
377     /*! Read function pointer */\r
378     bme280_com_fptr_t read;\r
379 \r
380     /*! Write function pointer */\r
381     bme280_com_fptr_t write;\r
382 \r
383     /*! Delay function pointer */\r
384     bme280_delay_fptr_t delay_ms;\r
385 \r
386     /*! Trim data */\r
387     struct bme280_calib_data calib_data;\r
388 \r
389     /*! Sensor settings */\r
390     struct bme280_settings settings;\r
391 };\r
392 \r
393 #endif /* BME280_DEFS_H_ */\r
394 /** @}*/\r
395 /** @}*/\r