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