test_freertos_trace_utilities.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Test FreeRTOS trace facility functions. These following functions are enabled
  3. * when configUSE_TRACE_FACILITY is defined 1 in FreeRTOS.
  4. * Tasks: uxTaskGetTaskNumber(), uxTaskSetTaskNumber()
  5. * Queues: ucQueueGetQueueType(), vQueueSetQueueNumber(), uxQueueGetQueueNumber()
  6. * Event Groups: xEventGroupSetBitsFromISR(), xEventGroupClearBitsFromISR(), uxEventGroupGetNumber()
  7. *
  8. * Note: uxTaskGetSystemState() is tested in a separate unit test
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "freertos/semphr.h"
  16. #include "freertos/event_groups.h"
  17. #include "unity.h"
  18. #include "test_utils.h"
  19. #ifdef CONFIG_FREERTOS_USE_TRACE_FACILITY
  20. #define TSK_PRIORITY (UNITY_FREERTOS_PRIORITY + 1)
  21. #define NO_OF_CORES portNUM_PROCESSORS
  22. #define BIN_SEM_QUEUE_TYPE queueQUEUE_TYPE_BINARY_SEMAPHORE //Expected Queue Type
  23. static QueueHandle_t test_queues[NO_OF_CORES];
  24. static TaskHandle_t task_handles[NO_OF_CORES];
  25. void task_test_trace_utilities(void *arg)
  26. {
  27. int core = xPortGetCoreID();
  28. TaskHandle_t handle = xTaskGetCurrentTaskHandle();
  29. uint32_t id = (uint32_t)arg;
  30. vTaskSetTaskNumber(handle, (UBaseType_t)id); //cast and store id as task number
  31. vQueueSetQueueNumber(test_queues[core], id); //store id as queue number
  32. //Wait to start
  33. xSemaphoreTake(test_queues[core], portMAX_DELAY);
  34. //Tests on this core
  35. TEST_ASSERT(uxTaskGetTaskNumber(task_handles[core]) == (0x0F << (core)));
  36. TEST_ASSERT(uxQueueGetQueueNumber(test_queues[core]) == (0x0F << (core)));
  37. TEST_ASSERT(ucQueueGetQueueType(test_queues[core]) == BIN_SEM_QUEUE_TYPE)
  38. //Test on other core
  39. #ifndef CONFIG_FREERTOS_UNICORE
  40. TEST_ASSERT(uxTaskGetTaskNumber(task_handles[!core]) == (0x0F << (!core)));
  41. TEST_ASSERT(uxQueueGetQueueNumber(test_queues[!core]) == (0x0F << (!core)));
  42. TEST_ASSERT(ucQueueGetQueueType(test_queues[!core]) == BIN_SEM_QUEUE_TYPE)
  43. #endif
  44. xSemaphoreGive(test_queues[core]); //Signal done
  45. vTaskDelete(NULL);
  46. }
  47. TEST_CASE("Test freertos trace facility functions", "[freertos]")
  48. {
  49. for(int i = 0; i < NO_OF_CORES; i++){
  50. test_queues[i] = xSemaphoreCreateBinary(); //Create a queue as binary semaphore for each core
  51. xTaskCreatePinnedToCore(task_test_trace_utilities, "Test Task", 4096, (void *)(0x0F << i), TSK_PRIORITY, &task_handles[i], i);
  52. }
  53. vTaskDelay(10);
  54. //Start the tasks
  55. for(int i = NO_OF_CORES - 1; i >= 0; i--){
  56. xSemaphoreGive(test_queues[i]);
  57. }
  58. vTaskDelay(10); //Small delay to ensure semaphores are taken
  59. //Wait for done
  60. for(int i = 0; i < NO_OF_CORES; i++){
  61. xSemaphoreTake(test_queues[i], portMAX_DELAY);
  62. vSemaphoreDelete(test_queues[i]);
  63. }
  64. vTaskDelay(10); //Give time for idle task to clean up
  65. }
  66. #define MAX_TASKS 15
  67. #define TASKS_TO_CREATE 5
  68. static TaskHandle_t created_handles[TASKS_TO_CREATE];
  69. static TaskStatus_t *tsk_status_array;
  70. void created_task(void* arg)
  71. {
  72. while(1){
  73. vTaskDelay(100);
  74. }
  75. }
  76. TEST_CASE("Test freertos uxTaskGetSystemState", "[freertos]")
  77. {
  78. tsk_status_array = calloc(MAX_TASKS, sizeof(TaskStatus_t));
  79. for(int i = 0; i < TASKS_TO_CREATE; i++){
  80. xTaskCreatePinnedToCore(created_task, "Created Task", 1024, NULL, TSK_PRIORITY, &created_handles[i], 0);
  81. }
  82. //Get System states
  83. int no_of_tasks = uxTaskGetSystemState(tsk_status_array, MAX_TASKS, NULL);
  84. TEST_ASSERT((no_of_tasks > 0) && (no_of_tasks <= MAX_TASKS));
  85. //Check if get system state has got all created tasks
  86. bool not_found = false;
  87. for(int i = 0; i < TASKS_TO_CREATE; i++){
  88. bool found = false;
  89. for(int j = 0; j < MAX_TASKS; j++){
  90. if(tsk_status_array[j].xHandle == created_handles[i]){
  91. found = true;
  92. break;
  93. }
  94. }
  95. if(!found){
  96. not_found = true;
  97. break;
  98. }
  99. }
  100. TEST_ASSERT(not_found == false);
  101. //Cleanup
  102. for(int i = 0; i < TASKS_TO_CREATE; i++){
  103. vTaskDelete(created_handles[i]);
  104. }
  105. free(tsk_status_array);
  106. vTaskDelay(10);
  107. }
  108. #endif //CONFIG_FREERTOS_USE_TRACE_FACILITY