flashing a device via boot loader works now!!

- use a simple rjmp to start main firmware
- add buffer size as a parameter for com2ReceivePacketRaw
- fixed bugs in
  - Flash_StartPage
  - Flash_FinishPage
  - Flash_ReadPageIntoPageBuffer
- assume irqs disabled in flash routines
- increase flash receive buffer size to 128
This commit is contained in:
Martin Preuss
2023-04-20 23:57:13 +02:00
parent 9b568393cd
commit 82c7626783
7 changed files with 56 additions and 71 deletions

View File

@@ -24,18 +24,20 @@ FLASH_BEGIN:
; ---------------------------------------------------------------------------
; start flashing a page
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to work on (byte address as for LPM!)
; OUT:
; - nothing
; REGS: R16 (R0, R1, R15, R16, R20, R24, R25)
; REGS: R16 (R0, R1, R16, R20, R24, R25)
Flash_StartPage:
push zh
push zl
ldi r16, ((PAGESIZE*2)-1)
ldi r16, ~((PAGESIZE*2)-1)
and zl, r16
rcall Flash_ReadPageIntoPageBuffer ; (R0, R1, R15, R16, R20, R24, R25, Z)
rcall Flash_ReadPageIntoPageBuffer ; (R0, R1, R16, R20, R24, R25, Z)
pop zl
pop zh
ret
@@ -45,6 +47,8 @@ Flash_StartPage:
; ---------------------------------------------------------------------------
; finish flashing a page
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to work on (byte address as for LPM!)
; OUT:
@@ -52,7 +56,7 @@ Flash_StartPage:
; REGS: R16 (R15, R20)
Flash_FinishPage:
ldi r16, ((PAGESIZE*2)-1)
ldi r16, ~((PAGESIZE*2)-1)
and zl, r16
rcall Flash_ErasePage ; (R15, R16, R20)
rcall Flash_WritePage ; (R15, R16, R20)
@@ -63,28 +67,26 @@ Flash_FinishPage:
; ---------------------------------------------------------------------------
; Flash_ReadPageIntoPageBuffer
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to read from (byte address as for LPM!)
; OUT:
; - nothing
; REGS: R0, R1, R15, R16, R20, R24, R25, Z
; REGS: R0, R1, R16, R20, R24, R25, Z
Flash_ReadPageIntoPageBuffer:
in r15, SREG
cli
ldi r24, LOW(PAGESIZE*2)
ldi r25, HIGH(PAGESIZE*2)
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<<SPMEN) ; enable next SPM, write into temp page buffer
rcall utilsDoSpm ; (R16)
adiw ZH:ZL, 2
sbiw r25:r24, 2 ; use subi for PAGESIZEB<=256
brne Flash_ReadPageIntoPageBuffer_loop
out SREG, r15
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<<SPMEN) ; enable next SPM, write into temp page buffer
rcall flashDoSpm ; (R16)
adiw ZH:ZL, 2
sbiw r25:r24, 2 ; use subi for PAGESIZEB<=256
brne Flash_ReadPageIntoPageBuffer_loop
ret
@@ -92,25 +94,19 @@ Flash_ReadPageIntoPageBuffer_loop:
; ---------------------------------------------------------------------------
; Flash_WriteIntoPage
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to write to (byte address as for LPM!)
; - R1:R0: Word to write into page
; OUT:
; - Z: address for next write
; MODIFIED REGISTERS: R15, R16
; REGS: R16
Flash_WriteIntoPage:
in r15, SREG
cli
ldi r20, (1<<SPMEN) ; enable next SPM, write into temp page buffer
rcall utilsDoSpm ; (R16)
ldi r16, 2
add zl, r16
dec r16 ; doesn't affect carry flag
dec r16
adc zh, r16
out SREG, r15
rcall flashDoSpm ; (R16)
adiw zh:zl, 2
ret
@@ -118,19 +114,17 @@ Flash_WriteIntoPage:
; ---------------------------------------------------------------------------
; Flash_ErasePage
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address of the page to erase.
; OUT:
; - nothing
; REGS: R15, R20 (R16)
; REGS: R20 (R16)
Flash_ErasePage:
in r15, SREG
cli
ldi r20, (1<<PGERS) + (1<<SPMEN) ; enable next SPM, erase page (R1/R0 ignored)
rcall utilsDoSpm ; (R16)
out SREG, r15
ldi r20, (1<<PGERS) + (1<<SPMEN) ; enable next SPM, erase page (R1/R0 ignored)
rcall flashDoSpm ; (R16)
ret
@@ -138,6 +132,7 @@ Flash_ErasePage:
; ---------------------------------------------------------------------------
; Flash_WritePage
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address of the page to erase.
@@ -146,17 +141,14 @@ Flash_ErasePage:
; REGS: R15, R20 (R16)
Flash_WritePage:
in r15, SREG
cli
ldi r20, (1<<PGWRT) + (1<<SPMEN) ; enable next SPM, write page (R1/R0 ignored)
rcall utilsDoSpm ; (R16)
out SREG, r15
ldi r20, (1<<PGWRT) + (1<<SPMEN) ; enable next SPM, write page (R1/R0 ignored)
rcall flashDoSpm ; (R16)
ret
; ---------------------------------------------------------------------------
; utilsDoSpm
; flashDoSpm
;
; wait until possible previous SPM finished and then issue another SPM.
;
@@ -169,7 +161,7 @@ Flash_WritePage:
; - nothing
; REGS: R16
utilsDoSpm:
flashDoSpm:
wait: ; wait for possibly previous SPM to complete
in r16, SPMCSR
sbrc r16, SPMEN