- OWINIT save Register used in C
[owSlave2.git] / common / OWDS2423.S
1
2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved. 
4 // 
5 // Redistribution and use in source and binary forms, with or without 
6 // modification, are permitted provided that the following conditions are 
7 // met: 
8 // 
9 //  * Redistributions of source code must retain the above copyright 
10 //    notice, this list of conditions and the following disclaimer. 
11 //  * Redistributions in binary form must reproduce the above copyright 
12 //    notice, this list of conditions and the following disclaimer in the 
13 //    documentation and/or other materials provided with the 
14 //    distribution. 
15 //  * All advertising materials mentioning features or use of this 
16 //    software must display the following acknowledgement: This product 
17 //    includes software developed by tm3d.de and its contributors. 
18 //  * Neither the name of tm3d.de nor the names of its contributors may 
19 //    be used to endorse or promote products derived from this software 
20 //    without specific prior written permission. 
21 // 
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
33
34
35 #define _CHANGEABLE_ID_
36 #define _ZERO_POLLING_
37 #include "../common/OWConfig.s"
38 #include "../common/OWCRC16.s"
39
40 .extern pack,45
41 .extern counters,8
42
43 .macro CHIP_INIT        
44 .endm
45
46 .macro COMMAND_TABLE
47                 rjmp h_writescratchpad
48                 rjmp h_writescratchpad_crc
49                 rjmp h_readscratchpad
50                 rjmp h_copyscratchpad
51                 rjmp h_readmemory_addr
52                 rjmp h_readmemory
53                 rjmp h_readmemorycounter_addr
54                 rjmp h_readmemorycounter
55                 rjmp h_readmemorycounter_ex
56 .endm
57
58 #include "../common/OWRomFunctions.s"
59 #include "../common/OWTimerInterrupt.s"
60
61
62
63 ; Ab hier Geraeteabhaenging
64 #define OW_WRITE_SCRATCHPAD OW_FIRST_COMMAND+0
65 #define OW_WRITE_SCRATCHPAD_CRC OW_FIRST_COMMAND+1
66 #define OW_READ_SCRATCHPAD OW_FIRST_COMMAND+2
67 #define OW_COPY_SCRATCHPAD OW_FIRST_COMMAND+3
68 #define OW_READ_MEMORY_ADDR OW_FIRST_COMMAND+4
69 #define OW_READ_MEMORY OW_FIRST_COMMAND+5
70 #define OW_READ_MEMORYCOUNTER_ADDR OW_FIRST_COMMAND+6
71 #define OW_READ_MEMORYCOUNTER OW_FIRST_COMMAND+7
72 #define OW_READ_MEMORYCOUNTER_EX OW_FIRST_COMMAND+8
73
74 ;---------------------------------------------------
75 ;       READ COMMAND and start operation
76 ;---------------------------------------------------
77
78
79 h_readcommand:
80         clr r_bytep
81 #ifndef _DIS_FLASH_
82         FLASH_COMMANDS ; muss zu erst sein....
83 #endif
84         cset 0x0F,OW_WRITE_SCRATCHPAD
85         cjmp 0xAA,hrc_set_readscratchpad
86         cset 0x5A,OW_COPY_SCRATCHPAD
87         cset 0xF0,OW_READ_MEMORY_ADDR
88         cset 0xA5,OW_READ_MEMORYCOUNTER_ADDR
89         FW_CONFIG_INFO
90 #ifdef _CHANGEABLE_ID_
91         CHANGE_ID_COMMANDS
92 #endif
93         ldi r_mode,OW_SLEEP
94         rjmp handle_end
95
96 hrc_set_readscratchpad:
97         ldi r_mode,OW_READ_SCRATCHPAD
98         ldi r_sendflag,1
99         rjmp h_readscratchpad
100
101 h_writescratchpad:
102         configZ pack,r_bytep
103         inc  r_bytep
104         st   Z,r_rwbyte
105         cpi  r_bytep,2
106         breq h_writescratchpad_block
107         brsh h_writescratchpad_set_eoffset ;;33
108         rjmp handle_end  ;handle_end zu weit entfernt fuer br...
109 h_writescratchpad_set_eoffset:
110         cpi  r_bytep,35
111         breq h_writescratchpad_setcrc
112         mov r_temp,r_bytep
113         subi r_temp,4
114         sts pack+2,r_temp ;AA und PF cleared
115         rjmp handle_end
116 ;Start writeing to 32 Byte Block ; skip status byte     
117 h_writescratchpad_block:
118         lds r_temp,pack ; Adresse low byte
119         andi r_temp,0x1F ;32 byte
120         add r_bytep,r_temp ;Zur angegebenen Startadresse springen
121         ;ori r_temp,0x20 ; Set PF flag
122         sts pack+2,r_temp  ;E4:E0 vorher setzen
123         ; Byte 3 ueberspringen
124         rjmp handle_end_inc
125
126 h_writescratchpad_setcrc:
127         ;copy crc to pack
128         lds r_temp,crc
129         com r_temp
130         sts pack+43,r_temp
131         lds r_temp,crc+1
132         com r_temp ; invertieren , komischer name.....
133         sts pack+44,r_temp
134         ldi  r_mode,OW_WRITE_SCRATCHPAD_CRC
135         ldi r_sendflag,1
136         ldi r_bytep,43
137 h_writescratchpad_crc:
138         cpi r_bytep,45
139         breq h_writescratchpad_crc_end
140         configZ pack,r_bytep
141         ld r_rwbyte,Z
142         rjmp handle_end_inc
143 h_writescratchpad_crc_end:
144         rjmp handle_end_sleep
145         
146         
147 h_readscratchpad:
148         cpi r_bytep,35
149         breq h_readscratchpad_end
150         cpi r_bytep,3
151         brne h_readscratchpad_read_byte
152 h_readscratchpad_set_offset:
153         lds r_temp,pack
154         andi r_temp,0x1F
155         ldi r_temp2,3
156         add r_temp,r_temp2
157         mov r_bytep,r_temp
158 h_readscratchpad_read_byte:
159         configZ pack,r_bytep
160         ld r_rwbyte,Z
161         rjmp handle_end_inc
162 h_readscratchpad_end:
163         rjmp handle_end_sleep
164
165
166
167 h_copyscratchpad:
168         cpi  r_bytep,3
169         brsh h_copyscratchpad_ok
170         configZ pack,r_bytep
171         inc  r_bytep
172         ld   r_temp,Z
173         cp r_temp,r_rwbyte
174         brne h_copyscratchpad_nok
175         cpi  r_bytep,3
176         breq h_copyscratchpad_ok
177         ldi  r_bcount,1 
178         rjmp handle_end
179 h_copyscratchpad_ok:
180         ldi r_rwbyte,0xAA
181         ldi r_sendflag,1
182         rjmp handle_end
183 h_copyscratchpad_nok:
184         lds r_temp,pack+3
185         andi r_temp,~0x80
186         sts pack+3,r_temp
187         rjmp handle_end_sleep
188
189
190 h_readmemory_addr:
191         cpi r_bytep,0
192         brne h_readmrmory_addr_byte1
193         sts pack,r_rwbyte
194         rjmp handle_end_inc
195 h_readmrmory_addr_byte1:
196         sts pack+1,r_rwbyte
197         ldi r_mode,OW_READ_MEMORY
198         ldi r_sendflag,1
199         clr r_bytep
200         rjmp h_readmemory2
201 h_readmemory:
202         lds r_bytep,pack
203         lds r_temp2,pack+1
204         inc r_bytep
205         clr r_temp
206         adc r_temp2,r_temp
207         sbrc r_temp2,1
208         rjmp h_readmemory_end
209         sts pack+1,r_temp
210         sts pack,r_bytep
211 h_readmemory2:
212         lds r_bytep,pack
213         andi r_bytep,0x1F
214         configZ pack+3,r_bytep
215         ld   r_rwbyte,Z
216         rjmp handle_end
217 h_readmemory_end:
218         rjmp handle_end_sleep
219
220
221
222
223
224
225
226
227 h_readmemorycounter_addr:
228         cpi r_bytep,0
229         brne h_readmrmorycounter_addr_byte1
230         sts pack,r_rwbyte
231         inc r_bytep
232         ;ldi r_bcount,1
233         rjmp handle_end
234 h_readmrmorycounter_addr_byte1:
235         sts pack+1,r_rwbyte
236         ldi r_mode,OW_READ_MEMORYCOUNTER
237         ;ldi r_bcount,1 
238         ldi r_sendflag,1
239         clr r_bytep
240         rjmp h_readmemorycounter2
241 h_readmemorycounter:
242         lds r_bytep,pack
243         lds r_temp2,pack+1
244         ldi r_temp,1  ;inc leider kein c flag
245         add r_bytep,r_temp
246         clr r_temp
247         adc r_temp2,r_temp
248         mov r_temp,r_bytep
249         andi r_temp,0x1F
250         breq h_readmemorycounter_next
251         sts pack+1,r_temp2
252         sts pack,r_bytep
253 h_readmemorycounter2:  ;Lesen von dem worauf die erstenzwei bytes zeigen
254         lds r_bytep,pack
255         andi r_bytep,0x1F
256         configZ pack+3,r_bytep
257         ld   r_rwbyte,Z
258         ;ldi r_bcount,1
259         rjmp handle_end
260 //h_readmemorycounter_end:
261 //      ldi  r_mode,OW_SLEEP
262 //      clr r_sendflag
263 //      rjmp handle_end
264 h_readmemorycounter_next:  ; rest lesen
265         ldi  r_mode,OW_READ_MEMORYCOUNTER_EX
266         ldi r_bytep,34
267         lds r_temp2,pack
268         lds r_temp,pack+1
269         //lsr r_temp
270         //ror r_temp2
271
272         lsl r_temp2
273         rol r_temp
274         cpi r_temp,3
275         brne h_readmemorycounter_cFF
276         andi r_temp2,0xC0
277         swap r_temp2
278
279         ;cpi r_temp,0xE0
280
281         configZ counters,r_temp2
282         ld r_temp,Z+
283         sts pack+35,r_temp      
284         ld r_temp,Z+
285         sts pack+36,r_temp      
286         ld r_temp,Z+
287         sts pack+37,r_temp      
288         ld r_temp,Z+
289         sts pack+38,r_temp      
290         rjmp h_readmemorycounter_ex
291 h_readmemorycounter_cFF:
292         ldi r_temp,0xFF
293         sts pack+35,r_temp      
294         sts pack+36,r_temp      
295         sts pack+37,r_temp      
296         sts pack+38,r_temp      
297
298 h_readmemorycounter_ex:
299         inc r_bytep
300         cpi r_bytep,45
301         breq h_readmemorycounter_ex_end
302         cpi r_bytep,43
303         brne h_readmemorycounter_ex2
304         lds r_temp,crc
305         com r_temp
306         sts pack+43,r_temp
307         lds r_temp,crc+1
308         com r_temp
309         sts pack+44,r_temp
310 h_readmemorycounter_ex2:
311         ;ldi r_bcount,1
312         configZ pack,r_bytep
313         ld   r_rwbyte,Z
314         rjmp handle_end
315 h_readmemorycounter_ex_end:
316         lds r_bytep,pack
317         lds r_temp2,pack+1
318         ldi r_temp,1  ;inc leider kein c flag
319         add r_bytep,r_temp
320         clr r_temp
321         adc r_temp2,r_temp
322         sbrc r_temp2,1 ;am ene von allem 
323         rjmp h_readmemorycounter_ex_sleep
324         CRCInit1
325         ldi  r_mode,OW_READ_MEMORYCOUNTER
326         sts pack+1,r_temp2
327         sts pack,r_bytep
328         rjmp h_readmemorycounter2
329 h_readmemorycounter_ex_sleep:
330         ldi r_mode,OW_SLEEP
331         clr r_sendflag
332         rjmp handle_end
333
334
335
336         
337
338 #include "../common/OWPinInterrupt.s"
339 .end