; ***************************************************************************
; 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


; ---------------------------------------------------------------------------
; Write a COM reception stats packet.
;
; IN:
; - R16: destination address
; - X  : buffer to write to
; OUT:
; - nothing
; REGS: R3, R4, R16, R17, R18, X (R19, R20, R21)

CPRO_WriteComRecvStats:
  ldi r17, COM2_PAYLOAD_FLAGS_UID | (12<<COM2_PAYLOAD_FLAGS_SHIFT_NUM) ; seconds + 12 bytes payload
  ldi r18, CPRO_CMD_COMRECVSTATS
  push xh
    push xl
      rcall COM2_BeginMsgWithVariablePayload    ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
      lds r16, com2StatsPacketsIn               ; packets in
      st X+, r16                             
      lds r16, com2StatsPacketsIn+1
      st X+, r16
      lds r16, com2StatsContentError            ; CRC errors
      st X+, r16
      lds r16, com2StatsContentError+1
      st X+, r16
      lds r16, com2StatsIoError                 ; IO errors
      st X+, r16
      lds r16, com2StatsIoError+1
      st X+, r16
      lds r16, com2StatsNoBufferError           ; no buffer
      st X+, r16
      lds r16, com2StatsNoBufferError+1
      st X+, r16
      lds r16, com2StatsHandled
      st X+, r16
      lds r16, com2StatsHandled+1
      st X+, r16
      lds r16, com2StatsMissed
      st X+, r16
      lds r16, com2StatsMissed+1
      st X+, r16
    pop xl
  pop xh
  rcall com2CalcAndAddChecksumByte             ; (R16, R17, R18, R19, X)
  ret




