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:

I have checked the NVM.CTRLB register before and after my SP_* calls and it is and remains all zeros, so I don't think this is the problem.

Also know that I don't have a bootloader and my application is not doing the SPM lock. I only have my own application (starting from address 0) but with the SP_* methods moved into the bootloader section to allow them (I had hoped) to write stuff into flash for me.

I have checked the lockbits too, they are 0xFF, which means no locks if I read correctly.

I can't help you. I don't know assembler. Maybe you have something wrong there. Your SP_CommonSPM is somewhat different from what I see in the sp_driver.s used in my bootloader, but that doesn't mean it is wrong.

If you have an on chip debugger, that might give you a clue.

I have attached my sp_driver.s in case you want to look. I had to add a .txt suffix because this forum won't upload a file ending in .s

Thanks for the help anyway. I might be on to something where the calling between C en assembler is not going properly. I experience problems with the read methods also, I converted one to C and that does work. I will compare list files and hopefully I figure out the problem today.

Anyway my assembler is the same as in you attachment, except for my addition of:

OMG reading the datasheet again I noticed that the 8K bootloader is on top of the 128K flash. (For Mega's it used to be part of the Flash at the end). So changing my linker script to have the bootloader methods start from 0x20000 made it work. Nice.

I may try something like that for testing purposes. I'm writing a bootloader. It's much easier for me to test it when it is part of an application. I can test reading flash this way but of course I can't test writing to flash unless I use your method.