avr: fixed crc code.

This commit is contained in:
Martin Preuss
2024-09-12 13:03:45 +02:00
parent 0107330c32
commit ec033cfd10
2 changed files with 46 additions and 11 deletions

View File

@@ -20,20 +20,23 @@
; @param X pointer to data to calc crc8 for
; @param r18 number of bytes to calc crc8 for
; @param r19 polynomial to use
; @clobbers: R16, R17, R18, R20, X
; @clobbers: R16, R17, R18, R20, R21, X
crc8Calc:
ldi r16, 0xff ; start crc
clr r16 ; start crc
crc8Calc_loop1:
ld r17, X+ ; running var
eor r16, r17
ldi r20, 8 ; counter for loop2
crc8Calc_loop2:
lsl r16
brcc crc8Calc_l1
crc8Calc_loop2: ; r16=crc so far, r17=current inbyte
mov r21, r16
lsr r16
eor r21, r17
lsr r17
andi r21, 1
breq crc8Calc_withoutPoly
eor r16, r19
crc8Calc_l1:
crc8Calc_withoutPoly:
dec r20
brne crc8Calc_loop2
dec r18
@@ -41,5 +44,3 @@ crc8Calc_l1:
ret
; @end

View File

@@ -66,8 +66,42 @@ com2CalcMsgChecksum:
sbiw xh:xl, COM2_MSG_OFFS_MSGLEN
inc r18 ; account for dest address
inc r18 ; account for msg len byte
ldi r19, COM2_CRC8_POLYNOMIAL ; polynomial
rjmp crc8Calc ; (R16, R17, R18, R20, X)
rjmp com2CrcCalc ; (R16, R17, R18, R20, X)
; @end
; ---------------------------------------------------------------------------
; @routine com2CrcCalc @global
; calc checksum using given polynomial
;
; @return r16 calculated checksum
; @return X points directly after last checked byte
; @param X pointer to data to calc crc8 for
; @param r18 number of bytes to calc crc8 for
; @param r19 polynomial to use
; @clobbers: R16, R17, R18, R20, X
com2CrcCalc:
ldi r16, 0xff ; start crc
ldi r19, COM2_CRC8_POLYNOMIAL ; polynomial
com2CrcCalc_loop1:
ld r17, X+ ; running var
eor r16, r17
ldi r20, 8 ; counter for loop2
com2CrcCalc_loop2:
lsl r16
brcc com2CrcCalc_l1
eor r16, r19
com2CrcCalc_l1:
dec r20
brne com2CrcCalc_loop2
dec r18
brne com2CrcCalc_loop1
ret
; @end