I have built a custom board that has an ATmega328p connected to an ESP8266 module via UART. I am currently programming the ATmega328p using a USBtiny programmer via an SPI header. The ESP8266 module is programmed via an FTDI cable connected the the UART.
Firmware for both are created in Arduino IDE.
I have set up a webserver on the ESP that allows for files to be uploaded to the SPIFFS via FTP. I would like to be able to upload a hex file for the ATmega and then have the ESP program the ATmega via the UART connection. A GPIO on the ESP controls a transistor that can pull the reset pin on the ATmega to ground.
I know I need a bootloader on the ATmega (e.g. optiboot), but I don't know what I need to make the ESP do to trigger the bootloader and flash the ATmega. I'm thinking I need something like:
void initialiseAtmegaFlash () {
// Trigger the bootloader to accept incoming hex over UART
}
void flashHexToAtmega (File atmegaHex) {
for (uint32_t i = 0; i < atmegaHex.size(); ++i) {
// Handle timing of sending byte by byte over serial... or chunk by chunk?
}
}