Files
aqhomecontrol/avr/modules/network/msg/crc.asm
2026-04-26 12:47:50 +02:00

110 lines
3.1 KiB
NASM

; ***************************************************************************
; copyright : (C) 2026 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome". *
; * Please see toplevel file COPYING of that project for license details. *
; ***************************************************************************
#ifndef AQH_AVR_NETWORK_MSG_CRC_ASM
#define AQH_AVR_NETWORK_MSG_CRC_ASM
; ---------------------------------------------------------------------------
; @routine NETMSG_CalcAndAddChecksumByte @global
;
; @return CFLAG set if okay, clear otherwise
; @return X points behind checksum byte pos upon return
; @param X pointer to packet buffer (points behind checksum upon return)
; @clobbers R16, R17, R18, R19, R20, X
NETMSG_CalcAndAddChecksumByte:
rcall netMsgCalcMsgChecksum ; (R16, R17, R18, R19, R20, X)
st X+, r16 ; add checksum byte
sec
ret
; @end
; ---------------------------------------------------------------------------
; @routine NETMSG_CheckMessageInBuffer
;
; check message in buffer
;
; @return CFLAG set if okay, clear otherwise
; @param X pointer to packet buffer
; @clobbers R16, R17 (R18, R19, R20, X)
NETMSG_CheckMessageInBuffer:
rcall netMsgCalcMsgChecksum ; (R16, R17, R18, R19, R20, X)
ld r17, X
cp r16, r17 ; should be equal
brne netMsgCheckMessageInBuffer_error
sec
ret
netMsgCheckMessageInBuffer_error:
clc
ret
; @end
; ---------------------------------------------------------------------------
; @routine netMsgCalcMsgChecksum
;
; calc checksum from msg buffer (except crc byte)
;
; @return r16 crc8 checksum
; @return X points to position of checksum byte
; @param X pointer to packet buffer (points to checksum byte upon return)
; @clobbers R18 (R16, R17, R19, R20, X)
netMsgCalcMsgChecksum:
adiw xh:xl, NETMSG_OFFS_MSGLEN
ld r18, X ; read msg len
sbiw xh:xl, NETMSG_OFFS_MSGLEN
inc r18 ; account for dest address
inc r18 ; account for msg len byte
rjmp netMsgCrcCalc ; (R16, R17, R18, R19, R20, X)
; @end
; ---------------------------------------------------------------------------
; @routine netMsgCrcCalc @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
; @clobbers R16, R17, R18, R19, R20, X
netMsgCrcCalc:
ldi r16, 0xff ; start crc
ldi r19, NETMSG_CRC8_POLYNOMIAL
netMsgCrcCalc_loop1:
ld r17, X+ ; running var
eor r16, r17
ldi r20, 8 ; counter for loop2
netMsgCrcCalc_loop2:
lsl r16
brcc netMsgCrcCalc_l1
eor r16, r19
netMsgCrcCalc_l1:
dec r20
brne netMsgCrcCalc_loop2
dec r18
brne netMsgCrcCalc_loop1
ret
; @end
#endif