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