test_ipc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <stdio.h>
  2. #include "sdkconfig.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/semphr.h"
  6. #include "unity.h"
  7. #if !CONFIG_FREERTOS_UNICORE
  8. #include "esp_ipc.h"
  9. #endif
  10. #include "esp_log.h"
  11. #include "esp_rom_sys.h"
  12. #if !CONFIG_FREERTOS_UNICORE
  13. static void test_func_ipc_cb(void *arg)
  14. {
  15. vTaskDelay(50);
  16. int *val = (int *)arg;
  17. *val = 0xa5a5;
  18. }
  19. TEST_CASE("Test blocking IPC function call", "[ipc]")
  20. {
  21. int val = 0x5a5a;
  22. esp_ipc_call_blocking(!xPortGetCoreID(), test_func_ipc_cb, &val);
  23. TEST_ASSERT_EQUAL_HEX(val, 0xa5a5);
  24. }
  25. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  26. static volatile bool exit_flag;
  27. static void task1(void *sema)
  28. {
  29. ESP_LOGI("task1", "start");
  30. esp_rom_delay_us(3000000);
  31. vTaskDelay(1);
  32. while (exit_flag == false) {
  33. }
  34. ESP_LOGI("task1", "finish");
  35. vTaskDelete(NULL);
  36. }
  37. static UBaseType_t func_ipc_priority;
  38. static void test_func_ipc(void *sema)
  39. {
  40. esp_rom_delay_us(1000000 + xPortGetCoreID() * 100);
  41. func_ipc_priority = uxTaskPriorityGet(NULL);
  42. xSemaphoreGive(*(xSemaphoreHandle *)sema);
  43. esp_rom_printf("test_func_ipc: [%d, %d]\n", func_ipc_priority, xPortGetCoreID());
  44. }
  45. TEST_CASE("Test ipc_task works with the priority of the caller's task", "[ipc]")
  46. {
  47. UBaseType_t priority = 18;
  48. func_ipc_priority = 0;
  49. vTaskPrioritySet(NULL, priority);
  50. xSemaphoreHandle sema_ipc_done = xSemaphoreCreateBinary();
  51. exit_flag = false;
  52. xTaskCreatePinnedToCore(task1, "task1", 4096, NULL, priority + 2, NULL, 1);
  53. vTaskDelay(100 / portTICK_PERIOD_MS);
  54. ESP_LOGI("test", "Start IPC call in IPC_WAIT_FOR_START mode");
  55. esp_ipc_call(1, test_func_ipc, &sema_ipc_done);
  56. ESP_LOGI("test", "Waiting for IPC finish");
  57. xSemaphoreTake(sema_ipc_done, 4000 / portTICK_PERIOD_MS);
  58. ESP_LOGI("test", "Stop task1");
  59. exit_flag = true;
  60. xSemaphoreTake(sema_ipc_done, portMAX_DELAY);
  61. vSemaphoreDelete(sema_ipc_done);
  62. ESP_LOGI("test", "Check ipc_priority with priority caller's task. Should be the same");
  63. vTaskPrioritySet(NULL, 5);
  64. TEST_ASSERT_EQUAL(priority, func_ipc_priority);
  65. }
  66. static void test_func2_ipc(void *arg)
  67. {
  68. int callers_priority = *(int *)arg;
  69. esp_rom_delay_us(1000000 + xPortGetCoreID() * 100);
  70. UBaseType_t priority = uxTaskPriorityGet(NULL);
  71. esp_rom_printf("test_func2_ipc: [callers_priority = %d, priority = %d, cpu = %d]\n", callers_priority, priority, xPortGetCoreID());
  72. }
  73. static void task(void *sema)
  74. {
  75. int priority = uxTaskPriorityGet(NULL);
  76. ESP_LOGI("task", "start [priority = %d, cpu = %d]", priority, xPortGetCoreID());
  77. xSemaphoreTake(*(xSemaphoreHandle *)sema, portMAX_DELAY);
  78. esp_ipc_call_blocking(!xPortGetCoreID(), test_func2_ipc, &priority);
  79. xSemaphoreGive(*(xSemaphoreHandle *)sema);
  80. ESP_LOGI("task", "finish [priority = %d, cpu = %d]", priority, xPortGetCoreID());
  81. vTaskDelete(NULL);
  82. }
  83. TEST_CASE("Test multiple ipc_calls", "[ipc]")
  84. {
  85. const int max_tasks = 5;
  86. UBaseType_t priority = uxTaskPriorityGet(NULL);
  87. ESP_LOGI("test", "priority = %d, cpu = %d", priority, xPortGetCoreID());
  88. xSemaphoreHandle sema_ipc_done[max_tasks * portNUM_PROCESSORS];
  89. for (int task_num = 0; task_num < max_tasks; ++task_num) {
  90. ++priority;
  91. ESP_LOGI("test", "task prio = %d", priority);
  92. for (int cpu_num = 0; cpu_num < portNUM_PROCESSORS; ++cpu_num) {
  93. sema_ipc_done[task_num * 2 + cpu_num] = xSemaphoreCreateBinary();
  94. xTaskCreatePinnedToCore(task, "task", 4096, &sema_ipc_done[task_num * 2 + cpu_num], priority, NULL, cpu_num);
  95. }
  96. }
  97. for (int task_num = 0; task_num < max_tasks; ++task_num) {
  98. for (int cpu_num = 0; cpu_num < portNUM_PROCESSORS; ++cpu_num) {
  99. xSemaphoreGive(sema_ipc_done[task_num * 2 + cpu_num]);
  100. }
  101. }
  102. for (int task_num = 0; task_num < max_tasks; ++task_num) {
  103. for (int cpu_num = 0; cpu_num < portNUM_PROCESSORS; ++cpu_num) {
  104. xSemaphoreTake(sema_ipc_done[task_num * 2 + cpu_num], portMAX_DELAY);
  105. vSemaphoreDelete(sema_ipc_done[task_num * 2 + cpu_num]);
  106. }
  107. }
  108. }
  109. #endif /* CONFIG_ESP_IPC_USE_CALLERS_PRIORITY */
  110. #endif /* !CONFIG_FREERTOS_UNICORE */