From 924c4d27e8e61c34f81c71f174869cdc16d7d942 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Tue, 25 Mar 2025 00:38:42 +0100 Subject: [PATCH] avr: added network/msg/crc.asm --- avr/modules/network/msg/0BUILD | 1 + avr/modules/network/msg/crc.asm | 103 ++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 avr/modules/network/msg/crc.asm diff --git a/avr/modules/network/msg/0BUILD b/avr/modules/network/msg/0BUILD index 42fc469..926fe04 100644 --- a/avr/modules/network/msg/0BUILD +++ b/avr/modules/network/msg/0BUILD @@ -4,6 +4,7 @@ common.asm + crc.asm defs.asm debug-w.asm device-w.asm diff --git a/avr/modules/network/msg/crc.asm b/avr/modules/network/msg/crc.asm new file mode 100644 index 0000000..c63551b --- /dev/null +++ b/avr/modules/network/msg/crc.asm @@ -0,0 +1,103 @@ +; *************************************************************************** +; copyright : (C) 2025 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. * +; *************************************************************************** + + + +; --------------------------------------------------------------------------- +; @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 + +