Skip to content

Commit

Permalink
usddeck: Add graceful shutdown callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasdn committed Sep 7, 2021
1 parent 1ce7cd9 commit 181afcc
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/deck/drivers/src/usddeck.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "sensors.h"
#include "debug.h"
#include "led.h"
#include "pm.h"

#include "statsCnt.h"
#include "log.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -788,7 +803,7 @@ static void usdWriteTask(void* prm)

vTaskDelay(M2T(50));

while (true) {
while (!in_shutdown) {
vTaskSuspend(NULL);
if (enableLogging) {
// reset stats
Expand Down Expand Up @@ -997,6 +1012,11 @@ static void usdWriteTask(void* prm)
}
}
}

if (in_shutdown) {
xTaskNotifyGive(xHandleLogTask);
}

/* something went wrong */
xHandleWriteTask = 0;
vTaskDelete(NULL);
Expand Down

0 comments on commit 181afcc

Please sign in to comment.