test_shared_stack_printf.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "unity.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "freertos/semphr.h"
  7. #include "sdkconfig.h"
  8. #include "test_utils.h"
  9. #include "esp_expression_with_stack.h"
  10. #define SHARED_STACK_SIZE 8192
  11. static StackType_t *shared_stack_sp = NULL;
  12. void external_stack_function(void)
  13. {
  14. printf("Executing this printf from external stack! sp=%p\n", get_sp());
  15. shared_stack_sp = (StackType_t *)get_sp();
  16. }
  17. void another_external_stack_function(void)
  18. {
  19. //We can even use Freertos resources inside of this context.
  20. printf("We can even use FreeRTOS resources... yielding, sp=%p\n", get_sp());
  21. taskYIELD();
  22. shared_stack_sp = (StackType_t *)get_sp();
  23. }
  24. TEST_CASE("test printf using shared buffer stack", "[newlib]")
  25. {
  26. portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE);
  27. TEST_ASSERT_NOT_NULL(shared_stack);
  28. SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
  29. TEST_ASSERT_NOT_NULL(printf_lock);
  30. printf("current task sp: %p\n", get_sp());
  31. printf("shared_stack: %p\n", (void *)shared_stack);
  32. printf("shared_stack expected top: %p\n", (void *)(shared_stack + SHARED_STACK_SIZE));
  33. esp_execute_shared_stack_function(printf_lock,
  34. shared_stack,
  35. SHARED_STACK_SIZE,
  36. external_stack_function);
  37. TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
  38. (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
  39. esp_execute_shared_stack_function(printf_lock,
  40. shared_stack,
  41. SHARED_STACK_SIZE,
  42. another_external_stack_function);
  43. TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
  44. (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
  45. vSemaphoreDelete(printf_lock);
  46. free(shared_stack);
  47. }