bsec now working

modbus
Ondřej Hruška 3 years ago
parent 4158f225e1
commit c84c085a21
  1. 34
      components/bme680/src/bme68x.c
  2. 16
      components/bme680/src/bsec2.c
  3. 17
      main/voc_sensor.c

@ -39,6 +39,11 @@
#include "bme68x.h" #include "bme68x.h"
#include <stdio.h> #include <stdio.h>
// for logging only
#include <sdkconfig.h>
#include <esp_log.h>
static const char *TAG = "bme68x";
/* This internal API is used to read the calibration coefficients */ /* This internal API is used to read the calibration coefficients */
static int8_t get_calib_data(struct bme68x_dev *dev); static int8_t get_calib_data(struct bme68x_dev *dev);
@ -160,13 +165,24 @@ int8_t bme68x_init(struct bme68x_dev *dev)
{ {
/* Get the Calibration data */ /* Get the Calibration data */
rslt = get_calib_data(dev); rslt = get_calib_data(dev);
if (rslt != BME68X_OK) {
ESP_LOGE(TAG, "get_calib_data failed");
}
} else {
ESP_LOGE(TAG, "read_variant_id failed");
} }
} }
else else
{ {
ESP_LOGE(TAG, "bme68x_get_regs unknown chip id %x", dev->chip_id);
rslt = BME68X_E_DEV_NOT_FOUND; rslt = BME68X_E_DEV_NOT_FOUND;
} }
} else {
ESP_LOGE(TAG, "bme68x_get_regs failed");
} }
} else {
ESP_LOGE(TAG, "bme68x_soft_reset failed");
} }
return rslt; return rslt;
@ -212,17 +228,20 @@ int8_t bme68x_set_regs(const uint8_t *reg_addr, const uint8_t *reg_data, uint32_
dev->intf_rslt = dev->write(tmp_buff[0], &tmp_buff[1], (2 * len) - 1, dev->intf_ptr); dev->intf_rslt = dev->write(tmp_buff[0], &tmp_buff[1], (2 * len) - 1, dev->intf_ptr);
if (dev->intf_rslt != 0) if (dev->intf_rslt != 0)
{ {
ESP_LOGE(TAG, "bme68x_set_regs: write failed");
rslt = BME68X_E_COM_FAIL; rslt = BME68X_E_COM_FAIL;
} }
} }
} }
else else
{ {
ESP_LOGE(TAG, "bme68x_set_regs: invalid len");
rslt = BME68X_E_INVALID_LENGTH; rslt = BME68X_E_INVALID_LENGTH;
} }
} }
else else
{ {
ESP_LOGE(TAG, "bme68x_set_regs: null ptr");
rslt = BME68X_E_NULL_PTR; rslt = BME68X_E_NULL_PTR;
} }
@ -247,17 +266,21 @@ int8_t bme68x_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct
if (rslt == BME68X_OK) if (rslt == BME68X_OK)
{ {
reg_addr = reg_addr | BME68X_SPI_RD_MSK; reg_addr = reg_addr | BME68X_SPI_RD_MSK;
} else {
ESP_LOGE(TAG, "bme68x_get_regs: set_mem_page fail");
} }
} }
dev->intf_rslt = dev->read(reg_addr, reg_data, len, dev->intf_ptr); dev->intf_rslt = dev->read(reg_addr, reg_data, len, dev->intf_ptr);
if (dev->intf_rslt != 0) if (dev->intf_rslt != 0)
{ {
ESP_LOGE(TAG, "bme68x_get_regs: comm fail");
rslt = BME68X_E_COM_FAIL; rslt = BME68X_E_COM_FAIL;
} }
} }
else else
{ {
ESP_LOGE(TAG, "bme68x_get_regs: null ptr");
rslt = BME68X_E_NULL_PTR; rslt = BME68X_E_NULL_PTR;
} }
@ -298,8 +321,14 @@ int8_t bme68x_soft_reset(struct bme68x_dev *dev)
{ {
rslt = get_mem_page(dev); rslt = get_mem_page(dev);
} }
} else {
ESP_LOGE(TAG, "bme68x_soft_reset: bme68x_set_regs fail");
} }
} else {
ESP_LOGE(TAG, "bme68x_soft_reset: get_mem_page fail");
} }
} else {
ESP_LOGE(TAG, "bme68x_soft_reset: null ptr");
} }
return rslt; return rslt;
@ -708,12 +737,17 @@ int8_t bme68x_get_heatr_conf(const struct bme68x_heatr_conf *conf, struct bme68x
{ {
conf->heatr_dur_prof[i] = data_array[i]; conf->heatr_dur_prof[i] = data_array[i];
} }
} else {
ESP_LOGE(TAG, "bme68x_get_heatr_conf: get regs err");
} }
} }
else else
{ {
ESP_LOGE(TAG, "bme68x_get_heatr_conf: null ptr");
rslt = BME68X_E_NULL_PTR; rslt = BME68X_E_NULL_PTR;
} }
} else {
ESP_LOGE(TAG, "bme68x_get_heatr_conf: bme68x_get_regs fail");
} }
return rslt; return rslt;

