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