Skip to content
Snippets Groups Projects
Commit 76acf664 authored by rahix's avatar rahix
Browse files

Merge branch 'rahix/fix-bme' into 'master'

Fix BME680 Bugs

See merge request card10/firmware!188
parents 47b8af42 28775e18
No related branches found
No related tags found
No related merge requests found
#include <stdbool.h> #include "epicardium.h"
#include <stddef.h> #include "modules/modules.h"
#include <stdio.h> #include "modules/log.h"
#include "card10.h"
#include "bme680.h" #include "bme680.h"
#include "bosch.h" #include "bosch.h"
#include "card10.h"
#include "epicardium.h" #include <stdbool.h>
#include "modules.h" #include <stddef.h>
#include "modules/log.h" #include <stdio.h>
#define HEATR_TEMP 320 #define HEATR_TEMP 320
#define HEATR_DUR 150 #define HEATR_DUR 150
...@@ -19,6 +20,8 @@ static struct bme680_dev bme; ...@@ -19,6 +20,8 @@ static struct bme680_dev bme;
static int convert_error(int8_t error) static int convert_error(int8_t error)
{ {
switch (error) { switch (error) {
case BME680_OK:
return 0;
case BME680_E_NULL_PTR: case BME680_E_NULL_PTR:
return EFAULT; return EFAULT;
case BME680_E_COM_FAIL: case BME680_E_COM_FAIL:
...@@ -36,25 +39,37 @@ int epic_bme680_init() ...@@ -36,25 +39,37 @@ int epic_bme680_init()
{ {
int8_t result = BME680_OK; int8_t result = BME680_OK;
if (__builtin_expect(!initialized, 0)) { if (initialized) {
return 0;
}
if (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)) < 0) {
return -EBUSY;
}
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 = card10_bosch_i2c_read;
bme.write = card10_bosch_i2c_write; bme.write = card10_bosch_i2c_write;
bme.delay_ms = card10_bosch_delay; bme.delay_ms = card10_bosch_delay;
/* amb_temp can be set to 25 prior to configuring the gas sensor
* or by performing a few temperature readings without operating the gas sensor. /*
* amb_temp can be set to 25 prior to configuring the gas sensor
* or by performing a few temperature readings without operating
* the gas sensor.
*/ */
bme.amb_temp = 25; bme.amb_temp = 25;
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);
return -convert_error(result); goto err;
} }
/* Select the power mode */ /*
/* Must be set before writing the sensor configuration */ * Select the power mode. Must be set before writing the sensor
* configuration
*/
bme.power_mode = BME680_FORCED_MODE; bme.power_mode = BME680_FORCED_MODE;
/* Set the temperature, pressure and humidity settings */ /* Set the temperature, pressure and humidity settings */
...@@ -79,24 +94,32 @@ int epic_bme680_init() ...@@ -79,24 +94,32 @@ 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);
return -convert_error(result); goto err;
} }
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()
{ {
int8_t result = BME680_OK; if (!initialized) {
return 0;
}
if (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)) < 0) {
return -EBUSY;
}
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);
return -convert_error(result);
} }
hwlock_release(HWLOCK_I2C);
initialized = false; initialized = false;
return 0; return 0;
} }
...@@ -110,29 +133,43 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data) ...@@ -110,29 +133,43 @@ int epic_bme680_read_sensors(struct bme680_sensor_data *data)
return -EINVAL; return -EINVAL;
} }
if (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)) < 0) {
return -EBUSY;
}
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);
return -convert_error(result); goto err;
} }
vTaskDelay(pdMS_TO_TICKS( /*
profile_dur)); /* Wait for the measurement to complete */ * Wait for the measurement to complete. Release the I2C lock in the
* meantime.
*/
hwlock_release(HWLOCK_I2C);
vTaskDelay(pdMS_TO_TICKS(profile_dur));
if (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)) < 0) {
return -EBUSY;
}
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);
return -convert_error(result); goto err;
} }
data->temperature = raw_data.temperature / 100.0l; data->temperature = (float)raw_data.temperature / 100.0f;
data->humidity = raw_data.humidity / 1000.0l; data->humidity = raw_data.humidity / 1000.0f;
data->pressure = raw_data.pressure / 100.0l; data->pressure = raw_data.pressure / 100.0f;
data->gas_resistance = raw_data.gas_resistance; data->gas_resistance = raw_data.gas_resistance;
return 0; result = BME680_OK;
err:
hwlock_release(HWLOCK_I2C);
return -convert_error(result);
} }
...@@ -12,7 +12,7 @@ static mp_obj_t mp_bme680_init() ...@@ -12,7 +12,7 @@ static mp_obj_t mp_bme680_init()
mp_raise_OSError(-ret); mp_raise_OSError(-ret);
} }
return 0; return mp_const_none;
} }
static MP_DEFINE_CONST_FUN_OBJ_0(bme680_init_obj, mp_bme680_init); static MP_DEFINE_CONST_FUN_OBJ_0(bme680_init_obj, mp_bme680_init);
...@@ -24,7 +24,7 @@ static mp_obj_t mp_bme680_deinit() ...@@ -24,7 +24,7 @@ static mp_obj_t mp_bme680_deinit()
mp_raise_OSError(-ret); mp_raise_OSError(-ret);
} }
return 0; return mp_const_none;
} }
static MP_DEFINE_CONST_FUN_OBJ_0(bme680_deinit_obj, mp_bme680_deinit); static MP_DEFINE_CONST_FUN_OBJ_0(bme680_deinit_obj, mp_bme680_deinit);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment