I am using an ATxmega128A3U and am trying to have my application modify the flash from the application section using some bootloader hack, but it doesn't work as expected.
I have linked sp_driver.S from AVR1316 application note into my C application. I have modified it to have all methods inside a .bootloader section. Using a modified linker script I moved the .bootloader section to the end of the flash (address 0x1FE00 to be specific). Using objcopy I have marked the .bootloader section to be allocated so that avrdude transfers both the .text and .bootloader sections to the chip. I have read back the contents and double checked, the code is on the chip at the right addresses. It runs without crash, but the flash is not modified. The list file shows the moved methods like this:
1ff56: fc 01 movw r30, r24
1ff58: 40 93 ca 01 sts 0x01CA, r20
1ff5c: 2d e9 ldi r18, 0x9D ; 157
1ff5e: 20 93 34 00 sts 0x0034, r18
1ff62: e8 95 spm
1ff64: 11 24 eor r1, r1
1ff66: 3b bf out 0x3b, r19 ; 59
1ff68: 80 e2 ldi r24, 0x20 ; 32
1ff6a: 80 93 41 06 sts 0x0641, r24
1ff6e: 08 95 ret
In my opinion the spm instruction should be allowed to do its work when executed here, right? It is not necessary that some boot fuse is set for this, correct?
I call all the code like this:
memset(x, 0xAA, sizeof(x));
I would expect the first page of the flash to get 0xAAAA, but when reading back using avrdude the flash is still in tact. What am I missing?
I know there are plenty of posts on this here, but all seem to suggest this construction could work.
Hope someone can point out my mistake.