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", esp_cpu_get_sp());
18
19 shared_stack_sp = (StackType_t *)esp_cpu_get_sp();
20
21 char *res = NULL;
22 /* Test return value from asprintf, this could potentially help catch a misaligned
23 stack pointer error */
24 asprintf(&res, "%d %011i %lu %p %x %c %.4f\n", 42, 2147483647, 2147483648UL, (void *) 0x40010000, 0x40020000, 'Q', 1.0f / 137.0f);
25 TEST_ASSERT_NOT_NULL(res);
26 TEST_ASSERT_EQUAL_STRING("42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n", res);
27 free(res);
28 }
29
another_external_stack_function(void)30 void another_external_stack_function(void)
31 {
32 //We can even use Freertos resources inside of this context.
33 printf("We can even use FreeRTOS resources... yielding, sp=%p\n", esp_cpu_get_sp());
34 taskYIELD();
35 shared_stack_sp = (StackType_t *)esp_cpu_get_sp();
36 }
37
38 TEST_CASE("test printf using shared buffer stack", "[newlib]")
39 {
40 portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE);
41
42 TEST_ASSERT_NOT_NULL(shared_stack);
43
44 SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
45 TEST_ASSERT_NOT_NULL(printf_lock);
46 printf("current task sp: %p\n", esp_cpu_get_sp());
47 printf("shared_stack: %p\n", (void *)shared_stack);
48 printf("shared_stack expected top: %p\n", (void *)(shared_stack + SHARED_STACK_SIZE));
49
50
51 esp_execute_shared_stack_function(printf_lock,
52 shared_stack,
53 SHARED_STACK_SIZE,
54 external_stack_function);
55
56 TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
57 (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
58
59 esp_execute_shared_stack_function(printf_lock,
60 shared_stack,
61 SHARED_STACK_SIZE,
62 another_external_stack_function);
63
64 TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
65 (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
66
67 vSemaphoreDelete(printf_lock);
68 free(shared_stack);
69 }
70