avr: added time message

This commit is contained in:
Martin Preuss
2026-03-15 20:24:16 +01:00
parent 6c83991df7
commit 6d3a27977a
5 changed files with 263 additions and 0 deletions

View File

@@ -24,6 +24,9 @@
value-d.asm value-d.asm
value-r.asm value-r.asm
value-w.asm value-w.asm
time-d.asm
time-r.asm
time-w.asm
</extradist> </extradist>
</gwbuild> </gwbuild>

View File

@@ -48,6 +48,10 @@
.equ NETMSG_CMD_DATA = 110 .equ NETMSG_CMD_DATA = 110
.equ NETMSG_CMD_TIME_ANNOUNCE = 120
.equ NETMSG_CMD_TIME_REQSET = 121
.equ NETMSG_CMD_TIME_RSPSET = 122
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------

View File

@@ -0,0 +1,29 @@
; ***************************************************************************
; 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_D_ASM
#define AQH_AVR_NETMSG_TIME_D_ASM
; YYYYMMDDwhhmmss
.equ NETMSG_TIME_OFFS_UID = NETMSG_OFFS_PAYLOAD
.equ NETMSG_TIME_OFFS_MSGID = NETMSG_OFFS_PAYLOAD+4
.equ NETMSG_TIME_OFFS_YEAR = NETMSG_OFFS_PAYLOAD+6
.equ NETMSG_TIME_OFFS_MONTH = NETMSG_OFFS_PAYLOAD+8
.equ NETMSG_TIME_OFFS_DAYOFMONTH = NETMSG_OFFS_PAYLOAD+9
.equ NETMSG_TIME_OFFS_DAYOFWEEK = NETMSG_OFFS_PAYLOAD+10
.equ NETMSG_TIME_OFFS_HOUR = NETMSG_OFFS_PAYLOAD+11
.equ NETMSG_TIME_OFFS_MINUTES = NETMSG_OFFS_PAYLOAD+12
.equ NETMSG_TIME_OFFS_SECONDS = NETMSG_OFFS_PAYLOAD+13
.equ NETMSG_TIME_SIZE = NETMSG_OFFS_PAYLOAD+14 ; size without CRC byte
#endif

View File

@@ -0,0 +1,105 @@
; ***************************************************************************
; 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_R_ASM
#define AQH_AVR_NETMSG_TIME_R_ASM
.include "common/divide8.asm"
; ---------------------------------------------------------------------------
; @routine netMsgTimeWrite @global
; Write a packet (including the checksum byte).
;
; @return r22 source address
; @return r23 command
; @return r25:r24 message id
; @param X buffer to read from
; @param Z pointer to buffer for RTC DS3231 format of time
netMsgTimeRead:
adiw xh:xl, NETMSG_OFFS_MSGDATA ; skip DEST, LEN 0
ld r23, X+ ; command 2
ld r22, X+ ; src address 3
adiw xh:xl, 4 ; skip uid
ld r24, X+ ; msg id
ld r25, X+
; X=payload
adiw zh:zl, 7 ; point to one byte behind year
; read year (only last two digits)
ld r16, X+
ld r17, X+
ldi r18, LOW(2000)
ldi r19, HIGH(2000)
sub r16, r18
sbc r17, r19
rcall toBcd
st -Z, r16
; read month
ld r16, X+
rcall toBcd
st -Z, r16
; read day of month
ld r16, X+
rcall toBcd
st -Z, r16
; read day of week
ld r16, X+
rcall toBcd
st -Z, r16
; read hours
ld r16, X+
rcall toBcd
st -Z, r16
; read minutes
ld r16, X+
rcall toBcd
st -Z, r16
; read seconds
ld r16, X+
rcall toBcd
st -Z, r16
sbiw xh:xl, NETMSG_TIME_SIZE ; go back to beginning of message
sec
ret
; @end
; ---------------------------------------------------------------------------
; @routine toBcd
;
; Write a packet (including the checksum byte).
;
; @param r16 bin value
; @return r16 BCD value
; @clobbers r17, r18
toBcd:
mov r18, r16
ldi r19, 10
bigcall Utils_Divu8_8_8 ; r16=result, r17=remainder (R25)
lsl r16
lsl r16
lsl r16
lsl r16
add r16, r17
ret
; @end
#endif

View File

@@ -0,0 +1,122 @@
; ***************************************************************************
; 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