LibDriver BME688
Loading...
Searching...
No Matches
driver_bme688.c
Go to the documentation of this file.
1
36
37#include "driver_bme688.h"
38
42#define CHIP_NAME "Bosch BME688"
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 BME688_REG_NVM_PAR_T1_L 0xE9
55#define BME688_REG_NVM_PAR_T1_H 0xEA
56#define BME688_REG_NVM_PAR_T2_L 0x8A
57#define BME688_REG_NVM_PAR_T2_H 0x8B
58#define BME688_REG_NVM_PAR_T3 0x8C
59#define BME688_REG_NVM_PAR_P1_L 0x8E
60#define BME688_REG_NVM_PAR_P1_H 0x8F
61#define BME688_REG_NVM_PAR_P2_L 0x90
62#define BME688_REG_NVM_PAR_P2_H 0x91
63#define BME688_REG_NVM_PAR_P3 0x92
64#define BME688_REG_NVM_PAR_P4_L 0x94
65#define BME688_REG_NVM_PAR_P4_H 0x95
66#define BME688_REG_NVM_PAR_P5_L 0x96
67#define BME688_REG_NVM_PAR_P5_H 0x97
68#define BME688_REG_NVM_PAR_P6 0x99
69#define BME688_REG_NVM_PAR_P7 0x98
70#define BME688_REG_NVM_PAR_P8_L 0x9C
71#define BME688_REG_NVM_PAR_P8_H 0x9D
72#define BME688_REG_NVM_PAR_P9_L 0x9E
73#define BME688_REG_NVM_PAR_P9_H 0x9F
74#define BME688_REG_NVM_PAR_P10 0xA0
75#define BME688_REG_NVM_PAR_H1_L 0xE2
76#define BME688_REG_NVM_PAR_H1_H 0xE3
77#define BME688_REG_NVM_PAR_H2_L 0xE2
78#define BME688_REG_NVM_PAR_H2_H 0xE1
79#define BME688_REG_NVM_PAR_H3 0xE4
80#define BME688_REG_NVM_PAR_H4 0xE5
81#define BME688_REG_NVM_PAR_H5 0xE6
82#define BME688_REG_NVM_PAR_H6 0xE7
83#define BME688_REG_NVM_PAR_H7 0xE8
84#define BME688_REG_NVM_PAR_G1 0xED
85#define BME688_REG_NVM_PAR_G2_L 0xEB
86#define BME688_REG_NVM_PAR_G2_H 0xEC
87#define BME688_REG_NVM_PAR_G3 0xEE
88#define BME688_REG_NVM_RES_HEAT_RANGE 0x02
89#define BME688_REG_NVM_RES_HEAT_VAL 0x00
90#define BME688_REG_NVM_RANGE_SWITCH 0x04
91#define BME688_REG_STATUS 0x73
92#define BME688_REG_VARIANT_ID 0xF0
93#define BME688_REG_RESET 0xE0
94#define BME688_REG_ID 0xD0
95#define BME688_REG_CONFIG 0x75
96#define BME688_REG_CTRL_MEAS 0x74
97#define BME688_REG_CTRL_HUM 0x72
98#define BME688_REG_CTRL_GAS_1 0x71
99#define BME688_REG_CTRL_GAS_0 0x70
100#define BME688_REG_GAS_WAIT_SHARED 0x6E
101#define BME688_REG_GAS_WAIT_X 0x64
102#define BME688_REG_RES_HEAT_X 0x5A
103#define BME688_REG_IDAC_HEAT_X 0x50
104#define BME688_REG_GAS_R_LSB_2 0x4F
105#define BME688_REG_GAS_R_MSB_2 0x4E
106#define BME688_REG_HUM_LSB_2 0x48
107#define BME688_REG_HUM_MSB_2 0x47
108#define BME688_REG_TEMP_XLSB_2 0x46
109#define BME688_REG_TEMP_LSB_2 0x45
110#define BME688_REG_TEMP_MSB_2 0x44
111#define BME688_REG_PRESS_XLSB_2 0x43
112#define BME688_REG_PRESS_LSB_2 0x42
113#define BME688_REG_PRESS_MSB_2 0x41
114#define BME688_REG_SUB_MEAS_INDEX_2 0x40
115#define BME688_REG_MEAS_STATUS_2 0x3F
116#define BME688_REG_GAS_R_LSB_1 0x3E
117#define BME688_REG_GAS_R_MSB_1 0x3D
118#define BME688_REG_HUM_LSB_1 0x37
119#define BME688_REG_HUM_MSB_1 0x36
120#define BME688_REG_TEMP_XLSB_1 0x35
121#define BME688_REG_TEMP_LSB_1 0x34
122#define BME688_REG_TEMP_MSB_1 0x33
123#define BME688_REG_PRESS_XLSB_1 0x32
124#define BME688_REG_PRESS_LSB_1 0x31
125#define BME688_REG_PRESS_MSB_1 0x30
126#define BME688_REG_SUB_MEAS_INDEX_1 0x2F
127#define BME688_REG_MEAS_STATUS_1 0x2E
128#define BME688_REG_GAS_R_LSB_0 0x2D
129#define BME688_REG_GAS_R_MSB_0 0x2C
130#define BME688_REG_HUM_LSB_0 0x26
131#define BME688_REG_HUM_MSB_0 0x25
132#define BME688_REG_TEMP_XLSB_0 0x24
133#define BME688_REG_TEMP_LSB_0 0x23
134#define BME688_REG_TEMP_MSB_0 0x22
135#define BME688_REG_PRESS_XLSB_0 0x21
136#define BME688_REG_PRESS_LSB_0 0x20
137#define BME688_REG_PRESS_MSB_0 0x1F
138#define BME688_REG_SUB_MEAS_INDEX_0 0x1E
139#define BME688_REG_MEAS_STATUS_0 0x1D
140
150static uint8_t a_bme688_change_spi_page(bme688_handle_t *handle, uint8_t page)
151{
152 uint8_t reg;
153 uint8_t buf[1];
154
155 reg = BME688_REG_STATUS; /* set address */
156 reg |= 1 << 7; /* set read mode */
157 if (handle->spi_read(reg, buf, 1) != 0) /* spi read */
158 {
159 return 1; /* return error */
160 }
161 if (page == 0) /* check page */
162 {
163 buf[0] &= ~(1 << 4); /* set page 0 */
164 }
165 else
166 {
167 buf[0] |= 1 << 4; /* set page 1 */
168 }
169 reg &= ~(1 << 7); /* write mode */
170 if (handle->spi_write(reg, buf, 1) != 0) /* spi write */
171 {
172 return 1; /* return error */
173 }
174 handle->page = page; /* save page */
175
176 return 0; /* success return 0 */
177}
178
190static uint8_t a_bme688_iic_spi_read(bme688_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
191{
192 if (handle->iic_spi == BME688_INTERFACE_IIC) /* iic interface */
193 {
194 if (handle->iic_read(handle->iic_addr, reg, buf, len) != 0) /* iic read */
195 {
196 return 1; /* return error */
197 }
198
199 return 0; /* success return 0 */
200 }
201 else /* spi interface */
202 {
203 if (reg > 0x7F) /* check address */
204 {
205 if (handle->page != 0) /* check page */
206 {
207 if (a_bme688_change_spi_page(handle, 0) != 0) /* change spi change */
208 {
209 return 1; /* return error */
210 }
211 }
212 reg -= 0x80; /* set address */
213 reg |= 1 << 7; /* set read mode */
214 if (handle->spi_read(reg, buf, len) != 0) /* spi read */
215 {
216 return 1; /* return error */
217 }
218 }
219 else
220 {
221 if (handle->page == 0) /* check page */
222 {
223 if (a_bme688_change_spi_page(handle, 1) != 0) /* change spi change */
224 {
225 return 1; /* return error */
226 }
227 }
228 reg |= 1 << 7; /* set read mode */
229 if (handle->spi_read(reg, buf, len) != 0) /* spi read */
230 {
231 return 1; /* return error */
232 }
233 }
234
235 return 0; /* success return 0 */
236 }
237}
238
250static uint8_t a_bme688_iic_spi_write(bme688_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
251{
252 if (handle->iic_spi == BME688_INTERFACE_IIC) /* iic interface */
253 {
254 if (handle->iic_write(handle->iic_addr, reg, buf, len) != 0) /* iic write */
255 {
256 return 1; /* return error */
257 }
258
259 return 0; /* success return 0 */
260 }
261 else /* spi interface */
262 {
263 if (reg > 0x7F) /* check address */
264 {
265 if (handle->page != 0) /* check page */
266 {
267 if (a_bme688_change_spi_page(handle, 0) != 0) /* change spi change */
268 {
269 return 1; /* return error */
270 }
271 }
272 reg -= 0x80; /* set address */
273 reg &= ~(1 << 7); /* write mode */
274 if (handle->spi_write(reg, buf, len) != 0) /* spi write */
275 {
276 return 1; /* return error */
277 }
278 }
279 else
280 {
281 if (handle->page == 0) /* check page */
282 {
283 if (a_bme688_change_spi_page(handle, 1) != 0) /* change spi change */
284 {
285 return 1; /* return error */
286 }
287 }
288 reg &= ~(1 << 7); /* write mode */
289 if (handle->spi_write(reg, buf, len) != 0) /* spi write */
290 {
291 return 1; /* return error */
292 }
293 }
294
295 return 0; /* success return 0 */
296 }
297}
298
307static uint8_t a_bme688_get_nvm_calibration(bme688_handle_t *handle)
308{
309 uint8_t reg;
310 uint8_t buf[2];
311
312 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_T1_L, (uint8_t *)buf, 2) != 0) /* read t1 */
313 {
314 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
315
316 return 1; /* return error */
317 }
318 handle->par_t1 = (uint16_t)buf[1] << 8 | buf[0]; /* set par t1 */
319 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_T2_L, (uint8_t *)buf, 2) != 0) /* read t2 */
320 {
321 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
322
323 return 1; /* return error */
324 }
325 handle->par_t2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par t2 */
326 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_T3, (uint8_t *)&reg, 1) != 0) /* read t3 */
327 {
328 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
329
330 return 1; /* return error */
331 }
332 handle->par_t3 = (int8_t)(reg); /* set par t3 */
333 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P1_L, (uint8_t *)buf, 2) != 0) /* read p1 */
334 {
335 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
336
337 return 1; /* return error */
338 }
339 handle->par_p1 = (uint16_t)buf[1] <<8 | buf[0]; /* set par p1 */
340 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P2_L, (uint8_t *)buf, 2) != 0) /* read p2 */
341 {
342 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
343
344 return 1; /* return error */
345 }
346 handle->par_p2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p2 */
347 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P3, (uint8_t *)&reg, 1) != 0) /* read p3 */
348 {
349 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
350
351 return 1; /* return error */
352 }
353 handle->par_p3 = (int8_t)(reg); /* set par p3 */
354 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P4_L, (uint8_t *)buf, 2) != 0) /* read p4 */
355 {
356 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
357
358 return 1; /* return error */
359 }
360 handle->par_p4 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p4 */
361 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P5_L, (uint8_t *)buf, 2) != 0) /* read p5 */
362 {
363 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
364
365 return 1; /* return error */
366 }
367 handle->par_p5 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p5 */
368 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P6, (uint8_t *)&reg, 1) != 0) /* read p6 */
369 {
370 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
371
372 return 1; /* return error */
373 }
374 handle->par_p6 = (int8_t)(reg); /* set par p6 */
375 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P7, (uint8_t *)&reg, 1) != 0) /* read p7 */
376 {
377 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
378
379 return 1; /* return error */
380 }
381 handle->par_p7 = (int8_t)(reg); /* set par p7 */
382 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P8_L, (uint8_t *)buf, 2) != 0) /* read p8 */
383 {
384 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
385
386 return 1; /* return error */
387 }
388 handle->par_p8 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p8 */
389 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P9_L, (uint8_t *)buf, 2) != 0) /* read p9 */
390 {
391 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
392
393 return 1; /* return error */
394 }
395 handle->par_p9 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par p9 */
396 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_P10, (uint8_t *)&reg, 1) != 0) /* read p10 */
397 {
398 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
399
400 return 1; /* return error */
401 }
402 handle->par_p10 = (uint8_t)(reg); /* set par p10 */
403 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_H1_L, (uint8_t *)buf, 2) != 0) /* read h1 */
404 {
405 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
406
407 return 1; /* return error */
408 }
409 handle->par_h1 = (uint16_t)((uint16_t)buf[1] << 4 | buf[0] & 0xF); /* set h1 */
410 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_H2_H, (uint8_t *)buf, 2) != 0) /* read h2 */
411 {
412 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
413
414 return 1; /* return error */
415 }
416 handle->par_h2 = (uint16_t)((uint16_t)buf[0] << 4 | ((buf[1] >> 4) & 0xF)); /* set h2 */
417 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_H3, (uint8_t *)&reg, 1) != 0) /* read h3 */
418 {
419 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
420
421 return 1; /* return error */
422 }
423 handle->par_h3 = (int8_t)reg; /* set h3 */
424 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_H4, (uint8_t *)&reg, 1) != 0) /* read h4 */
425 {
426 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
427
428 return 1; /* return error */
429 }
430 handle->par_h4 = (int8_t)reg; /* set h4 */
431 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_H5, (uint8_t *)&reg, 1) != 0) /* read h5 */
432 {
433 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
434
435 return 1; /* return error */
436 }
437 handle->par_h5 = (int8_t)reg; /* set h5 */
438 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_H6, (uint8_t *)&reg, 1) != 0) /* read h6 */
439 {
440 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
441
442 return 1; /* return error */
443 }
444 handle->par_h6 = (uint8_t)reg; /* set h6 */
445 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_H7, (uint8_t *)&reg, 1) != 0) /* read h7 */
446 {
447 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
448
449 return 1; /* return error */
450 }
451 handle->par_h7 = (int8_t)reg; /* set h7 */
452 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_G1, (uint8_t *)&reg, 1) != 0) /* read gh1 */
453 {
454 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
455
456 return 1; /* return error */
457 }
458 handle->par_gh1 = (int8_t)reg; /* set par gh1 */
459 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_G2_L, (uint8_t *)buf, 2) != 0) /* read gh2 */
460 {
461 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
462
463 return 1; /* return error */
464 }
465 handle->par_gh2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]); /* set par gh2 */
466 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_PAR_G3, (uint8_t *)&reg, 1) != 0) /* read gh3 */
467 {
468 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
469
470 return 1; /* return error */
471 }
472 handle->par_gh3 = (int8_t)reg; /* set par gh3 */
473 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_RES_HEAT_RANGE, (uint8_t *)&reg, 1) != 0) /* read res heat range */
474 {
475 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
476
477 return 1; /* return error */
478 }
479 handle->res_heat_range = (uint8_t)((reg >> 4) & 3); /* set res heat range */
480 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_RES_HEAT_VAL, (uint8_t *)&reg, 1) != 0) /* read res heat val */
481 {
482 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
483
484 return 1; /* return error */
485 }
486 handle->res_heat_val = (int8_t)(reg); /* set res heat val */
487 if (a_bme688_iic_spi_read(handle, BME688_REG_NVM_RANGE_SWITCH, (uint8_t *)&reg, 1) != 0) /* read range switch error */
488 {
489 handle->debug_print("bme688: get calibration data failed.\n"); /* get calibration data failed */
490
491 return 1; /* return error */
492 }
493 handle->range_sw_err = (int8_t)((reg >> 4) & 0xF); /* set range switch error */
494 handle->t_fine = 0.0f; /* init 0 */
495 handle->amb_temp = 25; /* set 25c */
496
497 return 0; /* success return 0 */
498}
499
510static uint8_t a_bme688_compensate_temperature(bme688_handle_t *handle, uint32_t raw, float *output)
511{
512 float var1;
513 float var2;
514
515 var1 = ((((float)raw / 16384.0f) - ((float)handle->par_t1 / 1024.0f)) * ((float)handle->par_t2)); /* calculate var1 data */
516 var2 = (((((float)raw / 131072.0f) - ((float)handle->par_t1 / 8192.0f)) *
517 (((float)raw / 131072.0f) - ((float)handle->par_t1 / 8192.0f))) * ((float)handle->par_t3 * 16.0f)); /* calculate var2 data */
518 handle->t_fine = (var1 + var2); /* t_fine value */
519 *output = ((handle->t_fine) / 5120.0f); /* compensated temperature data */
520
521 return 0; /* success return 0 */
522}
523
534static uint8_t a_bme688_compensate_pressure(bme688_handle_t *handle, uint32_t raw, float *output)
535{
536 float var1;
537 float var2;
538 float var3;
539 float calc_pres;
540
541 var1 = (((float)handle->t_fine / 2.0f) - 64000.0f); /* set var1 */
542 var2 = var1 * var1 * (((float)handle->par_p6) / (131072.0f)); /* set var2 */
543 var2 = var2 + (var1 * ((float)handle->par_p5) * 2.0f); /* set var2 */
544 var2 = (var2 / 4.0f) + (((float)handle->par_p4) * 65536.0f); /* set var2 */
545 var1 = (((((float)handle->par_p3 * var1 * var1) / 16384.0f) +
546 ((float)handle->par_p2 * var1)) / 524288.0f); /* set var1 */
547 var1 = ((1.0f + (var1 / 32768.0f)) * ((float)handle->par_p1)); /* set var1 */
548 calc_pres = (1048576.0f - ((float)raw)); /* set pressure */
549 if ((int32_t)var1 != 0) /* avoid exception caused by division by zero */
550 {
551 calc_pres = (((calc_pres - (var2 / 4096.0f)) * 6250.0f) / var1); /* set pressure */
552 var1 = (((float)handle->par_p9) * calc_pres * calc_pres) / 2147483648.0f; /* set var1 */
553 var2 = calc_pres * (((float)handle->par_p8) / 32768.0f); /* set var2 */
554 var3 = ((calc_pres / 256.0f) * (calc_pres / 256.0f) * (calc_pres / 256.0f) *
555 ((float)handle->par_p10 / 131072.0f)); /* set var3 */
556 calc_pres = (calc_pres + (var1 + var2 + var3 + ((float)handle->par_p7 * 128.0f)) / 16.0f); /* set pressure */
557 }
558 else
559 {
560 calc_pres = 0.0f; /* set 0 */
561 }
562 *output= calc_pres; /* save to output */
563
564 return 0; /* success return 0 */
565}
566
577static uint8_t a_bme688_compensate_humidity(bme688_handle_t *handle, uint16_t raw, float *output)
578{
579 float calc_hum;
580 float var1;
581 float var2;
582 float var3;
583 float var4;
584 float temp_comp;
585
586 temp_comp = ((handle->t_fine) / 5120.0f); /* compensated temperature data*/
587 var1 = (float)((float)raw) -
588 (((float)handle->par_h1 * 16.0f) + (((float)handle->par_h3 / 2.0f) * temp_comp)); /* set var1 */
589 var2 = var1 * ((float)(((float)handle->par_h2 / 262144.0f) *
590 (1.0f + (((float)handle->par_h4 / 16384.0f) * temp_comp) +
591 (((float)handle->par_h5 / 1048576.0f) * temp_comp * temp_comp)))); /* set var2 */
592 var3 = (float)handle->par_h6 / 16384.0f; /* set var3 */
593 var4 = (float)handle->par_h7 / 2097152.0f; /* set var4 */
594 calc_hum = var2 + ((var3 + (var4 * temp_comp)) * var2 * var2); /* set humidity */
595 if (calc_hum > 100.0f) /* check max range */
596 {
597 calc_hum = 100.0f; /* set max */
598 }
599 else if (calc_hum < 0.0f) /* check min range */
600 {
601 calc_hum = 0.0f; /* set min */
602 }
603 else
604 {
605 /* do nothing */
606 }
607 *output = calc_hum; /* save to output */
608
609 return 0; /* success return 0 */
610}
611
622static uint8_t a_bme688_compensate_heat(bme688_handle_t *handle, float temp, uint8_t *reg)
623{
624 float var1;
625 float var2;
626 float var3;
627 float var4;
628 float var5;
629 uint8_t res_heat;
630
631 if (temp > 400.0f) /* cap temperature */
632 {
633 temp = 400.0f; /* set 400 */
634 }
635 var1 = (((float)handle->par_gh1 / (16.0f)) + 49.0f); /* set var1 */
636 var2 = ((((float)handle->par_gh2 / (32768.0f)) * (0.0005f)) + 0.00235f); /* set var2 */
637 var3 = ((float)handle->par_gh3 / (1024.0f)); /* set var3 */
638 var4 = (var1 * (1.0f + (var2 * (float)temp))); /* set var4 */
639 var5 = (var4 + (var3 * (float)handle->amb_temp)); /* set var5 */
640 res_heat = (uint8_t)(3.4f * ((var5 * (4 / (4 + (float)handle->res_heat_range)) *
641 (1 / (1 + ((float)handle->res_heat_val * 0.002f)))) - 25)); /* set res heat */
642 *reg = res_heat; /* set res */
643
644 return 0; /* success return 0 */
645}
646
658static uint8_t a_bme688_compensate_gas_resistance(bme688_handle_t *handle, uint16_t gas_res_adc, uint8_t gas_range, float *ohms)
659{
660 float calc_gas_res;
661
662 uint32_t var1 = (uint32_t)262144 >> gas_range; /* set var1 */
663 int32_t var2 = (int32_t)gas_res_adc - (int32_t)512; /* set var2 */
664 var2 *= (int32_t)3; /* set var2 */
665 var2 = (int32_t)4096 + var2; /* set var2 */
666 calc_gas_res = 1000000.0f * (float)var1 / (float)var2; /* set calc_gas_res */
667 *ohms = calc_gas_res; /* save gas resistance to ohms */
668
669 return 0; /* success return 0 */
670}
671
678static uint8_t a_bme688_calculate_parallel_counter(bme688_handle_t *handle)
679{
680 uint8_t i;
681 uint8_t cnt;
682
683 cnt = 0; /* init 0 */
684 for (i = 0; i < 16; i++) /* 16 times */
685 {
686 if ((handle->parallel_data_flag >> (15 - i)) & 0x01) /* check flag */
687 {
688 cnt++; /* cnt++ */
689 }
690 }
691
692 return cnt; /* return counter */
693}
694
705{
706 if (handle == NULL) /* check handle */
707 {
708 return 2; /* return error */
709 }
710
711 handle->iic_addr = (uint8_t)addr_pin; /* set iic address */
712
713 return 0; /* success return 0 */
714}
715
726{
727 if (handle == NULL) /* check handle */
728 {
729 return 2; /* return error */
730 }
731
732 *addr_pin = (bme688_address_t)handle->iic_addr; /* get iic address */
733
734 return 0; /* success return 0 */
735}
736
747{
748 if (handle == NULL) /* check handle */
749 {
750 return 2; /* return error */
751 }
752
753 handle->iic_spi = (uint8_t)interface; /* set interface */
754
755 return 0; /* success return 0 */
756}
757
768{
769 if (handle == NULL) /* check handle */
770 {
771 return 2; /* return error */
772 }
773
774 *interface = (bme688_interface_t)(handle->iic_spi); /* get interface */
775
776 return 0; /* success return 0 */
777}
778
793{
794 uint8_t i;
795 uint8_t id;
796 uint8_t reg;
797
798 if (handle == NULL) /* check handle */
799 {
800 return 2; /* return error */
801 }
802 if (handle->debug_print == NULL) /* check debug_print */
803 {
804 return 3; /* return error */
805 }
806 if (handle->iic_init == NULL) /* check iic_init */
807 {
808 handle->debug_print("bme688: iic_init is null.\n"); /* iic_init is nul */
809
810 return 3; /* return error */
811 }
812 if (handle->iic_deinit == NULL) /* check iic_deinit */
813 {
814 handle->debug_print("bme688: iic_deinit is null.\n"); /* iic_deinit is null */
815
816 return 3; /* return error */
817 }
818 if (handle->iic_read == NULL) /* check iic_read */
819 {
820 handle->debug_print("bme688: iic_read is null.\n"); /* iic_read is null */
821
822 return 3; /* return error */
823 }
824 if (handle->iic_write == NULL) /* check iic_write */
825 {
826 handle->debug_print("bme688: iic_write is null.\n"); /* iic_write is null */
827
828 return 3; /* return error */
829 }
830 if (handle->spi_init == NULL) /* check spi_init */
831 {
832 handle->debug_print("bme688: spi_init is null.\n"); /* spi_init is nul */
833
834 return 3; /* return error */
835 }
836 if (handle->spi_deinit == NULL) /* check spi_deinit */
837 {
838 handle->debug_print("bme688: spi_deinit is null.\n"); /* spi_deinit is nul */
839
840 return 3; /* return error */
841 }
842 if (handle->spi_read == NULL) /* check spi_read */
843 {
844 handle->debug_print("bme688: spi_read is null.\n"); /* spi_read is nul */
845
846 return 3; /* return error */
847 }
848 if (handle->spi_write == NULL) /* check spi_write */
849 {
850 handle->debug_print("bme688: spi_write is null.\n"); /* spi_write is nul */
851
852 return 3; /* return error */
853 }
854 if (handle->delay_ms == NULL) /* check delay_ms */
855 {
856 handle->debug_print("bme688: delay_ms is null.\n"); /* delay_ms is null */
857
858 return 3; /* return error */
859 }
860 if (handle->receive_callback == NULL) /* check receive_callback */
861 {
862 handle->debug_print("bme688: receive_callback is null.\n"); /* receive_callback is null */
863
864 return 3; /* return error */
865 }
866
867 if (handle->iic_spi == BME688_INTERFACE_IIC) /* iic interface */
868 {
869 if (handle->iic_init() != 0) /* iic init */
870 {
871 handle->debug_print("bme688: iic init failed.\n"); /* iic init failed */
872
873 return 1; /* return error */
874 }
875 }
876 else /* spi interface */
877 {
878 if (handle->spi_init() != 0) /* spi init */
879 {
880 handle->debug_print("bme688: spi init failed.\n"); /* spi init failed */
881
882 return 1; /* return error */
883 }
884 if (a_bme688_change_spi_page(handle, 0) != 0) /* change spi page 0 */
885 {
886 handle->debug_print("bme688: change spi page failed.\n"); /* change spi page failed */
887
888 return 1; /* return error */
889 }
890 }
891
892 if (a_bme688_iic_spi_read(handle, BME688_REG_ID, (uint8_t *)&id, 1) != 0) /* read chip id */
893 {
894 handle->debug_print("bme688: read id failed.\n"); /* read id failed */
895 (void)handle->iic_deinit(); /* iic deinit */
896
897 return 4; /* return error */
898 }
899 if (id != 0x61) /* check id */
900 {
901 handle->debug_print("bme688: id is error.\n"); /* id is error */
902 (void)handle->iic_deinit(); /* iic deinit */
903
904 return 4; /* return error */
905 }
906 if (a_bme688_iic_spi_read(handle, BME688_REG_VARIANT_ID,
907 (uint8_t *)&id, 1) != 0) /* read variant id */
908 {
909 handle->debug_print("bme688: read variant id failed.\n"); /* read variant id failed */
910 (void)handle->iic_deinit(); /* iic deinit */
911
912 return 4; /* return error */
913 }
914 if (id != 0x01) /* check variant id */
915 {
916 handle->debug_print("bme688: variant id is error.\n"); /* variant id is error */
917 (void)handle->iic_deinit(); /* iic deinit */
918
919 return 4; /* return error */
920 }
921 reg = 0xB6; /* set the reset value */
922 if (a_bme688_iic_spi_write(handle, BME688_REG_RESET, &reg, 1) != 0) /* reset the chip */
923 {
924 handle->debug_print("bme688: reset failed.\n"); /* reset failed */
925 (void)handle->iic_deinit(); /* iic deinit */
926
927 return 5; /* return error */
928 }
929 handle->delay_ms(5); /* delay 5ms */
930 if (a_bme688_get_nvm_calibration(handle) != 0) /* get nvm calibration */
931 {
932 (void)handle->iic_deinit(); /* iic deinit */
933
934 return 6; /* return error */
935 }
936 for (i = 0; i < 3; i++) /* set all 0 */
937 {
938 handle->parallel_sequence_trigger[i] = 0; /* init 0 */
939 handle->parallel_sequence_number[i] = 0; /* init 0 */
940 }
941 handle->parallel_data_len = 0; /* init 0 */
942 handle->parallel_data_flag = 0; /* init 0 */
943 handle->inited = 1; /* flag finish initialization */
944
945 return 0; /* success return 0 */
946}
947
960{
961 uint8_t prev;
962
963 if (handle == NULL) /* check handle */
964 {
965 return 2; /* return error */
966 }
967 if (handle->inited != 1) /* check handle initialization */
968 {
969 return 3; /* return error */
970 }
971
972 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
973 {
974 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
975
976 return 4; /* return error */
977 }
978 prev &= ~(3 << 0); /* clear settings */
979 prev |= 0 << 0; /* set sleep mode */
980 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
981 {
982 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
983
984 return 4; /* return error */
985 }
986 if (handle->iic_spi == BME688_INTERFACE_IIC) /* iic interface */
987 {
988 if (handle->iic_deinit() != 0) /* iic deinit */
989 {
990 handle->debug_print("bme688: iic deinit failed.\n"); /* iic deinit failed */
991
992 return 1; /* return error */
993 }
994 }
995 else /* spi interface */
996 {
997 if (handle->spi_deinit() != 0) /* spi deinit */
998 {
999 handle->debug_print("bme688: spi deinit failed.\n"); /* spi deinit failed */
1000
1001 return 1; /* return error */
1002 }
1003 }
1004 handle->inited = 0; /* flag close */
1005
1006 return 0; /* success return 0 */
1007}
1008
1022uint8_t bme688_set_idac_heater(bme688_handle_t *handle, uint8_t index, uint8_t reg)
1023{
1024 uint8_t prev;
1025
1026 if (handle == NULL) /* check handle */
1027 {
1028 return 2; /* return error */
1029 }
1030 if (handle->inited != 1) /* check handle initialization */
1031 {
1032 return 3; /* return error */
1033 }
1034 if (index > 9) /* check index */
1035 {
1036 handle->debug_print("bme688: index > 9.\n"); /* index > 9 */
1037
1038 return 4; /* return error */
1039 }
1040
1041 prev = reg;
1042 if (a_bme688_iic_spi_write(handle, BME688_REG_IDAC_HEAT_X + index, &prev, 1) != 0) /* write idac heater */
1043 {
1044 handle->debug_print("bme688: write idac heater failed.\n"); /* write idac heater failed */
1045
1046 return 1; /* return error */
1047 }
1048
1049 return 0; /* success return 0 */
1050}
1051
1065uint8_t bme688_get_idac_heater(bme688_handle_t *handle, uint8_t index, uint8_t *reg)
1066{
1067 if (handle == NULL) /* check handle */
1068 {
1069 return 2; /* return error */
1070 }
1071 if (handle->inited != 1) /* check handle initialization */
1072 {
1073 return 3; /* return error */
1074 }
1075 if (index > 9) /* check index */
1076 {
1077 handle->debug_print("bme688: index > 9.\n"); /* index > 9 */
1078
1079 return 4; /* return error */
1080 }
1081
1082 if (a_bme688_iic_spi_read(handle, BME688_REG_IDAC_HEAT_X + index, reg, 1) != 0) /* read idac heater */
1083 {
1084 handle->debug_print("bme688: read idac heater failed.\n"); /* read idac heater failed */
1085
1086 return 1; /* return error */
1087 }
1088
1089 return 0; /* success return 0 */
1090}
1091
1105uint8_t bme688_set_resistance_heater(bme688_handle_t *handle, uint8_t index, uint8_t reg)
1106{
1107 uint8_t prev;
1108
1109 if (handle == NULL) /* check handle */
1110 {
1111 return 2; /* return error */
1112 }
1113 if (handle->inited != 1) /* check handle initialization */
1114 {
1115 return 3; /* return error */
1116 }
1117 if (index > 9) /* check index */
1118 {
1119 handle->debug_print("bme688: index > 9.\n"); /* index > 9 */
1120
1121 return 4; /* return error */
1122 }
1123
1124 prev = reg;
1125 if (a_bme688_iic_spi_write(handle, BME688_REG_RES_HEAT_X + index, &prev, 1) != 0) /* write resistance heater */
1126 {
1127 handle->debug_print("bme688: write resistance heater failed.\n"); /* write resistance heater failed */
1128
1129 return 1; /* return error */
1130 }
1131
1132 return 0; /* success return 0 */
1133}
1134
1148uint8_t bme688_get_resistance_heater(bme688_handle_t *handle, uint8_t index, uint8_t *reg)
1149{
1150 if (handle == NULL) /* check handle */
1151 {
1152 return 2; /* return error */
1153 }
1154 if (handle->inited != 1) /* check handle initialization */
1155 {
1156 return 3; /* return error */
1157 }
1158 if (index > 9) /* check index */
1159 {
1160 handle->debug_print("bme688: index > 9.\n"); /* index > 9 */
1161
1162 return 4; /* return error */
1163 }
1164
1165 if (a_bme688_iic_spi_read(handle, BME688_REG_RES_HEAT_X + index, reg, 1) != 0) /* read resistance heater */
1166 {
1167 handle->debug_print("bme688: read resistance heater failed.\n"); /* read resistance heater failed */
1168
1169 return 1; /* return error */
1170 }
1171
1172 return 0; /* success return 0 */
1173}
1174
1188uint8_t bme688_set_gas_wait(bme688_handle_t *handle, uint8_t index, uint8_t reg)
1189{
1190 uint8_t prev;
1191
1192 if (handle == NULL) /* check handle */
1193 {
1194 return 2; /* return error */
1195 }
1196 if (handle->inited != 1) /* check handle initialization */
1197 {
1198 return 3; /* return error */
1199 }
1200 if (index > 9) /* check index */
1201 {
1202 handle->debug_print("bme688: index > 9.\n"); /* index > 9 */
1203
1204 return 4; /* return error */
1205 }
1206
1207 prev = reg;
1208 if (a_bme688_iic_spi_write(handle, BME688_REG_GAS_WAIT_X + index, &prev, 1) != 0) /* write gas wait */
1209 {
1210 handle->debug_print("bme688: write gas wait failed.\n"); /* write gas wait failed */
1211
1212 return 1; /* return error */
1213 }
1214
1215 return 0; /* success return 0 */
1216}
1217
1231uint8_t bme688_get_gas_wait(bme688_handle_t *handle, uint8_t index, uint8_t *reg)
1232{
1233 if (handle == NULL) /* check handle */
1234 {
1235 return 2; /* return error */
1236 }
1237 if (handle->inited != 1) /* check handle initialization */
1238 {
1239 return 3; /* return error */
1240 }
1241 if (index > 9) /* check index */
1242 {
1243 handle->debug_print("bme688: index > 9.\n"); /* index > 9 */
1244
1245 return 4; /* return error */
1246 }
1247
1248 if (a_bme688_iic_spi_read(handle, BME688_REG_GAS_WAIT_X + index, reg, 1) != 0) /* read gas wait */
1249 {
1250 handle->debug_print("bme688: read gas wait failed.\n"); /* read gas wait failed */
1251
1252 return 1; /* return error */
1253 }
1254
1255 return 0; /* success return 0 */
1256}
1257
1269uint8_t bme688_set_gas_wait_shared(bme688_handle_t *handle, uint8_t reg)
1270{
1271 uint8_t prev;
1272
1273 if (handle == NULL) /* check handle */
1274 {
1275 return 2; /* return error */
1276 }
1277 if (handle->inited != 1) /* check handle initialization */
1278 {
1279 return 3; /* return error */
1280 }
1281
1282 prev = reg; /* set param */
1283 if (a_bme688_iic_spi_write(handle, BME688_REG_GAS_WAIT_SHARED, &prev, 1) != 0) /* write gas wait shared */
1284 {
1285 handle->debug_print("bme688: write gas wait shared failed.\n"); /* write gas wait shared failed */
1286
1287 return 1; /* return error */
1288 }
1289
1290 return 0; /* success return 0 */
1291}
1292
1304uint8_t bme688_get_gas_wait_shared(bme688_handle_t *handle, uint8_t *reg)
1305{
1306 uint8_t prev;
1307
1308 if (handle == NULL) /* check handle */
1309 {
1310 return 2; /* return error */
1311 }
1312 if (handle->inited != 1) /* check handle initialization */
1313 {
1314 return 3; /* return error */
1315 }
1316
1317 if (a_bme688_iic_spi_read(handle, BME688_REG_GAS_WAIT_SHARED, &prev, 1) != 0) /* read gas wait shared */
1318 {
1319 handle->debug_print("bme688: read gas wait shared failed.\n"); /* read gas wait shared failed */
1320
1321 return 1; /* return error */
1322 }
1323 *reg = prev; /* set reg */
1324
1325 return 0; /* success return 0 */
1326}
1327
1339{
1340 uint8_t reg;
1341
1342 if (handle == NULL) /* check handle */
1343 {
1344 return 2; /* return error */
1345 }
1346 if (handle->inited != 1) /* check handle initialization */
1347 {
1348 return 3; /* return error */
1349 }
1350
1351 reg = 0xB6; /* set the reset value */
1352 if (a_bme688_iic_spi_write(handle, BME688_REG_RESET, &reg, 1) != 0) /* reset the chip */
1353 {
1354 handle->debug_print("bme688: reset failed.\n"); /* reset failed */
1355
1356 return 1; /* return error */
1357 }
1358 handle->delay_ms(5); /* delay 5ms */
1359
1360 return 0; /* success return 0 */
1361}
1362
1375{
1376 uint8_t prev;
1377
1378 if (handle == NULL) /* check handle */
1379 {
1380 return 2; /* return error */
1381 }
1382 if (handle->inited != 1) /* check handle initialization */
1383 {
1384 return 3; /* return error */
1385 }
1386
1387 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1388 {
1389 handle->debug_print("bme688: read ctrl hum failed.\n"); /* read ctrl hum failed */
1390
1391 return 1; /* return error */
1392 }
1393 prev &= ~(1 << 6); /* clear settings */
1394 prev |= enable << 6; /* set bool */
1395 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_HUM, &prev, 1) != 0) /* write ctrl hum */
1396 {
1397 handle->debug_print("bme688: write ctrl hum failed.\n"); /* write ctrl hum failed */
1398
1399 return 1; /* return error */
1400 }
1401
1402 return 0; /* success return 0 */
1403}
1404
1417{
1418 uint8_t prev;
1419
1420 if (handle == NULL) /* check handle */
1421 {
1422 return 2; /* return error */
1423 }
1424 if (handle->inited != 1) /* check handle initialization */
1425 {
1426 return 3; /* return error */
1427 }
1428
1429 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1430 {
1431 handle->debug_print("bme688: read ctrl hum failed.\n"); /* read ctrl hum failed */
1432
1433 return 1; /* return error */
1434 }
1435 *enable = (bme688_bool_t)((prev >> 6) & 0x01); /* get bool */
1436
1437 return 0; /* success return 0 */
1438}
1439
1452{
1453 uint8_t prev;
1454
1455 if (handle == NULL) /* check handle */
1456 {
1457 return 2; /* return error */
1458 }
1459 if (handle->inited != 1) /* check handle initialization */
1460 {
1461 return 3; /* return error */
1462 }
1463
1464 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1465 {
1466 handle->debug_print("bme688: read ctrl hum failed.\n"); /* read ctrl hum failed */
1467
1468 return 1; /* return error */
1469 }
1470 prev &= ~(7 << 0); /* clear settings */
1471 prev |= oversampling << 0; /* set oversampling */
1472 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_HUM, &prev, 1) != 0) /* write ctrl hum */
1473 {
1474 handle->debug_print("bme688: write ctrl hum failed.\n"); /* write ctrl hum failed */
1475
1476 return 1; /* return error */
1477 }
1478
1479 return 0; /* success return 0 */
1480}
1481
1494{
1495 uint8_t prev;
1496
1497 if (handle == NULL) /* check handle */
1498 {
1499 return 2; /* return error */
1500 }
1501 if (handle->inited != 1) /* check handle initialization */
1502 {
1503 return 3; /* return error */
1504 }
1505
1506 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_HUM, &prev, 1) != 0) /* read ctrl hum */
1507 {
1508 handle->debug_print("bme688: read ctrl hum failed.\n"); /* read ctrl hum failed */
1509
1510 return 1; /* return error */
1511 }
1512 *oversampling = (bme688_oversampling_t)(prev & 0x07); /* get oversampling */
1513
1514 return 0; /* success return 0 */
1515}
1516
1529{
1530 uint8_t prev;
1531
1532 if (handle == NULL) /* check handle */
1533 {
1534 return 2; /* return error */
1535 }
1536 if (handle->inited != 1) /* check handle initialization */
1537 {
1538 return 3; /* return error */
1539 }
1540
1541 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1542 {
1543 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
1544
1545 return 1; /* return error */
1546 }
1547 prev &= ~(7 << 5); /* clear settings */
1548 prev |= oversampling << 5; /* set oversampling */
1549 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
1550 {
1551 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
1552
1553 return 1; /* return error */
1554 }
1555
1556 return 0; /* success return 0 */
1557}
1558
1571{
1572 uint8_t prev;
1573
1574 if (handle == NULL) /* check handle */
1575 {
1576 return 2; /* return error */
1577 }
1578 if (handle->inited != 1) /* check handle initialization */
1579 {
1580 return 3; /* return error */
1581 }
1582
1583 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1584 {
1585 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
1586
1587 return 1; /* return error */
1588 }
1589 *oversampling = (bme688_oversampling_t)((prev >> 5) & 0x7); /* set oversampling */
1590
1591 return 0; /* success return 0 */
1592}
1593
1606{
1607 uint8_t prev;
1608
1609 if (handle == NULL) /* check handle */
1610 {
1611 return 2; /* return error */
1612 }
1613 if (handle->inited != 1) /* check handle initialization */
1614 {
1615 return 3; /* return error */
1616 }
1617
1618 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1619 {
1620 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
1621
1622 return 1; /* return error */
1623 }
1624 prev &= ~(7 << 2); /* clear settings */
1625 prev |= oversampling << 2; /* set oversampling */
1626 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
1627 {
1628 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
1629
1630 return 1; /* return error */
1631 }
1632
1633 return 0; /* success return 0 */
1634}
1635
1648{
1649 uint8_t prev;
1650
1651 if (handle == NULL) /* check handle */
1652 {
1653 return 2; /* return error */
1654 }
1655 if (handle->inited != 1) /* check handle initialization */
1656 {
1657 return 3; /* return error */
1658 }
1659
1660 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1661 {
1662 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
1663
1664 return 1; /* return error */
1665 }
1666 *oversampling = (bme688_oversampling_t)((prev >> 2) & 0x7); /* set oversampling */
1667
1668 return 0; /* success return 0 */
1669}
1670
1683{
1684 uint8_t prev;
1685
1686 if (handle == NULL) /* check handle */
1687 {
1688 return 2; /* return error */
1689 }
1690 if (handle->inited != 1) /* check handle initialization */
1691 {
1692 return 3; /* return error */
1693 }
1694
1695 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1696 {
1697 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
1698
1699 return 1; /* return error */
1700 }
1701 prev &= ~(3 << 0); /* clear settings */
1702 prev |= mode << 0; /* set mode */
1703 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
1704 {
1705 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
1706
1707 return 1; /* return error */
1708 }
1709
1710 return 0; /* success return 0 */
1711}
1712
1725{
1726 uint8_t prev;
1727
1728 if (handle == NULL) /* check handle */
1729 {
1730 return 2; /* return error */
1731 }
1732 if (handle->inited != 1) /* check handle initialization */
1733 {
1734 return 3; /* return error */
1735 }
1736
1737 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
1738 {
1739 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
1740
1741 return 1; /* return error */
1742 }
1743 *mode = (bme688_mode_t)((prev >> 0) & 0x3); /* set mode */
1744
1745 return 0; /* success return 0 */
1746}
1747
1760{
1761 uint8_t prev;
1762
1763 if (handle == NULL) /* check handle */
1764 {
1765 return 2; /* return error */
1766 }
1767 if (handle->inited != 1) /* check handle initialization */
1768 {
1769 return 3; /* return error */
1770 }
1771
1772 if (a_bme688_iic_spi_read(handle, BME688_REG_CONFIG, &prev, 1) != 0) /* read config */
1773 {
1774 handle->debug_print("bme688: read config failed.\n"); /* read config failed */
1775
1776 return 1; /* return error */
1777 }
1778 prev &= ~(7 << 2); /* clear settings */
1779 prev |= (filter & 0x07) << 2; /* set filter */
1780 if (a_bme688_iic_spi_write(handle, BME688_REG_CONFIG, &prev, 1) != 0) /* write config */
1781 {
1782 handle->debug_print("bme688: write config failed.\n"); /* write config failed */
1783
1784 return 1; /* return error */
1785 }
1786
1787 return 0; /* success return 0 */
1788}
1789
1802{
1803 uint8_t prev;
1804
1805 if (handle == NULL) /* check handle */
1806 {
1807 return 2; /* return error */
1808 }
1809 if (handle->inited != 1) /* check handle initialization */
1810 {
1811 return 3; /* return error */
1812 }
1813
1814 if (a_bme688_iic_spi_read(handle, BME688_REG_CONFIG, &prev, 1) != 0) /* read config */
1815 {
1816 handle->debug_print("bme688: read config failed.\n"); /* read config failed */
1817
1818 return 1; /* return error */
1819 }
1820 *filter = (bme688_filter_t)((prev >> 2) & 0x07); /* set filter */
1821
1822 return 0; /* success return 0 */
1823}
1824
1837{
1838 uint8_t prev;
1839
1840 if (handle == NULL) /* check handle */
1841 {
1842 return 2; /* return error */
1843 }
1844 if (handle->inited != 1) /* check handle initialization */
1845 {
1846 return 3; /* return error */
1847 }
1848
1849 if (a_bme688_iic_spi_read(handle, BME688_REG_CONFIG, &prev, 1) != 0) /* read config */
1850 {
1851 handle->debug_print("bme688: read config failed.\n"); /* read config failed */
1852
1853 return 1; /* return error */
1854 }
1855 prev &= ~(1 << 0); /* clear settings */
1856 prev |= spi << 0; /* set spi wire */
1857 if (a_bme688_iic_spi_write(handle, BME688_REG_CONFIG, &prev, 1) != 0) /* write config */
1858 {
1859 handle->debug_print("bme688: write config failed.\n"); /* write config failed */
1860
1861 return 1; /* return error */
1862 }
1863
1864 return 0; /* success return 0 */
1865}
1866
1879{
1880 uint8_t prev;
1881
1882 if (handle == NULL) /* check handle */
1883 {
1884 return 2; /* return error */
1885 }
1886 if (handle->inited != 1) /* check handle initialization */
1887 {
1888 return 3; /* return error */
1889 }
1890
1891 if (a_bme688_iic_spi_read(handle, BME688_REG_CONFIG, &prev, 1) != 0) /* read config */
1892 {
1893 handle->debug_print("bme688: read config failed.\n"); /* read config failed */
1894
1895 return 1; /* return error */
1896 }
1897 *spi = (bme688_spi_wire_t)((prev >> 0) & 0x01); /* get spi */
1898
1899 return 0; /* success return 0 */
1900}
1901
1914{
1915 uint8_t prev;
1916
1917 if (handle == NULL) /* check handle */
1918 {
1919 return 2; /* return error */
1920 }
1921 if (handle->inited != 1) /* check handle initialization */
1922 {
1923 return 3; /* return error */
1924 }
1925
1926 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_GAS_0, &prev, 1) != 0) /* read ctrl gas0 */
1927 {
1928 handle->debug_print("bme688: read ctrl gas0 failed.\n"); /* read ctrl gas0 failed */
1929
1930 return 1; /* return error */
1931 }
1932 prev &= ~(1 << 3); /* clear settings */
1933 prev |= enable << 3; /* set bool */
1934 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_GAS_0, &prev, 1) != 0) /* write ctrl gas0 */
1935 {
1936 handle->debug_print("bme688: write ctrl gas0 failed.\n"); /* write ctrl gas0 failed */
1937
1938 return 1; /* return error */
1939 }
1940
1941 return 0; /* success return 0 */
1942}
1943
1956{
1957 uint8_t prev;
1958
1959 if (handle == NULL) /* check handle */
1960 {
1961 return 2; /* return error */
1962 }
1963 if (handle->inited != 1) /* check handle initialization */
1964 {
1965 return 3; /* return error */
1966 }
1967
1968 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_GAS_0, &prev, 1) != 0) /* read ctrl gas0 */
1969 {
1970 handle->debug_print("bme688: read ctrl gas0 failed.\n"); /* read ctrl gas0 failed */
1971
1972 return 1; /* return error */
1973 }
1974 *enable = (bme688_bool_t)((prev >> 3) & 0x01); /* get bool */
1975
1976 return 0; /* success return 0 */
1977}
1978
1991{
1992 uint8_t prev;
1993
1994 if (handle == NULL) /* check handle */
1995 {
1996 return 2; /* return error */
1997 }
1998 if (handle->inited != 1) /* check handle initialization */
1999 {
2000 return 3; /* return error */
2001 }
2002
2003 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2004 {
2005 handle->debug_print("bme688: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2006
2007 return 1; /* return error */
2008 }
2009 prev &= ~(1 << 5); /* clear settings */
2010 prev |= enable << 5; /* set bool */
2011 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* write ctrl gas1 */
2012 {
2013 handle->debug_print("bme688: write ctrl gas1 failed.\n"); /* write ctrl gas1 failed */
2014
2015 return 1; /* return error */
2016 }
2017
2018 return 0; /* success return 0 */
2019}
2020
2033{
2034 uint8_t prev;
2035
2036 if (handle == NULL) /* check handle */
2037 {
2038 return 2; /* return error */
2039 }
2040 if (handle->inited != 1) /* check handle initialization */
2041 {
2042 return 3; /* return error */
2043 }
2044
2045 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2046 {
2047 handle->debug_print("bme688: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2048
2049 return 1; /* return error */
2050 }
2051 *enable = (bme688_bool_t)((prev >> 5) & 0x01); /* get bool */
2052
2053 return 0; /* success return 0 */
2054}
2055
2068{
2069 uint8_t i;
2070 uint8_t prev;
2071
2072 if (handle == NULL) /* check handle */
2073 {
2074 return 2; /* return error */
2075 }
2076 if (handle->inited != 1) /* check handle initialization */
2077 {
2078 return 3; /* return error */
2079 }
2080
2081 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2082 {
2083 handle->debug_print("bme688: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2084
2085 return 1; /* return error */
2086 }
2087 prev &= ~(0x0F << 0); /* clear settings */
2088 prev |= (profile & 0x0F) << 0; /* set profile */
2089 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* write ctrl gas1 */
2090 {
2091 handle->debug_print("bme688: write ctrl gas1 failed.\n"); /* write ctrl gas1 failed */
2092
2093 return 1; /* return error */
2094 }
2095 for (i = 0; i < 3; i++) /* set all 0 */
2096 {
2097 handle->parallel_sequence_trigger[i] = 0; /* init 0 */
2098 handle->parallel_sequence_number[i] = 0; /* init 0 */
2099 }
2100 handle->parallel_data_len = (uint8_t)profile; /* set parallel data length */
2101 handle->parallel_data_flag = 0; /* flag init 0 */
2102
2103 return 0; /* success return 0 */
2104}
2105
2118{
2119 uint8_t prev;
2120
2121 if (handle == NULL) /* check handle */
2122 {
2123 return 2; /* return error */
2124 }
2125 if (handle->inited != 1) /* check handle initialization */
2126 {
2127 return 3; /* return error */
2128 }
2129
2130 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
2131 {
2132 handle->debug_print("bme688: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
2133
2134 return 1; /* return error */
2135 }
2136 *profile = (bme688_heater_profile_t)(prev & 0x0F); /* set profile */
2137
2138 return 0; /* success return 0 */
2139}
2140
2152uint8_t bme688_resistance_heater_convert_to_register(bme688_handle_t *handle, float degree_celsius, uint8_t *reg)
2153{
2154 if (handle == NULL) /* check handle */
2155 {
2156 return 2; /* return error */
2157 }
2158 if (handle->inited != 1) /* check handle initialization */
2159 {
2160 return 3; /* return error */
2161 }
2162
2163 (void)a_bme688_compensate_heat(handle, degree_celsius, reg); /* convert real data to register data */
2164
2165 return 0; /* success return 0 */
2166}
2167
2179uint8_t bme688_idac_heater_convert_to_register(bme688_handle_t *handle, float ma, uint8_t *reg)
2180{
2181 if (handle == NULL) /* check handle */
2182 {
2183 return 2; /* return error */
2184 }
2185 if (handle->inited != 1) /* check handle initialization */
2186 {
2187 return 3; /* return error */
2188 }
2189
2190 *reg = ((uint8_t)(ma * 8) - 1) << 1; /* convert real data to register data */
2191
2192 return 0; /* success return 0 */
2193}
2194
2206uint8_t bme688_idac_heater_convert_to_data(bme688_handle_t *handle, uint8_t reg, float *ma)
2207{
2208 if (handle == NULL) /* check handle */
2209 {
2210 return 2; /* return error */
2211 }
2212 if (handle->inited != 1) /* check handle initialization */
2213 {
2214 return 3; /* return error */
2215 }
2216
2217 *ma = (float)(((reg >> 1) + 1) / 8); /* convert raw data to real data */
2218
2219 return 0; /* success return 0 */
2220}
2221
2234uint8_t bme688_gas_wait_convert_to_register(bme688_handle_t *handle, uint16_t ms, uint8_t *reg)
2235{
2236 uint8_t factor = 0;
2237
2238 if (handle == NULL) /* check handle */
2239 {
2240 return 2; /* return error */
2241 }
2242 if (handle->inited != 1) /* check handle initialization */
2243 {
2244 return 3; /* return error */
2245 }
2246
2247 if (ms >= 0xFC0) /* check ms */
2248 {
2249 handle->debug_print("bme688: ms >= 0x0FC0.\n"); /* ms >= 0x0FC0 */
2250
2251 return 1; /* return error */
2252 }
2253 else
2254 {
2255 while (ms > 0x3F) /* check ms */
2256 {
2257 ms = ms / 4; /* div 4 */
2258 factor += 1; /* factor */
2259 }
2260
2261 *reg = (uint8_t)(ms + (factor * 64)); /* set reg */
2262 }
2263
2264 return 0; /* success return 0 */
2265}
2266
2280{
2281 uint16_t ms;
2282 uint8_t factor = 0;
2283
2284 if (handle == NULL) /* check handle */
2285 {
2286 return 2; /* return error */
2287 }
2288 if (handle->inited != 1) /* check handle initialization */
2289 {
2290 return 3; /* return error */
2291 }
2292
2293 ms = (uint16_t)m; /* set ms */
2294 if (ms >= 0x0783U) /* check ms */
2295 {
2296 handle->debug_print("bme688: ms >= 0x0783.\n"); /* ms >= 0x0783 */
2297
2298 return 1; /* return error */
2299 }
2300 else
2301 {
2302 ms = (uint16_t)(((uint32_t)ms * 1000) / 477); /* set ms */
2303 while (ms > 0x3F) /* check ms */
2304 {
2305 ms = ms >> 2; /* right shift 2 */
2306 factor += 1; /* add 1 */
2307 }
2308
2309 *reg = (uint8_t)(ms + (factor * 64)); /* set reg */
2310 }
2311
2312 return 0; /* success return 0 */
2313}
2314
2328{
2329 uint8_t reg;
2330 uint8_t prev;
2331
2332 if (handle == NULL) /* check handle */
2333 {
2334 return 2; /* return error */
2335 }
2336 if (handle->inited != 1) /* check handle initialization */
2337 {
2338 return 3; /* return error */
2339 }
2340
2341 if (section == BME688_SECTION_0) /* section 0 */
2342 {
2343 reg = BME688_REG_MEAS_STATUS_0; /* set section 0 */
2344 }
2345 else if (section == BME688_SECTION_1) /* section 1 */
2346 {
2347 reg = BME688_REG_MEAS_STATUS_1; /* set section 1 */
2348 }
2349 else /* section 2 */
2350 {
2351 reg = BME688_REG_MEAS_STATUS_2; /* set section 2 */
2352 }
2353
2354 if (a_bme688_iic_spi_read(handle, reg, &prev, 1) != 0) /* read status */
2355 {
2356 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
2357
2358 return 1; /* return error */
2359 }
2360 *enable = (bme688_bool_t)((prev >> 7) & 0x01); /* set bool */
2361
2362 return 0; /* success return 0 */
2363}
2364
2378{
2379 uint8_t reg;
2380 uint8_t prev;
2381
2382 if (handle == NULL) /* check handle */
2383 {
2384 return 2; /* return error */
2385 }
2386 if (handle->inited != 1) /* check handle initialization */
2387 {
2388 return 3; /* return error */
2389 }
2390
2391 if (section == BME688_SECTION_0) /* section 0 */
2392 {
2393 reg = BME688_REG_MEAS_STATUS_0; /* set section 0 */
2394 }
2395 else if (section == BME688_SECTION_1) /* section 1 */
2396 {
2397 reg = BME688_REG_MEAS_STATUS_1; /* set section 1 */
2398 }
2399 else /* section 2 */
2400 {
2401 reg = BME688_REG_MEAS_STATUS_2; /* set section 2 */
2402 }
2403
2404 if (a_bme688_iic_spi_read(handle, reg, &prev, 1) != 0) /* read status */
2405 {
2406 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
2407
2408 return 1; /* return error */
2409 }
2410 *enable = (bme688_bool_t)((prev >> 6) & 0x01); /* set bool */
2411
2412 return 0; /* success return 0 */
2413}
2414
2428{
2429 uint8_t reg;
2430 uint8_t prev;
2431
2432 if (handle == NULL) /* check handle */
2433 {
2434 return 2; /* return error */
2435 }
2436 if (handle->inited != 1) /* check handle initialization */
2437 {
2438 return 3; /* return error */
2439 }
2440
2441 if (section == BME688_SECTION_0) /* section 0 */
2442 {
2443 reg = BME688_REG_MEAS_STATUS_0; /* set section 0 */
2444 }
2445 else if (section == BME688_SECTION_1) /* section 1 */
2446 {
2447 reg = BME688_REG_MEAS_STATUS_1; /* set section 1 */
2448 }
2449 else /* section 2 */
2450 {
2451 reg = BME688_REG_MEAS_STATUS_2; /* set section 2 */
2452 }
2453
2454 if (a_bme688_iic_spi_read(handle, reg, &prev, 1) != 0) /* read status */
2455 {
2456 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
2457
2458 return 1; /* return error */
2459 }
2460 *enable = (bme688_bool_t)((prev >> 5) & 0x01); /* set bool */
2461
2462 return 0; /* success return 0 */
2463}
2464
2477uint8_t bme688_get_gas_measuring_index(bme688_handle_t *handle, bme688_section_t section, uint8_t *index)
2478{
2479 uint8_t reg;
2480 uint8_t prev;
2481
2482 if (handle == NULL) /* check handle */
2483 {
2484 return 2; /* return error */
2485 }
2486 if (handle->inited != 1) /* check handle initialization */
2487 {
2488 return 3; /* return error */
2489 }
2490
2491 if (section == BME688_SECTION_0) /* section 0 */
2492 {
2493 reg = BME688_REG_MEAS_STATUS_0; /* set section 0 */
2494 }
2495 else if (section == BME688_SECTION_1) /* section 1 */
2496 {
2497 reg = BME688_REG_MEAS_STATUS_1; /* set section 1 */
2498 }
2499 else /* section 2 */
2500 {
2501 reg = BME688_REG_MEAS_STATUS_2; /* set section 2 */
2502 }
2503
2504 if (a_bme688_iic_spi_read(handle, reg, &prev, 1) != 0) /* read status */
2505 {
2506 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
2507
2508 return 1; /* return error */
2509 }
2510 *index = (prev & 0xF); /* set index */
2511
2512 return 0; /* success return 0 */
2513}
2514
2528{
2529 uint8_t reg;
2530 uint8_t prev;
2531
2532 if (handle == NULL) /* check handle */
2533 {
2534 return 2; /* return error */
2535 }
2536 if (handle->inited != 1) /* check handle initialization */
2537 {
2538 return 3; /* return error */
2539 }
2540
2541 if (section == BME688_SECTION_0) /* section 0 */
2542 {
2543 reg = BME688_REG_GAS_R_LSB_0; /* set section 0 */
2544 }
2545 else if (section == BME688_SECTION_1) /* section 1 */
2546 {
2547 reg = BME688_REG_GAS_R_LSB_1; /* set section 1 */
2548 }
2549 else /* section 2 */
2550 {
2551 reg = BME688_REG_GAS_R_LSB_2; /* set section 2 */
2552 }
2553
2554 if (a_bme688_iic_spi_read(handle, reg, &prev, 1) != 0) /* read gas lsb */
2555 {
2556 handle->debug_print("bme688: read gas lsb failed.\n"); /* read gas lsb failed */
2557
2558 return 1; /* return error */
2559 }
2560 *enable = (bme688_bool_t)((prev >> 5) & 0x01); /* set bool */
2561
2562 return 0; /* success return 0 */
2563}
2564
2578{
2579 uint8_t reg;
2580 uint8_t prev;
2581
2582 if (handle == NULL) /* check handle */
2583 {
2584 return 2; /* return error */
2585 }
2586 if (handle->inited != 1) /* check handle initialization */
2587 {
2588 return 3; /* return error */
2589 }
2590
2591 if (section == BME688_SECTION_0) /* section 0 */
2592 {
2593 reg = BME688_REG_GAS_R_LSB_0; /* set section 0 */
2594 }
2595 else if (section == BME688_SECTION_1) /* section 1 */
2596 {
2597 reg = BME688_REG_GAS_R_LSB_1; /* set section 1 */
2598 }
2599 else /* section 2 */
2600 {
2601 reg = BME688_REG_GAS_R_LSB_2; /* set section 2 */
2602 }
2603
2604 if (a_bme688_iic_spi_read(handle, reg, &prev, 1) != 0) /* read gas lsb */
2605 {
2606 handle->debug_print("bme688: read gas lsb failed.\n"); /* read gas lsb failed */
2607
2608 return 1; /* return error */
2609 }
2610 *enable = (bme688_bool_t)((prev >> 4) & 0x01); /* set bool */
2611
2612 return 0; /* success return 0 */
2613}
2614
2628{
2629 uint8_t reg;
2630 uint8_t prev;
2631
2632 if (handle == NULL) /* check handle */
2633 {
2634 return 2; /* return error */
2635 }
2636 if (handle->inited != 1) /* check handle initialization */
2637 {
2638 return 3; /* return error */
2639 }
2640
2641 if (section == BME688_SECTION_0) /* section 0 */
2642 {
2643 reg = BME688_REG_SUB_MEAS_INDEX_0; /* set section 0 */
2644 }
2645 else if (section == BME688_SECTION_1) /* section 1 */
2646 {
2647 reg = BME688_REG_SUB_MEAS_INDEX_1; /* set section 1 */
2648 }
2649 else /* section 2 */
2650 {
2651 reg = BME688_REG_SUB_MEAS_INDEX_2; /* set section 2 */
2652 }
2653
2654 if (a_bme688_iic_spi_read(handle, reg, &prev, 1) != 0) /* get sub measurement index*/
2655 {
2656 handle->debug_print("bme688: get sub measurement index failed.\n"); /* get sub measurement index failed */
2657
2658 return 1; /* return error */
2659 }
2660 *ind = prev; /* set number */
2661
2662 return 0; /* success return 0 */
2663}
2664
2684uint8_t bme688_read_gas_resistance(bme688_handle_t *handle, bme688_section_t section, uint16_t *adc_raw, uint8_t *adc_range, float *ohms, uint8_t *index)
2685{
2686 uint8_t res;
2687 uint8_t reg_status;
2688 uint8_t reg_gas;
2689 uint8_t prev;
2690 uint32_t timeout;
2691 uint8_t buf[2];
2692
2693 if (handle == NULL) /* check handle */
2694 {
2695 return 2; /* return error */
2696 }
2697 if (handle->inited != 1) /* check handle initialization */
2698 {
2699 return 3; /* return error */
2700 }
2701
2702 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2703 {
2704 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
2705
2706 return 1; /* return error */
2707 }
2708
2709 if ((prev & 0x03) != (uint8_t)BME688_MODE_PARALLEL) /* sleep or forced mode */
2710 {
2711 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2712 {
2713 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
2714
2715 return 1; /* return error */
2716 }
2717 prev &= ~(3 << 0); /* clear settings */
2718 prev |= 0x01 << 0; /* set forced mode */
2719 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2720 {
2721 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
2722
2723 return 1; /* return error */
2724 }
2725 timeout = 10 * 1000; /* set timeout */
2726 while (timeout != 0) /* check timeout */
2727 {
2728 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2729 {
2730 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
2731
2732 return 1; /* return error */
2733 }
2734 if ((prev & 0x03) == 0) /* if finished */
2735 {
2736 break; /* break */
2737 }
2738 handle->delay_ms(1); /* delay 1ms */
2739 timeout--; /* timeout-- */
2740 }
2741 if (timeout == 0) /* check timeout */
2742 {
2743 handle->debug_print("bme688: read timeout.\n"); /* read timeout */
2744
2745 return 5; /* return error */
2746 }
2747
2748 if (section == BME688_SECTION_0) /* section 0 */
2749 {
2750 reg_gas = BME688_REG_GAS_R_MSB_0; /* set section 0 */
2751 reg_status = BME688_REG_MEAS_STATUS_0; /* set section 0 */
2752 }
2753 else if (section == BME688_SECTION_1) /* section 1 */
2754 {
2755 reg_gas = BME688_REG_GAS_R_MSB_1; /* set section 1 */
2756 reg_status = BME688_REG_MEAS_STATUS_1; /* set section 1 */
2757 }
2758 else /* section 2 */
2759 {
2760 reg_gas = BME688_REG_GAS_R_MSB_2; /* set section 2 */
2761 reg_status = BME688_REG_MEAS_STATUS_2; /* set section 2 */
2762 }
2763
2764 res = a_bme688_iic_spi_read(handle, reg_gas, buf, 2); /* read gas */
2765 if (res != 0) /* check result */
2766 {
2767 handle->debug_print("bme688: read failed.\n"); /* read failed */
2768
2769 return 1; /* return error */
2770 }
2771 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
2772 {
2773 handle->debug_print("bme688: gas is invalid.\n"); /* gas is invalid */
2774
2775 return 6; /* return error */
2776 }
2777 if ((buf[1] & (1 << 4)) == 0) /* check heat */
2778 {
2779 handle->debug_print("bme688: heater is not stable.\n"); /* heater is not stable */
2780
2781 return 7; /* return error */
2782 }
2783 *adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
2784 *adc_range = buf[1] & 0x0F; /* set adc range */
2785 res = a_bme688_compensate_gas_resistance(handle, *adc_raw , *adc_range, ohms); /* compensate gas resistance */
2786 if (res != 0) /* check result */
2787 {
2788 handle->debug_print("bme688: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
2789
2790 return 4; /* return error */
2791 }
2792 if (a_bme688_iic_spi_read(handle, reg_status, &prev, 1) != 0) /* read status */
2793 {
2794 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
2795
2796 return 1; /* return error */
2797 }
2798 *index = (prev & 0xF); /* set index */
2799 }
2800 else
2801 {
2802 if (section == BME688_SECTION_0) /* section 0 */
2803 {
2804 reg_gas = BME688_REG_GAS_R_MSB_0; /* set section 0 */
2805 reg_status = BME688_REG_MEAS_STATUS_0; /* set section 0 */
2806 }
2807 else if (section == BME688_SECTION_1) /* section 1 */
2808 {
2809 reg_gas = BME688_REG_GAS_R_MSB_1; /* set section 1 */
2810 reg_status = BME688_REG_MEAS_STATUS_1; /* set section 1 */
2811 }
2812 else /* section 2 */
2813 {
2814 reg_gas = BME688_REG_GAS_R_MSB_2; /* set section 2 */
2815 reg_status = BME688_REG_MEAS_STATUS_2; /* set section 2 */
2816 }
2817
2818 res = a_bme688_iic_spi_read(handle, reg_gas, buf, 2); /* read gas */
2819 if (res != 0) /* check result */
2820 {
2821 handle->debug_print("bme688: read failed.\n"); /* read failed */
2822
2823 return 1; /* return error */
2824 }
2825 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
2826 {
2827 handle->debug_print("bme688: gas is invalid.\n"); /* gas is invalid */
2828
2829 return 6; /* return error */
2830 }
2831 if ((buf[1] & (1 << 4)) == 0) /* check heat */
2832 {
2833 handle->debug_print("bme688: heater is not stable.\n"); /* heater is not stable */
2834
2835 return 7; /* return error */
2836 }
2837 *adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
2838 *adc_range = buf[1] & 0x0F; /* set adc range */
2839 res = a_bme688_compensate_gas_resistance(handle, *adc_raw , *adc_range, ohms); /* compensate gas resistance */
2840 if (res != 0) /* check result */
2841 {
2842 handle->debug_print("bme688: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
2843
2844 return 4; /* return error */
2845 }
2846 if (a_bme688_iic_spi_read(handle, reg_status, &prev, 1) != 0) /* read status */
2847 {
2848 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
2849
2850 return 1; /* return error */
2851 }
2852 *index = (prev & 0xF); /* set index */
2853 }
2854
2855 return 0; /* success return 0 */
2856}
2857
2873uint8_t bme688_read_pressure(bme688_handle_t *handle, bme688_section_t section, uint32_t *pressure_raw, float *pressure_pa)
2874{
2875 uint8_t res;
2876 uint8_t reg_press;
2877 uint8_t prev;
2878 uint32_t timeout;
2879 uint32_t temperature_raw;
2880 float temperature_c;
2881 uint8_t buf[6];
2882
2883 if (handle == NULL) /* check handle */
2884 {
2885 return 2; /* return error */
2886 }
2887 if (handle->inited != 1) /* check handle initialization */
2888 {
2889 return 3; /* return error */
2890 }
2891
2892 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2893 {
2894 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
2895
2896 return 1; /* return error */
2897 }
2898
2899 if ((prev & 0x03) != (uint8_t)BME688_MODE_PARALLEL) /* sleep or forced mode */
2900 {
2901 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2902 {
2903 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
2904
2905 return 1; /* return error */
2906 }
2907 prev &= ~(3 << 0); /* clear settings */
2908 prev |= 0x01 << 0; /* set forced mode */
2909 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
2910 {
2911 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
2912
2913 return 1; /* return error */
2914 }
2915 timeout = 10 * 1000; /* set timeout */
2916 while (timeout != 0) /* check timeout */
2917 {
2918 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
2919 {
2920 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
2921
2922 return 1; /* return error */
2923 }
2924 if ((prev & 0x03) == 0) /* if finished */
2925 {
2926 break; /* break */
2927 }
2928 handle->delay_ms(1); /* delay 1ms */
2929 timeout--; /* timeout-- */
2930 }
2931 if (timeout == 0) /* check timeout */
2932 {
2933 handle->debug_print("bme688: read timeout.\n"); /* read timeout */
2934
2935 return 5; /* return error */
2936 }
2937
2938 if (section == BME688_SECTION_0) /* section 0 */
2939 {
2940 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
2941 }
2942 else if (section == BME688_SECTION_1) /* section 1 */
2943 {
2944 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
2945 }
2946 else /* section 2 */
2947 {
2948 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
2949 }
2950
2951 res = a_bme688_iic_spi_read(handle, reg_press, buf, 6); /* read temperature and pressure */
2952 if (res != 0) /* check result */
2953 {
2954 handle->debug_print("bme688: read failed.\n"); /* read failed */
2955
2956 return 1; /* return error */
2957 }
2958 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
2959 (((uint32_t)(buf[4])) << 4) |
2960 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
2961 res = a_bme688_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
2962 if (res != 0) /* check result */
2963 {
2964 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
2965
2966 return 4; /* return error */
2967 }
2968 *pressure_raw = ((((uint32_t)(buf[0])) << 12) |
2969 (((uint32_t)(buf[1])) << 4) |
2970 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
2971 res = a_bme688_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
2972 if (res != 0) /* check result */
2973 {
2974 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
2975
2976 return 4; /* return error */
2977 }
2978 }
2979 else
2980 {
2981 if (section == BME688_SECTION_0) /* section 0 */
2982 {
2983 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
2984 }
2985 else if (section == BME688_SECTION_1) /* section 1 */
2986 {
2987 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
2988 }
2989 else /* section 2 */
2990 {
2991 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
2992 }
2993
2994 res = a_bme688_iic_spi_read(handle, reg_press, buf, 6); /* read temperature and pressure */
2995 if (res != 0) /* check result */
2996 {
2997 handle->debug_print("bme688: read failed.\n"); /* read failed */
2998
2999 return 1; /* return error */
3000 }
3001 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3002 (((uint32_t)(buf[4])) << 4) |
3003 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3004 res = a_bme688_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
3005 if (res != 0) /* check result */
3006 {
3007 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3008
3009 return 4; /* return error */
3010 }
3011 *pressure_raw = ((((uint32_t)(buf[0])) << 12) |
3012 (((uint32_t)(buf[1])) << 4) |
3013 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
3014 res = a_bme688_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
3015 if (res != 0) /* check result */
3016 {
3017 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
3018
3019 return 4; /* return error */
3020 }
3021 }
3022
3023 return 0; /* success return 0 */
3024}
3025
3041uint8_t bme688_read_temperature(bme688_handle_t *handle, bme688_section_t section, uint32_t *temperature_raw, float *temperature_c)
3042{
3043 uint8_t res;
3044 uint8_t reg_press;
3045 uint8_t prev;
3046 uint32_t timeout;
3047 uint8_t buf[6];
3048
3049 if (handle == NULL) /* check handle */
3050 {
3051 return 2; /* return error */
3052 }
3053 if (handle->inited != 1) /* check handle initialization */
3054 {
3055 return 3; /* return error */
3056 }
3057
3058 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3059 {
3060 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3061
3062 return 1; /* return error */
3063 }
3064
3065 if ((prev & 0x03) != (uint8_t)BME688_MODE_PARALLEL) /* sleep or forced mode */
3066 {
3067 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3068 {
3069 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3070
3071 return 1; /* return error */
3072 }
3073 prev &= ~(3 << 0); /* clear settings */
3074 prev |= 0x01 << 0; /* set forced mode */
3075 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
3076 {
3077 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
3078
3079 return 1; /* return error */
3080 }
3081 timeout = 10 * 1000; /* set timeout */
3082 while (timeout != 0) /* check timeout */
3083 {
3084 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3085 {
3086 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3087
3088 return 1; /* return error */
3089 }
3090 if ((prev & 0x03) == 0) /* if finished */
3091 {
3092 break; /* break */
3093 }
3094 handle->delay_ms(1); /* delay 1ms */
3095 timeout--; /* timeout-- */
3096 }
3097 if (timeout == 0) /* check timeout */
3098 {
3099 handle->debug_print("bme688: read timeout.\n"); /* read timeout */
3100
3101 return 5; /* return error */
3102 }
3103
3104 if (section == BME688_SECTION_0) /* section 0 */
3105 {
3106 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3107 }
3108 else if (section == BME688_SECTION_1) /* section 1 */
3109 {
3110 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3111 }
3112 else /* section 2 */
3113 {
3114 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3115 }
3116
3117 res = a_bme688_iic_spi_read(handle, reg_press, buf, 6); /* read temperature and pressure */
3118 if (res != 0) /* check result */
3119 {
3120 handle->debug_print("bme688: read failed.\n"); /* read failed */
3121
3122 return 1; /* return error */
3123 }
3124 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3125 (((uint32_t)(buf[4])) << 4) |
3126 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3127 res = a_bme688_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
3128 if (res != 0) /* check result */
3129 {
3130 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3131
3132 return 4; /* return error */
3133 }
3134 }
3135 else
3136 {
3137 if (section == BME688_SECTION_0) /* section 0 */
3138 {
3139 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3140 }
3141 else if (section == BME688_SECTION_1) /* section 1 */
3142 {
3143 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3144 }
3145 else /* section 2 */
3146 {
3147 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3148 }
3149
3150 res = a_bme688_iic_spi_read(handle, reg_press, buf, 6); /* read temperature and pressure */
3151 if (res != 0) /* check result */
3152 {
3153 handle->debug_print("bme688: read failed.\n"); /* read failed */
3154
3155 return 1; /* return error */
3156 }
3157 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3158 (((uint32_t)(buf[4])) << 4) |
3159 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3160 res = a_bme688_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
3161 if (res != 0) /* check result */
3162 {
3163 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3164
3165 return 4; /* return error */
3166 }
3167 }
3168
3169 return 0; /* success return 0 */
3170}
3171
3187uint8_t bme688_read_humidity(bme688_handle_t *handle, bme688_section_t section, uint32_t *humidity_raw, float *humidity_percentage)
3188{
3189 uint8_t res;
3190 uint8_t reg_press;
3191 uint8_t prev;
3192 uint32_t timeout;
3193 uint32_t temperature_raw;
3194 float temperature_c;
3195 uint8_t buf[8];
3196
3197 if (handle == NULL) /* check handle */
3198 {
3199 return 2; /* return error */
3200 }
3201 if (handle->inited != 1) /* check handle initialization */
3202 {
3203 return 3; /* return error */
3204 }
3205
3206 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3207 {
3208 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3209
3210 return 1; /* return error */
3211 }
3212
3213 if ((prev & 0x03) != (uint8_t)BME688_MODE_PARALLEL) /* sleep or forced mode */
3214 {
3215 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3216 {
3217 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3218
3219 return 1; /* return error */
3220 }
3221 prev &= ~(3 << 0); /* clear settings */
3222 prev |= 0x01 << 0; /* set forced mode */
3223 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
3224 {
3225 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
3226
3227 return 1; /* return error */
3228 }
3229 timeout = 10 * 1000; /* set timeout */
3230 while (timeout != 0) /* check timeout */
3231 {
3232 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3233 {
3234 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3235
3236 return 1; /* return error */
3237 }
3238 if ((prev & 0x03) == 0) /* if finished */
3239 {
3240 break; /* break */
3241 }
3242 handle->delay_ms(1); /* delay 1ms */
3243 timeout--; /* timeout-- */
3244 }
3245 if (timeout == 0) /* check timeout */
3246 {
3247 handle->debug_print("bme688: read timeout.\n"); /* read timeout */
3248
3249 return 5; /* return error */
3250 }
3251
3252 if (section == BME688_SECTION_0) /* section 0 */
3253 {
3254 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3255 }
3256 else if (section == BME688_SECTION_1) /* section 1 */
3257 {
3258 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3259 }
3260 else /* section 2 */
3261 {
3262 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3263 }
3264
3265 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
3266 if (res != 0) /* check result */
3267 {
3268 handle->debug_print("bme688: read failed.\n"); /* read failed */
3269
3270 return 1; /* return error */
3271 }
3272 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3273 (((uint32_t)(buf[4])) << 4) |
3274 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3275 res = a_bme688_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
3276 if (res != 0) /* check result */
3277 {
3278 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3279
3280 return 4; /* return error */
3281 }
3282 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
3283 res = a_bme688_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
3284 if (res != 0) /* check result */
3285 {
3286 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
3287
3288 return 4; /* return error */
3289 }
3290 }
3291 else
3292 {
3293 if (section == BME688_SECTION_0) /* section 0 */
3294 {
3295 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3296 }
3297 else if (section == BME688_SECTION_1) /* section 1 */
3298 {
3299 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3300 }
3301 else /* section 2 */
3302 {
3303 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3304 }
3305
3306 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
3307 if (res != 0) /* check result */
3308 {
3309 handle->debug_print("bme688: read failed.\n"); /* read failed */
3310
3311 return 1; /* return error */
3312 }
3313 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3314 (((uint32_t)(buf[4])) << 4) |
3315 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3316 res = a_bme688_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
3317 if (res != 0) /* check result */
3318 {
3319 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3320
3321 return 4; /* return error */
3322 }
3323 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
3324 res = a_bme688_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
3325 if (res != 0) /* check result */
3326 {
3327 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
3328
3329 return 4; /* return error */
3330 }
3331 }
3332
3333 return 0; /* success return 0 */
3334}
3335
3356 uint32_t *temperature_raw, float *temperature_c,
3357 uint32_t *pressure_raw, float *pressure_pa,
3358 uint32_t *humidity_raw, float *humidity_percentage)
3359{
3360 uint8_t res;
3361 uint8_t reg_press;
3362 uint8_t prev;
3363 uint32_t timeout;
3364 uint8_t buf[8];
3365
3366 if (handle == NULL) /* check handle */
3367 {
3368 return 2; /* return error */
3369 }
3370 if (handle->inited != 1) /* check handle initialization */
3371 {
3372 return 3; /* return error */
3373 }
3374
3375 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3376 {
3377 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3378
3379 return 1; /* return error */
3380 }
3381
3382 if ((prev & 0x03) != (uint8_t)BME688_MODE_PARALLEL) /* sleep or forced mode */
3383 {
3384 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3385 {
3386 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3387
3388 return 1; /* return error */
3389 }
3390 prev &= ~(3 << 0); /* clear settings */
3391 prev |= 0x01 << 0; /* set forced mode */
3392 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
3393 {
3394 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
3395
3396 return 1; /* return error */
3397 }
3398 timeout = 10 * 1000; /* set timeout */
3399 while (timeout != 0) /* check timeout */
3400 {
3401 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3402 {
3403 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3404
3405 return 1; /* return error */
3406 }
3407 if ((prev & 0x03) == 0) /* if finished */
3408 {
3409 break; /* break */
3410 }
3411 handle->delay_ms(1); /* delay 1ms */
3412 timeout--; /* timeout-- */
3413 }
3414 if (timeout == 0) /* check timeout */
3415 {
3416 handle->debug_print("bme688: read timeout.\n"); /* read timeout */
3417
3418 return 5; /* return error */
3419 }
3420
3421 if (section == BME688_SECTION_0) /* section 0 */
3422 {
3423 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3424 }
3425 else if (section == BME688_SECTION_1) /* section 1 */
3426 {
3427 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3428 }
3429 else /* section 2 */
3430 {
3431 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3432 }
3433
3434 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
3435 if (res != 0) /* check result */
3436 {
3437 handle->debug_print("bme688: read failed.\n"); /* read failed */
3438
3439 return 1; /* return error */
3440 }
3441 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3442 (((uint32_t)(buf[4])) << 4) |
3443 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3444 res = a_bme688_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
3445 if (res != 0) /* check result */
3446 {
3447 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3448
3449 return 4; /* return error */
3450 }
3451 *pressure_raw = ((((uint32_t)(buf[0])) << 12) |
3452 (((uint32_t)(buf[1])) << 4) |
3453 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
3454 res = a_bme688_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
3455 if (res != 0) /* check result */
3456 {
3457 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
3458
3459 return 4; /* return error */
3460 }
3461 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
3462 res = a_bme688_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
3463 if (res != 0) /* check result */
3464 {
3465 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
3466
3467 return 4; /* return error */
3468 }
3469 }
3470 else
3471 {
3472 if (section == BME688_SECTION_0) /* section 0 */
3473 {
3474 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3475 }
3476 else if (section == BME688_SECTION_1) /* section 1 */
3477 {
3478 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3479 }
3480 else /* section 2 */
3481 {
3482 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3483 }
3484
3485 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
3486 if (res != 0) /* check result */
3487 {
3488 handle->debug_print("bme688: read failed.\n"); /* read failed */
3489
3490 return 1; /* return error */
3491 }
3492 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3493 (((uint32_t)(buf[4])) << 4) |
3494 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3495 res = a_bme688_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
3496 if (res != 0) /* check result */
3497 {
3498 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3499
3500 return 4; /* return error */
3501 }
3502 *pressure_raw = ((((uint32_t)(buf[0])) << 12) |
3503 (((uint32_t)(buf[1])) << 4) |
3504 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
3505 res = a_bme688_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
3506 if (res != 0) /* check result */
3507 {
3508 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
3509
3510 return 4; /* return error */
3511 }
3512 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
3513 res = a_bme688_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
3514 if (res != 0) /* check result */
3515 {
3516 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
3517
3518 return 4; /* return error */
3519 }
3520 }
3521
3522 return 0; /* success return 0 */
3523}
3524
3551 uint32_t *temperature_raw, float *temperature_c,
3552 uint32_t *pressure_raw, float *pressure_pa,
3553 uint32_t *humidity_raw, float *humidity_percentage,
3554 uint16_t *adc_raw, uint8_t *adc_range, float *ohms, uint8_t *index)
3555{
3556 uint8_t res;
3557 uint8_t reg_status;
3558 uint8_t reg_gas;
3559 uint8_t reg_press;
3560 uint8_t prev;
3561 uint32_t timeout;
3562 uint8_t buf[8];
3563
3564 if (handle == NULL) /* check handle */
3565 {
3566 return 2; /* return error */
3567 }
3568 if (handle->inited != 1) /* check handle initialization */
3569 {
3570 return 3; /* return error */
3571 }
3572
3573 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3574 {
3575 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3576
3577 return 1; /* return error */
3578 }
3579
3580 if ((prev & 0x03) != (uint8_t)BME688_MODE_PARALLEL) /* sleep or forced mode */
3581 {
3582 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3583 {
3584 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3585
3586 return 1; /* return error */
3587 }
3588 prev &= ~(3 << 0); /* clear settings */
3589 prev |= 0x01 << 0; /* set forced mode */
3590 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* write ctrl meas */
3591 {
3592 handle->debug_print("bme688: write ctrl meas failed.\n"); /* write ctrl meas failed */
3593
3594 return 1; /* return error */
3595 }
3596 timeout = 10 * 1000; /* set timeout */
3597 while (timeout != 0) /* check timeout */
3598 {
3599 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3600 {
3601 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3602
3603 return 1; /* return error */
3604 }
3605 if ((prev & 0x03) == 0) /* if finished */
3606 {
3607 break; /* break */
3608 }
3609 handle->delay_ms(1); /* delay 1ms */
3610 timeout--; /* timeout-- */
3611 }
3612 if (timeout == 0) /* check timeout */
3613 {
3614 handle->debug_print("bme688: read timeout.\n"); /* read timeout */
3615
3616 return 5; /* return error */
3617 }
3618
3619 if (section == BME688_SECTION_0) /* section 0 */
3620 {
3621 reg_gas = BME688_REG_GAS_R_MSB_0; /* set section 0 */
3622 reg_status = BME688_REG_MEAS_STATUS_0; /* set section 0 */
3623 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3624 }
3625 else if (section == BME688_SECTION_1) /* section 1 */
3626 {
3627 reg_gas = BME688_REG_GAS_R_MSB_1; /* set section 1 */
3628 reg_status = BME688_REG_MEAS_STATUS_1; /* set section 1 */
3629 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3630 }
3631 else /* section 2 */
3632 {
3633 reg_gas = BME688_REG_GAS_R_MSB_2; /* set section 2 */
3634 reg_status = BME688_REG_MEAS_STATUS_2; /* set section 2 */
3635 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3636 }
3637
3638 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
3639 if (res != 0) /* check result */
3640 {
3641 handle->debug_print("bme688: read failed.\n"); /* read failed */
3642
3643 return 1; /* return error */
3644 }
3645 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3646 (((uint32_t)(buf[4])) << 4) |
3647 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3648 res = a_bme688_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
3649 if (res != 0) /* check result */
3650 {
3651 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3652
3653 return 4; /* return error */
3654 }
3655 *pressure_raw = ((((uint32_t)(buf[0])) << 12) |
3656 (((uint32_t)(buf[1])) << 4) |
3657 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
3658 res = a_bme688_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
3659 if (res != 0) /* check result */
3660 {
3661 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
3662
3663 return 4; /* return error */
3664 }
3665 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
3666 res = a_bme688_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
3667 if (res != 0) /* check result */
3668 {
3669 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
3670
3671 return 4; /* return error */
3672 }
3673
3674 res = a_bme688_iic_spi_read(handle, reg_gas, buf, 2); /* read gas */
3675 if (res != 0) /* check result */
3676 {
3677 handle->debug_print("bme688: read failed.\n"); /* read failed */
3678
3679 return 1; /* return error */
3680 }
3681 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
3682 {
3683 handle->debug_print("bme688: gas is invalid.\n"); /* gas is invalid */
3684
3685 return 6; /* return error */
3686 }
3687 if ((buf[1] & (1 << 4)) == 0) /* check heat */
3688 {
3689 handle->debug_print("bme688: heater is not stable.\n"); /* heater is not stable */
3690
3691 return 7; /* return error */
3692 }
3693 *adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
3694 *adc_range = buf[1] & 0x0F; /* set adc range */
3695 res = a_bme688_compensate_gas_resistance(handle, *adc_raw , *adc_range, ohms); /* compensate gas resistance */
3696 if (res != 0) /* check result */
3697 {
3698 handle->debug_print("bme688: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
3699
3700 return 4; /* return error */
3701 }
3702 if (a_bme688_iic_spi_read(handle, reg_status, &prev, 1) != 0) /* read status */
3703 {
3704 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
3705
3706 return 1; /* return error */
3707 }
3708 *index = (prev & 0xF); /* set index */
3709 }
3710 else
3711 {
3712 if (section == BME688_SECTION_0) /* section 0 */
3713 {
3714 reg_gas = BME688_REG_GAS_R_MSB_0; /* set section 0 */
3715 reg_status = BME688_REG_MEAS_STATUS_0; /* set section 0 */
3716 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3717 }
3718 else if (section == BME688_SECTION_1) /* section 1 */
3719 {
3720 reg_gas = BME688_REG_GAS_R_MSB_1; /* set section 1 */
3721 reg_status = BME688_REG_MEAS_STATUS_1; /* set section 1 */
3722 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
3723 }
3724 else /* section 2 */
3725 {
3726 reg_gas = BME688_REG_GAS_R_MSB_2; /* set section 2 */
3727 reg_status = BME688_REG_MEAS_STATUS_2; /* set section 2 */
3728 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
3729 }
3730
3731 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
3732 if (res != 0) /* check result */
3733 {
3734 handle->debug_print("bme688: read failed.\n"); /* read failed */
3735
3736 return 1; /* return error */
3737 }
3738 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3739 (((uint32_t)(buf[4])) << 4) |
3740 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3741 res = a_bme688_compensate_temperature(handle, *temperature_raw, temperature_c); /* compensate temperature */
3742 if (res != 0) /* check result */
3743 {
3744 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3745
3746 return 4; /* return error */
3747 }
3748 *pressure_raw = ((((uint32_t)(buf[0])) << 12) |
3749 (((uint32_t)(buf[1])) << 4) |
3750 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
3751 res = a_bme688_compensate_pressure(handle, *pressure_raw, pressure_pa); /* compensate pressure */
3752 if (res != 0) /* check result */
3753 {
3754 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
3755
3756 return 4; /* return error */
3757 }
3758 *humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
3759 res = a_bme688_compensate_humidity(handle, *humidity_raw, humidity_percentage); /* compensate humidity */
3760 if (res != 0) /* check result */
3761 {
3762 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
3763
3764 return 4; /* return error */
3765 }
3766
3767 res = a_bme688_iic_spi_read(handle, reg_gas, buf, 2); /* read gas */
3768 if (res != 0) /* check result */
3769 {
3770 handle->debug_print("bme688: read failed.\n"); /* read failed */
3771
3772 return 1; /* return error */
3773 }
3774 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
3775 {
3776 handle->debug_print("bme688: gas is invalid.\n"); /* gas is invalid */
3777
3778 return 6; /* return error */
3779 }
3780 if ((buf[1] & (1 << 4)) == 0) /* check heat */
3781 {
3782 handle->debug_print("bme688: heater is not stable.\n"); /* heater is not stable */
3783
3784 return 7; /* return error */
3785 }
3786 *adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
3787 *adc_range = buf[1] & 0x0F; /* set adc range */
3788 res = a_bme688_compensate_gas_resistance(handle, *adc_raw , *adc_range, ohms); /* compensate gas resistance */
3789 if (res != 0) /* check result */
3790 {
3791 handle->debug_print("bme688: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
3792
3793 return 4; /* return error */
3794 }
3795 if (a_bme688_iic_spi_read(handle, reg_status, &prev, 1) != 0) /* read status */
3796 {
3797 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
3798
3799 return 1; /* return error */
3800 }
3801 *index = (prev & 0xF); /* set index */
3802 }
3803
3804 return 0; /* success return 0 */
3805}
3806
3825{
3826 uint8_t res;
3827 uint8_t prev;
3828
3829 if (handle == NULL) /* check handle */
3830 {
3831 return 2; /* return error */
3832 }
3833 if (handle->inited != 1) /* check handle initialization */
3834 {
3835 return 3; /* return error */
3836 }
3837
3838 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_MEAS, &prev, 1) != 0) /* read ctrl meas */
3839 {
3840 handle->debug_print("bme688: read ctrl meas failed.\n"); /* read ctrl meas failed */
3841
3842 return 1; /* return error */
3843 }
3844
3845 if ((prev & 0x03) == (uint8_t)BME688_MODE_PARALLEL) /* parallel mode */
3846 {
3847 uint8_t reg;
3848 uint8_t trigger;
3849 uint8_t prev0;
3850 uint8_t prev1;
3851 uint8_t prev2;
3852 uint8_t index0;
3853 uint8_t index1;
3854 uint8_t index2;
3855 uint8_t reg_gas;
3856 uint8_t reg_press;
3857 uint8_t reg_sub;
3858 uint8_t buf[8];
3859 uint32_t temperature_raw;
3860 float temperature_c;
3861 uint32_t pressure_raw;
3862 float pressure_pa;
3863 uint32_t humidity_raw;
3864 float humidity_percentage;
3865 uint16_t adc_raw;
3866 uint8_t adc_range;
3867 float ohms;
3868 uint8_t ind;
3869
3870 if (a_bme688_iic_spi_read(handle, BME688_REG_MEAS_STATUS_0, &prev0, 1) != 0) /* read status */
3871 {
3872 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
3873
3874 return 1; /* return error */
3875 }
3876 if (a_bme688_iic_spi_read(handle, BME688_REG_MEAS_STATUS_1, &prev1, 1) != 0) /* read status */
3877 {
3878 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
3879
3880 return 1; /* return error */
3881 }
3882 if (a_bme688_iic_spi_read(handle, BME688_REG_MEAS_STATUS_2, &prev2, 1) != 0) /* read status */
3883 {
3884 handle->debug_print("bme688: read status failed.\n"); /* read status failed */
3885
3886 return 1; /* return error */
3887 }
3888
3889 trigger = 0; /* init 0 */
3890 if (((prev0 >> 7) & 0x01) != 0) /* check output */
3891 {
3892 trigger = 1; /* init 1 */
3893 index0 = prev0 & 0xF; /* set index 0 */
3894 if (index0 > 9) /* check index */
3895 {
3896 handle->debug_print("bme688: index is invalid.\n"); /* index is invalid */
3897
3898 return 6; /* return error */
3899 }
3900
3901 reg_gas = BME688_REG_GAS_R_MSB_0; /* set section 0 */
3902 reg_press = BME688_REG_PRESS_MSB_0; /* set section 0 */
3903 reg_sub = BME688_REG_SUB_MEAS_INDEX_0; /* set section 0 */
3904 ind = index0; /* set section 0 */
3905
3906 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
3907 if (res != 0) /* check result */
3908 {
3909 handle->debug_print("bme688: read failed.\n"); /* read failed */
3910
3911 return 1; /* return error */
3912 }
3913 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
3914 (((uint32_t)(buf[4])) << 4) |
3915 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
3916 res = a_bme688_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
3917 if (res != 0) /* check result */
3918 {
3919 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
3920
3921 return 7; /* return error */
3922 }
3923 pressure_raw = ((((uint32_t)(buf[0])) << 12) |
3924 (((uint32_t)(buf[1])) << 4) |
3925 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
3926 res = a_bme688_compensate_pressure(handle, pressure_raw, &pressure_pa); /* compensate pressure */
3927 if (res != 0) /* check result */
3928 {
3929 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
3930
3931 return 7; /* return error */
3932 }
3933 humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
3934 res = a_bme688_compensate_humidity(handle, humidity_raw, &humidity_percentage); /* compensate humidity */
3935 if (res != 0) /* check result */
3936 {
3937 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
3938
3939 return 7; /* return error */
3940 }
3941
3942 res = a_bme688_iic_spi_read(handle, reg_gas, buf, 2); /* read gas */
3943 if (res != 0) /* check result */
3944 {
3945 handle->debug_print("bme688: read failed.\n"); /* read failed */
3946
3947 return 1; /* return error */
3948 }
3949 adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
3950 adc_range = buf[1] & 0x0F; /* set adc range */
3951 res = a_bme688_compensate_gas_resistance(handle, adc_raw , adc_range, &ohms); /* compensate gas resistance */
3952 if (res != 0) /* check result */
3953 {
3954 handle->debug_print("bme688: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
3955
3956 return 7; /* return error */
3957 }
3958 res = a_bme688_iic_spi_read(handle, reg_sub, &reg, 1); /* read sub index */
3959 if (res != 0) /* check result */
3960 {
3961 handle->debug_print("bme688: read failed.\n"); /* read failed */
3962
3963 return 1; /* return error */
3964 }
3965 if (handle->parallel_sequence_trigger[0] == 0) /* check trigger */
3966 {
3967 handle->parallel_sequence_number[0] = reg; /* set sequence number */
3968 }
3969 else
3970 {
3971 if ((uint16_t)(handle->parallel_sequence_number[0] + 3) > 255) /* set sequence number */
3972 {
3973 reg = (uint8_t)(handle->parallel_sequence_number[0] + 3); /* reset to new data */
3974 }
3975 if (reg != (uint8_t)(handle->parallel_sequence_number[0] + 3)) /* check sequence number */
3976 {
3977 handle->debug_print("bme688: sequence is lost.\n"); /* sequence is lost */
3978 if (handle->receive_callback != NULL) /* not null */
3979 {
3981 NULL, 0); /* run the callback */
3982 }
3983
3984 return 10; /* return error */
3985 }
3986 }
3987 handle->parallel_sequence_trigger[0] = 1; /* set trigger */
3988 handle->parallel_sequence_number[0] = reg; /* set sequence number */
3989 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
3990 {
3991 goto step1; /* goto step1 */
3992 }
3993 if ((buf[1] & (1 << 4)) == 0) /* check heat */
3994 {
3995 goto step1; /* goto step1 */
3996 }
3997 handle->parallel_data[ind].channel = ind; /* set channel */
3998 handle->parallel_data[ind].temperature_raw = temperature_raw; /* set temperature raw */
3999 handle->parallel_data[ind].temperature_c = temperature_c; /* set temperature */
4000 handle->parallel_data[ind].pressure_raw = pressure_raw; /* set pressure raw */
4001 handle->parallel_data[ind].pressure_pa = pressure_pa; /* set pressure */
4002 handle->parallel_data[ind].humidity_raw = humidity_raw; /* set humidity raw */
4003 handle->parallel_data[ind].humidity_percentage = humidity_percentage; /* set humidity */
4004 handle->parallel_data[ind].adc_raw = adc_raw; /* set adc raw */
4005 handle->parallel_data[ind].adc_range = adc_range; /* set adc range */
4006 handle->parallel_data[ind].ohms = ohms; /* set ohms */
4007 handle->parallel_data_flag |= 1 << ind; /* set flag */
4008 if (handle->receive_callback != NULL) /* not null */
4009 {
4011 &handle->parallel_data[ind], 1); /* run the callback */
4012 }
4013 }
4014
4015 step1:
4016 if (((prev1 >> 7) & 0x01) != 0) /* check output */
4017 {
4018 trigger = 1; /* init 1 */
4019 index1 = prev1 & 0xF; /* set index 1 */
4020 if (index1 > 9) /* check index */
4021 {
4022 handle->debug_print("bme688: index is invalid.\n"); /* index is invalid */
4023
4024 return 6; /* return error */
4025 }
4026
4027 reg_gas = BME688_REG_GAS_R_MSB_1; /* set section 1 */
4028 reg_press = BME688_REG_PRESS_MSB_1; /* set section 1 */
4029 reg_sub = BME688_REG_SUB_MEAS_INDEX_1; /* set section 1 */
4030 ind = index1; /* set section 1 */
4031
4032 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
4033 if (res != 0) /* check result */
4034 {
4035 handle->debug_print("bme688: read failed.\n"); /* read failed */
4036
4037 return 1; /* return error */
4038 }
4039 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
4040 (((uint32_t)(buf[4])) << 4) |
4041 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
4042 res = a_bme688_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
4043 if (res != 0) /* check result */
4044 {
4045 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
4046
4047 return 7; /* return error */
4048 }
4049 pressure_raw = ((((uint32_t)(buf[0])) << 12) |
4050 (((uint32_t)(buf[1])) << 4) |
4051 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
4052 res = a_bme688_compensate_pressure(handle, pressure_raw, &pressure_pa); /* compensate pressure */
4053 if (res != 0) /* check result */
4054 {
4055 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
4056
4057 return 7; /* return error */
4058 }
4059 humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
4060 res = a_bme688_compensate_humidity(handle, humidity_raw, &humidity_percentage); /* compensate humidity */
4061 if (res != 0) /* check result */
4062 {
4063 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
4064
4065 return 7; /* return error */
4066 }
4067
4068 res = a_bme688_iic_spi_read(handle, reg_gas, buf, 2); /* read gas */
4069 if (res != 0) /* check result */
4070 {
4071 handle->debug_print("bme688: read failed.\n"); /* read failed */
4072
4073 return 1; /* return error */
4074 }
4075 adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
4076 adc_range = buf[1] & 0x0F; /* set adc range */
4077 res = a_bme688_compensate_gas_resistance(handle, adc_raw , adc_range, &ohms); /* compensate gas resistance */
4078 if (res != 0) /* check result */
4079 {
4080 handle->debug_print("bme688: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
4081
4082 return 7; /* return error */
4083 }
4084 res = a_bme688_iic_spi_read(handle, reg_sub, &reg, 1); /* read sub index */
4085 if (res != 0) /* check result */
4086 {
4087 handle->debug_print("bme688: read failed.\n"); /* read failed */
4088
4089 return 1; /* return error */
4090 }
4091 if (handle->parallel_sequence_trigger[1] == 0) /* check trigger */
4092 {
4093 handle->parallel_sequence_number[1] = reg; /* set sequence number */
4094 }
4095 else
4096 {
4097 if ((uint16_t)(handle->parallel_sequence_number[1] + 3) > 255) /* set sequence number */
4098 {
4099 reg = (uint8_t)(handle->parallel_sequence_number[1] + 3); /* reset to new data */
4100 }
4101 if (reg != (uint8_t)(handle->parallel_sequence_number[1] + 3)) /* check sequence number */
4102 {
4103 handle->debug_print("bme688: sequence is lost.\n"); /* sequence is lost */
4104 if (handle->receive_callback != NULL) /* not null */
4105 {
4107 NULL, 0); /* run the callback */
4108 }
4109
4110 return 10; /* return error */
4111 }
4112 }
4113 handle->parallel_sequence_trigger[1] = 1; /* set trigger */
4114 handle->parallel_sequence_number[1] = reg; /* set sequence number */
4115 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
4116 {
4117 goto step2; /* goto step1 */
4118 }
4119 if ((buf[1] & (1 << 4)) == 0) /* check heat */
4120 {
4121 goto step2; /* goto step1 */
4122 }
4123 handle->parallel_data[ind].channel = ind; /* set channel */
4124 handle->parallel_data[ind].temperature_raw = temperature_raw; /* set temperature raw */
4125 handle->parallel_data[ind].temperature_c = temperature_c; /* set temperature */
4126 handle->parallel_data[ind].pressure_raw = pressure_raw; /* set pressure raw */
4127 handle->parallel_data[ind].pressure_pa = pressure_pa; /* set pressure */
4128 handle->parallel_data[ind].humidity_raw = humidity_raw; /* set humidity raw */
4129 handle->parallel_data[ind].humidity_percentage = humidity_percentage; /* set humidity */
4130 handle->parallel_data[ind].adc_raw = adc_raw; /* set adc raw */
4131 handle->parallel_data[ind].adc_range = adc_range; /* set adc range */
4132 handle->parallel_data[ind].ohms = ohms; /* set ohms */
4133 handle->parallel_data_flag |= 1 << ind; /* set flag */
4134 if (handle->receive_callback != NULL) /* not null */
4135 {
4137 &handle->parallel_data[ind], 1); /* run the callback */
4138 }
4139 }
4140
4141 step2:
4142 if (((prev2 >> 7) & 0x01) != 0) /* check output */
4143 {
4144 trigger = 1; /* init 1 */
4145 index2 = prev2 & 0xF; /* set index 2 */
4146 if (index2 > 9) /* check index */
4147 {
4148 handle->debug_print("bme688: index is invalid.\n"); /* index is invalid */
4149
4150 return 6; /* return error */
4151 }
4152
4153 reg_gas = BME688_REG_GAS_R_MSB_2; /* set section 2 */
4154 reg_press = BME688_REG_PRESS_MSB_2; /* set section 2 */
4155 reg_sub = BME688_REG_SUB_MEAS_INDEX_2; /* set section 2 */
4156 ind = index2; /* set section 2 */
4157
4158 res = a_bme688_iic_spi_read(handle, reg_press, buf, 8); /* read temperature pressure and humidity */
4159 if (res != 0) /* check result */
4160 {
4161 handle->debug_print("bme688: read failed.\n"); /* read failed */
4162
4163 return 1; /* return error */
4164 }
4165 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
4166 (((uint32_t)(buf[4])) << 4) |
4167 ((uint32_t)buf[5] >> 4)); /* set temperature raw */
4168 res = a_bme688_compensate_temperature(handle, temperature_raw, &temperature_c); /* compensate temperature */
4169 if (res != 0) /* check result */
4170 {
4171 handle->debug_print("bme688: compensate temperature failed.\n"); /* compensate temperature failed */
4172
4173 return 7; /* return error */
4174 }
4175 pressure_raw = ((((uint32_t)(buf[0])) << 12) |
4176 (((uint32_t)(buf[1])) << 4) |
4177 (((uint32_t)(buf[2])) >> 4)); /* set pressure raw */
4178 res = a_bme688_compensate_pressure(handle, pressure_raw, &pressure_pa); /* compensate pressure */
4179 if (res != 0) /* check result */
4180 {
4181 handle->debug_print("bme688: compensate pressure failed.\n"); /* compensate pressure failed */
4182
4183 return 7; /* return error */
4184 }
4185 humidity_raw = (uint32_t)buf[6] << 8 | buf[7]; /* set humidity raw */
4186 res = a_bme688_compensate_humidity(handle, humidity_raw, &humidity_percentage); /* compensate humidity */
4187 if (res != 0) /* check result */
4188 {
4189 handle->debug_print("bme688: compensate humidity failed.\n"); /* compensate humidity failed */
4190
4191 return 7; /* return error */
4192 }
4193
4194 res = a_bme688_iic_spi_read(handle, reg_gas, buf, 2); /* read gas */
4195 if (res != 0) /* check result */
4196 {
4197 handle->debug_print("bme688: read failed.\n"); /* read failed */
4198
4199 return 1; /* return error */
4200 }
4201 adc_raw = (((uint16_t)buf[0]) << 2) | ((buf[1] >> 6) & 0x03); /* set adc raw */
4202 adc_range = buf[1] & 0x0F; /* set adc range */
4203 res = a_bme688_compensate_gas_resistance(handle, adc_raw , adc_range, &ohms); /* compensate gas resistance */
4204 if (res != 0) /* check result */
4205 {
4206 handle->debug_print("bme688: compensate gas resistance failed.\n"); /* compensate gas resistance failed */
4207
4208 return 7; /* return error */
4209 }
4210 res = a_bme688_iic_spi_read(handle, reg_sub, &reg, 1); /* read sub index */
4211 if (res != 0) /* check result */
4212 {
4213 handle->debug_print("bme688: read failed.\n"); /* read failed */
4214
4215 return 1; /* return error */
4216 }
4217 if (handle->parallel_sequence_trigger[2] == 0) /* check trigger */
4218 {
4219 handle->parallel_sequence_number[2] = reg; /* set sequence number */
4220 }
4221 else
4222 {
4223 if ((uint16_t)(handle->parallel_sequence_number[2] + 3) > 255) /* set sequence number */
4224 {
4225 reg = (uint8_t)(handle->parallel_sequence_number[2] + 3); /* reset to new data */
4226 }
4227 if (reg != (uint8_t)(handle->parallel_sequence_number[2] + 3)) /* check sequence number */
4228 {
4229 handle->debug_print("bme688: sequence is lost.\n"); /* sequence is lost */
4230 if (handle->receive_callback != NULL) /* not null */
4231 {
4233 NULL, 0); /* run the callback */
4234 }
4235
4236 return 10; /* return error */
4237 }
4238 }
4239 handle->parallel_sequence_trigger[2] = 1; /* set trigger */
4240 handle->parallel_sequence_number[2] = reg; /* set sequence number */
4241 if ((buf[1] & (1 << 5)) == 0) /* check gas valid */
4242 {
4243 return 8; /* return error */
4244 }
4245 if ((buf[1] & (1 << 4)) == 0) /* check heat */
4246 {
4247 return 9; /* return error */
4248 }
4249 handle->parallel_data[ind].channel = ind; /* set channel */
4250 handle->parallel_data[ind].temperature_raw = temperature_raw; /* set temperature raw */
4251 handle->parallel_data[ind].temperature_c = temperature_c; /* set temperature */
4252 handle->parallel_data[ind].pressure_raw = pressure_raw; /* set pressure raw */
4253 handle->parallel_data[ind].pressure_pa = pressure_pa; /* set pressure */
4254 handle->parallel_data[ind].humidity_raw = humidity_raw; /* set humidity raw */
4255 handle->parallel_data[ind].humidity_percentage = humidity_percentage; /* set humidity */
4256 handle->parallel_data[ind].adc_raw = adc_raw; /* set adc raw */
4257 handle->parallel_data[ind].adc_range = adc_range; /* set adc range */
4258 handle->parallel_data[ind].ohms = ohms; /* set ohms */
4259 handle->parallel_data_flag |= 1 << ind; /* set flag */
4260 if (handle->receive_callback != NULL) /* not null */
4261 {
4263 &handle->parallel_data[ind], 1); /* run the callback */
4264 }
4265 }
4266 if (a_bme688_calculate_parallel_counter(handle) >= handle->parallel_data_len) /* check parallel counter */
4267 {
4268 if (handle->receive_callback != NULL) /* not null */
4269 {
4270 handle->receive_callback((uint8_t)BME688_STATUS_ALL_DATA,
4271 &handle->parallel_data[0],
4272 handle->parallel_data_len); /* run the callback */
4273 }
4274 handle->parallel_data_flag = 0; /* init 0 */
4275 }
4276 if (trigger != 0) /* check trigger */
4277 {
4278 return 5; /* return not ready */
4279 }
4280
4281 return 0; /* success return 0 */
4282 }
4283 else
4284 {
4285 handle->debug_print("bme688: not parallel mode.\n"); /* not parallel mode */
4286
4287 return 4; /* return error */
4288 }
4289}
4290
4301{
4302 uint8_t i;
4303
4304 if (handle == NULL) /* check handle */
4305 {
4306 return 2; /* return error */
4307 }
4308 if (handle->inited != 1) /* check handle initialization */
4309 {
4310 return 3; /* return error */
4311 }
4312
4313 for (i = 0; i < 3; i++) /* set all 0 */
4314 {
4315 handle->parallel_sequence_trigger[i] = 0; /* init 0 */
4316 handle->parallel_sequence_number[i] = 0; /* init 0 */
4317 }
4318 handle->parallel_data_flag = 0; /* flag init 0 */
4319
4320 return 0; /* success return 0 */
4321}
4322
4338{
4339 uint8_t prev;
4340 uint8_t i;
4341 uint8_t factor;
4342 uint16_t ms;
4343
4344 if (handle == NULL) /* check handle */
4345 {
4346 return 2; /* return error */
4347 }
4348 if (handle->inited != 1) /* check handle initialization */
4349 {
4350 return 3; /* return error */
4351 }
4352 if (config == NULL) /* check config */
4353 {
4354 handle->debug_print("bme688: config is invalid.\n"); /* config is invalid */
4355
4356 return 4; /* return error */
4357 }
4358 if ((config->heater_dur_prof == NULL) || (config->heater_temp_prof == NULL)) /* check config */
4359 {
4360 handle->debug_print("bme688: config is invalid.\n"); /* config is invalid */
4361
4362 return 4; /* return error */
4363 }
4364 if (config->profile_len > 10) /* check profile length */
4365 {
4366 handle->debug_print("bme688: profile_len > 10.\n"); /* profile_len > 10 */
4367
4368 return 5; /* return error */
4369 }
4370
4371 for (i = 0; i < config->profile_len; i++) /* set all profile */
4372 {
4373 (void)a_bme688_compensate_heat(handle, config->heater_temp_prof[i], &prev); /* convert real data to register data */
4374 if (a_bme688_iic_spi_write(handle, BME688_REG_RES_HEAT_X + i, &prev, 1) != 0) /* write resistance heat */
4375 {
4376 handle->debug_print("bme688: write resistance heat failed.\n"); /* write resistance heat failed */
4377
4378 return 1; /* return error */
4379 }
4380
4381 prev = config->heater_dur_prof[i]; /* set wait */
4382 if (a_bme688_iic_spi_write(handle, BME688_REG_GAS_WAIT_X + i, &prev, 1) != 0) /* write gas wait */
4383 {
4384 handle->debug_print("bme688: write gas wait failed.\n"); /* write gas wait failed */
4385
4386 return 1; /* return error */
4387 }
4388 }
4389
4390 factor = 0; /* init 0 */
4391 ms = (uint16_t)config->shared_heater_dur; /* set ms */
4392 if (ms >= 0x0783U) /* check ms */
4393 {
4394 handle->debug_print("bme688: ms >= 0x0783.\n"); /* ms >= 0x0783 */
4395
4396 return 6; /* return error */
4397 }
4398 else
4399 {
4400 ms = (uint16_t)(((uint32_t)ms * 1000) / 477); /* set ms */
4401 while (ms > 0x3F) /* check ms */
4402 {
4403 ms = ms >> 2; /* right shift 2 */
4404 factor += 1; /* add 1 */
4405 }
4406
4407 prev = (uint8_t)(ms + (factor * 64)); /* set param */
4408 }
4409
4410 if (a_bme688_iic_spi_write(handle, BME688_REG_GAS_WAIT_SHARED, &prev, 1) != 0) /* write gas wait shared */
4411 {
4412 handle->debug_print("bme688: write gas wait shared failed.\n"); /* write gas wait shared failed */
4413
4414 return 1; /* return error */
4415 }
4416
4417 if (a_bme688_iic_spi_read(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* read ctrl gas1 */
4418 {
4419 handle->debug_print("bme688: read ctrl gas1 failed.\n"); /* read ctrl gas1 failed */
4420
4421 return 1; /* return error */
4422 }
4423 prev &= ~(0x0F << 0); /* clear settings */
4424 prev |= (config->profile_len & 0x0F) << 0; /* set profile */
4425 if (a_bme688_iic_spi_write(handle, BME688_REG_CTRL_GAS_1, &prev, 1) != 0) /* write ctrl gas1 */
4426 {
4427 handle->debug_print("bme688: write ctrl gas1 failed.\n"); /* write ctrl gas1 failed */
4428
4429 return 1; /* return error */
4430 }
4431 handle->parallel_data_len = config->profile_len; /* set parallel data length */
4432
4433 return 0; /* success return 0 */
4434}
4435
4449uint8_t bme688_set_reg(bme688_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
4450{
4451 if (handle == NULL) /* check handle */
4452 {
4453 return 2; /* return error */
4454 }
4455 if (handle->inited != 1) /* check handle initialization */
4456 {
4457 return 3; /* return error */
4458 }
4459
4460 return a_bme688_iic_spi_write(handle, reg, buf, len); /* write register */
4461}
4462
4476uint8_t bme688_get_reg(bme688_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
4477{
4478 if (handle == NULL) /* check handle */
4479 {
4480 return 2; /* return error */
4481 }
4482 if (handle->inited != 1) /* check handle initialization */
4483 {
4484 return 3; /* return error */
4485 }
4486
4487 return a_bme688_iic_spi_read(handle, reg, buf, len); /* read register */
4488}
4489
4499{
4500 if (info == NULL) /* check handle */
4501 {
4502 return 2; /* return error */
4503 }
4504
4505 memset(info, 0, sizeof(bme688_info_t)); /* initialize bme688 info structure */
4506 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
4507 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
4508 strncpy(info->interface, "IIC SPI", 8); /* copy interface name */
4509 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
4510 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
4511 info->max_current_ma = MAX_CURRENT; /* set maximum current */
4512 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
4513 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
4514 info->driver_version = DRIVER_VERSION; /* set driver version */
4515
4516 return 0; /* success return 0 */
4517}
#define BME688_REG_CTRL_MEAS
#define BME688_REG_NVM_PAR_H1_L
#define BME688_REG_PRESS_MSB_2
#define BME688_REG_NVM_RES_HEAT_VAL
#define BME688_REG_VARIANT_ID
#define BME688_REG_CTRL_GAS_1
#define BME688_REG_NVM_PAR_P7
#define BME688_REG_NVM_PAR_T1_L
chip register definition
#define BME688_REG_NVM_PAR_P10
#define BME688_REG_GAS_R_LSB_1
#define BME688_REG_NVM_PAR_G3
#define MAX_CURRENT
#define BME688_REG_NVM_PAR_G1
#define BME688_REG_CTRL_GAS_0
#define BME688_REG_CONFIG
#define BME688_REG_GAS_WAIT_SHARED
#define BME688_REG_GAS_R_MSB_2
#define BME688_REG_NVM_PAR_H4
#define BME688_REG_NVM_PAR_P9_L
#define BME688_REG_NVM_PAR_H3
#define BME688_REG_SUB_MEAS_INDEX_0
#define BME688_REG_NVM_PAR_G2_L
#define BME688_REG_CTRL_HUM
#define BME688_REG_NVM_RANGE_SWITCH
#define BME688_REG_NVM_PAR_P2_L
#define SUPPLY_VOLTAGE_MAX
#define BME688_REG_PRESS_MSB_1
#define BME688_REG_NVM_PAR_P4_L
#define BME688_REG_SUB_MEAS_INDEX_1
#define BME688_REG_ID
#define BME688_REG_MEAS_STATUS_1
#define BME688_REG_NVM_PAR_P6
#define BME688_REG_NVM_PAR_H6
#define BME688_REG_NVM_PAR_T2_L
#define BME688_REG_STATUS
#define BME688_REG_GAS_WAIT_X
#define BME688_REG_NVM_PAR_H2_H
#define TEMPERATURE_MAX
#define BME688_REG_NVM_PAR_P8_L
#define BME688_REG_IDAC_HEAT_X
#define BME688_REG_RESET
#define BME688_REG_GAS_R_MSB_1
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define BME688_REG_NVM_PAR_P5_L
#define BME688_REG_NVM_RES_HEAT_RANGE
#define BME688_REG_NVM_PAR_T3
#define BME688_REG_MEAS_STATUS_0
#define BME688_REG_NVM_PAR_H7
#define BME688_REG_NVM_PAR_H5
#define BME688_REG_NVM_PAR_P3
#define CHIP_NAME
chip information definition
#define BME688_REG_NVM_PAR_P1_L
#define DRIVER_VERSION
#define BME688_REG_SUB_MEAS_INDEX_2
#define BME688_REG_GAS_R_LSB_0
#define BME688_REG_GAS_R_LSB_2
#define BME688_REG_PRESS_MSB_0
#define BME688_REG_MEAS_STATUS_2
#define BME688_REG_GAS_R_MSB_0
#define BME688_REG_RES_HEAT_X
driver bme688 header file
uint8_t bme688_set_filter(bme688_handle_t *handle, bme688_filter_t filter)
set filter
uint8_t bme688_get_temperature_oversampling(bme688_handle_t *handle, bme688_oversampling_t *oversampling)
get temperature oversampling
uint8_t bme688_set_interface(bme688_handle_t *handle, bme688_interface_t interface)
set the interface
uint8_t bme688_soft_reset(bme688_handle_t *handle)
soft reset
bme688_mode_t
bme688 mode enumeration definition
uint8_t bme688_get_mode(bme688_handle_t *handle, bme688_mode_t *mode)
get mode
uint8_t bme688_get_humidity_oversampling(bme688_handle_t *handle, bme688_oversampling_t *oversampling)
get humidity oversampling
bme688_address_t
bme688 address enumeration definition
uint8_t bme688_set_addr_pin(bme688_handle_t *handle, bme688_address_t addr_pin)
set the iic address pin
uint8_t bme688_read_gas_resistance(bme688_handle_t *handle, bme688_section_t section, uint16_t *adc_raw, uint8_t *adc_range, float *ohms, uint8_t *index)
read the gas resistance
uint8_t bme688_get_measuring_status(bme688_handle_t *handle, bme688_section_t section, bme688_bool_t *enable)
get measuring status
uint8_t bme688_get_spi_wire_3_data_interrupt(bme688_handle_t *handle, bme688_bool_t *enable)
get spi wire3 data interrupt status
uint8_t bme688_gas_wait_shared_convert_to_register(bme688_handle_t *handle, float m, uint8_t *reg)
convert the gas wait shared to the register raw data
uint8_t bme688_set_heater_profile(bme688_handle_t *handle, bme688_heater_profile_t profile)
set heater profile
uint8_t bme688_get_run_gas(bme688_handle_t *handle, bme688_bool_t *enable)
get run gas status
uint8_t bme688_get_resistance_heater(bme688_handle_t *handle, uint8_t index, uint8_t *reg)
get resistance heater
uint8_t bme688_read_temperature_pressure_humidity(bme688_handle_t *handle, bme688_section_t section, 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 bme688_read(bme688_handle_t *handle, bme688_section_t section, 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
bme688_heater_profile_t
bme688 heater profile enumeration definition
uint8_t bme688_idac_heater_convert_to_register(bme688_handle_t *handle, float ma, uint8_t *reg)
convert the idac heater to the register raw data
uint8_t bme688_get_gas_measuring_status(bme688_handle_t *handle, bme688_section_t section, bme688_bool_t *enable)
get gas measuring status
uint8_t bme688_set_temperature_oversampling(bme688_handle_t *handle, bme688_oversampling_t oversampling)
set temperature oversampling
uint8_t bme688_set_mode(bme688_handle_t *handle, bme688_mode_t mode)
set mode
uint8_t bme688_get_heater_stability_status(bme688_handle_t *handle, bme688_section_t section, bme688_bool_t *enable)
get heater stability status
uint8_t bme688_parallel_config(bme688_handle_t *handle, bme688_parallel_config_t *config)
parallel config
uint8_t bme688_set_run_gas(bme688_handle_t *handle, bme688_bool_t enable)
enable or disable run gas
uint8_t bme688_get_gas_valid_status(bme688_handle_t *handle, bme688_section_t section, bme688_bool_t *enable)
get gas valid status
uint8_t bme688_get_gas_measuring_index(bme688_handle_t *handle, bme688_section_t section, uint8_t *index)
get gas measuring index
uint8_t bme688_get_heater_off(bme688_handle_t *handle, bme688_bool_t *enable)
get heater off status
uint8_t bme688_set_spi_wire(bme688_handle_t *handle, bme688_spi_wire_t spi)
set spi wire
uint8_t bme688_get_filter(bme688_handle_t *handle, bme688_filter_t *filter)
get filter
uint8_t bme688_get_heater_profile(bme688_handle_t *handle, bme688_heater_profile_t *profile)
get heater profile
uint8_t bme688_set_idac_heater(bme688_handle_t *handle, uint8_t index, uint8_t reg)
set idac heater
bme688_filter_t
bme688 filter enumeration definition
uint8_t bme688_init(bme688_handle_t *handle)
initialize the chip
uint8_t bme688_info(bme688_info_t *info)
get chip's information
uint8_t bme688_get_idac_heater(bme688_handle_t *handle, uint8_t index, uint8_t *reg)
get idac heater
uint8_t bme688_get_interface(bme688_handle_t *handle, bme688_interface_t *interface)
get the interface
uint8_t bme688_read_temperature(bme688_handle_t *handle, bme688_section_t section, uint32_t *temperature_raw, float *temperature_c)
read the temperature data
uint8_t bme688_set_spi_wire_3_data_interrupt(bme688_handle_t *handle, bme688_bool_t enable)
enable or disable spi wire3 data interrupt
uint8_t bme688_gas_wait_convert_to_register(bme688_handle_t *handle, uint16_t ms, uint8_t *reg)
convert the gas wait to the register raw data
struct bme688_info_s bme688_info_t
bme688 information structure definition
uint8_t bme688_set_gas_wait(bme688_handle_t *handle, uint8_t index, uint8_t reg)
set gas wait
uint8_t bme688_resistance_heater_convert_to_register(bme688_handle_t *handle, float degree_celsius, uint8_t *reg)
convert the resistance heater to the register raw data
uint8_t bme688_get_gas_wait_shared(bme688_handle_t *handle, uint8_t *reg)
get gas wait shared
uint8_t bme688_deinit(bme688_handle_t *handle)
close the chip
uint8_t bme688_set_resistance_heater(bme688_handle_t *handle, uint8_t index, uint8_t reg)
set resistance heater
uint8_t bme688_get_new_data_status(bme688_handle_t *handle, bme688_section_t section, bme688_bool_t *enable)
get new data status
bme688_section_t
bme688 section enumeration definition
uint8_t bme688_get_gas_wait(bme688_handle_t *handle, uint8_t index, uint8_t *reg)
get gas wait
uint8_t bme688_idac_heater_convert_to_data(bme688_handle_t *handle, uint8_t reg, float *ma)
convert the register raw data to idac heater
uint8_t bme688_read_humidity(bme688_handle_t *handle, bme688_section_t section, uint32_t *humidity_raw, float *humidity_percentage)
read the humidity data
uint8_t bme688_get_addr_pin(bme688_handle_t *handle, bme688_address_t *addr_pin)
get the iic address pin
uint8_t bme688_read_pressure(bme688_handle_t *handle, bme688_section_t section, uint32_t *pressure_raw, float *pressure_pa)
read the pressure data
uint8_t bme688_set_heater_off(bme688_handle_t *handle, bme688_bool_t enable)
enable or disable heater off
bme688_bool_t
bme688 bool enumeration definition
uint8_t bme688_parallel_progress(bme688_handle_t *handle)
parallel progress
uint8_t bme688_parallel_reset(bme688_handle_t *handle)
parallel reset
uint8_t bme688_get_pressure_oversampling(bme688_handle_t *handle, bme688_oversampling_t *oversampling)
get pressure oversampling
bme688_interface_t
bme688 interface enumeration definition
bme688_oversampling_t
bme688 oversampling enumeration definition
uint8_t bme688_set_gas_wait_shared(bme688_handle_t *handle, uint8_t reg)
set gas wait shared
uint8_t bme688_set_humidity_oversampling(bme688_handle_t *handle, bme688_oversampling_t oversampling)
set humidity oversampling
uint8_t bme688_set_pressure_oversampling(bme688_handle_t *handle, bme688_oversampling_t oversampling)
set pressure oversampling
struct bme688_parallel_config_s bme688_parallel_config_t
bme688 parallel config structure definition
uint8_t bme688_get_sub_measurement_index(bme688_handle_t *handle, bme688_section_t section, uint8_t *ind)
get sub measurement index
bme688_spi_wire_t
bme688 spi wire enumeration definition
uint8_t bme688_get_spi_wire(bme688_handle_t *handle, bme688_spi_wire_t *spi)
get spi wire
struct bme688_handle_s bme688_handle_t
bme688 handle structure definition
@ BME688_MODE_PARALLEL
@ BME688_STATUS_ALL_DATA
@ BME688_STATUS_CHANNEL_DATA
@ BME688_STATUS_SEQUENCE_LOST
@ BME688_SECTION_1
@ BME688_SECTION_0
@ BME688_INTERFACE_IIC
uint8_t bme688_get_reg(bme688_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t bme688_set_reg(bme688_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t parallel_sequence_trigger[3]
uint8_t(* spi_init)(void)
void(* receive_callback)(uint8_t status, bme688_parallel_data_t *data, uint8_t len)
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)
bme688_parallel_data_t parallel_data[10]
uint8_t parallel_data_len
void(* debug_print)(const char *const fmt,...)
uint8_t parallel_sequence_number[3]
uint8_t(* iic_init)(void)
uint8_t(* spi_deinit)(void)
uint16_t parallel_data_flag
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]
const float * heater_temp_prof
const uint8_t * heater_dur_prof