@ -315,12 +315,6 @@ static void bsec2_setBme68xConfigForced(struct bsec2 *self) {
return; return;
} }
self->sensor_status = bme68x_get_heatr_conf(&hconf, self->sensor);
if (BME68X_OK != self->sensor_status) {
bsec2_errormsg("bsec2_setBme68xConfigForced: error from bme68x_get_heatr_conf");
return;
}
conf.os_hum = self->bmeConf.humidity_oversampling; conf.os_hum = self->bmeConf.humidity_oversampling;
conf.os_pres = self->bmeConf.pressure_oversampling; conf.os_pres = self->bmeConf.pressure_oversampling;
conf.os_temp = self->bmeConf.temperature_oversampling; conf.os_temp = self->bmeConf.temperature_oversampling;
@ -334,6 +328,7 @@ static void bsec2_setBme68xConfigForced(struct bsec2 *self) {
hconf.enable = BME68X_ENABLE; hconf.enable = BME68X_ENABLE;
hconf.heatr_dur = self->bmeConf.heater_duration; hconf.heatr_dur = self->bmeConf.heater_duration;
hconf.heatr_temp = self->bmeConf.heater_temperature; hconf.heatr_temp = self->bmeConf.heater_temperature;
// the other parameters are not needed for forced mode
self->sensor_status = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &hconf, self->sensor); self->sensor_status = bme68x_set_heatr_conf(BME68X_FORCED_MODE, &hconf, self->sensor);
if (BME68X_OK != self->sensor_status) { if (BME68X_OK != self->sensor_status) {
@ -362,12 +357,6 @@ static void bsec2_setBme68xConfigParallel(struct bsec2 *self) {
return; return;
} }
self->sensor_status = bme68x_get_heatr_conf(&hconf, self->sensor);
if (BME68X_OK != self->sensor_status) {
bsec2_errormsg("bsec2_setBme68xConfigParallel: error from bme68x_get_heatr_conf");
return;
}
conf.os_hum = self->bmeConf.humidity_oversampling; conf.os_hum = self->bmeConf.humidity_oversampling;
conf.os_pres = self->bmeConf.pressure_oversampling; conf.os_pres = self->bmeConf.pressure_oversampling;
conf.os_temp = self->bmeConf.temperature_oversampling; conf.os_temp = self->bmeConf.temperature_oversampling;
@ -381,9 +370,10 @@ static void bsec2_setBme68xConfigParallel(struct bsec2 *self) {
sharedHeaterDur = BSEC_TOTAL_HEAT_DUR - (bme68x_get_meas_dur(BME68X_PARALLEL_MODE, &conf, self->sensor) / INT64_C(1000)); sharedHeaterDur = BSEC_TOTAL_HEAT_DUR - (bme68x_get_meas_dur(BME68X_PARALLEL_MODE, &conf, self->sensor) / INT64_C(1000));
hconf.enable = BME68X_ENABLE; hconf.enable = BME68X_ENABLE;
hconf.shared_heatr_dur = sharedHeaterDur;
hconf.heatr_dur = self->bmeConf.heater_duration; hconf.heatr_dur = self->bmeConf.heater_duration;
hconf.heatr_temp = self->bmeConf.heater_temperature; hconf.heatr_temp = self->bmeConf.heater_temperature;
hconf.shared_heatr_dur = sharedHeaterDur; hconf.heatr_temp_prof = self->bmeConf.heater_temperature_profile;
hconf.heatr_dur_prof = self->bmeConf.heater_duration_profile; hconf.heatr_dur_prof = self->bmeConf.heater_duration_profile;
hconf.profile_len = self->bmeConf.heater_profile_len; hconf.profile_len = self->bmeConf.heater_profile_len;

@ -78,6 +78,10 @@ static BME68X_INTF_RET_TYPE user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, u
i2c_reg_read(cmd, itf->dev_addr, reg_addr, reg_data, length); i2c_reg_read(cmd, itf->dev_addr, reg_addr, reg_data, length);
esp_err_t ret = i2c_master_cmd_begin(VOC_I2C_NUM, cmd, pdMS_TO_TICKS(VOC_I2C_TO_MS)); esp_err_t ret = i2c_master_cmd_begin(VOC_I2C_NUM, cmd, pdMS_TO_TICKS(VOC_I2C_TO_MS));
i2c_cmd_link_delete(cmd); i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "I2C read error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), itf->dev_addr, reg_addr, length);
}
ESP_LOG_BUFFER_HEXDUMP(TAG, reg_data, length, ESP_LOG_DEBUG);
return ret == ESP_OK ? BME68X_OK : BME68X_E_COM_FAIL; return ret == ESP_OK ? BME68X_OK : BME68X_E_COM_FAIL;
} }
@ -91,6 +95,9 @@ static BME68X_INTF_RET_TYPE user_i2c_write(uint8_t reg_addr, const uint8_t *reg_
i2c_reg_write(cmd, itf->dev_addr, reg_addr, reg_data, length); i2c_reg_write(cmd, itf->dev_addr, reg_addr, reg_data, length);
esp_err_t ret = i2c_master_cmd_begin(VOC_I2C_NUM, cmd, pdMS_TO_TICKS(VOC_I2C_TO_MS)); esp_err_t ret = i2c_master_cmd_begin(VOC_I2C_NUM, cmd, pdMS_TO_TICKS(VOC_I2C_TO_MS));
i2c_cmd_link_delete(cmd); i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "I2C write error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), itf->dev_addr, reg_addr, length);
}
return ret == ESP_OK ? BME68X_OK : BME68X_E_COM_FAIL; return ret == ESP_OK ? BME68X_OK : BME68X_E_COM_FAIL;
} }
@ -103,7 +110,6 @@ static esp_err_t voc_init(void) {
gas_sensor.read = user_i2c_read; gas_sensor.read = user_i2c_read;
gas_sensor.write = user_i2c_write; gas_sensor.write = user_i2c_write;
gas_sensor.delay_us = user_delay_us; gas_sensor.delay_us = user_delay_us;
gas_sensor.intf_ptr = &gas_sensor;
for (int retry = 0; retry < 3; retry++) { for (int retry = 0; retry < 3; retry++) {
ESP_LOGD(TAG, "BME680 initializing"); ESP_LOGD(TAG, "BME680 initializing");
@ -180,13 +186,13 @@ void voc_read_task(void *param) {
if (ESP_OK != voc_init()) { if (ESP_OK != voc_init()) {
ESP_LOGE(TAG, "Fail to init sensor!"); ESP_LOGE(TAG, "Fail to init sensor!");
return; goto abort;
} }
int rv = bsec2_init(&gas_sensor_bsec, &gas_sensor); int rv = bsec2_init(&gas_sensor_bsec, &gas_sensor);
if (rv != 0) { if (rv != 0) {
ESP_LOGE(TAG, "Error in bsec init: %d", rv); ESP_LOGE(TAG, "Error in bsec init: %d", rv);
return; goto abort;
} }
bsecSensor sensorList[] = { bsecSensor sensorList[] = {
@ -214,7 +220,7 @@ void voc_read_task(void *param) {
if (false == rv) { if (false == rv) {
ESP_LOGE(TAG, "Fail to update bsec subscriptions!"); ESP_LOGE(TAG, "Fail to update bsec subscriptions!");
return; goto abort;
} }
bsec2_attachCallback(&gas_sensor_bsec, new_data_callback); bsec2_attachCallback(&gas_sensor_bsec, new_data_callback);
@ -235,4 +241,7 @@ void voc_read_task(void *param) {
} }
vTaskDelay(pdMS_TO_TICKS(100)); vTaskDelay(pdMS_TO_TICKS(100));
} }
abort:
vTaskDelete(NULL);
} }

Loading…
Cancel
Save