From 161982b53e91125c858b1c2bf6bf78ba935c7e16 Mon Sep 17 00:00:00 2001 From: Gary Ott Date: Tue, 14 Jul 2020 11:09:05 +0100 Subject: [PATCH] Added code to jsuGetFreeStack() for ESP32 and introduced ESP_STACK_SIZE constant. --- boards/ESP32.py | 1 + src/jsutils.c | 14 ++++++++++++++ src/jsutils.h | 4 ++++ targets/esp32/main.c | 14 ++++++++++++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/boards/ESP32.py b/boards/ESP32.py index 5ed735df19..a693e0879b 100755 --- a/boards/ESP32.py +++ b/boards/ESP32.py @@ -37,6 +37,7 @@ ], 'makefile' : [ 'DEFINES+=-DESP_PLATFORM -DESP32=1', + 'DEFINES+=-DESP_STACK_SIZE=25000', 'DEFINES+=-DJSVAR_MALLOC', # Allocate space for variables at jsvInit time 'ESP32_FLASH_MAX=1572864' ] diff --git a/src/jsutils.c b/src/jsutils.c index c59e270d33..1cd568f9ec 100644 --- a/src/jsutils.c +++ b/src/jsutils.c @@ -869,6 +869,20 @@ size_t jsuGetFreeStack() { const uint32_t max_stack = 1000000; // give it 1 megabyte of stack if (count>max_stack) return 0; return max_stack - count; +#elif defined(ESP32) + char ptr; // this is on the stack + + //RTOS task stacks work the opposite way to what you may expect. + //Early entries are in higher memory locations. + //Later entries are in lower memory locations. + + + uint32_t stackPos = (uint32_t)&ptr; + uint32_t stackStart = (uint32_t)espruino_stackHighPtr - ESP_STACK_SIZE; + + if (stackPos < stackStart) return 0; // should never happen, but just in case of overflow! + + return stackPos - stackStart; #else // stack depth seems pretty platform-specific :( Default to a value that disables it return 1000000; // no stack depth check on this platform diff --git a/src/jsutils.h b/src/jsutils.h index 79057b2535..e3fba7614b 100755 --- a/src/jsutils.h +++ b/src/jsutils.h @@ -530,4 +530,8 @@ void srand(unsigned int seed); /** get the amount of free stack we have, in bytes */ size_t jsuGetFreeStack(); +#ifdef ESP32 + void *espruino_stackHighPtr; //Used by jsuGetFreeStack +#endif + #endif /* JSUTILS_H_ */ diff --git a/targets/esp32/main.c b/targets/esp32/main.c index c0e9e55bcc..598d7ce708 100644 --- a/targets/esp32/main.c +++ b/targets/esp32/main.c @@ -30,6 +30,8 @@ #include "jsvar.h" +extern void *espruino_stackHighPtr; //Name spaced because this has to be a global variable. + //Used in jsuGetFreeStack(). extern void initialise_wifi(void); @@ -43,6 +45,14 @@ static void uartTask(void *data) { static void espruinoTask(void *data) { int heapVars; + +#ifdef ESP32 + espruino_stackHighPtr = &heapVars; //Ignore the name, 'heapVars' is on the stack! + //I didn't use another variable becaue this function never ends so + //all variables declared here consume stack space that is never freed. +#endif + + PWMInit(); RMTInit(); SPIChannelsInit(); @@ -101,10 +111,10 @@ int app_main(void) #ifdef RTOS queues_init(); tasks_init(); - task_init(espruinoTask,"EspruinoTask",25000,5,0); + task_init(espruinoTask,"EspruinoTask", ESP_STACK_SIZE, 5, 0); task_init(uartTask,"ConsoleTask",2200,20,0); #else - xTaskCreatePinnedToCore(&espruinoTask, "espruinoTask", 25000, NULL, 5, NULL, 0); + xTaskCreatePinnedToCore(&espruinoTask, "espruinoTask", ESP_STACK_SIZE, NULL, 5, NULL, 0); xTaskCreatePinnedToCore(&uartTask,"uartTask",2200,NULL,20,NULL,0); #endif return 0;