X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=client%2FGoodFET.py;h=178ebd61d7addb450c323070b4b62ed10945f958;hp=9bf627ca2d14d68670658b8c3f9695f021eadf77;hb=9efdb05e8177ee6925bda8ca39f0a113505bdf57;hpb=9ba7e4f5ec74d83c1c167655dbd0de02db15e9f9 diff --git a/client/GoodFET.py b/client/GoodFET.py index 9bf627c..178ebd6 100755 --- a/client/GoodFET.py +++ b/client/GoodFET.py @@ -58,8 +58,12 @@ class GoodFETbtser: print "found %d devices" % len(nearby_devices) for addr, name in nearby_devices: print " %s - '%s'" % (addr, name) + #TODO switch to wildcards. if name=='FireFly-A6BD': btaddr=addr; + if name=='RN42-A94A': + btaddr=addr; + print "Please set $GOODFET to the address of your device."; sys.exit(); print "Identified GoodFET at %s" % btaddr; @@ -124,7 +128,7 @@ class GoodFET: if port==None: port=os.environ.get("GOODFET"); if port=="bluetooth" or (port is not None and re.match("..:..:..:..:..:..",port)): - self.btInit(port,timeout,attemptlimit); + self.btInit(port,2,attemptlimit); else: self.pyserInit(port,timeout,attemptlimit); def btInit(self, port, timeout, attemptlimit): @@ -136,6 +140,7 @@ class GoodFET: """Open the serial port""" # Make timeout None to wait forever, 0 for non-blocking mode. import serial; + fixserial=False; if os.name=='nt' and sys.version.find('64 bit')!=-1: print "WARNING: PySerial requires a 32-bit Python build in Windows."; @@ -190,15 +195,33 @@ class GoodFET: #print "'%s'!=\n'%s'" % (self.data,"http://goodfet.sf.net/"); if attemptlimit is not None and attempts >= attemptlimit: return - elif attempts>2: - print "Resyncing."; - self.serialport.flushInput() - self.serialport.flushOutput() + elif attempts==2 and os.environ.get("board")!='telosb': + print "See the GoodFET FAQ about missing info flash."; + self.serialport.setTimeout(0.2); + elif attempts == 100: + print "Tried 100 times to connect and failed." + sys.stdout.write("Continuing to try forever.") # No newline + sys.stdout.flush() + self.verbose=True # Something isn't going right, give the user more info + elif attempts > 100 and attempts % 10 == 0: + sys.stdout.write('.') + sys.stdout.flush() + #self.serialport.flushInput() + #self.serialport.flushOutput() #TelosB reset, prefer software to I2C SPST Switch. - if(os.environ.get("platform")=='telosb' or os.environ.get("board")=='telosb'): + if (os.environ.get("board")=='telosb'): #print "TelosB Reset"; self.telosBReset(); + elif (os.environ.get("board")=='z1'): + self.bslResetZ1(invokeBSL=0); + elif (os.environ.get("board")=='apimote1') or (os.environ.get("board")=='apimote'): + #Explicitly set RTS and DTR to halt board. + self.serialport.setRTS(1); + self.serialport.setDTR(1); + #RTS pin, not DTR is used for reset. + self.serialport.setRTS(0); + #print "Resetting Apimote not yet tested."; else: #Explicitly set RTS and DTR to halt board. self.serialport.setRTS(1); @@ -218,9 +241,15 @@ class GoodFET: attempts=attempts+1; self.readcmd(); #Read the first command. #print "Got %02x,%02x:'%s'" % (self.app,self.verb,self.data); + if self.verb!=0x7f: + #Retry again. This usually times out, but helps connect. + self.readcmd(); + #print "Retry got %02x,%02x:'%s'" % (self.app,self.verb,self.data); #Here we have a connection, but maybe not a good one. #print "We have a connection." connected=1; + if attempts >= 100: + print "" # Add a newline olds=self.infostring(); clocking=self.monitorclocking(); for foo in range(1,30): @@ -228,8 +257,8 @@ class GoodFET: if self.verbose: print "Comm error on %i try, resyncing out of %s." % (foo, clocking); - connected=0; - break; + connected=0; + break; if self.verbose: print "Connected after %02i attempts." % attempts; self.mon_connected(); self.serialport.setTimeout(12); @@ -274,6 +303,82 @@ class GoodFET: self.telosI2CWriteByte( 0x90 | (addr << 1) ) self.telosI2CWriteByte( cmdbyte ) self.telosI2CStop() + def bslResetZ1(self, invokeBSL=0): + ''' + Applies BSL entry sequence on RST/NMI and TEST/VPP pins + Parameters: + invokeBSL = 1: complete sequence + invokeBSL = 0: only RST/NMI pin accessed + + By now only BSL mode is accessed + ''' + + #if DEBUG > 1: sys.stderr.write("* bslReset(invokeBSL=%s)\n" % invokeBSL) + if invokeBSL: + #sys.stderr.write("in Z1 bsl reset...\n") + time.sleep(0.1) + self.writepicROM(0xFF, 0xFF) + time.sleep(0.1) + #sys.stderr.write("z1 bsl reset done...\n") + else: + #sys.stderr.write("in Z1 reset...\n") + time.sleep(0.1) + self.writepicROM(0xFF, 0xFE) + time.sleep(0.1) + #sys.stderr.write("z1 reset done...\n") + def writepicROM(self, address, data): + ''' Writes data to @address''' + for i in range(7,-1,-1): + self.picROMclock((address >> i) & 0x01) + self.picROMclock(0) + recbuf = 0 + for i in range(7,-1,-1): + s = ((data >> i) & 0x01) + #print s + if i < 1: + r = not self.picROMclock(s, True) + else: + r = not self.picROMclock(s) + recbuf = (recbuf << 1) + r + + self.picROMclock(0, True) + #k = 1 + #while not self.serial.getCTS(): + # pass + #time.sleep(0.1) + return recbuf + def readpicROM(self, address): + ''' reads a byte from @address''' + for i in range(7,-1,-1): + self.picROMclock((address >> i) & 0x01) + self.picROMclock(1) + recbuf = 0 + r = 0 + for i in range(7,-1,-1): + r = self.picROMclock(0) + recbuf = (recbuf << 1) + r + self.picROMclock(r) + #time.sleep(0.1) + return recbuf + + #This seems more reliable when slowed. + def picROMclock(self, masterout, slow = True): + #print "setting masterout to "+str(masterout) + self.serialport.setRTS(masterout) + self.serialport.setDTR(1) + #time.sleep(0.02) + self.serialport.setDTR(0) + if slow: + time.sleep(0.02) + return self.serialport.getCTS() + + def picROMfastclock(self, masterout): + #print "setting masterout to "+str(masterout) + self.serialport.setRTS(masterout) + self.serialport.setDTR(1) + self.serialport.setDTR(0) + time.sleep(0.02) + return self.serialport.getCTS() def telosBReset(self,invokeBSL=0): # "BSL entry sequence at dedicated JTAG pins" @@ -480,6 +585,9 @@ class GoodFET: self.writecmd(0,0x03,3,self.data); return ord(self.data[0]); def poke16(self,address,value): + """Set a word of memory by the monitor.""" + self.MONpoke16(address,value); + def MONpoke16(self,address,value): """Set a word of memory by the monitor.""" self.pokebyte(address,value&0xFF); self.pokebyte(address,(value>>8)&0xFF); @@ -573,7 +681,8 @@ class GoodFET: data="The quick brown fox jumped over the lazy dog."; self.writecmd(self.MONITORAPP,0x81,len(data),data); if self.data!=data: - print "Comm error recognized by monitorecho(), got:\n%s" % self.data; + if self.verbose: + print "Comm error recognized by monitorecho(), got:\n%s" % self.data; return 0; return 1; @@ -582,6 +691,15 @@ class GoodFET: print "Clocked at %s" % self.monitorclocking(); return 1; + def testleds(self): + print "Flashing LEDs" + self.writecmd(self.MONITORAPP,0xD0,0,"") + try: + print "Flashed %d LED." % ord(self.data) + except: + print "Unable to process response:", self.data + return 1 + def monitor_list_apps(self, full=False): self.monitor_info() old_value = self.besilent