ipc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 SemaphoreHandle_t s_ipc_mutex; // This mutex is used as a global lock for esp_ipc_* APIs
  24. static SemaphoreHandle_t s_ipc_sem[portNUM_PROCESSORS]; // Two semaphores used to wake each of ipc tasks
  25. static SemaphoreHandle_t s_ipc_ack; // Semaphore used to acknowledge that task was woken up,
  26. // or function has finished running
  27. static volatile esp_ipc_func_t s_func; // Function which should be called by high priority task
  28. static void * volatile s_func_arg; // Argument to pass into s_func
  29. typedef enum {
  30. IPC_WAIT_FOR_START,
  31. IPC_WAIT_FOR_END
  32. } esp_ipc_wait_t;
  33. static volatile esp_ipc_wait_t s_ipc_wait; // This variable tells high priority task when it should give
  34. // s_ipc_ack semaphore: before s_func is called, or
  35. // after it returns
  36. static void IRAM_ATTR ipc_task(void* arg)
  37. {
  38. const uint32_t cpuid = (uint32_t) arg;
  39. assert(cpuid == xPortGetCoreID());
  40. while (true) {
  41. // Wait for IPC to be initiated.
  42. // This will be indicated by giving the semaphore corresponding to
  43. // this CPU.
  44. if (xSemaphoreTake(s_ipc_sem[cpuid], portMAX_DELAY) != pdTRUE) {
  45. // TODO: when can this happen?
  46. abort();
  47. }
  48. esp_ipc_func_t func = s_func;
  49. void* arg = s_func_arg;
  50. if (s_ipc_wait == IPC_WAIT_FOR_START) {
  51. xSemaphoreGive(s_ipc_ack);
  52. }
  53. (*func)(arg);
  54. if (s_ipc_wait == IPC_WAIT_FOR_END) {
  55. xSemaphoreGive(s_ipc_ack);
  56. }
  57. }
  58. // TODO: currently this is unreachable code. Introduce esp_ipc_uninit
  59. // function which will signal to both tasks that they can shut down.
  60. // Not critical at this point, we don't have a use case for stopping
  61. // IPC yet.
  62. // Also need to delete the semaphore here.
  63. vTaskDelete(NULL);
  64. }
  65. /*
  66. * Initialize inter-processor call module. This function is called automatically
  67. * on CPU start and should not be called from the application.
  68. *
  69. * This function start two tasks, one on each CPU. These tasks are started
  70. * with high priority. These tasks are normally inactive, waiting until one of
  71. * the esp_ipc_call_* functions to be used. One of these tasks will be
  72. * woken up to execute the callback provided to esp_ipc_call_nonblocking or
  73. * esp_ipc_call_blocking.
  74. */
  75. static void esp_ipc_init() __attribute__((constructor));
  76. static void esp_ipc_init()
  77. {
  78. s_ipc_mutex = xSemaphoreCreateMutex();
  79. s_ipc_ack = xSemaphoreCreateBinary();
  80. char task_name[15];
  81. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  82. snprintf(task_name, sizeof(task_name), "ipc%d", i);
  83. s_ipc_sem[i] = xSemaphoreCreateBinary();
  84. portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, CONFIG_IPC_TASK_STACK_SIZE, (void*) i,
  85. configMAX_PRIORITIES - 1, NULL, i);
  86. assert(res == pdTRUE);
  87. }
  88. }
  89. 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)
  90. {
  91. if (cpu_id >= portNUM_PROCESSORS) {
  92. return ESP_ERR_INVALID_ARG;
  93. }
  94. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  95. return ESP_ERR_INVALID_STATE;
  96. }
  97. xSemaphoreTake(s_ipc_mutex, portMAX_DELAY);
  98. s_func = func;
  99. s_func_arg = arg;
  100. s_ipc_wait = wait_for;
  101. xSemaphoreGive(s_ipc_sem[cpu_id]);
  102. xSemaphoreTake(s_ipc_ack, portMAX_DELAY);
  103. xSemaphoreGive(s_ipc_mutex);
  104. return ESP_OK;
  105. }
  106. esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  107. {
  108. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
  109. }
  110. esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  111. {
  112. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
  113. }