1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
23 #include "freertos/FreeRTOS.h"
24 #include "freertos/task.h"
25 #include "freertos/semphr.h"
28 static TaskHandle_t s_ipc_tasks[portNUM_PROCESSORS]; // Two high priority tasks, one for each CPU
29 static SemaphoreHandle_t s_ipc_mutex; // This mutex is used as a global lock for esp_ipc_* APIs
30 static SemaphoreHandle_t s_ipc_sem[portNUM_PROCESSORS]; // Two semaphores used to wake each of s_ipc_tasks
31 static SemaphoreHandle_t s_ipc_ack; // Semaphore used to acknowledge that task was woken up,
32 // or function has finished running
33 static volatile esp_ipc_func_t s_func; // Function which should be called by high priority task
34 static void * volatile s_func_arg; // Argument to pass into s_func
40 static volatile esp_ipc_wait_t s_ipc_wait; // This variable tells high priority task when it should give
41 // s_ipc_ack semaphore: before s_func is called, or
44 static void IRAM_ATTR ipc_task(void* arg)
46 const uint32_t cpuid = (uint32_t) arg;
47 assert(cpuid == xPortGetCoreID());
49 // Wait for IPC to be initiated.
50 // This will be indicated by giving the semaphore corresponding to
52 if (xSemaphoreTake(s_ipc_sem[cpuid], portMAX_DELAY) != pdTRUE) {
53 // TODO: when can this happen?
57 esp_ipc_func_t func = s_func;
58 void* arg = s_func_arg;
60 if (s_ipc_wait == IPC_WAIT_FOR_START) {
61 xSemaphoreGive(s_ipc_ack);
64 if (s_ipc_wait == IPC_WAIT_FOR_END) {
65 xSemaphoreGive(s_ipc_ack);
68 // TODO: currently this is unreachable code. Introduce esp_ipc_uninit
69 // function which will signal to both tasks that they can shut down.
70 // Not critical at this point, we don't have a use case for stopping
72 // Also need to delete the semaphore here.
78 s_ipc_mutex = xSemaphoreCreateMutex();
79 s_ipc_ack = xSemaphoreCreateBinary();
81 for (int i = 0; i < portNUM_PROCESSORS; ++i) {
82 sprintf(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, &s_ipc_tasks[i], i);
86 assert(res == pdTRUE);
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)
92 if (cpu_id >= portNUM_PROCESSORS) {
93 return ESP_ERR_INVALID_ARG;
95 if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
96 return ESP_ERR_INVALID_STATE;
99 xSemaphoreTake(s_ipc_mutex, portMAX_DELAY);
103 s_ipc_wait = IPC_WAIT_FOR_START;
104 xSemaphoreGive(s_ipc_sem[cpu_id]);
105 xSemaphoreTake(s_ipc_ack, portMAX_DELAY);
106 xSemaphoreGive(s_ipc_mutex);
110 esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
112 return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
115 esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
117 return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);