Change Time Values so it works for all Adapters
[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         sbic OW_PIN,OW_PINN ;warten bis leitung wieder h 
119         rjmp zeropolling_wait  ;leitung ist low ->Schleie
120         in r_temp,TCNT_REG
121         cpi r_temp,(~OWT_MIN_RESET)+OWT_ZP_WAIT_LOW_TO;aller zwei us zaehlt der timer
122         brsh  zeropolling_timeout ;Timeout Reset?
123         rjmp zeropolling
124 zeropolling_wait:
125         sbis OW_PIN,OW_PINN ;warten bis leitung wieder l
126         rjmp zeropolling_low_imp ;Leitung ist low raus aus schleufe
127         in r_temp,TCNT_REG
128         cpi r_temp,(~OWT_MIN_RESET)+OWT_ZP_WAIT_HIGH_TO ;aller zwei us zaehlt der timer
129         brsh  zeropolling_timeout ;Timeout
130         rjmp zeropolling_wait
131 zeropolling_low_imp:
132         sbi OW_DDR,OW_PINN 
133         RESETZEROMARKER 
134         rjmp int_internal_start
135
136 zeropolling_timeout:
137         //in r_temp,TCNT_REG
138         //clr r_sendflag
139         //RESETZEROMARKER 
140
141 no_zerromaker:  
142 #endif
143         CLEAR_INTERRUPT_FLAG  ; wichtig falls inzwischen wider ein Interrupt aufgelaufen ist
144         sts sendflag,r_sendflag
145         sts bcount,r_bcount
146         sts rwbyte,r_rwbyte
147         pop r_sendflag
148         pop r_rwbyte
149         pop r_bcount
150         pop r_temp
151         cdb
152         cbi OW_DDR,OW_PINN  ;vorsichtsmassname Nicht dauerhaft auf low
153         pop r_temp2                       //; die benutzten Register wiederherstellen
154     out _SFR_IO_ADDR(SREG),r_temp2
155     pop r_temp2
156     reti
157
158
159
160
161 handle_byte:
162         push zl
163         push zh
164         push r_mode
165         push r_bytep
166         //cdb
167         lds r_mode,mode
168         lds r_bytep,bytep
169
170
171         ldi zl,lo8(pm(handle_stable)) 
172         ldi zh,hi8(pm(handle_stable)) 
173         add zl,r_mode
174 #if ((handle_stable&0xFE00)!=(handle_stable_end&0xFE00))
175         ldi r_temp,0 
176         adc zh,r_temp
177 #endif
178         ijmp 
179 handle_end_sleep:
180         clr r_bcount
181         ldi r_mode,OW_SLEEP
182         clr r_sendflag
183         rjmp handle_end_no_bcount
184 handle_end_inc:
185         inc r_bytep
186 handle_end:
187         ldi r_bcount,1
188 handle_end_no_bcount:
189         sts mode,r_mode
190         sts bytep,r_bytep
191         //sdb
192         pop r_bytep
193         pop r_mode
194         pop zh
195         pop zl
196         ret