From 82c762678343c6bc258935e1aa027bed3eeaa045 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Thu, 20 Apr 2023 23:57:13 +0200 Subject: [PATCH] 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 --- aqhome/msg/msg_node.h | 2 +- avr/att84_base.asm | 2 +- avr/att84_temp1.asm | 7 ++-- avr/com2.asm | 1 + avr/com2_packets.asm | 10 +++--- avr/flash.asm | 74 +++++++++++++++++++------------------------ avr/flashproto.asm | 31 +++++++----------- 7 files changed, 56 insertions(+), 71 deletions(-) diff --git a/aqhome/msg/msg_node.h b/aqhome/msg/msg_node.h index 3d1f3ee..9779b16 100644 --- a/aqhome/msg/msg_node.h +++ b/aqhome/msg/msg_node.h @@ -16,7 +16,7 @@ #include -#define AQH_MAXMSGSIZE 24 +#define AQH_MAXMSGSIZE 128 #define AQH_MSG_OFFS_ALL_DEST_ADDRESS 0 diff --git a/avr/att84_base.asm b/avr/att84_base.asm index ceb9f8c..4540de9 100644 --- a/avr/att84_base.asm +++ b/avr/att84_base.asm @@ -139,7 +139,7 @@ firmwareType: .dw FW_TYPE firmwareVersion: .dw FW_VERSION firmwareModules: .dw MODULES_MASK -firmwareStart: .dw 0 ; will be overwritten when flashing +firmwareStart: rjmp main ; will be overwritten when flashing diff --git a/avr/att84_temp1.asm b/avr/att84_temp1.asm index f917ae6..78df466 100644 --- a/avr/att84_temp1.asm +++ b/avr/att84_temp1.asm @@ -174,8 +174,8 @@ ; --------------------------------------------------------------------------- ; Reset and interrupt vectors (will be removed as soon as we can flash data over COM) - rjmp main ; Reset vector -; rjmp 0xd00 ; Reset vector ; use this for flashed system +; rjmp main ; Reset vector + rjmp 0xd00 ; Reset vector ; use this for flashed system reti ; EXT_INT0 rjmp com2IsrPcint0 ; PCI0 reti ; PCI1 @@ -197,8 +197,7 @@ firmwareType: .dw FW_TYPE firmwareVersion: .dw FW_VERSION firmwareModules: .dw MODULES_MASK -firmwareStart: - rjmp main +firmwareStart: rjmp main ; *************************************************************************** diff --git a/avr/com2.asm b/avr/com2.asm index 2cb84c0..64e5ae1 100644 --- a/avr/com2.asm +++ b/avr/com2.asm @@ -296,6 +296,7 @@ com2ReceivePacket_bufferAvailable: push xl push xh lds r16, com2Address + ldi r17, (COM2_BUFFER_SIZE-3) rcall com2ReceivePacketRaw pop xh pop xl diff --git a/avr/com2_packets.asm b/avr/com2_packets.asm index 282a2d7..6887231 100644 --- a/avr/com2_packets.asm +++ b/avr/com2_packets.asm @@ -15,12 +15,14 @@ ; ; IN: ; - R16: COM address to listen to +; - R17: maximum value for accepted msg data (i.e. buffersize minus 3) ; - X : buffer to receive to ; OUT: ; - CFLAG: set if okay (packet received), cleared on error ; - 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: + mov r18, r17 push r16 ; read destination address rcall com2ReceiveByte ; read byte (R16, R17, R20, R21, R22) @@ -37,11 +39,11 @@ com2ReceivePacketRaw: com2ReceivePacketRaw_acceptAddr: st X+, r16 ; store dest address, lock buffer ; 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 st X+, r16 - cpi r16, (COM2_BUFFER_SIZE-3) - brcc com2ReceivePacketRaw_contentError ; packet too long + cp r16, r18 ; (COM2_BUFFER_SIZE-3) + brcc com2ReceivePacketRaw_contentError ; packet too long inc r16 ; account for checksum byte mov r17, r16 com2ReceivePacketRaw_loop: diff --git a/avr/flash.asm b/avr/flash.asm index 6afa180..a9fb61f 100644 --- a/avr/flash.asm +++ b/avr/flash.asm @@ -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<