ipc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_tasks[portNUM_PROCESSORS]; // Two high priority tasks, one for each CPU
  24. static SemaphoreHandle_t s_ipc_mutex; // 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 s_ipc_tasks
  26. static SemaphoreHandle_t s_ipc_ack; // Semaphore used to acknowledge that task was woken up,
  27. // or function has finished running
  28. static volatile esp_ipc_func_t s_func; // Function which should be called by high priority task
  29. static void * volatile s_func_arg; // 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; // 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;
  50. void* arg = s_func_arg;
  51. if (s_ipc_wait == IPC_WAIT_FOR_START) {
  52. xSemaphoreGive(s_ipc_ack);
  53. }
  54. (*func)(arg);
  55. if (s_ipc_wait == IPC_WAIT_FOR_END) {
  56. xSemaphoreGive(s_ipc_ack);
  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. void esp_ipc_init()
  67. {
  68. s_ipc_mutex = xSemaphoreCreateMutex();
  69. s_ipc_ack = xSemaphoreCreateBinary();
  70. const char* task_names[2] = {"ipc0", "ipc1"};
  71. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  72. s_ipc_sem[i] = xSemaphoreCreateBinary();
  73. xTaskCreatePinnedToCore(ipc_task, task_names[i], XT_STACK_MIN_SIZE, (void*) i,
  74. configMAX_PRIORITIES - 1, &s_ipc_tasks[i], i);
  75. }
  76. }
  77. 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)
  78. {
  79. if (cpu_id >= portNUM_PROCESSORS) {
  80. return ESP_ERR_INVALID_ARG;
  81. }
  82. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  83. return ESP_ERR_INVALID_STATE;
  84. }
  85. xSemaphoreTake(s_ipc_mutex, portMAX_DELAY);
  86. s_func = func;
  87. s_func_arg = arg;
  88. s_ipc_wait = IPC_WAIT_FOR_START;
  89. xSemaphoreGive(s_ipc_sem[cpu_id]);
  90. xSemaphoreTake(s_ipc_ack, portMAX_DELAY);
  91. xSemaphoreGive(s_ipc_mutex);
  92. return ESP_OK;
  93. }
  94. esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  95. {
  96. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
  97. }
  98. esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  99. {
  100. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
  101. }