diff --git a/avr/flash.asm b/avr/flash.asm new file mode 100644 index 0000000..a5574bc --- /dev/null +++ b/avr/flash.asm @@ -0,0 +1,174 @@ + + +; *************************************************************************** +; data + + +.dseg + +flashDataBegin: + flashCurrentAddress: .byte 2 + flashLastReceivedMsg: .byte 2 +flashDataEnd: + + + + +; *************************************************************************** +; code + + +.cseg + + +; --------------------------------------------------------------------------- +; start flashing a page +; +; IN: +; - Z: Address to work on (byte address as for LPM!) + +Flash_StartPage: + push zh + push zl + ldi r16, ((PAGESIZE*2)-1) + and r16, zl + rcall Flash_ReadPageIntoPageBuffer + pop zl + pop zh + ret + + + +; --------------------------------------------------------------------------- +; finish flashing a page +; +; IN: +; - Z: Address to work on (byte address as for LPM!) + +Flash_FinishPage: + ldi r16, ((PAGESIZE*2)-1) + and r16, zl + rcall Flash_ErasePage + rcall Flash_WritePage + ret + + + +; --------------------------------------------------------------------------- +; Flash_ReadPageIntoPageBuffer +; +; +; IN: +; - Z: Address to read from (byte address as for LPM!) +; OUT: +; - nothing +; MODIFIED REGISTERS: R0, R1, R15, R16, R20, R24, R25, Z + +Flash_ReadPageIntoPageBuffer: + in r15, SREG + cli + ldi r24, LOW(PAGESIZE*2) + ldi r25, HIGH(PAGESIZE*2) +Flash_ReadPageIntoPageBuffer_loop: + lpm r0, Z+ ; read source data from FLASH (low) + lpm r1, Z ; read source data from FLASH (high) + sbiw ZH:ZL, 1 ; rewind Z address for following SPM + ldi r20, (1<