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


#ifndef AQH_AVR_NETMSG_TIME_W_ASM
#define AQH_AVR_NETMSG_TIME_W_ASM
 


; ---------------------------------------------------------------------------
; @routine netMsgTimeWrite @global
; Write a packet (including the checksum byte).
;
; @return nothing
; @param  R16     destination address
; @param  R23     msg code
; @param  R25:R24 message id
; @param  X       buffer to write to
; @param  Y       pointer to network interface
; @param  Z       pointer to RTC DS3231 format of time

netMsgTimeWrite:
  st X+, r16     ; dest address
  ldi r16, NETMSG_TIME_SIZE-2     ; msg code+src address+12 payload bytes
  st X+, r16     ; msg len

  ; begin of msg data
  st X+, r23     ; msg code
  ldd r16, Y+NET_IFACE_OFFS_ADDRESS
  st X+, r16     ; src address
  adiw xh:xl, 4  ; skip uid (4 bytes)
  st X+, r24     ; msg id (low)
  st X+, r25     ; msg id (high)

  adiw zh:zl, 7  ; point to one byte behind year

  ; write year
  ld r16, -Z           ; 6
  rcall fromBcd        ; (r17, r18)
  ldi r18, LOW(2000)
  ldi r19, HIGH(2000)
  clr r17
  add r16, r18
  adc r17, r19
  st X+, r16
  st X+, r17
  ; write month
  ld r16, -Z           ; 5
  andi r16, 0x7f       ; mask out century (ignore)
  rcall fromBcd        ; (r17, r18)
  st X+, r16
  ; write day of month
  ld r16, -Z           ; 4
  rcall fromBcd        ; (r17, r18)
  st X+, r16
  ; write day of week
  ld r16, -Z           ; 3
  rcall fromBcd        ; (r17, r18)
  st X+, r16
  ; write hours
  ld r16, -Z           ; 2
  andi r16, 0x3f       ; mask out 12/24 bit (we use the clock in 24h mode)
  rcall fromBcd        ; (r17, r18)
  st X+, r16
  ; write minutes
  ld r16, -Z           ; 1
  rcall fromBcd        ; (r17, r18)
  st X+, r16
  ; write seconds
  ld r16, -Z           ; 0
  rcall fromBcd        ; (r17, r18)
  st X+, r16
  sbiw xh:xl, NETMSG_TIME_SIZE ; go back to start

  adiw xh:xl, NETMSG_TIME_OFFS_UID                    ; go back to UID
  rcall NETMSG_Common_AddUidToBuffer                  ; (R16, R18, R19, R20, R21)
  sbiw xh:xl, (NETMSG_TIME_OFFS_UID+4)                ; go back to beginning of message
  
  ; finish msg
  rcall NETMSG_CalcAndAddChecksumByte                 ; (R16, R17, R18, R19, R20, X)
  sbiw xh:xl, (NETMSG_TIME_SIZE+1)                    ; go back to beginning of message
  ret
; @end



; ---------------------------------------------------------------------------
; @routine fromBcd
;
; Write a packet (including the checksum byte).
;
; @param r16 BCD value
; @return r16 bin value
; @clobbers r17, r18

fromBcd:
  mov r17, r16
  ; high nibble
  swap r17              ; /16
  andi r17, 0x0f
  mov r18, r17          ; x10
  lsl r17        ; x2
  lsl r17        ; x4
  add r17, r18   ; x5
  lsl r17        ; x10
  ; right digit
  andi r16, 0xf
  add r16, r17
  ret
; @end




#endif

