ipc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. void esp_ipc_init()
  66. {
  67. s_ipc_mutex = xSemaphoreCreateMutex();
  68. s_ipc_ack = xSemaphoreCreateBinary();
  69. char task_name[15];
  70. for (int i = 0; i < portNUM_PROCESSORS; ++i) {
  71. snprintf(task_name, sizeof(task_name), "ipc%d", i);
  72. s_ipc_sem[i] = xSemaphoreCreateBinary();
  73. portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, CONFIG_IPC_TASK_STACK_SIZE, (void*) i,
  74. configMAX_PRIORITIES - 1, NULL, i);
  75. assert(res == pdTRUE);
  76. }
  77. }
  78. 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)
  79. {
  80. if (cpu_id >= portNUM_PROCESSORS) {
  81. return ESP_ERR_INVALID_ARG;
  82. }
  83. if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
  84. return ESP_ERR_INVALID_STATE;
  85. }
  86. xSemaphoreTake(s_ipc_mutex, portMAX_DELAY);
  87. s_func = func;
  88. s_func_arg = arg;
  89. s_ipc_wait = wait_for;
  90. xSemaphoreGive(s_ipc_sem[cpu_id]);
  91. xSemaphoreTake(s_ipc_ack, portMAX_DELAY);
  92. xSemaphoreGive(s_ipc_mutex);
  93. return ESP_OK;
  94. }
  95. esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  96. {
  97. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
  98. }
  99. esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
  100. {
  101. return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
  102. }