misc adjustment of esp32 component

This commit is contained in:
morris
2019-03-26 16:30:43 +08:00
committed by suda-morris
parent 30e3e26834
commit f5b03c9ea3
62 changed files with 146 additions and 302 deletions

View File

@@ -0,0 +1,64 @@
@COMMENT@
#include <string.h>
#if __has_include("soc/soc.h")
#include "soc/soc.h"
#endif
@HEADERS@
#ifdef CONFIG_ESP_ERR_TO_NAME_LOOKUP
#define ERR_TBL_IT(err) {err, #err}
typedef struct {
esp_err_t code;
const char *msg;
} esp_err_msg_t;
static const esp_err_msg_t esp_err_msg_table[] = {
@ERROR_ITEMS@
};
#endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP
static const char esp_unknown_msg[] =
#ifdef CONFIG_ESP_ERR_TO_NAME_LOOKUP
"ERROR";
#else
"UNKNOWN ERROR";
#endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP
const char *esp_err_to_name(esp_err_t code)
{
#ifdef CONFIG_ESP_ERR_TO_NAME_LOOKUP
int i;
for (i = 0; i < sizeof(esp_err_msg_table)/sizeof(esp_err_msg_table[0]); ++i) {
if (esp_err_msg_table[i].code == code) {
return esp_err_msg_table[i].msg;
}
}
#endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP
return esp_unknown_msg;
}
const char *esp_err_to_name_r(esp_err_t code, char *buf, size_t buflen)
{
#ifdef CONFIG_ESP_ERR_TO_NAME_LOOKUP
int i;
for (i = 0; i < sizeof(esp_err_msg_table)/sizeof(esp_err_msg_table[0]); ++i) {
if (esp_err_msg_table[i].code == code) {
strlcpy(buf, esp_err_msg_table[i].msg, buflen);
return buf;
}
}
#endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP
if (strerror_r(code, buf, buflen) == 0) {
return buf;
}
snprintf(buf, buflen, "%s 0x%x(%d)", esp_unknown_msg, code, code);
return buf;
}

View File

@@ -0,0 +1,151 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "esp_attr.h"
#include "esp_freertos_hooks.h"
#include "sdkconfig.h"
#include "esp_pm.h"
#include "esp_private/pm_impl.h"
//We use just a static array here because it's not expected many components will need
//an idle or tick hook.
#define MAX_HOOKS 8
static portMUX_TYPE hooks_spinlock = portMUX_INITIALIZER_UNLOCKED;
static esp_freertos_idle_cb_t idle_cb[portNUM_PROCESSORS][MAX_HOOKS]={0};
static esp_freertos_tick_cb_t tick_cb[portNUM_PROCESSORS][MAX_HOOKS]={0};
void IRAM_ATTR esp_vApplicationTickHook()
{
int n;
int core = xPortGetCoreID();
for (n=0; n<MAX_HOOKS; n++) {
if (tick_cb[core][n]!=NULL) {
tick_cb[core][n]();
}
}
}
void esp_vApplicationIdleHook()
{
bool can_go_idle=true;
int core = xPortGetCoreID();
for (int n = 0; n < MAX_HOOKS; n++) {
if (idle_cb[core][n] != NULL && !idle_cb[core][n]()) {
can_go_idle = false;
}
}
if (!can_go_idle) {
return;
}
#ifdef CONFIG_PM_ENABLE
esp_pm_impl_idle_hook();
#endif
esp_pm_impl_waiti();
}
esp_err_t esp_register_freertos_idle_hook_for_cpu(esp_freertos_idle_cb_t new_idle_cb, UBaseType_t cpuid)
{
if(cpuid >= portNUM_PROCESSORS){
return ESP_ERR_INVALID_ARG;
}
portENTER_CRITICAL(&hooks_spinlock);
for(int n = 0; n < MAX_HOOKS; n++){
if (idle_cb[cpuid][n]==NULL) {
idle_cb[cpuid][n]=new_idle_cb;
portEXIT_CRITICAL(&hooks_spinlock);
return ESP_OK;
}
}
portEXIT_CRITICAL(&hooks_spinlock);
return ESP_ERR_NO_MEM;
}
esp_err_t esp_register_freertos_idle_hook(esp_freertos_idle_cb_t new_idle_cb)
{
return esp_register_freertos_idle_hook_for_cpu(new_idle_cb, xPortGetCoreID());
}
esp_err_t esp_register_freertos_tick_hook_for_cpu(esp_freertos_tick_cb_t new_tick_cb, UBaseType_t cpuid)
{
if(cpuid >= portNUM_PROCESSORS){
return ESP_ERR_INVALID_ARG;
}
portENTER_CRITICAL(&hooks_spinlock);
for(int n = 0; n < MAX_HOOKS; n++){
if (tick_cb[cpuid][n]==NULL) {
tick_cb[cpuid][n]=new_tick_cb;
portEXIT_CRITICAL(&hooks_spinlock);
return ESP_OK;
}
}
portEXIT_CRITICAL(&hooks_spinlock);
return ESP_ERR_NO_MEM;
}
esp_err_t esp_register_freertos_tick_hook(esp_freertos_tick_cb_t new_tick_cb)
{
return esp_register_freertos_tick_hook_for_cpu(new_tick_cb, xPortGetCoreID());
}
void esp_deregister_freertos_idle_hook_for_cpu(esp_freertos_idle_cb_t old_idle_cb, UBaseType_t cpuid)
{
portENTER_CRITICAL(&hooks_spinlock);
if(cpuid >= portNUM_PROCESSORS){
return;
}
for(int n = 0; n < MAX_HOOKS; n++){
if(idle_cb[cpuid][n] == old_idle_cb) idle_cb[cpuid][n] = NULL;
}
portEXIT_CRITICAL(&hooks_spinlock);
}
void esp_deregister_freertos_idle_hook(esp_freertos_idle_cb_t old_idle_cb)
{
portENTER_CRITICAL(&hooks_spinlock);
for(int m = 0; m < portNUM_PROCESSORS; m++) {
esp_deregister_freertos_idle_hook_for_cpu(old_idle_cb, m);
}
portEXIT_CRITICAL(&hooks_spinlock);
}
void esp_deregister_freertos_tick_hook_for_cpu(esp_freertos_tick_cb_t old_tick_cb, UBaseType_t cpuid)
{
portENTER_CRITICAL(&hooks_spinlock);
if(cpuid >= portNUM_PROCESSORS){
return;
}
for(int n = 0; n < MAX_HOOKS; n++){
if(tick_cb[cpuid][n] == old_tick_cb) tick_cb[cpuid][n] = NULL;
}
portEXIT_CRITICAL(&hooks_spinlock);
}
void esp_deregister_freertos_tick_hook(esp_freertos_tick_cb_t old_tick_cb)
{
portENTER_CRITICAL(&hooks_spinlock);
for(int m = 0; m < portNUM_PROCESSORS; m++){
esp_deregister_freertos_tick_hook_for_cpu(old_tick_cb, m);
}
portEXIT_CRITICAL(&hooks_spinlock);
}

