LibDriver BME680
Loading...
Searching...
No Matches
driver_bme680.c
Go to the documentation of this file.
1
36
37#include "driver_bme680.h"
38
42#define CHIP_NAME "Bosch BME680"
43#define MANUFACTURER_NAME "Bosch"
44#define SUPPLY_VOLTAGE_MIN 1.71f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.849f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define BME680_REG_NVM_PAR_T1_L 0xE9
55#define BME680_REG_NVM_PAR_T1_H 0xEA
56#define BME680_REG_NVM_PAR_T2_L 0x8A
57#define BME680_REG_NVM_PAR_T2_H 0x8B
58#define BME680_REG_NVM_PAR_T3 0x8C
59#define BME680_REG_NVM_PAR_P1_L 0x8E
60#define BME680_REG_NVM_PAR_P1_H 0x8F
61#define BME680_REG_NVM_PAR_P2_L 0x90
62#define BME680_REG_NVM_PAR_P2_H 0x91
63#define BME680_REG_NVM_PAR_P3 0x92
64#define BME680_REG_NVM_PAR_P4_L 0x94
65#define BME680_REG_NVM_PAR_P4_H 0x95
66#define BME680_REG_NVM_PAR_P5_L 0x96
67#define BME680_REG_NVM_PAR_P5_H 0x97
68#define BME680_REG_NVM_PAR_P6 0x99
69#define BME680_REG_NVM_PAR_P7 0x98
70#define BME680_REG_NVM_PAR_P8_L 0x9C
71#define BME680_REG_NVM_PAR_P8_H 0x9D
72#define BME680_REG_NVM_PAR_P9_L 0x9E
73#define BME680_REG_NVM_PAR_P9_H 0x9F
74#define BME680_REG_NVM_PAR_P10 0xA0
75#define BME680_REG_NVM_PAR_H1_L 0xE2
76#define BME680_REG_NVM_PAR_H1_H 0xE3
77#define BME680_REG_NVM_PAR_H2_L 0xE2
78#define BME680_REG_NVM_PAR_H2_H 0xE1
79#define BME680_REG_NVM_PAR_H3 0xE4
80#define BME680_REG_NVM_PAR_H4 0xE5
81#define BME680_REG_NVM_PAR_H5 0xE6
82#define BME680_REG_NVM_PAR_H6 0xE7
83#define BME680_REG_NVM_PAR_H7 0xE8
84#define BME680_REG_NVM_PAR_G1 0xED
85#define BME680_REG_NVM_PAR_G2_L 0xEB
86#define BME680_REG_NVM_PAR_G2_H 0xEC
87#define BME680_REG_NVM_PAR_G3 0xEE
88#define BME680_REG_NVM_RES_HEAT_RANGE 0x02
89#define BME680_REG_NVM_RES_HEAT_VAL 0x00
90#define BME680_REG_NVM_RANGE_SWITCH 0x04
91#define BME680_REG_STATUS 0x73
92#define BME680_REG_RESET 0xE0
93#define BME680_REG_ID 0xD0
94#define BME680_REG_CONFIG 0x75
95#define BME680_REG_CTRL_MEAS 0x74
96#define BME680_REG_CTRL_HUM 0x72
97#define BME680_REG_CTRL_GAS_1 0x71
98#define BME680_REG_CTRL_GAS_0 0x70
99#define BME680_REG_GAS_WAIT_X 0x64
100#define BME680_REG_RES_HEAT_X 0x5A
101#define BME680_REG_IDAC_HEAT_X 0x50
102#define BME680_REG_GAS_R_LSB 0x2B
103#define BME680_REG_GAS_R_MSB 0x2A
104#define BME680_REG_HUM_LSB 0x26
105#define BME680_REG_HUM_MSB 0x25
106#define BME680_REG_TEMP_XLSB 0x24
107#define BME680_REG_TEMP_LSB 0x23
108#define BME680_REG_TEMP_MSB 0x22
109#define BME680_REG_PRESS_XLSB 0x21
110#define BME680_REG_PRESS_LSB 0x20
111#define BME680_REG_PRESS_MSB 0x1F
112#define BME680_REG_EAS_STATUS_0 0x1D
113
123static uint8_t a_bme680_change_spi_page(bme680_handle_t *handle, uint8_t page)
124{
125 uint8_t reg;
126 uint8_t buf[1];
127
128 reg = BME680_REG_STATUS; /* set address */
129 reg |= 1 << 7; /* set read mode */
130 if (handle->spi_read(reg, buf, 1) != 0) /* spi read */
131 {
132 return 1; /* return error */
133 }
134 if (page == 0) /* check page */
135 {
136 buf[0] &= ~(1 << 4); /* set page 0 */
137 }
138 else
139 {
140 buf[0] |= 1 << 4; /* set page 1 */
141 }
142 reg &= ~(1 << 7); /* write mode */
143 if (handle->spi_write(reg, buf, 1) != 0) /* spi write */
144 {
145 return 1; /* return error */
146 }
147 handle->page = page; /* save page */
148
149 return 0; /* success return 0 */
150}
151
163static uint8_t a_bme680_iic_spi_read(bme680_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
164{
165 if (handle->iic_spi == BME680_INTERFACE_IIC) /* iic interface */
166 {
167 if (handle->iic_read(handle->iic_addr, reg, buf, len) != 0) /* iic read */
168 {
169 return 1; /* return error */
170 }
171
172 return 0; /* success return 0 */
173 }
174 else /* spi interface */
175 {
176 if (reg > 0x7F) /* check address */
177 {
178 if (handle->page != 0) /* check page */
179 {
180 if (a_bme680_change_spi_page(handle, 0) != 0) /* change spi change */
181 {
182 return 1; /* return error */
183 }
184 }
185 reg -= 0x80; /* set address */
186 reg |= 1 << 7; /* set read mode */
187 if (handle->spi_read(reg, buf, len) != 0) /* spi read */
188 {
189 return 1; /* return error */
190 }
191 }
192 else
193 {
194 if (handle->page == 0) /* check page */
195 {
196 if (a_bme680_change_spi_page(handle, 1) != 0) /* change spi change */
197 {
198 return 1; /* return error */
199 }
200 }
201 reg |= 1 << 7; /* set read mode */
202 if (handle->spi_read(reg, buf, len) != 0) /* spi read */
203 {
204 return 1; /* return error */
205 }
206 }
207
208 return 0; /* success return 0 */
209 }
210}
211
223static uint8_t a_bme680_iic_spi_write(bme680_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
224{
225 if (handle->iic_spi == BME680_INTERFACE_IIC) /* iic interface */
226 {
227 if (handle->iic_write(handle->iic_addr, reg, buf, len) != 0) /* iic write */
228 {
229 return 1; /* return error */
230 }
231
232 return 0; /* success return 0 */
233 }
234 else /* spi interface */
235 {
236 if (reg > 0x7F) /* check address */
237 {
238 if (handle->page != 0) /* check page */
239 {
240 if (a_bme680_change_spi_page(handle, 0) != 0) /* change spi change */
241 {
242 return 1; /* return error */
243 }
244 }
245 reg -= 0x80; /* set address */
246 reg &= ~(1 << 7); /* write mode */
247 if (handle->spi_write(reg, buf, len) != 0) /* spi write */
248 {
249 return 1; /* return error */
250 }
251 }
252 else
253 {
254 if (handle->page == 0) /* check page */
255 {
256 if (a_bme680_change_spi_page(handle, 1) != 0) /* change spi change */
257 {
258 return 1; /* return error */
259 }
260 }
261 reg &= ~(1 << 7); /* write mode */
262 if (handle->spi_write(reg, buf, len) != 0) /* spi write */
263 {
264 return 1; /* return error */
265 }
266 }
267
268 return 0; /* success return 0 */
269 }
270}
271
280static uint8_t a_bme680_get_nvm_calibration(bme680_handle_t *handle)
281{
282 uint8_t reg;
283 uint8_t buf[2];
284
285 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_T1_L, (uint8_t *)buf, 2) != 0) /* read t1 */
286 {
287 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
288
289 return 1; /* return error */
290 }
291 handle->par_t1 = (uint16_t)buf[1] << 8 | buf[0]; /* set par t1 */
292 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_T2_L, (uint8_t *)buf, 2) != 0) /* read t2 */
293 {
294 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
295
296 return 1; /* return error */
297 }
298 handle->par_t2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par t2 */
299 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_T3, (uint8_t *)&reg, 1) != 0) /* read t3 */
300 {
301 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
302
303 return 1; /* return error */
304 }
305 handle->par_t3 = (int8_t)(reg); /* set par t3 */
306 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P1_L, (uint8_t *)buf, 2) != 0) /* read p1 */
307 {
308 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
309
310 return 1; /* return error */
311 }
312 handle->par_p1 = (uint16_t)buf[1] <<8 | buf[0]; /* set par p1 */
313 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P2_L, (uint8_t *)buf, 2) != 0) /* read p2 */
314 {
315 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
316
317 return 1; /* return error */
318 }
319 handle->par_p2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p2 */
320 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P3, (uint8_t *)&reg, 1) != 0) /* read p3 */
321 {
322 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
323
324 return 1; /* return error */
325 }
326 handle->par_p3 = (int8_t)(reg); /* set par p3 */
327 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P4_L, (uint8_t *)buf, 2) != 0) /* read p4 */
328 {
329 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
330
331 return 1; /* return error */
332 }
333 handle->par_p4 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p4 */
334 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P5_L, (uint8_t *)buf, 2) != 0) /* read p5 */
335 {
336 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
337
338 return 1; /* return error */
339 }
340 handle->par_p5 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p5 */
341 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P6, (uint8_t *)&reg, 1) != 0) /* read p6 */
342 {
343 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
344
345 return 1; /* return error */
346 }
347 handle->par_p6 = (int8_t)(reg); /* set par p6 */
348 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P7, (uint8_t *)&reg, 1) != 0) /* read p7 */
349 {
350 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
351
352 return 1; /* return error */
353 }
354 handle->par_p7 = (int8_t)(reg); /* set par p7 */
355 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P8_L, (uint8_t *)buf, 2) != 0) /* read p8 */
356 {
357 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
358
359 return 1; /* return error */
360 }
361 handle->par_p8 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p8 */
362 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P9_L, (uint8_t *)buf, 2) != 0) /* read p9 */
363 {
364 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
365
366 return 1; /* return error */
367 }
368 handle->par_p9 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p9 */
369 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_P10, (uint8_t *)&reg, 1) != 0) /* read p10 */
370 {
371 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
372
373 return 1; /* return error */
374 }
375 handle->par_p10 = (uint8_t)(reg); /* set par p10 */
376 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_H1_L, (uint8_t *)buf, 2) != 0) /* read h1 */
377 {
378 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
379
380 return 1; /* return error */
381 }
382 handle->par_h1 = (uint16_t)((uint16_t)buf[1] << 4 | buf[0] & 0xF); /* set h1 */
383 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_H2_H, (uint8_t *)buf, 2) != 0) /* read h2 */
384 {
385 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
386
387 return 1; /* return error */
388 }
389 handle->par_h2 = (uint16_t)((uint16_t)buf[0] << 4 | ((buf[1] >> 4) & 0xF)); /* set h2 */
390 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_H3, (uint8_t *)&reg, 1) != 0) /* read h3 */
391 {
392 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
393
394 return 1; /* return error */
395 }
396 handle->par_h3 = (int8_t)reg; /* set h3 */
397 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_H4, (uint8_t *)&reg, 1) != 0) /* read h4 */
398 {
399 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
400
401 return 1; /* return error */
402 }
403 handle->par_h4 = (int8_t)reg; /* set h4 */
404 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_H5, (uint8_t *)&reg, 1) != 0) /* read h5 */
405 {
406 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
407
408 return 1; /* return error */
409 }
410 handle->par_h5 = (int8_t)reg; /* set h5 */
411 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_H6, (uint8_t *)&reg, 1) != 0) /* read h6 */
412 {
413 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
414
415 return 1; /* return error */
416 }
417 handle->par_h6 = (int8_t)reg; /* set h6 */
418 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_H7, (uint8_t *)&reg, 1) != 0) /* read h7 */
419 {
420 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
421
422 return 1; /* return error */
423 }
424 handle->par_h7 = (int8_t)reg; /* set h7 */
425 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_G1, (uint8_t *)&reg, 1) != 0) /* read gh1 */
426 {
427 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
428
429 return 1; /* return error */
430 }
431 handle->par_gh1 = (int8_t)reg; /* set par gh1 */
432 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_G2_L, (uint8_t *)buf, 2) != 0) /* read gh2 */
433 {
434 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
435
436 return 1; /* return error */
437 }
438 handle->par_gh2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par gh2 */
439 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_PAR_G3, (uint8_t *)&reg, 1) != 0) /* read gh3 */
440 {
441 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
442
443 return 1; /* return error */
444 }
445 handle->par_gh3 = (int8_t)reg; /* set par gh3 */
446 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_RES_HEAT_RANGE, (uint8_t *)&reg, 1) != 0) /* read res heat range */
447 {
448 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
449
450 return 1; /* return error */
451 }
452 handle->res_heat_range = (uint8_t)((reg >> 4) & 3); /* set res heat range */
453 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_RES_HEAT_VAL, (uint8_t *)&reg, 1) != 0) /* read res heat val */
454 {
455 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
456
457 return 1; /* return error */
458 }
459 handle->res_heat_val = (int8_t)(reg); /* set res heat val */
460 if (a_bme680_iic_spi_read(handle, BME680_REG_NVM_RANGE_SWITCH, (uint8_t *)&reg, 1) != 0) /* read range switch error */
461 {
462 handle->debug_print("bme680: get calibration data failed.\n"); /* get calibration data failed */
463
464 return 1; /* return error */
465 }
466 handle->range_sw_err = (int8_t)((reg >> 4) & 0xF); /* set range switch error */
467 handle->t_fine = 0.0f; /* init 0 */
468 handle->amb_temp = 25; /* set 25c */
469
470 return 0; /* success return 0 */
471}
472
483static uint8_t a_bme680_compensate_temperature(bme680_handle_t *handle, uint32_t raw, float *output)
484{
485 float var1;
486 float var2;
487
488 var1 = ((((float)raw / 16384.0f) - ((float)handle->par_t1 / 1024.0f)) * ((float)handle->par_t2)); /* calculate var1 data */
489 var2 = (((((float)raw / 131072.0f) - ((float)handle->par_t1 / 8192.0f)) *
490 (((float)raw / 131072.0f) - ((float)handle->par_t1 / 8192.0f))) * ((float)handle->par_t3 * 16.0f)); /* calculate var2 data */
491 handle->t_fine = (var1 + var2); /* t_fine value */
492 *output = ((handle->t_fine) / 5120.0f); /* compensated temperature data */
493
494 return 0; /* success return 0 */
495}
496
507static uint8_t a_bme680_compensate_pressure(bme680_handle_t *handle, uint32_t raw, float *output)
508{
509 float var1;
510 float var2;
511 float var3;
512 float calc_pres;
513
514 var1 = (((float)handle->t_fine / 2.0f) - 64000.0f); /* set var1 */
515 var2 = var1 * var1 * (((float)handle->par_p6) / (131072.0f)); /* set var2 */
516 var2 = var2 + (var1 * ((float)handle->par_p5) * 2.0f); /* set var2 */
517 var2 = (var2 / 4.0f) + (((float)handle->par_p4) * 65536.0f); /* set var2 */
518 var1 = (((((float)handle->par_p3 * var1 * var1) / 16384.0f) +
519 ((float)handle->par_p2 * var1)) / 524288.0f); /* set var1 */
520 var1 = ((1.0f + (var1 / 32768.0f)) * ((float)handle->par_p1)); /* set var1 */
521 calc_pres = (1048576.0f - ((float)raw)); /* set pressure */
522 if ((int32_t)var1 != 0) /* avoid exception caused by division by zero */
523 {
524 calc_pres = (((calc_pres - (var2 / 4096.0f)) * 6250.0f) / var1); /* set pressure */
525 var1 = (((float)handle->par_p9) * calc_pres * calc_pres) / 2147483648.0f; /* set var1 */
526 var2 = calc_pres * (((float)handle->par_p8) / 32768.0f); /* set var2 */
527 var3 = ((calc_pres / 256.0f) * (calc_pres / 256.0f) * (calc_pres / 256.0f) *
528 ((float)handle->par_p10 / 131072.0f)); /* set var3 */
529 calc_pres = (calc_pres + (var1 + var2 + var3 + ((float)handle->par_p7 * 128.0f)) / 16.0f); /* set pressure */
530 }
531 else
532 {
533 calc_pres = 0.0f; /* set 0 */
534 }
535 *output= calc_pres; /* save to output */
536
537 return 0; /* success return 0 */
538}
539
550static uint8_t a_bme680_compensate_humidity(bme680_handle_t *handle, uint16_t raw, float *output)
551{
552 float calc_hum;
553 float var1;
554 float var2;
555 float var3;
556 float var4;
557 float temp_comp;
558
559 temp_comp = ((handle->t_fine) / 5120.0f); /* compensated temperature data*/
560 var1 = (float)((float)raw) -
561 (((float)handle->par_h1 * 16.0f) + (((float)handle->par_h3 / 2.0f) * temp_comp)); /* set var1 */
562 var2 = var1 * ((float)(((float)handle->par_h2 / 262144.0f) *
563 (1.0f + (((float)handle->par_h4 / 16384.0f) * temp_comp) +
564 (((float)handle->par_h5 / 1048576.0f) * temp_comp * temp_comp)))); /* set var2 */
565 var3 = (float)handle->par_h6 / 16384.0f; /* set var3 */
566 var4 = (float)handle->par_h7 / 2097152.0f; /* set var4 */
567 calc_hum = var2 + ((var3 + (var4 * temp_comp)) * var2 * var2); /* set humidity */
568 if (calc_hum > 100.0f) /* check max range */
569 {
570 calc_hum = 100.0f; /* set max */
571 }
572 else if (calc_hum < 0.0f) /* check min range */
573 {
574 calc_hum = 0.0f; /* set min */
575 }
576 else
577 {
578 /* do nothing */
579 }
580 *output = calc_hum; /* save to output */
581
582 return 0; /* success return 0 */
583}
584
595static uint8_t a_bme680_compensate_heat(bme680_handle_t *handle, float temp, uint8_t *reg)
596{
597 float var1;
598 float var2;
599 float var3;
600 float var4;
601 float var5;
602 uint8_t res_heat;
603
604 if (temp > 400.0f) /* cap temperature */
605 {
606 temp = 400.0f; /* set 400 */
607 }
608 var1 = (((float)handle->par_gh1 / (16.0f)) + 49.0f); /* set var1 */
609 var2 = ((((float)handle->par_gh2 / (32768.0f)) * (0.0005f)) + 0.00235f); /* set var2 */
610 var3 = ((float)handle->par_gh3 / (1024.0f)); /* set var3 */
611 var4 = (var1 * (1.0f + (var2 * (float)temp))); /* set var4 */
612 var5 = (var4 + (var3 * (float)handle->amb_temp)); /* set var5 */
613 res_heat = (uint8_t)(3.4f * ((var5 * (4 / (4 + (float)handle->res_heat_range)) *
614 (1 / (1 + ((float)handle->res_heat_val * 0.002f)))) - 25)); /* set res heat */
615 *reg =res_heat; /* set res */
616
617 return 0; /* success return 0 */
618}
619
631static uint8_t a_bme680_compensate_gas_resistance(bme680_handle_t *handle, uint16_t gas_res_adc, uint8_t gas_range, float *ohms)
632{
633 float calc_gas_res;
634 float var1;
635 float var2;
636 float var3;
637 float gas_res_f = (float)gas_res_adc;
638 float gas_range_f = (float)(1U << gas_range);
639 const float lookup_k1_range[16] =
640 {
641 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, -0.8f, 0.0f, 0.0f, -0.2f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f
642 };
643 const float lookup_k2_range[16] =
644 {
645 0.0f, 0.0f, 0.0f, 0.0f, 0.1f, 0.7f, 0.0f, -0.8f, -0.1f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
646 };
647
648 var1 = (1340.0f + (5.0f * handle->range_sw_err)); /* set var1 */
649 var2 = (var1) * (1.0f + lookup_k1_range[gas_range] / 100.0f); /* set var2 */
650 var3 = 1.0f + (lookup_k2_range[gas_range] / 100.0f); /* set var3 */
651 calc_gas_res = 1.0f / (float)(var3 * (0.000000125f) * gas_range_f *
652 (((gas_res_f - 512.0f) / var2) + 1.0f)); /* set gas resistance */
653 *ohms = calc_gas_res; /* save gas resistance to ohms */
654
655 return 0; /* success return 0 */
656}
657
668{
669 if (handle == NULL) /* check handle */
670 {
671 return 2; /* return error */
672 }
673
674 handle->iic_addr = (uint8_t)addr_pin; /* set iic address */
675
676 return 0; /* success return 0 */
677}
678
689{
690 if (handle == NULL) /* check handle */
691 {
692 return 2; /* return error */
693 }
694
695 *addr_pin = (bme680_address_t)handle->iic_addr; /* get iic address */
696
697 return 0; /* success return 0 */
698}
699
710{
711 if (handle == NULL) /* check handle */
712 {
713 return 2; /* return error */
714 }
715
716 handle->iic_spi = (uint8_t)interface; /* set interface */
717
718 return 0; /* success return 0 */
719}
720
731{
732 if (handle == NULL) /* check handle */
733 {
734 return 2; /* return error */
735 }
736
737 *interface = (bme680_interface_t)(handle->iic_spi); /* get interface */
738
739 return 0; /* success return 0 */
740}
741
756{
757 uint8_t id;
758 uint8_t reg;
759
760 if (handle == NULL) /* check handle */
761 {
762 return 2; /* return error */
763 }
764 if (handle->debug_print == NULL) /* check debug_print */
765 {
766 return 3; /* return error */
767 }
768 if (handle->iic_init == NULL) /* check iic_init */
769 {
770 handle->debug_print("bme680: iic_init is null.\n"); /* iic_init is nul */
771
772 return 3; /* return error */
773 }
774 if (handle->iic_deinit == NULL) /* check iic_deinit */
775 {
776 handle->debug_print("bme680: iic_deinit is null.\n"); /* iic_deinit is null */
777
778 return 3; /* return error */
779 }
780 if (handle->iic_read == NULL) /* check iic_read */
781 {
782 handle->debug_print("bme680: iic_read is null.\n"); /* iic_read is null */
783
784 return 3; /* return error */
785 }
786 if (handle->iic_write == NULL) /* check iic_write */
787 {
788 handle->debug_print("bme680: iic_write is null.\n"); /* iic_write is null */
789
790 return 3; /* return error */
791 }
792 if (handle->spi_init == NULL) /* check spi_init */
793 {
794 handle->debug_print("bme680: spi_init is null.\n"); /* spi_init is nul */
795
796 return 3; /* return error */
797 }
798 if (handle->spi_deinit == NULL) /* check spi_deinit */
799 {
800 handle->debug_print("bme680: spi_deinit is null.\n"); /* spi_deinit is nul */
801
802 return 3; /* return error */
803 }
804 if (handle->spi_read == NULL) /* check spi_read */
805 {
806 handle->debug_print("bme680: spi_read is null.\n"); /* spi_read is nul */
807
808 return 3; /* return error */
809 }
810 if (handle->spi_write == NULL) /* check spi_write */
811 {
812 handle->debug_print("bme680: spi_write is null.\n"); /* spi_write is nul */
813
814 return 3; /* return error */
815 }
816 if (handle->delay_ms == NULL) /* check delay_ms */
817 {
818 handle->debug_print("bme680: delay_ms is null.\n"); /* delay_ms is null */
819
820 return 3; /* return error */
821 }
822
823 if (handle->iic_spi == BME680_INTERFACE_IIC) /* iic interface */
824 {
825 if (handle->iic_init() != 0) /* iic init */
826 {
827 handle->debug_print("bme680: iic init failed.\n"); /* iic init failed */
828
829 return 1; /* return error */
830 }
831 }
832 else /* spi interface */
833 {
834 if (handle->spi_init() != 0) /* spi init */
835 {
836 handle->debug_print("bme680: spi init failed.\n"); /* spi init failed */
837
838 return 1; /* return error */
839 }
840 if (a_bme680_change_spi_page(handle, 0) != 0) /* change spi page 0 */
841 {
842 handle->debug_print("bme680: change spi page failed.\n"); /* change spi page failed */
843
844 return 1; /* return error */
845 }
846 }
847
848 if (a_bme680_iic_spi_read(handle, BME680_REG_ID, (uint8_t *)&id, 1) != 0) /* read chip id */
849 {
850 handle->debug_print("bme680: read id failed.\n"); /* read id failed */
851 (void)handle->iic_deinit(); /* iic deinit */
852
853 return 4; /* return error */
854 }
855 if (id != 0x61) /* check id */
856 {
857 handle->debug_print("bme680: id is error.\n"); /* id is error */
858 (void)handle->iic_deinit(); /* iic deinit */
859
860 return 4; /* return error */
861 }
862 reg = 0xB6; /* set the reset value */
863 if (a_bme680_iic_spi_write(handle, BME680_REG_RESET, &reg, 1) != 0) /* reset the chip */
864 {
865 handle->debug_print("bme680: reset failed.\n"); /* reset failed */
866 (void)handle->iic_deinit(); /* iic deinit */
867
868 return 5; /* return error */
869 }
870 handle->delay_ms(5); /* delay 5ms */
871 if (a_bme680_get_nvm_calibration(handle) != 0) /* get nvm calibration */
872 {
873 (void)handle->iic_deinit(); /* iic deinit */
874
875 return 6; /* return error */
876 }
877 handle->inited = 1; /* flag finish initialization */
878
879 return 0; /* success return 0 */
880}
881
894{
895 uint8_t prev;
896
897 if (handle == NULL) /* check handle */
898 {
899 return 2; /* return error */
900 }
901 if (handle->inited != 1) /* check handle initialization */
902 {
903 return 3; /* return error */
904 }
905
906 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
907 {
908 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
909
910 return 4; /* return error */
911 }
912 prev &= ~(3 << 0); /* clear settings */
913 prev |= 0 << 0; /* set sleep mode */
914 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
915 {
916 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
917
918 return 4; /* return error */
919 }
920 if (handle->iic_spi == BME680_INTERFACE_IIC) /* iic interface */
921 {
922 if (handle->iic_deinit() != 0) /* iic deinit */
923 {
924 handle->debug_print("bme680: iic deinit failed.\n"); /* iic deinit failed */
925
926 return 1; /* return error */
927 }
928 }
929 else /* spi interface */
930 {
931 if (handle->spi_deinit() != 0) /* spi deinit */
932 {
933 handle->debug_print("bme680: spi deinit failed.\n"); /* spi deinit failed */
934
935 return 1; /* return error */
936 }
937 }
938 handle->inited = 0; /* flag close */
939
940 return 0; /* success return 0 */
941}
942
956uint8_t bme680_set_idac_heat(bme680_handle_t *handle, uint8_t index, uint8_t reg)
957{
958 uint8_t prev;
959
960 if (handle == NULL) /* check handle */
961 {
962 return 2; /* return error */
963 }
964 if (handle->inited != 1) /* check handle initialization */
965 {
966 return 3; /* return error */
967 }
968 if (index > 9) /* check index */
969 {
970 handle->debug_print("bme680: index > 9.\n"); /* index > 9 */
971
972 return 4; /* return error */
973 }
974
975 prev = reg;
976 if (a_bme680_iic_spi_write(handle, BME680_REG_IDAC_HEAT_X + index, &prev, 1) != 0) /* write idac heat */
977 {
978 handle->debug_print("bme680: write idac heat failed.\n"); /* write idac heat failed */
979
980 return 1; /* return error */
981 }
982
983 return 0; /* success return 0 */
984}
985
999uint8_t bme680_get_idac_heat(bme680_handle_t *handle, uint8_t index, uint8_t *reg)
1000{
1001 if (handle == NULL) /* check handle */
1002 {
1003 return 2; /* return error */
1004 }
1005 if (handle->inited != 1) /* check handle initialization */
1006 {
1007 return 3; /* return error */
1008 }
1009 if (index > 9) /* check index */
1010 {
1011 handle->debug_print("bme680: index > 9.\n"); /* index > 9 */
1012
1013 return 4; /* return error */
1014 }
1015
1016 if (a_bme680_iic_spi_read(handle, BME680_REG_IDAC_HEAT_X + index, reg, 1) != 0) /* read idac heat */
1017 {
1018 handle->debug_print("bme680: read idac heat failed.\n"); /* read idac heat failed */
1019
1020 return 1; /* return error */
1021 }
1022
1023 return 0; /* success return 0 */
1024}
1025
1039uint8_t bme680_set_resistance_heat(bme680_handle_t *handle, uint8_t index, uint8_t reg)
1040{
1041 uint8_t prev;
1042
1043 if (handle == NULL) /* check handle */
1044 {
1045 return 2; /* return error */
1046 }
1047 if (handle->inited != 1) /* check handle initialization */
1048 {
1049 return 3; /* return error */
1050 }
1051 if (index > 9) /* check index */
1052 {
1053 handle->debug_print("bme680: index > 9.\n"); /* index > 9 */
1054
1055 return 4; /* return error */
1056 }
1057
1058 prev = reg;
1059 if (a_bme680_iic_spi_write(handle, BME680_REG_RES_HEAT_X + index, &prev, 1) != 0) /* write resistance heat */
1060 {
1061 handle->debug_print("bme680: write resistance heat failed.\n"); /* write resistance heat failed */
1062
1063 return 1; /* return error */
1064 }
1065
1066 return 0; /* success return 0 */
1067}
1068
1082uint8_t bme680_get_resistance_heat(bme680_handle_t *handle, uint8_t index, uint8_t *reg)
1083{
1084 if (handle == NULL) /* check handle */
1085 {
1086 return 2; /* return error */
1087 }
1088 if (handle->inited != 1) /* check handle initialization */
1089 {
1090 return 3; /* return error */
1091 }
1092 if (index > 9) /* check index */
1093 {
1094 handle->debug_print("bme680: index > 9.\n"); /* index > 9 */
1095
1096 return 4; /* return error */
1097 }
1098
1099 if (a_bme680_iic_spi_read(handle, BME680_REG_RES_HEAT_X + index, reg, 1) != 0) /* read resistance heat */
1100 {
1101 handle->debug_print("bme680: read resistance heat failed.\n"); /* read resistance heat failed */
1102
1103 return 1; /* return error */
1104 }
1105
1106 return 0; /* success return 0 */
1107}
1108
1122uint8_t bme680_set_gas_wait(bme680_handle_t *handle, uint8_t index, uint8_t reg)
1123{
1124 uint8_t prev;
1125
1126 if (handle == NULL) /* check handle */
1127 {
1128 return 2; /* return error */
1129 }
1130 if (handle->inited != 1) /* check handle initialization */
1131 {
1132 return 3; /* return error */
1133 }
1134 if (index > 9) /* check index */
1135 {
1136 handle->debug_print("bme680: index > 9.\n"); /* index > 9 */
1137
1138 return 4; /* return error */
1139 }
1140
1141 prev = reg;
1142 if (a_bme680_iic_spi_write(handle, BME680_REG_GAS_WAIT_X + index, &prev, 1) != 0) /* write gas wait */
1143 {
1144 handle->debug_print("bme680: write gas wait failed.\n"); /* write gas wait failed */
1145
1146 return 1; /* return error */
1147 }
1148
1149 return 0; /* success return 0 */
1150}
1151
1165uint8_t bme680_get_gas_wait(bme680_handle_t *handle, uint8_t index, uint8_t *reg)
1166{
1167 if (handle == NULL) /* check handle */
1168 {
1169 return 2; /* return error */
1170 }
1171 if (handle->inited != 1) /* check handle initialization */
1172 {
1173 return 3; /* return error */
1174 }
1175 if (index > 9) /* check index */
1176 {
1177 handle->debug_print("bme680: index > 9.\n"); /* index > 9 */
1178
1179 return 4; /* return error */
1180 }
1181
1182 if (a_bme680_iic_spi_read(handle, BME680_REG_GAS_WAIT_X + index, reg, 1) != 0) /* read gas wait */
1183 {
1184 handle->debug_print("bme680: read gas wait failed.\n"); /* read gas wait failed */
1185
1186 return 1; /* return error */
1187 }
1188
1189 return 0; /* success return 0 */
1190}
1191
1203{
1204 uint8_t reg;
1205
1206 if (handle == NULL) /* check handle */
1207 {
1208 return 2; /* return error */
1209 }
1210 if (handle->inited != 1) /* check handle initialization */
1211 {
1212 return 3; /* return error */
1213 }
1214
1215 reg = 0xB6; /* set the reset value */
1216 if (a_bme680_iic_spi_write(handle, BME680_REG_RESET, &reg, 1) != 0) /* reset the chip */
1217 {
1218 handle->debug_print("bme680: reset failed.\n"); /* reset failed */
1219
1220 return 1; /* return error */
1221 }
1222 handle->delay_ms(5); /* delay 5ms */
1223
1224 return 0; /* success return 0 */
1225}
1226
1239{
1240 uint8_t prev;
1241
1242 if (handle == NULL) /* check handle */
1243 {
1244 return 2; /* return error */
1245 }
1246 if (handle->inited != 1) /* check handle initialization */
1247 {
1248 return 3; /* return error */
1249 }
1250
1251 if (a_bme680_iic_spi_read(handle, BME680_REG_EAS_STATUS_0, &prev, 1) != 0) /* read status */
1252 {
1253 handle->debug_print("bme680: read status failed.\n"); /* read status failed */
1254
1255 return 1; /* return error */
1256 }
1257 *enable = (bme680_bool_t)((prev >> 7) & 0x01); /* set bool */
1258
1259 return 0; /* success return 0 */
1260}
1261
1274{
1275 uint8_t prev;
1276
1277 if (handle == NULL) /* check handle */
1278 {
1279 return 2; /* return error */
1280 }
1281 if (handle->inited != 1) /* check handle initialization */
1282 {
1283 return 3; /* return error */
1284 }
1285
1286 if (a_bme680_iic_spi_read(handle, BME680_REG_EAS_STATUS_0, &prev, 1) != 0) /* read status */
1287 {
1288 handle->debug_print("bme680: read status failed.\n"); /* read status failed */
1289
1290 return 1; /* return error */
1291 }
1292 *enable = (bme680_bool_t)((prev >> 6) & 0x01); /* set bool */
1293
1294 return 0; /* success return 0 */
1295}
1296
1309{
1310 uint8_t prev;
1311
1312 if (handle == NULL) /* check handle */
1313 {
1314 return 2; /* return error */
1315 }
1316 if (handle->inited != 1) /* check handle initialization */
1317 {
1318 return 3; /* return error */
1319 }
1320
1321 if (a_bme680_iic_spi_read(handle, BME680_REG_EAS_STATUS_0, &prev, 1) != 0) /* read status */
1322 {
1323 handle->debug_print("bme680: read status failed.\n"); /* read status failed */
1324
1325 return 1; /* return error */
1326 }
1327 *enable = (bme680_bool_t)((prev >> 5) & 0x01); /* set bool */
1328
1329 return 0; /* success return 0 */
1330}
1331
1343uint8_t bme680_get_gas_measuring_index(bme680_handle_t *handle, uint8_t *index)
1344{
1345 uint8_t prev;
1346
1347 if (handle == NULL) /* check handle */
1348 {
1349 return 2; /* return error */
1350 }
1351 if (handle->inited != 1) /* check handle initialization */
1352 {
1353 return 3; /* return error */
1354 }
1355
1356 if (a_bme680_iic_spi_read(handle, BME680_REG_EAS_STATUS_0, &prev, 1) != 0) /* read status */
1357 {
1358 handle->debug_print("bme680: read status failed.\n"); /* read status failed */
1359
1360 return 1; /* return error */
1361 }
1362 *index = (prev & 0xF); /* set index */
1363
1364 return 0; /* success return 0 */
1365}
1366
1379{
1380 uint8_t prev;
1381
1382 if (handle == NULL) /* check handle */
1383 {
1384 return 2; /* return error */
1385 }
1386 if (handle->inited != 1) /* check handle initialization */
1387 {
1388 return 3; /* return error */
1389 }
1390
1391 if (a_bme680_iic_spi_read(handle, BME680_REG_GAS_R_LSB, &prev, 1) != 0) /* read gas lsb */
1392 {
1393 handle->debug_print("bme680: read gas lsb failed.\n"); /* read gas lsb failed */
1394
1395 return 1; /* return error */
1396 }
1397 *enable = (bme680_bool_t)((prev >> 5) & 0x01); /* set bool */
1398
1399 return 0; /* success return 0 */
1400}
1401
1414{
1415 uint8_t prev;
1416
1417 if (handle == NULL) /* check handle */
1418 {
1419 return 2; /* return error */
1420 }
1421 if (handle->inited != 1) /* check handle initialization */
1422 {
1423 return 3; /* return error */
1424 }
1425
1426 if (a_bme680_iic_spi_read(handle, BME680_REG_GAS_R_LSB, &prev, 1) != 0) /* read gas lsb */
1427 {
1428 handle->debug_print("bme680: read gas lsb failed.\n"); /* read gas lsb failed */
1429
1430 return 1; /* return error */
1431 }
1432 *enable = (bme680_bool_t)((prev >> 4) & 0x01); /* set bool */
1433
1434 return 0; /* success return 0 */
1435}
1436
1449{
1450 uint8_t prev;
1451
1452 if (handle == NULL) /* check handle */
1453 {
1454 return 2; /* return error */
1455 }
1456 if (handle->inited != 1) /* check handle initialization */
1457 {
1458 return 3; /* return error */
1459 }
1460
1461 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1462 {
1463 handle->debug_print("bme680: read ctrl hum failed.\n"); /* read ctrl hum failed */
1464
1465 return 1; /* return error */
1466 }
1467 prev &= ~(1 << 6); /* clear settings */
1468 prev |= enable << 6; /* set bool */
1469 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_HUM, &prev, 1) != 0) /* write ctrl hum */
1470 {
1471 handle->debug_print("bme680: write ctrl hum failed.\n"); /* write ctrl hum failed */
1472
1473 return 1; /* return error */
1474 }
1475
1476 return 0; /* success return 0 */
1477}
1478
1491{
1492 uint8_t prev;
1493
1494 if (handle == NULL) /* check handle */
1495 {
1496 return 2; /* return error */
1497 }
1498 if (handle->inited != 1) /* check handle initialization */
1499 {
1500 return 3; /* return error */
1501 }
1502
1503 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1504 {
1505 handle->debug_print("bme680: read ctrl hum failed.\n"); /* read ctrl hum failed */
1506
1507 return 1; /* return error */
1508 }
1509 *enable = (bme680_bool_t)((prev >> 6) & 0x01); /* get bool */
1510
1511 return 0; /* success return 0 */
1512}
1513
1526{
1527 uint8_t prev;
1528
1529 if (handle == NULL) /* check handle */
1530 {
1531 return 2; /* return error */
1532 }
1533 if (handle->inited != 1) /* check handle initialization */
1534 {
1535 return 3; /* return error */
1536 }
1537
1538 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1539 {
1540 handle->debug_print("bme680: read ctrl hum failed.\n"); /* read ctrl hum failed */
1541
1542 return 1; /* return error */
1543 }
1544 prev &= ~(7 << 0); /* clear settings */
1545 prev |= oversampling << 0; /* set oversampling */
1546 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_HUM, &prev, 1) != 0) /* write ctrl hum */
1547 {
1548 handle->debug_print("bme680: write ctrl hum failed.\n"); /* write ctrl hum failed */
1549
1550 return 1; /* return error */
1551 }
1552
1553 return 0; /* success return 0 */
1554}
1555
1568{
1569 uint8_t prev;
1570
1571 if (handle == NULL) /* check handle */
1572 {
1573 return 2; /* return error */
1574 }
1575 if (handle->inited != 1) /* check handle initialization */
1576 {
1577 return 3; /* return error */
1578 }
1579
1580 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1581 {
1582 handle->debug_print("bme680: read ctrl hum failed.\n"); /* read ctrl hum failed */
1583
1584 return 1; /* return error */
1585 }
1586 *oversampling = (bme680_oversampling_t)(prev & 0x07); /* get oversampling */
1587
1588 return 0; /* success return 0 */
1589}
1590
1603{
1604 uint8_t prev;
1605
1606 if (handle == NULL) /* check handle */
1607 {
1608 return 2; /* return error */
1609 }
1610 if (handle->inited != 1) /* check handle initialization */
1611 {
1612 return 3; /* return error */
1613 }
1614
1615 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1616 {
1617 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
1618
1619 return 1; /* return error */
1620 }
1621 prev &= ~(7 << 5); /* clear settings */
1622 prev |= oversampling << 5; /* set oversampling */
1623 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
1624 {
1625 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
1626
1627 return 1; /* return error */
1628 }
1629
1630 return 0; /* success return 0 */
1631}
1632
1645{
1646 uint8_t prev;
1647
1648 if (handle == NULL) /* check handle */
1649 {
1650 return 2; /* return error */
1651 }
1652 if (handle->inited != 1) /* check handle initialization */
1653 {
1654 return 3; /* return error */
1655 }
1656
1657 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1658 {
1659 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
1660
1661 return 1; /* return error */
1662 }
1663 *oversampling = (bme680_oversampling_t)((prev >> 5) & 0x7); /* set oversampling */
1664
1665 return 0; /* success return 0 */
1666}
1667
1680{
1681 uint8_t prev;
1682
1683 if (handle == NULL) /* check handle */
1684 {
1685 return 2; /* return error */
1686 }
1687 if (handle->inited != 1) /* check handle initialization */
1688 {
1689 return 3; /* return error */
1690 }
1691
1692 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1693 {
1694 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
1695
1696 return 1; /* return error */
1697 }
1698 prev &= ~(7 << 2); /* clear settings */
1699 prev |= oversampling << 2; /* set oversampling */
1700 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
1701 {
1702 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
1703
1704 return 1; /* return error */
1705 }
1706
1707 return 0; /* success return 0 */
1708}
1709
1722{
1723 uint8_t prev;
1724
1725 if (handle == NULL) /* check handle */
1726 {
1727 return 2; /* return error */
1728 }
1729 if (handle->inited != 1) /* check handle initialization */
1730 {
1731 return 3; /* return error */
1732 }
1733
1734 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1735 {
1736 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
1737
1738 return 1; /* return error */
1739 }
1740 *oversampling = (bme680_oversampling_t)((prev >> 2) & 0x7); /* set oversampling */
1741
1742 return 0; /* success return 0 */
1743}
1744
1757{
1758 uint8_t prev;
1759
1760 if (handle == NULL) /* check handle */
1761 {
1762 return 2; /* return error */
1763 }
1764 if (handle->inited != 1) /* check handle initialization */
1765 {
1766 return 3; /* return error */
1767 }
1768
1769 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1770 {
1771 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
1772
1773 return 1; /* return error */
1774 }
1775 prev &= ~(3 << 0); /* clear settings */
1776 prev |= mode << 0; /* set mode */
1777 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
1778 {
1779 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
1780
1781 return 1; /* return error */
1782 }
1783
1784 return 0; /* success return 0 */
1785}
1786
1799{
1800 uint8_t prev;
1801
1802 if (handle == NULL) /* check handle */
1803 {
1804 return 2; /* return error */
1805 }
1806 if (handle->inited != 1) /* check handle initialization */
1807 {
1808 return 3; /* return error */
1809 }
1810
1811 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1812 {
1813 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
1814
1815 return 1; /* return error */
1816 }
1817 *mode = (bme680_mode_t)((prev >> 0) & 0x3); /* set mode */
1818
1819 return 0; /* success return 0 */
1820}
1821
1834{
1835 uint8_t prev;
1836
1837 if (handle == NULL) /* check handle */
1838 {
1839 return 2; /* return error */
1840 }
1841 if (handle->inited != 1) /* check handle initialization */
1842 {
1843 return 3; /* return error */
1844 }
1845
1846 if (a_bme680_iic_spi_read(handle, BME680_REG_CONFIG, &prev, 1) != 0) /* read config */
1847 {
1848 handle->debug_print("bme680: read config failed.\n"); /* read config failed */
1849
1850 return 1; /* return error */
1851 }
1852 prev &= ~(7 << 2); /* clear settings */
1853 prev |= (filter & 0x07) << 2; /* set filter */
1854 if (a_bme680_iic_spi_write(handle, BME680_REG_CONFIG, &prev, 1) != 0) /* write config */
1855 {
1856 handle->debug_print("bme680: write config failed.\n"); /* write config failed */
1857
1858 return 1; /* return error */
1859 }
1860
1861 return 0; /* success return 0 */
1862}
1863
1876{
1877 uint8_t prev;
1878
1879 if (handle == NULL) /* check handle */
1880 {
1881 return 2; /* return error */
1882 }
1883 if (handle->inited != 1) /* check handle initialization */
1884 {
1885 return 3; /* return error */
1886 }
1887
1888 if (a_bme680_iic_spi_read(handle, BME680_REG_CONFIG, &prev, 1) != 0) /* read config */
1889 {
1890 handle->debug_print("bme680: read config failed.\n"); /* read config failed */
1891
1892 return 1; /* return error */
1893 }
1894 *filter = (bme680_filter_t)((prev >> 2) & 0x07); /* set filter */
1895
1896 return 0; /* success return 0 */
1897}
1898
1911{
1912 uint8_t prev;
1913
1914 if (handle == NULL) /* check handle */
1915 {
1916 return 2; /* return error */
1917 }
1918 if (handle->inited != 1) /* check handle initialization */
1919 {
1920 return 3; /* return error */
1921 }
1922
1923 if (a_bme680_iic_spi_read(handle, BME680_REG_CONFIG, &prev, 1) != 0) /* read config */
1924 {
1925 handle->debug_print("bme680: read config failed.\n"); /* read config failed */
1926
1927 return 1; /* return error */
1928 }
1929 prev &= ~(1 << 0); /* clear settings */
1930 prev |= spi << 0; /* set spi wire */
1931 if (a_bme680_iic_spi_write(handle, BME680_REG_CONFIG, &prev, 1) != 0) /* write config */
1932 {
1933 handle->debug_print("bme680: write config failed.\n"); /* write config failed */
1934
1935 return 1; /* return error */
1936 }
1937
1938 return 0; /* success return 0 */
1939}
1940
1953{
1954 uint8_t prev;
1955
1956 if (handle == NULL) /* check handle */
1957 {
1958 return 2; /* return error */
1959 }
1960 if (handle->inited != 1) /* check handle initialization */
1961 {
1962 return 3; /* return error */
1963 }
1964
1965 if (a_bme680_iic_spi_read(handle, BME680_REG_CONFIG, &prev, 1) != 0) /* read config */
1966 {
1967 handle->debug_print("bme680: read config failed.\n"); /* read config failed */
1968
1969 return 1; /* return error */
1970 }
1971 *spi = (bme680_spi_wire_t)((prev >> 0) & 0x01); /* get spi */
1972
1973 return 0; /* success return 0 */
1974}
1975
1988{
1989 uint8_t prev;
1990
1991 if (handle == NULL) /* check handle */
1992 {
1993 return 2; /* return error */
1994 }
1995 if (handle->inited != 1) /* check handle initialization */
1996 {
1997 return 3; /* return error */
1998 }
1999
2000 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_GAS_0, &prev, 1) != 0) /* read ctrl gas0 */
2001 {
2002 handle->debug_print("bme680: read ctrl gas0 failed.\n"); /* read ctrl gas0 failed */
2003
2004 return 1; /* return error */
2005 }
2006 prev &= ~(1 << 3); /* clear settings */
2007 prev |= enable << 3; /* set bool */
2008 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_GAS_0, &prev, 1) != 0) /* write ctrl gas0 */
2009 {
2010 handle->debug_print("bme680: write ctrl gas0 failed.\n"); /* write ctrl gas0 failed */
2011
2012 return 1; /* return error */
2013 }
2014
2015 return 0; /* success return 0 */
2016}
2017
2030{
2031 uint8_t prev;
2032
2033 if (handle == NULL) /* check handle */
2034 {
2035 return 2; /* return error */
2036 }
2037 if (handle->inited != 1) /* check handle initialization */
2038 {
2039 return 3; /* return error */
2040 }
2041
2042 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_GAS_0, &prev, 1) != 0) /* read ctrl gas0 */
2043 {
2044 handle->debug_print("bme680: read ctrl gas0 failed.\n"); /* read ctrl gas0 failed */
2045
2046 return 1; /* return error */
2047 }
2048 *enable = (bme680_bool_t)((prev >> 3) & 0x01); /* get bool */
2049
2050 return 0; /* success return 0 */
2051}
2052
2065{
2066 uint8_t prev;
2067
2068 if (handle == NULL) /* check handle */
2069 {
2070 return 2; /* return error */
2071 }
2072 if (handle->inited != 1) /* check handle initialization */
2073 {
2074 return 3; /* return error */
2075 }
2076
2077 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2078 {
2079 handle->debug_print("bme680: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2080
2081 return 1; /* return error */
2082 }
2083 prev &= ~(1 << 4); /* clear settings */
2084 prev |= enable << 4; /* set bool */
2085 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_GAS_1, &prev, 1) != 0) /* write ctrl gas1 */
2086 {
2087 handle->debug_print("bme680: write ctrl gas1 failed.\n"); /* write ctrl gas1 failed */
2088
2089 return 1; /* return error */
2090 }
2091
2092 return 0; /* success return 0 */
2093}
2094
2107{
2108 uint8_t prev;
2109
2110 if (handle == NULL) /* check handle */
2111 {
2112 return 2; /* return error */
2113 }
2114 if (handle->inited != 1) /* check handle initialization */
2115 {
2116 return 3; /* return error */
2117 }
2118
2119 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2120 {
2121 handle->debug_print("bme680: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2122
2123 return 1; /* return error */
2124 }
2125 *enable = (bme680_bool_t)((prev >> 4) & 0x01); /* get bool */
2126
2127 return 0; /* success return 0 */
2128}
2129
2142uint8_t bme680_set_convert_index(bme680_handle_t *handle, uint8_t index)
2143{
2144 uint8_t prev;
2145
2146 if (handle == NULL) /* check handle */
2147 {
2148 return 2; /* return error */
2149 }
2150 if (handle->inited != 1) /* check handle initialization */
2151 {
2152 return 3; /* return error */
2153 }
2154 if (index > 9) /* check index */
2155 {
2156 handle->debug_print("bme680: index > 9.\n"); /* index > 9 */
2157
2158 return 4; /* return error */
2159 }
2160
2161 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2162 {
2163 handle->debug_print("bme680: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2164
2165 return 1; /* return error */
2166 }
2167 prev &= ~(0x0F << 0); /* clear settings */
2168 prev |= (index & 0x0F) << 0; /* set index */
2169 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_GAS_1, &prev, 1) != 0) /* write ctrl gas1 */
2170 {
2171 handle->debug_print("bme680: write ctrl gas1 failed.\n"); /* write ctrl gas1 failed */
2172
2173 return 1; /* return error */
2174 }
2175
2176 return 0; /* success return 0 */
2177}
2178
2190uint8_t bme680_get_convert_index(bme680_handle_t *handle, uint8_t *index)
2191{
2192 uint8_t prev;
2193
2194 if (handle == NULL) /* check handle */
2195 {
2196 return 2; /* return error */
2197 }
2198 if (handle->inited != 1) /* check handle initialization */
2199 {
2200 return 3; /* return error */
2201 }
2202
2203 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2204 {
2205 handle->debug_print("bme680: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2206
2207 return 1; /* return error */
2208 }
2209 *index = prev & 0x0F; /* set index */
2210
2211 return 0; /* success return 0 */
2212}
2213
2225uint8_t bme680_resistance_heat_convert_to_register(bme680_handle_t *handle, float degree_celsius, uint8_t *reg)
2226{
2227 if (handle == NULL) /* check handle */
2228 {
2229 return 2; /* return error */
2230 }
2231 if (handle->inited != 1) /* check handle initialization */
2232 {
2233 return 3; /* return error */
2234 }
2235
2236 (void)a_bme680_compensate_heat(handle, degree_celsius, reg); /* convert real data to register data */
2237
2238 return 0; /* success return 0 */
2239}
2240
2252uint8_t bme680_idac_heat_convert_to_register(bme680_handle_t *handle, float ma, uint8_t *reg)
2253{
2254 if (handle == NULL) /* check handle */
2255 {
2256 return 2; /* return error */
2257 }
2258 if (handle->inited != 1) /* check handle initialization */
2259 {
2260 return 3; /* return error */
2261 }
2262
2263 *reg = ((uint8_t)(ma * 8) - 1) << 1; /* convert real data to register data */
2264
2265 return 0; /* success return 0 */
2266}
2267
2279uint8_t bme680_idac_heat_convert_to_data(bme680_handle_t *handle, uint8_t reg, float *ma)
2280{
2281 if (handle == NULL) /* check handle */
2282 {
2283 return 2; /* return error */
2284 }
2285 if (handle->inited != 1) /* check handle initialization */
2286 {
2287 return 3; /* return error */
2288 }
2289
2290 *ma = (float)(((reg >> 1) + 1) / 8); /* convert raw data to real data */
2291
2292 return 0; /* success return 0 */
2293}
2294
2307uint8_t bme680_gas_wait_convert_to_register(bme680_handle_t *handle, uint16_t ms, uint8_t *reg)
2308{
2309 uint8_t factor = 0;
2310
2311 if (handle == NULL) /* check handle */
2312 {
2313 return 2; /* return error */
2314 }
2315 if (handle->inited != 1) /* check handle initialization */
2316 {
2317 return 3; /* return error */
2318 }
2319
2320 if (ms >= 0xFC0) /* check ms */
2321 {
2322 handle->debug_print("bme680: ms >= 0x0FC0.\n"); /* ms >= 0x0FC0 */
2323
2324 return 1; /* return error */
2325 }
2326 else
2327 {
2328 while (ms > 0x3F) /* check ms */
2329 {
2330 ms = ms / 4; /* div 4 */
2331 factor += 1; /* factor */
2332 }
2333
2334 *reg = (uint8_t)(ms + (factor * 64)); /* set reg */
2335 }
2336
2337 return 0; /* success return 0 */
2338}
2339
2358uint8_t bme680_read_gas_resistance(bme680_handle_t *handle, uint16_t *adc_raw, uint8_t *adc_range, float *ohms, uint8_t *index)
2359{
2360 uint8_t res;
2361 uint8_t prev;
2362 uint32_t timeout;
2363 uint8_t buf[2];
2364
2365 if (handle == NULL) /* check handle */
2366 {
2367 return 2; /* return error */
2368 }
2369 if (handle->inited != 1) /* check handle initialization */
2370 {
2371 return 3; /* return error */
2372 }
2373
2374 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2375 {
2376 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2377
2378 return 1; /* return error */
2379 }
2380 prev &= ~(3 << 0); /* clear settings */
2381 prev |= 0x01 << 0; /* set forced mode */
2382 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2383 {
2384 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
2385
2386 return 1; /* return error */
2387 }
2388 timeout = 10 * 1000; /* set timeout */
2389 while (timeout != 0) /* check timeout */
2390 {
2391 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2392 {
2393 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2394
2395 return 1; /* return error */
2396 }
2397 if ((prev & 0x03) == 0) /* if finished */
2398 {
2399 break; /* break */
2400 }
2401 handle->delay_ms(1); /* delay 1ms */
2402 timeout--; /* timeout-- */
2403 }
2404 if (timeout == 0) /* check timeout */
2405 {
2406 handle->debug_print("bme680: read timeout.\n"); /* read timeout */
2407
2408 return 5; /* return error */
2409 }
2410 res = a_bme680_iic_spi_read(handle, BME680_REG_GAS_R_MSB, buf, 2); /* read gas */
2411 if (res != 0)
2412 {
2413 handle->debug_print("bme680: read failed.\n"); /* read failed */
2414
2415 return 1; /* return error */
2416 }
2417 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
2418 {
2419 handle->debug_print("bme680: gas is invalid.\n"); /* gas is invalid */
2420
2421 return 6; /* return error */
2422 }
2423 if ((buf[1] & (1 << 4)) == 0) /* check heat */
2424 {
2425 handle->debug_print("bme680: heater is not stable.\n"); /* heater is not stable */
2426
2427 return 7; /* return error */
2428 }
2429 *adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
2430 *adc_range = buf[1] & 0x0F; /* set adc range */
2431 res = a_bme680_compensate_gas_resistance(handle, *adc_raw , *adc_range, ohms); /* compensate gas resistance */
2432 if (res != 0) /* check result */
2433 {
2434 handle->debug_print("bme680: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
2435
2436 return 4; /* return error */
2437 }
2438 if (a_bme680_iic_spi_read(handle, BME680_REG_EAS_STATUS_0, &prev, 1) != 0) /* read status */
2439 {
2440 handle->debug_print("bme680: read status failed.\n"); /* read status failed */
2441
2442 return 1; /* return error */
2443 }
2444 *index = (prev & 0xF); /* set index */
2445
2446 return 0; /* success return 0 */
2447}
2448
2463uint8_t bme680_read_pressure(bme680_handle_t *handle, uint32_t *pressure_raw, float *pressure_pa)
2464{
2465 uint8_t res;
2466 uint8_t prev;
2467 uint32_t timeout;
2468 uint32_t temperature_raw;
2469 float temperature_c;
2470 uint8_t buf[6];
2471
2472 if (handle == NULL) /* check handle */
2473 {
2474 return 2; /* return error */
2475 }
2476 if (handle->inited != 1) /* check handle initialization */
2477 {
2478 return 3; /* return error */
2479 }
2480
2481 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2482 {
2483 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2484
2485 return 1; /* return error */
2486 }
2487 prev &= ~(3 << 0); /* clear settings */
2488 prev |= 0x01 << 0; /* set forced mode */
2489 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2490 {
2491 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
2492
2493 return 1; /* return error */
2494 }
2495 timeout = 10 * 1000; /* set timeout */
2496 while (timeout != 0) /* check timeout */
2497 {
2498 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2499 {
2500 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2501
2502 return 1; /* return error */
2503 }
2504 if ((prev & 0x03) == 0) /* if finished */
2505 {
2506 break; /* break */
2507 }
2508 handle->delay_ms(1); /* delay 1ms */
2509 timeout--; /* timeout-- */
2510 }
2511 if (timeout == 0) /* check timeout */
2512 {
2513 handle->debug_print("bme680: read timeout.\n"); /* read timeout */
2514
2515 return 5; /* return error */
2516 }
2517 res = a_bme680_iic_spi_read(handle, BME680_REG_PRESS_MSB, buf, 6); /* read temperature and pressure */
2518 if (res != 0)
2519 {
2520 handle->debug_print("bme680: read failed.\n"); /* read failed */
2521
2522 return 1; /* return error */
2523 }
2524 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
2525 (((uint32_t)(buf[4])) << 4) |
2526 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
2527 res = a_bme680_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
2528 if (res != 0)
2529 {
2530 handle->debug_print("bme680: compensate temperature failed.\n"); /* compensate temperature failed */
2531
2532 return 4; /* return error */
2533 }
2534 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
2535 (((int32_t)(buf[1])) << 4) |
2536 (((int32_t)(buf[2])) >> 4)); /* set pressure raw */
2537 res = a_bme680_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
2538 if (res != 0)
2539 {
2540 handle->debug_print("bme680: compensate pressure failed.\n"); /* compensate pressure failed */
2541
2542 return 4; /* return error */
2543 }
2544
2545 return 0; /* success return 0 */
2546}
2547
2562uint8_t bme680_read_temperature(bme680_handle_t *handle, uint32_t *temperature_raw, float *temperature_c)
2563{
2564 uint8_t res;
2565 uint8_t prev;
2566 uint32_t timeout;
2567 uint8_t buf[6];
2568
2569 if (handle == NULL) /* check handle */
2570 {
2571 return 2; /* return error */
2572 }
2573 if (handle->inited != 1) /* check handle initialization */
2574 {
2575 return 3; /* return error */
2576 }
2577
2578 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2579 {
2580 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2581
2582 return 1; /* return error */
2583 }
2584 prev &= ~(3 << 0); /* clear settings */
2585 prev |= 0x01 << 0; /* set forced mode */
2586 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2587 {
2588 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
2589
2590 return 1; /* return error */
2591 }
2592 timeout = 10 * 1000; /* set timeout */
2593 while (timeout != 0) /* check timeout */
2594 {
2595 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2596 {
2597 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2598
2599 return 1; /* return error */
2600 }
2601 if ((prev & 0x03) == 0) /* if finished */
2602 {
2603 break; /* break */
2604 }
2605 handle->delay_ms(1); /* delay 1ms */
2606 timeout--; /* timeout-- */
2607 }
2608 if (timeout == 0) /* check timeout */
2609 {
2610 handle->debug_print("bme680: read timeout.\n"); /* read timeout */
2611
2612 return 5; /* return error */
2613 }
2614 res = a_bme680_iic_spi_read(handle, BME680_REG_PRESS_MSB, buf, 6); /* read temperature and pressure */
2615 if (res != 0)
2616 {
2617 handle->debug_print("bme680: read failed.\n"); /* read failed */
2618
2619 return 1; /* return error */
2620 }
2621 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
2622 (((uint32_t)(buf[4])) << 4) |
2623 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
2624 res = a_bme680_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
2625 if (res != 0)
2626 {
2627 handle->debug_print("bme680: compensate temperature failed.\n"); /* compensate temperature failed */
2628
2629 return 4; /* return error */
2630 }
2631
2632 return 0; /* success return 0 */
2633}
2634
2649uint8_t bme680_read_humidity(bme680_handle_t *handle, uint32_t *humidity_raw, float *humidity_percentage)
2650{
2651 uint8_t res;
2652 uint8_t prev;
2653 uint32_t timeout;
2654 uint32_t temperature_raw;
2655 float temperature_c;
2656 uint8_t buf[8];
2657
2658 if (handle == NULL) /* check handle */
2659 {
2660 return 2; /* return error */
2661 }
2662 if (handle->inited != 1) /* check handle initialization */
2663 {
2664 return 3; /* return error */
2665 }
2666
2667 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2668 {
2669 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2670
2671 return 1; /* return error */
2672 }
2673 prev &= ~(3 << 0); /* clear settings */
2674 prev |= 0x01 << 0; /* set forced mode */
2675 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2676 {
2677 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
2678
2679 return 1; /* return error */
2680 }
2681 timeout = 10 * 1000; /* set timeout */
2682 while (timeout != 0) /* check timeout */
2683 {
2684 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2685 {
2686 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2687
2688 return 1; /* return error */
2689 }
2690 if ((prev & 0x03) == 0) /* if finished */
2691 {
2692 break; /* break */
2693 }
2694 handle->delay_ms(1); /* delay 1ms */
2695 timeout--; /* timeout-- */
2696 }
2697 if (timeout == 0) /* check timeout */
2698 {
2699 handle->debug_print("bme680: read timeout.\n"); /* read timeout */
2700
2701 return 5; /* return error */
2702 }
2703 res = a_bme680_iic_spi_read(handle, BME680_REG_PRESS_MSB, buf, 8); /* read temperature pressure and humidity */
2704 if (res != 0)
2705 {
2706 handle->debug_print("bme680: read failed.\n"); /* read failed */
2707
2708 return 1; /* return error */
2709 }
2710 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
2711 (((uint32_t)(buf[4])) << 4) |
2712 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
2713 res = a_bme680_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
2714 if (res != 0)
2715 {
2716 handle->debug_print("bme680: compensate temperature failed.\n"); /* compensate temperature failed */
2717
2718 return 4; /* return error */
2719 }
2720 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
2721 res = a_bme680_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
2722 if (res != 0)
2723 {
2724 handle->debug_print("bme680: compensate humidity failed.\n"); /* compensate humidity failed */
2725
2726 return 4; /* return error */
2727 }
2728
2729 return 0; /* success return 0 */
2730}
2731
2750uint8_t bme680_read_temperature_pressure_humidity(bme680_handle_t *handle, uint32_t *temperature_raw, float *temperature_c,
2751 uint32_t *pressure_raw, float *pressure_pa,
2752 uint32_t *humidity_raw, float *humidity_percentage)
2753{
2754 uint8_t res;
2755 uint8_t prev;
2756 uint32_t timeout;
2757 uint8_t buf[8];
2758
2759 if (handle == NULL) /* check handle */
2760 {
2761 return 2; /* return error */
2762 }
2763 if (handle->inited != 1) /* check handle initialization */
2764 {
2765 return 3; /* return error */
2766 }
2767
2768 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2769 {
2770 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2771
2772 return 1; /* return error */
2773 }
2774 prev &= ~(3 << 0); /* clear settings */
2775 prev |= 0x01 << 0; /* set forced mode */
2776 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2777 {
2778 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
2779
2780 return 1; /* return error */
2781 }
2782 timeout = 10 * 1000; /* set timeout */
2783 while (timeout != 0) /* check timeout */
2784 {
2785 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2786 {
2787 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2788
2789 return 1; /* return error */
2790 }
2791 if ((prev & 0x03) == 0) /* if finished */
2792 {
2793 break; /* break */
2794 }
2795 handle->delay_ms(1); /* delay 1ms */
2796 timeout--; /* timeout-- */
2797 }
2798 if (timeout == 0) /* check timeout */
2799 {
2800 handle->debug_print("bme680: read timeout.\n"); /* read timeout */
2801
2802 return 5; /* return error */
2803 }
2804 res = a_bme680_iic_spi_read(handle, BME680_REG_PRESS_MSB, buf, 8); /* read temperature pressure and humidity */
2805 if (res != 0)
2806 {
2807 handle->debug_print("bme680: read failed.\n"); /* read failed */
2808
2809 return 1; /* return error */
2810 }
2811 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
2812 (((uint32_t)(buf[4])) << 4) |
2813 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
2814 res = a_bme680_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
2815 if (res != 0)
2816 {
2817 handle->debug_print("bme680: compensate temperature failed.\n"); /* compensate temperature failed */
2818
2819 return 4; /* return error */
2820 }
2821 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
2822 (((int32_t)(buf[1])) << 4) |
2823 (((int32_t)(buf[2])) >> 4)); /* set pressure raw */
2824 res = a_bme680_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
2825 if (res != 0)
2826 {
2827 handle->debug_print("bme680: compensate pressure failed.\n"); /* compensate pressure failed */
2828
2829 return 4; /* return error */
2830 }
2831 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
2832 res = a_bme680_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
2833 if (res != 0)
2834 {
2835 handle->debug_print("bme680: compensate humidity failed.\n"); /* compensate humidity failed */
2836
2837 return 4; /* return error */
2838 }
2839
2840 return 0; /* success return 0 */
2841}
2842
2867uint8_t bme680_read(bme680_handle_t *handle, uint32_t *temperature_raw, float *temperature_c,
2868 uint32_t *pressure_raw, float *pressure_pa,
2869 uint32_t *humidity_raw, float *humidity_percentage,
2870 uint16_t *adc_raw, uint8_t *adc_range, float *ohms, uint8_t *index)
2871{
2872 uint8_t res;
2873 uint8_t prev;
2874 uint32_t timeout;
2875 uint8_t buf[8];
2876
2877 if (handle == NULL) /* check handle */
2878 {
2879 return 2; /* return error */
2880 }
2881 if (handle->inited != 1) /* check handle initialization */
2882 {
2883 return 3; /* return error */
2884 }
2885
2886 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2887 {
2888 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2889
2890 return 1; /* return error */
2891 }
2892 prev &= ~(3 << 0); /* clear settings */
2893 prev |= 0x01 << 0; /* set forced mode */
2894 if (a_bme680_iic_spi_write(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2895 {
2896 handle->debug_print("bme680: write ctrl meas failed.\n"); /* write ctrl meas failed */
2897
2898 return 1; /* return error */
2899 }
2900 timeout = 10 * 1000; /* set timeout */
2901 while (timeout != 0) /* check timeout */
2902 {
2903 if (a_bme680_iic_spi_read(handle, BME680_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2904 {
2905 handle->debug_print("bme680: read ctrl meas failed.\n"); /* read ctrl meas failed */
2906
2907 return 1; /* return error */
2908 }
2909 if ((prev & 0x03) == 0) /* if finished */
2910 {
2911 break; /* break */
2912 }
2913 handle->delay_ms(1); /* delay 1ms */
2914 timeout--; /* timeout-- */
2915 }
2916 if (timeout == 0) /* check timeout */
2917 {
2918 handle->debug_print("bme680: read timeout.\n"); /* read timeout */
2919
2920 return 5; /* return error */
2921 }
2922 res = a_bme680_iic_spi_read(handle, BME680_REG_PRESS_MSB, buf, 8); /* read temperature pressure and humidity */
2923 if (res != 0)
2924 {
2925 handle->debug_print("bme680: read failed.\n"); /* read failed */
2926
2927 return 1; /* return error */
2928 }
2929 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
2930 (((uint32_t)(buf[4])) << 4) |
2931 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
2932 res = a_bme680_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
2933 if (res != 0)
2934 {
2935 handle->debug_print("bme680: compensate temperature failed.\n"); /* compensate temperature failed */
2936
2937 return 4; /* return error */
2938 }
2939 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
2940 (((int32_t)(buf[1])) << 4) |
2941 (((int32_t)(buf[2])) >> 4)); /* set pressure raw */
2942 res = a_bme680_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
2943 if (res != 0)
2944 {
2945 handle->debug_print("bme680: compensate pressure failed.\n"); /* compensate pressure failed */
2946
2947 return 4; /* return error */
2948 }
2949 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
2950 res = a_bme680_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
2951 if (res != 0)
2952 {
2953 handle->debug_print("bme680: compensate humidity failed.\n"); /* compensate humidity failed */
2954
2955 return 4; /* return error */
2956 }
2957
2958 res = a_bme680_iic_spi_read(handle, BME680_REG_GAS_R_MSB, buf, 2); /* read gas */
2959 if (res != 0)
2960 {
2961 handle->debug_print("bme680: read failed.\n"); /* read failed */
2962
2963 return 1; /* return error */
2964 }
2965 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
2966 {
2967 handle->debug_print("bme680: gas is invalid.\n"); /* gas is invalid */
2968
2969 return 6; /* return error */
2970 }
2971 if ((buf[1] & (1 << 4)) == 0) /* check heat */
2972 {
2973 handle->debug_print("bme680: heater is not stable.\n"); /* heater is not stable */
2974
2975 return 7; /* return error */
2976 }
2977 *adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
2978 *adc_range = buf[1] & 0x0F; /* set adc range */
2979 res = a_bme680_compensate_gas_resistance(handle, *adc_raw , *adc_range, ohms); /* compensate gas resistance */
2980 if (res != 0) /* check result */
2981 {
2982 handle->debug_print("bme680: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
2983
2984 return 4; /* return error */
2985 }
2986 if (a_bme680_iic_spi_read(handle, BME680_REG_EAS_STATUS_0, &prev, 1) != 0) /* read status */
2987 {
2988 handle->debug_print("bme680: read status failed.\n"); /* read status failed */
2989
2990 return 1; /* return error */
2991 }
2992 *index = (prev & 0xF); /* set index */
2993
2994 return 0; /* success return 0 */
2995}
2996
3010uint8_t bme680_set_reg(bme680_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
3011{
3012 if (handle == NULL) /* check handle */
3013 {
3014 return 2; /* return error */
3015 }
3016 if (handle->inited != 1) /* check handle initialization */
3017 {
3018 return 3; /* return error */
3019 }
3020
3021 return a_bme680_iic_spi_write(handle, reg, buf, len); /* write register */
3022}
3023
3037uint8_t bme680_get_reg(bme680_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
3038{
3039 if (handle == NULL) /* check handle */
3040 {
3041 return 2; /* return error */
3042 }
3043 if (handle->inited != 1) /* check handle initialization */
3044 {
3045 return 3; /* return error */
3046 }
3047
3048 return a_bme680_iic_spi_read(handle, reg, buf, len); /* read register */
3049}
3050
3060{
3061 if (info == NULL) /* check handle */
3062 {
3063 return 2; /* return error */
3064 }
3065
3066 memset(info, 0, sizeof(bme680_info_t)); /* initialize bme680 info structure */
3067 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
3068 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
3069 strncpy(info->interface, "IIC SPI", 8); /* copy interface name */
3070 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
3071 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
3072 info->max_current_ma = MAX_CURRENT; /* set maximum current */
3073 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
3074 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
3075 info->driver_version = DRIVER_VERSION; /* set driver version */
3076
3077 return 0; /* success return 0 */
3078}
#define BME680_REG_NVM_PAR_G3
#define BME680_REG_NVM_PAR_P1_L
#define BME680_REG_NVM_PAR_P7
#define BME680_REG_NVM_PAR_H5
#define MAX_CURRENT
#define BME680_REG_NVM_PAR_P3
#define BME680_REG_IDAC_HEAT_X
#define BME680_REG_NVM_RES_HEAT_VAL
#define BME680_REG_NVM_PAR_G2_L
#define BME680_REG_CONFIG
#define BME680_REG_NVM_PAR_P8_L
#define BME680_REG_CTRL_GAS_0
#define BME680_REG_NVM_PAR_P6
#define BME680_REG_CTRL_GAS_1
#define SUPPLY_VOLTAGE_MAX
#define BME680_REG_NVM_PAR_T3
#define BME680_REG_RESET
#define BME680_REG_NVM_PAR_H7
#define BME680_REG_GAS_WAIT_X
#define BME680_REG_NVM_PAR_H6
#define BME680_REG_NVM_PAR_H1_L
#define TEMPERATURE_MAX
#define BME680_REG_GAS_R_LSB
#define BME680_REG_NVM_PAR_P4_L
#define BME680_REG_PRESS_MSB
#define BME680_REG_EAS_STATUS_0
#define BME680_REG_CTRL_HUM
#define BME680_REG_NVM_PAR_P10
#define BME680_REG_NVM_RES_HEAT_RANGE
#define BME680_REG_NVM_PAR_H3
#define BME680_REG_NVM_PAR_P5_L
#define BME680_REG_NVM_PAR_G1
#define BME680_REG_ID
#define BME680_REG_GAS_R_MSB
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define BME680_REG_NVM_PAR_P9_L
#define BME680_REG_CTRL_MEAS
#define BME680_REG_NVM_PAR_H2_H
#define BME680_REG_RES_HEAT_X
#define BME680_REG_NVM_PAR_P2_L
#define BME680_REG_STATUS
#define BME680_REG_NVM_PAR_H4
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define BME680_REG_NVM_PAR_T2_L
#define BME680_REG_NVM_RANGE_SWITCH
#define BME680_REG_NVM_PAR_T1_L
chip register definition
driver bme680 header file
uint8_t bme680_set_run_gas(bme680_handle_t *handle, bme680_bool_t enable)
enable or disable run gas
uint8_t bme680_gas_wait_convert_to_register(bme680_handle_t *handle, uint16_t ms, uint8_t *reg)
convert the gas wait to the register raw data
uint8_t bme680_set_filter(bme680_handle_t *handle, bme680_filter_t filter)
set filter
uint8_t bme680_read(bme680_handle_t *handle, uint32_t *temperature_raw, float *temperature_c, uint32_t *pressure_raw, float *pressure_pa, uint32_t *humidity_raw, float *humidity_percentage, uint16_t *adc_raw, uint8_t *adc_range, float *ohms, uint8_t *index)
read the temperature pressure humidity and gas resistance
uint8_t bme680_get_measuring_status(bme680_handle_t *handle, bme680_bool_t *enable)
get measuring status
uint8_t bme680_deinit(bme680_handle_t *handle)
close the chip
uint8_t bme680_get_mode(bme680_handle_t *handle, bme680_mode_t *mode)
get mode
uint8_t bme680_get_gas_measuring_index(bme680_handle_t *handle, uint8_t *index)
get gas measuring index
bme680_filter_t
bme680 filter enumeration definition
uint8_t bme680_set_temperature_oversampling(bme680_handle_t *handle, bme680_oversampling_t oversampling)
set temperature oversampling
uint8_t bme680_set_heat_off(bme680_handle_t *handle, bme680_bool_t enable)
enable or disable heat off
bme680_mode_t
bme680 mode enumeration definition
uint8_t bme680_set_interface(bme680_handle_t *handle, bme680_interface_t interface)
set the interface
uint8_t bme680_set_idac_heat(bme680_handle_t *handle, uint8_t index, uint8_t reg)
set idac heat
uint8_t bme680_set_convert_index(bme680_handle_t *handle, uint8_t index)
set convert index
uint8_t bme680_get_spi_wire_3_data_interrupt(bme680_handle_t *handle, bme680_bool_t *enable)
get spi wire3 data interrupt status
uint8_t bme680_read_pressure(bme680_handle_t *handle, uint32_t *pressure_raw, float *pressure_pa)
read the pressure data
uint8_t bme680_set_spi_wire_3_data_interrupt(bme680_handle_t *handle, bme680_bool_t enable)
enable or disable spi wire3 data interrupt
uint8_t bme680_get_temperature_oversampling(bme680_handle_t *handle, bme680_oversampling_t *oversampling)
get temperature oversampling
uint8_t bme680_get_resistance_heat(bme680_handle_t *handle, uint8_t index, uint8_t *reg)
get resistance heat
uint8_t bme680_get_heat_off(bme680_handle_t *handle, bme680_bool_t *enable)
get heat off status
uint8_t bme680_get_interface(bme680_handle_t *handle, bme680_interface_t *interface)
get the interface
bme680_interface_t
bme680 interface enumeration definition
uint8_t bme680_set_resistance_heat(bme680_handle_t *handle, uint8_t index, uint8_t reg)
set resistance heat
uint8_t bme680_set_gas_wait(bme680_handle_t *handle, uint8_t index, uint8_t reg)
set gas wait
uint8_t bme680_info(bme680_info_t *info)
get chip's information
struct bme680_info_s bme680_info_t
bme680 information structure definition
uint8_t bme680_read_temperature(bme680_handle_t *handle, uint32_t *temperature_raw, float *temperature_c)
read the temperature data
uint8_t bme680_get_idac_heat(bme680_handle_t *handle, uint8_t index, uint8_t *reg)
get idac heat
uint8_t bme680_read_humidity(bme680_handle_t *handle, uint32_t *humidity_raw, float *humidity_percentage)
read the humidity data
uint8_t bme680_get_filter(bme680_handle_t *handle, bme680_filter_t *filter)
get filter
uint8_t bme680_resistance_heat_convert_to_register(bme680_handle_t *handle, float degree_celsius, uint8_t *reg)
convert the resistance heat to the register raw data
uint8_t bme680_init(bme680_handle_t *handle)
initialize the chip
uint8_t bme680_get_spi_wire(bme680_handle_t *handle, bme680_spi_wire_t *spi)
get spi wire
bme680_address_t
bme680 address enumeration definition
uint8_t bme680_read_gas_resistance(bme680_handle_t *handle, uint16_t *adc_raw, uint8_t *adc_range, float *ohms, uint8_t *index)
read the gas resistance
uint8_t bme680_soft_reset(bme680_handle_t *handle)
soft reset
bme680_oversampling_t
bme680 oversampling enumeration definition
uint8_t bme680_get_humidity_oversampling(bme680_handle_t *handle, bme680_oversampling_t *oversampling)
get humidity oversampling
uint8_t bme680_get_new_data_status(bme680_handle_t *handle, bme680_bool_t *enable)
get new data status
uint8_t bme680_idac_heat_convert_to_register(bme680_handle_t *handle, float ma, uint8_t *reg)
convert the idac heat to the register raw data
uint8_t bme680_set_spi_wire(bme680_handle_t *handle, bme680_spi_wire_t spi)
set spi wire
bme680_bool_t
bme680 bool enumeration definition
uint8_t bme680_get_gas_measuring_status(bme680_handle_t *handle, bme680_bool_t *enable)
get gas measuring status
uint8_t bme680_set_addr_pin(bme680_handle_t *handle, bme680_address_t addr_pin)
set the iic address pin
uint8_t bme680_set_pressure_oversampling(bme680_handle_t *handle, bme680_oversampling_t oversampling)
set pressure oversampling
uint8_t bme680_set_mode(bme680_handle_t *handle, bme680_mode_t mode)
set mode
struct bme680_handle_s bme680_handle_t
bme680 handle structure definition
uint8_t bme680_get_run_gas(bme680_handle_t *handle, bme680_bool_t *enable)
get run gas status
bme680_spi_wire_t
bme680 spi wire enumeration definition
uint8_t bme680_read_temperature_pressure_humidity(bme680_handle_t *handle, uint32_t *temperature_raw, float *temperature_c, uint32_t *pressure_raw, float *pressure_pa, uint32_t *humidity_raw, float *humidity_percentage)
read the temperature pressure and humidity data
uint8_t bme680_idac_heat_convert_to_data(bme680_handle_t *handle, uint8_t reg, float *ma)
convert the register raw data to idac heat
uint8_t bme680_get_heater_stability_status(bme680_handle_t *handle, bme680_bool_t *enable)
get heater stability status
uint8_t bme680_set_humidity_oversampling(bme680_handle_t *handle, bme680_oversampling_t oversampling)
set humidity oversampling
uint8_t bme680_get_convert_index(bme680_handle_t *handle, uint8_t *index)
get convert index
uint8_t bme680_get_gas_wait(bme680_handle_t *handle, uint8_t index, uint8_t *reg)
get gas wait
uint8_t bme680_get_gas_valid_status(bme680_handle_t *handle, bme680_bool_t *enable)
get gas valid status
uint8_t bme680_get_addr_pin(bme680_handle_t *handle, bme680_address_t *addr_pin)
get the iic address pin
uint8_t bme680_get_pressure_oversampling(bme680_handle_t *handle, bme680_oversampling_t *oversampling)
get pressure oversampling
@ BME680_INTERFACE_IIC
uint8_t bme680_get_reg(bme680_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t bme680_set_reg(bme680_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_read)(uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* spi_write)(uint8_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* spi_deinit)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]