b5b7b87d3cae315c59f680eeacd4545cb343b59f
[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         ; Wegen Power Down Mode Widerherstellen der fallenden Flanke und disable von POWER DONW 
47         SET_FALLING_RESET_SLEEP
48         push r_bcount
49         push r_rwbyte
50         push r_sendflag
51         ;schauen ob noch ein Bit in der Pipeline 
52         lds r_bcount,bcount
53         lds r_rwbyte,rwbyte
54         lds r_sendflag,sendflag
55 int_internal_start:
56         ;Timer zuruecksetzen
57         ldi r_temp,~OWT_MIN_RESET
58         out TCNT_REG,r_temp
59         EN_TIM_INT
60         tst r_sendflag
61         breq receive_bit ; sendflag=0 Slave empfaengt
62
63 send_bit: ; bit senden
64         ;nachstes bit vorbereiten
65         tst r_bcount
66         brne send_bit_no_handle ;noch bits da 
67         rcall handle_byte ; neues Byte muss bearbeitet werden
68         tst r_sendflag  
69         breq send_bit_low_loop ; Nach dem Gelesen byte koennte gesendet werden muessen....
70 send_bit_no_handle:  ;noch bits da
71         ldi r_temp2,0   ; fuer die CRC berechnung in CRCS
72         ror r_rwbyte
73         brcs send_bit_no_low
74         SETZEROMARKER
75         ldi r_temp2,1 ;WICHTIG fuer CRC berechnung
76 send_bit_no_low: ;ueberspringen von r_wzero=1 wenn leitung nicht auf 0 gezogen wird
77         CRCS
78         ;naechstes byte ....
79         lsl r_bcount
80         sbis OW_DDR,OW_PINN
81         rjmp iend ;abkuerzung wenn leitung nicht low
82 send_bit_low_loop:
83         in r_temp,TCNT_REG
84         cpi r_temp,(~OWT_MIN_RESET)+OWT_WRITE ;aller zwei us zaehlt der timer
85         brlo send_bit_low_loop ;wenn kleiner 
86         cbi OW_DDR,OW_PINN  ;Leitung auf hochohmig 
87         rjmp iend
88
89 receive_bit: ;or reset 
90         ;beim lesen zuerst zeit bis zum lesen abwarten 
91         ;(9us nach den 6 us vom low impuls) 
92         in r_temp,TCNT_REG
93         cpi r_temp,(~OWT_MIN_RESET)+OWT_READ ;aller zwei us zaehlt der timer
94         brlo receive_bit ;wenn kleiner 
95         lsr r_rwbyte 
96         ldi r_temp2,1  ;fuer CRC Berechnung
97         sbis OW_PIN,OW_PINN
98         rjmp receive_bit_crc
99         ori r_rwbyte,0x80
100         ldi r_temp2,0 ;fuer CRC Berechnung
101 receive_bit_crc:
102         CRCR
103         lsl r_bcount
104         brne recive_bit_no_handle ;bcount nicht 0
105         ;naechstes byte ....
106         rcall handle_byte
107         tst r_sendflag  
108         brne send_bit ; Nach dem Gelesen byte koennte gesendet werden muessen....
109 recive_bit_no_handle:   
110 iend:
111 #ifdef _ZERO_POLLING_
112         ;--------------------------------------- Polling for set low... for 4us low impuls of DS2490
113         TESTZEROMARKER ; ueberspringe wenn zeromarker=0  
114         rjmp zeropolling
115         rjmp no_zerromaker
116 zeropolling:
117         sbic OW_PIN,OW_PINN ;warten bis leitung wieder h 
118         rjmp zeropolling_wait  ;leitung ist low ->Schleie
119         in r_temp,TCNT_REG
120         cpi r_temp,(~OWT_MIN_RESET)+50;aller zwei us zaehlt der timer
121         brsh  zeropolling_timeout ;Timeout Reset?
122         rjmp zeropolling
123 zeropolling_wait:
124         sbis OW_PIN,OW_PINN ;warten bis leitung wieder l
125         rjmp zeropolling_low_imp ;Leitung ist low raus aus schleufe
126         in r_temp,TCNT_REG
127         cpi r_temp,(~OWT_MIN_RESET)+70 ;aller zwei us zaehlt der timer
128         brsh  zeropolling_timeout ;Timeout
129         rjmp zeropolling_wait
130 zeropolling_low_imp:
131         sbi OW_DDR,OW_PINN 
132         RESETZEROMARKER 
133         rjmp int_internal_start
134
135 zeropolling_timeout:
136         //in r_temp,TCNT_REG
137         //clr r_sendflag
138         //RESETZEROMARKER 
139
140 no_zerromaker:  
141 #endif
142         CLEAR_INTERRUPT_FLAG  ; wichtig falls inzwischen wider ein Interrupt aufgelaufen ist
143         sts sendflag,r_sendflag
144         sts bcount,r_bcount
145         sts rwbyte,r_rwbyte
146         //cdb
147         pop r_sendflag
148         pop r_rwbyte
149         pop r_bcount
150         pop r_temp
151         cbi OW_DDR,OW_PINN  ;vorsichtsmassname Nicht dauerhaft auf low
152         pop r_temp2                       //; die benutzten Register wiederherstellen
153     out _SFR_IO_ADDR(SREG),r_temp2
154     pop r_temp2
155     reti
156
157
158
159
160 handle_byte:
161         push zl
162         push zh
163         push r_mode
164         push r_bytep
165         //cdb
166         lds r_mode,mode
167         lds r_bytep,bytep
168
169
170         ldi zl,lo8(pm(handle_stable)) 
171         ldi zh,hi8(pm(handle_stable)) 
172         add zl,r_mode
173 #if ((handle_stable&0xFE00)!=(handle_stable_end&0xFE00))
174         ldi r_temp,0 
175         adc zh,r_temp
176 #endif
177         ijmp 
178 handle_end_sleep:
179         clr r_bcount
180         ldi r_mode,OW_SLEEP
181         clr r_sendflag
182         rjmp handle_end_no_bcount
183 handle_end_inc:
184         inc r_bytep
185 handle_end:
186         ldi r_bcount,1
187 handle_end_no_bcount:
188         sts mode,r_mode
189         sts bytep,r_bytep
190         //sdb
191         pop r_bytep
192         pop r_mode
193         pop zh
194         pop zl
195         ret