e0a97fd17d51fa9f6ad70c06509a4929e180c18e
[owSlave2.git] / DS2423 / 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 #define  _CHANGEABLE_ID_
35 #include "../common/OWConfig.s"
36 #include "../common/OWCRC16.s"
37
38 .extern pack,45
39 .extern counters,8
40
41 .macro CHIP_INIT        
42 .endm
43
44 .macro COMMAND_TABLE
45                 rjmp h_writescratchpad
46                 rjmp h_writescratchpad_crc
47                 rjmp h_readscratchpad
48                 rjmp h_copyscratchpad
49                 rjmp h_readmemory_addr
50                 rjmp h_readmemory
51                 rjmp h_readmemorycounter_addr
52                 rjmp h_readmemorycounter
53                 rjmp h_readmemorycounter_ex
54 .endm
55
56 #include "../common/OWRomFunctions.s"
57 #include "../common/OWTimerInterrupt.s"
58
59
60
61 ; Ab hier Geraeteabhaenging
62 #define OW_WRITE_SCRATCHPAD OW_FIRST_COMMAND+0
63 #define OW_WRITE_SCRATCHPAD_CRC OW_FIRST_COMMAND+1
64 #define OW_READ_SCRATCHPAD OW_FIRST_COMMAND+2
65 #define OW_COPY_SCRATCHPAD OW_FIRST_COMMAND+3
66 #define OW_READ_MEMORY_ADDR OW_FIRST_COMMAND+4
67 #define OW_READ_MEMORY OW_FIRST_COMMAND+5
68 #define OW_READ_MEMORYCOUNTER_ADDR OW_FIRST_COMMAND+6
69 #define OW_READ_MEMORYCOUNTER OW_FIRST_COMMAND+7
70 #define OW_READ_MEMORYCOUNTER_EX OW_FIRST_COMMAND+8
71
72 ;---------------------------------------------------
73 ;       READ COMMAND and start operation
74 ;---------------------------------------------------
75
76
77 h_readcommand:
78         clr r_bytep
79 #ifndef _DIS_FLASH_
80         FLASH_COMMANDS ; muss zu erst sein....
81 #endif
82         cset 0x0F,OW_WRITE_SCRATCHPAD
83         cjmp 0xAA,hrc_set_readscratchpad
84         cset 0x5A,OW_COPY_SCRATCHPAD
85         cset 0xF0,OW_READ_MEMORY_ADDR
86         cset 0xA5,OW_READ_MEMORYCOUNTER_ADDR
87 #ifdef _CHANGEABLE_ID_
88         CHANGE_ID_COMMANDS
89 #endif
90         ldi r_mode,OW_SLEEP
91         rjmp handle_end
92
93 hrc_set_readscratchpad:
94         ldi r_mode,OW_READ_SCRATCHPAD
95         ldi r_sendflag,1
96         rjmp h_readscratchpad
97
98 h_writescratchpad:
99         configZ pack,r_bytep
100         inc  r_bytep
101         st   Z,r_rwbyte
102         cpi  r_bytep,2
103         breq h_writescratchpad_block
104         brsh h_writescratchpad_set_eoffset ;;33
105         rjmp handle_end  ;handle_end zu weit entfernt fuer br...
106 h_writescratchpad_set_eoffset:
107         cpi  r_bytep,35
108         breq h_writescratchpad_setcrc
109         mov r_temp,r_bytep
110         subi r_temp,4
111         sts pack+2,r_temp ;AA und PF cleared
112         rjmp handle_end
113 ;Start writeing to 32 Byte Block ; skip status byte     
114 h_writescratchpad_block:
115         lds r_temp,pack ; Adresse low byte
116         andi r_temp,0x1F ;32 byte
117         add r_bytep,r_temp ;Zur angegebenen Startadresse springen
118         ;ori r_temp,0x20 ; Set PF flag
119         sts pack+2,r_temp  ;E4:E0 vorher setzen
120         ; Byte 3 ueberspringen
121         rjmp handle_end_inc
122
123 h_writescratchpad_setcrc:
124         ;copy crc to pack
125         lds r_temp,crc
126         com r_temp
127         sts pack+43,r_temp
128         lds r_temp,crc+1
129         com r_temp ; invertieren , komischer name.....
130         sts pack+44,r_temp
131         ldi  r_mode,OW_WRITE_SCRATCHPAD_CRC
132         ldi r_sendflag,1
133         ldi r_bytep,43
134 h_writescratchpad_crc:
135         cpi r_bytep,45
136         breq h_writescratchpad_crc_end
137         configZ pack,r_bytep
138         ld r_rwbyte,Z
139         rjmp handle_end_inc
140 h_writescratchpad_crc_end:
141         rjmp handle_end_sleep
142         
143         
144 h_readscratchpad:
145         cpi r_bytep,35
146         breq h_readscratchpad_end
147         cpi r_bytep,3
148         brne h_readscratchpad_read_byte
149 h_readscratchpad_set_offset:
150         lds r_temp,pack
151         andi r_temp,0x1F
152         ldi r_temp2,3
153         add r_temp,r_temp2
154         mov r_bytep,r_temp
155 h_readscratchpad_read_byte:
156         configZ pack,r_bytep
157         ld r_rwbyte,Z
158         rjmp handle_end_inc
159 h_readscratchpad_end:
160         rjmp handle_end_sleep
161
162
163
164 h_copyscratchpad:
165         cpi  r_bytep,3
166         brsh h_copyscratchpad_ok
167         configZ pack,r_bytep
168         inc  r_bytep
169         ld   r_temp,Z
170         cp r_temp,r_rwbyte
171         brne h_copyscratchpad_nok
172         cpi  r_bytep,3
173         breq h_copyscratchpad_ok
174         ldi  r_bcount,1 
175         rjmp handle_end
176 h_copyscratchpad_ok:
177         ldi r_rwbyte,0xAA
178         ldi r_sendflag,1
179         rjmp handle_end
180 h_copyscratchpad_nok:
181         lds r_temp,pack+3
182         andi r_temp,~0x80
183         sts pack+3,r_temp
184         rjmp handle_end_sleep
185
186
187 h_readmemory_addr:
188         cpi r_bytep,0
189         brne h_readmrmory_addr_byte1
190         sts pack,r_rwbyte
191         rjmp handle_end_inc
192 h_readmrmory_addr_byte1:
193         sts pack+1,r_rwbyte
194         ldi r_mode,OW_READ_MEMORY
195         ldi r_sendflag,1
196         clr r_bytep
197         rjmp h_readmemory2
198 h_readmemory:
199         lds r_bytep,pack
200         lds r_temp2,pack+1
201         inc r_bytep
202         clr r_temp
203         adc r_temp2,r_temp
204         sbrc r_temp2,1
205         rjmp h_readmemory_end
206         sts pack+1,r_temp
207         sts pack,r_bytep
208 h_readmemory2:
209         lds r_bytep,pack
210         andi r_bytep,0x1F
211         configZ pack+3,r_bytep
212         ld   r_rwbyte,Z
213         rjmp handle_end
214 h_readmemory_end:
215         rjmp handle_end_sleep
216
217
218
219
220
221
222
223
224 h_readmemorycounter_addr:
225         cpi r_bytep,0
226         brne h_readmrmorycounter_addr_byte1
227         sts pack,r_rwbyte
228         inc r_bytep
229         ;ldi r_bcount,1
230         rjmp handle_end
231 h_readmrmorycounter_addr_byte1:
232         sts pack+1,r_rwbyte
233         ldi r_mode,OW_READ_MEMORYCOUNTER
234         ;ldi r_bcount,1 
235         ldi r_sendflag,1
236         clr r_bytep
237         rjmp h_readmemorycounter2
238 h_readmemorycounter:
239         lds r_bytep,pack
240         lds r_temp2,pack+1
241         ldi r_temp,1  ;inc leider kein c flag
242         add r_bytep,r_temp
243         clr r_temp
244         adc r_temp2,r_temp
245         mov r_temp,r_bytep
246         andi r_temp,0x1F
247         breq h_readmemorycounter_next
248         sts pack+1,r_temp2
249         sts pack,r_bytep
250 h_readmemorycounter2:  ;Lesen von dem worauf die erstenzwei bytes zeigen
251         lds r_bytep,pack
252         andi r_bytep,0x1F
253         configZ pack+3,r_bytep
254         ld   r_rwbyte,Z
255         ;ldi r_bcount,1
256         rjmp handle_end
257 //h_readmemorycounter_end:
258 //      ldi  r_mode,OW_SLEEP
259 //      clr r_sendflag
260 //      rjmp handle_end
261 h_readmemorycounter_next:  ; rest lesen
262         ldi  r_mode,OW_READ_MEMORYCOUNTER_EX
263         ldi r_bytep,34
264         lds r_temp2,pack
265         lds r_temp,pack+1
266         //lsr r_temp
267         //ror r_temp2
268
269         lsl r_temp2
270         rol r_temp
271         cpi r_temp,3
272         brne h_readmemorycounter_cFF
273         andi r_temp2,0xC0
274         swap r_temp2
275
276         ;cpi r_temp,0xE0
277
278         configZ counters,r_temp2
279         ld r_temp,Z+
280         sts pack+35,r_temp      
281         ld r_temp,Z+
282         sts pack+36,r_temp      
283         ld r_temp,Z+
284         sts pack+37,r_temp      
285         ld r_temp,Z+
286         sts pack+38,r_temp      
287         rjmp h_readmemorycounter_ex
288 h_readmemorycounter_cFF:
289         ldi r_temp,0xFF
290         sts pack+35,r_temp      
291         sts pack+36,r_temp      
292         sts pack+37,r_temp      
293         sts pack+38,r_temp      
294
295 h_readmemorycounter_ex:
296         inc r_bytep
297         cpi r_bytep,45
298         breq h_readmemorycounter_ex_end
299         cpi r_bytep,43
300         brne h_readmemorycounter_ex2
301         lds r_temp,crc
302         com r_temp
303         sts pack+43,r_temp
304         lds r_temp,crc+1
305         com r_temp
306         sts pack+44,r_temp
307 h_readmemorycounter_ex2:
308         ;ldi r_bcount,1
309         configZ pack,r_bytep
310         ld   r_rwbyte,Z
311         rjmp handle_end
312 h_readmemorycounter_ex_end:
313         lds r_bytep,pack
314         lds r_temp2,pack+1
315         ldi r_temp,1  ;inc leider kein c flag
316         add r_bytep,r_temp
317         clr r_temp
318         adc r_temp2,r_temp
319         sbrc r_temp2,1 ;am ene von allem 
320         rjmp h_readmemorycounter_ex_sleep
321         CRCInit1
322         ldi  r_mode,OW_READ_MEMORYCOUNTER
323         sts pack+1,r_temp2
324         sts pack,r_bytep
325         rjmp h_readmemorycounter2
326 h_readmemorycounter_ex_sleep:
327         ldi r_mode,OW_SLEEP
328         clr r_sendflag
329         rjmp handle_end
330
331
332
333         
334
335 #include "../common/OWPinInterrupt.s"
336 .end