How to create custom bootloader in esp8266 - Page 1
Forum section: electronics / general

author: digitalectron
author_rank: Contributor
timestamp: April 27, 2024, 08:05:05 am
content: Hello EEVblog community!I am currently working on a project with ESP8266 and I want to write an OTA bootloader for it. This bootloader should run when the microcontroller is reset and, based on user selection, either enter programming mode to upload a new main application, or run the previously uploaded application.I have not worked with linker scripts before, so I am looking for a simple example that includes two programs:Bootloader program: This program should remain constant and perform the following tasks:Detect whether the user has pressed a button to enter programming mode.If the button is pressed, go to programming mode.If the button is not pressed, run the main application that is already stored in flash memory.Main application: This could be a simple program like an LED blinker program.I would appreciate any help you can provide on writing this bootloader and how to use linker scripts to achieve this goal.Additional details:I am using an ESP8266-12E.I want to use the platform IO for development.Questions:How can I write a bootloader program that runs when the microcontroller is reset?How can I use linker scripts to select which program (bootloader or main application) should run?What is a simple example of how to use linker scripts to achieve this goal?Thanks for your time!

author: tooki
author_rank: Super Contributor
timestamp: April 27, 2024, 03:05:33 pm
content: Why not look at existing OTA update libraries and see how it works?Bear in mind that the ESP chips have their own bootloader which you cannot remove or replace.Have you read any of the documentation from Espressif? The OTA update mechanism is pretty well documented in the ESP-IDF documentation. (In PlatformIO, you can code directly with the ESP-IDF framework, or you can use the Arduino framework, which then functions as a layer on top of the ESP-IDF framework. So no matter which framework you target, it’s ultimately running on ESP-IDF and its FreeRTOS implementation.)

author: tellurium
author_rank: Frequent Contributor
timestamp: April 27, 2024, 03:13:19 pm
content: There is no need to write anything.ESP8266 ROM already has this functionality. If you pull GPIO 0 down, and reset the micro - it'll boot in the bootloader mode.Then you can reflash it. Seehttps://github.com/cpq/esputil#esp32-flashing

author: tooki
author_rank: Super Contributor
timestamp: April 27, 2024, 05:29:33 pm
content: They said they want OTA (over the air, i.e. WiFi, Bluetooth, etc) updating. So I don’t think they’re referring to the hardcoded bootloader’s ability to load from serial.

author: kripton2035
author_rank: Super Contributor
timestamp: April 27, 2024, 05:43:04 pm
content: https://randomnerdtutorials.com/esp8266-ota-updates-with-arduino-ide-over-the-air/

author: tellurium
author_rank: Frequent Contributor
timestamp: April 27, 2024, 05:45:36 pm
content: Well, yeah, but then it is quite unclear what "if button is pressed, enter the programming mode" means. The description goes about a simple bootloader that either boots a firmware, whatever it is, or "enters a programming mode" - which is, I believe, a built-in ROM bootloader in the programming mode. Note that OP mentions "OTA", but there is nothing really about OTA in the description.OP, you do not describe your task well enough, apparently because you don't understand it well enough. You need to refine your description.

author: digitalectron
author_rank: Contributor
timestamp: April 28, 2024, 08:22:53 am
content: Thank you for taking the time to answer my question. I was wondering if there would be any problems booting the program if the power goes out while updating the ESP8266 firmware?

author: xvr
author_rank: Frequent Contributor
timestamp: April 28, 2024, 09:58:59 am
content: > I was wondering if there would be any problems booting the program if the power goes out while updating the ESP8266 firmware?No program - no problemIt just will not start, only builtin bootloader will survive

author: mianos
author_rank: Regular Contributor
timestamp: April 28, 2024, 10:24:43 am
content: Quote from: digitalectron on April 28, 2024, 08:22:53 amGenerally you set up different partitions to store the OTA. Generally it's not possible to write to the running application as you update it.If the flash fails, it does not affect the existing application. You can just reboot and try again.Keeping in mind, a lot of this depends on how much flash you have.https://github.com/espressif/ESP8266_RTOS_SDK/tree/master/examples/system/ota/simple_ota_exampleAlso, the OTA updater is not part of the boot system. It needs the tcp ip stack that is way way later. To implement OTA you generally set it up and organise a permanent thread to listen for the OTA or you can poll the OTA socket if you you are using a toy stack over the top of the RTOS, like the Arduino wrapper.There are several Arduino wrappers for OTA systems for the 8266. They mostly work OK. For example the OTA for the Tasmota project is very reliable and uses multiple partitions with fall back so if it fails it's no big deal to just try again, not having to resort to connecting the USB up and flashing it via serial.May I suggest, you also look at the esp32, for which there is much better support now and it's about the same price. I think the C3 is now 10c cheaper than than the 8266 in volume.

author: digitalectron
author_rank: Contributor
timestamp: April 29, 2024, 09:03:40 am
content: QuoteThanks for your help , How do I find the best link for Tasmota project?

author: eutectique
author_rank: Frequent Contributor
timestamp: April 29, 2024, 10:23:49 am
content: Quote from: digitalectron on April 29, 2024, 09:03:40 amClickhere.

author: mianos
author_rank: Regular Contributor
timestamp: April 29, 2024, 10:40:31 am
content: ps, I hope I have not confused you. I mean the tasmota project is attestation that OTA does work on the ESP8266 and can work very well.I have not looked at their OTA code.Maybe start here:https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html

author: tooki
author_rank: Super Contributor
timestamp: April 29, 2024, 04:06:06 pm
content: Quote from: digitalectron on April 28, 2024, 08:22:53 amThe worst-case scenario is that you have to use serial/USB to reflash the firmware. But IIRC, the OTA mechanism in ESP-IDF at least theoretically can recover from an outage.