+ while 1:#self.serialport.inWaiting(): # Loop while input data is available
+ try:
+ #print "Reading...";
+ self.app=ord(self.serialport.read(1));
+ #print "APP=%2x" % self.app;
+ self.verb=ord(self.serialport.read(1));
+ #print "VERB=%02x" % self.verb;
+ self.count=(
+ ord(self.serialport.read(1))
+ +(ord(self.serialport.read(1))<<8)
+ );
+
+ 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:
+ if self.verb==0xFF:
+ 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]
+ sys.stdout.flush();
+ else:
+ self.data=self.serialport.read(self.count);
+ return self.data;
+ except TypeError:
+ if self.connected:
+ print "Error: waiting for serial read timed out (most likely).";
+ print "This shouldn't happen after syncing. Exiting for safety.";
+ sys.exit(-1)
+ return self.data;
+ #Glitching stuff.
+ def glitchApp(self,app):
+ """Glitch into a device by its application."""
+ self.data=[app&0xff];
+ self.writecmd(self.GLITCHAPP,0x80,1,self.data);
+ #return ord(self.data[0]);
+ def glitchVerb(self,app,verb,data):
+ """Glitch during a transaction."""
+ if data==None: data=[];
+ self.data=[app&0xff, verb&0xFF]+data;
+ self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
+ #return ord(self.data[0]);
+ def glitchstart(self):
+ """Glitch into the AVR application."""
+ self.glitchVerb(self.APP,0x20,None);
+ def glitchstarttime(self):
+ """Measure the timer of the START verb."""
+ return self.glitchTime(self.APP,0x20,None);
+ def glitchTime(self,app,verb,data):
+ """Time the execution of a verb."""
+ if data==None: data=[];
+ self.data=[app&0xff, verb&0xFF]+data;
+ self.writecmd(self.GLITCHAPP,0x82,len(self.data),self.data);
+ return ord(self.data[0])+(ord(self.data[1])<<8);
+ def glitchVoltages(self,low=0x0880, high=0x0fff):
+ """Set glitching voltages. (0x0fff is max.)"""
+ self.data=[low&0xff, (low>>8)&0xff,
+ high&0xff, (high>>8)&0xff];
+ self.writecmd(self.GLITCHAPP,0x90,4,self.data);
+ #return ord(self.data[0]);
+ def glitchRate(self,count=0x0800):
+ """Set glitching count period."""
+ self.data=[count&0xff, (count>>8)&0xff];
+ self.writecmd(self.GLITCHAPP,0x91,2,
+ self.data);
+ #return ord(self.data[0]);
+
+