Latest AT Firmware for ESP8266 with 512k Flash
by kacang bawang
The AT firmware is the default firmware for ESP8266 and provides AT style commands for actions such as join access point
and send tcp packet
. It is distributed in binary form by Espressif, but it can also be built from source (binary blobby as it may be). Typically one can find a new version of AT firmware in each release of Espressif’s SDK.
Since SDK version 1.0.1 the binary distribution of AT has become too large to fit on a 512kB flash. However, at this point in time (end of May 2015) most of the ESP8266 units out there are of the 512 kB variety. Thus begging the queston: how can one update their AT firmware to the latest version?
The answer: we will build it ourselves. For this we will need a way to compile esp8266 projects. There are 3 options:
1. Gather the tools yourself (see for example: Russian, Google translated)
2. Use ESP OPEN SDK, which gathers all the tools from (1) for you automatically. They also post updates, such as when new SDKs come out. This is the preferred method.
3. Use the Windows SDK by Cherts. I develop on Linux, so I won’t be covering this route, but it should work just the same.
As our starting point, let’s assume you have the development environment installed. You may have even tried to cd
to /SDK/examples/at
and tried to build it, but have received the following message:
1 |
make: Nothing to be done for `FORCE'. |
Running a little bit ahead I will tell you that it is due to Espressif’s makefile structure, but first let’s step back and take a look at the flash layout.
Since SDK 0.8 we’ve had the ability to do OTA updates. This was achieved by splitting the flash into two halves – the current firmware and the incoming update. The device runs from one or the other such that the unused slot may be overwritten with new data. This requires the use of a second stage bootloader, since the first stage (hardware) bootloader always starts executing from the same location and as such, does not recognize existence of two firmware slots. For more details on how that works, see Richard Burton’s excellent series of posts about the ESP bootloading process and his own open source bootloader: rBoot. Furthermore, since there are now two copies of the firmware living in the flash, each one can only be 1/2 as large as before. Let’s take a look at the difference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# ------------- 512K single slot -------------------- # 0x00000 - 0x3DFFF (248kB) flash.bin ............ user code # 0x3E000 - 0x3FFFF (8kB) master_device_key.bin # 0x40000 - 0x7DFFF (248kB) irom0text.bin ............ espressif's blobs # 0x7E000 - 0x7FFFF (8kB) wifi config # ------------- 512K layout double slot -------------------- # 0x00000 - 0x00FFF (4kB) boot.bin ............ 2nd stage bootldr # 0x01000 - 0x3DFFF (249kB) user1.bin ............ firmware 1 (both user code and blobs) # 0x3e000 - 0x3FFFF (8kB) master_device_key.bin # 0x40000 - 0x40FFF (4kB) unused # 0x41000 - 0x7DFFF (249kB) user2.bin ............ firmware 2 (both user code and blobs) # 0x7e000 - 0x7FFFF (8kB) wifi config # ------------- 1MB layout -------------------- # 0x00000 - 0x00FFF (4kB) boot.bin # 0x01000 - 0x7DFFF (512kB) user1.bin # 0x7e000 - 0x7EFFF (8kB) master_device_key.bin # 0x80000 - 0x80FFF (4kB) unused # 0x81000 - 0xFDFFF (512kB) user2.bin # 0xfe000 - 0xFFFFF (8kB) wifi config |
Thus, the amount of memory available to firmware is:
1 2 3 4 5 6 |
single slot: 512kB flash: 496kB rom dual slot: 512kB flash: 249kB rom (each) 1024kB flash: 512kB rom (each) |
Now let’s take a look at the latest AT, as built with default settings:
1 2 3 |
irom0text.bin = 208.1 kB //these are the espressif binary blobs flash.bin = 43.3 kB //this is the user code total: 251.4 kB |
As you can see the combined firmware at 251.4 kB will not fit into the 1/2 flash sized slot of 249kB. Just barely. For this reason Espressif now packages the new AT as built for the larger flash, with a 2 slot setup. With a 512kB flash the only option is to use the whole flash for one firmware, which will accomodate up to 496kB.
An aside:
Q: How do I tell what SPI Flash I have and what is its size?
A: using esptool.py
reboot into flash mode and issue the following command: ./esptool.py flash_id
. Using the returned info find your flash chip and look up its specs. This is actually pretty hard, but eventually I found a comment on esp8266.com (don’t know the specific post anymore – hat tip to the unknown). It said that:
1 2 3 4 5 6 |
Manufacturer: c8 Device: 4013 means: GigaDevice: 4Mbits/512Kbyte GD25Q40 |
So where were we? Oh, right – we’ve got the build environment installed and we’re going to build our own AT firmware which we will then package into a 1-slot image to be flashed.
Since we’re not going to be modifying the project – we just want to build it – we also won’t rewrite its makefile. We will use Espressif’s makefile, which is rather convoluted. Most of the work is in the non-project specific makefile (let’s call it “master” makefile) and it is referred to from each project as “../Makefile”. If such master makefile is not found, nothing will build. The master makefile takes as its inputs several variables (with defaults). They are defined when you run gen_misc.sh
If you “make” the master makefile it will attempt to build every directory in its folder. Sort of like “build all projects”. If you go into a project directory and do a “make”, it will build only that project, but it will still need that master makefile.
Here is how you should organize your AT project directory. Create a directory, somewhere away from the SDK, like in your home dir, for example. Fill it with the following contents:
1 2 3 4 5 6 7 8 |
/home/my_at \--at .......... copy from SDK/examples Makefile ....... copy or link from SDK/, this is the master makefile include ........ link to SDK/include ld ............. link to SDK/ld tools .......... link to SDK/tools bin ............ create empty, output will be placed here \--upgrade ..... create empty |
At this point you can either go into /home/my_at/at
and run genmisc.sh
which will have you choose some options interactively and then will call the master makefile. Choose all defaults – this gives us the single slot setup for a 512kB sized flash. Although I prefer to inline these options at the top of the copy of the master makefile. That way I just “make” instead of having to go through genmisc.sh
prompts. Here’s what I added to the top of /home/my_at/Makefile
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
####################### # My locations # OPENSDK = /home/projects/esp8266/esp-open-sdk TOOLS = $(OPENSDK)/xtensa-lx106-elf/bin PATH := $(PATH):$(TOOLS) export COMPILE = gcc ###################### # My defaults. [] = genmisc script defaults # #old=boot_v1.1, new=boot_v1.2+, [none=none] BOOT := none #[0=eagle.flash.bin+eagle.irom0text.bin], 1=user1.bin, 2=user2.bin APP = 0 #20MHz, 26.7MHz, [40MHz], 80MHz SPI_SPEED = 40 #[QIO], QOUT, DIO, DOUT SPI_MODE = QIO #[0= 512KB( 256KB+ 256KB)] #2=1024KB( 512KB+ 512KB) #3=2048KB( 512KB+ 512KB) #4=4096KB( 512KB+ 512KB) #5=2048KB(1024KB+1024KB) #6=4096KB(1024KB+1024KB) SPI_SIZE_MAP = 0 |
This should produce eagle.flash.bin
and eagle.irom0text.bin
in the bin
directory for us.
Oh, one other note: in SDK 1.1.0_15_05_22 there is a missing define. This was fixed in the latest 1.0.1, but in case you see a linking error complaining that:
1 2 3 4 5 |
undefined reference to 'at_exeCmdCiupdate' add to user_config.h: #define AT_CUSTOM_UPGRADE |
Hat tip to rudi.
Ok, so we’re ready to flash. This should be simple enough:
1 |
esptool.py write_flash 0x00000 flash.bin 0x40000 irom0text.bin |
But somehow (for me at least) that results in the first 0x5000 bytes being erased (all FF’s). Through some experimentation, it was established that the order of files in the flash command matters. See here for a discussion.
1 2 3 4 5 6 7 8 9 |
esptool.py write_flash 0x00000 flash.bin 0x40000 irom0text.bin \-- first 0x5000 bytes nuked esptool.py write_flash 0x00000 flash.bin 0x40000 irom0text.bin esptool.py write_flash 0x00000 flash.bin \-- OK, seems to be the safest method? esptool.py write_flash 0x40000 irom0text.bin 0x00000 flash.bin \-- OK |
If you are encountering this weird flashing issue, could you please comment either here or on esp8266.com?
That should right about do it for building and flashing the newest AT firmware from Espressif’s SDKs. Thank you for reading this far ;P
[…] http://kacangbawang.com/latest-at-firmware-for-esp8266-with-512k-flash/ […]
Thanks for the info on this page! On an earlier version of the toolchain, I struggled through the same issues that you outline. Consequently, I was loath to upgrade because I expected much the same again. But your notes got me through it must more smoothly!
One addition… when setting up your home dir, you also need a link to tools (like “ln -s root-for-toolchain/esp-open-sdk/sdk/tools workspace/tools”).
Glad to help 🙂 And thank you for the feedback!
Nice post. I have similar problem with ESPRESSIF SDK RTOS 1.3. It seems that espressif force user to use boot, so any example compile to file over 240kB. I was thinking what did you changed in linker script to compile your code. For me is always “irom0.text will not fit in region”.
Interesting. I have not tried to compile using RTOS SDK with this method. I am not doing anything special other than to define ‘noboot’ in the makefile. Why does RTOS SDK require usage of boot?
Just tried to compile an example project with the RTOS SDK. Indeed, it is as you say. Even with ‘noboot’ selected the linker still errors out with a message about irom0 being too big. I went through the differences in makefiles – there’s nothing of significance. Except – the RTOS SDK makefile does not use -O2, so I tried adding it. Also tried -Os, but neither allowed compilation to succeed.
I wonder if all the libs that RTOS SDK drags in are simply too big?
answer to your question here
Hi.
20-Dec-2020.
Thanks for this info.
I’m trying to get the latest nano_AT command set (non-FOTA, NonOS) installed in my old 1Mb ESP8266-01’s. I need the less buggy AT firmware and some of the advanced AT commands and the security that the ESP can’t be ‘hack flashed’. I can’t quite follow all the steps and vague clues in SDK however to create the eagle.flash.bin and eagle.irom0text.bin files.
Do you have any expertise in generating these files and would you be so kind as be be able to help these files get made.?
Best regards, Mike_Malaysia.