diff --git a/include/globals.h b/include/globals.h index 2b421d6d..f8ef5fa5 100644 --- a/include/globals.h +++ b/include/globals.h @@ -35,6 +35,10 @@ #define BLE_MODE (0x40) #define SCREEN_MODE (0x80) +// I2C bus access control +#define I2C_MUTEX_LOCK() xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) != pdTRUE +#define I2C_MUTEX_UNLOCK() xSemaphoreGive(I2Caccess) + // Struct holding devices's runtime configuration typedef struct { uint8_t lorasf; // 7-12, lora spreadfactor diff --git a/src/bme680mems.cpp b/src/bme680mems.cpp index bb420db4..924bc656 100644 --- a/src/bme680mems.cpp +++ b/src/bme680mems.cpp @@ -32,8 +32,7 @@ int bme_init(void) { // return = 0 -> error / return = 1 -> success // block i2c bus access - if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == - pdTRUE) { + if (I2C_MUTEX_LOCK()) { Wire.begin(HAS_BME); iaqSensor.begin(BME_ADDR, Wire); @@ -62,17 +61,16 @@ int bme_init(void) { ESP_LOGE(TAG, "BSEC subscription error"); goto error; } - } else { ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error"); goto error; } - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access return 1; error: - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access return 0; } // bme_init() @@ -106,24 +104,24 @@ void bme_loop(void *pvParameters) { configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check #ifdef HAS_BME - // block i2c bus access - while (xSemaphoreTake(I2Caccess, portMAX_DELAY) == pdTRUE) { - - if (iaqSensor.run()) { // If new data is available - bme_status.raw_temperature = iaqSensor.rawTemperature; - bme_status.raw_humidity = iaqSensor.rawHumidity; - bme_status.temperature = iaqSensor.temperature; - bme_status.humidity = iaqSensor.humidity; - bme_status.pressure = - (iaqSensor.pressure / 100.0); // conversion Pa -> hPa - bme_status.iaq = iaqSensor.iaqEstimate; - bme_status.iaq_accuracy = iaqSensor.iaqAccuracy; - bme_status.gas = iaqSensor.gasResistance; - updateState(); + while (1) { + // block i2c bus access + if (I2C_MUTEX_LOCK()) { + if (iaqSensor.run()) { // If new data is available + bme_status.raw_temperature = iaqSensor.rawTemperature; + bme_status.raw_humidity = iaqSensor.rawHumidity; + bme_status.temperature = iaqSensor.temperature; + bme_status.humidity = iaqSensor.humidity; + bme_status.pressure = + (iaqSensor.pressure / 100.0); // conversion Pa -> hPa + bme_status.iaq = iaqSensor.iaqEstimate; + bme_status.iaq_accuracy = iaqSensor.iaqAccuracy; + bme_status.gas = iaqSensor.gasResistance; + updateState(); + } + I2C_MUTEX_UNLOCK(); } - xSemaphoreGive(I2Caccess); // release i2c bus access - - } // while + } #endif ESP_LOGE(TAG, "BME task ended"); vTaskDelete(BmeTask); // should never be reached diff --git a/src/display.cpp b/src/display.cpp index 861328fd..af472397 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -99,8 +99,7 @@ void init_display(const char *Productname, const char *Version) { void refreshtheDisplay() { // block i2c bus access - if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == - pdTRUE) { + if (I2C_MUTEX_LOCK()) { // set display on/off according to current device configuration if (DisplayState != cfg.screenon) { @@ -193,7 +192,7 @@ void refreshtheDisplay() { #endif // HAS_LORA - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access } } // refreshDisplay() diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 624e30e4..50ee9fb4 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -13,8 +13,7 @@ int rtc_init(void) { // return = 0 -> error / return = 1 -> success // block i2c bus access - if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == - pdTRUE) { + if (I2C_MUTEX_LOCK()) { Wire.begin(HAS_RTC); Rtc.Begin(); @@ -48,12 +47,12 @@ int rtc_init(void) { goto error; } - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access ESP_LOGI(TAG, "RTC initialized"); return 1; error: - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access return 0; } // rtc_init() @@ -61,9 +60,9 @@ error: int set_rtctime(uint32_t UTCTime) { // return = 0 -> error / return = 1 -> success // block i2c bus access - while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { + if (I2C_MUTEX_LOCK()) { Rtc.SetDateTime(RtcDateTime(UTCTime)); - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access return 1; } return 0; @@ -72,9 +71,9 @@ int set_rtctime(uint32_t UTCTime) { int set_rtctime(RtcDateTime t) { // return = 0 -> error / return = 1 -> success // block i2c bus access - while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { + if (I2C_MUTEX_LOCK()) { Rtc.SetDateTime(t); - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access return 1; } return 0; @@ -84,14 +83,14 @@ time_t get_rtctime(void) { // never call now() in this function, would cause recursion! time_t tt = 0; // block i2c bus access - if (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { + if (I2C_MUTEX_LOCK()) { if (!Rtc.IsDateTimeValid()) { ESP_LOGW(TAG, "RTC lost confidence in the DateTime"); } else { RtcDateTime t = Rtc.GetDateTime(); tt = t.Epoch32Time(); } - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access return tt; } return tt; @@ -115,9 +114,9 @@ void sync_rtctime(void) { float get_rtctemp(void) { // block i2c bus access - while (xSemaphoreTake(I2Caccess, DISPLAYREFRESH_MS) == pdTRUE) { + if (I2C_MUTEX_LOCK()) { RtcTemperature temp = Rtc.GetTemperature(); - xSemaphoreGive(I2Caccess); // release i2c bus access + I2C_MUTEX_UNLOCK(); // release i2c bus access return temp.AsFloatDegC(); } // while return 0;