esp_expression_with_stack.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. int watchpoint_place=(int)stack; \
  31. portSTACK_TYPE *top_of_stack = &stack[0] + \
  32. (sizeof(stack_size * sizeof(portSTACK_TYPE)) / \
  33. sizeof(portSTACK_TYPE)); \
  34. watchpoint_place=(watchpoint_place+31)&(~31); \
  35. xSemaphoreTake(lock, portMAX_DELAY); \
  36. esp_set_watchpoint(2, (char*)watchpoint_place, 32, ESP_WATCHPOINT_STORE);\
  37. esp_switch_stack_enter(top_of_stack, &backup); \
  38. { \
  39. expression; \
  40. } \
  41. esp_clear_watchpoint(2); \
  42. esp_switch_stack_exit(&backup); \
  43. xSemaphoreGive(lock); \
  44. } \
  45. })
  46. /**
  47. * @brief Changes CPU sp-register to use another stack space and save the previous one
  48. * @param stack Caller allocated stack pointer
  49. * @param backup_stack Pointer to a place to save the current stack
  50. * @note Application must not call this function directly
  51. */
  52. extern void esp_switch_stack_enter(portSTACK_TYPE *stack, uint32_t *backup_stack);
  53. /**
  54. * @brief Restores the previous CPU sp-register
  55. * @param backup_stack Pointer to the place where stack was saved
  56. * @note Application must not call this function directly
  57. */
  58. extern void esp_switch_stack_exit(uint32_t *backup_stack);
  59. #endif