avr: fixed 1-wire protocol for multiple speeds.

fixed delays won't work with 8MHz when calibrated for 1MHz... use
wait macro instead.
This commit is contained in:
Martin Preuss
2024-10-06 01:30:52 +02:00
parent 072ed88102
commit adcb037976
2 changed files with 33 additions and 18 deletions

View File

@@ -136,7 +136,7 @@ Ds18b20_OnTimer_done:
Ds18b20_SendCommand: Ds18b20_SendCommand:
mov r18, r16 mov r18, r16
rcall OwiMaster_Reset ; (r22) rcall OwiMaster_Reset ; (r21, r22)
tst r16 ; presence detected? tst r16 ; presence detected?
breq Ds18b20_SendCommand_error ; nope, jump breq Ds18b20_SendCommand_error ; nope, jump
@@ -166,7 +166,7 @@ Ds18b20_SendCommand_error:
; @clobbers r16, r18, r19, r23, X (r17, r20, r21, r22) ; @clobbers r16, r18, r19, r23, X (r17, r20, r21, r22)
Ds18b20_ReadRom: Ds18b20_ReadRom:
rcall OwiMaster_Reset ; (r22) rcall OwiMaster_Reset ; (r21, r22)
tst r16 ; presence detected? tst r16 ; presence detected?
breq Ds18b20_ReadRom_error ; nope, jump breq Ds18b20_ReadRom_error ; nope, jump

View File

@@ -36,7 +36,7 @@ OwiMaster_Init:
; Cave: Needs interrupts to be disabled! ; Cave: Needs interrupts to be disabled!
; ;
; @return r16 0xff if slave presence signal received, 0x00 otherwise ; @return r16 0xff if slave presence signal received, 0x00 otherwise
; @clobbers r22 ; @clobbers r21, r22
OwiMaster_Reset: OwiMaster_Reset:
; send RESET pulse (min. 480usec) ; send RESET pulse (min. 480usec)
@@ -47,15 +47,30 @@ OwiMaster_Reset:
rcall Utils_WaitFor100MicroSecs rcall Utils_WaitFor100MicroSecs
rcall Utils_WaitFor100MicroSecs rcall Utils_WaitFor100MicroSecs
rcall Utils_WaitFor100MicroSecs rcall Utils_WaitFor100MicroSecs
cbi OWI_DDR, OWI_PINNUM ; set to input cbi OWI_DDR, OWI_PINNUM ; set to input
; after 16-60 uS slave pulls line low for 60-240 uS
; if present slave pulls line low for 60 usecs (we check after 30 usecs) ; if present slave pulls line low for 60 usecs (we check after 30 usecs)
Utils_WaitNanoSecs 30000, 0, r22 ldi r21, 100
clr r16 OwiMaster_Reset_LoopWaitForLow: ; wait for line low
sbic OWI_PORTIN, OWI_PINNUM Utils_WaitNanoSecs 5000, 0, r22
rjmp OwiMaster_Reset_done sbis OWI_PORTIN, OWI_PINNUM
dec r16 rjmp OwiMaster_Reset_isLow
rcall owiWaitForDataState1ms ; wait for line pulled up dec r21
brne OwiMaster_Reset_LoopWaitForLow
clr r16 ; not present
rjmp OwiMaster_Reset_done
OwiMaster_Reset_isLow:
ldi r21, 160
OwiMaster_Reset_LoopWaitForHigh: ; wait for line high
Utils_WaitNanoSecs 5000, 0, r22
sbic OWI_PORTIN, OWI_PINNUM
rjmp OwiMaster_Reset_isHigh
dec r21
brne OwiMaster_Reset_LoopWaitForHigh
clr r16 ; not present
rjmp OwiMaster_Reset_done
OwiMaster_Reset_isHigh:
ldi r16, 0xff ; presence detected
OwiMaster_Reset_done: OwiMaster_Reset_done:
ret ret
; @end ; @end
@@ -75,16 +90,16 @@ OwiMaster_SendByte:
cbi OWI_PORTOUT, OWI_PINNUM ; set value to zero cbi OWI_PORTOUT, OWI_PINNUM ; set value to zero
ldi r21, 8 ldi r21, 8
OwiMaster_SendByte_loop: OwiMaster_SendByte_loop:
sbi OWI_DDR, OWI_PINNUM ; set to output sbi OWI_DDR, OWI_PINNUM ; set to output (pull line low)
lsr r16 ; bit to send -> CARRY lsr r16 ; +1 bit to send -> CARRY
brcs OwiMaster_SendByte_setHigh brcs OwiMaster_SendByte_setHigh ; +1 / +2
OwiMaster_SendByte_setLow: OwiMaster_SendByte_setLow:
Utils_WaitNanoSecs 60000, 0, r22 ; set to low for 60 usecs ("0") Utils_WaitNanoSecs 60000, 0, r22 ; low for 60 usecs
cbi OWI_DDR, OWI_PINNUM ; set to output cbi OWI_DDR, OWI_PINNUM ; set to input (pulls line high)
Utils_WaitNanoSecs 5000, 0, r22 ; set to low for 5 usecs ("1") Utils_WaitNanoSecs 5000, 0, r22 ; wait for 5 usecs
rjmp OwiMaster_SendByte_loopEnd rjmp OwiMaster_SendByte_loopEnd
OwiMaster_SendByte_setHigh: OwiMaster_SendByte_setHigh:
Utils_WaitNanoSecs 5000, 0, r22 ; set to low for 5 usecs ("1") Utils_WaitNanoSecs 5000, 0, r22 ; keep low for 5 usecs
cbi OWI_DDR, OWI_PINNUM ; set to output cbi OWI_DDR, OWI_PINNUM ; set to output
Utils_WaitNanoSecs 60000, 0, r22 ; keep high for remainder of write slot Utils_WaitNanoSecs 60000, 0, r22 ; keep high for remainder of write slot
OwiMaster_SendByte_loopEnd: OwiMaster_SendByte_loopEnd: