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