test_freertos_debug_functions.c 3.7 KB

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