; *************************************************************************** ; 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_Common_AddUidToBuffer @global ; ; @return X points directly behind the UID in buffer ; @param X current buffer position to write UID to ; @clobbers R16, R18, R19, R20, R21 NETMSG_Common_AddUidToBuffer: push xh push xl rcall Utils_ReadUid ; (R16, X) pop xl pop xh st X+, r18 st X+, r19 st X+, r20 st X+, r21 ret ; @end ; --------------------------------------------------------------------------- ; @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 NETMSG_AddNextMsgIdToBuffer @global ; ; Write next message id (2 bytes) into buffer given by X, advance X. ; ; @return X points to behind written message id ; @param X pointer to packet buffer ; @param Y pointer to network interface data ; @clobbers: r16, r17, r18 NETMSG_AddNextMsgIdToBuffer: ldi r18, 1 ldd r16, Y+NET_IFACE_OFFS_LASTMSGID_LOW ldd r17, Y+NET_IFACE_OFFS_LASTMSGID_HIGH add r16, r18 dec r18 adc r17, r18 std Y+NET_IFACE_OFFS_LASTMSGID_LOW, r16 std Y+NET_IFACE_OFFS_LASTMSGID_HIGH, r17 st X+, r16 st X+, r17 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