Lots of new CC2420 stabilization.
[goodfet] / client / GoodFET.py
index ab6bb09..05c2045 100755 (executable)
@@ -52,18 +52,23 @@ class GoodFETbtser:
     """py-bluez class for emulating py-serial."""
     def __init__(self,btaddr):
         import bluetooth;
-        while btaddr==None or btaddr=="none" or btaddr=="bluetooth":
+        if btaddr==None or btaddr=="none" or btaddr=="bluetooth":
             print "performing inquiry..."
             nearby_devices = bluetooth.discover_devices(lookup_names = True)
             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;
 
-        # BlueFET doesn't run the Service Discovery Protocol.
-        # Instead we manually use the portnumber.
+        # Manually use the portnumber.
         port=1;
         
         print "Connecting to %s on port %i." % (btaddr, port);
@@ -72,18 +77,27 @@ class GoodFETbtser:
         sock.connect((btaddr,port));
         sock.settimeout(10);  #IMPORTANT Must be patient.
         
-        #Once connected, we need to get synced.
-        #This might require a firmware patch.
-        str=None;
-        while str!="http://goodfet.sf.net/":
-            str=self.read(64);
-            print str;
+        ##This is what we'd do for a normal reset.
+        #str="";
+        #while not str.endswith("goodfet.sf.net/"):
+        #    str=self.read(64);
+        #    print str;
+        
+        # Instead, just return and hope for the best.
+        return;
+        
     def write(self,msg):
         """Send traffic."""
-        return self.sock.send(msg);
-    def read(self,len):
+        import time;
+        self.sock.send(msg);
+        #time.sleep(0.1);
+        return;
+    def read(self,length):
         """Read traffic."""
-        return self.sock.recv(len);
+        data="";
+        while len(data)<length:
+            data=data+self.sock.recv(length-len(data));
+        return data;
 class GoodFET:
     """GoodFET Client Library"""
 
@@ -109,14 +123,17 @@ class GoodFET:
         print "timeout\n";
     def serInit(self, port=None, timeout=2, attemptlimit=None):
         """Open a serial port of some kind."""
+        import re;
         
-        if port=="bluetooth" or os.environ.get("GOODFET")=="bluetooth":
-            self.btInit(port,timeout,attemptlimit);
+        if port==None:
+            port=os.environ.get("GOODFET");
+        if port=="bluetooth" or (port is not None and re.match("..:..:..:..:..:..",port)):
+            self.btInit(port,2,attemptlimit);
         else:
             self.pyserInit(port,timeout,attemptlimit);
     def btInit(self, port, timeout, attemptlimit):
         """Open a bluetooth port.""";
-        self.verbose=True;
+        #self.verbose=True;  #For debugging BT.
         self.serialport=GoodFETbtser(port);
         
     def pyserInit(self, port, timeout, attemptlimit):
@@ -158,7 +175,7 @@ class GoodFET:
                     a=1;
         
         baud=115200;
-        if(os.environ.get("platform")=='arduino'):
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
             baud=19200; #Slower, for now.
         self.serialport = serial.Serial(
             port,
@@ -178,14 +195,23 @@ class GoodFET:
                 if attemptlimit is not None and attempts >= attemptlimit:
                     return
                 elif attempts>2:
-                    print "Resyncing.";
-                self.serialport.flushInput()
-                self.serialport.flushOutput()
+                    print "Resyncing.  See the GoodFET FAQ about missing info flash.";
+                #self.serialport.flushInput()
+                #self.serialport.flushOutput()
                 
                 #TelosB reset, prefer software to I2C SPST Switch.
-                if(os.environ.get("platform")=='telosb'):
+                if (os.environ.get("platform")=='telosb' or  os.environ.get("board")=='telosb'):
                     #print "TelosB Reset";
                     self.telosBReset();
+                elif (os.environ.get("board")=='zolertiaz1' or  os.environ.get("board")=='z1'):
+                    self.bslResetZ1(invokeBSL=0);
+                elif (os.environ.get("board")=='apimote1'):
+                    #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);
@@ -205,12 +231,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;
             olds=self.infostring();
             clocking=self.monitorclocking();
-            #if(os.environ.get("platform")!='arduino'):
             for foo in range(1,30):
                 if not self.monitorecho():
                     if self.verbose:
@@ -262,6 +291,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"
@@ -347,8 +452,8 @@ class GoodFET:
                     +(ord(self.serialport.read(1))<<8)
                     );
 
-                #if self.verbose:
-                #print "Rx: ( 0x%02x, 0x%02x, 0x%04x )" % ( self.app, self.verb, self.count )
+                if self.verbose:
+                    print "Rx: ( 0x%02x, 0x%02x, 0x%04x )" % ( self.app, self.verb, self.count )
             
                 #Debugging string; print, but wait.
                 if self.app==0xFF:
@@ -570,6 +675,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
@@ -597,7 +711,7 @@ class GoodFET:
         self.MONpoke16(0x56, clock);
     def monitorgetclock(self):
         """Get the clocking value."""
-        if(os.environ.get("platform")=='arduino'):
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
             return 0xDEAD;
         #Check for MSP430 before peeking this.
         return self.MONpeek16(0x56);
@@ -605,7 +719,7 @@ class GoodFET:
     # every client.
     
     def infostring(self):
-        if(os.environ.get("platform")=='arduino'):
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
             return "Arduino";
         else:
             a=self.MONpeek8(0xff0);