ipc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. static void IRAM_ATTR ipc_task(void* arg)
  38. {
  39. const uint32_t cpuid = (uint32_t) arg;
  40. assert(cpuid == xPortGetCoreID());
  41. while (true) {
  42. // Wait for IPC to be initiated.
  43. // This will be indicated by giving the semaphore corresponding to
  44. // this CPU.
  45. if (xSemaphoreTake(s_ipc_sem[cpuid], portMAX_DELAY) != pdTRUE) {
  46. // TODO: when can this happen?
  47. abort();
  48. }
  49. esp_ipc_func_t func = s_func[cpuid];
  50. void* arg = s_func_arg[cpuid];
  51. if (s_ipc_wait[cpuid] == IPC_WAIT_FOR_START) {
  52. xSemaphoreGive(s_ipc_ack[cpuid]);
  53. }
  54. (*func)(arg);
  55. if (s_ipc_wait[cpuid] == IPC_WAIT_FOR_END) {
  56. xSemaphoreGive(s_ipc_ack[cpuid]);
  57. }
  58. }
  59. // TODO: currently this is unreachable code. Introduce esp_ipc_uninit
  60. // function which will signal to both tasks that they can shut down.
  61. // Not critical at this point, we don't have a use case for stopping
  62. // IPC yet.
  63. // Also need to delete the semaphore here.
  64. vTaskDelete(NULL);
  65. }
  66. /*
  67. * Initialize inter-processor call module. This function is called automatically
  68. * on CPU start and should not be called from the application.
  69. *
  70. * This function start two tasks, one on each CPU. These tasks are started
  71. * with high priority. These tasks are normally inactive, waiting until one of
  72. * the esp_ipc_call_* functions to be used. One of these tasks will be
  73. * woken up to execute the callback provided to esp_ipc_call_nonblocking or
  74. * esp_ipc_call_blocking.
  75. */
  76. static void esp_ipc_init(void) __attribute__((constructor));
  77. static void esp_ipc_init(void)
  78. {
  79. char task_name[15];
  80. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  81. snprintf(task_name, sizeof(task_name), "ipc%d", i);
  82. s_ipc_mutex[i] = xSemaphoreCreateMutex();
  83. s_ipc_ack[i] = xSemaphoreCreateBinary();
  84. s_ipc_sem[i] = xSemaphoreCreateBinary();
  85. portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, CONFIG_ESP_IPC_TASK_STACK_SIZE, (void*) i,
  86. configMAX_PRIORITIES - 1, &s_ipc_task_handle[i], i);
  87. assert(res == pdTRUE);
  88. }
  89. }
  90. 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)
  91. {
  92. if (cpu_id >= portNUM_PROCESSORS) {
  93. return ESP_ERR_INVALID_ARG;
  94. }
  95. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  96. return ESP_ERR_INVALID_STATE;
  97. }
  98. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  99. TaskHandle_t task_handler = xTaskGetCurrentTaskHandle();
  100. UBaseType_t priority_of_current_task = uxTaskPriorityGet(task_handler);
  101. UBaseType_t priority_of_running_ipc_task = uxTaskPriorityGet(s_ipc_task_handle[cpu_id]);
  102. if (priority_of_running_ipc_task < priority_of_current_task) {
  103. vTaskPrioritySet(s_ipc_task_handle[cpu_id], priority_of_current_task);
  104. }
  105. xSemaphoreTake(s_ipc_mutex[cpu_id], portMAX_DELAY);
  106. vTaskPrioritySet(s_ipc_task_handle[cpu_id], priority_of_current_task);
  107. #else
  108. xSemaphoreTake(s_ipc_mutex[0], portMAX_DELAY);
  109. #endif
  110. s_func[cpu_id] = func;
  111. s_func_arg[cpu_id] = arg;
  112. s_ipc_wait[cpu_id] = wait_for;
  113. xSemaphoreGive(s_ipc_sem[cpu_id]);
  114. xSemaphoreTake(s_ipc_ack[cpu_id], portMAX_DELAY);
  115. #ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
  116. xSemaphoreGive(s_ipc_mutex[cpu_id]);
  117. #else
  118. xSemaphoreGive(s_ipc_mutex[0]);
  119. #endif
  120. return ESP_OK;
  121. }
  122. esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  123. {
  124. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
  125. }
  126. esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  127. {
  128. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
  129. }