+ attempts=0;
+ connected=0;
+ while connected==0:
+ while self.verb!=0x7F or self.data!="http://goodfet.sf.net/":
+ #while self.data!="http://goodfet.sf.net/":
+ #print "'%s'!=\n'%s'" % (self.data,"http://goodfet.sf.net/");
+ if attemptlimit is not None and attempts >= attemptlimit:
+ return
+ elif attempts==2 and os.environ.get("board")!='telosb':
+ print "See the GoodFET FAQ about missing info flash.";
+ self.serialport.setTimeout(0.2);
+ #self.serialport.flushInput()
+ #self.serialport.flushOutput()
+
+ #TelosB reset, prefer software to I2C SPST Switch.
+ 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);
+ self.serialport.setDTR(1);
+ #Drop DTR, which is !RST, low to begin the app.
+ self.serialport.setDTR(0);
+
+ #self.serialport.write(chr(0x80));
+ #self.serialport.write(chr(0x80));
+ #self.serialport.write(chr(0x80));
+ #self.serialport.write(chr(0x80));
+
+
+ #self.serialport.flushInput()
+ #self.serialport.flushOutput()
+ #time.sleep(60);
+ 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;
+ olds=self.infostring();
+ clocking=self.monitorclocking();
+ for foo in range(1,30):
+ if not self.monitorecho():
+ if self.verbose:
+ print "Comm error on %i try, resyncing out of %s." % (foo,
+ clocking);
+ connected=0;
+ break;
+ if self.verbose: print "Connected after %02i attempts." % attempts;
+ self.mon_connected();
+ self.serialport.setTimeout(12);
+ def serClose(self):
+ self.serialport.close();
+ def telosSetSCL(self, level):
+ self.serialport.setRTS(not level)
+ def telosSetSDA(self, level):
+ self.serialport.setDTR(not level)
+
+ def telosI2CStart(self):
+ self.telosSetSDA(1)
+ self.telosSetSCL(1)
+ self.telosSetSDA(0)
+
+ def telosI2CStop(self):
+ self.telosSetSDA(0)
+ self.telosSetSCL(1)
+ self.telosSetSDA(1)
+
+ def telosI2CWriteBit(self, bit):
+ self.telosSetSCL(0)
+ self.telosSetSDA(bit)
+ time.sleep(2e-6)
+ self.telosSetSCL(1)
+ time.sleep(1e-6)
+ self.telosSetSCL(0)
+
+ def telosI2CWriteByte(self, byte):
+ self.telosI2CWriteBit( byte & 0x80 );
+ self.telosI2CWriteBit( byte & 0x40 );
+ self.telosI2CWriteBit( byte & 0x20 );
+ self.telosI2CWriteBit( byte & 0x10 );
+ self.telosI2CWriteBit( byte & 0x08 );
+ self.telosI2CWriteBit( byte & 0x04 );
+ self.telosI2CWriteBit( byte & 0x02 );
+ self.telosI2CWriteBit( byte & 0x01 );
+ self.telosI2CWriteBit( 0 ); # "acknowledge"
+
+ def telosI2CWriteCmd(self, addr, cmdbyte):
+ self.telosI2CStart()
+ 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
+ '''