Meaning of ICACHE_FLASH_ATTR
-
- Posts: 28
- Joined: Tue Mar 31, 2015 7:27 pm
Meaning of ICACHE_FLASH_ATTR
Postby geo.espressif » Fri Oct 02, 2015 4:55 pm
Re: Meaning of ICACHE_FLASH_ATTR
Postby eriksl » Fri Oct 02, 2015 5:32 pm
-
- Posts: 28
- Joined: Tue Mar 31, 2015 7:27 pm
Re: Meaning of ICACHE_FLASH_ATTR
Postby geo.espressif » Fri Oct 02, 2015 5:41 pm
Any rough ideas? I have no idea and I've been using it roughly at random. Something to do with the memory the function is compiled to / speed of calling?
Re: Meaning of ICACHE_FLASH_ATTR
Postby eriksl » Fri Oct 02, 2015 5:55 pm
The esp8266 has 32k (maybe 64k, that's not completely clear) of IRAM. That means RAM for instructions, code. This is different from the ~80k of DRAM (which means data ram, not dynamic ram, it's all dynamic ram, the iram too). One cannot be use for the other and v.v.
Programs are stored in the flash memory and due to the fast interface (SPI-quad) instructions can be fetched and executed "in place", in other words, without copying them to ram first. In some occasions you cannot execute your code from SPI flash memory directly, mostly because it needs to be maximum fast or because it handles the flash memory itself (e.g. writing to it). In that case, you can have some code, 32k in total, have copied from the flash at startup, to the IRAM. This code will run from IRAM, not from FLASH.
The forementioned #define, which has an ugly, completely wrong name, if applied to a function, takes care that the function is output to the .irom0_text section, which tells the linker to put the function in FLASH memory segment. The default is to output to .text segment which will put the function into the IRAM segment.
A lot of the SDK-code, which you must link to your firmware to have it working, also output their code to the IRAM segment. So there is actually not a lot left, from the 32k for your own application, about 2k, maybe 3k. So use it very sparingly. Also many functions if not all, from libc, are placed into the IRAM segment. So it is very easy to run out of IRAM space. Even though you may have 512k or even 4m of FLASH memory (depening on the chip used). I had to reimplement some functions from libc in my own code, simply because they take up too much space in IRAM.
So always, unless impossible, add this #define to all of your functions.
I think that should cover most of it.
Re: Meaning of ICACHE_FLASH_ATTR
Postby kolban » Fri Oct 02, 2015 8:38 pm

One technique I have seen used is to not include the ICACHE_FLASH_ATTR modification of each of the source functions ... but instead, when the compilation of a .c to a .o has completed, rename the .text section to be .irom0.text
For example ...:
objcopy --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal myObjectFile.o
Do you have any thoughts on the wisdom of that notion?
Neil
-
- Posts: 28
- Joined: Tue Mar 31, 2015 7:27 pm
Re: Meaning of ICACHE_FLASH_ATTR
Postby geo.espressif » Fri Oct 02, 2015 8:40 pm
Re: Meaning of ICACHE_FLASH_ATTR
Postby eriksl » Fri Oct 02, 2015 8:54 pm
This is what I did: forget about the whole ICACHE_FLASH_ATTR thing and #define my own, similar replacement, with the simple name "irom" which exactly describes what it does and also is so much easier to type.
The other way around, default everything in IROM and only exceptions in IRAM would be even better, but I appreciate that that is not possible because then you would need to scan ALL library sources (including libc) for functions that possibly might need to end up in IRAM. This decision is non-trivial, it depends on what code calls the function.
Re: Meaning of ICACHE_FLASH_ATTR
Postby dkinzer » Fri Oct 02, 2015 10:41 pm
For those few functions that need to be in IRAM (e.g. interrupt service handlers), use a different attribute that puts them in the .iram0 section.eriksl wrote:Neil, that's also a good method. Doesn't work for me though, because I always have a few functions that need to be in IRAM.
Code: Select all
#define IRAM0 __attribute__((section(".iram0.text")))
void IRAM0 myISR(void)
{
}
Code: Select all
objcopy --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal myObjectFile.o
It should also be noted how the SDK include files define ICACHE_FLASH_ATTR (in c_types.h):
Code: Select all
#ifdef ICACHE_FLASH
#define ICACHE_FLASH_ATTR __attribute__((section(".irom0.text")))
#define ICACHE_RODATA_ATTR __attribute__((section(".irom.text")))
#else
#define ICACHE_FLASH_ATTR
#define ICACHE_RODATA_ATTR
#endif /* ICACHE_FLASH */
Another useful technique can be found in the ESP8266-Arduino linker script. The excerpt below comes from esp8266/Arduino on GitHub:
Code: Select all
.irom0.text : ALIGN(4)
{
_irom0_text_start = ABSOLUTE(.);
*core_esp8266_*.o(.literal*, .text*)
*spiffs*.o(.literal*, .text*)
*.cpp.o(.literal*, .text*)
*libm.a:(.literal .text .literal.* .text.*)
*libsmartconfig.a:(.literal .text .literal.* .text.*)
*(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text)
_irom0_text_end = ABSOLUTE(.);
_flash_code_end = ABSOLUTE(.);
} >irom0_0_seg :irom0_0_phdr
Note, particularly, the fourth and seventh lines. The fourth line causes all of the core esp8266/Arduino code .literal and .text sections to be put in .irom0.text. This accomplishes exactly the same purpose as does using objcopy to rename the sections. (The fifth line does the same for SPIFFS code.) The seventh line causes all .literal and .text sections in the libm.a to be put in .irom0.text. This works around a problem where using sqrt() in an application would cause the cache RAM size to be exceeded.
Beaverton, OR, USA
Re: Meaning of ICACHE_FLASH_ATTR
Postby eriksl » Fri Oct 02, 2015 11:19 pm
Re: Meaning of ICACHE_FLASH_ATTR
Postby dkinzer » Sat Oct 03, 2015 5:54 am
What were the circumstances? What ISR and what function(s)?eriksl wrote:You never know what IRAM-bound functions (including in the SDK!) call these functions and cause unexpected crashes. I've been there.
Beaverton, OR, USA
Who is online
Users browsing this forum: No registered users and 284 guests
Login
Newbies Start Here
Are you new to ESP8266?
Unsure what to do?
Dunno where to start?
Start right here!
Latest SDK
Documentation
Complete listing of the official ESP8266 related documentation release by ESPRESSIF!
Must read here!
- All times are UTC+08:00
- Top
- Delete all board cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. We are the manufacturer of ESP8266EX.