esp_expression_with_stack.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /**
  19. * @brief Executes a 1-line expression with a application alocated stack
  20. * @param lock Mutex object to protect in case of shared stack
  21. * @param stack Pointer to user alocated stack, it must points to its top
  22. * @param expression Expression or function to be executed using the stack
  23. */
  24. #define ESP_EXECUTE_EXPRESSION_WITH_STACK(lock, stack, expression) \
  25. ({ \
  26. if(lock) { \
  27. uint32_t backup; \
  28. xSemaphoreTake(lock, portMAX_DELAY); \
  29. esp_switch_stack_enter(stack, &backup); \
  30. { \
  31. expression; \
  32. } \
  33. esp_switch_stack_exit(&backup); \
  34. xSemaphoreGive(lock); \
  35. } \
  36. })
  37. /**
  38. * @brief Changes CPU sp-register to use another stack space and save the previous one
  39. * @param stack Caller allocated stack pointer
  40. * @param backup_stack Pointer to a place to save the current stack
  41. * @note Application must not call this function directly
  42. */
  43. extern void esp_switch_stack_enter(portSTACK_TYPE *stack, uint32_t *backup_stack);
  44. /**
  45. * @brief Restores the previous CPU sp-register
  46. * @param backup_stack Pointer to the place where stack was saved
  47. * @note Application must not call this function directly
  48. */
  49. extern void esp_switch_stack_exit(uint32_t *backup_stack);
  50. #endif