feat(ipc): Adds a new no blocking IPC call

This commit is contained in:
KonstantinKondrashov
2023-11-25 23:32:47 +08:00
committed by Konstantin Kondrashov
parent 6f46db3de8
commit 17c3f85a89
5 changed files with 136 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -12,11 +12,14 @@
#include "esp_ipc.h"
#include "esp_private/esp_ipc_isr.h"
#include "esp_attr.h"
#include "esp_cpu.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#define IPC_MAX_PRIORITY (configMAX_PRIORITIES - 1)
#if !defined(CONFIG_FREERTOS_UNICORE) || defined(CONFIG_APPTRACE_GCOV_ENABLE)
#if CONFIG_COMPILER_OPTIMIZATION_NONE
@@ -39,10 +42,11 @@ typedef enum {
IPC_WAIT_FOR_END,
} esp_ipc_wait_t;
#if CONFIG_APPTRACE_GCOV_ENABLE
static volatile esp_ipc_func_t s_gcov_func = NULL; // Gcov dump starter function which should be called by high priority task
static void * volatile s_gcov_func_arg; // Argument to pass into s_gcov_func
#endif
static esp_ipc_wait_t volatile s_wait_for[portNUM_PROCESSORS];
static volatile esp_ipc_func_t s_no_block_func[portNUM_PROCESSORS] = { 0 };
static volatile bool s_no_block_func_and_arg_are_ready[portNUM_PROCESSORS] = { 0 };
static void * volatile s_no_block_func_arg[portNUM_PROCESSORS];
static void IRAM_ATTR ipc_task(void* arg)
{
@@ -54,29 +58,23 @@ static void IRAM_ATTR ipc_task(void* arg)
#endif
while (true) {
uint32_t ipc_wait;
xTaskNotifyWait(0, ULONG_MAX, &ipc_wait, portMAX_DELAY);
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
#if CONFIG_APPTRACE_GCOV_ENABLE
if (s_gcov_func) {
(*s_gcov_func)(s_gcov_func_arg);
s_gcov_func = NULL;
/* we can not interfer with IPC calls so no need for further processing */
// esp_ipc API and gcov_from_isr APIs can be processed together if they came at the same time
if (ipc_wait == IPC_WAIT_NO) {
continue;
}
if (s_no_block_func_and_arg_are_ready[cpuid] && s_no_block_func[cpuid]) {
(*s_no_block_func[cpuid])(s_no_block_func_arg[cpuid]);
s_no_block_func_and_arg_are_ready[cpuid] = false;
s_no_block_func[cpuid] = NULL;
}
#endif // CONFIG_APPTRACE_GCOV_ENABLE
#ifndef CONFIG_FREERTOS_UNICORE
if (s_func[cpuid]) {
// we need to cache s_func, s_func_arg and ipc_ack variables locally
// because they can be changed by a subsequent IPC call (after xTaskNotify(caller_task_handle)).
esp_ipc_func_t func = s_func[cpuid];
s_func[cpuid] = NULL;
void* func_arg = s_func_arg[cpuid];
esp_ipc_wait_t ipc_wait = s_wait_for[cpuid];
SemaphoreHandle_t ipc_ack = s_ipc_ack[cpuid];
s_func[cpuid] = NULL;
if (ipc_wait == IPC_WAIT_FOR_START) {
xSemaphoreGive(ipc_ack);
@@ -119,7 +117,7 @@ static void esp_ipc_init(void)
s_ipc_mutex[i] = xSemaphoreCreateMutexStatic(&s_ipc_mutex_buffer[i]);
s_ipc_ack[i] = xSemaphoreCreateBinaryStatic(&s_ipc_ack_buffer[i]);
portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, IPC_STACK_SIZE, (void*) i,
configMAX_PRIORITIES - 1, &s_ipc_task_handle[i], i);
IPC_MAX_PRIORITY, &s_ipc_task_handle[i], i);
assert(res == pdTRUE);
(void)res;
}
@@ -151,9 +149,11 @@ static esp_err_t esp_ipc_call_and_wait(uint32_t cpu_id, esp_ipc_func_t func, voi
xSemaphoreTake(s_ipc_mutex[0], portMAX_DELAY);
#endif
s_func[cpu_id] = func;
s_func_arg[cpu_id] = arg;
xTaskNotify(s_ipc_task_handle[cpu_id], wait_for, eSetValueWithOverwrite);
s_wait_for[cpu_id] = wait_for;
// s_func must be set after all other parameters. The ipc_task use this as indicator of the IPC is prepared.
s_func[cpu_id] = func;
xTaskNotifyGive(s_ipc_task_handle[cpu_id]);
xSemaphoreTake(s_ipc_ack[cpu_id], portMAX_DELAY);
#ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
@@ -174,28 +174,33 @@ esp_err_t esp_ipc_call_blocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_END);
}
// currently this is only called from gcov component
// the top level guarantees that the next call will be only after the previous one has completed
#if CONFIG_APPTRACE_GCOV_ENABLE
esp_err_t esp_ipc_start_gcov_from_isr(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
esp_err_t esp_ipc_call_nonblocking(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
{
if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
if (cpu_id >= portNUM_PROCESSORS || s_ipc_task_handle[cpu_id] == NULL) {
return ESP_ERR_INVALID_ARG;
}
if (cpu_id == xPortGetCoreID() && xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
return ESP_ERR_INVALID_STATE;
}
// Since it is called from an interrupt, it can not wait for a mutex to be released.
if (s_gcov_func == NULL) {
s_gcov_func_arg = arg;
s_gcov_func = func;
// Since it can be called from an interrupt or Scheduler is Suspened, it can not wait for a mutex to be released.
if (esp_cpu_compare_and_set((volatile uint32_t *)&s_no_block_func[cpu_id], 0, (uint32_t)func)) {
s_no_block_func_arg[cpu_id] = arg;
s_no_block_func_and_arg_are_ready[cpu_id] = true;
// If the target task already has a notification pending then its notification value is not updated (WithoutOverwrite).
xTaskNotifyFromISR(s_ipc_task_handle[cpu_id], IPC_WAIT_NO, eSetValueWithoutOverwrite, NULL);
if (xPortInIsrContext()) {
vTaskNotifyGiveFromISR(s_ipc_task_handle[cpu_id], NULL);
} else {
#ifdef CONFIG_ESP_IPC_USES_CALLERS_PRIORITY
vTaskPrioritySet(s_ipc_task_handle[cpu_id], IPC_MAX_PRIORITY);
#endif
xTaskNotifyGive(s_ipc_task_handle[cpu_id]);
}
return ESP_OK;
}
// the previous call was not completed
return ESP_FAIL;
}
#endif // CONFIG_APPTRACE_GCOV_ENABLE
#endif // !defined(CONFIG_FREERTOS_UNICORE) || defined(CONFIG_APPTRACE_GCOV_ENABLE)