Moving glitcher away from AVR-specific calls.
[goodfet] / client / GoodFET.py
index 579eff0..154bd38 100755 (executable)
@@ -13,7 +13,8 @@ fmt = ("B", "<H", None, "<L")
 def getClient(name="GoodFET"):
     import GoodFET, GoodFETCC, GoodFETAVR, GoodFETSPI, GoodFETMSP430, GoodFETNRF;
     if(name=="GoodFET" or name=="monitor"): return GoodFET.GoodFET();
-    elif name=="cc" or name=="chipcon": return GoodFETCC.GoodFETCC();
+    elif name=="cc" or name=="cc51": return GoodFETCC.GoodFETCC();
+    elif name=="cc2420" or name=="ccspi": return GoodFETCC.GoodFETCC();
     elif name=="avr": return GoodFETAVR.GoodFETAVR();
     elif name=="spi": return GoodFETSPI.GoodFETSPI();
     elif name=="msp430": return GoodFETMSP430.GoodFETMSP430();
@@ -93,10 +94,13 @@ class GoodFET:
             from scanwin32 import winScan;
             scan=winScan();
             for order,comport,desc,hwid in sorted(scan.comports()):
-                if hwid.index('FTDI')==0:
-                    port=comport;
-                    #print "Using FTDI port %s" % port
-        
+                try:
+                    if hwid.index('FTDI')==0:
+                        port=comport;
+                        #print "Using FTDI port %s" % port
+                except:
+                    #Do nothing.
+                    a=1;
         
         self.serialport = serial.Serial(
             port,
@@ -119,6 +123,16 @@ class GoodFET:
                 self.serialport.setDTR(1);
                 #Drop DTR, which is !RST, low to begin the app.
                 self.serialport.setDTR(0);
+                
+                #TelosB reset, prefer software to I2C SPST Switch.
+                if(os.environ.get("platform")=='telosb'):
+                    self.telosBReset();
+                #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);
@@ -136,7 +150,75 @@ class GoodFET:
                     break;
         if self.verbose: print "Connected after %02i attempts." % attempts;
         self.mon_connected();
-        
+    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 telosBReset(self,invokeBSL=0):
+        # "BSL entry sequence at dedicated JTAG pins"
+        # rst !s0: 0 0 0 0 1 1
+        # tck !s1: 1 0 1 0 0 1
+        #   s0|s1: 1 3 1 3 2 0
+
+        # "BSL entry sequence at shared JTAG pins"
+        # rst !s0: 0 0 0 0 1 1
+        # tck !s1: 0 1 0 1 1 0
+        #   s0|s1: 3 1 3 1 0 2
+
+        if invokeBSL:
+            self.telosI2CWriteCmd(0,1)
+            self.telosI2CWriteCmd(0,3)
+            self.telosI2CWriteCmd(0,1)
+            self.telosI2CWriteCmd(0,3)
+            self.telosI2CWriteCmd(0,2)
+            self.telosI2CWriteCmd(0,0)
+        else:
+            self.telosI2CWriteCmd(0,3)
+            self.telosI2CWriteCmd(0,2)
+
+        # This line was not defined inside the else: block, not sure where it
+        # should be however
+        self.telosI2CWriteCmd(0,0)
+        time.sleep(0.250)       #give MSP430's oscillator time to stabilize
+        self.serialport.flushInput()  #clear buffers
+
+
     def getbuffer(self,size=0x1c00):
         writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
         print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
@@ -195,6 +277,10 @@ class GoodFET:
                         print "# DEBUG %s" % self.serialport.read(self.count)
                            elif self.verb==0xFE:
                         print "# DEBUG 0x%x" % struct.unpack(fmt[self.count-1], self.serialport.read(self.count))[0]
+                    elif self.verb==0xFD:
+                        #Do nothing, just wait so there's no timeout.
+                        print "# NOP.";
+                        
                     sys.stdout.flush();
                 else:
                     self.data=self.serialport.read(self.count);
@@ -280,11 +366,23 @@ class GoodFET:
     def peek(self,address):
         """Read a word of memory from the monitor."""
         return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
+    def eeprompeek(self,address):
+        """Read a word of memory from the monitor."""
+        return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
+    
     def pokebyte(self,address,value):
         """Set a byte of memory by the monitor."""
         self.data=[address&0xff,address>>8,value];
         self.writecmd(0,0x03,3,self.data);
         return ord(self.data[0]);
+    def setsecret(self,value):
+        """Set a secret word for later retreival.  Used by glitcher."""
+        self.eeprompoke(0,value);
+        self.eeprompoke(1,value);
+    def getsecret(self):
+        """Get a secret word.  Used by glitcher."""
+        self.eeprompeek(0);
+    
     def dumpmem(self,begin,end):
         i=begin;
         while i<end: