From 57edda15a0dbff375f1a6f023170061964151cb6 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Mon, 6 Jan 2020 17:28:37 -0300 Subject: [PATCH] shared_stack: added minimal stack size for shared stack, configurable via menuconfig --- components/esp_common/Kconfig | 7 ++++ .../include/esp_expression_with_stack.h | 37 +++++++++++-------- .../newlib/test/test_shared_stack_printf.c | 2 + 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/components/esp_common/Kconfig b/components/esp_common/Kconfig index bbed1dca1e..d236d804fa 100644 --- a/components/esp_common/Kconfig +++ b/components/esp_common/Kconfig @@ -77,6 +77,13 @@ menu "Common ESP-related" FreeRTOS timer task size, see "FreeRTOS timer task stack size" option in "FreeRTOS" menu. + config ESP_MINIMAL_SHARED_STACK_SIZE + int "Minimal allowed size for shared stack" + default 2048 + help + Minimal value of size, in bytes, accepted to execute a expression + with shared stack. + choice ESP_CONSOLE_UART prompt "UART for console output" default ESP_CONSOLE_UART_DEFAULT diff --git a/components/esp_common/include/esp_expression_with_stack.h b/components/esp_common/include/esp_expression_with_stack.h index 3aa12a2538..cd1e4765a8 100644 --- a/components/esp_common/include/esp_expression_with_stack.h +++ b/components/esp_common/include/esp_expression_with_stack.h @@ -11,14 +11,16 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - -#ifndef __ESP_EXPRESSION_WITH_STACK_H -#define __ESP_EXPRESSION_WITH_STACK_H +#pragma once #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "esp_debug_helpers.h" +#include "esp_log.h" +#ifdef __cplusplus +extern "C" { +#endif /** * @brief Executes a 1-line expression with a application alocated stack @@ -26,20 +28,21 @@ * @param stack Pointer to user alocated stack * @param stack_size Size of current stack in bytes * @param expression Expression or function to be executed using the stack + * @note if either lock, stack or stack size is invalid, the expression will + * be called using the current stack. */ -#define ESP_EXECUTE_EXPRESSION_WITH_STACK(lock, stack, stack_size, expression) \ -({ \ - if (lock && stack && stack_size) { \ - uint32_t backup; \ - xSemaphoreTake(lock, portMAX_DELAY); \ - StackType_t *top_of_stack = esp_switch_stack_setup(stack, stack_size);\ - esp_switch_stack_enter(top_of_stack, &backup); \ - { \ - expression; \ - } \ - esp_switch_stack_exit(&backup); \ - xSemaphoreGive(lock); \ - } \ +#define ESP_EXECUTE_EXPRESSION_WITH_STACK(lock, stack, stack_size, expression) \ +({ \ + assert(lock && stack && (stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE)); \ + uint32_t backup; \ + xSemaphoreTake(lock, portMAX_DELAY); \ + StackType_t *top_of_stack = esp_switch_stack_setup(stack, stack_size); \ + esp_switch_stack_enter(top_of_stack, &backup); \ + { \ + expression; \ + } \ + esp_switch_stack_exit(&backup); \ + xSemaphoreGive(lock); \ }) /** @@ -66,4 +69,6 @@ extern void esp_switch_stack_enter(StackType_t *stack, uint32_t *backup_stack); */ extern void esp_switch_stack_exit(uint32_t *backup_stack); +#ifdef __cplusplus +} #endif \ No newline at end of file diff --git a/components/newlib/test/test_shared_stack_printf.c b/components/newlib/test/test_shared_stack_printf.c index 9b020f5815..81997a5a9a 100644 --- a/components/newlib/test/test_shared_stack_printf.c +++ b/components/newlib/test/test_shared_stack_printf.c @@ -1,4 +1,5 @@ #include +#include #include "unity.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -29,3 +30,4 @@ TEST_CASE("test printf using shared buffer stack", "[newlib]") vSemaphoreDelete(printf_lock); free(shared_stack); } +