esp_expression_with_stack.h 2.9 KB

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