esp_function_with_shared_stack.rst 2.1 KB

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