com2w: added some recovery code, improved protocol.

after sending a byte wait for the same time when sending the last bit as
with the other bits (otherwise the last bit might get lost by slower
devices).
This commit is contained in:
Martin Preuss
2025-08-20 23:27:17 +02:00
parent 6a3f6be8a9
commit 3a7951be16
2 changed files with 44 additions and 14 deletions

View File

@@ -352,8 +352,8 @@ com2wSendByte:
ldi r17, 8 ldi r17, 8
com2wSendByte_loop: com2wSendByte_loop:
M_COM2WCLKSETLOW M_COM2WCLKSETLOW
out SREG, r15 ; probably enable irqs out SREG, r15 ; probably enable irqs
rcall com2wWaitTime1 ; wait for longer time (R22) rcall com2wWaitTime1 ; wait for longer time (R22)
lsr r16 lsr r16
brcs com2wSendByte_send1 brcs com2wSendByte_send1
M_COM2WDATASETLOW M_COM2WDATASETLOW
@@ -361,14 +361,16 @@ com2wSendByte_loop:
com2wSendByte_send1: com2wSendByte_send1:
M_COM2WDATASETHIGH M_COM2WDATASETHIGH
com2wSendByte_sent: com2wSendByte_sent:
Utils_WaitNanoSecs 5000, 0, r22 ; wait for very short time to ensure data is stable when clock rises Utils_WaitNanoSecs 5000, 0, r22 ; wait for very short time to ensure data is stable when clock rises
cli ; ensure time period by disabling irqs cli ; ensure time period by disabling irqs
M_COM2WCLKSETHIGH M_COM2WCLKSETHIGH
rcall com2wWaitTime2 ; wait for shorter time (R22) rcall com2wWaitTime2 ; wait for shorter time (R22)
dec r17 dec r17
brne com2wSendByte_loop brne com2wSendByte_loop
out SREG, r15 out SREG, r15
pop r15 pop r15
rcall com2wWaitTime2 ; wait for shorter time (R22)
M_COM2WDATASETHIGH ; ensure data line is high
ret ret
; @end ; @end
@@ -435,6 +437,7 @@ com2wRecvMsg_eIo:
ldi r16, NET_IFACE_OFFS_ERR_IO_LOW ldi r16, NET_IFACE_OFFS_ERR_IO_LOW
com2wRecvMsg_incCounterRet: com2wRecvMsg_incCounterRet:
rcall NET_Interface_IncCounter16 ; (R24, R25) rcall NET_Interface_IncCounter16 ; (R24, R25)
rcall com2wRecvByteWaitForQuietClk ; (r18, r20, r22)
com2wRecvMsg_clcRet: com2wRecvMsg_clcRet:
clc clc
com2wRecvMsg_end: com2wRecvMsg_end:
@@ -443,6 +446,32 @@ com2wRecvMsg_end:
; ---------------------------------------------------------------------------
; @routine com2wRecvByteWaitForQuietClk
;
; Wait until the clock line is consistently high for 1ms.
;
; @clobbers r18, r20, r22
com2wRecvByteWaitForQuietClk:
ldi r18, 200
com2wRecvByteWaitForQuietClk_loop:
ldi r20, 100 ; wait up to 1ms for clock high (R20, R22)
rcall com2wWaitForClockLowMulti10Us
brcs com2wRecvByteWaitForQuietClk_waitForLow
dec r18
brne com2wRecvByteWaitForQuietClk_loop
rjmp com2wRecvByteWaitForQuietClk_ret
com2wRecvByteWaitForQuietClk_waitForLow:
ldi r20, 100 ; wait up to 1ms for clock low (R20, R22)
rcall com2wWaitForClockLowMulti10Us
brcc com2wRecvByteWaitForQuietClk_ret ; not low within 1ms, assume line is quiet
brne com2wRecvByteWaitForQuietClk_loop
com2wRecvByteWaitForQuietClk_ret:
ret
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine com2wRecvByte ; @routine com2wRecvByte
; ;
@@ -455,10 +484,10 @@ com2wRecvByte:
clr r16 clr r16
com2wRecvByte_loop: com2wRecvByte_loop:
ldi r20, 50 ; wait up to 500us for clock low ldi r20, 50 ; wait up to 500us for clock low
rcall com2wWaitForClockLowMulti10Us ; (R16, R20, R22) rcall com2wWaitForClockLowMulti10Us ; (R20, R22)
brcc com2wRecvByte_end brcc com2wRecvByte_end
ldi r20, 50 ; wait up to 500us for clock high ldi r20, 100 ; wait up to 1ms for clock high
rcall com2wWaitForClockHighMulti10Us ; (R16, R20, R22) rcall com2wWaitForClockHighMulti10Us ; (R20, R22)
brcc com2wRecvByte_end brcc com2wRecvByte_end
; handle received bit ; handle received bit
inr r18, COM_DATA_INPUT inr r18, COM_DATA_INPUT

View File

@@ -12,9 +12,8 @@
.equ COM2W_WAITTIME1 = 25000
.equ COM2W_WAITTIME1 = 17000 .equ COM2W_WAITTIME2 = 15000
.equ COM2W_WAITTIME2 = 10000
@@ -206,7 +205,7 @@ com2wSendByte:
ldi r17, 8 ldi r17, 8
com2wSendByte_loop: com2wSendByte_loop:
rcall com2wClkSetLow rcall com2wClkSetLow
rcall com2wWaitTime2 ; shorter wait period (R22) rcall com2wWaitTime2 ; shorter wait period (R22)
lsr r16 lsr r16
brcs com2wSendByte_send1 brcs com2wSendByte_send1
rcall com2wDataSetLow rcall com2wDataSetLow
@@ -214,11 +213,13 @@ com2wSendByte_loop:
com2wSendByte_send1: com2wSendByte_send1:
rcall com2wDataSetHigh rcall com2wDataSetHigh
com2wSendByte_sent: com2wSendByte_sent:
Utils_WaitNanoSecs 5000, 0, r22 ; wait for very short time to ensure data is stable when clock rises Utils_WaitNanoSecs 5000, 0, r22 ; wait for very short time to ensure data is stable when clock rises
rcall com2wClkSetHigh rcall com2wClkSetHigh
rcall com2wWaitTime2 ; shorter wait period (R22) rcall com2wWaitTime2 ; shorter wait period (R22)
dec r17 dec r17
brne com2wSendByte_loop brne com2wSendByte_loop
rcall com2wWaitTime2 ; shorter wait period (R22)
rcall com2wDataSetHigh
ret ret
; @end ; @end