Change Slave sends '0' during convert prozess on all DS18B20
[owSlave2.git] / common / OWPinInterrupt.s
1 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved. 
3 // 
4 // Redistribution and use in source and binary forms, with or without 
5 // modification, are permitted provided that the following conditions are 
6 // met: 
7 // 
8 //  * Redistributions of source code must retain the above copyright 
9 //    notice, this list of conditions and the following disclaimer. 
10 //  * Redistributions in binary form must reproduce the above copyright 
11 //    notice, this list of conditions and the following disclaimer in the 
12 //    documentation and/or other materials provided with the 
13 //    distribution. 
14 //  * All advertising materials mentioning features or use of this 
15 //    software must display the following acknowledgement: This product 
16 //    includes software developed by tm3d.de and its contributors. 
17 //  * Neither the name of tm3d.de nor the names of its contributors may 
18 //    be used to endorse or promote products derived from this software 
19 //    without specific prior written permission. 
20 // 
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
32
33         
34 .global PIN_INTERRIPT  
35 PIN_INTERRIPT:
36         ;leitung auf Low ziehen
37         TESTZEROMARKER ; ueberspringe wenn zeromarker=0   ;sbic und cbi/sbi andern SREG nicht
38         sbi OW_DDR,OW_PINN  
39         push r_temp2                //; wichtig: Benutzte Register und das
40     in r_temp2,_SFR_IO_ADDR(SREG)     //; Status-Register (SREG) sichern!
41         ;Zeromarker loeschen
42         RESETZEROMARKER 
43         ;Weitere Register sichern
44     push r_temp2 
45         push r_temp
46         sdb
47         ; Wegen Power Down Mode Widerherstellen der fallenden Flanke und disable von POWER DONW 
48         SET_FALLING_RESET_SLEEP
49         push r_bcount
50         push r_rwbyte
51         push r_sendflag
52         ;schauen ob noch ein Bit in der Pipeline 
53         lds r_bcount,bcount
54         lds r_rwbyte,rwbyte
55         lds r_sendflag,sendflag
56 int_internal_start:
57         ;Timer zuruecksetzen
58         ldi r_temp,~OWT_MIN_RESET
59         out TCNT_REG,r_temp
60         EN_TIM_INT
61         tst r_sendflag
62         breq receive_bit ; sendflag=0 Slave empfaengt
63
64 send_bit: ; bit senden
65         ;nachstes bit vorbereiten
66         tst r_bcount
67         brne send_bit_no_handle ;noch bits da 
68         rcall handle_byte ; neues Byte muss bearbeitet werden
69         tst r_sendflag  
70         breq send_bit_low_loop ; Nach dem Gelesen byte koennte gesendet werden muessen....
71 send_bit_no_handle:  ;noch bits da
72         ldi r_temp2,0   ; fuer die CRC berechnung in CRCS
73         ror r_rwbyte
74         brcs send_bit_no_low
75         SETZEROMARKER
76         ldi r_temp2,1 ;WICHTIG fuer CRC berechnung
77 send_bit_no_low: ;ueberspringen von r_wzero=1 wenn leitung nicht auf 0 gezogen wird
78         CRCS
79         ;naechstes byte ....
80         lsl r_bcount
81         sbis OW_DDR,OW_PINN
82         rjmp iend ;abkuerzung wenn leitung nicht low
83 send_bit_low_loop:
84         in r_temp,TCNT_REG
85         cpi r_temp,(~OWT_MIN_RESET)+OWT_WRITE ;aller zwei us zaehlt der timer
86         brlo send_bit_low_loop ;wenn kleiner 
87         cbi OW_DDR,OW_PINN  ;Leitung auf hochohmig 
88         rjmp iend
89
90 receive_bit: ;or reset 
91         ;beim lesen zuerst zeit bis zum lesen abwarten 
92         ;(9us nach den 6 us vom low impuls) 
93         in r_temp,TCNT_REG
94         cpi r_temp,(~OWT_MIN_RESET)+OWT_READ ;aller zwei us zaehlt der timer
95         brlo receive_bit ;wenn kleiner 
96         lsr r_rwbyte 
97         ldi r_temp2,1  ;fuer CRC Berechnung
98         sbis OW_PIN,OW_PINN
99         rjmp receive_bit_crc
100         ori r_rwbyte,0x80
101         ldi r_temp2,0 ;fuer CRC Berechnung
102 receive_bit_crc:
103         CRCR
104         lsl r_bcount
105         brne recive_bit_no_handle ;bcount nicht 0
106         ;naechstes byte ....
107         rcall handle_byte
108         tst r_sendflag  
109         brne send_bit ; Nach dem Gelesen byte koennte gesendet werden muessen....
110 recive_bit_no_handle:   
111 iend:
112 #ifdef _ZERO_POLLING_
113         ;--------------------------------------- Polling for set low... for 4us low impuls of DS2490
114         TESTZEROMARKER ; ueberspringe wenn zeromarker=0  
115         rjmp zeropolling
116         rjmp no_zerromaker
117 zeropolling:
118         sbrc r_sendflag,1  ; sendflag bit 1 -> switch off zeropolling in software eg for asking convert is donne (DS2450, DS18B20 and others)
119         rjmp no_zerromaker
120 zeropolling_h_loop:
121         sbic OW_PIN,OW_PINN ;warten bis leitung wieder h 
122         rjmp zeropolling_wait  ;leitung ist low ->Schleie
123         in r_temp,TCNT_REG
124         cpi r_temp,(~OWT_MIN_RESET)+OWT_ZP_WAIT_LOW_TO;aller zwei us zaehlt der timer
125         brsh  zeropolling_timeout ;Timeout Reset?
126         rjmp zeropolling_h_loop
127 zeropolling_wait:
128         sbis OW_PIN,OW_PINN ;warten bis leitung wieder l
129         rjmp zeropolling_low_imp ;Leitung ist low raus aus schleufe
130         in r_temp,TCNT_REG
131         cpi r_temp,(~OWT_MIN_RESET)+OWT_ZP_WAIT_HIGH_TO ;aller zwei us zaehlt der timer
132         sbis OW_PIN,OW_PINN ;noch eine Abfrage, da sonst schleife zu lang (sbis aendert keine flags)
133         rjmp zeropolling_low_imp ;Leitung ist low raus aus schleufe
134         brlo zeropolling_wait
135         rjmp zeropolling_timeout 
136 zeropolling_low_imp:
137         sbi OW_DDR,OW_PINN 
138         RESETZEROMARKER 
139         rjmp int_internal_start
140
141 zeropolling_timeout:
142         //in r_temp,TCNT_REG
143         //clr r_sendflag
144         //RESETZEROMARKER 
145
146 no_zerromaker:  
147 #endif
148         CLEAR_INTERRUPT_FLAG  ; wichtig falls inzwischen wider ein Interrupt aufgelaufen ist
149         sts sendflag,r_sendflag
150         sts bcount,r_bcount
151         sts rwbyte,r_rwbyte
152         pop r_sendflag
153         pop r_rwbyte
154         pop r_bcount
155         pop r_temp
156         cdb
157         cbi OW_DDR,OW_PINN  ;vorsichtsmassname Nicht dauerhaft auf low
158         pop r_temp2                       //; die benutzten Register wiederherstellen
159     out _SFR_IO_ADDR(SREG),r_temp2
160     pop r_temp2
161     reti
162
163
164
165
166 handle_byte:
167         push zl
168         push zh
169         push r_mode
170         push r_bytep
171         //cdb
172         lds r_mode,mode
173         lds r_bytep,bytep
174
175
176         ldi zl,lo8(pm(handle_stable)) 
177         ldi zh,hi8(pm(handle_stable)) 
178         add zl,r_mode
179 #if ((handle_stable&0xFE00)!=(handle_stable_end&0xFE00))
180         ldi r_temp,0 
181         adc zh,r_temp
182 #endif
183         ijmp 
184 handle_end_sleep:
185         clr r_bcount
186         ldi r_mode,OW_SLEEP
187         clr r_sendflag
188         rjmp handle_end_no_bcount
189 handle_end_inc:
190         inc r_bytep
191 handle_end:
192         ldi r_bcount,1
193 handle_end_no_bcount:
194         sts mode,r_mode
195         sts bytep,r_bytep
196         //sdb
197         pop r_bytep
198         pop r_mode
199         pop zh
200         pop zl
201         ret