test_ipc.c 4.1 KB

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