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