expression_with_stack.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <setjmp.h>
  16. #include <string.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/portmacro.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. //We need also to tweak current task stackpointer to avoid erroneous
  29. //stack overflow indication, so fills the stack with freertos known pattern:
  30. memset(stack, 0xa5U, stack_size * sizeof(StackType_t));
  31. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  32. //Then put the fake stack inside of TCB:
  33. current_task_stack = current->pxDummy6;
  34. current->pxDummy6 = (void *)stack;
  35. StackType_t *top_of_stack = stack + stack_size;
  36. //Align stack to a 16byte boundary, as required by CPU specific:
  37. top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf));
  38. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  39. vPortSetStackWatchpoint(stack);
  40. #endif
  41. xtensa_shared_stack = top_of_stack;
  42. }
  43. void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
  44. {
  45. assert(lock);
  46. assert(stack);
  47. assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
  48. assert(function);
  49. xSemaphoreTake(lock, portMAX_DELAY);
  50. portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
  51. xtensa_shared_stack_function_done = false;
  52. esp_switch_stack_setup(stack, stack_size);
  53. xtensa_shared_stack_callback = function;
  54. portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
  55. setjmp(xtensa_shared_stack_env);
  56. if(!xtensa_shared_stack_function_done) {
  57. esp_shared_stack_invoke_function();
  58. }
  59. portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
  60. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  61. //Restore current task stack:
  62. current->pxDummy6 = (StackType_t *)current_task_stack;
  63. vPortSetStackWatchpoint(current->pxDummy6);
  64. portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
  65. xSemaphoreGive(lock);
  66. }