; ***************************************************************************
; 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 Ds1307_AnnounceTime
;
; @return CFLAG on success, cleared on error
; @clobbers R16, R17, R18, R19, R20, R21, R23, R24, R25, X, Y, Z

Ds1307_AnnounceTime:
  ldi r22, 0xff
  ldi r23, NETMSG_CMD_TIME_ANNOUNCE
  clr r24
  clr r25
  rjmp ds1307SendTime
; @end



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

Ds1307RespondToSetTime:
  ldi r23, NETMSG_CMD_TIME_RSPSET
  rjmp ds1307SendTime




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

ds1307SendTime:
  lds r16, ds1307Flags
  clc
  sbrs r16, DS1307_FLAGS_VALID_BIT
    rjmp Ds1307_SendTime_ret

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

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


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

ds1307SendDebug:
  ; allocate network buffer
  rcall NET_Buffer_Alloc                 ; (R16, R17, X)
  brcc ds1307SendDebug_ret

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

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



