BME-Sensor / i2C mutex bugfix

This commit is contained in:
Verkehrsrot 2019-07-25 22:04:38 +02:00
parent b7b41968ab
commit 837988081e
3 changed files with 18 additions and 14 deletions

View File

@ -41,6 +41,7 @@ Adafruit_BME280 bme; // I2C
int bme_init(void) { int bme_init(void) {
// return = 0 -> error / return = 1 -> success // return = 0 -> error / return = 1 -> success
int rc = 1;
#ifdef HAS_BME680 #ifdef HAS_BME680
// block i2c bus access // block i2c bus access
@ -59,7 +60,8 @@ int bme_init(void) {
ESP_LOGI(TAG, "BME680 sensor found and initialized"); ESP_LOGI(TAG, "BME680 sensor found and initialized");
else { else {
ESP_LOGE(TAG, "BME680 sensor not found"); ESP_LOGE(TAG, "BME680 sensor not found");
goto error; rc = 0;
goto finish;
} }
loadState(); loadState();
@ -71,39 +73,40 @@ int bme_init(void) {
ESP_LOGI(TAG, "BSEC subscription succesful"); ESP_LOGI(TAG, "BSEC subscription succesful");
else { else {
ESP_LOGE(TAG, "BSEC subscription error"); ESP_LOGE(TAG, "BSEC subscription error");
goto error; rc = 0;
goto finish;
} }
} else { } else {
ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error"); ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error");
goto error; rc = 0;
goto finish;
} }
#elif defined HAS_BME280 #elif defined HAS_BME280
bool status; bool status;
// return = 0 -> error / return = 1 -> success
// block i2c bus access // block i2c bus access
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
status = bme.begin(BME280_ADDR); status = bme.begin(BME280_ADDR);
if (!status) { if (!status) {
ESP_LOGE(TAG, "BME280 sensor not found"); ESP_LOGE(TAG, "BME280 sensor not found");
goto error; rc = 0;
goto finish;
} }
ESP_LOGI(TAG, "BME280 sensor found and initialized"); ESP_LOGI(TAG, "BME280 sensor found and initialized");
} else { } else {
ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error"); ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error");
goto error; rc = 0;
goto finish;
} }
#endif #endif
finish:
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1; return rc;
error:
I2C_MUTEX_UNLOCK(); // release i2c bus access
return 0;
} // bme_init() } // bme_init()

View File

@ -63,7 +63,7 @@ void init_display(const char *Productname, const char *Version) {
// block i2c bus access // block i2c bus access
if (!I2C_MUTEX_LOCK()) if (!I2C_MUTEX_LOCK())
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); ESP_LOGD(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
else { else {
// show startup screen // show startup screen
uint8_t buf[32]; uint8_t buf[32];
@ -143,7 +143,7 @@ void refreshTheDisplay(bool nextPage) {
// block i2c bus access // block i2c bus access
if (!I2C_MUTEX_LOCK()) if (!I2C_MUTEX_LOCK())
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); ESP_LOGD(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
else { else {
// set display on/off according to current device configuration // set display on/off according to current device configuration
if (DisplayIsOn != cfg.screenon) { if (DisplayIsOn != cfg.screenon) {

View File

@ -114,6 +114,7 @@ void setup() {
// create some semaphores for syncing / mutexing tasks // create some semaphores for syncing / mutexing tasks
I2Caccess = xSemaphoreCreateMutex(); // for access management of i2c bus I2Caccess = xSemaphoreCreateMutex(); // for access management of i2c bus
assert(I2Caccess != NULL); assert(I2Caccess != NULL);
I2C_MUTEX_UNLOCK();
// disable brownout detection // disable brownout detection
#ifdef DISABLE_BROWNOUT #ifdef DISABLE_BROWNOUT
@ -377,7 +378,7 @@ void setup() {
"bmeloop", // name of task "bmeloop", // name of task
2048, // stack size of task 2048, // stack size of task
(void *)1, // parameter of the task (void *)1, // parameter of the task
1, // priority of the task 2, // priority of the task
&BmeTask, // task handle &BmeTask, // task handle
1); // CPU core 1); // CPU core
} }