2 * Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
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.
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.
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
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.
49 /*! @file bme280_defs.h
50 @brief Sensor driver for BME280 sensor */
52 * @defgroup BME280 SENSOR API
55 #ifndef BME280_DEFS_H_
56 #define BME280_DEFS_H_
58 /********************************************************/
61 #include <linux/types.h>
68 #if (LONG_MAX) > 0x7fffffff
69 #define __have_long64 1
70 #elif (LONG_MAX) == 0x7fffffff
71 #define __have_long32 1
79 #define UINT8_C(x) x##U
83 #if !defined(UINT16_C)
85 #if (INT_MAX) > 0x7fff
88 #define UINT16_C(x) x##U
92 #if !defined(INT32_C) && !defined(UINT32_C)
94 #define INT32_C(x) x##L
95 #define UINT32_C(x) x##UL
98 #define UINT32_C(x) x##U
102 #if !defined(INT64_C) && !defined(UINT64_C)
104 #define INT64_C(x) x##L
105 #define UINT64_C(x) x##UL
107 #define INT64_C(x) x##LL
108 #define UINT64_C(x) x##ULL
114 /**\name C standard macros */
119 #define NULL ((void *) 0)
123 #ifndef BME280_FLOAT_ENABLE
124 //#define BME280_FLOAT_ENABLE
127 #ifndef BME280_FLOAT_ENABLE
128 # ifndef BME280_64BIT_ENABLE
129 # define BME280_64BIT_ENABLE
134 #define TRUE UINT8_C(1)
137 #define FALSE UINT8_C(0)
140 /**\name I2C addresses */
141 #define BME280_I2C_ADDR_PRIM UINT8_C(0x76)
142 #define BME280_I2C_ADDR_SEC UINT8_C(0x77)
144 /**\name BME280 chip identifier */
145 #define BME280_CHIP_ID UINT8_C(0x60)
147 /**\name Register Address */
148 #define BME280_CHIP_ID_ADDR UINT8_C(0xD0)
149 #define BME280_RESET_ADDR UINT8_C(0xE0)
150 #define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88)
151 #define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1)
152 #define BME280_PWR_CTRL_ADDR UINT8_C(0xF4)
153 #define BME280_CTRL_HUM_ADDR UINT8_C(0xF2)
154 #define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4)
155 #define BME280_CONFIG_ADDR UINT8_C(0xF5)
156 #define BME280_DATA_ADDR UINT8_C(0xF7)
158 /**\name API success code */
159 #define BME280_OK INT8_C(0)
160 /**\name API error codes */
161 #define BME280_E_NULL_PTR INT8_C(-1)
162 #define BME280_E_DEV_NOT_FOUND INT8_C(-2)
163 #define BME280_E_INVALID_LEN INT8_C(-3)
164 #define BME280_E_COMM_FAIL INT8_C(-4)
165 #define BME280_E_SLEEP_MODE_FAIL INT8_C(-5)
166 /**\name API warning codes */
167 #define BME280_W_INVALID_OSR_MACRO UINT8_C(1)
169 /**\name Macros related to size */
170 #define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26)
171 #define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7)
172 #define BME280_P_T_H_DATA_LEN UINT8_C(8)
174 /**\name Sensor power modes */
175 #define BME280_SLEEP_MODE UINT8_C(0x00)
176 #define BME280_FORCED_MODE UINT8_C(0x01)
177 #define BME280_NORMAL_MODE UINT8_C(0x03)
179 /**\name Macro to combine two 8 bit data's to form a 16 bit data */
180 #define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
182 #define BME280_SET_BITS(reg_data, bitname, data) \
183 ((reg_data & ~(bitname##_MSK)) | \
184 ((data << bitname##_POS) & bitname##_MSK))
185 #define BME280_SET_BITS_POS_0(reg_data, bitname, data) \
186 ((reg_data & ~(bitname##_MSK)) | \
187 (data & bitname##_MSK))
189 #define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \
191 #define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))
193 /**\name Macros for bit masking */
194 #define BME280_SENSOR_MODE_MSK UINT8_C(0x03)
195 #define BME280_SENSOR_MODE_POS UINT8_C(0x00)
197 #define BME280_CTRL_HUM_MSK UINT8_C(0x07)
198 #define BME280_CTRL_HUM_POS UINT8_C(0x00)
200 #define BME280_CTRL_PRESS_MSK UINT8_C(0x1C)
201 #define BME280_CTRL_PRESS_POS UINT8_C(0x02)
203 #define BME280_CTRL_TEMP_MSK UINT8_C(0xE0)
204 #define BME280_CTRL_TEMP_POS UINT8_C(0x05)
206 #define BME280_FILTER_MSK UINT8_C(0x1C)
207 #define BME280_FILTER_POS UINT8_C(0x02)
209 #define BME280_STANDBY_MSK UINT8_C(0xE0)
210 #define BME280_STANDBY_POS UINT8_C(0x05)
212 /**\name Sensor component selection macros
213 These values are internal for API implementation. Don't relate this to
215 #define BME280_PRESS UINT8_C(1)
216 #define BME280_TEMP UINT8_C(1 << 1)
217 #define BME280_HUM UINT8_C(1 << 2)
218 #define BME280_ALL UINT8_C(0x07)
220 /**\name Settings selection macros */
221 #define BME280_OSR_PRESS_SEL UINT8_C(1)
222 #define BME280_OSR_TEMP_SEL UINT8_C(1 << 1)
223 #define BME280_OSR_HUM_SEL UINT8_C(1 << 2)
224 #define BME280_FILTER_SEL UINT8_C(1 << 3)
225 #define BME280_STANDBY_SEL UINT8_C(1 << 4)
226 #define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F)
228 /**\name Oversampling macros */
229 #define BME280_NO_OVERSAMPLING UINT8_C(0x00)
230 #define BME280_OVERSAMPLING_1X UINT8_C(0x01)
231 #define BME280_OVERSAMPLING_2X UINT8_C(0x02)
232 #define BME280_OVERSAMPLING_4X UINT8_C(0x03)
233 #define BME280_OVERSAMPLING_8X UINT8_C(0x04)
234 #define BME280_OVERSAMPLING_16X UINT8_C(0x05)
236 /**\name Standby duration selection macros */
237 #define BME280_STANDBY_TIME_1_MS (0x00)
238 #define BME280_STANDBY_TIME_62_5_MS (0x01)
239 #define BME280_STANDBY_TIME_125_MS (0x02)
240 #define BME280_STANDBY_TIME_250_MS (0x03)
241 #define BME280_STANDBY_TIME_500_MS (0x04)
242 #define BME280_STANDBY_TIME_1000_MS (0x05)
243 #define BME280_STANDBY_TIME_10_MS (0x06)
244 #define BME280_STANDBY_TIME_20_MS (0x07)
246 /**\name Filter coefficient selection macros */
247 #define BME280_FILTER_COEFF_OFF (0x00)
248 #define BME280_FILTER_COEFF_2 (0x01)
249 #define BME280_FILTER_COEFF_4 (0x02)
250 #define BME280_FILTER_COEFF_8 (0x03)
251 #define BME280_FILTER_COEFF_16 (0x04)
254 * @brief Interface selection Enums
264 * @brief Type definitions
266 typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr,
267 uint8_t *data, uint16_t len);
269 typedef void (*bme280_delay_fptr_t)(uint32_t period);
272 * @brief Calibration data
274 struct bme280_calib_data {
302 * @brief bme280 sensor structure which comprises of temperature, pressure and
305 #ifdef BME280_FLOAT_ENABLE
307 /*! Compensated pressure */
309 /*! Compensated temperature */
311 /*! Compensated humidity */
316 /*! Compensated pressure */
318 /*! Compensated temperature */
320 /*! Compensated humidity */
323 #endif /* BME280_USE_FLOATING_POINT */
326 * @brief bme280 sensor structure which comprises of uncompensated temperature,
327 * pressure and humidity data
329 struct bme280_uncomp_data {
330 /*! un-compensated pressure */
332 /*! un-compensated temperature */
333 uint32_t temperature;
334 /*! un-compensated humidity */
339 * @brief bme280 sensor settings structure which comprises of mode,
340 * oversampling and filter settings.
342 struct bme280_settings {
343 /*! pressure oversampling */
345 /*! temperature oversampling */
347 /*! humidity oversampling */
349 /*! filter coefficient */
352 uint8_t standby_time;
356 * @brief bme280 device structure
363 /*! SPI/I2C interface */
364 enum bme280_intf intf;
365 /*! Read function pointer */
366 bme280_com_fptr_t read;
367 /*! Write function pointer */
368 bme280_com_fptr_t write;
369 /*! Delay function pointer */
370 bme280_delay_fptr_t delay_ms;
372 struct bme280_calib_data calib_data;
373 /*! Sensor settings */
374 struct bme280_settings settings;
377 #endif /* BME280_DEFS_H_ */