Iram1_0_seg overflow with native decorator

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
kevinkk525
Posts: 864
Joined: Sat Feb 03, 2018 7:02 pm

Iram1_0_seg overflow with native decorator

Post by kevinkk525 » Wed Aug 12, 2020 5:20 am

I have a problem building my esp8266 firmware:

Code: Select all

LINK build-pysmartnode_4M/firmware.elf
xtensa-lx106-elf-ld: build-pysmartnode_4M/firmware.elf section `.text' will not fit in region `iram1_0_seg'
xtensa-lx106-elf-ld: region `iram1_0_seg' overflowed by 764 bytes
make: *** [Makefile:216: build-pysmartnode_4M/firmware.elf] Error 1
Whenever I put some more code in @micropython.native, the iram1_0_seg overflows. Looks to me like @native code gets stuffed into iram1_0_seg while non native code all ends up in iram0_0_seg. Sadly my iram1_0_seg is already over 32K.

So I changed the iram1_0_seg in esp8266_2m.ld to 36K (was 32K originally) and reduzed the irom0_0_seg by 4K:

Code: Select all

MEMORY
{
    dport0_0_seg : org = 0x3ff00000, len = 16
    dram0_0_seg :  org = 0x3ffe8000, len = 80K
    iram1_0_seg :  org = 0x40100000, len = 32K+4K
    irom0_0_seg :  org = 0x40209000, len = 1M - 36K-4K
}
It builds fine but the esp just keeps constantly rebooting..
How do I fix this properly? How do I add more space to iram1_0_seg so I can add more native code?
Last edited by kevinkk525 on Wed Aug 12, 2020 3:32 pm, edited 1 time in total.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
rcolistete
Posts: 336
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Iram1_0_seg overflow

Post by rcolistete » Wed Aug 12, 2020 1:52 pm

`iram1_0_seg' is "Instruction RAM", about 4-10x faster than IROM. Used only to run code chosen from ROM when booting, but is almost full with important pieces of the MicroPython firmware.
Some links about iram1 and ESP8266 memory :
https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map
http://cholla.mmto.org/esp8266/where.html
http://chelahmy.blogspot.com/2018/10/es ... n-sdk.html
https://www.kickstarter.com/projects/21 ... ts/1501224

How to move modules from IRAM to IROM ?
* use ICACHE_FLASH_ATTR before each function;
* or add the name of the modules to file "micropython/ports/esp8266/boards/esp8266_common.ld". See :
viewtopic.php?f=16&t=8767&p=49505#p49505
For ulab module, it is enough to add :

Code: Select all

        *code/*.o*(.literal* .text*)   /* ulab module */
in line 183, after :

Code: Select all

        */frozen.o(.rodata.mp_frozen_content) /* frozen modules */
Last edited by rcolistete on Wed Aug 12, 2020 5:23 pm, edited 1 time in total.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

User avatar
rcolistete
Posts: 336
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Iram1_0_seg overflow

Post by rcolistete » Wed Aug 12, 2020 2:02 pm

Sorry, I think my answer is not a solution for your case with @micropython.native decorator (but it is for native C user/external modules).

IRAM size is fixed, AFAIK. So there is maximum size of code using decorators. But try to use "esp.set_native_code_location(start, length)".
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

kevinkk525
Posts: 864
Joined: Sat Feb 03, 2018 7:02 pm

Re: Iram1_0_seg overflow with native decorator

Post by kevinkk525 » Wed Aug 12, 2020 3:32 pm

Thanks a lot. Now I understand the problem a lot better.
Your solution however only seems to work when I define a native function during runtime, whereas my functions are to be frozen into the firmware. But as I understand it, even frozen native functions are being recompiled on each boot and therefore in iRAM? Seems strange to be as I would expect the cross compilation to store the native code in the firmware so there is no need to recompile it during runtime and it could therefore be stored in the flash/irom.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
rcolistete
Posts: 336
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Iram1_0_seg overflow with native decorator

Post by rcolistete » Wed Aug 12, 2020 5:21 pm

See line 183 above : frozen modules are kept in IROM.

But running @micropython.native decorator needs IRAM if you have not configured "esp.set_native_code_location(start, length)" to use IROM.

I don't have any experience with "esp.set_native_code_location(start, length)", so it is better to have opinion from other MicroPython developers/advanced users.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

User avatar
Roberthh
Posts: 2438
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Iram1_0_seg overflow with native decorator

Post by Roberthh » Wed Aug 12, 2020 6:41 pm

> I don't have any experience with "esp.set_native_code_location(start, length)", so it is better to have opinion from other MicroPython developers/advanced users.

I tried that once, but did not use it for long. Two reasons:

a) flash is written whenever the code is imported. If you start your device rarely, that is fine. Otherwise you have high flash wear, even if that is not the biggest risk. The same happens with the file system, and I never had a worn flash until now. And even then, you just have to swap the flash chip (~1 USD costs). But at the price of ESP8266 modules, that is not economical (but maybe better for the environment: repair instead of trashing).
b) the code resides in flash and is subject to cache misses, different to IRAM. So if you need reliable fast response, IRAM should be used.

Post Reply