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



; ***************************************************************************
; defs


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

.cseg


; ---------------------------------------------------------------------------
; @routine Ds3231_AnnounceTime
;
; @return CFLAG on success, cleared on error
; @clobbers R16, R17, R18, R19, R20, R21, R23, R24, R25, X, Y, Z

Ds3231_AnnounceTime:
  ldi r22, 0xff
  ldi r23, NETMSG_CMD_TIME_ANNOUNCE
  clr r24
  clr r25
  rjmp ds3231SendTime
; @end



; ---------------------------------------------------------------------------
; @routine Ds3231RespondToSetTime
;
; @return CFLAG on success, cleared on error
; @param r22     dest address
; @param r25:r24 msg id/ref msg id

Ds3231RespondToSetTime:
  ldi r23, NETMSG_CMD_TIME_RSPSET
  rjmp ds3231SendTime




; ---------------------------------------------------------------------------
; @routine ds3231SendTime
;
; @return CFLAG on success, cleared on error
; @param r22     dest address
; @param r23     command
; @param r25:r24 msg id/ref msg id

ds3231SendTime:
  lds r16, ds3231Flags
  clc
  sbrs r16, DS3231_FLAGS_VALID_BIT
    rjmp Ds3231_SendTime_ret

  ; allocate network buffer
  rcall NET_Buffer_Alloc                 ; (R16, R17, X)
  brcc Ds3231_SendTime_ret

  push r16                               ; buffer number
    mov r16, r22
    adiw xh:xl, 1
    ldi yl, LOW(netInterfaceData)
    ldi yh, HIGH(netInterfaceData)
    ldi zl, LOW(ds3231RecvBuffer)
    ldi zh, HIGH(ds3231RecvBuffer)
    bigcall netMsgTimeWrite
    sbiw xh:xl, 1
  pop r16                                ; buffer number
  rcall NET_Interface_AddOrReleaseOutMsg ; (R16, R17, R18, X)
Ds3231_SendTime_ret:
  ret
; @end


#if 0
; ---------------------------------------------------------------------------
; @routine ds3231SendDebug
;
; @return CFLAG on success, cleared on error

ds3231SendDebug:
  ; allocate network buffer
  rcall NET_Buffer_Alloc                 ; (R16, R17, X)
  brcc ds3231SendDebug_ret

  push r16                               ; buffer number
    mov r16, r22
    adiw xh:xl, 1
    ldi yl, LOW(netInterfaceData)
    ldi yh, HIGH(netInterfaceData)

    ldi r20, ds3231DataEnd-ds3231DataBegin
    ldi zl, LOW(ds3231DataBegin)
    ldi zh, HIGH(ds3231DataBegin)
    bigcall NETMSG_Debug_Write2
    sbiw xh:xl, 1
  pop r16                                ; buffer number
  rcall NET_Interface_AddOrReleaseOutMsg ; (R16, R17, R18, X)
ds3231SendDebug_ret:
  ret
; @end
#endif



