Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: Take advantage of JsVars being smaller. #2298

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion boards/ESP32.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,44 @@
# as various source and header files for Espruino.
# ----------------------------------------------------------------------------------------



# A Note about the 'variables' parameter on ESP32 Builds
# ------------------------------------------------------
#
# For the ESP32 build, the number of JsVars is governed by two factors:
# * Available memory
# * Maximum number of JsVars for the used JsVar format
#
# This setting will chose the optimum JsVar format for a given number
# of JsVars.

# If you add PSRAM to your ESP32 or compile with modules removed, you
# may wish to select a value using this table:
#
# Value | Max JsVars | Bytes per JsVar | Maximum References |
# ------+--------------+-----------------+--------------------+
# 4095 | 4095 | 13 | 255 |
# 8191 | 8191 | 13 | 15 |
# 16383 | 16383 | 14 | 255 |
# 65535 | 65535 | 16 | 255 |
# ------+--------------+-----------------+--------------------+

# CAUTION: Chosing 8191 only allows 15 references to a variable. This
# may be too restrictive to run some code.

# Using too large a JsVar format may limit how many JsVars can fit into
# available memory. Using too small a JsVar format will under utilise
# available memory.


import pinutils;
info = {
'name' : "ESP32",
'espruino_page_link' : 'ESP32',
'default_console' : "EV_SERIAL1",
'default_console_baudrate' : "115200",
'variables' : 2500, # JSVAR_MALLOC is defined below - so this can vary depending on what is initialised
'variables' : 4095, # See note above
'binary_name' : 'espruino_%v_esp32.bin',
'build' : {
'optimizeflags' : '-Og',
Expand Down
5 changes: 5 additions & 0 deletions src/jsutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ See comments after JsVar in jsvar.c for more info.
#define JSVARREFCOUNT_BITS 4 // 56 - 13*4
typedef uint16_t JsVarRef;
typedef int16_t JsVarRefSigned;
#elif JSVAR_CACHE_SIZE <= 16383 // 14 bytes
#define JSVARREF_BITS 14
#define JSVARREFCOUNT_BITS 8 // 64 - 13*4
typedef uint16_t JsVarRef;
typedef int16_t JsVarRefSigned;
#elif JSVAR_CACHE_SIZE <= 65535 // 16 bytes
#define JSVARREF_BITS 16
#define JSVARREFCOUNT_BITS 8
Expand Down
27 changes: 18 additions & 9 deletions targets/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,32 @@ 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

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.

PWMInit();
RMTInit();
SPIChannelsInit();
initADC(1);
jshInit(); // Initialize the hardware
jswHWInit();
heapVars = (esp_get_free_heap_size() - 40000) / 16; //calculate space for jsVars
heapVars = heapVars - heapVars % 100; //round to 100
if(heapVars > 20000) heapVars = 20000; //WROVER boards have much more RAM, so we set a limit


heapVars = (esp_get_free_heap_size() - 40000) / sizeof(JsVar); //calculate space for jsVars

//Limit number of JsVars to maximum addressable. Can otherwise be
//breached by builds with modules removed or boards using PSRAM.
{
int maxVars = (1 << JSVARREF_BITS) - 1;

if (heapVars > maxVars) {
heapVars = maxVars;
}
}

jsvInit(heapVars); // Initialize the variables

// not sure why this delay is needed?
vTaskDelay(200 / portTICK_PERIOD_MS);
jsiInit(true); // Initialize the interactive subsystem
Expand Down