Better locating of SmartRF Studio 7.
[goodfet] / client / GoodFET.py
index 98c2494..de3ce7d 100755 (executable)
@@ -11,10 +11,10 @@ import sqlite3;
 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();
-    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();
@@ -72,10 +72,13 @@ class GoodFET:
         return self.symbols.get(name);
     def timeout(self):
         print "timeout\n";
-    def serInit(self, port=None, timeout=2):
+    def serInit(self, port=None, timeout=2, attemptlimit=None):
         """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:
@@ -90,7 +93,11 @@ class GoodFET:
             glob_list = glob.glob("/dev/ttyUSB*");
             if len(glob_list) > 0:
                 port = glob_list[0];
-        if os.name=='nt':
+        if port is None:
+            glob_list = glob.glob("/dev/ttyU0");
+            if len(glob_list) > 0:
+                port = glob_list[0];
+        if port is None and os.name=='nt':
             from scanwin32 import winScan;
             scan=winScan();
             for order,comport,desc,hwid in sorted(scan.comports()):
@@ -102,10 +109,13 @@ class GoodFET:
                     #Do nothing.
                     a=1;
         
+        baud=115200;
+        if(os.environ.get("platform")=='arduino'):
+            baud=19200; #Slower, for now.
         self.serialport = serial.Serial(
             port,
             #9600,
-            115200,
+            baud,
             parity = serial.PARITY_NONE,
             timeout=timeout
             )
@@ -114,8 +124,12 @@ class GoodFET:
         attempts=0;
         connected=0;
         while connected==0:
+            #print "Got %s" % self.data;
             while self.verb!=0x7F or self.data!="http://goodfet.sf.net/":
-                #print "Resyncing.";
+                if attemptlimit is not None and attempts >= attemptlimit:
+                    return
+                elif attempts>2:
+                    print "Resyncing.";
                 self.serialport.flushInput()
                 self.serialport.flushOutput()
                 #Explicitly set RTS and DTR to halt board.
@@ -126,7 +140,10 @@ class GoodFET:
                 
                 #TelosB reset, prefer software to I2C SPST Switch.
                 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));
@@ -139,17 +156,23 @@ class GoodFET:
                 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();
+            #if(os.environ.get("platform")!='arduino'):
             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);
+    def serClose(self):
+        self.serialport.close();
     def telosSetSCL(self, level):
         self.serialport.setRTS(not level)
     def telosSetSDA(self, level):
@@ -357,21 +380,22 @@ class GoodFET:
         """Execute supplied code."""
         self.writecmd(0,0x31,2,#len(code),
                       code);
-    def peekbyte(self,address):
+    def MONpeek8(self,address):
         """Read a byte of memory from the monitor."""
         self.data=[address&0xff,address>>8];
         self.writecmd(0,0x02,2,self.data);
         #self.readcmd();
         return ord(self.data[0]);
-    def peekword(self,address):
+    def MONpeek16(self,address):
         """Read a word of memory from the monitor."""
-        return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
+        return self.MONpeek8(address)+(self.MONpeek8(address+1)<<8);
     def peek(self,address):
         """Read a word of memory from the monitor."""
-        return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
+        return self.MONpeek8(address)+(self.MONpeek8(address+1)<<8);
     def eeprompeek(self,address):
         """Read a word of memory from the monitor."""
-        return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
+        print "EEPROM peeking not supported for the monitor.";
+        #return self.MONpeek8(address)+(self.MONpeek8(address+1)<<8);
     def peekbysym(self,name):
         """Read a value by its symbol name."""
         #TODO include memory in symbol.
@@ -394,16 +418,22 @@ class GoodFET:
         return value;
     def setsecret(self,value):
         """Set a secret word for later retreival.  Used by glitcher."""
-        self.eeprompoke(0,value);
-        self.eeprompoke(1,value);
+        #self.eeprompoke(0,value);
+        #self.eeprompoke(1,value);
+        print "Secret setting is not yet suppored for this target.";
+        print "Aborting.";
+        
     def getsecret(self):
         """Get a secret word.  Used by glitcher."""
-        self.eeprompeek(0);
+        #self.eeprompeek(0);
+        print "Secret getting is not yet suppored for this target.";
+        print "Aborting.";
+        sys.exit();
     
     def dumpmem(self,begin,end):
         i=begin;
         while i<end:
-            print "%04x %04x" % (i, self.peekword(i));
+            print "%04x %04x" % (i, self.MONpeek16(i));
             i+=2;
     def monitor_ram_pattern(self):
         """Overwrite all of RAM with 0xBEEF."""
@@ -459,12 +489,12 @@ class GoodFET:
         print "Performing monitor self-test.";
         self.monitorclocking();
         for f in range(0,3000):
-            a=self.peekword(0x0c00);
-            b=self.peekword(0x0c02);
+            a=self.MONpeek16(0x0c00);
+            b=self.MONpeek16(0x0c02);
             if a!=0x0c04 and a!=0x0c06:
                 print "ERROR Fetched %04x, %04x" % (a,b);
             self.pokebyte(0x0021,0); #Drop LED
-            if self.peekbyte(0x0021)!=0:
+            if self.MONpeek8(0x0021)!=0:
                 print "ERROR, P1OUT not cleared.";
             self.pokebyte(0x0021,1); #Light LED
             if not self.monitorecho():
@@ -475,26 +505,56 @@ class GoodFET:
         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;
+
+    def monitor_info(self):
+        print "GoodFET with %s MCU" % self.infostring();
+        print "Clocked at %s" % self.monitorclocking();
+        return 1;
+
+    def monitor_list_apps(self, full=False): 
+        self.monitor_info()
+        old_value = self.besilent
+        self.besilent = True    # turn off automatic call to readcmd
+        self.writecmd(self.MONITORAPP, 0x82, 1, [int(full)]);
+        self.besilent = old_value
+        
+        # read the build date string 
+        self.readcmd()
+        print "Build Date: %s" % self.data
+        print "Firmware apps:"
+        while True:
+            self.readcmd()
+            if self.count == 0:
+                break
+            print self.data
+        return 1;
+
     def monitorclocking(self):
         """Return the 16-bit clocking value."""
         return "0x%04x" % self.monitorgetclock();
     
     def monitorsetclock(self,clock):
         """Set the clocking value."""
-        self.poke16(0x56, clock);
+        self.MONpoke16(0x56, clock);
     def monitorgetclock(self):
         """Get the clocking value."""
-        return self.peek16(0x56);
+        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):
-        a=self.peekbyte(0xff0);
-        b=self.peekbyte(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):
@@ -534,10 +594,7 @@ class GoodFET:
                 (self.peek8(address+1,memory)<<8));
     def peek8(self,address, memory="vn"):
         """Peek a byte of memory."""
-        return self.peekbyte(address); #monitor
-    def peekword(self,address, memory="vn"):
-        """Peek a natively sized word of memory."""
-        return self.peek16(address); #monitor
+        return self.MONpeek8(address); #monitor
     def peekblock(self,address,length,memory="vn"):
         """Return a block of data."""
         data=range(0,length);