esp_expression_with_stack.h 3.1 KB

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