I have a modified version of the AVR231 bootloader--actually modified version of the port to gcc that's posted on this website.
After all the application code has been saved to flash (and data to eeprom), an spmEnableRWW() function is executed. This is supposed to enable RWW of the loaded code and loop until it's enabled. A CRC check of the newly, self programmd flash is then done.
However, while doing the CRC,the function pgm_read_byte returns 0xff at every address.
In debug mode using the dragon, flash memory also shows all 0xff.
If I set a break point anywhere (in AS 6.2) suddenly Dragon's display of flash memory is valid--not all 0xff. Furthermore, subsequent pgm_read_byte calls result in valid CRC (after processing all of the newly loaded memory).
Doing the CRC immediately after a RST also works.
After self programming flash, the spmEnableRWW function is called. I think this should enable the applicaiton code to be read?
The assembly code C function spmEnableRWW() maps to is pretty much right out of avr231:
ldi r19, (1<<RWWSRE) | (1<<SPMEN)
LOAD r23, SPMCR
sbrc r23, SPMEN
So, I think the code will be in the spmWait loop until the application code section can be read. However, I see no looping at all, SPMEN bit says it's ready the first time through the loop (this is suspicious).
The original AVR gcc code actually used the WDT to generate a RST after the application code was programmed. I changed to code to only do the CRC after programming, rather than every time the processor is booted.
I can change the code to only do the memory CRC after a RST, but I'd like to understand why the spmEnableRWW isn't working.
I thought maybe this was a quirk of being in debugWire mode and specifically cards that were difficult to get out of debugWire mode. I got some great help from this forum to get out of debugWire mode, but the problem is still there.
After self programming application memory I have a simple loop that doesn't exit while pgm_read_byte is returning 0xff. It flashes LEDs while 0xff is returned. That loop never exits, so nowI know that application memory can't be read when not in debug mode.
I see this on two different cards.