First Commit
[owSlave2.git] / programmer / owflash.py
1 #!/usr/bin/python
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
36
37 import time
38 import subprocess
39 import sys
40
41 def owcom(dev,send,rc):
42         res=[]
43         f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)
44         f.write("".join(map(chr, send)))
45         if (rc!=0):
46                 res=map(ord,f.read(rc))
47         f.close()
48         return res
49                 
50
51 def crc8(arr):
52         lscrc=0x0;
53         for v in arr:
54                 bit=1;
55                 while bit<256:
56                         if (v&bit)==bit:
57                                 lb=1
58                         else:
59                                 lb=0
60                         if (lscrc&1)!=lb:
61                                 lscrc=(lscrc>>1)^0x8c 
62                         else:
63                                 lscrc=(lscrc>>1)
64                         bit=bit*2
65         return lscrc
66         
67 def testnr(s):
68         for c in s.lower()[:]:
69                 if not((c) in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','-']):
70                         return False
71         return True
72  
73 def addid(id,val):
74         for i in range(7):
75                 id[i+1]=id[i+1]+val
76                 if id[i+1]>254:
77                         id[i+1]=id[i+1]-254
78                         val=1
79                 else:
80                         return id
81         return id
82 #
83
84 print "Open Hex File ...",
85 fi=open(sys.argv[1],"r")
86 data=[]
87 for l in fi.readlines():
88         sys.stdout.write(".")
89         sys.stdout.flush()
90         bc=int(l[1:3],16)
91         fw=int(l[3:7],16)
92         ty=int(l[7:9],16)
93         chsm=bc+(fw>>8)+(fw&0xFF)+ty
94         for i in range(bc):
95                 p=9+(i*2)
96                 d=int(l[p:p+2],16)
97                 chsm=(chsm+d)&0xFF
98                 data.append(d)
99         chsm=(chsm+int(l[9+bc*2:11+bc*2],16))&0xFF
100         if (chsm!=0):
101                 print "Error Checksum...."
102                 exit()
103         #print bc,fw,ty,chsm
104 fi.close()
105 print
106
107 l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)
108 dc=0
109 dl=[]
110 for g in  (l.split("\n")):
111         if len(g)>2:
112                 if testnr(g[0:2]):
113                         dl.append(g)
114                         dc=dc+1
115                         print dc,") ",g
116 if dc==0:
117         print "No 1-Wire Device found"
118         exit(0)
119 n=int(raw_input("No. of Device: "))
120 n=n-1
121 if (n>dc-1)or(n<0):
122                 exit(0)
123 s=dl[n]
124 sys.stdout.write('Go to Flashmode....')
125 sys.stdout.flush()
126 owcom(s,[0x88],0)
127 owcom(s,[0x88],0)
128 owcom(s,[0x88],0)
129 for i in range (20):
130         l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)
131         dc=0
132         dl=[]
133         sys.stdout.write(".")
134         sys.stdout.flush()
135         for g in  (l.split("\n")):
136                 if (g[0:15]=="a3-55aa55aa55aa"):
137                         break
138         if (g[0:15]!="a3-55aa55aa55aa"):
139                 time.sleep(1)
140 if (g[0:15]=="a3-55aa55aa55aa"):
141         print "found"
142 else:
143         print "ERROR Enter Flashmode!" 
144         exit()
145 f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_remove","r+b",0)
146 f.write(s)
147 f.close()
148 time.sleep(5)
149 s="a3-55aa55aa55aa"
150 prog=data
151 l=len(prog)
152 if (l>7616):
153         print "Code to big  ... Max 7616 Byte (119 Pages)"
154         exit()
155 pages= l/64
156 for i in range(64-(l%64)):
157         #print i
158         prog.append(0xFF)
159 pages= len(prog)/64
160 if (pages>119):
161         print "Code to big  ... Max 7616 Byte (119 Pages)"
162         exit()
163
164
165 print "Programm Page (of ", pages,")"
166         
167 for i in range(pages):
168         sys.stdout.write("%i " % (i+1) )
169         sys.stdout.flush()
170
171         h=i*64;
172         hl=h&0xFF
173         hh=h>>8
174         #print hh, hl
175         mem=[hl,hh]+prog[h:h+64]
176         erroc=0
177         while (1):
178                 owcom(s,[0x0F]+mem,0) 
179                 rmem=owcom(s,[0xAA],66)
180                 if (rmem!=mem):
181                         print rmem
182                         erroc=erroc+1
183                         if erroc>5:
184                                 print "WRITING ERROR ... "
185                                 exit()
186                         continue
187                 owcom(s,[0x55],0)       
188                 time.sleep(0.05)
189                 owcom(s,[0xB8,hl,hh],0)
190                 time.sleep(0.05)
191                 rmem=owcom(s,[0xAA],66)
192                 if (rmem!=mem):
193                         print "error in flash"
194                         print mem
195                         print rmem
196                         erroc=erroc+1
197                         if erroc>5:
198                                 print "WRITING ERROR ... "
199                                 exit()
200                         continue
201                 #for v in rmem:
202                 #       print "%02X " % (v),
203                 break
204 print "\nReset AVR"
205 owcom(s,[0x89],0)
206 time.sleep(1)
207 f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_remove","r+b",0)
208 f.write("a3-55aa55aa55aa")
209 f.close()       
210         
211         
212
213
214
215
216 #mem=[0x00,0x2]
217 #for i in range (64):
218 #       mem.append(i)
219 #owcom(s,[0x0F]+mem,0)
220 #rmem=owcom(s,[0xAA],70)
221 #print rmem
222 #owcom(s,[0x55],0)
223 #time.sleep(0.05)
224 #owcom(s,[0xB8,0x00,0x02],0)
225 #time.sleep(0.05)
226 #rmem=owcom(s,[0xAA],70)
227 #print rmem
228 #for v in rmem:
229 #       print "%02X " % (v)
230                 
231
232