diff --git a/src/deck/drivers/src/usddeck.c b/src/deck/drivers/src/usddeck.c index 6f1c7f2ca4..978a591f76 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 = false; + typedef struct usdLogEventConfig_s { uint16_t eventId; uint8_t numVars; @@ -237,6 +242,7 @@ static crc32Context_t crcContext; static xTimerHandle timer; static void usdTimer(xTimerHandle timer); +static SemaphoreHandle_t shutdownMutex; // Handling from the memory module static uint32_t handleMemGetSize(void) { return usddeckFileSize(); } @@ -440,6 +446,8 @@ static void usdInit(DeckInfo *info) logFileMutex = xSemaphoreCreateMutex(); logBufferMutex = xSemaphoreCreateMutex(); + shutdownMutex = xSemaphoreCreateMutex(); + /* try to mount drives before creating the tasks */ if (f_mount(&FatFs, "", 1) == FR_OK) { DEBUG_PRINT("mount SD-Card [OK].\n"); @@ -521,6 +529,14 @@ static void usddeckEventtriggerCallback(const eventtrigger *event) } } +static void usdGracefulShutdownCallback() +{ + uint32_t timeout = 15; /* ms */ + in_shutdown = true; + vTaskResume(xHandleWriteTask); + xSemaphoreTake(shutdownMutex, M2T(timeout)); +} + static void usdLogTask(void* prm) { TickType_t lastWakeTime = xTaskGetTickCount(); @@ -682,6 +698,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 +806,7 @@ static void usdWriteTask(void* prm) vTaskDelay(M2T(50)); - while (true) { + while (!in_shutdown) { vTaskSuspend(NULL); if (enableLogging) { // reset stats @@ -997,6 +1015,11 @@ static void usdWriteTask(void* prm) } } } + + if (in_shutdown) { + xSemaphoreGive(shutdownMutex); + } + /* something went wrong */ xHandleWriteTask = 0; vTaskDelete(NULL); diff --git a/src/hal/src/pm_stm32f4.c b/src/hal/src/pm_stm32f4.c index 43db699fbd..7998796db9 100644 --- a/src/hal/src/pm_stm32f4.c +++ b/src/hal/src/pm_stm32f4.c @@ -41,6 +41,7 @@ #include "sound.h" #include "deck.h" #include "static_mem.h" +#include "worker.h" typedef struct _PmSyslinkInfo { @@ -200,7 +201,7 @@ float pmGetBatteryVoltageMax(void) * GRACEFUL_SHUTDOWN_MAX_CALLBACKS number of callbacks to be registred. */ #define GRACEFUL_SHUTDOWN_MAX_CALLBACKS 5 -static uint8_t graceful_shutdown_callbacks_index; +static int graceful_shutdown_callbacks_index; static graceful_shutdown_callback_t graceful_shutdown_callbacks[GRACEFUL_SHUTDOWN_MAX_CALLBACKS]; /* @@ -249,7 +250,7 @@ void pmSyslinkUpdate(SyslinkPacket *slp) temp = pmSyslinkInfo.temp; #endif } else if (slp->type == SYSLINK_PM_SHUTDOWN_REQUEST) { - pmGracefulShutdown(); + workerSchedule(pmGracefulShutdown, NULL); } }