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