Last active
November 28, 2022 00:35
-
-
Save PeterBorisenko/8255471f0e1c3a08fc6b3fb478bb8240 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file task_watchdog.c | |
* | |
* @date Nov 27, 2022 | |
* @author peter | |
* @brief | |
* | |
* @license | |
* | |
*/ | |
#include <stdint.h> | |
#include "app_result.h" | |
#define TASK_NUMBER_MAX 16 | |
static struct { | |
rtems_id id; | |
uint32_t votes; | |
} registry[TASK_NUMBER_MAX]= {0}; | |
static uint8_t registrants= 0; | |
// Call at the start of init task | |
void TaskWatchdog_Init(void) { | |
hal_wdgInit(); | |
} | |
// Call when task is created | |
AppResult_t TaskWatchdog_Register(rtems_id taskID, uint8_t * idx) { | |
AppResult_t res= RESULT_ERR_WRONG_ARGUMENT; | |
if (taskID != RTEMS_ID_NONE && idx) { | |
res= RESULT_ERR_INSUFFICENT_RESOURCES; | |
if (registrants < TASK_NUMBER_MAX) { | |
registry[registrants].id= taskID; | |
*idx= registrants; | |
registrants+= 1; | |
res= RESULT_OK; | |
} | |
} | |
return res; | |
} | |
// Call in the loop in a task to be monitored | |
AppResult_t TaskWatchdog_Vote(uint8_t idx) { | |
AppResult_t res= RESULT_ERR_WRONG_ARGUMENT; | |
if (idx < TASK_NUMBER_MAX) { | |
registry[idx].votes+= 1; | |
res= RESULT_OK; | |
} | |
return res; | |
} | |
// Call in the background task | |
void TaskWatchdog_Handle(void) { | |
uint8_t votes= 0; | |
for (uint8_t i= 0; i < registrants; ++i) { | |
if (registry[i].votes > 0) { | |
votes+= 1; | |
} | |
} | |
if (votes == registrants) { | |
hal_wdgClear(); | |
for (uint8_t i= 0; i < registrants; ++i) { | |
votes= 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment