ipc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include "esp_err.h"
  18. #include "esp_ipc.h"
  19. #include "esp_attr.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. #include "freertos/semphr.h"
  23. static TaskHandle_t s_ipc_task_handle[portNUM_PROCESSORS];
  24. static SemaphoreHandle_t s_ipc_mutex[portNUM_PROCESSORS]; // This mutex is used as a global lock for esp_ipc_* APIs
  25. static SemaphoreHandle_t s_ipc_sem[portNUM_PROCESSORS]; // Two semaphores used to wake each of ipc tasks
  26. static SemaphoreHandle_t s_ipc_ack[portNUM_PROCESSORS]; // Semaphore used to acknowledge that task was woken up,
  27. // or function has finished running
  28. static volatile esp_ipc_func_t s_func[portNUM_PROCESSORS]; // Function which should be called by high priority task
  29. static void * volatile s_func_arg[portNUM_PROCESSORS]; // Argument to pass into s_func
  30. typedef enum {
  31. IPC_WAIT_FOR_START,
  32. IPC_WAIT_FOR_END
  33. } esp_ipc_wait_t;
  34. static volatile esp_ipc_wait_t s_ipc_wait[portNUM_PROCESSORS];// This variable tells high priority task when it should give
  35. // s_ipc_ack semaphore: before s_func is called, or
  36. // after it returns
  37. #if CONFIG_APPTRACE_GCOV_ENABLE
  38. static volatile esp_ipc_func_t s_gcov_func = NULL; // Gcov dump starter function which should be called by high priority task
  39. static void * volatile s_gcov_func_arg; // Argument to pass into s_gcov_func
  40. #endif
  41. static void IRAM_ATTR ipc_task(void* arg)
  42. {
  43. const int cpuid = (int) arg;
  44. assert(cpuid == xPortGetCoreID());
  45. while (true) {
  46. // Wait for IPC to be initiated.
  47. // This will be indicated by giving the semaphore corresponding to
  48. // this CPU.
  49. if (xSemaphoreTake(s_ipc_sem[cpuid], portMAX_DELAY) != pdTRUE) {
  50. // TODO: when can this happen?
  51. abort();
  52. }
  53. #if CONFIG_APPTRACE_GCOV_ENABLE
  54. if (s_gcov_func) {
  55. (*s_gcov_func)(s_gcov_func_arg);
  56. s_gcov_func = NULL;
  57. }
  58. #endif
  59. if (s_func[cpuid]) {
  60. esp_ipc_func_t func = s_func[cpuid];
  61. void* arg = s_func_arg[cpuid];
  62. if (s_ipc_wait[cpuid] == IPC_WAIT_FOR_START) {
  63. xSemaphoreGive(s_ipc_ack[cpuid]);
  64. }
  65. (*func)(arg);
  66. if (s_ipc_wait[cpuid] == IPC_WAIT_FOR_END) {
  67. xSemaphoreGive(s_ipc_ack[cpuid]);
  68. }
  69. }
  70. }
  71. // TODO: currently this is unreachable code. Introduce esp_ipc_uninit
  72. // function which will signal to both tasks that they can shut down.
  73. // Not critical at this point, we don't have a use case for stopping
  74. // IPC yet.
  75. // Also need to delete the semaphore here.
  76. vTaskDelete(NULL);
  77. }
  78. /*
  79. * Initialize inter-processor call module. This function is called automatically
  80. * on CPU start and should not be called from the application.
  81. *
  82. * This function start two tasks, one on each CPU. These tasks are started
  83. * with high priority. These tasks are normally inactive, waiting until one of
  84. * the esp_ipc_call_* functions to be used. One of these tasks will be
  85. * woken up to execute the callback provided to esp_ipc_call_nonblocking or
  86. * esp_ipc_call_blocking.
  87. */
  88. static void esp_ipc_init(void) __attribute__((constructor));
  89. static void esp_ipc_init(void)
  90. {
  91. char task_name[15];
  92. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  93. snprintf(task_name, sizeof(task_name), "ipc%d", i);
  94. s_ipc_mutex[i] = xSemaphoreCreateMutex();
  95. s_ipc_ack[i] = xSemaphoreCreateBinary();
  96. s_ipc_sem[i] = xSemaphoreCreateBinary();
  97. portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, CONFIG_ESP_IPC_TASK_STACK_SIZE, (void*) i,
  98. configMAX_PRIORITIES - 1, &s_ipc_task_handle[i], i);
  99. assert(res == pdTRUE);
  100. }
  101. }
  102. static esp_err_t esp_ipc_call_and_wait(uint32_t cpu_id, esp_ipc_func_t func, void* arg, esp_ipc_wait_t wait_for)
  103. {
  104. if (cpu_id >= portNUM_PROCESSORS) {
  105. return ESP_ERR_INVALID_ARG;
  106. }
  107. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  108. return ESP_ERR_INVALID_STATE;
  109. }
  110. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  111. TaskHandle_t task_handler = xTaskGetCurrentTaskHandle();
  112. UBaseType_t priority_of_current_task = uxTaskPriorityGet(task_handler);
  113. UBaseType_t priority_of_running_ipc_task = uxTaskPriorityGet(s_ipc_task_handle[cpu_id]);
  114. if (priority_of_running_ipc_task < priority_of_current_task) {
  115. vTaskPrioritySet(s_ipc_task_handle[cpu_id], priority_of_current_task);
  116. }
  117. xSemaphoreTake(s_ipc_mutex[cpu_id], portMAX_DELAY);
  118. vTaskPrioritySet(s_ipc_task_handle[cpu_id], priority_of_current_task);
  119. #else
  120. xSemaphoreTake(s_ipc_mutex[0], portMAX_DELAY);
  121. #endif
  122. s_func[cpu_id] = func;
  123. s_func_arg[cpu_id] = arg;
  124. s_ipc_wait[cpu_id] = wait_for;
  125. xSemaphoreGive(s_ipc_sem[cpu_id]);
  126. xSemaphoreTake(s_ipc_ack[cpu_id], portMAX_DELAY);
  127. s_func[cpu_id] = NULL;
  128. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  129. xSemaphoreGive(s_ipc_mutex[cpu_id]);
  130. #else
  131. xSemaphoreGive(s_ipc_mutex[0]);
  132. #endif
  133. return ESP_OK;
  134. }
  135. esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  136. {
  137. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
  138. }
  139. esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  140. {
  141. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
  142. }
  143. // currently this is only called from gcov component
  144. #if CONFIG_APPTRACE_GCOV_ENABLE
  145. esp_err_t esp_ipc_start_gcov_from_isr(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  146. {
  147. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  148. return ESP_ERR_INVALID_STATE;
  149. }
  150. s_gcov_func = func;
  151. s_gcov_func_arg = arg;
  152. xSemaphoreGiveFromISR(s_ipc_sem[cpu_id], NULL);
  153. return ESP_OK;
  154. }
  155. #endif