1 #include <esp_types.h>
2 #include <stdio.h>
3 #include "freertos/FreeRTOS.h"
4 #include "freertos/task.h"
5 #include "freertos/semphr.h"
6 #include "freertos/queue.h"
7 #include "esp_intr_alloc.h"
8 #include "unity.h"
9 #include "soc/cpu.h"
10 #include "test_utils.h"
11
12 #define NUMBER_OF_ITERATIONS 10
13
14 typedef struct {
15 SemaphoreHandle_t end_sema;
16 uint32_t before_sched;
17 uint32_t cycles_to_sched;
18 TaskHandle_t t1_handle;
19 } test_context_t;
20
test_task_1(void * arg)21 static void test_task_1(void *arg) {
22 test_context_t *context = (test_context_t *)arg;
23
24 for( ;; ) {
25 context->before_sched = portGET_RUN_TIME_COUNTER_VALUE();
26 vPortYield();
27 }
28
29 vTaskDelete(NULL);
30 }
31
test_task_2(void * arg)32 static void test_task_2(void *arg) {
33 test_context_t *context = (test_context_t *)arg;
34 uint64_t accumulator = 0;
35
36 vTaskPrioritySet(NULL, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
37 vTaskPrioritySet(context->t1_handle, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
38 vPortYield();
39
40 for(int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
41 accumulator += (portGET_RUN_TIME_COUNTER_VALUE() - context->before_sched);
42 vPortYield();
43 }
44
45 context->cycles_to_sched = accumulator / NUMBER_OF_ITERATIONS;
46 vTaskDelete(context->t1_handle);
47 xSemaphoreGive(context->end_sema);
48 vTaskDelete(NULL);
49 }
50
51 TEST_CASE("scheduling time test", "[freertos]")
52 {
53 test_context_t context;
54
55 context.end_sema = xSemaphoreCreateBinary();
56 TEST_ASSERT(context.end_sema != NULL);
57
58 #if !CONFIG_FREERTOS_UNICORE
59 xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, 1, &context.t1_handle,1);
60 xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, 1, NULL,1);
61 #else
62 xTaskCreatePinnedToCore(test_task_1, "test1" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, &context.t1_handle,0);
63 xTaskCreatePinnedToCore(test_task_2, "test2" , 4096, &context, CONFIG_UNITY_FREERTOS_PRIORITY - 1, NULL,0);
64 #endif
65
66 BaseType_t result = xSemaphoreTake(context.end_sema, portMAX_DELAY);
67 TEST_ASSERT_EQUAL_HEX32(pdTRUE, result);
68 TEST_PERFORMANCE_LESS_THAN(SCHEDULING_TIME , "%d cycles" ,context.cycles_to_sched);
69 }
70