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