expression_with_stack_riscv.c 2.7 KB

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