i2c mutex control sanitized
This commit is contained in:
parent
44b821f2a5
commit
79a963b34f
@ -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
|
||||
|
@ -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,9 +104,9 @@ void bme_loop(void *pvParameters) {
|
||||
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
|
||||
|
||||
#ifdef HAS_BME
|
||||
while (1) {
|
||||
// block i2c bus access
|
||||
while (xSemaphoreTake(I2Caccess, portMAX_DELAY) == pdTRUE) {
|
||||
|
||||
if (I2C_MUTEX_LOCK()) {
|
||||
if (iaqSensor.run()) { // If new data is available
|
||||
bme_status.raw_temperature = iaqSensor.rawTemperature;
|
||||
bme_status.raw_humidity = iaqSensor.rawHumidity;
|
||||
@ -121,9 +119,9 @@ void bme_loop(void *pvParameters) {
|
||||
bme_status.gas = iaqSensor.gasResistance;
|
||||
updateState();
|
||||
}
|
||||
xSemaphoreGive(I2Caccess); // release i2c bus access
|
||||
|
||||
} // while
|
||||
I2C_MUTEX_UNLOCK();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
ESP_LOGE(TAG, "BME task ended");
|
||||
vTaskDelete(BmeTask); // should never be reached
|
||||
|
@ -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()
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user