-->
Page 1 of 2

How does esp8266 upload, store and execute code ?

PostPosted: Tue Feb 14, 2017 11:24 am
by apicquot
When looking at specs of Esp8266 it very often refers to firmware or TCP/IP stack embarked on the chip: When programming with Arduino IDE, does it use some kind of bootloader and permanent libraries on the chip or does it flush all the chip memory and upload all necessary libraries every time ?

The board also have a flash memory chip: can anyone explain how this flash memory is used ? Is the code loaded on that memory chip and a firmware is loaded on the esp to load and execute the code from the flash memory ?

Thank you for any explanation.

Re: How does esp8266 upload, store and execute code ?

PostPosted: Tue Feb 14, 2017 1:19 pm
by lethe
The ESP contains a ROM with a boatloader, that takes care of firmware download via UART and executing code from SPI flash or SDIO, depending on the bootmode pins (GPIO0/2/15).
There is no onchip storage for user code, all code is loaded fron SPI flash (either by loading it to RAM or in place through memory mapping).
I don't know if the arduino IDE wipes the entire flash with every upload, but with the vendor SDK, all libraries are linked into the user code.

Re: How does esp8266 upload, store and execute code ?

PostPosted: Tue Feb 14, 2017 4:43 pm
by martinayotte
lethe wrote:I don't know if the arduino IDE wipes the entire flash with every upload,

No, the arduino IDE is only erasing the blocks used by the new firmware, leaving untouched other areas.
But as the Native SDK, it links all the libraries required into a single binary.

Re: How does esp8266 upload, store and execute code ?

PostPosted: Wed Feb 15, 2017 7:52 am
by piersfinlayson
lethe wrote:The ESP contains a ROM with a boatloader, that takes care of firmware download via UART and executing code from SPI flash or SDIO, depending on the bootmode pins (GPIO0/2/15).
There is no onchip storage for user code, all code is loaded fron SPI flash (either by loading it to RAM or in place through memory mapping).
I don't know if the arduino IDE wipes the entire flash with every upload, but with the vendor SDK, all libraries are linked into the user code.


The ROM has more than just a bootloader - it also has some (hard coded, obviously) implementations of basic functions such as alloc, memcpy, SPI erase/read/write etc. These can be used by applications that are themselves loaded from flash. I haven't looked into whether the ESP8266's SDK methods (which are linked into the user's app so are loaded from flash) actually call these ROM functions themselves, or are parallel implementations, nor have I checked whether the Arduino code uses any of these directly.

The user's application code can either be loaded into RAM at boot time, or can be loaded and executed just in time from the flash chip itself - the latter saving precious RAM on the ESP8266. This is why you'll often see ICACHE_FLASH_ATTR littering ESP8266 code - this indicates to the linker that these methods should be stored on the flash and only loaded in as required. This is slower, but for any sizeable app, most of your code will have to be handled this way.

Take a look at eagle.rom.addr.v6.ld if you're interested in what's located on the ROM.