esp_expression_with_stack.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. Call function with external stack
  2. =================================
  3. Overview
  4. --------
  5. A given function can be executed with a user allocated stack space
  6. which is independent of current task stack, this mechanism can be
  7. used to save stack space wasted by tasks which call a common function
  8. with intensive stack usage such as `printf`. The given function can
  9. be called inside the macro :cpp:func:`ESP_EXECUTE_EXPRESSION_WITH_STACK`
  10. it will redirect the target function to be executed using the space
  11. allocated by the user.
  12. Usage
  13. -----
  14. :cpp:func:`ESP_EXECUTE_EXPRESSION_WITH_STACK` takes three arguments,
  15. a mutex object allocated by the caller, which is used to protect if
  16. the same function shares its allocated stack, a pointer to the top
  17. of stack used to that fuction, and the function itself, note the
  18. function is passed exactly in the same way as do when you call
  19. it on a regular way.
  20. The usage may looks like the code below:
  21. .. code-block:: c
  22. //Let's suppose we wanting to call printf using a separated stack space
  23. //allowing app to reduce its stack size.
  24. void app_main()
  25. {
  26. //Allocate a stack buffer, from heap or as a static form:
  27. portSTACK_TYPE *shared_stack = malloc(8192 * sizeof(portSTACK_TYPE));
  28. //Allocate a mutex to protect its usage:
  29. SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
  30. //Call the desired function using the macro helper:
  31. ESP_EXECUTE_EXPRESSION_WITH_STACK(printf_lock,
  32. shared_stack,
  33. printf("Executing this from external stack! \n"));
  34. vSemaphoreDelete(printf_lock);
  35. free(shared_stack);
  36. }
  37. .. _esp-call-with-stack-basic_usage:
  38. API Reference
  39. -------------
  40. .. include:: /_build/inc/esp_expression_with_stack.inc