Skip to content

Commit

Permalink
Merge pull request #1882 from GaryOtt/ESP32
Browse files Browse the repository at this point in the history
Added code to jsuGetFreeStack() for ESP32 and introduced ESP_STACK_SI…
  • Loading branch information
gfwilliams authored Jul 14, 2020
2 parents 0408288 + 161982b commit aaf3080
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions boards/ESP32.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
Expand Down
14 changes: 14 additions & 0 deletions src/jsutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/jsutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
14 changes: 12 additions & 2 deletions targets/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit aaf3080

Please sign in to comment.