esp_expression_with_stack.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. * These functions are intended to be use by the macro above, and
  39. * Should never be called directly, otherwise crashes could
  40. * occur
  41. */
  42. extern void esp_switch_stack_enter(portSTACK_TYPE *stack, uint32_t *backup_stack);
  43. extern void esp_switch_stack_exit(uint32_t *backup_stack);
  44. #endif