I'm building a project where I will need to implement a bootloader on the ATmega328PB that will allow me to download/flash new firmware from a host UART programmer (the host is a Particle Electron --> STM32) I will use this bootloader to allow the UART programmer host MCU to tell my ATmega to prepare for a firmware update, at which point the ATmega will "reset" itself, enter bootloader, and wait for the Electron to begin flashing new firmware to it (i.e. no external reset signal will be provided... only signals between the ATmega and the programmer are GND, UART-TX, and UART_RX)
I am at the start of this journey and am quite confused about how to proceed.
I feel like I should be trying to use Optiboot as-is. This would allow me to easily adapt the ArduinoAsISP sketch to the Electron without having to re-invent the wheel. The problem with this approach (as far as I can tell) is that I would need to re-work how the ATmega resets, since Optiboot requires a hardware reset in order to accept new firmware. I started a separate thread on this subject and it would seem from the answers therein that I should be able to use something like the following to "meta-reset" the ATmega without use of the software watchdog or external reset pin:
typedef void(* fptr_t) (void); fptr_t resetFunc = (fptr_t)0x7E00; //declare reset function @ address 0x7E00 --> bootloader start address resetFunc(); //pseudo-reset (enter bootloader)
My understanding is that if I do things this way, then the MCUCSR register will be 0 when Optiboot spools up, which in turn will allow it to run. Approaching things this way would mean that all registers and RAM would be in the state they were in when I called resetFunc(). Would this break Optiboot (i.e. does optiboot need to be run on a "fresh" ATmega runtime instance... with registers in their POR values?) or will things hum along nicely?
Is my reasoning for using Optiboot sound, or should I roll up my sleeves and take a deep-dive into the world of coding my own bootloader from scratch?
FYI I have no prior experience writing bootloaders or using Make.