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