1 // Copyright 2015-2019 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 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
15 #include <esp_expression_with_stack.h>
16 #include <setjmp.h>
17 #include <string.h>
18 #include "freertos/FreeRTOS.h"
19 #include "freertos/portmacro.h"
20 
21 StackType_t *xtensa_shared_stack;
22 shared_stack_function xtensa_shared_stack_callback;
23 jmp_buf xtensa_shared_stack_env;
24 bool xtensa_shared_stack_function_done = false;
25 static portMUX_TYPE xtensa_shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
26 static void *current_task_stack = NULL;
27 
28 extern void esp_shared_stack_invoke_function(void);
29 
esp_switch_stack_setup(StackType_t * stack,size_t stack_size)30 static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
31 {
32     //We need also to tweak current task stackpointer to avoid erroneous
33     //stack overflow indication, so fills the stack with freertos known pattern:
34     memset(stack, 0xa5U, stack_size * sizeof(StackType_t));
35 
36     StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
37     //Then put the fake stack inside of TCB:
38     current_task_stack = current->pxDummy6;
39     current->pxDummy6 = (void *)stack;
40 
41     StackType_t *top_of_stack = stack + stack_size;
42 
43     //Align stack to a 16byte boundary, as required by CPU specific:
44     top_of_stack =  (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf));
45 
46 #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
47     vPortSetStackWatchpoint(stack);
48 #endif
49 
50     xtensa_shared_stack = top_of_stack;
51 }
52 
53 
esp_execute_shared_stack_function(SemaphoreHandle_t lock,void * stack,size_t stack_size,shared_stack_function function)54 void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
55 {
56     assert(lock);
57     assert(stack);
58     assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
59     assert(function);
60 
61     xSemaphoreTake(lock, portMAX_DELAY);
62     portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
63     xtensa_shared_stack_function_done = false;
64     esp_switch_stack_setup(stack, stack_size);
65     xtensa_shared_stack_callback = function;
66     portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
67 
68     setjmp(xtensa_shared_stack_env);
69     if(!xtensa_shared_stack_function_done) {
70         esp_shared_stack_invoke_function();
71     }
72 
73     portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
74     StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
75 
76     //Restore current task stack:
77     current->pxDummy6 = (StackType_t *)current_task_stack;
78     vPortSetStackWatchpoint(current->pxDummy6);
79     portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
80 
81     xSemaphoreGive(lock);
82 }
83