2019-11-15 08:07:57 +00:00
|
|
|
#include <stdio.h>
|
2020-01-06 20:28:37 +00:00
|
|
|
#include <string.h>
|
2019-11-15 08:07:57 +00:00
|
|
|
#include "unity.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "test_utils.h"
|
|
|
|
#include "esp_expression_with_stack.h"
|
|
|
|
|
2020-03-31 16:58:02 +00:00
|
|
|
#define SHARED_STACK_SIZE 8192
|
|
|
|
|
2020-11-10 07:40:01 +00:00
|
|
|
static StackType_t *shared_stack_sp = NULL;
|
2020-03-31 16:58:02 +00:00
|
|
|
|
2020-03-12 05:59:53 +00:00
|
|
|
void external_stack_function(void)
|
|
|
|
{
|
2021-02-19 12:23:32 +00:00
|
|
|
printf("Executing this printf from external stack! sp=%p\n", esp_cpu_get_sp());
|
2021-02-19 03:26:21 +00:00
|
|
|
|
2021-02-19 12:23:32 +00:00
|
|
|
shared_stack_sp = (StackType_t *)esp_cpu_get_sp();
|
2021-02-19 03:26:21 +00:00
|
|
|
|
|
|
|
char *res = NULL;
|
|
|
|
/* Test return value from asprintf, this could potentially help catch a misaligned
|
|
|
|
stack pointer error */
|
|
|
|
asprintf(&res, "%d %011i %lu %p %x %c %.4f\n", 42, 2147483647, 2147483648UL, (void *) 0x40010000, 0x40020000, 'Q', 1.0f / 137.0f);
|
|
|
|
TEST_ASSERT_NOT_NULL(res);
|
|
|
|
TEST_ASSERT_EQUAL_STRING("42 02147483647 2147483648 0x40010000 40020000 Q 0.0073\n", res);
|
|
|
|
free(res);
|
2020-03-12 05:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 07:40:01 +00:00
|
|
|
void another_external_stack_function(void)
|
2019-11-15 08:07:57 +00:00
|
|
|
{
|
2019-12-04 14:25:02 +00:00
|
|
|
//We can even use Freertos resources inside of this context.
|
2021-02-19 12:23:32 +00:00
|
|
|
printf("We can even use FreeRTOS resources... yielding, sp=%p\n", esp_cpu_get_sp());
|
2020-04-01 15:10:13 +00:00
|
|
|
taskYIELD();
|
2021-02-19 12:23:32 +00:00
|
|
|
shared_stack_sp = (StackType_t *)esp_cpu_get_sp();
|
2019-11-15 08:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("test printf using shared buffer stack", "[newlib]")
|
|
|
|
{
|
2020-03-31 16:58:02 +00:00
|
|
|
portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE);
|
2019-11-15 08:45:18 +00:00
|
|
|
|
2020-04-01 15:10:13 +00:00
|
|
|
TEST_ASSERT_NOT_NULL(shared_stack);
|
2019-11-15 08:45:18 +00:00
|
|
|
|
2019-11-15 08:07:57 +00:00
|
|
|
SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
|
2019-12-20 16:27:30 +00:00
|
|
|
TEST_ASSERT_NOT_NULL(printf_lock);
|
2021-02-19 12:23:32 +00:00
|
|
|
printf("current task sp: %p\n", esp_cpu_get_sp());
|
2020-03-12 05:59:53 +00:00
|
|
|
printf("shared_stack: %p\n", (void *)shared_stack);
|
2020-04-01 15:10:13 +00:00
|
|
|
printf("shared_stack expected top: %p\n", (void *)(shared_stack + SHARED_STACK_SIZE));
|
|
|
|
|
2019-12-20 16:27:30 +00:00
|
|
|
|
2020-11-10 07:40:01 +00:00
|
|
|
esp_execute_shared_stack_function(printf_lock,
|
2020-03-31 16:58:02 +00:00
|
|
|
shared_stack,
|
|
|
|
SHARED_STACK_SIZE,
|
|
|
|
external_stack_function);
|
2020-11-10 07:40:01 +00:00
|
|
|
|
|
|
|
TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
|
2020-03-31 16:58:02 +00:00
|
|
|
(shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
|
2020-11-10 07:40:01 +00:00
|
|
|
|
|
|
|
esp_execute_shared_stack_function(printf_lock,
|
2020-03-31 16:58:02 +00:00
|
|
|
shared_stack,
|
|
|
|
SHARED_STACK_SIZE,
|
2020-11-10 07:40:01 +00:00
|
|
|
another_external_stack_function);
|
|
|
|
|
|
|
|
TEST_ASSERT(((shared_stack_sp >= shared_stack) &&
|
2020-03-31 16:58:02 +00:00
|
|
|
(shared_stack_sp < (shared_stack + SHARED_STACK_SIZE))));
|
|
|
|
|
2020-11-10 07:40:01 +00:00
|
|
|
vSemaphoreDelete(printf_lock);
|
2019-11-15 08:45:18 +00:00
|
|
|
free(shared_stack);
|
2019-11-15 08:07:57 +00:00
|
|
|
}
|