From 181afcc9bc0200dccb5533ed7374b0ec7db23e47 Mon Sep 17 00:00:00 2001 From: Jonas Danielsson Date: Tue, 7 Sep 2021 07:27:43 +0200 Subject: [PATCH] usddeck: Add graceful shutdown callback --- src/deck/drivers/src/usddeck.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/deck/drivers/src/usddeck.c b/src/deck/drivers/src/usddeck.c index 6f1c7f2ca4..a0c8a66642 100644 --- a/src/deck/drivers/src/usddeck.c +++ b/src/deck/drivers/src/usddeck.c @@ -53,6 +53,7 @@ #include "sensors.h" #include "debug.h" #include "led.h" +#include "pm.h" #include "statsCnt.h" #include "log.h" @@ -92,6 +93,10 @@ #define FIXED_FREQUENCY_EVENT_ID (0xFFFF) #define FIXED_FREQUENCY_EVENT_NAME "fixedFrequency" + +/* set to true when graceful shutdown is triggered */ +static volatile bool in_shutdown; + typedef struct usdLogEventConfig_s { uint16_t eventId; uint8_t numVars; @@ -228,6 +233,7 @@ static SemaphoreHandle_t logFileMutex; static SemaphoreHandle_t logBufferMutex; static ringBuffer_t logBuffer; +static TaskHandle_t xHandleLogTask; static TaskHandle_t xHandleWriteTask; static bool enableLogging; @@ -447,7 +453,7 @@ static void usdInit(DeckInfo *info) /* create usd-log task */ xTaskCreate(usdLogTask, USDLOG_TASK_NAME, USDLOG_TASK_STACKSIZE, NULL, - USDLOG_TASK_PRI, NULL); + USDLOG_TASK_PRI, &xHandleLogTask); } else { DEBUG_PRINT("mount SD-Card [FAIL].\n"); } @@ -521,6 +527,13 @@ static void usddeckEventtriggerCallback(const eventtrigger *event) } } +static void usdGracefulShutdownCallback() +{ + uint32_t timeout = 10; /* ms */ + in_shutdown = true; + ulTaskNotifyTake(pdTRUE, timeout / portTICK_PERIOD_MS); +} + static void usdLogTask(void* prm) { TickType_t lastWakeTime = xTaskGetTickCount(); @@ -682,6 +695,8 @@ static void usdLogTask(void* prm) xHandleWriteTask = 0; enableLogging = usdLogConfig.enableOnStartup; // enable logging if desired + pmRegisterGracefulShutdownCallback(usdGracefulShutdownCallback); + /* create usd-write task */ xTaskCreate(usdWriteTask, USDWRITE_TASK_NAME, USDWRITE_TASK_STACKSIZE, 0, @@ -788,7 +803,7 @@ static void usdWriteTask(void* prm) vTaskDelay(M2T(50)); - while (true) { + while (!in_shutdown) { vTaskSuspend(NULL); if (enableLogging) { // reset stats @@ -997,6 +1012,11 @@ static void usdWriteTask(void* prm) } } } + + if (in_shutdown) { + xTaskNotifyGive(xHandleLogTask); + } + /* something went wrong */ xHandleWriteTask = 0; vTaskDelete(NULL);