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

@@ -16,7 +16,7 @@
#include <gwenhywfar/buffer.h> #include <gwenhywfar/buffer.h>
#define AQH_MAXMSGSIZE 24 #define AQH_MAXMSGSIZE 128
#define AQH_MSG_OFFS_ALL_DEST_ADDRESS 0 #define AQH_MSG_OFFS_ALL_DEST_ADDRESS 0

View File

@@ -139,7 +139,7 @@
firmwareType: .dw FW_TYPE firmwareType: .dw FW_TYPE
firmwareVersion: .dw FW_VERSION firmwareVersion: .dw FW_VERSION
firmwareModules: .dw MODULES_MASK firmwareModules: .dw MODULES_MASK
firmwareStart: .dw 0 ; will be overwritten when flashing firmwareStart: rjmp main ; will be overwritten when flashing

View File

@@ -174,8 +174,8 @@
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; Reset and interrupt vectors (will be removed as soon as we can flash data over COM) ; Reset and interrupt vectors (will be removed as soon as we can flash data over COM)
rjmp main ; Reset vector ; rjmp main ; Reset vector
; rjmp 0xd00 ; Reset vector ; use this for flashed system rjmp 0xd00 ; Reset vector ; use this for flashed system
reti ; EXT_INT0 reti ; EXT_INT0
rjmp com2IsrPcint0 ; PCI0 rjmp com2IsrPcint0 ; PCI0
reti ; PCI1 reti ; PCI1
@@ -197,8 +197,7 @@
firmwareType: .dw FW_TYPE firmwareType: .dw FW_TYPE
firmwareVersion: .dw FW_VERSION firmwareVersion: .dw FW_VERSION
firmwareModules: .dw MODULES_MASK firmwareModules: .dw MODULES_MASK
firmwareStart: firmwareStart: rjmp main
rjmp main
; *************************************************************************** ; ***************************************************************************

View File

@@ -296,6 +296,7 @@ com2ReceivePacket_bufferAvailable:
push xl push xl
push xh push xh
lds r16, com2Address lds r16, com2Address
ldi r17, (COM2_BUFFER_SIZE-3)
rcall com2ReceivePacketRaw rcall com2ReceivePacketRaw
pop xh pop xh
pop xl pop xl

View File

@@ -15,12 +15,14 @@
; ;
; IN: ; IN:
; - R16: COM address to listen to ; - R16: COM address to listen to
; - R17: maximum value for accepted msg data (i.e. buffersize minus 3)
; - X : buffer to receive to ; - X : buffer to receive to
; OUT: ; OUT:
; - CFLAG: set if okay (packet received), cleared on error ; - CFLAG: set if okay (packet received), cleared on error
; - R16: error code if CFLAG is cleared ; - R16: error code if CFLAG is cleared
; REGS: r16, r17, X (r18, r19, r20, r21, r22) ; REGS: r16, r17, r18, X (r19, r20, r21, r22)
com2ReceivePacketRaw: com2ReceivePacketRaw:
mov r18, r17
push r16 push r16
; read destination address ; read destination address
rcall com2ReceiveByte ; read byte (R16, R17, R20, R21, R22) rcall com2ReceiveByte ; read byte (R16, R17, R20, R21, R22)
@@ -37,11 +39,11 @@ com2ReceivePacketRaw:
com2ReceivePacketRaw_acceptAddr: com2ReceivePacketRaw_acceptAddr:
st X+, r16 ; store dest address, lock buffer st X+, r16 ; store dest address, lock buffer
; read msg length ; read msg length
rcall com2ReceiveByte ; read packet length (R16, R17, R20, R21, R22) rcall com2ReceiveByte ; read packet length (R16, R17, R20, R21, R22)
brcc com2ReceivePacketRaw_ioError brcc com2ReceivePacketRaw_ioError
st X+, r16 st X+, r16
cpi r16, (COM2_BUFFER_SIZE-3) cp r16, r18 ; (COM2_BUFFER_SIZE-3)
brcc com2ReceivePacketRaw_contentError ; packet too long brcc com2ReceivePacketRaw_contentError ; packet too long
inc r16 ; account for checksum byte inc r16 ; account for checksum byte
mov r17, r16 mov r17, r16
com2ReceivePacketRaw_loop: com2ReceivePacketRaw_loop:

View File

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

View File

