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