1 /* bme680.c - Driver for Bosch Sensortec's BME680 temperature, pressure,
2 * humidity and gas sensor
3 *
4 * https://www.bosch-sensortec.com/bst/products/all_products/bme680
5 */
6
7 /*
8 * Copyright (c) 2018 Bosch Sensortec GmbH
9 * Copyright (c) 2022, Leonard Pollak
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 */
13
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/init.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/sys/byteorder.h>
18 #include <zephyr/sys/__assert.h>
19 #include <zephyr/drivers/sensor.h>
20 #include <zephyr/logging/log.h>
21 #include <zephyr/pm/device.h>
22
23 #include "bme680.h"
24
25 LOG_MODULE_REGISTER(bme680, CONFIG_SENSOR_LOG_LEVEL);
26
27 struct bme_data_regs {
28 uint8_t pressure[3];
29 uint8_t temperature[3];
30 uint8_t humidity[2];
31 uint8_t padding[3];
32 uint8_t gas[2];
33 } __packed;
34
35 #if BME680_BUS_SPI
bme680_is_on_spi(const struct device * dev)36 static inline bool bme680_is_on_spi(const struct device *dev)
37 {
38 const struct bme680_config *config = dev->config;
39
40 return config->bus_io == &bme680_bus_io_spi;
41 }
42 #endif
43
bme680_bus_check(const struct device * dev)44 static inline int bme680_bus_check(const struct device *dev)
45 {
46 const struct bme680_config *config = dev->config;
47
48 return config->bus_io->check(&config->bus);
49 }
50
bme680_reg_read(const struct device * dev,uint8_t start,void * buf,int size)51 static inline int bme680_reg_read(const struct device *dev,
52 uint8_t start, void *buf, int size)
53 {
54 const struct bme680_config *config = dev->config;
55
56 return config->bus_io->read(dev, start, buf, size);
57 }
58
bme680_reg_write(const struct device * dev,uint8_t reg,uint8_t val)59 static inline int bme680_reg_write(const struct device *dev, uint8_t reg,
60 uint8_t val)
61 {
62 const struct bme680_config *config = dev->config;
63
64 return config->bus_io->write(dev, reg, val);
65 }
66
bme680_calc_temp(struct bme680_data * data,uint32_t adc_temp)67 static void bme680_calc_temp(struct bme680_data *data, uint32_t adc_temp)
68 {
69 int64_t var1, var2, var3;
70
71 var1 = ((int32_t)adc_temp >> 3) - ((int32_t)data->par_t1 << 1);
72 var2 = (var1 * (int32_t)data->par_t2) >> 11;
73 var3 = ((var1 >> 1) * (var1 >> 1)) >> 12;
74 var3 = ((var3) * ((int32_t)data->par_t3 << 4)) >> 14;
75 data->t_fine = var2 + var3;
76 data->calc_temp = ((data->t_fine * 5) + 128) >> 8;
77 }
78
bme680_calc_press(struct bme680_data * data,uint32_t adc_press)79 static void bme680_calc_press(struct bme680_data *data, uint32_t adc_press)
80 {
81 int32_t var1, var2, var3, calc_press;
82
83 var1 = (((int32_t)data->t_fine) >> 1) - 64000;
84 var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) *
85 (int32_t)data->par_p6) >> 2;
86 var2 = var2 + ((var1 * (int32_t)data->par_p5) << 1);
87 var2 = (var2 >> 2) + ((int32_t)data->par_p4 << 16);
88 var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
89 ((int32_t)data->par_p3 << 5)) >> 3)
90 + (((int32_t)data->par_p2 * var1) >> 1);
91 var1 = var1 >> 18;
92 var1 = ((32768 + var1) * (int32_t)data->par_p1) >> 15;
93 calc_press = 1048576 - adc_press;
94 calc_press = (calc_press - (var2 >> 12)) * ((uint32_t)3125);
95 /* This max value is used to provide precedence to multiplication or
96 * division in the pressure calculation equation to achieve least
97 * loss of precision and avoiding overflows.
98 * i.e Comparing value, signed int 32bit (1 << 30)
99 */
100 if (calc_press >= (int32_t)0x40000000) {
101 calc_press = ((calc_press / var1) << 1);
102 } else {
103 calc_press = ((calc_press << 1) / var1);
104 }
105 var1 = ((int32_t)data->par_p9 *
106 (int32_t)(((calc_press >> 3)
107 * (calc_press >> 3)) >> 13)) >> 12;
108 var2 = ((int32_t)(calc_press >> 2) * (int32_t)data->par_p8) >> 13;
109 var3 = ((int32_t)(calc_press >> 8) * (int32_t)(calc_press >> 8)
110 * (int32_t)(calc_press >> 8)
111 * (int32_t)data->par_p10) >> 17;
112
113 data->calc_press = calc_press
114 + ((var1 + var2 + var3
115 + ((int32_t)data->par_p7 << 7)) >> 4);
116 }
117
bme680_calc_humidity(struct bme680_data * data,uint16_t adc_humidity)118 static void bme680_calc_humidity(struct bme680_data *data, uint16_t adc_humidity)
119 {
120 int32_t var1, var2_1, var2_2, var2, var3, var4, var5, var6;
121 int32_t temp_scaled, calc_hum;
122
123 temp_scaled = (((int32_t)data->t_fine * 5) + 128) >> 8;
124 var1 = (int32_t)(adc_humidity - ((int32_t)((int32_t)data->par_h1 * 16))) -
125 (((temp_scaled * (int32_t)data->par_h3)
126 / ((int32_t)100)) >> 1);
127 var2_1 = (int32_t)data->par_h2;
128 var2_2 = ((temp_scaled * (int32_t)data->par_h4) / (int32_t)100)
129 + (((temp_scaled * ((temp_scaled * (int32_t)data->par_h5)
130 / ((int32_t)100))) >> 6) / ((int32_t)100))
131 + (int32_t)(1 << 14);
132 var2 = (var2_1 * var2_2) >> 10;
133 var3 = var1 * var2;
134 var4 = (int32_t)data->par_h6 << 7;
135 var4 = ((var4) + ((temp_scaled * (int32_t)data->par_h7) /
136 ((int32_t)100))) >> 4;
137 var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
138 var6 = (var4 * var5) >> 1;
139 calc_hum = (((var3 + var6) >> 10) * ((int32_t)1000)) >> 12;
140
141 if (calc_hum > 100000) { /* Cap at 100%rH */
142 calc_hum = 100000;
143 } else if (calc_hum < 0) {
144 calc_hum = 0;
145 }
146
147 data->calc_humidity = calc_hum;
148 }
149
bme680_calc_gas_resistance(struct bme680_data * data,uint8_t gas_range,uint16_t adc_gas_res)150 static void bme680_calc_gas_resistance(struct bme680_data *data, uint8_t gas_range,
151 uint16_t adc_gas_res)
152 {
153 int64_t var1, var3;
154 uint64_t var2;
155
156 static const uint32_t look_up1[16] = { 2147483647, 2147483647, 2147483647,
157 2147483647, 2147483647, 2126008810, 2147483647,
158 2130303777, 2147483647, 2147483647, 2143188679,
159 2136746228, 2147483647, 2126008810, 2147483647,
160 2147483647 };
161
162 static const uint32_t look_up2[16] = { 4096000000, 2048000000, 1024000000,
163 512000000, 255744255, 127110228, 64000000,
164 32258064, 16016016, 8000000, 4000000, 2000000,
165 1000000, 500000, 250000, 125000 };
166
167 var1 = (int64_t)((1340 + (5 * (int64_t)data->range_sw_err)) *
168 ((int64_t)look_up1[gas_range])) >> 16;
169 var2 = (((int64_t)((int64_t)adc_gas_res << 15) - (int64_t)(16777216)) + var1);
170 var3 = (((int64_t)look_up2[gas_range] * (int64_t)var1) >> 9);
171 data->calc_gas_resistance = (uint32_t)((var3 + ((int64_t)var2 >> 1))
172 / (int64_t)var2);
173 }
174
bme680_calc_res_heat(struct bme680_data * data,uint16_t heatr_temp)175 static uint8_t bme680_calc_res_heat(struct bme680_data *data, uint16_t heatr_temp)
176 {
177 uint8_t heatr_res;
178 int32_t var1, var2, var3, var4, var5;
179 int32_t heatr_res_x100;
180 int32_t amb_temp = 25; /* Assume ambient temperature to be 25 deg C */
181
182 if (heatr_temp > 400) { /* Cap temperature */
183 heatr_temp = 400;
184 }
185
186 var1 = ((amb_temp * data->par_gh3) / 1000) * 256;
187 var2 = (data->par_gh1 + 784) * (((((data->par_gh2 + 154009)
188 * heatr_temp * 5) / 100)
189 + 3276800) / 10);
190 var3 = var1 + (var2 / 2);
191 var4 = (var3 / (data->res_heat_range + 4));
192 var5 = (131 * data->res_heat_val) + 65536;
193 heatr_res_x100 = ((var4 / var5) - 250) * 34;
194 heatr_res = (heatr_res_x100 + 50) / 100;
195
196 return heatr_res;
197 }
198
bme680_calc_gas_wait(uint16_t dur)199 static uint8_t bme680_calc_gas_wait(uint16_t dur)
200 {
201 uint8_t factor = 0, durval;
202
203 if (dur >= 0xfc0) {
204 durval = 0xff; /* Max duration*/
205 } else {
206 while (dur > 0x3F) {
207 dur = dur / 4;
208 factor += 1;
209 }
210 durval = dur + (factor * 64);
211 }
212
213 return durval;
214 }
215
bme680_sample_fetch(const struct device * dev,enum sensor_channel chan)216 static int bme680_sample_fetch(const struct device *dev,
217 enum sensor_channel chan)
218 {
219 struct bme680_data *data = dev->data;
220 struct bme_data_regs data_regs;
221 uint8_t gas_range;
222 uint32_t adc_temp, adc_press;
223 uint16_t adc_hum, adc_gas_res;
224 uint8_t status;
225 int cnt = 0;
226 int ret;
227
228 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
229
230 /* Trigger the measurement */
231 ret = bme680_reg_write(dev, BME680_REG_CTRL_MEAS, BME680_CTRL_MEAS_VAL);
232 if (ret < 0) {
233 return ret;
234 }
235
236 do {
237 /* Wait for a maximum of 250ms for data.
238 * Initial delay after boot has been measured at 170ms.
239 * Subequent triggers are < 1ms.
240 */
241 if (cnt++ > 250) {
242 return -EAGAIN;
243 }
244 k_sleep(K_MSEC(1));
245 ret = bme680_reg_read(dev, BME680_REG_MEAS_STATUS, &status, 1);
246 if (ret < 0) {
247 return ret;
248 }
249 } while (!(status & BME680_MSK_NEW_DATA));
250 LOG_DBG("New data after %d ms", cnt);
251
252 ret = bme680_reg_read(dev, BME680_REG_FIELD0, &data_regs, sizeof(data_regs));
253 if (ret < 0) {
254 return ret;
255 }
256
257 adc_press = sys_get_be24(data_regs.pressure) >> 4;
258 adc_temp = sys_get_be24(data_regs.temperature) >> 4;
259 adc_hum = sys_get_be16(data_regs.humidity);
260 adc_gas_res = sys_get_be16(data_regs.gas) >> 6;
261 data->heatr_stab = data_regs.gas[1] & BME680_MSK_HEATR_STAB;
262 gas_range = data_regs.gas[1] & BME680_MSK_GAS_RANGE;
263
264 bme680_calc_temp(data, adc_temp);
265 bme680_calc_press(data, adc_press);
266 bme680_calc_humidity(data, adc_hum);
267 bme680_calc_gas_resistance(data, gas_range, adc_gas_res);
268 return 0;
269 }
270
bme680_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)271 static int bme680_channel_get(const struct device *dev,
272 enum sensor_channel chan,
273 struct sensor_value *val)
274 {
275 struct bme680_data *data = dev->data;
276
277 switch (chan) {
278 case SENSOR_CHAN_AMBIENT_TEMP:
279 /*
280 * data->calc_temp has a resolution of 0.01 degC.
281 * So 5123 equals 51.23 degC.
282 */
283 val->val1 = data->calc_temp / 100;
284 val->val2 = data->calc_temp % 100 * 10000;
285 break;
286 case SENSOR_CHAN_PRESS:
287 /*
288 * data->calc_press has a resolution of 1 Pa.
289 * So 96321 equals 96.321 kPa.
290 */
291 val->val1 = data->calc_press / 1000;
292 val->val2 = (data->calc_press % 1000) * 1000;
293 break;
294 case SENSOR_CHAN_HUMIDITY:
295 /*
296 * data->calc_humidity has a resolution of 0.001 %RH.
297 * So 46333 equals 46.333 %RH.
298 */
299 val->val1 = data->calc_humidity / 1000;
300 val->val2 = (data->calc_humidity % 1000) * 1000;
301 break;
302 case SENSOR_CHAN_GAS_RES:
303 /*
304 * data->calc_gas_resistance has a resolution of 1 ohm.
305 * So 100000 equals 100000 ohms.
306 */
307 val->val1 = data->calc_gas_resistance;
308 val->val2 = 0;
309 break;
310 default:
311 return -ENOTSUP;
312 }
313
314 return 0;
315 }
316
bme680_read_compensation(const struct device * dev)317 static int bme680_read_compensation(const struct device *dev)
318 {
319 struct bme680_data *data = dev->data;
320 uint8_t buff[BME680_LEN_COEFF_ALL];
321 int err = 0;
322
323 if (data->has_read_compensation) {
324 return 0;
325 }
326
327 err = bme680_reg_read(dev, BME680_REG_COEFF1, buff, BME680_LEN_COEFF1);
328 if (err < 0) {
329 return err;
330 }
331
332 err = bme680_reg_read(dev, BME680_REG_COEFF2, &buff[BME680_LEN_COEFF1],
333 BME680_LEN_COEFF2);
334 if (err < 0) {
335 return err;
336 }
337
338 err = bme680_reg_read(dev, BME680_REG_COEFF3,
339 &buff[BME680_LEN_COEFF1 + BME680_LEN_COEFF2],
340 BME680_LEN_COEFF3);
341 if (err < 0) {
342 return err;
343 }
344
345 /* Temperature related coefficients */
346 data->par_t1 = (uint16_t)(BME680_CONCAT_BYTES(buff[32], buff[31]));
347 data->par_t2 = (int16_t)(BME680_CONCAT_BYTES(buff[1], buff[0]));
348 data->par_t3 = (uint8_t)(buff[2]);
349
350 /* Pressure related coefficients */
351 data->par_p1 = (uint16_t)(BME680_CONCAT_BYTES(buff[5], buff[4]));
352 data->par_p2 = (int16_t)(BME680_CONCAT_BYTES(buff[7], buff[6]));
353 data->par_p3 = (int8_t)buff[8];
354 data->par_p4 = (int16_t)(BME680_CONCAT_BYTES(buff[11], buff[10]));
355 data->par_p5 = (int16_t)(BME680_CONCAT_BYTES(buff[13], buff[12]));
356 data->par_p6 = (int8_t)(buff[15]);
357 data->par_p7 = (int8_t)(buff[14]);
358 data->par_p8 = (int16_t)(BME680_CONCAT_BYTES(buff[19], buff[18]));
359 data->par_p9 = (int16_t)(BME680_CONCAT_BYTES(buff[21], buff[20]));
360 data->par_p10 = (uint8_t)(buff[22]);
361
362 /* Humidity related coefficients */
363 data->par_h1 = (uint16_t)(((uint16_t)buff[25] << 4) | (buff[24] & 0x0f));
364 data->par_h2 = (uint16_t)(((uint16_t)buff[23] << 4) | ((buff[24]) >> 4));
365 data->par_h3 = (int8_t)buff[26];
366 data->par_h4 = (int8_t)buff[27];
367 data->par_h5 = (int8_t)buff[28];
368 data->par_h6 = (uint8_t)buff[29];
369 data->par_h7 = (int8_t)buff[30];
370
371 /* Gas heater related coefficients */
372 data->par_gh1 = (int8_t)buff[35];
373 data->par_gh2 = (int16_t)(BME680_CONCAT_BYTES(buff[34], buff[33]));
374 data->par_gh3 = (int8_t)buff[36];
375
376 data->res_heat_val = (int8_t)buff[37];
377 data->res_heat_range = ((buff[39] & BME680_MSK_RH_RANGE) >> 4);
378 data->range_sw_err = ((int8_t)(buff[41] & BME680_MSK_RANGE_SW_ERR)) / 16;
379
380 data->has_read_compensation = true;
381 return 0;
382 }
383
bme680_power_up(const struct device * dev)384 static int bme680_power_up(const struct device *dev)
385 {
386 struct bme680_data *data = dev->data;
387 int err;
388
389 #if BME680_BUS_SPI
390 if (bme680_is_on_spi(dev)) {
391 uint8_t mem_page;
392
393 err = bme680_reg_read(dev, BME680_REG_STATUS, &mem_page, 1);
394 if (err < 0) {
395 return err;
396 }
397
398 data->mem_page = (mem_page & BME680_SPI_MEM_PAGE_MSK) >> BME680_SPI_MEM_PAGE_POS;
399 }
400 #endif
401
402 err = bme680_reg_read(dev, BME680_REG_CHIP_ID, &data->chip_id, 1);
403 if (err < 0) {
404 return err;
405 }
406
407 if (data->chip_id == BME680_CHIP_ID) {
408 LOG_DBG("BME680 chip detected");
409 } else {
410 LOG_ERR("Bad BME680 chip id: 0x%x", data->chip_id);
411 return -ENOTSUP;
412 }
413
414 err = bme680_read_compensation(dev);
415 if (err < 0) {
416 return err;
417 }
418
419 err = bme680_reg_write(dev, BME680_REG_CTRL_HUM, BME680_HUMIDITY_OVER);
420 if (err < 0) {
421 return err;
422 }
423
424 err = bme680_reg_write(dev, BME680_REG_CONFIG, BME680_CONFIG_VAL);
425 if (err < 0) {
426 return err;
427 }
428
429 err = bme680_reg_write(dev, BME680_REG_CTRL_GAS_1, BME680_CTRL_GAS_1_VAL);
430 if (err < 0) {
431 return err;
432 }
433
434 err = bme680_reg_write(dev, BME680_REG_RES_HEAT0,
435 bme680_calc_res_heat(data, BME680_HEATR_TEMP));
436 if (err < 0) {
437 return err;
438 }
439
440 err = bme680_reg_write(dev, BME680_REG_GAS_WAIT0,
441 bme680_calc_gas_wait(BME680_HEATR_DUR_MS));
442 if (err < 0) {
443 return err;
444 }
445
446 return bme680_reg_write(dev, BME680_REG_CTRL_MEAS, BME680_CTRL_MEAS_VAL);
447 }
448
bme680_pm_control(const struct device * dev,enum pm_device_action action)449 static int bme680_pm_control(const struct device *dev, enum pm_device_action action)
450 {
451 int rc = 0;
452
453 switch (action) {
454 case PM_DEVICE_ACTION_SUSPEND:
455 case PM_DEVICE_ACTION_RESUME:
456 case PM_DEVICE_ACTION_TURN_OFF:
457 break;
458 case PM_DEVICE_ACTION_TURN_ON:
459 rc = bme680_power_up(dev);
460 break;
461 default:
462 return -ENOTSUP;
463 }
464
465 return rc;
466 }
467
bme680_init(const struct device * dev)468 static int bme680_init(const struct device *dev)
469 {
470 int err;
471
472 err = bme680_bus_check(dev);
473 if (err < 0) {
474 LOG_ERR("Bus not ready for '%s'", dev->name);
475 return err;
476 }
477
478 return pm_device_driver_init(dev, bme680_pm_control);
479 }
480
481 static DEVICE_API(sensor, bme680_api_funcs) = {
482 .sample_fetch = bme680_sample_fetch,
483 .channel_get = bme680_channel_get,
484 };
485
486 /* Initializes a struct bme680_config for an instance on a SPI bus. */
487 #define BME680_CONFIG_SPI(inst) \
488 { \
489 .bus.spi = SPI_DT_SPEC_INST_GET( \
490 inst, BME680_SPI_OPERATION, 0), \
491 .bus_io = &bme680_bus_io_spi, \
492 }
493
494 /* Initializes a struct bme680_config for an instance on an I2C bus. */
495 #define BME680_CONFIG_I2C(inst) \
496 { \
497 .bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
498 .bus_io = &bme680_bus_io_i2c, \
499 }
500
501 /*
502 * Main instantiation macro, which selects the correct bus-specific
503 * instantiation macros for the instance.
504 */
505 #define BME680_DEFINE(inst) \
506 static struct bme680_data bme680_data_##inst; \
507 static const struct bme680_config bme680_config_##inst = \
508 COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
509 (BME680_CONFIG_SPI(inst)), \
510 (BME680_CONFIG_I2C(inst))); \
511 PM_DEVICE_DT_INST_DEFINE(inst, bme680_pm_control); \
512 SENSOR_DEVICE_DT_INST_DEFINE(inst, \
513 bme680_init, \
514 PM_DEVICE_DT_INST_GET(inst), \
515 &bme680_data_##inst, \
516 &bme680_config_##inst, \
517 POST_KERNEL, \
518 CONFIG_SENSOR_INIT_PRIORITY, \
519 &bme680_api_funcs);
520
521 /* Create the struct device for every status "okay" node in the devicetree. */
522 DT_INST_FOREACH_STATUS_OKAY(BME680_DEFINE)
523