avr/module/flash: unified flash code for 1p/4p devices.

AtTiny 841 erases 4 pages at once, others only erase one.
Fixed a bug: only send ONE flash response to FLASH_START!!!
This commit is contained in:
Martin Preuss
2025-05-04 03:32:53 +02:00
parent b632a10fff
commit e25b0ad69d
6 changed files with 385 additions and 619 deletions

View File

@@ -47,11 +47,11 @@ checkFlash:
rcall flashProcessWriteFlashReady ; (R16, R17, R18, R19, R20, Y, Z)
rcall ioRawSendMsg ; (r16, r17, X)
ldi r16, CPRO_CMD_FLASH_START
ldi r16, NETMSG_CMD_FLASH_START
rcall ioWaitForGivenMsg ; (r16, r17, r18, r19, r20, r22, X)
brcc checkFlash_end
cpi r16, CPRO_CMD_FLASH_START
cpi r16, NETMSG_CMD_FLASH_START
clc
brne checkFlash_end ; no FLASH_START msg received
@@ -60,10 +60,6 @@ checkFlash:
ldi xh, HIGH(flashRecvBuffer)
rcall flashProcessHandleFlashStart
brcc checkFlash_end
; prepare and send response
clr r16
rcall flashProcessSendFlashResponse ; (R15, R16, R17, R18, R19, R20, R21, R22, X)
sec
checkFlash_end:
ret
@@ -77,21 +73,21 @@ checkFlash_end:
; handling receiving FLASH_DATA and FLASH_END messages, flash new firmware
;
; @return CFLAG set if okay, cleared otherwise
; @clobbers r16, r20
; @clobbers r16, r20 (R0, R1, R15, R16, R17, R18, R19, R22, R24, Z)
flashProcess:
rcall Flash_Init
flashProcess_loop1:
; wait up to 10s for incoming FLASH_DATA message
ldi r16, CPRO_CMD_FLASH_DATA
ldi r16, NETMSG_CMD_FLASH_DATA
rcall ioWaitForGivenMsg ; (r16, r17, r18, r19, r20, r22, X)
brcc flashProcess_end ; no FLASH_DATA or FLASH_END msg
; either FLASH_DATA or FLASH_END received
ldi xl, LOW(flashRecvBuffer)
ldi xh, HIGH(flashRecvBuffer)
cpi r16, CPRO_CMD_FLASH_DATA ; not FLASH_DATA, flashing ended/aborted
cpi r16, NETMSG_CMD_FLASH_DATA ; not FLASH_DATA, flashing ended/aborted
brne flashProcess_endReceived ; FLASH_END received, done
rcall flashProcessHandleFlashData ; (R0, R1, R15, R16, R17, R18, R19, R20, Z)
rcall flashProcessHandleFlashData ; (R0, R1, R15, R16, R17, R18, R19, R20, R22, R24, Z)
rjmp flashProcess_loop1
flashProcess_endReceived:
rcall flashProcessHandleFlashEnd
@@ -161,27 +157,39 @@ flashProcessCheckFlashStart_notMe:
;
; @return CFLAG set if okay, cleared otherwise
; @param X buffer containing the message
; @clobbers R0, R1, R18, Z (R15, R16, R17, R19, R20)
; @clobbers R0, R1, R18, Z (R15, R16, R17, R19, R20, R22, R24)
flashProcessHandleFlashData:
mov yl, xl
mov yh, xh
adiw yh:yl, FLASH_MSG_OFFS_MSGLEN
ld r17, Y ; length (subtract 6)
cpi r17, 6 ; cmd(1), src(1), addr(4)
brcs flashProcessHandleFlashData_badData
subi r17, 6 ; remaining length
adiw yh:yl, FLASH_PACKET_DATA_OFFS_ADDR-FLASH_MSG_OFFS_MSGLEN
ld zl, Y+ ; address (low)
ld zh, Y+ ; address (high)
adiw yh:yl, 2 ; ignore high bytes, points to first data byte now
ldd r17, Y+FLASH_MSG_OFFS_MSGLEN ; msg length
subi r17, 6 ; remaining length (sub cmd(1), src(1), addr(4))
brcs flashProcessHandleFlashData_badData ; too few payload bytes
breq flashProcessHandleFlashData_badData ; no payload bytes
ldd zl, Y+FLASH_PACKET_DATA_OFFS_ADDR ; only read lower two address bytes
ldd zh, Y+(FLASH_PACKET_DATA_OFFS_ADDR+1)
adiw yh:yl, FLASH_PACKET_DATA_OFFS_ADDR+4
rcall Flash_WriteData ; (R0, R1, R16, R18, R19, R20, R24, R25, X)
clr r16
rjmp flashProcessHandleFlashData_sendResponse
flashProcessHandleFlashData_badData:
ldi r16, FLASH_ERROR_MSGERROR
flashProcessHandleFlashData_sendResponse:
#if 0
push r16
; send flash message back (debugging)
ldi r24, 50
rcall flashWaitForMillisecs ; (R22, R24)
ldi xl, LOW(flashRecvBuffer)
ldi xh, HIGH(flashRecvBuffer)
rcall ioRawSendPacket ; (r16, r17, X)
pop r16
#endif
ldi r24, 50
rcall flashWaitForMillisecs ; (R22, R24)
rcall flashProcessSendFlashResponse ; (R16, R17, R18, R19, R20, X)
sbi LED_PIN, LED_PINNUM ; toggle
sec
ret
; @end
@@ -216,7 +224,7 @@ flashProcessHandleFlashEnd:
; @clobbers X (R16, R17, R18, R19, R20)
flashProcessSendFlashResponse:
; send flash ready message
; send flash response message
ldi xl, LOW(flashSendBuffer)
ldi xh, HIGH(flashSendBuffer)
rcall flashProcessWriteFlashRsp ; (R16, R17, R18, R19, R20, X)
@@ -238,7 +246,7 @@ flashProcessWriteFlashRsp:
st X+, r18 ; dest address (unused)
ldi r18, 3 ; msg code+src address+one payload byte
st X+, r18 ; msg len
ldi r17, CPRO_CMD_FLASH_RSP
ldi r17, NETMSG_CMD_FLASH_RSP
st X+, r17 ; msg code
clr r17
st X+, r17 ; src address (not used)
@@ -262,7 +270,7 @@ flashProcessWriteFlashReady:
st X+, r16 ; dest address (unused)
ldi r16, 20 ; msg code+src address+18 payload bytes
st X+, r16 ; msg len
ldi r16, CPRO_CMD_FLASH_READY
ldi r16, NETMSG_CMD_FLASH_READY
st X+, r16 ; msg code
ldi r16, COM2_MAINTENANCE_ADDR
st X+, r16 ; src address