I have a self-modifying program which writes to the program flash area (it does not break the program flow because I write to another flash sector than the one my program is running in – it runs in the protected bootloader section).
The complicated part, writing to the flash, works. I can check it in the debugger, the values I have sent are successfully written to the flash.
However, when I try to check the contents with the LPM instruction, it always reads zero.
I identified the following causes when LPM can fail to read:
- When the lock bits are set, forbidding reading the flash. Not the case here, as no lock bits are set.
- The reading of the flash is locked because of a previous write instruction. Not the case here, as I set
RWWSREand wait until the green light withwhile (SPMCSR & 0b01000000) {} - I miscalculated the address (the segmentation of the Z pointer can be tricky). Not the case here, as I also tried it with the very first word (at address 0) and it still doesn’t work.
I use the following code for my test, reading the first two bytes of the flash (the writing instruction completed successfully, as Program memory at that position is not zero, checked with the debugger)
lpm r0,Z+
lpm r1,Z+
movw r2, r0
Before that, I set the Z pointer to zero, and check it with the debugger that it is really zero (r30 and r31).
However, r2 and r3 will always be zero, regardless of what was in the flash.
Is there another situation where LPM fails to read?
I found the solution while running it step by step and checking the contents of all the affected registers. It turned out the problem has nothing to do with
LPM, but I keep the question for future reference.the code part
was in a C for loop, and the
Zpointer was set before that loop. However, the loop comparison just happened to be compiled to user30as a temporary register, so it corrupted theZpointer. Problem solved by placing the initialization of theZpointer and the part with theLPMinto the same#asm ... #endasmblock.