test_shared_stack_printf.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 delaying..., sp=%p\n", get_sp());
  21. vTaskDelay(100);
  22. printf("Done!, sp=%p\n", get_sp());
  23. shared_stack_sp = (StackType_t *)get_sp();
  24. }
  25. TEST_CASE("test printf using shared buffer stack", "[newlib]")
  26. {
  27. portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE);
  28. TEST_ASSERT(shared_stack != NULL);
  29. SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
  30. TEST_ASSERT_NOT_NULL(printf_lock);
  31. printf("SP: %p\n", get_sp());
  32. printf("shared_stack: %p\n", (void *)shared_stack);
  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_sp) &&
  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_sp) &&
  44. (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
  45. vSemaphoreDelete(printf_lock);
  46. free(shared_stack);
  47. }