From 3e9637a893e4462937abca5e5e1ae83c390dac00 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Tue, 31 Mar 2020 13:58:02 -0300 Subject: [PATCH] test/shared_stack_printf: improved printf with shared stack function test --- .../newlib/test/test_shared_stack_printf.c | 34 ++++++++++++++++--- .../xtensa/expression_with_stack_xtensa_asm.S | 20 +++++++---- docs/en/api-reference/system/index.rst | 2 +- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/components/newlib/test/test_shared_stack_printf.c b/components/newlib/test/test_shared_stack_printf.c index 81997a5a9a..f81d27d78d 100644 --- a/components/newlib/test/test_shared_stack_printf.c +++ b/components/newlib/test/test_shared_stack_printf.c @@ -8,25 +8,49 @@ #include "test_utils.h" #include "esp_expression_with_stack.h" -//makes sure this is not the task stack... +#define SHARED_STACK_SIZE 8192 + +static StackType_t *shared_stack_sp = NULL; + +void external_stack_function(void) +{ + printf("Executing this printf from external stack! sp=%p\n", get_sp()); + shared_stack_sp = (StackType_t *)get_sp(); +} + void another_external_stack_function(void) { //We can even use Freertos resources inside of this context. vTaskDelay(100); - printf("Executing this another printf inside a function with external stack"); + printf("Done!, sp=%p\n", get_sp()); + shared_stack_sp = (StackType_t *)get_sp(); } TEST_CASE("test printf using shared buffer stack", "[newlib]") { - portSTACK_TYPE *shared_stack = malloc(8192 * sizeof(portSTACK_TYPE)); + portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE); TEST_ASSERT(shared_stack != NULL); SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex(); TEST_ASSERT_NOT_NULL(printf_lock); - ESP_EXECUTE_EXPRESSION_WITH_STACK(printf_lock, shared_stack,8192,printf("Executing this printf from external stack! \n")); - ESP_EXECUTE_EXPRESSION_WITH_STACK(printf_lock, shared_stack,8192,another_external_stack_function()); + esp_execute_shared_stack_function(printf_lock, + shared_stack, + SHARED_STACK_SIZE, + external_stack_function); + + TEST_ASSERT(((shared_stack_sp >= shared_stack_sp) && + (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE)))); + + esp_execute_shared_stack_function(printf_lock, + shared_stack, + SHARED_STACK_SIZE, + another_external_stack_function); + + TEST_ASSERT(((shared_stack_sp >= shared_stack_sp) && + (shared_stack_sp < (shared_stack + SHARED_STACK_SIZE)))); + vSemaphoreDelete(printf_lock); free(shared_stack); } diff --git a/components/xtensa/expression_with_stack_xtensa_asm.S b/components/xtensa/expression_with_stack_xtensa_asm.S index b01cc5280d..acd7496f91 100644 --- a/components/xtensa/expression_with_stack_xtensa_asm.S +++ b/components/xtensa/expression_with_stack_xtensa_asm.S @@ -45,12 +45,20 @@ esp_switch_stack_enter: esp_switch_stack_exit: #ifndef __XTENSA_CALL0_ABI__ - entry sp, 0x10 - - l32i a4, a2, 0 /* recover the original task stack */ - mov a1, a4 /* put it on sp register again */ - retw - + movi a0, 0 /* no need to rotate window, it will be destroyed anyway */ + movi a6, shared_stack + l32i sp, a6, 0 /* load shared stack pointer */ + movi a12, shared_stack_callback + l32i a12, a12, 0 + callx4 a12 /* call user function */ + movi a6, shared_stack_function_done + movi a7, 1 + s32i a7, a6, 0 /* hint the function was finished */ + movi a6, shared_stack_env + movi a7, 0 + movi a12, longjmp + callx4 a12 /* jump to last clean state previously saved */ + ret #else #error "this code is written for Window ABI" #endif diff --git a/docs/en/api-reference/system/index.rst b/docs/en/api-reference/system/index.rst index 5cd0db3b62..7fd3ea5f16 100644 --- a/docs/en/api-reference/system/index.rst +++ b/docs/en/api-reference/system/index.rst @@ -19,7 +19,7 @@ System API High Resolution Timer :esp32: Himem (large external SPI RAM) API :esp32: Inter-Processor Call - Call function with external stack + Call function with external stack Interrupt Allocation Logging Miscellaneous System APIs