2020-01-06 20:01:53 +00:00
|
|
|
// Copyright 2015-2019 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.
|
|
|
|
|
2019-11-27 16:52:27 +00:00
|
|
|
#include <esp_expression_with_stack.h>
|
|
|
|
#include <freertos/xtensa_rtos.h>
|
|
|
|
#include <freertos/xtensa_context.h>
|
2020-03-12 05:59:53 +00:00
|
|
|
#include <setjmp.h>
|
2020-04-01 15:10:13 +00:00
|
|
|
#include <string.h>
|
2019-11-27 16:52:27 +00:00
|
|
|
|
2020-04-01 15:10:13 +00:00
|
|
|
StackType_t *xtensa_shared_stack;
|
|
|
|
shared_stack_function xtensa_shared_stack_callback;
|
|
|
|
jmp_buf xtensa_shared_stack_env;
|
|
|
|
bool xtensa_shared_stack_function_done = false;
|
|
|
|
static portMUX_TYPE xtensa_shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
static void *current_task_stack = NULL;
|
2020-03-12 05:59:53 +00:00
|
|
|
|
|
|
|
extern void esp_shared_stack_invoke_function(void);
|
|
|
|
|
|
|
|
static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
|
2019-11-27 16:52:27 +00:00
|
|
|
{
|
2020-01-06 20:01:53 +00:00
|
|
|
#if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
|
2020-02-10 15:03:24 +00:00
|
|
|
esp_clear_watchpoint(1);
|
2020-03-12 05:59:53 +00:00
|
|
|
uint32_t watchpoint_place = ((uint32_t)stack + 32) & ~0x1f ;
|
2020-01-06 20:01:53 +00:00
|
|
|
#endif
|
2020-04-01 15:10:13 +00:00
|
|
|
//We need also to tweak current task stackpointer to avoid erroneous
|
|
|
|
//stack overflow indication, so fills the stack with freertos known pattern:
|
|
|
|
memset(stack, 0xa5U, stack_size * sizeof(StackType_t));
|
|
|
|
|
|
|
|
StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
|
|
|
|
//Then put the fake stack inside of TCB:
|
|
|
|
current_task_stack = current->pxDummy6;
|
|
|
|
current->pxDummy6 = (void *)stack;
|
|
|
|
|
2020-03-12 05:59:53 +00:00
|
|
|
StackType_t *top_of_stack = stack + stack_size;
|
2019-11-27 16:52:27 +00:00
|
|
|
|
|
|
|
//Align stack to a 16byte boundary, as required by CPU specific:
|
2020-03-12 05:59:53 +00:00
|
|
|
top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf));
|
2019-11-27 16:52:27 +00:00
|
|
|
|
2020-01-06 20:01:53 +00:00
|
|
|
#if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
|
2020-02-10 15:03:24 +00:00
|
|
|
esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE);
|
2020-01-06 20:01:53 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-01 15:10:13 +00:00
|
|
|
xtensa_shared_stack = top_of_stack;
|
2020-03-12 05:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
|
|
|
|
{
|
|
|
|
assert(lock);
|
|
|
|
assert(stack);
|
|
|
|
assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
|
|
|
|
assert(function);
|
|
|
|
|
|
|
|
xSemaphoreTake(lock, portMAX_DELAY);
|
2020-04-01 15:10:13 +00:00
|
|
|
portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
|
|
|
|
xtensa_shared_stack_function_done = false;
|
2020-03-12 05:59:53 +00:00
|
|
|
esp_switch_stack_setup(stack, stack_size);
|
2020-04-01 15:10:13 +00:00
|
|
|
xtensa_shared_stack_callback = function;
|
|
|
|
portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
|
2020-03-12 05:59:53 +00:00
|
|
|
|
2020-04-01 15:10:13 +00:00
|
|
|
setjmp(xtensa_shared_stack_env);
|
|
|
|
if(!xtensa_shared_stack_function_done) {
|
2020-03-12 05:59:53 +00:00
|
|
|
esp_shared_stack_invoke_function();
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:10:13 +00:00
|
|
|
portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
|
2020-03-12 05:59:53 +00:00
|
|
|
StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
|
2020-04-01 15:10:13 +00:00
|
|
|
|
|
|
|
//Restore current task stack:
|
|
|
|
current->pxDummy6 = (StackType_t *)current_task_stack;
|
2020-03-12 05:59:53 +00:00
|
|
|
vPortSetStackWatchpoint(current->pxDummy6);
|
2020-04-01 15:10:13 +00:00
|
|
|
portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
|
2020-03-12 05:59:53 +00:00
|
|
|
|
|
|
|
xSemaphoreGive(lock);
|
2019-11-27 16:52:27 +00:00
|
|
|
}
|