From f450ab3e672baeb92a670f72adb782602843b904 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sun, 19 Mar 2023 12:43:33 +0100 Subject: [PATCH] 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. --- avr/com.asm | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/avr/com.asm b/avr/com.asm index 2686c4b..9e7a37d 100644 --- a/avr/com.asm +++ b/avr/com.asm @@ -327,7 +327,7 @@ comSendPacketHandleRepeat: sbi COM_DDR_ATTN, COM_PINNUM_ATTN ; set ATTN as output Utils_WaitNanoSecs COM_BIT_LENGTH, 0, r22 ; wait for one bit duration 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 ; packet sent, adjust stats, release buffer ldi xl, LOW(comStatsPacketsOut) @@ -642,31 +642,32 @@ comReceivePacketToSram_error: ; MODIFIED REGS: R16, R21, R22 comSendByte: - ldi r21, 8 ; +1 bits left + ldi r21, 8 ; +1 bits left ; send startbit - cbi COM_PORT_DATA, COM_PINNUM_DATA ; +2 set DATA low - sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output - Utils_WaitNanoSecs COM_BIT_LENGTH, 5, r22 ; wait for on bit duration - ; send remaining bits -comSendByte_loop: - lsr r16 ; 1+ bit to send -> CARRY - brcs comSendByte_setHigh ; HI: +2, LO: +1 + cbi COM_PORT_DATA, COM_PINNUM_DATA ; +2 set DATA low + sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output + Utils_WaitNanoSecs COM_BIT_LENGTH, 5, r22 ; wait for one bit duration + ; send data bits +comSendByte_loop: ; 9 for low bit + lsr r16 ; 1+ bit to send -> CARRY + brcs comSendByte_setHigh ; HI: +2, LO: +1 comSendByte_setLow: - sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output - nop - rjmp comSendByte_waitBit ; +2 + sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output + Utils_WaitNanoSecs COM_BIT_LENGTH, 9, r22 + rjmp comSendByte_loopEnd ; +2 comSendByte_setHigh: - cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE - nop ; +1 (to make pin change available) - sbis COM_PIN_DATA, COM_PINNUM_DATA ; +1 if okay, +2 otherwise - rjmp comSendByte_error ; +2 if error -comSendByte_waitBit: ; 7 cycles in this loop until now - Utils_WaitNanoSecs COM_BIT_LENGTH, 10, r22 - dec r21 ; +1 - brne comSendByte_loop ; +2, sum per loop: 10 cycles + cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE + nop ; +1 (to make pin change available) + Utils_WaitNanoSecs COM_BIT_LENGTH/2, 11, r22 ; wait for half a bit length for line to safely settle + sbis COM_PIN_DATA, COM_PINNUM_DATA ; +1 if no skip, +2 if skipped + rjmp comSendByte_error ; +2 if error (collision: we wanted line to be high but it is low) + Utils_WaitNanoSecs COM_BIT_LENGTH/2, 0, r22 +comSendByte_loopEnd: + dec r21 ; +1 + brne comSendByte_loop ; +2, sum per loop: 10 cycles ; send stopbit - cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE - Utils_WaitNanoSecs COM_BIT_LENGTH, 4, r22 + cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE + Utils_WaitNanoSecs COM_BIT_LENGTH, 4, r22 ; wait for one bit length sec ret