expression_with_stack_riscv.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <riscv/rvruntime-frames.h>
  16. #include <string.h>
  17. static portMUX_TYPE shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
  18. static void *current_task_stack = NULL;
  19. extern void esp_shared_stack_invoke_function(shared_stack_function function, void *stack);
  20. static StackType_t *esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
  21. {
  22. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  23. // TODO ESP32-C3 IDF-2207
  24. // esp_clear_watchpoint(1);
  25. // uint32_t watchpoint_place = ((uint32_t)stack + 32) & ~0x1f ;
  26. #endif
  27. //We need also to tweak current task stackpointer to avoid erroneous
  28. //stack overflow indication, so fills the stack with freertos known pattern:
  29. memset(stack, 0xa5U, stack_size * sizeof(StackType_t));
  30. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  31. //Then put the fake stack inside of TCB:
  32. current_task_stack = current->pxDummy6;
  33. current->pxDummy6 = (void *)stack;
  34. StackType_t *top_of_stack = stack + stack_size;
  35. //Align stack to a 16byte boundary, as required by CPU specific:
  36. top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf));
  37. RvExcFrame *adjusted_top_of_stack = (RvExcFrame *) top_of_stack;
  38. adjusted_top_of_stack--;
  39. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  40. // TODO ESP32-C3 IDF-2207
  41. //esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE);
  42. #endif
  43. return ((StackType_t *)adjusted_top_of_stack);
  44. }
  45. void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
  46. {
  47. assert(lock);
  48. assert(stack);
  49. assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
  50. assert(function);
  51. xSemaphoreTake(lock, portMAX_DELAY);
  52. portENTER_CRITICAL(&shared_stack_spinlock);
  53. stack = esp_switch_stack_setup(stack, stack_size);
  54. portEXIT_CRITICAL(&shared_stack_spinlock);
  55. esp_shared_stack_invoke_function(function, stack);
  56. portENTER_CRITICAL(&shared_stack_spinlock);
  57. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  58. //Restore current task stack:
  59. current->pxDummy6 = (StackType_t *)current_task_stack;
  60. vPortSetStackWatchpoint(current->pxDummy6);
  61. portEXIT_CRITICAL(&shared_stack_spinlock);
  62. xSemaphoreGive(lock);
  63. }