Skip to content
Snippets Groups Projects
Commit 452deca2 authored by schneider's avatar schneider
Browse files

refact(bme680): Use more granular locks. Sleep using FreeRTOS

parent fc41d13d
No related branches found
No related tags found
1 merge request!428Environmental Sensing Service
...@@ -35,6 +35,34 @@ static int convert_error(int8_t error) ...@@ -35,6 +35,34 @@ static int convert_error(int8_t error)
} }
} }
static int8_t
i2c_write(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size)
{
int8_t ret;
hwlock_acquire(HWLOCK_I2C);
ret = card10_bosch_i2c_write(addr, reg, p_buf, size);
hwlock_release(HWLOCK_I2C);
return ret;
}
static int8_t i2c_read(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size)
{
int8_t ret;
hwlock_acquire(HWLOCK_I2C);
ret = card10_bosch_i2c_read(addr, reg, p_buf, size);
hwlock_release(HWLOCK_I2C);
return ret;
}
static void delay(uint32_t msec)
{
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
card10_bosch_delay(msec);
} else {
vTaskDelay(pdMS_TO_TICKS(msec));
}
}
int epic_bme680_init() int epic_bme680_init()
{ {
int8_t result = BME680_OK; int8_t result = BME680_OK;
...@@ -43,13 +71,11 @@ int epic_bme680_init() ...@@ -43,13 +71,11 @@ int epic_bme680_init()
return 0; return 0;
} }
hwlock_acquire(HWLOCK_I2C);
bme.dev_id = BME680_I2C_ADDR_PRIMARY; bme.dev_id = BME680_I2C_ADDR_PRIMARY;
bme.intf = BME680_I2C_INTF; bme.intf = BME680_I2C_INTF;
bme.read = card10_bosch_i2c_read; bme.read = i2c_read;
bme.write = card10_bosch_i2c_write; bme.write = i2c_write;
bme.delay_ms = card10_bosch_delay; bme.delay_ms = delay;
/* /*
* amb_temp can be set to 25 prior to configuring the gas sensor * amb_temp can be set to 25 prior to configuring the gas sensor
...@@ -61,7 +87,7 @@ int epic_bme680_init() ...@@ -61,7 +87,7 @@ int epic_bme680_init()
result = bme680_init(&bme); result = bme680_init(&bme);
if (result != BME680_OK) { if (result != BME680_OK) {
LOG_ERR("bme680", "bme680_init error: %d\n", result); LOG_ERR("bme680", "bme680_init error: %d\n", result);
goto err; return -convert_error(result);
} }
/* /*
...@@ -92,14 +118,11 @@ int epic_bme680_init() ...@@ -92,14 +118,11 @@ int epic_bme680_init()
LOG_ERR("bme680", LOG_ERR("bme680",
"bme680_set_sensor_settings error: %d\n", "bme680_set_sensor_settings error: %d\n",
result); result);
goto err; return -convert_error(result);
} }
initialized = true; initialized = true;
result = BME680_OK; return 0;
err:
hwlock_release(HWLOCK_I2C);
return -convert_error(result);
} }
int epic_bme680_deinit() int epic_bme680_deinit()
...@@ -108,14 +131,11 @@ int epic_bme680_deinit() ...@@ -108,14 +131,11 @@ int epic_bme680_deinit()
return 0; return 0;
} }
hwlock_acquire(HWLOCK_I2C);
int8_t result = bme680_soft_reset(&bme); int8_t result = bme680_soft_reset(&bme);
if (result != BME680_OK) { if (result != BME680_OK) {
LOG_ERR("bme680", "bme680_soft_reset error: %d\n", result); LOG_ERR("bme680", "bme680_soft_reset error: %d\n", result);
} }
hwlock_release(HWLOCK_I2C);
initialized = false; initialized = false;
return 0; return 0;
} }
...@@ -129,30 +149,26 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data) ...@@ -129,30 +149,26 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data)
return -EINVAL; return -EINVAL;
} }
hwlock_acquire(HWLOCK_I2C);
uint16_t profile_dur = 0; uint16_t profile_dur = 0;
bme680_get_profile_dur(&profile_dur, &bme); bme680_get_profile_dur(&profile_dur, &bme);
result = bme680_set_sensor_mode(&bme); /* Trigger a measurement */ result = bme680_set_sensor_mode(&bme); /* Trigger a measurement */
if (result != BME680_OK) { if (result != BME680_OK) {
LOG_ERR("bme680", "bme680_set_sensor_mode error: %d\n", result); LOG_ERR("bme680", "bme680_set_sensor_mode error: %d\n", result);
goto err; return -convert_error(result);
} }
/* /*
* Wait for the measurement to complete. Release the I2C lock in the * Wait for the measurement to complete. Release the I2C lock in the
* meantime. * meantime.
*/ */
hwlock_release(HWLOCK_I2C);
vTaskDelay(pdMS_TO_TICKS(profile_dur)); vTaskDelay(pdMS_TO_TICKS(profile_dur));
hwlock_acquire(HWLOCK_I2C);
struct bme680_field_data raw_data; struct bme680_field_data raw_data;
result = bme680_get_sensor_data(&raw_data, &bme); result = bme680_get_sensor_data(&raw_data, &bme);
if (result != BME680_OK) { if (result != BME680_OK) {
LOG_ERR("bme680", "bme680_get_sensor_data error: %d\n", result); LOG_ERR("bme680", "bme680_get_sensor_data error: %d\n", result);
goto err; return -convert_error(result);
} }
data->temperature = (float)raw_data.temperature / 100.0f; data->temperature = (float)raw_data.temperature / 100.0f;
...@@ -160,8 +176,5 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data) ...@@ -160,8 +176,5 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data)
data->pressure = raw_data.pressure / 100.0f; data->pressure = raw_data.pressure / 100.0f;
data->gas_resistance = raw_data.gas_resistance; data->gas_resistance = raw_data.gas_resistance;
result = BME680_OK; return 0;
err:
hwlock_release(HWLOCK_I2C);
return -convert_error(result);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment