; ***************************************************************************
; 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 MEMSTATS packet.
;
; IN:
; - R16: destination address
; OUT:
; - nothing
; REGS: R3, R4, R16, R17, R18, X (R19, R20, R21)

CPRO_WriteMemStats:
  ldi r17, COM2_PAYLOAD_FLAGS_UID | COM2_PAYLOAD_FLAGS_SECONDS | (6<<COM2_PAYLOAD_FLAGS_SHIFT_NUM)
  ldi r18, CPRO_CMD_MEMSTATS
  push xh
    push xl
      rcall COM2_BeginMsgWithVariablePayload    ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
      ; payload
      ldi r20, LOW(RAMEND)
      ldi r21, HIGH(RAMEND)
      in r17, SPL
      sub r20, r17
      st X+, r20                            ; stack used
      in r17, SPH
      sbc r21, r17
      st X+, r21
      lds r17, com2RecvBuffersUsed            
      st X+, r17                             ; used buffers
      lds r17, com2MaxBuffersUsed
      st X+, r17                             ; max used buffers
      lds r17, com2StatsNoBufferError          
      st X+, r17                             ; recvNoBuffer
      lds r17, com2StatsNoBufferError+1
      st X+, r17
    pop xl
  pop xh
  rcall com2CalcAndAddChecksumByte              ; (R16, R17, R18, R19, X)
  ret



