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 8, 2021
1 parent 1ce7cd9 commit e82ffc4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
25 changes: 24 additions & 1 deletion 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 = false;

typedef struct usdLogEventConfig_s {
uint16_t eventId;
uint8_t numVars;
Expand Down Expand Up @@ -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(); }
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -788,7 +806,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 +1015,11 @@ static void usdWriteTask(void* prm)
}
}
}

if (in_shutdown) {
xSemaphoreGive(shutdownMutex);
}

/* something went wrong */
xHandleWriteTask = 0;
vTaskDelete(NULL);
Expand Down
5 changes: 3 additions & 2 deletions src/hal/src/pm_stm32f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "sound.h"
#include "deck.h"
#include "static_mem.h"
#include "worker.h"

typedef struct _PmSyslinkInfo
{
Expand Down Expand Up @@ -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];

/*
Expand Down Expand Up @@ -249,7 +250,7 @@ void pmSyslinkUpdate(SyslinkPacket *slp)
temp = pmSyslinkInfo.temp;
#endif
} else if (slp->type == SYSLINK_PM_SHUTDOWN_REQUEST) {
pmGracefulShutdown();
workerSchedule(pmGracefulShutdown, NULL);
}
}

Expand Down

0 comments on commit e82ffc4

Please sign in to comment.