avr: added module ds1307.

This commit is contained in:
Martin Preuss
2026-04-27 00:39:02 +02:00
parent 14edff1369
commit accc7d41a2
9 changed files with 438 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
<?xml?>
<gwbuild>
<extradist>
main.asm
recv.asm
send.asm
</extradist>
</gwbuild>

View File

@@ -0,0 +1,226 @@
; ***************************************************************************
; 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
.equ DS1307_FLAGS_PRESENT_BIT = 7
.equ DS1307_FLAGS_VALID_BIT = 6
; ***************************************************************************
; data
.dseg
ds1307DataBegin:
ds1307Flags: .byte 1
ds1307RecvBuffer: .byte 7
ds1307DataEnd:
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; @routine Ds1307_Init @global
;
; @return CFLAG set if okay, cleared on error
; @clobbers
Ds1307_Init:
ldi xh, HIGH(ds1307DataBegin)
ldi xl, LOW(ds1307DataBegin)
clr r16
ldi r17, (ds1307DataEnd-ds1307DataBegin)
rcall Utils_FillSram
rcall ds1307CheckPresence
brcc Ds1307_Init_ret
ldi r16, (1<<DS1307_FLAGS_PRESENT_BIT)
sts ds1307Flags, r16
sec
Ds1307_Init_ret:
ret
; @end
; ---------------------------------------------------------------------------
; @routine Ds1307_Fini @global
;
; @return CFLAG set if okay, cleared on error
; @clobbers none
Ds1307_Fini:
ret
; @end
; ---------------------------------------------------------------------------
; @routine Ds1307_Fini @global
;
; @return CFLAG set if okay, cleared on error
; @clobbers
Ds1307_OnEveryMinute:
push r15
in r15, SREG
cli
lds r16, ds1307Flags
sbrs r16, DS1307_FLAGS_PRESENT_BIT
rjmp Ds1307_OnEveryMinute_ret
; clear valid bit
cbr r16, (1<<DS1307_FLAGS_VALID_BIT)
sts ds1307Flags, r16
; read time from clock
ldi xl, LOW(ds1307RecvBuffer)
ldi xh, HIGH(ds1307RecvBuffer)
clr r16
ldi r17, 7
rcall ds1307ReadData
brcc Ds1307_OnEveryMinute_ret
; set valid bit
lds r16, ds1307Flags
sbr r16, (1<<DS1307_FLAGS_VALID_BIT)
sts ds1307Flags, r16
#ifdef MODULES_NETWORK
; rcall ds1307SendDebug
rcall Ds1307_AnnounceTime
#endif
Ds1307_OnEveryMinute_ret:
out SREG, r15
pop r15
ret
; @end
; ---------------------------------------------------------------------------
; @routine ds1307CheckPresence
;
; Expects interrupts being disabled!
;
; @return CFLAG set if okay, clear on error
; @clobbers R16 (R17, R18, R22)
ds1307CheckPresence:
ldi r16, (DS1307_ADDR*2) ; write access
rjmp twiCheckPresence ; (R16, R17, R18, R22)
; @end
; ---------------------------------------------------------------------------
; @routine ds1307ReadData
;
; @param r16 start address
; @param r17 number of bytes to read
; @param X pointer to buffer to receive data
; @return CFLAG set if okay, cleared on error
; @return r16 number of bytes received if CFLAG set
; @clobbers r16, r17, r18, r19, r20, r22, X
ds1307ReadData:
mov r20, r16
mov r19, r17
rcall twiStart ; (R22)
ldi r16, (DS1307_ADDR*2) ; write access
rcall twiSendByteExpectAck ; (R16, R17, R18, R22)
brcc ds1307ReadData_error
mov r16, r20
rcall twiSendByteExpectAck
brcc ds1307ReadData_error
rcall twiRestart ; (R22)
ldi r16, (DS1307_ADDR*2)+1 ; read access
rcall twiSendByteExpectAck
brcc ds1307ReadData_error
clr r20
ds1307ReadData_loop1:
cpi r19, 1
breq ds1307ReadData_recvLast
rcall twiReceiveByteSendAck ; (R16, R17, R18, R22)
rjmp ds1307ReadData_recvd
ds1307ReadData_recvLast:
rcall twiReceiveByte ; don't send ACK (R16, R17, R18, R22)
ds1307ReadData_recvd:
brcc ds1307ReadData_error
st X+, r16
inc r20
dec r19
brne ds1307ReadData_loop1
rcall twiStop ; (R22)
mov r16, r20
sec
ret
ds1307ReadData_error:
rcall twiStop ; (R22)
clc
ret
; @end
; ---------------------------------------------------------------------------
; @routine ds1307SendData
;
; @param r16 start address
; @param r17 number of bytes to read
; @param Z pointer to buffer to send
; @return CFLAG set if okay, cleared on error
; @return r16 number of bytes received if CFLAG set
; @clobbers r16, r17, r18, r19, r20, r22, Z
ds1307SendData:
mov r20, r16
mov r19, r17
rcall twiStart ; (R22)
ldi r16, (DS1307_ADDR*2) ; write access
rcall twiSendByteExpectAck ; (R16, R17, R18, R22)
brcc ds1307SendData_error
mov r16, r20 ; address
rcall twiSendByteExpectAck ; (R16, R17, R18, R22)
brcc ds1307SendData_error
clr r20
ds1307SendData_loop1:
ld r16, Z+
rcall twiSendByteExpectAck ; (R16, R17, R18, R22)
brcc ds1307SendData_error
inc r20
dec r19
brne ds1307SendData_loop1
rcall twiStop ; (R22)
mov r16, r20
sec
ret
ds1307SendData_error:
rcall twiStop ; (R22)
clc
ret
; @end

View File

@@ -0,0 +1,57 @@
; ***************************************************************************
; 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
Ds1307_OnPacketReceived:
adiw xh:xl, NETMSG_OFFS_CMD ; command
ld r16, X
sbiw xh:xl, NETMSG_OFFS_CMD
cpi r16, NETMSG_CMD_TIME_REQSET
breq Ds1307_OnPacketReceived_set
clc ; unexpected msg
ret
Ds1307_OnPacketReceived_set:
ldi zl, LOW(ds1307RecvBuffer)
ldi zh, HIGH(ds1307RecvBuffer)
bigcall netMsgTimeRead ; r22=sender, r23=command, r25:r24=msg id
push r22
push r15
in r15, SREG
cli
clr r16
ldi r17, 7
ldi zl, LOW(ds1307RecvBuffer)
ldi zh, HIGH(ds1307RecvBuffer)
rcall ds1307SendData ; (r16, r17, r18, r19, r20, r22, Z)
out SREG, r15
pop r15
pop r22
lds r16, ds1307Flags
sbr r16, (1<<DS1307_FLAGS_VALID_BIT)
sts ds1307Flags, r16
#ifdef MODULES_NETWORK
rcall Ds1307RespondToSetTime
#endif
sec
ret
; @end

View File

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