check in owflash.py / non stop on error DS2438
authortobias <tm@tm3d.de>
Fri, 17 Feb 2017 08:29:25 +0000 (09:29 +0100)
committertobias <tm@tm3d.de>
Fri, 17 Feb 2017 08:29:25 +0000 (09:29 +0100)
owflash.py [new file with mode: 0644]
owlib.py

diff --git a/owflash.py b/owflash.py
new file mode 100644 (file)
index 0000000..90550ea
--- /dev/null
@@ -0,0 +1,268 @@
+#!/usr/bin/python\r
+# Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de\r
+# All rights reserved. \r
+# \r
+# Redistribution and use in source and binary forms, with or without \r
+# modification, are permitted provided that the following conditions are \r
+# met: \r
+# \r
+#  * Redistributions of source code must retain the above copyright \r
+#    notice, this list of conditions and the following disclaimer. \r
+#  * Redistributions in binary form must reproduce the above copyright \r
+#    notice, this list of conditions and the following disclaimer in the \r
+#    documentation and/or other materials provided with the \r
+#    distribution. \r
+#  * All advertising materials mentioning features or use of this \r
+#    software must display the following acknowledgement: This product \r
+#    includes software developed by tm3d.de and its contributors. \r
+#  * Neither the name of tm3d.de nor the names of its contributors may \r
+#    be used to endorse or promote products derived from this software \r
+#    without specific prior written permission. \r
+# \r
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR \r
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \r
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, \r
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT \r
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, \r
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY \r
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \r
+\r
+\r
+\r
+\r
+import time\r
+import subprocess\r
+import sys\r
+\r
+def owcom(dev,send,rc):\r
+       res=[]\r
+       try:\r
+               f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)\r
+       except IOError:\r
+               l=subprocess.check_output("rmmod w1_therm",shell=True)\r
+               f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)\r
+       f.write("".join(map(chr, send)))\r
+       if (rc!=0):\r
+               res=map(ord,f.read(rc))\r
+       f.close()\r
+       return res\r
+               \r
+\r
+               \r
+\r
+def crc8(arr):\r
+       lscrc=0x0;\r
+       for v in arr:\r
+               bit=1;\r
+               while bit<256:\r
+                       if (v&bit)==bit:\r
+                               lb=1\r
+                       else:\r
+                               lb=0\r
+                       if (lscrc&1)!=lb:\r
+                               lscrc=(lscrc>>1)^0x8c \r
+                       else:\r
+                               lscrc=(lscrc>>1)\r
+                       bit=bit*2\r
+       return lscrc\r
+       \r
+def testnr(s):\r
+       for c in s.lower()[:]:\r
+               if not((c) in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','-']):\r
+                       return False\r
+       return True\r
\r
+def addid(id,val):\r
+       for i in range(7):\r
+               id[i+1]=id[i+1]+val\r
+               if id[i+1]>254:\r
+                       id[i+1]=id[i+1]-254\r
+                       val=1\r
+               else:\r
+                       return id\r
+       return id\r
+#\r
+\r
+\r
+if len(sys.argv)<2:\r
+       print "A hex-File is needed! \n"\r
+       print "sudo python owflash.py test.hex"\r
+       exit(0)\r
+       \r
+\r
+\r
+\r
+#start search\r
+f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_search","r+b",0)\r
+f.write("-1")\r
+f.close()      \r
+\r
+\r
+\r
+print "Open Hex File ...",\r
+fi=open(sys.argv[1],"r")\r
+data=[]\r
+for l in fi.readlines():\r
+       sys.stdout.write(".")\r
+       sys.stdout.flush()\r
+       bc=int(l[1:3],16)\r
+       fw=int(l[3:7],16)\r
+       ty=int(l[7:9],16)\r
+       chsm=bc+(fw>>8)+(fw&0xFF)+ty\r
+       for i in range(bc):\r
+               p=9+(i*2)\r
+               d=int(l[p:p+2],16)\r
+               chsm=(chsm+d)&0xFF\r
+               data.append(d)\r
+       chsm=(chsm+int(l[9+bc*2:11+bc*2],16))&0xFF\r
+       if (chsm!=0):\r
+               print "Error Checksum...."\r
+               exit()\r
+       #print bc,fw,ty,chsm\r
+fi.close()\r
+print\r
+\r
+l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)\r
+dc=0\r
+dl=[]\r
+for g in  (l.split("\n")):\r
+       if len(g)>2:\r
+               if testnr(g[0:2]):\r
+                       dl.append(g)\r
+                       dc=dc+1\r
+                       print dc,") ",g\r
+if dc==0:\r
+       print "No 1-Wire Device found"\r
+       exit(0)\r
+n=int(raw_input("No. of Device: "))\r
+n=n-1\r
+if (n>dc-1)or(n<0):\r
+               exit(0)\r
+s=dl[n]\r
+if (s!="a3-55aa55aa55aa"):\r
+       sys.stdout.write('Go to Flashmode....')\r
+       sys.stdout.flush()\r
+       owcom(s,[0x88],0)\r
+       owcom(s,[0x88],0)\r
+       owcom(s,[0x88],0)\r
+       for i in range (20):\r
+               l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)\r
+               dc=0\r
+               dl=[]\r
+               sys.stdout.write(".")\r
+               sys.stdout.flush()\r
+               for g in  (l.split("\n")):\r
+                       if (g[0:15]=="a3-55aa55aa55aa"):\r
+                               break\r
+               if (g[0:15]!="a3-55aa55aa55aa"):\r
+                       time.sleep(1)\r
+       if (g[0:15]=="a3-55aa55aa55aa"):\r
+               print "found"\r
+       else:\r
+               print "ERROR Enter Flashmode!" \r
+               exit()\r
+       f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_remove","r+b",0)\r
+       f.write(s)\r
+       f.close()\r
+       time.sleep(5)\r
+\r
+#stop search\r
+print("Disable Device Search (Wait 15s)")\r
+f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_search","r+b",0)\r
+f.write("0")\r
+f.close()      \r
+time.sleep(15)\r
+\r
+s="a3-55aa55aa55aa"\r
+prog=data\r
+l=len(prog)\r
+if (l>7551):\r
+       print "Code to big  ... Max 7552 Byte (118 Pages)"\r
+       exit()\r
+pages= l/64\r
+for i in range(64-(l%64)):\r
+       #print i\r
+       prog.append(0xFF)\r
+pages= len(prog)/64\r
+if (pages>118):\r
+       print "Code to big  ... Max 7552 Byte (118 Pages)"\r
+       exit()\r
+\r
+\r
+print "Programm Page (of ", pages,")"\r
+       \r
+for i in range(pages):\r
+       sys.stdout.write("%i " % (i+1) )\r
+       sys.stdout.flush()\r
+\r
+       h=i*64;\r
+       hl=h&0xFF\r
+       hh=h>>8\r
+       #print hh, hl\r
+       mem=[hl,hh]+prog[h:h+64]\r
+       erroc=0\r
+       while (1):\r
+               owcom(s,[0x0F]+mem,0) \r
+               rmem=owcom(s,[0xAA],66)\r
+               if (rmem!=mem):\r
+                       print rmem\r
+                       erroc=erroc+1\r
+                       if erroc>5:\r
+                               print "WRITING ERROR ... "\r
+                               exit()\r
+                       continue\r
+               owcom(s,[0x55],0)       \r
+               time.sleep(0.05)\r
+               owcom(s,[0xB8,hl,hh],0)\r
+               time.sleep(0.05)\r
+               rmem=owcom(s,[0xAA],66)\r
+               if (rmem!=mem):\r
+                       print "error in flash"\r
+                       print mem\r
+                       print rmem\r
+                       erroc=erroc+1\r
+                       if erroc>5:\r
+                               print "WRITING ERROR ... "\r
+                               exit()\r
+                       continue\r
+               #for v in rmem:\r
+               #       print "%02X " % (v),\r
+               break\r
+print "\nReset AVR"\r
+owcom(s,[0x89],0)\r
+time.sleep(1)\r
+f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_remove","r+b",0)\r
+f.write("a3-55aa55aa55aa")\r
+f.close()      \r
+\r
+print("Enable Device Search")\r
+f=open("/sys/bus/w1/devices/w1_bus_master1/w1_master_search","r+b",0)\r
+f.write("-1")\r
+f.close()      \r
+       \r
+       \r
+\r
+\r
+\r
+\r
+#mem=[0x00,0x2]\r
+#for i in range (64):\r
+#      mem.append(i)\r
+#owcom(s,[0x0F]+mem,0)\r
+#rmem=owcom(s,[0xAA],70)\r
+#print rmem\r
+#owcom(s,[0x55],0)\r
+#time.sleep(0.05)\r
+#owcom(s,[0xB8,0x00,0x02],0)\r
+#time.sleep(0.05)\r
+#rmem=owcom(s,[0xAA],70)\r
+#print rmem\r
+#for v in rmem:\r
+#      print "%02X " % (v)\r
+               \r
+\r
+               \r
index 04f2ade..7dee3e6 100644 (file)
--- a/owlib.py
+++ b/owlib.py
@@ -182,12 +182,13 @@ class owDevice:
        
        def readConfig(self):
                self.config=owcom(self.owid,[0x85],26)
+               print self.config
                if self.config[0]==0xFF:
                        print("No Deviceconfig. Not a Device form tm3d.de. Set Default");
                        self.setdefaultConfig()
                else:
                        if self.config[25]==0xFF:
-                               if not(crc8(self.config[0:24])):
+                               if crc8(self.config[0:24]):
                                        print("CRC Error reading Deviceconfig. Set Default")
                                        self.setdefaultConfig()
                        else:
@@ -285,12 +286,16 @@ class owDS2438(owDevice):
                owcom(self.owid,[0xB4],0)
                time.sleep(0.01)
                sp=self.readScratchpad(0,True)
+               if sp==[]:
+                       return
                temp=ow_fconvert(sp[1],sp[2]);
                VDD=ow_fconvert(sp[3],sp[4]);
                self.setConfigByte(0x00)
                owcom(self.owid,[0xB4],0)
                time.sleep(0.01)
                sp=self.readScratchpad(0,True);
+               if sp==[]: 
+                       return
                I=ow_fconvert(sp[5],sp[6]);
                VAD=ow_fconvert(sp[3],sp[4]);
                self.raw[0]=temp;