From 7dbe3a8e743837eaa4b3fdc7c2c4221a692c74c3 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 20 Feb 2023 23:48:17 +0100 Subject: [PATCH] avr: started working on flash module. --- avr/flash.asm | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 avr/flash.asm 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<