From 7745accfae7621871cb6f5511c9b51f41ae9b25b Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Thu, 5 Sep 2024 18:48:11 +0200 Subject: [PATCH] avr: refactor ctc calculation code. --- avr/modules/com2/crc.asm | 43 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/avr/modules/com2/crc.asm b/avr/modules/com2/crc.asm index 5cd136f..e4afb4c 100644 --- a/avr/modules/com2/crc.asm +++ b/avr/modules/com2/crc.asm @@ -14,19 +14,15 @@ ; add checksum byte to buffer ; ; IN: -; - X : pointer to packet buffer +; - X : pointer to packet buffer (points behind checksum upon return) ; OUT: ; - CFLAG: set if okay, clear otherwise +; - X : points behind checksum byte pos upon return ; MODIFIED REGS: R16, R17, R18, R19, X com2CalcAndAddChecksumByte: - adiw xh:xl, COM2_MSG_OFFS_MSGLEN - ld r18, X ; read msg len - sbiw xh:xl, COM2_MSG_OFFS_MSGLEN - inc r18 ; account for dest address - inc r18 ; account for msg len byte - rcall crc8Calc ; (R16, R17, R18, R20, X) - st X, r16 ; add checksum byte + rcall com2CalcMsgChecksum + st X+, r16 ; add checksum byte sec ret @@ -39,15 +35,10 @@ com2CalcAndAddChecksumByte: ; - X : pointer to packet buffer ; OUT: ; - CFLAG: set if okay, clear otherwise -; MODIFIED REGS: R16, R17, R18, R19, R20, X +; MODIFIED REGS: R16, R17 (R18, R20, X) com2CheckMessageInBuffer: - adiw xh:xl, COM2_MSG_OFFS_MSGLEN - ld r18, X ; read msg len - sbiw xh:xl, COM2_MSG_OFFS_MSGLEN - inc r18 ; account for dest address - inc r18 ; account for msg len byte - rcall crc8Calc ; (R16, R17, R18, R20, X) + rcall com2CalcMsgChecksum ; R16, R18, (R17, R20, X) ld r17, X cp r16, r17 ; should be equal brne com2CheckMessageInBuffer_error @@ -58,3 +49,25 @@ com2CheckMessageInBuffer_error: ret + +; --------------------------------------------------------------------------- +; com2CalcMsgChecksum +; +; calc checksum from msg buffer (except crc byte) +; +; IN: +; - X : pointer to packet buffer (points to checksum byte upon return) +; OUT: +; - r16: crc8 checksum +; - X : points to position of checksum byte +; MODIFIED REGS: R16, R18, (R17, R20, X) + +com2CalcMsgChecksum: + adiw xh:xl, COM2_MSG_OFFS_MSGLEN + ld r18, X ; read msg len + sbiw xh:xl, COM2_MSG_OFFS_MSGLEN + inc r18 ; account for dest address + inc r18 ; account for msg len byte + rjmp crc8Calc ; (R16, R17, R18, R20, X) + +