-
Notifications
You must be signed in to change notification settings - Fork 1
/
bme680.c
618 lines (494 loc) · 19.9 KB
/
bme680.c
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include <stdio.h>
#include "bme680.h"
static void calc_temp_comp(bme680_t *bme680);
static void calc_press_comp(bme680_t *bme680);
static void calc_hum_comp(bme680_t *bme680);
static void calc_gas_res(bme680_t *bme680);
static void check_spi_page(bme680_t *bme680, uint8_t reg);
static int set_spi_page(bme680_t *bme680, uint8_t no);
/********************************************************************/
static int write_dev(bme680_t *bme680, uint8_t reg, uint8_t value) {
if (BME680_IS_SPI(bme680->mode)) {
check_spi_page(bme680, reg);
reg &= 0x7F;
}
return bme680->dev.write(reg, value);
}
/********************************************************************/
static int read_dev(bme680_t *bme680, uint8_t reg, uint8_t *dst, uint32_t size) {
if (BME680_IS_SPI(bme680->mode)) {
check_spi_page(bme680, reg);
reg |= 0x80;
}
return bme680->dev.read(reg, dst, size);
}
/********************************************************************/
/* change spi page if necessary */
static void check_spi_page(bme680_t *bme680, uint8_t reg) {
uint8_t required_page = REG_SPI_PAGE(reg);
if (required_page != bme680->spi_page) {
set_spi_page(bme680, required_page);
bme680->spi_page = required_page;
}
}
/********************************************************************/
static int set_spi_page(bme680_t *bme680, uint8_t page_no) {
uint8_t status_reg = page_no << 4;
return bme680->dev.write(REG_STATUS, status_reg);
}
/********************************************************************/
static int read_id(bme680_t *bme680, uint8_t *id) {
uint8_t id_reg;
/* force spi page 0. special case */
if (BME680_IS_SPI(bme680->mode)) {
set_spi_page(bme680, 0);
bme680->spi_page = 0;
id_reg = 0x50 | 0x80;
} else {
id_reg = REG_ID;
}
return bme680->dev.read(id_reg, id, 1);
}
/********************************************************************/
/* write local setpoint index to device, must not be running */
int bme680_write_setpoint_index(bme680_t *bme680) {
/* setpoint (0 thru 9) bits 0,1,2,3 and run_gas bit 4 */
uint8_t ctrl_gas_1 = (bme680->setpoint & 0x0F) | (1 << 4);
return write_dev(bme680, REG_CTRL_GAS_1, ctrl_gas_1);
}
/********************************************************************/
/* read the currently selected heater setpoint index on the device */
int bme680_read_setpoint_index(bme680_t *bme680, uint8_t *index) {
uint8_t meas_status;
int err = 0;
err |= read_dev(bme680, REG_MEAS_STATUS, &meas_status, 1);
*index = (meas_status) & 0x0F;
return err;
}
/********************************************************************/
int bme680_init(bme680_t *bme680, uint8_t mode) {
uint8_t id;
int i;
bme680->mode = mode;
bme680->spi_page = 0;
bme680->gas_valid = 0;
bme680->heat_stab = 0;
bme680->setpoint = 0;
if (bme680->dev.init() != 0) {
return 1;
}
if (read_id(bme680, &id) != 0) {
return 1;
}
if (id != 0x61) {
return 1;
}
/* zero gas sensor arrays */
for(i=0; i<10; i++) {
bme680->cfg.idac_heat[i] = 0;
bme680->cfg.res_heat[i] = 0;
bme680->cfg.gas_wait[i] = 0;
}
return 0;
}
/********************************************************************/
int bme680_deinit(bme680_t *bme680) {
if (bme680->dev.deinit) {
bme680->dev.deinit();
}
return 0;
}
/********************************************************************/
int bme680_reset(bme680_t *bme680) {
uint8_t magic = 0xB6;
uint8_t reg;
int ret;
/* force page 0. special case */
if (BME680_IS_SPI(bme680->mode)) {
set_spi_page(bme680, 0);
bme680->spi_page = 0;
reg = 0x60 | 0x80;
} else {
reg = REG_RESET;
}
ret = bme680->dev.write(reg, magic);
bme680->dev.sleep(2000); /* sleep for 2 ms */
return ret;
}
/********************************************************************/
/* configure device */
int bme680_configure(bme680_t *bme680) {
uint8_t meas, hum, filter, ctrl_gas1, ctrl_gas0, i;
int err = 0;
meas = hum = filter = 0;
/* ctrl_meas. the last 0 is ticked on to enable forced mode,
* but the config has to be written first. strange behaviour.
*/
meas = bme680->cfg.osrs_t << 5 | bme680->cfg.osrs_p << 2;
hum = bme680->cfg.osrs_h;
filter = bme680->cfg.filter << 2;
/* backup of ctrl meas reg because you cannot retrieve it from the device later */
bme680->cfg.meas = meas;
err |= write_dev(bme680, REG_CTRL_MEAS, meas);
err |= write_dev(bme680, REG_CTRL_HUM, hum);
err |= write_dev(bme680, REG_CONFIG, filter);
if (!BME680_GAS_ENABLED(bme680->mode)) {
goto SKIP_GAS;
}
/* write out all 10 setpoints */
/* those not explicitly set are defaulted to 0 (which has no effect) */
for(i=0; i<10; i++) {
err |= write_dev(bme680, 0x6D - i, bme680->cfg.gas_wait[9 - i]);
err |= write_dev(bme680, 0x63 - i, bme680->cfg.res_heat[9 - i]);
err |= write_dev(bme680, 0x59 - i, bme680->cfg.idac_heat[9 - i]);
}
ctrl_gas1 = bme680->setpoint | (1 << 4);
ctrl_gas0 = 0; /* := (1 << 3) to turn off current going to heater */
err |= write_dev(bme680, REG_CTRL_GAS_1, ctrl_gas1);
err |= write_dev(bme680, REG_CTRL_GAS_0, ctrl_gas0);
SKIP_GAS:
return err;
}
/********************************************************************/
/* To start forced mode, you just have to set the lsb=1 of REG_CTRL_MEAS */
int bme680_start(bme680_t *bme680) {
int err = 0;
uint8_t meas;
meas = bme680->cfg.meas | 1;
err |= write_dev(bme680, REG_CTRL_MEAS, meas);
return err;
}
/********************************************************************/
/* blocks until all scheduled conversions on the device are done. */
int bme680_poll(bme680_t *bme680) {
uint8_t meas_status = 0;
uint8_t gas_measuring = 0;
uint8_t any_measuring = 0;
int err = 0;
do {
bme680->dev.sleep(5000); /* 5 ms */
err |= read_dev(bme680, REG_MEAS_STATUS, &meas_status, 1);
gas_measuring = (meas_status >> 6) & 1;
any_measuring = (meas_status >> 5) & 1;
} while ((gas_measuring || any_measuring) && !err);
return err;
}
/********************************************************************/
/* assume start'd and poll'd */
int bme680_read(bme680_t *bme680) {
/* begin by reading ADCs */
uint8_t buffer[3] = {0, 0 ,0};
int err = 0;
err |= read_dev(bme680, 0x22, buffer, 3);
bme680->adc.temp = (buffer[0] << 12) | (buffer[1] << 4) | (buffer[2] >> 4);
err |= read_dev(bme680, 0x1F, buffer, 3);
bme680->adc.press = (buffer[0] << 12) | (buffer[1] << 4) | (buffer[2] >> 4);
err |= read_dev(bme680, 0x25, buffer, 2);
bme680->adc.hum = (buffer[0] << 8) | buffer[1];
/* adc readings are only 20-bit when the IIR filter is enabled.
* otherwise, it depends on the oversample settings.
* note: humidity is not IIR filtered, and always 16-bit.
* IIR filter on (any level) -> 20-bit
* IIR filter off -> 16 + (osrs_x - 1) bits.
* */
if (bme680->cfg.filter == BME680_IIR_COEFF_0) {
bme680->adc.temp >>= (bme680->cfg.osrs_t - 1);
bme680->adc.press >>= (bme680->cfg.osrs_p - 1);
}
/* read gas adc values and check error bits */
if (BME680_GAS_ENABLED(bme680->mode)) {
err |= read_dev(bme680, 0x2A, buffer, 2);
/* read gas-related adc values */
bme680->adc.gas = (buffer[0] << 2) | (buffer[1] >> 6);
bme680->adc.gas_range = buffer[1] & 0xF;
/* check gas validity status (if one actually took place ??? ) */
bme680->gas_valid = (buffer[1] >> 5) & 1;
/* check heater stability. if it managed to get to temp within given time + preload current */
bme680->heat_stab = (buffer[1] >> 4) & 1;
}
/* read/convert in order ..*/
calc_temp_comp(bme680);
calc_press_comp(bme680);
calc_hum_comp(bme680);
if (BME680_GAS_ENABLED(bme680->mode)) {
calc_gas_res(bme680);
}
return err;
}
/***********************************************************************/
/* These arrays are used to compute a sensor heating value `res_heat' */
/* for a specified heating target, specified in degree C. */
/***********************************************************************/
/*
* Below functions are out of the datasheet
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverflow"
#pragma GCC diagnostic ignored "-Wstrict-overflow"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wfloat-conversion"
static double const_array1[16] = {
1, 1, 1, 1, 1, 0.99, 1, 0.992, 1,
1, 0.998, 0.995, 1, 0.99, 1, 1
};
static double const_array2[16] = {
8000000, 4000000, 2000000, 1000000, 499500.4995, 248262.1648,
125000, 63004.03226, 31281.28128, 15625, 7812.5, 3906.25,
1953.125, 976.5625, 488.28125, 244.140625
};
static int const_array1_int[16] = {
2147483647, 2147483647, 2147483647, 2147483647, 2147483647,
2126008810, 2147483647, 2130303777, 2147483647, 2147483647,
2143188679, 2136746228, 2147483647, 2126008810, 2147483647,
2147483647
};
static int const_array2_int[16] = {
4096000000, 2048000000, 1024000000, 512000000, 255744255,
127110228, 64000000, 32258064, 16016016, 8000000, 4000000,
2000000, 1000000, 500000, 250000, 125000
};
/********************************************************************/
/********************************************************************/
static void calc_temp_comp_1 (bme680_t *bme680) {
double var1, var2, temp_comp;
var1 = (((double)bme680->adc.temp / 16384.0) - ((double)bme680->cal.par_t1 / 1024.0)) *
(double)bme680->cal.par_t2;
var2 = ((((double)bme680->adc.temp / 131072.0) - ((double)bme680->cal.par_t1 / 8192.0)) *
(((double)bme680->adc.temp / 131072.0) - ((double)bme680->cal.par_t1 / 8192.0))) *
((double)bme680->cal.par_t3 * 16.0);
bme680->fcomp.tfine = var1 + var2;
temp_comp = (var1 + var2) / 5120.0;
bme680->fcomp.temp = temp_comp;
}
/********************************************************************/
static void calc_temp_comp_2 (bme680_t *bme680) {
int32_t var1, var2, var3, temp_comp;
var1 = ((int32_t)bme680->adc.temp >> 3) - ((int32_t) bme680->cal.par_t1 << 1);
var2 = (var1 * (int32_t)bme680->cal.par_t2) >> 11;
var3 = ((((var1 >> 1) * (var1 >> 1)) >> 12) * ((int32_t)bme680->cal.par_t3 << 4)) >> 14;
bme680->icomp.tfine = var2 + var3;
temp_comp = (((var2 + var3) * 5) + 128) >> 8;
bme680->icomp.temp = temp_comp;
}
/********************************************************************/
static void calc_temp_comp (bme680_t *bme680) {
if (BME680_IS_FLOAT(bme680->mode)) {
calc_temp_comp_1(bme680);
} else {
calc_temp_comp_2(bme680);
}
}
/********************************************************************/
static void calc_press_comp_1 (bme680_t *bme680) {
double var1, var2, var3, press_comp;
var1 = ((double)bme680->fcomp.tfine / 2.0) - 64000.0;
var2 = var1 * var1 * ((double)bme680->cal.par_p6 / 131072.0);
var2 = var2 + (var1 * (double)bme680->cal.par_p5 * 2.0);
var2 = (var2 / 4.0) + ((double)bme680->cal.par_p4 * 65536.0);
var1 = ((((double)bme680->cal.par_p3 * var1 * var1) / 16384.0) +
((double)bme680->cal.par_p2 * var1)) / 524288.0;
var1 = (1.0 + (var1 / 32768.0)) * (double)bme680->cal.par_p1;
press_comp = 1048576.0 - (double)bme680->adc.press;
press_comp = ((press_comp - (var2 / 4096.0)) * 6250.0) / var1;
var1 = ((double)bme680->cal.par_p9 * press_comp * press_comp) / 2147483648.0;
var2 = press_comp * ((double)bme680->cal.par_p8 / 32768.0);
var3 = (press_comp / 256.0) * (press_comp / 256.0) *
(press_comp / 256.0) * (bme680->cal.par_p10 / 131072.0);
press_comp = press_comp + (var1 + var2 + var3 + ((double)bme680->cal.par_p7 * 128.0)) / 16.0;
bme680->fcomp.press = press_comp;
}
/********************************************************************/
static void calc_press_comp_2 (bme680_t *bme680 ) {
int32_t var1, var2, var3, press_comp;
var1 = ((int32_t)bme680->icomp.tfine >> 1) - 64000;
var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * (int32_t)bme680->cal.par_p6) >> 2;
var2 = var2 + ((var1 * (int32_t)bme680->cal.par_p5) << 1);
var2 = (var2 >> 2) + ((int32_t)bme680->cal.par_p4 << 16);
var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
((int32_t)bme680->cal.par_p3 << 5)) >> 3) + (((int32_t)bme680->cal.par_p2 * var1) >> 1);
var1 = var1 >> 18;
var1 = ((32768 + var1) * (int32_t)bme680->cal.par_p1) >> 15;
press_comp = 1048576 - bme680->adc.press; /* bosch code pg 19 says "press_raw" here ??? */
press_comp = (uint32_t)((press_comp - (var2 >> 12)) * ((uint32_t)3125));
if (press_comp >= (1 << 30))
press_comp = ((press_comp / (uint32_t)var1) << 1);
else
press_comp = ((press_comp << 1) / (uint32_t)var1);
var1 = ((int32_t)bme680->cal.par_p9 * (int32_t)(((press_comp >> 3) *
(press_comp >> 3)) >> 13)) >> 12;
var2 = ((int32_t)(press_comp >> 2) * (int32_t)bme680->cal.par_p8) >> 13;
var3 = ((int32_t)(press_comp >> 8) * (int32_t)(press_comp >> 8) *
(int32_t)(press_comp >> 8) * (int32_t)bme680->cal.par_p10) >> 17;
press_comp = (int32_t)(press_comp) + ((var1 + var2 + var3 + ((int32_t)bme680->cal.par_p7 << 7)) >> 4);
bme680->icomp.press = press_comp;
}
/********************************************************************/
static void calc_press_comp (bme680_t *bme680) {
if (BME680_IS_FLOAT(bme680->mode)) {
calc_press_comp_1(bme680);
} else {
calc_press_comp_2(bme680);
}
}
/********************************************************************/
static void calc_hum_comp_1 (bme680_t *bme680) {
double var1, var2, var3, var4, hum_comp, temp_comp;
temp_comp = bme680->fcomp.temp;
var1 = bme680->adc.hum - (((double)bme680->cal.par_h1 * 16.0) + (((double)bme680->cal.par_h3 / 2.0) * temp_comp));
var2 = var1 * (((double)bme680->cal.par_h2 / 262144.0) * (1.0 + (((double)bme680->cal.par_h4 / 16384.0) *
temp_comp) + (((double)bme680->cal.par_h5 / 1048576.0) * temp_comp * temp_comp)));
var3 = (double)bme680->cal.par_h6 / 16384.0;
var4 = (double)bme680->cal.par_h7 / 2097152.0;
hum_comp = var2 + ((var3 + (var4 * temp_comp)) * var2 * var2);
bme680->fcomp.hum = hum_comp;
}
/********************************************************************/
static void calc_hum_comp_2 (bme680_t *bme680) {
int32_t var1, var2, var3, var4, var5, var6, temp_scaled, hum_comp;
temp_scaled = (int32_t)bme680->icomp.temp;
var1 = (int32_t)bme680->adc.hum - (int32_t)((int32_t)bme680->cal.par_h1 << 4) -
(((temp_scaled * (int32_t)bme680->cal.par_h3) / ((int32_t)100)) >> 1);
var2 = ((int32_t)bme680->cal.par_h2 * (((temp_scaled * (int32_t)bme680->cal.par_h4) / ((int32_t)100)) +
(((temp_scaled * ((temp_scaled * (int32_t)bme680->cal.par_h5) /
((int32_t)100))) >> 6) / ((int32_t)100)) + ((int32_t)(1 << 14)))) >> 10;
var3 = var1 * var2;
var4 = (((int32_t)bme680->cal.par_h6 << 7) +
((temp_scaled * (int32_t)bme680->cal.par_h7) / ((int32_t)100))) >> 4;
var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
var6 = (var4 * var5) >> 1;
hum_comp = (((var3 + var6) >> 10) * ((int32_t) 1000)) >> 12;
bme680->icomp.hum = hum_comp;
}
/********************************************************************/
static void calc_hum_comp (bme680_t *bme680) {
if (BME680_IS_FLOAT(bme680->mode)) {
calc_hum_comp_1(bme680);
} else {
calc_hum_comp_2(bme680);
}
}
/********************************************************************/
static void calc_gas_res_1(bme680_t *bme680) {
double var1, gas_res;
var1 = (1340.0 + 5.0 * bme680->cal.range_switching_error) * const_array1[bme680->adc.gas_range];
gas_res = var1 * const_array2[bme680->adc.gas_range] / (bme680->adc.gas - 512.0 + var1);
bme680->fcomp.gas_res = gas_res;
}
/********************************************************************/
static void calc_gas_res_2(bme680_t *bme680) {
int64_t var1, var2;
int32_t gas_res;
var1 = (int64_t)(((1340 + (5 * (int64_t)bme680->cal.range_switching_error)) *
((int64_t)const_array1_int[bme680->adc.gas_range])) >> 16);
var2 = (int64_t)(bme680->adc.gas << 15) - (int64_t)(1 << 24) + var1;
gas_res = (int32_t)((((int64_t)(const_array2_int[bme680->adc.gas_range] *
(int64_t)var1) >> 9) + (var2 >> 1)) / var2);
bme680->icomp.gas_res = gas_res;
}
/********************************************************************/
static void calc_gas_res(bme680_t *bme680) {
if (BME680_IS_FLOAT(bme680->mode)) {
calc_gas_res_1(bme680);
} else {
calc_gas_res_2(bme680);
}
}
/********************************************************************/
static uint8_t calc_target_1(bme680_t *bme680, double target, double ambient) {
double var1, var2, var3, var4, var5;
uint8_t res_heat;
var1 = ((double)bme680->cal.par_g1 / 16.0) + 49.0;
var2 = (((double)bme680->cal.par_g2 / 32768.0) * 0.0005) + 0.00235;
var3 = (double)bme680->cal.par_g3 / 1024.0;
var4 = var1 * (1.0 + (var2 * (double)target));
var5 = var4 + (var3 * (double)ambient);
res_heat = (uint8_t)(3.4 * ((var5 * (4.0 / (4.0 + (double)bme680->cal.res_heat_range)) *
(1.0 / (1.0 + ((double)bme680->cal.res_heat_val * 0.002)))) - 25));
return res_heat;
}
/********************************************************************/
static uint8_t calc_target_2(bme680_t *bme680, double target, double ambient) {
int32_t var1, var2, var3, var4, var5, res_heat_x100;
uint8_t res_heat;
var1 = (((int32_t)ambient * bme680->cal.par_g3) / 10) << 8;
var2 = (bme680->cal.par_g1 + 784) * (((((bme680->cal.par_g2 + 154009) * target * 5) /
100) + 3276800) / 10);
var3 = var1 + (var2 >> 1);
var4 = (var3 / (bme680->cal.res_heat_range + 4));
var5 = (131 * bme680->cal.res_heat_val) + 65536;
res_heat_x100 = (int32_t)(((var4 / var5) - 250) * 34);
res_heat = (uint8_t)((res_heat_x100 + 50) / 100);
return res_heat;
}
/********************************************************************/
uint8_t bme680_calc_target(bme680_t *bme680, double target, double ambient) {
if (BME680_IS_FLOAT(bme680->mode)) {
return calc_target_1(bme680, target, ambient);
} else {
return calc_target_2(bme680, target, ambient);
}
}
#pragma GCC diagnostic pop
/********************************************************************/
/* TODO: read one big contiguous block */
int bme680_calibrate(bme680_t *bme680) {
uint8_t buffer[3] = {0, 0 ,0};
int err = 0;
/* temperature */
err |= read_dev(bme680, 0xE9, buffer, 2);
bme680->cal.par_t1 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0x8A, buffer, 2);
bme680->cal.par_t2 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0x8C, buffer, 1);
bme680->cal.par_t3 = (int8_t)buffer[0];
/* pressure */
err |= read_dev(bme680, 0x8E, buffer, 2);
bme680->cal.par_p1 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0x90, buffer, 2);
bme680->cal.par_p2 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0x92, buffer, 1);
bme680->cal.par_p3 = (int8_t)buffer[0];
err |= read_dev(bme680, 0x94, buffer, 2);
bme680->cal.par_p4 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0x96, buffer, 2);
bme680->cal.par_p5 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0x99, buffer, 1);
bme680->cal.par_p6 = (int8_t)buffer[0];
err |= read_dev(bme680, 0x98, buffer, 1);
bme680->cal.par_p7 = (int8_t)buffer[0];
err |= read_dev(bme680, 0x9C, buffer, 1);
bme680->cal.par_p8 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0x9E, buffer, 2);
bme680->cal.par_p9 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0xA0, buffer, 1);
bme680->cal.par_p10 = buffer[0];
/* humidity */
err |= read_dev(bme680, 0xE2, buffer, 2);
bme680->cal.par_h1 = (buffer[1] << 4) | (buffer[0] & 0xF);
err |= read_dev(bme680, 0xE1, buffer, 2);
bme680->cal.par_h2 = (buffer[0] << 4) | ((buffer[1] >> 4) & 0xF);
err |= read_dev(bme680, 0xE4, buffer, 1);
bme680->cal.par_h3 = (int8_t)buffer[0];
err |= read_dev(bme680, 0xE5, buffer, 1);
bme680->cal.par_h4 = (int8_t)buffer[0];
err |= read_dev(bme680, 0xE6, buffer, 1);
bme680->cal.par_h5 = (int8_t)buffer[0];
err |= read_dev(bme680, 0xE7, buffer, 1);
bme680->cal.par_h6 = buffer[0];
err |= read_dev(bme680, 0xE8, buffer, 1);
bme680->cal.par_h7 = (int8_t)buffer[0];
/* gas */
err |= read_dev(bme680, 0xED, buffer, 1);
bme680->cal.par_g1 = buffer[0];
err |= read_dev(bme680, 0xEB, buffer, 2);
bme680->cal.par_g2 = (buffer[1] << 8) | buffer[0];
err |= read_dev(bme680, 0xEE, buffer, 1);
bme680->cal.par_g3 = buffer[0];
err |= read_dev(bme680, 0x04, buffer, 1);
bme680->cal.range_switching_error = buffer[0];
err |= read_dev(bme680, 0x02, buffer, 1);
bme680->cal.res_heat_range = (buffer[0] >> 4) & 3;
err |= read_dev(bme680, 0x00, buffer, 1);
bme680->cal.res_heat_val = (int8_t)buffer[0];
return err;
}