COM: Check for collisions after half a bit length.

Especially with longer cables we need to give the line some time to safely
raise the signal to high.
This commit is contained in:
Martin Preuss
2023-03-19 12:43:33 +01:00
parent 118f7605fa
commit 267b2e83ff

View File

@@ -327,7 +327,7 @@ comSendPacketHandleRepeat:
sbi COM_DDR_ATTN, COM_PINNUM_ATTN ; set ATTN as output sbi COM_DDR_ATTN, COM_PINNUM_ATTN ; set ATTN as output
Utils_WaitNanoSecs COM_BIT_LENGTH, 0, r22 ; wait for one bit duration Utils_WaitNanoSecs COM_BIT_LENGTH, 0, r22 ; wait for one bit duration
rcall comSendPacketHandleBuffer ; (R16, R17, R21, R22) rcall comSendPacketHandleBuffer ; (R16, R17, R21, R22)
cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; release ATTN line cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; release ATTN line (by setting direction to IN)
brcc comSendPacketHandleRepeat_collision brcc comSendPacketHandleRepeat_collision
; packet sent, adjust stats, release buffer ; packet sent, adjust stats, release buffer
ldi xl, LOW(comStatsPacketsOut) ldi xl, LOW(comStatsPacketsOut)
@@ -642,31 +642,32 @@ comReceivePacketToSram_error:
; MODIFIED REGS: R16, R21, R22 ; MODIFIED REGS: R16, R21, R22
comSendByte: comSendByte:
ldi r21, 8 ; +1 bits left ldi r21, 8 ; +1 bits left
; send startbit ; send startbit
cbi COM_PORT_DATA, COM_PINNUM_DATA ; +2 set DATA low cbi COM_PORT_DATA, COM_PINNUM_DATA ; +2 set DATA low
sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output
Utils_WaitNanoSecs COM_BIT_LENGTH, 5, r22 ; wait for on bit duration Utils_WaitNanoSecs COM_BIT_LENGTH, 5, r22 ; wait for one bit duration
; send remaining bits ; send data bits
comSendByte_loop: comSendByte_loop: ; 9 for low bit
lsr r16 ; 1+ bit to send -> CARRY lsr r16 ; 1+ bit to send -> CARRY
brcs comSendByte_setHigh ; HI: +2, LO: +1 brcs comSendByte_setHigh ; HI: +2, LO: +1
comSendByte_setLow: comSendByte_setLow:
sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output
nop Utils_WaitNanoSecs COM_BIT_LENGTH, 9, r22
rjmp comSendByte_waitBit ; +2 rjmp comSendByte_loopEnd ; +2
comSendByte_setHigh: comSendByte_setHigh:
cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE
nop ; +1 (to make pin change available) nop ; +1 (to make pin change available)
sbis COM_PIN_DATA, COM_PINNUM_DATA ; +1 if okay, +2 otherwise Utils_WaitNanoSecs COM_BIT_LENGTH/2, 11, r22 ; wait for half a bit length for line to safely settle
rjmp comSendByte_error ; +2 if error sbis COM_PIN_DATA, COM_PINNUM_DATA ; +1 if no skip, +2 if skipped
comSendByte_waitBit: ; 7 cycles in this loop until now rjmp comSendByte_error ; +2 if error (collision: we wanted line to be high but it is low)
Utils_WaitNanoSecs COM_BIT_LENGTH, 10, r22 Utils_WaitNanoSecs COM_BIT_LENGTH/2, 0, r22
dec r21 ; +1 comSendByte_loopEnd:
brne comSendByte_loop ; +2, sum per loop: 10 cycles dec r21 ; +1
brne comSendByte_loop ; +2, sum per loop: 10 cycles
; send stopbit ; send stopbit
cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE
Utils_WaitNanoSecs COM_BIT_LENGTH, 4, r22 Utils_WaitNanoSecs COM_BIT_LENGTH, 4, r22 ; wait for one bit length
sec sec
ret ret