Files
aqhomecontrol/avr/com_crc.asm
Martin Preuss 4e1f08b567 reworked com stack.
- prepared for use of CRC8
- organized code in more files
- recv stats message now contains crc errors and io errors
2023-04-07 19:14:05 +02:00

111 lines
2.6 KiB
NASM

; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; add checksum byte to buffer
;
; IN:
; - X : pointer to packet buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R18, X
comCalcAndAddChecksumByte:
adiw xh:xl, COM_MSG_OFFS_MSGLEN
ld r18, X ; read msg len
inc r18 ; account for dest address
inc r18 ; account for msg len bytes
sbiw xh:xl, COM_MSG_OFFS_MSGLEN
rcall cproCalcXor ; (R16, R17, R18, X)
st X, r16 ; add checksum byte
sec
ret
; ---------------------------------------------------------------------------
; check message in buffer
;
; IN:
; - X : pointer to packet buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R18, X
cproCheckMessageInBuffer:
adiw xh:xl, COM_MSG_OFFS_MSGLEN
ld r18, X ; read msg len
inc r18 ; account for dest address
inc r18 ; account for msg len bytes
sbiw xh:xl, COM_MSG_OFFS_MSGLEN
rcall cproCalcXor ; (R16, R17, R18, X)
ld r17, X
cp r16,r17 ; should be equal
brne cproCheckMessageInBuffer_error
sec
ret
cproCheckMessageInBuffer_error:
clc
ret
; ---------------------------------------------------------------------------
; calc xor checksum
;
; IN:
; - X : pointer to data to calc crc8 for
; - r18: number of bytes to calc crc8 for
; OUT:
; - r16: xor checksum
; - X : point directly behind the checked area
; MODIFIED REGS: R16, R17, R18, X
cproCalcXor:
clr r16
cproCalcXor_loop1:
ld r17, X+
eor r16, r17
dec r18
brne cproCalcXor_loop1
ret
; ---------------------------------------------------------------------------
; calc crc8 checksum using given polynomial
;
; IN:
; - X : pointer to data to calc crc8 for
; - r18: number of bytes to calc crc8 for
; - r19: polynomial to use (e.g. 0x97: HD=4 up to 119 bytes, e.g. detects all 1 to 3 bit errors)
; OUT:
; - r16: crc8 checksum
; - X : point directly behind the checked area
; MODIFIED REGS: R16, R17, R18, R20, X
cproCalcCrc8:
ldi r16, 0xff ; crc
cproCalcCrc8_loop1:
ld r17, X+ ; running var
eor r16, r17
ldi r20, 8 ; counter for loop2
cproCalcCrc8_loop2:
lsl r16
brcc cproCalcCrc8_l1
eor r16, r19
cproCalcCrc8_l1:
dec r20
brne cproCalcCrc8_loop2
dec r18
brne cproCalcCrc8_loop1
ret