HDC1080 support
[owSlave2.git] / programmer / 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 print "Open Hex File ...",\r
85 fi=open(sys.argv[1],"r")\r
86 data=[]\r
87 for l in fi.readlines():\r
88         sys.stdout.write(".")\r
89         sys.stdout.flush()\r
90         bc=int(l[1:3],16)\r
91         fw=int(l[3:7],16)\r
92         ty=int(l[7:9],16)\r
93         chsm=bc+(fw>>8)+(fw&0xFF)+ty\r
94         for i in range(bc):\r
95                 p=9+(i*2)\r
96                 d=int(l[p:p+2],16)\r
97                 chsm=(chsm+d)&0xFF\r
98                 data.append(d)\r
99         chsm=(chsm+int(l[9+bc*2:11+bc*2],16))&0xFF\r
100         if (chsm!=0):\r
101                 print "Error Checksum...."\r
102                 exit()\r
103         #print bc,fw,ty,chsm\r
104 fi.close()\r
105 print\r
106 \r
107 l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)\r
108 dc=0\r
109 dl=[]\r
110 for g in  (l.split("\n")):\r
111         if len(g)>2:\r
112                 if testnr(g[0:2]):\r
113                         dl.append(g)\r
114                         dc=dc+1\r
115                         print dc,") ",g\r
116 if dc==0:\r
117         print "No 1-Wire Device found"\r
118         exit(0)\r
119 n=int(raw_input("No. of Device: "))\r
120 n=n-1\r
121 if (n>dc-1)or(n<0):\r
122                 exit(0)\r
123 s=dl[n]\r
124 sys.stdout.write('Go to Flashmode....')\r
125 sys.stdout.flush()\r
126 owcom(s,[0x88],0)\r
127 owcom(s,[0x88],0)\r
128 owcom(s,[0x88],0)\r
129 for i in range (20):\r
130         l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)\r
131         dc=0\r
132         dl=[]\r
133         sys.stdout.write(".")\r
134         sys.stdout.flush()\r
135         for g in  (l.split("\n")):\r
136                 if (g[0:15]=="a3-55aa55aa55aa"):\r
137                         break\r
138         if (g[0:15]!="a3-55aa55aa55aa"):\r
139                 time.sleep(1)\r
140 if (g[0:15]=="a3-55aa55aa55aa"):\r
141         print "found"\r
142 else:\r
143         print "ERROR Enter Flashmode!" \r
144         exit()\r
145 f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_remove","r+b",0)\r
146 f.write(s)\r
147 f.close()\r
148 time.sleep(5)\r
149 s="a3-55aa55aa55aa"\r
150 prog=data\r
151 l=len(prog)\r
152 if (l>7616):\r
153         print "Code to big  ... Max 7616 Byte (119 Pages)"\r
154         exit()\r
155 pages= l/64\r
156 for i in range(64-(l%64)):\r
157         #print i\r
158         prog.append(0xFF)\r
159 pages= len(prog)/64\r
160 if (pages>119):\r
161         print "Code to big  ... Max 7616 Byte (119 Pages)"\r
162         exit()\r
163 \r
164 \r
165 print "Programm Page (of ", pages,")"\r
166         \r
167 for i in range(pages):\r
168         sys.stdout.write("%i " % (i+1) )\r
169         sys.stdout.flush()\r
170 \r
171         h=i*64;\r
172         hl=h&0xFF\r
173         hh=h>>8\r
174         #print hh, hl\r
175         mem=[hl,hh]+prog[h:h+64]\r
176         erroc=0\r
177         while (1):\r
178                 owcom(s,[0x0F]+mem,0) \r
179                 rmem=owcom(s,[0xAA],66)\r
180                 if (rmem!=mem):\r
181                         print rmem\r
182                         erroc=erroc+1\r
183                         if erroc>5:\r
184                                 print "WRITING ERROR ... "\r
185                                 exit()\r
186                         continue\r
187                 owcom(s,[0x55],0)       \r
188                 time.sleep(0.05)\r
189                 owcom(s,[0xB8,hl,hh],0)\r
190                 time.sleep(0.05)\r
191                 rmem=owcom(s,[0xAA],66)\r
192                 if (rmem!=mem):\r
193                         print "error in flash"\r
194                         print mem\r
195                         print rmem\r
196                         erroc=erroc+1\r
197                         if erroc>5:\r
198                                 print "WRITING ERROR ... "\r
199                                 exit()\r
200                         continue\r
201                 #for v in rmem:\r
202                 #       print "%02X " % (v),\r
203                 break\r
204 print "\nReset AVR"\r
205 owcom(s,[0x89],0)\r
206 time.sleep(1)\r
207 f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_remove","r+b",0)\r
208 f.write("a3-55aa55aa55aa")\r
209 f.close()       \r
210         \r
211         \r
212 \r
213 \r
214 \r
215 \r
216 #mem=[0x00,0x2]\r
217 #for i in range (64):\r
218 #       mem.append(i)\r
219 #owcom(s,[0x0F]+mem,0)\r
220 #rmem=owcom(s,[0xAA],70)\r
221 #print rmem\r
222 #owcom(s,[0x55],0)\r
223 #time.sleep(0.05)\r
224 #owcom(s,[0xB8,0x00,0x02],0)\r
225 #time.sleep(0.05)\r
226 #rmem=owcom(s,[0xAA],70)\r
227 #print rmem\r
228 #for v in rmem:\r
229 #       print "%02X " % (v)\r
230                 \r
231 \r
232                 \r