I try to run the AVR1316 example to test self-programming.
Code is compiled in studio 6 and I have set .BOOT to .BOOT=0x010000 which generates -Wl,-section-start=.BOOT=0x20000 for the linker.
added Flash_Defines.h to #define FLASH_PAGE_SIZE 512.
I can see that the SP_CommonSPM: instruction are in the boot section.
So far, so good.
The issue is that SP_EraseApplicationPage(a_value_here); always erase starting at 0x100 to 0x1FF which erase a big part of the firmware of course!
Values at address < 0x100 are probably protected, 0x1ff = 511.
If I track the value during execution with disassembly everything seems fine,
Z is loaded with the correct address,
CCp protection is loaded to 0x9D in line with the define
movw ZL, r24 ; Load R25:R24 into Z.
sts NVM_CMD, r20 ; Load prepared command into NVM Command register.
ldi r18, CCP_SPM_gc ; Prepare Protect SPM signature in R18 #define CCP_SPM_gc (0x9D<<0)
sts CCP, r18 ; Enable SPM operation (this disables interrupts for 4 cycles).
spm ; Self-program.
clr r1 ; Clear R1 for GCC _zero_reg_ to function properly.
out RAMPZ, r19 ; Restore RAMPZ register.
Any suggestion why erase does not erase the page given.
/*! \brief Erase page at byte address in application or application table section.
* This function erase one page given by the byte address.
* \param address Byte address for flash page.
void SP_EraseApplicationPage(uint32_t address);