avr: cleanup lowlevel com2 routines.

This commit is contained in:
Martin Preuss
2023-04-22 12:45:06 +02:00
parent 462515b566
commit 270066dd9f

View File

@@ -81,20 +81,20 @@ com2ReceiveByte:
ldi r21, 8 ; bits left
clr r20 ; byte currently receiving
; wait for startbit
rcall com2WaitForDataLow ; (R17)
rcall com2WaitForDataLow ; (R16, R17, R22)
brcc com2ReceiveByte_error
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 5, r22 ; goto middle of startbit to maximize sync stability
com2ReceiveByte_loop:
Utils_WaitNanoSecs COM_BIT_LENGTH, 8, r22 ; 8 cycles used in the complete loop between waits
sec ; +1
sbic COM_PIN_DATA, COM_PINNUM_DATA ; LOW: +2, HIGH: +1
rjmp com2ReceiveByte_shiftIn ; HIGH: +2, rjmp, use set CFLAG
rjmp com2ReceiveByte_shiftIn ; HIGH: +2, rjmp, use set CFLAG
clc ; LOW: +1
com2ReceiveByte_shiftIn:
ror r20 ; +1
dec r21 ; +1
brne com2ReceiveByte_loop ; +2, sum per loop: 8 cycles
rcall com2WaitForDataHigh ; wait for start of stopbit
brne com2ReceiveByte_loop ; +2, sum per loop: 8 cycles
rcall com2WaitForDataHigh ; wait for start of stopbit
brcc com2ReceiveByte_error
mov r16, r20
sec
@@ -112,23 +112,11 @@ com2ReceiveByte_error:
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22
; MODIFIED REGISTERS: r16 (r17, r22)
com2WaitForDataLow:
ldi r17, COM2_MAXWAIT_US
com2WaitForDataLow_loop:
sbis COM_PIN_DATA, COM_PINNUM_DATA
rjmp com2WaitForDataLow_done
rcall com2WaitFor1000ns
dec r17
brne com2WaitForDataLow_loop
clc ; timeout
ret
com2WaitForDataLow_done:
sec ; ok
ret
clr r16
rjmp com2WaitForDataState1ms
@@ -139,23 +127,11 @@ com2WaitForDataLow_done:
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22, X
; MODIFIED REGISTERS: r16 (r17, r22)
com2WaitForDataHigh:
ldi r17, COM2_MAXWAIT_US
com2WaitForDataHigh_loop:
sbic COM_PIN_DATA, COM_PINNUM_DATA
rjmp com2WaitForDataHigh_done
rcall com2WaitFor1000ns
dec r17
brne com2WaitForDataHigh_loop
clc ; timeout
ret
com2WaitForDataHigh_done:
sec ; ok
ret
ldi r16, 0xff
rjmp com2WaitForDataState1ms
@@ -166,23 +142,11 @@ com2WaitForDataHigh_done:
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22, X
; REGS: r16 (r17, r22)
com2WaitForAttnHigh:
ldi r17, COM2_MAXWAIT_US
com2WaitForAttnHigh_loop:
sbic COM_PIN_ATTN, COM_PINNUM_ATTN
rjmp com2WaitForAttnHigh_done
rcall com2WaitFor1000ns
dec r17
brne com2WaitForAttnHigh_loop
clc ; timeout
ret
com2WaitForAttnHigh_done:
sec ; ok
ret
ldi r16, 0xff
rjmp com2WaitForAttnState1ms
@@ -215,18 +179,30 @@ com2WaitForDataState1ms_stateReached:
; ---------------------------------------------------------------------------
; comWaitFor1000ns
; com2WaitForAttnState1ms
;
; Waits for 1000 nanoseconds.
; Waits up to 100ms for high DATA line
; IN:
; - R16: state to wait for (00 for low, 0xff for high)
; OUT:
; - CFLAG: set if okay, clear otherwise
; REGS: r22
com2WaitFor1000ns:
Utils_WaitNanoSecs 1000, 7, r22 ; wait for 1000 nanosecs (minus 3 cycles for rcall, 4 for ret)
ret
; - CFLAG: set if state reached, cleared otherwise
; REGS: R17, R22
com2WaitForAttnState1ms:
ldi r17, 100
com2WaitForAttnState1ms_loop:
in r22, COM_PIN_ATTN
eor r22, r16
andi r22, (1<<COM_PINNUM_ATTN)
breq com2WaitForAttnState1ms_stateReached
Utils_WaitNanoSecs 10000, 0, r22 ; wait for 10us
dec r17
brne com2WaitForAttnState1ms_loop
clc
ret
com2WaitForAttnState1ms_stateReached:
sec
ret