expression_with_stack_xtensa.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. esp_clear_watchpoint(1);
  30. uint32_t watchpoint_place = ((uint32_t)stack + 32) & ~0x1f ;
  31. #endif
  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. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  36. //Then put the fake stack inside of TCB:
  37. current_task_stack = current->pxDummy6;
  38. current->pxDummy6 = (void *)stack;
  39. StackType_t *top_of_stack = stack + stack_size;
  40. //Align stack to a 16byte boundary, as required by CPU specific:
  41. top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf));
  42. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  43. esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE);
  44. #endif
  45. xtensa_shared_stack = top_of_stack;
  46. }
  47. void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
  48. {
  49. assert(lock);
  50. assert(stack);
  51. assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
  52. assert(function);
  53. xSemaphoreTake(lock, portMAX_DELAY);
  54. portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
  55. xtensa_shared_stack_function_done = false;
  56. esp_switch_stack_setup(stack, stack_size);
  57. xtensa_shared_stack_callback = function;
  58. portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
  59. setjmp(xtensa_shared_stack_env);
  60. if(!xtensa_shared_stack_function_done) {
  61. esp_shared_stack_invoke_function();
  62. }
  63. portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
  64. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  65. //Restore current task stack:
  66. current->pxDummy6 = (StackType_t *)current_task_stack;
  67. vPortSetStackWatchpoint(current->pxDummy6);
  68. portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
  69. xSemaphoreGive(lock);
  70. }