; ***************************************************************************
; copyright   : (C) 2023 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.   *
; ***************************************************************************




; ***************************************************************************
; code

.cseg


; ---------------------------------------------------------------------------
; Enqueue a DEVICE packet.
;
; IN:
; - R16: destination address
; OUT:
; - nothing
; REGS: R3, R4, R16, R17, R18, X (R19, R20, R21)

CPRO_WriteDevice:
  ldi r17, COM2_PAYLOAD_FLAGS_UID | (12<<COM2_PAYLOAD_FLAGS_SHIFT_NUM)
  ldi r18, CPRO_CMD_DEVICE
  rcall COM2_BeginMsgWithVariablePayload    ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
  push yh
    push yl
      ldi zh, HIGH(devInfoBlock*2)          ; 6-17: devInfoBlock
      ldi zl, LOW(devInfoBlock*2)
      ldi r18, 12
      rcall Utils_CopyFromFlash             ; (R17, R18, X, Y)
    pop yl
  pop yh

  sbiw xh:xl, 20 ; go back to beginning of message (1 byte dst addr, 1 byte length, 14 bytes payload)
  rcall com2CalcAndAddChecksumByte           ; (R16, R17, R18, R19, R20, X)
  sbiw xh:xl, 21 ; go back to beginning of message (1 byte dst addr, 1 byte length, 14 bytes payload, 1 byte crc)
  ret


