Skip to content

Commit b9c9426

Browse files
JordanYatescarlescufi
authored andcommitted
sensor: bme680: simplify conversion logic
Read less data from the sensor and use standard zephyr functions for extracting integers from byte arrays. Signed-off-by: Jordan Yates <[email protected]>
1 parent b67185b commit b9c9426

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

drivers/sensor/bosch/bme680/bme680.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323

2424
LOG_MODULE_REGISTER(bme680, CONFIG_SENSOR_LOG_LEVEL);
2525

26+
struct bme_data_regs {
27+
uint8_t pressure[3];
28+
uint8_t temperature[3];
29+
uint8_t humidity[2];
30+
uint8_t padding[3];
31+
uint8_t gas[2];
32+
} __packed;
2633

2734
#if BME680_BUS_SPI
2835
static inline bool bme680_is_on_spi(const struct device *dev)
@@ -41,7 +48,7 @@ static inline int bme680_bus_check(const struct device *dev)
4148
}
4249

4350
static inline int bme680_reg_read(const struct device *dev,
44-
uint8_t start, uint8_t *buf, int size)
51+
uint8_t start, void *buf, int size)
4552
{
4653
const struct bme680_config *config = dev->config;
4754

@@ -209,12 +216,11 @@ static int bme680_sample_fetch(const struct device *dev,
209216
enum sensor_channel chan)
210217
{
211218
struct bme680_data *data = dev->data;
212-
uint8_t buff[BME680_LEN_FIELD] = { 0 };
219+
struct bme_data_regs data_regs;
213220
uint8_t gas_range;
214221
uint32_t adc_temp, adc_press;
215222
uint16_t adc_hum, adc_gas_res;
216223
uint8_t status;
217-
int size = BME680_LEN_FIELD;
218224
int cnt = 0;
219225
int ret;
220226

@@ -235,35 +241,29 @@ static int bme680_sample_fetch(const struct device *dev,
235241
return -EAGAIN;
236242
}
237243
k_sleep(K_MSEC(1));
238-
ret = bme680_reg_read(dev, BME680_REG_FIELD0, &status, 1);
244+
ret = bme680_reg_read(dev, BME680_REG_MEAS_STATUS, &status, 1);
239245
if (ret < 0) {
240246
return ret;
241247
}
242248
} while (!(status & BME680_MSK_NEW_DATA));
243249
LOG_DBG("New data after %d ms", cnt);
244250

245-
ret = bme680_reg_read(dev, BME680_REG_FIELD0, buff, size);
251+
ret = bme680_reg_read(dev, BME680_REG_FIELD0, &data_regs, sizeof(data_regs));
246252
if (ret < 0) {
247253
return ret;
248254
}
249255

250-
data->new_data = true;
251-
data->heatr_stab = buff[14] & BME680_MSK_HEATR_STAB;
252-
253-
adc_press = (uint32_t)(((uint32_t)buff[2] << 12) | ((uint32_t)buff[3] << 4)
254-
| ((uint32_t)buff[4] >> 4));
255-
adc_temp = (uint32_t)(((uint32_t)buff[5] << 12) | ((uint32_t)buff[6] << 4)
256-
| ((uint32_t)buff[7] >> 4));
257-
adc_hum = (uint16_t)(((uint32_t)buff[8] << 8) | (uint32_t)buff[9]);
258-
adc_gas_res = (uint16_t)((uint32_t)buff[13] << 2 | (((uint32_t)buff[14]) >> 6));
259-
gas_range = buff[14] & BME680_MSK_GAS_RANGE;
260-
261-
if (data->new_data) {
262-
bme680_calc_temp(data, adc_temp);
263-
bme680_calc_press(data, adc_press);
264-
bme680_calc_humidity(data, adc_hum);
265-
bme680_calc_gas_resistance(data, gas_range, adc_gas_res);
266-
}
256+
adc_press = sys_get_be24(data_regs.pressure) >> 4;
257+
adc_temp = sys_get_be24(data_regs.temperature) >> 4;
258+
adc_hum = sys_get_be16(data_regs.humidity);
259+
adc_gas_res = sys_get_be16(data_regs.gas) >> 6;
260+
data->heatr_stab = data_regs.gas[1] & BME680_MSK_HEATR_STAB;
261+
gas_range = data_regs.gas[1] & BME680_MSK_GAS_RANGE;
262+
263+
bme680_calc_temp(data, adc_temp);
264+
bme680_calc_press(data, adc_press);
265+
bme680_calc_humidity(data, adc_hum);
266+
bme680_calc_gas_resistance(data, gas_range, adc_gas_res);
267267
return 0;
268268
}
269269

drivers/sensor/bosch/bme680/bme680.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,24 @@ struct bme680_config {
5757

5858
#define BME680_CHIP_ID 0x61
5959

60-
#define BME680_LEN_FIELD 15
6160
#define BME680_LEN_COEFF_ALL 42
6261
#define BME680_LEN_COEFF1 23
6362
#define BME680_LEN_COEFF2 14
6463
#define BME680_LEN_COEFF3 5
6564

6665
#define BME680_REG_COEFF3 0x00
67-
#define BME680_REG_FIELD0 0x1d
66+
#define BME680_REG_MEAS_STATUS 0x1D
67+
#define BME680_REG_FIELD0 0x1F
6868
#define BME680_REG_IDAC_HEAT0 0x50
6969
#define BME680_REG_RES_HEAT0 0x5A
7070
#define BME680_REG_GAS_WAIT0 0x64
7171
#define BME680_REG_SHD_HEATR_DUR 0x6E
7272
#define BME680_REG_CTRL_GAS_0 0x70
7373
#define BME680_REG_CTRL_GAS_1 0x71
7474
#define BME680_REG_CTRL_HUM 0x72
75+
#define BME680_REG_STATUS 0x73
7576
#define BME680_REG_CTRL_MEAS 0x74
7677
#define BME680_REG_CONFIG 0x75
77-
#define BME680_REG_STATUS 0x73
7878
#define BME680_REG_UNIQUE_ID 0x83
7979
#define BME680_REG_COEFF1 0x8a
8080
#define BME680_REG_COEFF2 0xe1
@@ -204,7 +204,6 @@ struct bme680_data {
204204
uint32_t calc_gas_resistance;
205205

206206
/* Additional information */
207-
uint8_t new_data;
208207
uint8_t heatr_stab;
209208

210209
/* Carryover between temperature and pressure/humidity compensation. */

0 commit comments

Comments
 (0)