718eeeac90e658142b91a32e630ed06084ab1aaf
[owSlave2.git] / programmer / programmer / programmer.asm
1 .def srbyte = r12  ;byty for search rom algorithm
2 .def writelow = r13;  marker for send low
3 .def zero =r14;  always zero 
4 .def smode=r15; if 1 then send 
5 .def temp = r16 ; 
6 .def temp2 = r17; 
7 .def mode = r18 ; 
8 .def bitp = r19 ; bit counter ... shift...
9 .def rwbyte = r21;
10 .def param = r22;
11 .def bytep = r23  ;byte counter
12 #define spmcrval  param
13
14
15 .equ OWM_READ_ROM_COMMAND=0 ; 0 wegen schnellen test ist dieser wert 0! Daturch wird die Sprungdabelle nicht verwendet
16 .equ OWM_SLEEP=1 ; Warten auf Reset
17 .equ OWM_MATCH_ROM=2
18 .equ OWM_SEARCH_ROM_S=3  ;send bit
19 .equ OWM_SEARCH_ROM_R=4  ;resive master
20 .equ OWM_READ_COMMAND=5
21 .equ OWM_WRITE_SCRATCHPAD=6
22 .equ OWM_READ_SCRATCHPAD=7
23 .equ OWM_PROGRAMM_PAGE=8
24 .equ OWM_RECALL_FLASH=9
25
26
27 .equ OW_DDR = DDRB 
28 .equ OW_PIN = PORTB2
29 .equ OW_PORT = PORTB 
30 .equ OW_PINN = PINB 
31
32 ;.equ SRAM_START = 0x60
33
34 .macro set_clock
35                 ldi temp,0x80;
36                 out CLKPR,temp
37                 ldi temp,@0
38                 out CLKPR,temp
39 .endmacro 
40
41 .macro owwl
42                 sbic OW_PINN,OW_PIN
43                 rjmp pc-1
44 .endmacro 
45
46 .macro owwh
47                 sbis OW_PINN,OW_PIN
48                 rjmp pc-1
49 .endmacro 
50
51
52
53
54  ;---------------------------------------------------
55 ; START of PROG 
56 ;---------------------------------------------------
57
58
59 .CSEG 
60 .ORG 0x000
61 jreset:
62                 rjmp start ; Reset-Vector 
63                 reti ; 
64                 reti ; 
65                 reti ; 
66                 reti ; 
67                 reti ; 
68                 reti ; 
69                 reti ; 
70                 reti ; 
71                 reti ; 
72                 reti ; 
73                 reti ; 
74                 reti ; 
75                 reti ; 
76                 reti ; 
77                 reti ; 
78                 reti ; 
79                 reti ; 
80                 reti ; 
81                 reti ; 
82                 reti ; 
83
84
85 .ORG 0x0EE0
86
87
88 start: 
89                 cli
90                 ldi temp,0
91                 mov zero,temp
92                 set_clock 0x00 ;8mhz
93                 ldi mode,OWM_SLEEP
94                 ldi temp,(1<<CS01) //1us
95                 out TCCR0B,temp
96                 ldi temp, HIGH(RAMEND) ; HIGH-Byte der obersten RAM-Adresse 
97                 out SPH, temp 
98                 ldi temp, LOW(RAMEND) ; LOW-Byte der obersten RAM-Adresse 
99                 out SPL, temp 
100                 ;ldi temp,1
101                 ;out DDRB,temp
102                 clr writelow
103                 clr bitp
104         
105                 ;sbi PORTB,0
106                 ldi ZL,low(pro_owid*2) 
107                 ldi ZH,high(pro_owid*2) 
108                 ldi XL,low(sowid)
109                 ldi XH,high(sowid)
110                 ;ldi temp2,8
111 pro_copy_loop: ;copy ID on SRAM for better handling
112                 lpm temp,Z+
113                 st X+,temp
114                 cpi XL,SRAM_START+8
115                 brlo pro_copy_loop
116 pro_loop:
117                 ;sbi PORTB,0
118                 owwl                            ;wait for line goes low (polling)
119                 sbrs writelow,0         ;test of zero send
120                 rjmp pro_loop1          ;no ? goes next
121                 sbi OW_DDR,OW_PIN       ;yes pull line to low
122                 ldi param,50            ;wait for 50 us
123                 rcall wait_time
124                 clr writelow            ;reset write low indecator
125                 cbi OW_DDR,OW_PIN   ;release line
126                 owwh                            ;wait for line is high (it can takes some time cause of the capacity of line)
127 pro_loop1:
128                 tst smode                       ;smode=1 for slave sends to master
129                 breq pro_loop_resv
130 pro_loop_send:
131                 tst bitp
132                 brne pro_loop_send1
133                 rcall pro_hb
134                 tst smode
135                 breq pro_loop_end ; now reading ... do nothing 
136 pro_loop_send1:  ;prebare next bit
137                 sbrs rwbyte ,0; if bit 0 set in rwbyte then skip next command
138                 inc writelow
139                 lsl bitp
140                 ror rwbyte
141                 rjmp pro_loop_end
142
143 pro_loop_resv:
144                 ldi param,15  ;wait 15us
145                 rcall wait_time
146                 lsr rwbyte
147                 ;cbi PORTB,0
148                 sbic OW_PINN,OW_PIN  ;test line
149                 ori rwbyte,0x80
150                 lsl bitp
151                 brne pro_loop_end ;no handle need
152                 rcall pro_hb
153                 tst smode
154                 brne pro_loop_send ; Nach dem Gelesen byte koennte gesendet werden muessen....
155 pro_loop_end:
156                 //owwh
157                 out TCNT0,zero
158 pro_loop_end_test_reset:
159                 sbic OW_PINN,OW_PIN  //leitung wieder high
160                 rjmp pro_loop
161                 in temp,TCNT0
162                 cpi temp,130
163                 brlo pro_loop_end_test_reset
164                 rcall pro_sleep_s2
165                 rjmp pro_loop
166
167
168
169
170 pro_sleep:
171                 out TCNT0,zero
172 pro_sleep_s1:
173                 sbic OW_PINN,OW_PIN  //leitung wieder high
174                 ret
175                 in temp,TCNT0
176                 cpi temp,200
177                 brlo pro_sleep_s1
178                 //leitung wieder high
179 pro_sleep_s2:
180                 owwh 
181                 ldi param,40
182                 rcall wait_time
183                 //Presents Impuls
184                 sbi OW_DDR,OW_PIN
185                 ldi param,130
186                 rcall wait_time
187                 cbi OW_DDR,OW_PIN
188                 //init read byte
189                 ldi bitp,0x01
190                 ldi rwbyte,0
191                 clr smode
192                 ldi mode,OWM_READ_ROM_COMMAND
193                 //Wait for all other devices presents impuls finished
194                 ldi param,40
195                 rcall wait_time
196                 ret
197
198
199
200 pro_hb:
201                 ldi ZL,low(pro_stable) 
202                 ldi ZH,high(pro_stable) 
203                 add ZL,mode 
204                 adc ZH,zero
205                 icall
206                 ret
207
208 pro_stable: 
209                 rjmp pro_read_rom_command
210                 rjmp pro_sleep 
211                 rjmp pro_match_rom
212                 rjmp pro_search_rom_s
213                 rjmp pro_search_rom_r
214                 rjmp pro_read_command
215                 rjmp pro_write_scratchpad
216                 rjmp pro_read_scratchpad
217                 rjmp pro_programm_page
218                 rjmp pro_recall_flash
219
220 pro_read_rom_command:
221                 ldi mode,OWM_SLEEP
222                 cpi rwbyte,0xCC
223                 brne pro_rcc_1
224                 ldi mode,OWM_READ_COMMAND
225                 rjmp pro_out_bitp1
226 pro_rcc_1:
227                 cpi rwbyte,0xF0
228                 brne pro_rcc_2
229                 ldi XL,low(sowid)  ;init sram pointer
230                 ldi XH,high(sowid)
231                 ld srbyte,X+
232                 ldi bytep,0
233                 rjmp pro_serchrom_next_bit
234 pro_rcc_2:
235                 cpi rwbyte,0x55
236                 brne pro_rcc_3
237 //              rcall pro_owidinit
238                 ldi XL,low(sowid)  ;init sram pointer
239                 ldi XH,high(sowid)
240                 ldi mode,OWM_MATCH_ROM
241                 rjmp pro_out_bytep0
242
243 pro_rcc_3:
244                 ret
245
246 pro_match_rom:          
247                 ld temp,X+
248                 cp temp,rwbyte
249                 breq pro_match_rom_next
250                 ldi mode,OWM_SLEEP
251                 ret
252 pro_match_rom_next:                                     
253                 cpi XL,SRAM_START+8
254                 breq pro_match_rom_found
255                 rjmp pro_out_bitp1
256 pro_match_rom_found:
257             ldi mode,OWM_READ_COMMAND
258                 rjmp pro_out_bitp1
259
260 pro_read_command:
261                 ldi mode,OWM_SLEEP
262                 cpi rwbyte,0x0F
263                 brne pro_rc_1
264                 ldi mode,OWM_WRITE_SCRATCHPAD
265                 ldi XL,low(scratchpad)  ;init sram pointer
266                 ldi XH,high(scratchpad)
267                 rjmp pro_out_bytep0
268 pro_rc_1:
269                 cpi rwbyte,0xAA
270                 brne pro_rc_2
271                 ldi mode,OWM_READ_SCRATCHPAD
272                 ldi XL,low(scratchpad)  ;init sram pointer
273                 ldi XH,high(scratchpad)
274                 inc smode
275                 ld rwbyte,X+
276                 rjmp pro_out_bytep0
277
278 pro_rc_2:
279                 cpi rwbyte,0xB8
280                 brne pro_rc_3
281                 ldi mode,OWM_RECALL_FLASH
282                 ldi XL,low(scratchpad)  ;init sram pointer
283                 ldi XH,high(scratchpad)
284                 rjmp pro_out_bytep0
285 pro_rc_3:
286                 cpi rwbyte,0x55
287                 brne pro_rc_4
288                 ldi mode,OWM_SLEEP
289                 rjmp pro_programm_page
290                 
291 pro_rc_4:
292                 cpi rwbyte,0x89
293                 brne pro_rc_5
294                 rjmp jreset             
295 pro_rc_5:
296                 cpi rwbyte,0x8B
297                 brne pro_rc_6   
298                 ldi XL,low(E2END)
299                 ldi XH,high(E2END)
300                 subi XL,7
301                 out EEARH,XH
302                 out EEARL,XL
303                 ldi temp, (0<<EEPM1)|(0<<EEPM0)
304                 out EECR, temp
305                 ldi temp,0xFF
306                 out EEDR, temp
307                 sbi EECR, EEMPE
308                 sbi EECR, EEPE
309                 ret
310
311
312 pro_rc_6:
313                 ret
314
315
316 pro_write_scratchpad:
317                 st X+,rwbyte
318                 cpi XL,SRAM_START+8+66
319                 brlo pro_write_scratchpad_next
320                 ldi mode,OWM_SLEEP
321                 ret             
322 pro_write_scratchpad_next:
323                 ldi bitp,1
324                 ret
325
326 pro_read_scratchpad:
327                 cpi XL,SRAM_START+8+66
328                 brlo pro_read_scratchpad_next
329                 ldi mode,OWM_SLEEP
330                 clr smode
331                 ret
332 pro_read_scratchpad_next:
333                 ld rwbyte,X+
334                 rjmp pro_out_bitp1
335
336
337 pro_programm_page:
338 .equ PAGESIZEB = PAGESIZE*2;PAGESIZEB is page size in BYTES, not words
339 // .org SMALLBOOTSTART
340 write_page:
341                 ;transfer data from RAM to Flash page buffer
342                 ldi bytep, PAGESIZEB ;init loop variable
343                 ldi YL,low(scratchpad)  ;init sram pointer
344                 ldi YH,high(scratchpad)
345                 ld ZL,Y+
346                 ld ZH,Y+
347                 ;page erase
348                 ldi spmcrval, (1<<PGERS) + (1<<SPMEN)
349                 rcall do_spm
350 wrloop:
351                 ld r0, Y+
352                 ld r1, Y+
353                 ldi spmcrval, (1<<SPMEN)
354                 rcall do_spm
355                 adiw ZH:ZL, 2
356                 subi bytep, 2;use subi for PAGESIZEB<=256
357                 brne wrloop
358                 ;execute page write
359                 subi ZL, low(PAGESIZEB) ;restore pointer
360                 sbci ZH, high(PAGESIZEB) ;not required for PAGESIZEB<=256
361                 ldi spmcrval, (1<<PGWRT) + (1<<SPMEN)
362                 rcall do_spm
363                 ;read back and check, optional
364                 ldi bytep, PAGESIZEB
365                 subi YL, low(PAGESIZEB) ;restore pointer
366                 sbci YH, high(PAGESIZEB)
367 rdloop:
368                 lpm r0, Z+
369                 ld r1, Y+
370                 cpse r0, r1
371                 rjmp error
372                 subi bytep, 2;use subi for PAGESIZEB<=256
373                 brne rdloop
374                 ;return
375                 ret
376 do_spm:
377                  ;input: spmcrval determines SPM action
378                 ;disable interrupts if enabled, store status
379                 in temp2, SREG
380                 cli
381                 ;check for previous SPM complete
382 wait:
383                 in temp, SPMCSR
384                 sbrc temp, SPMEN
385                 rjmp wait
386                 ;SPM timed sequence
387                 out SPMCSR, spmcrval
388                 spm
389                 ;restore SREG (to enable interrupts if originally enabled)
390                 out SREG, temp2
391                 ret
392
393 error:
394
395
396                 ret
397
398 pro_recall_flash:
399                 st X+,rwbyte
400                 ;inc bytep
401                 cpi XL,SRAM_START+8+2
402                 brlo pro_out_bitp1;pro_recall_flash_next
403                 lds ZL,scratchpad
404                 lds ZH,scratchpad+1
405 pro_recall_flash_cl:
406                 lpm temp,Z+
407                 st X+,temp
408                 cpi XL,SRAM_START+8+66
409                 brne pro_recall_flash_cl
410                 ldi mode,OWM_SLEEP
411                 ret
412
413 pro_out_read_command:
414                 ldi mode,OWM_READ_COMMAND
415 pro_out_bytep0:
416                 ldi bytep,0
417 pro_out_bitp1:
418                 ldi bitp,1
419                 ret             
420
421
422
423 pro_serchrom_next_bit:
424                 mov rwbyte,srbyte
425                 mov temp2,rwbyte
426                 com rwbyte
427                 ror temp2  ;first bit in C
428                 rol rwbyte ;C in first bit 
429                 inc smode
430                 ldi bitp,0x40
431                 ldi mode,OWM_SEARCH_ROM_R  ;next mod Resive
432                 ret
433
434
435 pro_search_rom_s:
436                 clr temp2
437                 lsr srbyte ;shift in C lowest bit
438                 ror temp2  ; shift in temp2 as highest bit
439                 andi rwbyte,0x80  ;  clear other bits
440                 eor temp2,rwbyte
441                 breq pro_search_rom_s_goon
442                 ldi mode,OWM_SLEEP
443                 ret
444 pro_search_rom_s_goon:
445                 inc bytep
446                 mov temp2,bytep
447                 andi temp2,0x07
448                 brne pro_serchrom_next_bit ;prepare next bit
449                 mov temp2,bytep
450                 andi temp2,0x40 ;;end
451                 brne pro_search_rom_found
452                 ;read next byte
453                 ld srbyte,X+
454                 rjmp pro_serchrom_next_bit
455
456 pro_search_rom_found:
457                 ldi mode,OWM_READ_COMMAND
458                 rjmp pro_out_bytep0
459
460 pro_search_rom_r:
461                 clr smode
462                 ldi mode,OWM_SEARCH_ROM_S
463                 ldi bitp,0  ;go to searchrom_s after bit get
464                 ret
465
466 pro_owid: .DB  0xA3, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xFA
467
468
469
470
471
472 wait_time:
473                 out TCNT0,zero
474 wait_time1:
475                 in temp,TCNT0
476                 cp temp,param
477                 brlo wait_time1
478                 ret
479
480
481 .DSEG
482 sowid: .BYTE 8
483 scratchpad: .BYTE 66