avr: refactor ctc calculation code.

This commit is contained in:
Martin Preuss
2024-09-05 18:48:11 +02:00
parent cea3137b5a
commit 7745accfae

View File

@@ -14,19 +14,15 @@
; add checksum byte to buffer ; add checksum byte to buffer
; ;
; IN: ; IN:
; - X : pointer to packet buffer ; - X : pointer to packet buffer (points behind checksum upon return)
; OUT: ; OUT:
; - CFLAG: set if okay, clear otherwise ; - CFLAG: set if okay, clear otherwise
; - X : points behind checksum byte pos upon return
; MODIFIED REGS: R16, R17, R18, R19, X ; MODIFIED REGS: R16, R17, R18, R19, X
com2CalcAndAddChecksumByte: com2CalcAndAddChecksumByte:
adiw xh:xl, COM2_MSG_OFFS_MSGLEN rcall com2CalcMsgChecksum
ld r18, X ; read msg len st X+, r16 ; add checksum byte
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
sec sec
ret ret
@@ -39,15 +35,10 @@ com2CalcAndAddChecksumByte:
; - X : pointer to packet buffer ; - X : pointer to packet buffer
; OUT: ; OUT:
; - CFLAG: set if okay, clear otherwise ; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R18, R19, R20, X ; MODIFIED REGS: R16, R17 (R18, R20, X)
com2CheckMessageInBuffer: com2CheckMessageInBuffer:
adiw xh:xl, COM2_MSG_OFFS_MSGLEN rcall com2CalcMsgChecksum ; R16, R18, (R17, R20, X)
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)
ld r17, X ld r17, X
cp r16, r17 ; should be equal cp r16, r17 ; should be equal
brne com2CheckMessageInBuffer_error brne com2CheckMessageInBuffer_error
@@ -58,3 +49,25 @@ com2CheckMessageInBuffer_error:
ret 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)