]> git.itanic.dy.fi Git - BME280_driver/blob - bme280_defs.h
Updated to v3.2.0
[BME280_driver] / bme280_defs.h
1 /**
2  * Copyright (C) 2016 - 2017 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        21 Mar 2017
44  * @version     3.2.0
45  * @brief
46  *
47  */
48
49 /*! @file bme280_defs.h
50     @brief Sensor driver for BME280 sensor */
51 /*!
52  * @defgroup BME280 SENSOR API
53  * @brief
54  * @{*/
55 #ifndef BME280_DEFS_H_
56 #define BME280_DEFS_H_
57
58 /********************************************************/
59 /* header includes */
60 #ifdef __KERNEL__
61 #include <linux/types.h>
62 #else
63 #include <stdint.h>
64 #include <stdio.h>
65 #endif
66
67 #ifdef __KERNEL__
68 #if (LONG_MAX) > 0x7fffffff
69 #define __have_long64   1
70 #elif (LONG_MAX) == 0x7fffffff
71 #define __have_long32   1
72 #endif
73
74 #if !defined(UINT8_C)
75 #define INT8_C(x)       x
76 #if (INT_MAX) > 0x7f
77 #define UINT8_C(x)      x
78 #else
79 #define UINT8_C(x)      x##U
80 #endif
81 #endif
82
83 #if !defined(UINT16_C)
84 #define INT16_C(x)      x
85 #if (INT_MAX) > 0x7fff
86 #define UINT16_C(x)     x
87 #else
88 #define UINT16_C(x)     x##U
89 #endif
90 #endif
91
92 #if !defined(INT32_C) && !defined(UINT32_C)
93 #if __have_long32
94 #define INT32_C(x)      x##L
95 #define UINT32_C(x)     x##UL
96 #else
97 #define INT32_C(x)      x
98 #define UINT32_C(x)     x##U
99 #endif
100 #endif
101
102 #if !defined(INT64_C) && !defined(UINT64_C)
103 #if __have_long64
104 #define INT64_C(x)      x##L
105 #define UINT64_C(x)     x##UL
106 #else
107 #define INT64_C(x)      x##LL
108 #define UINT64_C(x)     x##ULL
109 #endif
110 #endif
111 #endif
112 /**@}*/
113
114 /**\name C standard macros */
115 #ifndef NULL
116 #ifdef __cplusplus
117 #define NULL   0
118 #else
119 #define NULL   ((void *) 0)
120 #endif
121 #endif
122
123 /* #define FLOATING_POINT_REPRESENTATION */
124 #define MACHINE_64_BIT
125
126 #ifndef TRUE
127 #define TRUE                UINT8_C(1)
128 #endif
129 #ifndef FALSE
130 #define FALSE               UINT8_C(0)
131 #endif
132
133 /**\name I2C addresses */
134 #define BME280_I2C_ADDR_PRIM            UINT8_C(0x76)
135 #define BME280_I2C_ADDR_SEC             UINT8_C(0x77)
136
137 /**\name BME280 chip identifier */
138 #define BME280_CHIP_ID  UINT8_C(0x60)
139
140 /**\name Register Address */
141 #define BME280_CHIP_ID_ADDR                     UINT8_C(0xD0)
142 #define BME280_RESET_ADDR                       UINT8_C(0xE0)
143 #define BME280_TEMP_PRESS_CALIB_DATA_ADDR       UINT8_C(0x88)
144 #define BME280_HUMIDITY_CALIB_DATA_ADDR         UINT8_C(0xE1)
145 #define BME280_PWR_CTRL_ADDR                    UINT8_C(0xF4)
146 #define BME280_CTRL_HUM_ADDR                    UINT8_C(0xF2)
147 #define BME280_CTRL_MEAS_ADDR                   UINT8_C(0xF4)
148 #define BME280_CONFIG_ADDR                      UINT8_C(0xF5)
149 #define BME280_DATA_ADDR                        UINT8_C(0xF7)
150
151 /**\name API success code */
152 #define BME280_OK                               INT8_C(0)
153 /**\name API error codes */
154 #define BME280_E_NULL_PTR                       INT8_C(-1)
155 #define BME280_E_DEV_NOT_FOUND                  INT8_C(-2)
156 #define BME280_E_INVALID_LEN                    INT8_C(-3)
157 #define BME280_E_COMM_FAIL                      INT8_C(-4)
158 #define BME280_E_SLEEP_MODE_FAIL                INT8_C(-5)
159 /**\name API warning codes */
160 #define BME280_W_INVALID_OSR_MACRO              UINT8_C(1)
161
162 /**\name Macros related to size */
163 #define BME280_TEMP_PRESS_CALIB_DATA_LEN        UINT8_C(26)
164 #define BME280_HUMIDITY_CALIB_DATA_LEN          UINT8_C(7)
165 #define BME280_P_T_H_DATA_LEN                   UINT8_C(8)
166
167 /**\name Sensor power modes */
168 #define BME280_SLEEP_MODE                       UINT8_C(0x00)
169 #define BME280_FORCED_MODE                      UINT8_C(0x01)
170 #define BME280_NORMAL_MODE                      UINT8_C(0x03)
171
172 /**\name Macro to combine two 8 bit data's to form a 16 bit data */
173 #define BME280_CONCAT_BYTES(msb, lsb)     (((uint16_t)msb << 8) | (uint16_t)lsb)
174
175 #define BME280_SET_BITS(reg_data, bitname, data) \
176                                 ((reg_data & ~(bitname##_MSK)) | \
177                                 ((data << bitname##_POS) & bitname##_MSK))
178 #define BME280_SET_BITS_POS_0(reg_data, bitname, data) \
179                                 ((reg_data & ~(bitname##_MSK)) | \
180                                 (data & bitname##_MSK))
181
182 #define BME280_GET_BITS(reg_data, bitname)  ((reg_data & (bitname##_MSK)) >> \
183                                                         (bitname##_POS))
184 #define BME280_GET_BITS_POS_0(reg_data, bitname)  (reg_data & (bitname##_MSK))
185
186 /**\name Macros for bit masking */
187 #define BME280_SENSOR_MODE_MSK          UINT8_C(0x03)
188 #define BME280_SENSOR_MODE_POS          UINT8_C(0x00)
189
190 #define BME280_CTRL_HUM_MSK             UINT8_C(0x07)
191 #define BME280_CTRL_HUM_POS             UINT8_C(0x00)
192
193 #define BME280_CTRL_PRESS_MSK           UINT8_C(0x1C)
194 #define BME280_CTRL_PRESS_POS           UINT8_C(0x02)
195
196 #define BME280_CTRL_TEMP_MSK            UINT8_C(0xE0)
197 #define BME280_CTRL_TEMP_POS            UINT8_C(0x05)
198
199 #define BME280_FILTER_MSK               UINT8_C(0x1C)
200 #define BME280_FILTER_POS               UINT8_C(0x02)
201
202 #define BME280_STANDBY_MSK              UINT8_C(0xE0)
203 #define BME280_STANDBY_POS              UINT8_C(0x05)
204
205 /**\name Sensor component selection macros
206    These values are internal for API implementation. Don't relate this to
207    data sheet.*/
208 #define BME280_PRESS                    UINT8_C(1)
209 #define BME280_TEMP                     UINT8_C(1 << 1)
210 #define BME280_HUM                      UINT8_C(1 << 2)
211 #define BME280_ALL                      UINT8_C(0x07)
212
213 /**\name Settings selection macros */
214 #define BME280_OSR_PRESS_SEL            UINT8_C(1)
215 #define BME280_OSR_TEMP_SEL             UINT8_C(1 << 1)
216 #define BME280_OSR_HUM_SEL              UINT8_C(1 << 2)
217 #define BME280_FILTER_SEL               UINT8_C(1 << 3)
218 #define BME280_STANDBY_SEL              UINT8_C(1 << 4)
219 #define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F)
220
221 /**\name Oversampling macros */
222 #define BME280_NO_OVERSAMPLING          UINT8_C(0x00)
223 #define BME280_OVERSAMPLING_1X          UINT8_C(0x01)
224 #define BME280_OVERSAMPLING_2X          UINT8_C(0x02)
225 #define BME280_OVERSAMPLING_4X          UINT8_C(0x03)
226 #define BME280_OVERSAMPLING_8X          UINT8_C(0x04)
227 #define BME280_OVERSAMPLING_16X         UINT8_C(0x05)
228
229 /**\name Standby duration selection macros */
230 #define BME280_STANDBY_TIME_1_MS              (0x00)
231 #define BME280_STANDBY_TIME_62_5_MS             (0x01)
232 #define BME280_STANDBY_TIME_125_MS                        (0x02)
233 #define BME280_STANDBY_TIME_250_MS            (0x03)
234 #define BME280_STANDBY_TIME_500_MS            (0x04)
235 #define BME280_STANDBY_TIME_1000_MS           (0x05)
236 #define BME280_STANDBY_TIME_10_MS             (0x06)
237 #define BME280_STANDBY_TIME_20_MS             (0x07)
238
239 /**\name Filter coefficient selection macros */
240 #define BME280_FILTER_COEFF_OFF               (0x00)
241 #define BME280_FILTER_COEFF_2                 (0x01)
242 #define BME280_FILTER_COEFF_4                 (0x02)
243 #define BME280_FILTER_COEFF_8                 (0x03)
244 #define BME280_FILTER_COEFF_16                (0x04)
245
246 /*!
247  * @brief Interface selection Enums
248  */
249 enum bme280_intf {
250         /*! SPI interface */
251         BME280_SPI_INTF,
252         /*! I2C interface */
253         BME280_I2C_INTF
254 };
255
256 /*!
257  * @brief Type definitions
258  */
259 typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr,
260                 uint8_t *data, uint16_t len);
261
262 typedef void (*bme280_delay_fptr_t)(uint32_t period);
263
264 /*!
265  * @brief Calibration data
266  */
267 struct bme280_calib_data {
268  /**
269  * @ Trim Variables
270  */
271 /**@{*/
272         uint16_t dig_T1;
273         int16_t dig_T2;
274         int16_t dig_T3;
275         uint16_t dig_P1;
276         int16_t dig_P2;
277         int16_t dig_P3;
278         int16_t dig_P4;
279         int16_t dig_P5;
280         int16_t dig_P6;
281         int16_t dig_P7;
282         int16_t dig_P8;
283         int16_t dig_P9;
284         uint8_t  dig_H1;
285         int16_t dig_H2;
286         uint8_t  dig_H3;
287         int16_t dig_H4;
288         int16_t dig_H5;
289         int8_t  dig_H6;
290         int32_t t_fine;
291 /**@}*/
292 };
293
294 /*!
295  * @brief bme280 sensor structure which comprises of temperature, pressure and
296  * humidity data
297  */
298 #ifdef FLOATING_POINT_REPRESENTATION
299 struct bme280_data {
300         /*! Compensated pressure */
301         double pressure;
302         /*! Compensated temperature */
303         double temperature;
304         /*! Compensated humidity */
305         double humidity;
306 };
307 #else
308 struct bme280_data {
309         /*! Compensated pressure */
310         uint32_t pressure;
311         /*! Compensated temperature */
312         int32_t temperature;
313         /*! Compensated humidity */
314         uint32_t humidity;
315 };
316 #endif /* BME280_USE_FLOATING_POINT */
317
318 /*!
319  * @brief bme280 sensor structure which comprises of uncompensated temperature,
320  * pressure and humidity data
321  */
322 struct bme280_uncomp_data {
323         /*! un-compensated pressure */
324         uint32_t pressure;
325         /*! un-compensated temperature */
326         uint32_t temperature;
327         /*! un-compensated humidity */
328         uint32_t humidity;
329 };
330
331 /*!
332  * @brief bme280 sensor settings structure which comprises of mode,
333  * oversampling and filter settings.
334  */
335 struct bme280_settings {
336         /*! pressure oversampling */
337         uint8_t osr_p;
338         /*! temperature oversampling */
339         uint8_t osr_t;
340         /*! humidity oversampling */
341         uint8_t osr_h;
342         /*! filter coefficient */
343         uint8_t filter;
344         /*! standby time */
345         uint8_t standby_time;
346 };
347
348 /*!
349  * @brief bme280 device structure
350  */
351 struct bme280_dev {
352         /*! Chip Id */
353         uint8_t chip_id;
354         /*! Device Id */
355         uint8_t id;
356         /*! SPI/I2C interface */
357         enum bme280_intf interface;
358         /*! Read function pointer */
359         bme280_com_fptr_t read;
360         /*! Write function pointer */
361         bme280_com_fptr_t write;
362         /*! Delay function pointer */
363         bme280_delay_fptr_t delay_ms;
364         /*! Trim data */
365         struct bme280_calib_data calib_data;
366         /*! Sensor settings */
367         struct bme280_settings settings;
368 };
369
370 #endif /* BME280_DEFS_H_ */
371 /** @}*/
372 /** @}*/