Open LadislavOzobot opened 2 years ago
We have implemented the part that writes calibration using do_spm stub and it seems to work fine on atmega328 target. Is there interest in this PR? If so, let me know, I'll add documentation and example calibration code.
Based on discussion with @ondrej-stanek-ozobot , this is attempt at implementing osccal in the bootloader. Would solve issue #87 The idea is originally by @WestfW in https://github.com/Optiboot/optiboot/pull/342#issuecomment-1090918606, this is the implementation i came up with:
rjmp start_main
andOSCCAL
is not applied (MCU core loads factory default at startup and we don't touch it). This make sure the cpu can start up before the calibration is performed, and for example run a calibration program.0x05 0xc0
, where0x05
is the offset.ldi r24,0xFF
instruction is patched to replace0xFF
with the new osccal valuerjmp start_main
torjmp set_osccal
. This can be patched, as set osccal has relative address0x05
only needs to modify 1s to 0s to become set_osccal0x01
.OSCCAL
valueret
andrjmp
instructions are the same length, so it should work both withAPP_NOSPM
and without. But withNOSPM
, it's probably necessary to flash it using a programmer, as there is no access to this region from program.Caveats:
NOP
, as I needed to be able to rewrite the address from0x05
to0x01
. Makingsts
andldi
combo shorter would also help, the address just needs to be an even number. Or therjmp
instruction replaced for a different instruction? Not sure about that.We will test on our platform and report if it works as expected. For now, we can have a discussion if this approach on the bootloader side is OK-ish and what more needs to be done.
Feel free to discuss, modify and reject if necessary :-)
Edit: formating.