Author Topic: How to create custom bootloader in esp8266  (Read 870 times)

0 Members and 1 Guest are viewing this topic.

Offline digitalectronTopic starter

  • Newbie
  • Posts: 6
  • Country: de
How to create custom bootloader in esp8266
« on: April 27, 2024, 08:05:05 am »
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!
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11632
  • Country: ch
Re: How to create custom bootloader in esp8266
« Reply #1 on: April 27, 2024, 03:05:33 pm »
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.)
« Last Edit: April 27, 2024, 03:09:25 pm by tooki »
 

Offline tellurium

  • Frequent Contributor
  • **
  • Posts: 253
  • Country: ua
Re: How to create custom bootloader in esp8266
« Reply #2 on: April 27, 2024, 03:13:19 pm »
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. See https://github.com/cpq/esputil#esp32-flashing
Open source embedded network library https://github.com/cesanta/mongoose
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11632
  • Country: ch
Re: How to create custom bootloader in esp8266
« Reply #3 on: April 27, 2024, 05:29:33 pm »
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.
 


Offline tellurium

  • Frequent Contributor
  • **
  • Posts: 253
  • Country: ua
Re: How to create custom bootloader in esp8266
« Reply #5 on: April 27, 2024, 05:45:36 pm »
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.
Open source embedded network library https://github.com/cesanta/mongoose
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 
The following users thanked this post: tooki

Offline digitalectronTopic starter

  • Newbie
  • Posts: 6
  • Country: de
Re: How to create custom bootloader in esp8266
« Reply #6 on: April 28, 2024, 08:22:53 am »
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?
 

Online xvr

  • Regular Contributor
  • *
  • Posts: 205
  • Country: ie
    • LinkedIn
Re: How to create custom bootloader in esp8266
« Reply #7 on: April 28, 2024, 09:58:59 am »
> 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 problem  :-// It just will not start, only builtin bootloader will survive  :box:
 
The following users thanked this post: digitalectron

Online mianos

  • Contributor
  • Posts: 34
  • Country: au
Re: How to create custom bootloader in esp8266
« Reply #8 on: April 28, 2024, 10:24:43 am »
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?
Generally 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_example

Also, 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.
 
The following users thanked this post: digitalectron

Offline digitalectronTopic starter

  • Newbie
  • Posts: 6
  • Country: de
Re: How to create custom bootloader in esp8266
« Reply #9 on: April 29, 2024, 09:03:40 am »
Quote
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.

Thanks for your help , How do I find the best link for Tasmota project?
 

Online eutectique

  • Frequent Contributor
  • **
  • Posts: 400
  • Country: be
Re: How to create custom bootloader in esp8266
« Reply #10 on: April 29, 2024, 10:23:49 am »
Thanks for your help , How do I find the best link for Tasmota project?

Click here.
 
The following users thanked this post: digitalectron

Online mianos

  • Contributor
  • Posts: 34
  • Country: au
Re: How to create custom bootloader in esp8266
« Reply #11 on: April 29, 2024, 10:40:31 am »
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
 
The following users thanked this post: digitalectron

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11632
  • Country: ch
Re: How to create custom bootloader in esp8266
« Reply #12 on: April 29, 2024, 04:06:06 pm »
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?
The 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf