1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "freertos/FreeRTOS.h"
4 #include "freertos/task.h"
5 #include "freertos/semphr.h"
6 #include "unity.h"
7 #include "test_utils.h"
8 
9 #define TSK_PRIORITY    (UNITY_FREERTOS_PRIORITY + 1)
10 
11 static TaskHandle_t blocked_task_handle;
12 static TaskHandle_t suspended_task_handle;
13 static SemaphoreHandle_t done_sem;
14 
test_task_get_state(void * arg)15 void test_task_get_state(void* arg)
16 {
17     //Current task should return eRunning
18     TEST_ASSERT(eTaskGetState(xTaskGetCurrentTaskHandle()) == eRunning);
19     //Idle task of current core should return eReady
20     TEST_ASSERT(eTaskGetState(xTaskGetIdleTaskHandle()) == eReady);
21     //Blocked Task should return eBlocked
22     TEST_ASSERT(eTaskGetState(blocked_task_handle) == eBlocked);
23     //Suspended Task should return eSuspended
24     TEST_ASSERT(eTaskGetState(suspended_task_handle) == eSuspended);
25 
26     xSemaphoreGive(done_sem);
27     vTaskDelete(NULL);
28 }
29 
blocked_task(void * arg)30 void blocked_task(void *arg)
31 {
32     uint32_t notify_value;
33 
34     while(1){
35         xTaskNotifyWait(0, 0xFFFFFFFF, &notify_value, portMAX_DELAY);
36     }
37 }
38 
suspended_task(void * arg)39 void suspended_task(void *arg)
40 {
41     while(1){
42         vTaskSuspend(NULL);
43     }
44 }
45 
46 TEST_CASE("Test eTaskGetState", "[freertos]")
47 {
48     done_sem = xQueueCreateCountingSemaphore(portNUM_PROCESSORS, 0);
49     //Create blocked and suspended task
50     xTaskCreatePinnedToCore(blocked_task, "Blocked task", 1024, NULL, TSK_PRIORITY, &blocked_task_handle, tskNO_AFFINITY);
51     xTaskCreatePinnedToCore(suspended_task, "Suspended task", 1024, NULL, TSK_PRIORITY, &suspended_task_handle, tskNO_AFFINITY);
52     //Create testing task
53     for(int i = 0; i < portNUM_PROCESSORS; i++){
54         xTaskCreatePinnedToCore(test_task_get_state, "Test task", 1024, NULL, TSK_PRIORITY, NULL, i);
55     }
56 
57     //Wait until done
58     for(int i = 0; i < portNUM_PROCESSORS; i++){
59         xSemaphoreTake(done_sem, portMAX_DELAY);
60     }
61     //Clean up blocked and suspended tasks
62     vTaskDelete(blocked_task_handle);
63     blocked_task_handle = NULL;
64     vTaskDelete(suspended_task_handle);
65     suspended_task_handle = NULL;
66     vSemaphoreDelete(done_sem);
67 
68     vTaskDelay(10);     //Give time for idle to actually delete the tasks
69 }
70