test_freertos_debug_functions.c 3.7 KB

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