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