Hi @Juraj, a little update for you. I was trying to get the optiboot bootloader installed. I think its set up and working now. This weekend I had posted a related question and you had responded with:
on the ATmega2560, only the bootloader can write into the flash memory.
the Optiboot 8 has a function that can do it. there is a .h file which declares a function call to this function in the bootloader.
but it s just the start. you need a way for running application to move the new application. but the application runs from the flash so it can't overwrite itself. so I added a function to Optiboot which does this.
it is used here
https://github.com/JAndrassy/ArduinoOTA/blob/master/src/InternalStorageAVR.cpp
What I did was write a small test program similar to the one in the question I had posted this weekend:
#include <ArduinoOTA.h>
#include "hexfile.h" // bytes of a hex file for a test blink + serial monitor program
void setup()
{
Serial.begin(9600);
size_t fileSize = sizeof(hexcode); // hexcode is the name of the buffer containing the byte from the hex file
if (!InternalStorage.open(fileSize))
{
Serial.println("Not enough space. Failed to update firmware.");
while (true);
}
Serial.println("Writing program to flash...");
for (int i = 0; i < fileSize; ++i)
{
InternalStorage.write(hexcode[i]);
}
InternalStorage.close();
Serial.println("Done flashing.");
delay(1000);
InternalStorage.apply();
// press reset
}
void loop()
{
}
I uploaded with Tools > Board > "Arduino Mega 2560 (Optiboot)" selected.
When I run this program and reset, the processor doesn't respond with the blink sketch. But it also doesn't run same program again (like from this weekend) which leads me to believe that the code is being written to flash. But it wont start it. I went back and checked your response:
you need a way for running application to move the new application. but the application runs from the flash so it can't overwrite itself. so I added a function to Optiboot which does this.
can you tell me which specific function you were referring to?
Just for your information, hexfile.h
looks like this:
const uint8_t hexcode[] PROGMEM = {0x3a, 0x31,...}; // all 7594 bytes