switch to Recursive Mutex and non-blocking

This commit is contained in:
2024-12-13 22:29:48 +01:00
parent 8205253b5a
commit 1af962190e
6 changed files with 88 additions and 36 deletions

View File

@ -22,12 +22,12 @@ void setSafeState(void);
void initSafety(void)
{
xMutexAccessSafety = xSemaphoreCreateBinary();
xMutexAccessSafety = xSemaphoreCreateRecursiveMutex();
if (xMutexAccessSafety == NULL)
{
ESP_LOGE(TAG, "Unable to create mutex");
}
xSemaphoreGive(xMutexAccessSafety);
xSemaphoreGiveRecursive(xMutexAccessSafety);
BaseType_t taskCreated = xTaskCreate(
taskSafety, // Function to implement the task
@ -54,11 +54,11 @@ void taskSafety(void *pvParameters)
{
vTaskDelay(PERIODIC_INTERVAL * 1000U / portTICK_PERIOD_MS);
if (xSemaphoreTake(xMutexAccessSafety, portMAX_DELAY) == pdTRUE)
if (xSemaphoreTakeRecursive(xMutexAccessSafety, portMAX_DELAY) == pdTRUE)
{
checkSensorSanity();
xSemaphoreGive(xMutexAccessSafety);
xSemaphoreGiveRecursive(xMutexAccessSafety);
}
}
}
@ -128,7 +128,7 @@ void setSafeState(void)
void getSensorSanityStates(sSensorSanityCheck *pSensorSanityChecks)
{
if (xSemaphoreTake(xMutexAccessSafety, portMAX_DELAY) == pdTRUE)
if (xSemaphoreTakeRecursive(xMutexAccessSafety, pdMS_TO_TICKS(5000)) == pdTRUE)
{
for (size_t i = 0; i < NUMBER_OF_SENSOR_SANITY_CHECKS; i++)
{
@ -136,17 +136,26 @@ void getSensorSanityStates(sSensorSanityCheck *pSensorSanityChecks)
pSensorSanityChecks[i].status = sanityChecks[i].status;
strcpy(pSensorSanityChecks[i].name, sanityChecks[i].name);
}
xSemaphoreGive(xMutexAccessSafety);
xSemaphoreGiveRecursive(xMutexAccessSafety);
}
else
{
ESP_LOGE(TAG, "Unable to take mutex: getSensorSanityStates()");
}
}
eSafetyState getSafetyState(void)
{
eSafetyState state = SAFETY_NO_ERROR;
if (xSemaphoreTake(xMutexAccessSafety, portMAX_DELAY) == pdTRUE)
if (xSemaphoreTakeRecursive(xMutexAccessSafety, pdMS_TO_TICKS(5000)) == pdTRUE)
{
state = sSafetyState;
xSemaphoreGive(xMutexAccessSafety);
xSemaphoreGiveRecursive(xMutexAccessSafety);
}
else
{
state = SAFETY_INTERNAL_ERROR;
ESP_LOGE(TAG, "Unable to take mutex: getSafetyState()");
}
return state;
}