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
11 #define SHARED_STACK_SIZE 8192
12
13 static StackType_t *shared_stack_sp = NULL;
14
external_stack_function(void)15 void external_stack_function(void)
16 {
17 printf("Executing this printf from external stack! sp=%p\n", get_sp());
18 shared_stack_sp = (StackType_t *)get_sp();
19 }
20
another_external_stack_function(void)21 void another_external_stack_function(void)
22 {
23 //We can even use Freertos resources inside of this context.
24 printf("We can even use FreeRTOS resources... yielding, sp=%p\n", get_sp());
25 taskYIELD();
26 shared_stack_sp = (StackType_t *)get_sp();
27 }
28
29 TEST_CASE("test printf using shared buffer stack", "[newlib]")
30 {
31 portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE);
32
33 TEST_ASSERT_NOT_NULL(shared_stack);
34
35 SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
36 TEST_ASSERT_NOT_NULL(printf_lock);
37 printf("current task sp: %p\n", get_sp());
38 printf("shared_stack: %p\n", (void *)shared_stack);
39 printf("shared_stack expected top: %p\n", (void *)(shared_stack + SHARED_STACK_SIZE));
40
41
42 esp_execute_shared_stack_function(printf_lock,
43 shared_stack,
44 SHARED_STACK_SIZE,
45 external_stack_function);
46
47 TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
48 (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
49
50 esp_execute_shared_stack_function(printf_lock,
51 shared_stack,
52 SHARED_STACK_SIZE,
53 another_external_stack_function);
54
55 TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
56 (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
57
58 vSemaphoreDelete(printf_lock);
59 free(shared_stack);
60 }
61