@@ -12,6 +12,7 @@
.equ FLASH_ERROR_NONE = 0 .equ FLASH_ERROR_NONE = 0
.equ FLASH_ERROR_MSGERROR = 1 .equ FLASH_ERROR_MSGERROR = 1
.equ FLASH_RECVBUFFER_MAXLEN = 128
.equ FLASH_CMD_FLASH_RSP = 74 .equ FLASH_CMD_FLASH_RSP = 74
@@ -37,8 +38,8 @@
flashDataBegin: flashDataBegin:
flashUid: .byte 4 flashUid: .byte 4
flashSendBuffer: .byte 64 flashSendBuffer: .byte 32
flashRecvBuffer: .byte 64 flashRecvBuffer: .byte FLASH_RECVBUFFER_MAXLEN
flashDataEnd: flashDataEnd:
@@ -120,21 +121,12 @@ bootLoader_loop1:
; try to start firmware ; try to start firmware
bootLoader_startFirmware: bootLoader_startFirmware:
sbi PORTA, PORTA3 ; LED off sbi PORTA, PORTA3 ; LED off
cbi PORTA, PORTA2 ; on (debug)
ldi r16, 20 ldi r16, 20
rcall flashWaitForMulti100ms rcall flashWaitForMulti100ms
rjmp firmwareStart
ldi zl, LOW(firmwareStart*2)
ldi zh, HIGH(firmwareStart*2)
lpm r20, Z+
lpm r21, Z+
mov r16, r20
or r16, r21
breq bootLoader ; restart boot loader
; jmp via stack
push r20
push r21
ret
bootLoader_waitAndRestartBootLoader: bootLoader_waitAndRestartBootLoader:
sbi PORTA, PORTA3 ; LED off sbi PORTA, PORTA3 ; LED off
ldi r16, 20 ldi r16, 20
@@ -228,6 +220,7 @@ flashWaitForSpecificMessage_loop1:
flashWaitForSpecificMessage_isLow: ; is low, receive message, check for msg type flashWaitForSpecificMessage_isLow: ; is low, receive message, check for msg type
push r17 push r17
ldi r16, COM2_MAINTENANCE_ADDR ldi r16, COM2_MAINTENANCE_ADDR
ldi r17, FLASH_RECVBUFFER_MAXLEN-3
ldi xl, LOW(flashRecvBuffer) ldi xl, LOW(flashRecvBuffer)
ldi xh, HIGH(flashRecvBuffer) ldi xh, HIGH(flashRecvBuffer)
rcall com2ReceivePacketRaw rcall com2ReceivePacketRaw
@@ -545,17 +538,16 @@ flashHandleFlashStart_notMe:
flashHandleFlashData: flashHandleFlashData:
adiw xh:xl, FLASH_MSG_OFFS_MSGLEN adiw xh:xl, FLASH_MSG_OFFS_MSGLEN
ld r18, X ; length ld r18, X ; length (subtract 6
cpi r18, 2+2+4 ; dest, msglen + cmd, src + addr cpi r18, 6 ; cmd(1), src(1), addr(4)
brcs flashHandleFlashData_badData brcs flashHandleFlashData_badData
subi r18, 2+2+4 ; remaining length subi r18, 6 ; remaining length
adiw xh:xl, FLASH_PACKET_DATA_OFFS_ADDR-FLASH_MSG_OFFS_MSGLEN adiw xh:xl, FLASH_PACKET_DATA_OFFS_ADDR-FLASH_MSG_OFFS_MSGLEN
ld zl, X+ ; address (low) ld zl, X+ ; address (low)
ld zh, X+ ; address (high) ld zh, X+ ; address (high)
adiw xh:xl, 2 ; ignore high bytes, points to first data byte now adiw xh:xl, 2 ; ignore high bytes, points to first data byte now
cbi PORTA, PORTA2 ; on (debug) ; rcall Flash_StartPage ; (R0, R1, R16, R20, R24, R25)
rcall Flash_StartPage ; (R0, R1, R15, R16, R20, R24, R25)
push zl push zl
push zh push zh
flashHandleFlashData_loop: flashHandleFlashData_loop:
@@ -567,7 +559,6 @@ flashHandleFlashData_loop:
pop zh pop zh
pop zl pop zl
rcall Flash_FinishPage ; (R15, R16, R20) rcall Flash_FinishPage ; (R15, R16, R20)
sbi PORTA, PORTA2 ; off (debug)
clr r16 clr r16
rjmp flashHandleFlashData_sendResponse rjmp flashHandleFlashData_sendResponse
flashHandleFlashData_badData: flashHandleFlashData_badData: