watchdog-timer-checkpoint-freertos  2.1.0
watchdog-timer-checkpoint-freertos
watchdog_timer_checkpoint_freertos.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2022 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
17 /* Includes ------------------------------------------------------------------*/
18 #include <stdio.h>
21 
22 #include "FreeRTOS.h"
23 #include "timers.h"
24 
25 #include "LLWATCHDOG_TIMER_configuration.h"
26 #include "LLWATCHDOG_TIMER_impl.h"
27 
28 
29 #ifdef __cplusplus
30  extern "C" {
31 #endif
32 
33 /* Macros and Defines --------------------------------------------------------*/
34 
42 #if WATCHDOG_TIMER_FREERTOS_CONFIGURATION_VERSION != 1
43  #error "Version of the configuration file watchdog_timer_freertos_configuration.h is not compatible with this implementation."
44 #endif
45 
46 
47 /* Static variables -------------------------------------------------------------------*/
48 static xTimerHandle wtd_rtos_timer = NULL;
49 
53 static volatile int32_t checkpoint_id_rtos_scheduler = WATCHDOG_TIMER_ERROR;
54 
55 /* Internal functions ------------------------------------------------*/
56 
62 static void watchdog_timer_freertos_check_callback(xTimerHandle pxTimer);
63 
64 
65 /* API functions ----------------------------------------------------------*/
66 
68  if (NULL != wtd_rtos_timer && WATCHDOG_TIMER_ERROR != checkpoint_id_rtos_scheduler) {
69  LLWATCHDOG_TIMER_DEBUG_TRACE("Watchdog FreeRTOS scheduler checkpoint already started.\n");
71  }
72 
73  if (NULL == wtd_rtos_timer && WATCHDOG_TIMER_ERROR == checkpoint_id_rtos_scheduler){
74  wtd_rtos_timer = xTimerCreate(
75  "watchdog_timer_rtos_check", /* name */
76  pdMS_TO_TICKS(FREERTOS_TIMER_PERIOD_MS), /* period in ms */
77  pdTRUE, /* set auto reload */
78  (void*)0, /* timer ID = 0 */
79  watchdog_timer_freertos_check_callback); /* callback function pointer */
80 
81  if (NULL == wtd_rtos_timer) {
82  LLWATCHDOG_TIMER_DEBUG_TRACE("Watchdog FreeRTOS SW timer creation failed\n");
84  }
85 
86  if(pdFAIL == xTimerStart( wtd_rtos_timer, 0 )){
87  LLWATCHDOG_TIMER_DEBUG_TRACE("Fail to start Watchdog FreeRTOS SW timer\n");
88  xTimerDelete( wtd_rtos_timer, 0 );
89  wtd_rtos_timer = NULL;
91  }else{
92  checkpoint_id_rtos_scheduler = LLWATCHDOG_TIMER_IMPL_registerCheckpoint();
93  if(WATCHDOG_TIMER_ERROR == checkpoint_id_rtos_scheduler){
94  LLWATCHDOG_TIMER_DEBUG_TRACE("Fail to register a checkpoint for Watchdog FreeRTOS scheduler activity\n");
95  xTimerDelete( wtd_rtos_timer, 0 );
96  wtd_rtos_timer = NULL;
98  }
100  }
101  }
102 
103  // Here, the timer is null (was deleted in stop function) and the checkpoint is still valid (not unregistered)
104  // This may be caused by a failure of a previous call of watchdog_timer_freertos_scheduler_checkpoint_stop() function.
105  // We cannot restart the scheduler checkpoint when it was not correctly stopped.
106  LLWATCHDOG_TIMER_DEBUG_TRACE("Cannot restart the scheduler checkpoint when it was not correctly stopped.\n");
108 }
109 
111  if(NULL != wtd_rtos_timer){
112  if(pdFAIL == xTimerStop( wtd_rtos_timer, 0 )){
113  LLWATCHDOG_TIMER_DEBUG_TRACE("Watchdog FreeRTOS fails to stop the SW timer\n");
115  }else{
116  xTimerDelete( wtd_rtos_timer, 0 );
117  wtd_rtos_timer = NULL;
118  }
119  }
120  if(WATCHDOG_TIMER_ERROR != checkpoint_id_rtos_scheduler){
121  if(WATCHDOG_TIMER_ERROR == LLWATCHDOG_TIMER_IMPL_unregisterCheckpoint(checkpoint_id_rtos_scheduler)){
122  LLWATCHDOG_TIMER_DEBUG_TRACE("Fail to unregister the FreeRTOS scheduler checkpoint!\n");
124  }
125  checkpoint_id_rtos_scheduler = WATCHDOG_TIMER_ERROR;
126  }
127 
128  return CHECKPOINT_FREERTOS_OK;
129 }
130 
132 #if (configUSE_TRACE_FACILITY == 1) && (configUSE_STATS_FORMATTING_FUNCTIONS == 1)
133  char* tasks_log_buffer;
134  uint16_t number_of_tasks = 0;
135  /* Take a snapshot of the number of tasks in case it changes while this
136  function is executing. */
137  number_of_tasks = uxTaskGetNumberOfTasks();
138 
139  if(0 < number_of_tasks){
140  /* Allocate a 50 bytes for each task to print data. */
141  tasks_log_buffer = pvPortMalloc(number_of_tasks * 50 * sizeof(char));
142  vTaskList(tasks_log_buffer);
143  printf("******************************************\n");
144  printf("Task \t State Prio Stack Num\n");
145  printf("******************************************\n");
146  printf("%s \n", tasks_log_buffer);
147  printf("******************************************\n");
148  printf("X-runnning, B–Blocked, R–Ready, D–Deleted, S–Suspended\n");
149  /* The array is no longer needed, free the memory it consumes. */
150  vPortFree(tasks_log_buffer);
151  } else {
152  LLWATCHDOG_TIMER_DEBUG_TRACE("No tasks retrieved with uxTaskGetNumberOfTasks()\n");
153  }
154 #endif
155 }
156 
157 /* Internal functions definition ---------------------------------------------*/
158 
159 static void watchdog_timer_freertos_check_callback(xTimerHandle pxTimer){
160  LLWATCHDOG_TIMER_DEBUG_TRACE("Watchdog FreeRTOS SW timer callback: attest the FreeRTOS scheduler activity with the checkpoint ID %d", checkpoint_id_rtos_scheduler);
161  if(WATCHDOG_TIMER_ERROR == LLWATCHDOG_TIMER_IMPL_checkpoint(checkpoint_id_rtos_scheduler)){
162  LLWATCHDOG_TIMER_DEBUG_TRACE("Watchdog FreeRTOS fail to attest the scheduler activity\n");
163  }
164 }
165 
166 
167 #ifdef __cplusplus
168  }
169 #endif
170 
#define CHECKPOINT_FREERTOS_OK
Error codes constants.
#define CHECKPOINT_FREERTOS_ERROR
#define FREERTOS_TIMER_PERIOD_MS
Period (in milliseconds) of the FreeRTOS timer that will attest the FreeRTOS scheduler activity...
WATCHDOG TIMER FreeRTOS scheduler checkpoint CCO header file.
int32_t watchdog_timer_freertos_scheduler_checkpoint_stop(void)
Stops the FreeRTOS scheduler checkpoint.
void watchdog_timer_freertos_print_tasks_state(void)
Prints the current FreeRTOS tasks state.
int32_t watchdog_timer_freertos_scheduler_checkpoint_start(void)
Starts the FreeRTOS scheduler checkpoint.
WATCHDOG TIMER FreeRTOS CCO configuration file.