1 /*
2  Test of FreeRTOS "legacy" hook functions, and delete hook
3 
4  Only compiled in if the relevant options are enabled.
5 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "freertos/FreeRTOS.h"
9 #include "freertos/task.h"
10 #include <freertos/semphr.h>
11 #include "driver/timer.h"
12 #include "unity.h"
13 #include "test_utils.h"
14 
15 #if CONFIG_FREERTOS_LEGACY_HOOKS
16 
17 static volatile unsigned idle_count;
18 
vApplicationIdleHook(void)19 void vApplicationIdleHook(void)
20 {
21     idle_count++;
22 }
23 
24 TEST_CASE("legacy idle hook is correctly called based on config", "[freertos]")
25 {
26     idle_count = 0;
27     vTaskDelay(10);
28     TEST_ASSERT_NOT_EQUAL(0, idle_count); // The legacy idle hook should be called at least once
29 }
30 
31 static volatile unsigned tick_count;
32 
vApplicationTickHook(void)33 void IRAM_ATTR vApplicationTickHook(void)
34 {
35     tick_count++;
36 }
37 
38 TEST_CASE("legacy tick hook is called based on config", "[freertos]")
39 {
40     unsigned before = xTaskGetTickCount();
41     const unsigned SLEEP_FOR = 20;
42     tick_count = before;
43     vTaskDelay(SLEEP_FOR);
44     TEST_ASSERT_UINT32_WITHIN_MESSAGE(3 * portNUM_PROCESSORS, before + SLEEP_FOR * portNUM_PROCESSORS, tick_count,
45                                       "The legacy tick hook should have been called approx 1 time per tick per CPU");
46 }
47 
48 #endif // CONFIG_FREERTOS_LEGACY_HOOKS
49 
50 #if CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP
51 
52 static volatile void *deleted_tcb;
53 
taskDeletesItself(void * ignored)54 static void taskDeletesItself(void *ignored)
55 {
56     vTaskDelete(NULL);
57 }
58 
vPortCleanUpTCB(void * pxTCB)59 void vPortCleanUpTCB(void *pxTCB)
60 {
61     deleted_tcb = pxTCB;
62 }
63 
64 TEST_CASE("static task cleanup hook is called based on config", "[freertos]")
65 {
66     for(int i = 0; i < portNUM_PROCESSORS; i++) {
67         printf("Creating task CPU %d\n", i);
68         TaskHandle_t new_task = NULL;
69         deleted_tcb = NULL;
70         xTaskCreatePinnedToCore(taskDeletesItself, "delete", 2048, NULL, UNITY_FREERTOS_PRIORITY, &new_task, i);
71         vTaskDelay(5);
72         TEST_ASSERT_EQUAL_PTR(deleted_tcb, new_task); // TCB & TaskHandle are the same in FreeRTOS
73     }
74 }
75 
76 #endif // CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP
77