Skip to content

Commit 1201c24

Browse files
JordanYateskartben
authored andcommitted
sensor: bme280: wait for sampling period before checking
Wait until the typical time that the sample would be ready before starting to poll the status register. Signed-off-by: Jordan Yates <[email protected]>
1 parent bdb04db commit 1201c24

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

drivers/sensor/bosch/bme280/bme280.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ LOG_MODULE_REGISTER(BME280, CONFIG_SENSOR_LOG_LEVEL);
3333
*/
3434
#define BME280_MEASUREMENT_TIMEOUT_MS 150
3535

36+
/* Equation 9.1, with the fractional parts rounded down */
37+
#define BME280_EXPECTED_SAMPLE_TIME_MS \
38+
1 + BME280_TEMP_SAMPLE_TIME + BME280_PRESS_SAMPLE_TIME + BME280_HUMIDITY_SAMPLE_TIME
39+
40+
BUILD_ASSERT(BME280_EXPECTED_SAMPLE_TIME_MS < BME280_MEASUREMENT_TIMEOUT_MS,
41+
"Expected duration over timeout duration");
42+
3643
struct bme280_config {
3744
union bme280_bus bus;
3845
const struct bme280_bus_io *bus_io;
@@ -154,6 +161,7 @@ int bme280_sample_fetch_helper(const struct device *dev,
154161
struct bme280_data *dev_data = dev->data;
155162
uint8_t buf[8];
156163
int32_t adc_press, adc_temp, adc_humidity;
164+
uint32_t poll_timeout;
157165
int size = 6;
158166
int ret;
159167

@@ -173,9 +181,14 @@ int bme280_sample_fetch_helper(const struct device *dev,
173181
if (ret < 0) {
174182
return ret;
175183
}
184+
/* Wait until the expected measurement time elapses */
185+
k_sleep(K_MSEC(BME280_EXPECTED_SAMPLE_TIME_MS));
186+
poll_timeout = BME280_MEASUREMENT_TIMEOUT_MS - BME280_EXPECTED_SAMPLE_TIME_MS;
187+
#else
188+
poll_timeout = BME280_MEASUREMENT_TIMEOUT_MS;
176189
#endif
177190

178-
ret = bme280_wait_until_ready(dev, K_MSEC(BME280_MEASUREMENT_TIMEOUT_MS));
191+
ret = bme280_wait_until_ready(dev, K_MSEC(poll_timeout));
179192
if (ret < 0) {
180193
return ret;
181194
}

drivers/sensor/bosch/bme280/bme280.h

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,39 +81,54 @@ extern const struct bme280_bus_io bme280_bus_io_i2c;
8181
#endif
8282

8383
#if defined CONFIG_BME280_TEMP_OVER_1X
84-
#define BME280_TEMP_OVER (1 << 5)
84+
#define BME280_TEMP_OVER (1 << 5)
85+
#define BME280_TEMP_SAMPLE_TIME 2
8586
#elif defined CONFIG_BME280_TEMP_OVER_2X
86-
#define BME280_TEMP_OVER (2 << 5)
87+
#define BME280_TEMP_OVER (2 << 5)
88+
#define BME280_TEMP_SAMPLE_TIME 4
8789
#elif defined CONFIG_BME280_TEMP_OVER_4X
88-
#define BME280_TEMP_OVER (3 << 5)
90+
#define BME280_TEMP_OVER (3 << 5)
91+
#define BME280_TEMP_SAMPLE_TIME 8
8992
#elif defined CONFIG_BME280_TEMP_OVER_8X
90-
#define BME280_TEMP_OVER (4 << 5)
93+
#define BME280_TEMP_OVER (4 << 5)
94+
#define BME280_TEMP_SAMPLE_TIME 16
9195
#elif defined CONFIG_BME280_TEMP_OVER_16X
92-
#define BME280_TEMP_OVER (5 << 5)
96+
#define BME280_TEMP_OVER (5 << 5)
97+
#define BME280_TEMP_SAMPLE_TIME 32
9398
#endif
9499

95100
#if defined CONFIG_BME280_PRESS_OVER_1X
96-
#define BME280_PRESS_OVER (1 << 2)
101+
#define BME280_PRESS_OVER (1 << 2)
102+
#define BME280_PRESS_SAMPLE_TIME 2
97103
#elif defined CONFIG_BME280_PRESS_OVER_2X
98-
#define BME280_PRESS_OVER (2 << 2)
104+
#define BME280_PRESS_OVER (2 << 2)
105+
#define BME280_PRESS_SAMPLE_TIME 4
99106
#elif defined CONFIG_BME280_PRESS_OVER_4X
100-
#define BME280_PRESS_OVER (3 << 2)
107+
#define BME280_PRESS_OVER (3 << 2)
108+
#define BME280_PRESS_SAMPLE_TIME 8
101109
#elif defined CONFIG_BME280_PRESS_OVER_8X
102-
#define BME280_PRESS_OVER (4 << 2)
110+
#define BME280_PRESS_OVER (4 << 2)
111+
#define BME280_PRESS_SAMPLE_TIME 16
103112
#elif defined CONFIG_BME280_PRESS_OVER_16X
104-
#define BME280_PRESS_OVER (5 << 2)
113+
#define BME280_PRESS_OVER (5 << 2)
114+
#define BME280_PRESS_SAMPLE_TIME 32
105115
#endif
106116

107117
#if defined CONFIG_BME280_HUMIDITY_OVER_1X
108-
#define BME280_HUMIDITY_OVER 1
118+
#define BME280_HUMIDITY_OVER 1
119+
#define BME280_HUMIDITY_SAMPLE_TIME 2
109120
#elif defined CONFIG_BME280_HUMIDITY_OVER_2X
110-
#define BME280_HUMIDITY_OVER 2
121+
#define BME280_HUMIDITY_OVER 2
122+
#define BME280_HUMIDITY_SAMPLE_TIME 4
111123
#elif defined CONFIG_BME280_HUMIDITY_OVER_4X
112-
#define BME280_HUMIDITY_OVER 3
124+
#define BME280_HUMIDITY_OVER 3
125+
#define BME280_HUMIDITY_SAMPLE_TIME 8
113126
#elif defined CONFIG_BME280_HUMIDITY_OVER_8X
114-
#define BME280_HUMIDITY_OVER 4
127+
#define BME280_HUMIDITY_OVER 4
128+
#define BME280_HUMIDITY_SAMPLE_TIME 16
115129
#elif defined CONFIG_BME280_HUMIDITY_OVER_16X
116-
#define BME280_HUMIDITY_OVER 5
130+
#define BME280_HUMIDITY_OVER 5
131+
#define BME280_HUMIDITY_SAMPLE_TIME 32
117132
#endif
118133

119134
#if defined CONFIG_BME280_STANDBY_05MS

0 commit comments

Comments
 (0)