/ components / freertos / test / test_task_priorities.c
test_task_priorities.c
 1  /*
 2    Unit tests for FreeRTOS task priority get/set
 3  */
 4  
 5  #include <esp_types.h>
 6  #include <stdio.h>
 7  #include <strings.h>
 8  
 9  #include "freertos/FreeRTOS.h"
10  #include "freertos/task.h"
11  #include "unity.h"
12  #include "test_utils.h"
13  
14  
15  static void counter_task(void *param)
16  {
17      volatile uint32_t *counter = (volatile uint32_t *)param;
18      while (1) {
19          (*counter)++;
20      }
21  }
22  
23  
24  TEST_CASE("Get/Set Priorities", "[freertos]")
25  {
26      /* Two tasks per processor */
27      TaskHandle_t tasks[portNUM_PROCESSORS][2] = { 0 };
28      unsigned volatile counters[portNUM_PROCESSORS][2] = { 0 };
29  
30      TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY, uxTaskPriorityGet(NULL));
31  
32      /* create a matrix of counter tasks on each core */
33      for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
34          for (int task = 0; task < 2; task++) {
35              xTaskCreatePinnedToCore(counter_task, "count", 2048, (void *)&(counters[cpu][task]), UNITY_FREERTOS_PRIORITY - task, &(tasks[cpu][task]), cpu);
36          }
37      }
38  
39      /* check they were created with the expected priorities */
40      for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
41          for (int task = 0; task < 2; task++) {
42              TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY - task, uxTaskPriorityGet(tasks[cpu][task]));
43          }
44      }
45  
46      vTaskDelay(10);
47  
48      /* at this point, only the higher priority tasks (first index) should be counting */
49      for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
50          TEST_ASSERT_NOT_EQUAL(0, counters[cpu][0]);
51          TEST_ASSERT_EQUAL(0, counters[cpu][1]);
52      }
53  
54      /* swap priorities! */
55      for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
56          vTaskPrioritySet(tasks[cpu][0], UNITY_FREERTOS_PRIORITY - 1);
57          vTaskPrioritySet(tasks[cpu][1], UNITY_FREERTOS_PRIORITY);
58      }
59  
60      /* check priorities have swapped... */
61      for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
62          TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY -1, uxTaskPriorityGet(tasks[cpu][0]));
63          TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY, uxTaskPriorityGet(tasks[cpu][1]));
64      }
65  
66      /* check the tasks which are counting have also swapped now... */
67      for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
68          unsigned old_counters[2];
69          old_counters[0] = counters[cpu][0];
70          old_counters[1] = counters[cpu][1];
71          vTaskDelay(10);
72          TEST_ASSERT_EQUAL(old_counters[0], counters[cpu][0]);
73          TEST_ASSERT_NOT_EQUAL(old_counters[1], counters[cpu][1]);
74      }
75  
76      /* clean up */
77      for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
78          for (int task = 0; task < 2; task++) {
79              vTaskDelete(tasks[cpu][task]);
80          }
81      }
82  }