View File

@@ -0,0 +1,130 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "esp_err.h"
#include "esp_ipc.h"
#include "esp_attr.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
static SemaphoreHandle_t s_ipc_mutex; // This mutex is used as a global lock for esp_ipc_* APIs
static SemaphoreHandle_t s_ipc_sem[portNUM_PROCESSORS]; // Two semaphores used to wake each of ipc tasks
static SemaphoreHandle_t s_ipc_ack; // Semaphore used to acknowledge that task was woken up,
// or function has finished running
static volatile esp_ipc_func_t s_func; // Function which should be called by high priority task
static void * volatile s_func_arg; // Argument to pass into s_func
typedef enum {
IPC_WAIT_FOR_START,
IPC_WAIT_FOR_END
} esp_ipc_wait_t;
static volatile esp_ipc_wait_t s_ipc_wait; // This variable tells high priority task when it should give
// s_ipc_ack semaphore: before s_func is called, or
// after it returns
static void IRAM_ATTR ipc_task(void* arg)
{
const uint32_t cpuid = (uint32_t) arg;
assert(cpuid == xPortGetCoreID());
while (true) {
// Wait for IPC to be initiated.
// This will be indicated by giving the semaphore corresponding to
// this CPU.
if (xSemaphoreTake(s_ipc_sem[cpuid], portMAX_DELAY) != pdTRUE) {
// TODO: when can this happen?
abort();
}
esp_ipc_func_t func = s_func;
void* arg = s_func_arg;
if (s_ipc_wait == IPC_WAIT_FOR_START) {
xSemaphoreGive(s_ipc_ack);
}
(*func)(arg);
if (s_ipc_wait == IPC_WAIT_FOR_END) {
xSemaphoreGive(s_ipc_ack);
}
}
// TODO: currently this is unreachable code. Introduce esp_ipc_uninit
// function which will signal to both tasks that they can shut down.
// Not critical at this point, we don't have a use case for stopping
// IPC yet.
// Also need to delete the semaphore here.
vTaskDelete(NULL);
}
/*
* Initialize inter-processor call module. This function is called automatically
* on CPU start and should not be called from the application.
*
* This function start two tasks, one on each CPU. These tasks are started
* with high priority. These tasks are normally inactive, waiting until one of
* the esp_ipc_call_* functions to be used. One of these tasks will be
* woken up to execute the callback provided to esp_ipc_call_nonblocking or
* esp_ipc_call_blocking.
*/
static void esp_ipc_init() __attribute__((constructor));
static void esp_ipc_init()
{
s_ipc_mutex = xSemaphoreCreateMutex();
s_ipc_ack = xSemaphoreCreateBinary();
char task_name[15];
for (int i = 0; i < portNUM_PROCESSORS; ++i) {
snprintf(task_name, sizeof(task_name), "ipc%d", i);
s_ipc_sem[i] = xSemaphoreCreateBinary();
portBASE_TYPE res = xTaskCreatePinnedToCore(ipc_task, task_name, CONFIG_IPC_TASK_STACK_SIZE, (void*) i,
configMAX_PRIORITIES - 1, NULL, i);
assert(res == pdTRUE);
}
}
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)
{
if (cpu_id >= portNUM_PROCESSORS) {
return ESP_ERR_INVALID_ARG;
}
if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
return ESP_ERR_INVALID_STATE;
}
xSemaphoreTake(s_ipc_mutex, portMAX_DELAY);
s_func = func;
s_func_arg = arg;
s_ipc_wait = wait_for;
xSemaphoreGive(s_ipc_sem[cpu_id]);
xSemaphoreTake(s_ipc_ack, portMAX_DELAY);
xSemaphoreGive(s_ipc_mutex);
return ESP_OK;
}
esp_err_t esp_ipc_call(uint32_t cpu_id, esp_ipc_func_t func, void* arg)
{
return esp_ipc_call_and_wait(cpu_id, func, arg, IPC_WAIT_FOR_START);
}
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);
}