I have written a bootloader that receives a new program from the UART and programs it into the flash as soon as it receives a full page. I have ISRs for the UART Tx, Rx, and SPM_READY.
I'm having an issue with the last page of flash not being written. I tried delaying for as long as 750 ms (an eternity in CPU time!) but the last page was never written. However, If I reset the MCU, the last page is immediately written.
I'm using an ATMega162.
The basic flow of the program is:
1. Rx ISR tells main that a frame was received.
2. Main pushes new data onto temp page buffer.
3. If temp page buffer is full, erase the page and enable SPM interrupt.
4a. Send a request down the UART for the next frame.
4b. When SPM interrupt fires, write the page.
The thing that really has me puzzled is that resetting the CPU causes the last page to be written, but delaying does not. What could cause this? Any help would be greatly appreciated.