test_freertos_debug_functions.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * Test FreeRTOS debug functions and utilities.
  8. * - Queue registry functions vQueueAddToRegistry(), vQueueUnregisterQueue(),
  9. * and pcQueueGetName(backported)
  10. *
  11. *
  12. */
  13. #include <stdio.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/queue.h"
  18. #include "unity.h"
  19. #include "test_utils.h"
  20. #if (CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE > 0)
  21. #define NO_OF_QUEUES_PER_CORE ((int)((CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE - 3)/portNUM_PROCESSORS)) //Save space for some preallocated tasks
  22. #define NO_OF_QUEUES_TOTAL (NO_OF_QUEUES_PER_CORE * portNUM_PROCESSORS)
  23. #define QUEUE_NAME_MAX_LENGTH 30
  24. static SemaphoreHandle_t start_sem[portNUM_PROCESSORS];
  25. static SemaphoreHandle_t done_sem = NULL;
  26. static char *names[NO_OF_QUEUES_TOTAL];
  27. static QueueHandle_t handles[NO_OF_QUEUES_TOTAL];
  28. void test_queue_registry_task(void *arg)
  29. {
  30. int core = xPortGetCoreID();
  31. int offset = core * NO_OF_QUEUES_PER_CORE;
  32. //Create queues and accompanying queue names
  33. for(int i = 0; i < NO_OF_QUEUES_PER_CORE; i++){
  34. handles[i + offset] = xQueueCreate(1,1); //Create queues
  35. names[i + offset] = calloc(QUEUE_NAME_MAX_LENGTH, sizeof(char));
  36. sprintf(names[i + offset], "Queue%d%d", core, i);
  37. }
  38. xSemaphoreTake(start_sem[core], portMAX_DELAY); //Wait for start vQueueAddToRegistry()
  39. for(int i = 0; i < NO_OF_QUEUES_PER_CORE; i++){
  40. vQueueAddToRegistry(handles[i + offset] , names[i + offset]); //Register queues to queue registry
  41. }
  42. xSemaphoreGive(done_sem); //Signal that vQueueAddToRegistry() has completed
  43. vTaskDelay(1);
  44. xSemaphoreTake(start_sem[core], portMAX_DELAY); //Wait to start vQueueUnregisterQueue()
  45. for(int i = 0; i < NO_OF_QUEUES_PER_CORE; i++){
  46. vQueueDelete(handles[i + offset]); //Internally calls vQueueUnregisterQueue
  47. }
  48. xSemaphoreGive(done_sem); //Signal done
  49. vTaskDelete(NULL); //Delete self
  50. }
  51. TEST_CASE("Test FreeRTOS Queue Registry", "[freertos]")
  52. {
  53. //Create synchronization semaphores and tasks to test queue registry
  54. done_sem = xSemaphoreCreateCounting(portNUM_PROCESSORS, 0);
  55. for(int i = 0; i < portNUM_PROCESSORS; i++){
  56. start_sem[i] = xSemaphoreCreateBinary();
  57. xTaskCreatePinnedToCore(test_queue_registry_task, "testing task", 4096, NULL, UNITY_FREERTOS_PRIORITY+1, NULL, i);
  58. }
  59. portDISABLE_INTERRUPTS();
  60. for(int i = 0; i < portNUM_PROCESSORS; i++){
  61. xSemaphoreGive(start_sem[i]); //Trigger start
  62. }
  63. portENABLE_INTERRUPTS();
  64. for(int i = 0; i < portNUM_PROCESSORS; i++){
  65. xSemaphoreTake(done_sem, portMAX_DELAY); //Wait for tasks to complete vQueueAddToRegistry
  66. }
  67. for(int i = 0; i < NO_OF_QUEUES_TOTAL; i++){
  68. const char *addr = pcQueueGetName(handles[i]);
  69. TEST_ASSERT(addr == names[i]) //Check vQueueAddToRegistry was successful
  70. }
  71. portDISABLE_INTERRUPTS();
  72. for(int i = 0; i < portNUM_PROCESSORS; i++){
  73. xSemaphoreGive(start_sem[i]); //Trigger start
  74. }
  75. portENABLE_INTERRUPTS();
  76. for(int i = 0; i < portNUM_PROCESSORS; i++){
  77. xSemaphoreTake(done_sem, portMAX_DELAY); //Wait for tasks to complete vQueueUnregisterQueue
  78. }
  79. for(int i = 0; i < NO_OF_QUEUES_TOTAL; i++){
  80. const char *addr = pcQueueGetName(handles[i]);
  81. TEST_ASSERT(addr == NULL) //Check vQueueUnregisterQueue was successful
  82. handles[i] = NULL;
  83. }
  84. //Cleanup
  85. for(int i = 0; i < NO_OF_QUEUES_TOTAL; i++){
  86. free(names[i]);
  87. names[i] = NULL;
  88. }
  89. for(int i = 0; i < portNUM_PROCESSORS; i++){
  90. vSemaphoreDelete(start_sem[i]);
  91. start_sem[i] = NULL;
  92. }
  93. vSemaphoreDelete(done_sem);
  94. done_sem = NULL;
  95. }
  96. #endif //(CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE > 0)