expression_with_stack_xtensa.c 3.4 KB

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