esp_function_with_shared_stack.rst 2.1 KB

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