esp_expression_with_stack.h 3.5 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. #pragma once
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/task.h"
  18. #include "esp_debug_helpers.h"
  19. #include "esp_log.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * @brief Executes a 1-line expression with a application alocated stack
  25. * @param lock Mutex object to protect in case of shared stack
  26. * @param stack Pointer to user alocated stack
  27. * @param stack_size Size of current stack in bytes
  28. * @param expression Expression or function to be executed using the stack
  29. * @note if either lock, stack or stack size is invalid, the expression will
  30. * be called using the current stack.
  31. */
  32. #define ESP_EXECUTE_EXPRESSION_WITH_STACK(lock, stack, stack_size, expression) \
  33. ({ \
  34. assert(lock && stack && (stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE)); \
  35. uint32_t backup; \
  36. xSemaphoreTake(lock, portMAX_DELAY); \
  37. StackType_t *top_of_stack = esp_switch_stack_setup(stack, stack_size); \
  38. esp_switch_stack_enter(top_of_stack, &backup); \
  39. { \
  40. expression; \
  41. } \
  42. esp_switch_stack_exit(&backup); \
  43. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle(); \
  44. /* pxDummy6 is the stack base of current thread defined in TCB_t */ \
  45. /* place the watchpoint on current task stack after function execution*/ \
  46. vPortSetStackWatchpoint(current->pxDummy6); \
  47. xSemaphoreGive(lock); \
  48. })
  49. /**
  50. * @brief Fill stack frame with CPU-specifics value before use
  51. * @param stack Caller allocated stack pointer
  52. * @param stack_size Size of stack in bytes
  53. * @return New pointer to the top of stack
  54. * @note Application must not call this function directly
  55. */
  56. StackType_t * esp_switch_stack_setup(StackType_t *stack, size_t stack_size);
  57. /**
  58. * @brief Changes CPU sp-register to use another stack space and save the previous one
  59. * @param stack Caller allocated stack pointer
  60. * @param backup_stack Pointer to a place to save the current stack
  61. * @note Application must not call this function directly
  62. */
  63. extern void esp_switch_stack_enter(StackType_t *stack, uint32_t *backup_stack);
  64. /**
  65. * @brief Restores the previous CPU sp-register
  66. * @param backup_stack Pointer to the place where stack was saved
  67. * @note Application must not call this function directly
  68. */
  69. extern void esp_switch_stack_exit(uint32_t *backup_stack);
  70. #ifdef __cplusplus
  71. }
  72. #endif