Better locating of SmartRF Studio 7.
[goodfet] / client / GoodFET.py
index a602b1e..de3ce7d 100755 (executable)
@@ -11,10 +11,10 @@ import sqlite3;
 fmt = ("B", "<H", None, "<L")
 
 def getClient(name="GoodFET"):
 fmt = ("B", "<H", None, "<L")
 
 def getClient(name="GoodFET"):
-    import GoodFET, GoodFETCC, GoodFETAVR, GoodFETSPI, GoodFETMSP430, GoodFETNRF;
+    import GoodFET, GoodFETCC, GoodFETAVR, GoodFETSPI, GoodFETMSP430, GoodFETNRF, GoodFETCCSPI;
     if(name=="GoodFET" or name=="monitor"): return GoodFET.GoodFET();
     elif name=="cc" or name=="cc51": return GoodFETCC.GoodFETCC();
     if(name=="GoodFET" or name=="monitor"): return GoodFET.GoodFET();
     elif name=="cc" or name=="cc51": return GoodFETCC.GoodFETCC();
-    elif name=="cc2420" or name=="ccspi": return GoodFETCC.GoodFETCC();
+    elif name=="cc2420" or name=="ccspi": return GoodFETCCSPI.GoodFETCCSPI();
     elif name=="avr": return GoodFETAVR.GoodFETAVR();
     elif name=="spi": return GoodFETSPI.GoodFETSPI();
     elif name=="msp430": return GoodFETMSP430.GoodFETMSP430();
     elif name=="avr": return GoodFETAVR.GoodFETAVR();
     elif name=="spi": return GoodFETSPI.GoodFETSPI();
     elif name=="msp430": return GoodFETMSP430.GoodFETMSP430();
@@ -76,6 +76,9 @@ class GoodFET:
         """Open the serial port"""
         # Make timeout None to wait forever, 0 for non-blocking mode.
         
         """Open the serial port"""
         # Make timeout None to wait forever, 0 for non-blocking mode.
         
+        if os.name=='nt' and sys.version.find('64 bit')!=-1:
+            print "WARNING: PySerial requires a 32-bit Python build in Windows.";
+        
         if port is None and os.environ.get("GOODFET")!=None:
             glob_list = glob.glob(os.environ.get("GOODFET"));
             if len(glob_list) > 0:
         if port is None and os.environ.get("GOODFET")!=None:
             glob_list = glob.glob(os.environ.get("GOODFET"));
             if len(glob_list) > 0:
@@ -94,7 +97,7 @@ class GoodFET:
             glob_list = glob.glob("/dev/ttyU0");
             if len(glob_list) > 0:
                 port = glob_list[0];
             glob_list = glob.glob("/dev/ttyU0");
             if len(glob_list) > 0:
                 port = glob_list[0];
-        if os.name=='nt':
+        if port is None and os.name=='nt':
             from scanwin32 import winScan;
             scan=winScan();
             for order,comport,desc,hwid in sorted(scan.comports()):
             from scanwin32 import winScan;
             scan=winScan();
             for order,comport,desc,hwid in sorted(scan.comports()):
@@ -106,10 +109,13 @@ class GoodFET:
                     #Do nothing.
                     a=1;
         
                     #Do nothing.
                     a=1;
         
+        baud=115200;
+        if(os.environ.get("platform")=='arduino'):
+            baud=19200; #Slower, for now.
         self.serialport = serial.Serial(
             port,
             #9600,
         self.serialport = serial.Serial(
             port,
             #9600,
-            115200,
+            baud,
             parity = serial.PARITY_NONE,
             timeout=timeout
             )
             parity = serial.PARITY_NONE,
             timeout=timeout
             )
@@ -118,6 +124,7 @@ class GoodFET:
         attempts=0;
         connected=0;
         while connected==0:
         attempts=0;
         connected=0;
         while connected==0:
+            #print "Got %s" % self.data;
             while self.verb!=0x7F or self.data!="http://goodfet.sf.net/":
                 if attemptlimit is not None and attempts >= attemptlimit:
                     return
             while self.verb!=0x7F or self.data!="http://goodfet.sf.net/":
                 if attemptlimit is not None and attempts >= attemptlimit:
                     return
@@ -135,6 +142,8 @@ class GoodFET:
                 if(os.environ.get("platform")=='telosb'):
                     #print "TelosB Reset";
                     self.telosBReset();
                 if(os.environ.get("platform")=='telosb'):
                     #print "TelosB Reset";
                     self.telosBReset();
+                
+                    
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
@@ -147,15 +156,18 @@ class GoodFET:
                 attempts=attempts+1;
                 self.readcmd(); #Read the first command.
             #Here we have a connection, but maybe not a good one.
                 attempts=attempts+1;
                 self.readcmd(); #Read the first command.
             #Here we have a connection, but maybe not a good one.
+            #print "We have a connection."
             connected=1;
             olds=self.infostring();
             clocking=self.monitorclocking();
             connected=1;
             olds=self.infostring();
             clocking=self.monitorclocking();
+            #if(os.environ.get("platform")!='arduino'):
             for foo in range(1,30):
                 if not self.monitorecho():
             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 "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);
         if self.verbose: print "Connected after %02i attempts." % attempts;
         self.mon_connected();
         self.serialport.setTimeout(12);
@@ -493,7 +505,7 @@ class GoodFET:
         data="The quick brown fox jumped over the lazy dog.";
         self.writecmd(self.MONITORAPP,0x81,len(data),data);
         if self.data!=data:
         data="The quick brown fox jumped over the lazy dog.";
         self.writecmd(self.MONITORAPP,0x81,len(data),data);
         if self.data!=data:
-            if self.verbose: print "Comm error recognized by monitorecho().";
+            print "Comm error recognized by monitorecho(), got:\n%s" % self.data;
             return 0;
         return 1;
 
             return 0;
         return 1;
 
@@ -529,14 +541,20 @@ class GoodFET:
         self.MONpoke16(0x56, clock);
     def monitorgetclock(self):
         """Get the clocking value."""
         self.MONpoke16(0x56, clock);
     def monitorgetclock(self):
         """Get the clocking value."""
+        if(os.environ.get("platform")=='arduino'):
+            return 0xDEAD;
+        #Check for MSP430 before peeking this.
         return self.MONpeek16(0x56);
     # The following functions ought to be implemented in
     # every client.
     
     def infostring(self):
         return self.MONpeek16(0x56);
     # The following functions ought to be implemented in
     # every client.
     
     def infostring(self):
-        a=self.MONpeek8(0xff0);
-        b=self.MONpeek8(0xff1);
-        return "%02x%02x" % (a,b);
+        if(os.environ.get("platform")=='arduino'):
+            return "Arduino";
+        else:
+            a=self.MONpeek8(0xff0);
+            b=self.MONpeek8(0xff1);
+            return "%02x%02x" % (a,b);
     def lock(self):
         print "Locking Unsupported.";
     def erase(self):
     def lock(self):
         print "Locking Unsupported.";
     def erase(self):