ESP32: Optimizing Heap and Stack Allocation

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
cversek
Posts: 13
Joined: Sat Jan 07, 2017 4:45 am

ESP32: Optimizing Heap and Stack Allocation

Post by cversek » Fri Feb 24, 2017 7:04 am

Hi, just got my ESP32 WROOM32 dev board from Adafruit. I noticed that both the filesystem size (256kB) (https://github.com/micropython/micropyt ... dev.py#L34) and the RAM allocation (https://github.com/micropython/micropyt ... main.c#L53) are currently pegged at a lower than where these boards should be capable. It was easy enough to increase the filesystem size by tweaking that line, and I was able to increase the heap size to 128kB, but in trying to allocate a 256kB heap, I caused a panic:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (Double exception)
Register dump:
PC      : 0x40007d0c  PS      : 0x00060f36  A0      : 0x00000000  A1      : 0x3ffe3df0  
A2      : 0x3ffe3e60  A3      : 0x00000000  A4      : 0x000008e6  A5      : 0x000008e6  
A6      : 0x00002aaa  A7      : 0x3ffe3e90  A8      : 0x80007c34  A9      : 0x00000009  
A10     : 0x3ff0005c  A11     : 0x40006840  A12     : 0x3f400000  A13     : 0x00020000  
A14     : 0x00000aaa  A15     : 0x3ffe3e50  SAR     : 0x0000001f  EXCCAUSE: 0x00000002  
EXCVADDR: 0x00000008  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0xffffffff  

Backtrace: 0x40007d0c:0x3ffe3df0 0x00000000:0x3ffe3eb0 0x40007d0c:0x3ffe3bd0 0x40007c6c:0x3ffe3bf0 0x4000814b:0x3ffe3c10 0x400f5ec8:0x3ffe3ca0 0x40080fa7:0x3f410f8c

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:2372
load:0x40078000,len:6432
ho 0 tail 12 room 4
load:0x40080000,len:252
entry 0x40080034
So how do I figure out how much of the 512kB of SRAM can be allocated to the micropython task, including stack space? Are there any other figures that need adjusting, like in the linker script? Maybe this is gratuitous, but why not?

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: ESP32: Optimizing Heap and Stack Allocation

Post by Damien » Fri Feb 24, 2017 10:20 pm

Unfortunately a lot of the "512k" RAM of the esp32 is not available for the user's app. To start with, there's only 320k of actual DRAM to store stack, data and bss (the rest seems to be used for caching of flash). If you have bluetooth enabled (MicroPython doesn't, yet) then that uses 64k of that DRAM. There are a few FreeRTOS threads that are running to control the wifi and interrupts and they all need a stack, roughly 2k-4k in size. Then there are all the wifi buffers that are allocated on the FreeRTOS heap.

The port is still work in progress and it's not certain at this point how much free RAM there will be for the uPy heap. But I wouldn't be surprised if it was around 128k.

Post Reply