; ***************************************************************************
; 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_ADDR_W_ASM
#define AQH_AVR_NETWORK_MSG_ADDR_W_ASM


; ---------------------------------------------------------------------------
; @routine NETMSG_Address_Write
;
; @param Y   pointer to device to write msg for
; @param X   pointer to buffer to write to
; @param R18 command
; @param R19 address to send
; @clobbers R16, R18 (R17, R19, R20, R21, Z)

NETMSG_Address_Write:
  ldi r16, 0xff
  st X+, r16                          ; dest address
  ldi r16, 7                          ; msg code+src address+5 payload bytes
  st X+, r16                          ; msg len
  st X+, r18                          ; msg code
  ldd r16, Y+NET_IFACE_OFFS_ADDRESS
  st X+, r16                          ; src address
  mov r17, r19
  rcall NETMSG_Common_AddUidToBuffer  ; (R16, R18, R19, R20, R21)
  st X+, r17                          ; address to send
  
  sbiw xh:xl, 9  ; go back to beginning of message (1 byte dst addr, 1 byte length, 7 bytes payload)
  rcall NETMSG_CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X)
  sbiw xh:xl, 10 ; go back to beginning of message (1 byte dst addr, 1 byte length, 7 bytes payload, 1 byte crc)
  ret
; @end



#endif

