esp_function_with_shared_stack.rst 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 shared stack space which is a callback function
  10. deferred by calling :cpp:func:`esp_execute_shared_stack_function`,
  11. passing that function as parameter
  12. Usage
  13. -----
  14. :cpp:func:`esp_execute_shared_stack_function` takes four 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, the size in bytes of stack and, a pointer
  18. to a user function where the shared stack space will reside, after calling
  19. the function, the user defined function will be deferred as a callback
  20. where functions can be called using the user allocated space without
  21. taking space from current task stack.
  22. The usage may looks like the code below:
  23. .. code-block:: c
  24. void external_stack_function(void)
  25. {
  26. printf("Executing this printf from external stack! \n");
  27. }
  28. //Let's suppose we wanting to call printf using a separated stack space
  29. //allowing app to reduce its stack size.
  30. void app_main()
  31. {
  32. //Allocate a stack buffer, from heap or as a static form:
  33. portSTACK_TYPE *shared_stack = malloc(8192 * sizeof(portSTACK_TYPE));
  34. assert(shared_stack != NULL);
  35. //Allocate a mutex to protect its usage:
  36. SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
  37. assert(printf_lock != NULL);
  38. //Call the desired function using the macro helper:
  39. esp_execute_shared_stack_function(printf_lock,
  40. shared_stack,
  41. 8192,
  42. external_stack_function);
  43. vSemaphoreDelete(printf_lock);
  44. free(shared_stack);
  45. }
  46. .. _esp-call-with-stack-basic_usage:
  47. API Reference
  48. -------------
  49. .. include-build-file:: inc/esp_expression_with_stack.inc