test_ipc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include "sdkconfig.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/semphr.h"
  11. #include "unity.h"
  12. #if !CONFIG_FREERTOS_UNICORE
  13. #include "esp_ipc.h"
  14. #endif
  15. #include "esp_log.h"
  16. #include "esp_rom_sys.h"
  17. #if !CONFIG_FREERTOS_UNICORE
  18. static void test_func_ipc_cb(void *arg)
  19. {
  20. vTaskDelay(50);
  21. int *val = (int *)arg;
  22. *val = 0xa5a5;
  23. }
  24. TEST_CASE("Test blocking IPC function call", "[ipc]")
  25. {
  26. int val = 0x5a5a;
  27. esp_ipc_call_blocking(!xPortGetCoreID(), test_func_ipc_cb, &val);
  28. TEST_ASSERT_EQUAL_HEX(val, 0xa5a5);
  29. }
  30. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  31. static volatile bool exit_flag;
  32. static void task1(void *sema)
  33. {
  34. ESP_LOGI("task1", "start");
  35. esp_rom_delay_us(3000000);
  36. vTaskDelay(1);
  37. while (exit_flag == false) {
  38. }
  39. ESP_LOGI("task1", "finish");
  40. vTaskDelete(NULL);
  41. }
  42. static UBaseType_t func_ipc_priority;
  43. static void test_func_ipc(void *sema)
  44. {
  45. esp_rom_delay_us(1000000 + xPortGetCoreID() * 100);
  46. func_ipc_priority = uxTaskPriorityGet(NULL);
  47. xSemaphoreGive(*(SemaphoreHandle_t *)sema);
  48. esp_rom_printf("test_func_ipc: [%d, %d]\n", func_ipc_priority, xPortGetCoreID());
  49. }
  50. TEST_CASE("Test ipc_task works with the priority of the caller's task", "[ipc]")
  51. {
  52. UBaseType_t priority = 18;
  53. func_ipc_priority = 0;
  54. vTaskPrioritySet(NULL, priority);
  55. SemaphoreHandle_t sema_ipc_done = xSemaphoreCreateBinary();
  56. exit_flag = false;
  57. xTaskCreatePinnedToCore(task1, "task1", 4096, NULL, priority + 2, NULL, 1);
  58. vTaskDelay(100 / portTICK_PERIOD_MS);
  59. ESP_LOGI("test", "Start IPC call in IPC_WAIT_FOR_START mode");
  60. esp_ipc_call(1, test_func_ipc, &sema_ipc_done);
  61. ESP_LOGI("test", "Waiting for IPC finish");
  62. xSemaphoreTake(sema_ipc_done, 4000 / portTICK_PERIOD_MS);
  63. ESP_LOGI("test", "Stop task1");
  64. exit_flag = true;
  65. xSemaphoreTake(sema_ipc_done, portMAX_DELAY);
  66. vSemaphoreDelete(sema_ipc_done);
  67. ESP_LOGI("test", "Check ipc_priority with priority caller's task. Should be the same");
  68. vTaskPrioritySet(NULL, 5);
  69. TEST_ASSERT_EQUAL(priority, func_ipc_priority);
  70. }
  71. static void test_func2_ipc(void *arg)
  72. {
  73. int callers_priority = *(int *)arg;
  74. esp_rom_delay_us(1000000 + xPortGetCoreID() * 100);
  75. UBaseType_t priority = uxTaskPriorityGet(NULL);
  76. esp_rom_printf("test_func2_ipc: [callers_priority = %d, priority = %d, cpu = %d]\n", callers_priority, priority, xPortGetCoreID());
  77. }
  78. static void task(void *sema)
  79. {
  80. int priority = uxTaskPriorityGet(NULL);
  81. ESP_LOGI("task", "start [priority = %d, cpu = %d]", priority, xPortGetCoreID());
  82. xSemaphoreTake(*(SemaphoreHandle_t *)sema, portMAX_DELAY);
  83. esp_ipc_call_blocking(!xPortGetCoreID(), test_func2_ipc, &priority);
  84. xSemaphoreGive(*(SemaphoreHandle_t *)sema);
  85. ESP_LOGI("task", "finish [priority = %d, cpu = %d]", priority, xPortGetCoreID());
  86. vTaskDelete(NULL);
  87. }
  88. TEST_CASE("Test multiple ipc_calls", "[ipc]")
  89. {
  90. const int max_tasks = 5;
  91. UBaseType_t priority = uxTaskPriorityGet(NULL);
  92. ESP_LOGI("test", "priority = %d, cpu = %d", priority, xPortGetCoreID());
  93. SemaphoreHandle_t sema_ipc_done[max_tasks * portNUM_PROCESSORS];
  94. for (int task_num = 0; task_num < max_tasks; ++task_num) {
  95. ++priority;
  96. ESP_LOGI("test", "task prio = %d", priority);
  97. for (int cpu_num = 0; cpu_num < portNUM_PROCESSORS; ++cpu_num) {
  98. sema_ipc_done[task_num * 2 + cpu_num] = xSemaphoreCreateBinary();
  99. xTaskCreatePinnedToCore(task, "task", 4096, &sema_ipc_done[task_num * 2 + cpu_num], priority, NULL, 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. xSemaphoreGive(sema_ipc_done[task_num * 2 + cpu_num]);
  105. }
  106. }
  107. for (int task_num = 0; task_num < max_tasks; ++task_num) {
  108. for (int cpu_num = 0; cpu_num < portNUM_PROCESSORS; ++cpu_num) {
  109. xSemaphoreTake(sema_ipc_done[task_num * 2 + cpu_num], portMAX_DELAY);
  110. vSemaphoreDelete(sema_ipc_done[task_num * 2 + cpu_num]);
  111. }
  112. }
  113. }
  114. #endif /* CONFIG_ESP_IPC_USES_CALLERS_PRIORITY */
  115. static void test_func_ipc_cb2(void *arg)
  116. {
  117. vTaskDelay(100 / portTICK_PERIOD_MS);
  118. int *val = (int *)arg;
  119. *val = *val + 1;
  120. }
  121. static void test_func_ipc_cb3(void *arg)
  122. {
  123. vTaskDelay(500 / portTICK_PERIOD_MS);
  124. int *val = (int *)arg;
  125. *val = *val + 1;
  126. }
  127. TEST_CASE("Test ipc_task can not wake up blocking task early", "[ipc]")
  128. {
  129. int val1 = 20;
  130. esp_ipc_call(1, test_func_ipc_cb2, &val1);
  131. TEST_ASSERT_EQUAL(20, val1);
  132. int val2 = 30;
  133. esp_ipc_call_blocking(1, test_func_ipc_cb3, &val2);
  134. TEST_ASSERT_EQUAL(21, val1);
  135. TEST_ASSERT_EQUAL(31, val2);
  136. }
  137. #endif /* !CONFIG_FREERTOS_UNICORE */