Adds client support for the Apimote, which uses the RTS line instead of the DTR line.
[goodfet] / client / GoodFETCCSPI.py
index 5ea56be..0e2d787 100644 (file)
@@ -5,7 +5,7 @@
 #
 # This code is being rewritten and refactored.  You've been warned!
 
-import sys, time, string, cStringIO, struct, glob, serial, os;
+import sys, time, string, cStringIO, struct, glob, os;
 
 from GoodFET import GoodFET;
 
@@ -24,6 +24,7 @@ class GoodFETCCSPI(GoodFET):
         self.poke(0x12, 0x0500); #MDMCTRL1
         self.poke(0x1C, 0x007F); #IOCFG0
         self.poke(0x19, 0x01C4); #SECCTRL0, disabling crypto
+        self.RF_setsync();
         
     def ident(self):
         return self.peek(0x1E); #MANFIDL
@@ -83,12 +84,13 @@ class GoodFETCCSPI(GoodFET):
         """Write a CCSPI Register."""
         data=[reg,(val>>8)&0xFF,val&0xFF];
         self.writecmd(self.CCSPIAPP,0x03,len(data),data);
-        if self.peek(reg,bytes)!=val:
+        if self.peek(reg,bytes)!=val and reg!=0x18:
             print "Warning, failed to set r%02x=0x%04x, got %02x." %(
                 reg,
                 val,
                 self.peek(reg,bytes));
-        return;
+            return False;
+        return True;
     
     def status(self):
         """Read the status byte."""
@@ -120,20 +122,30 @@ class GoodFETCCSPI(GoodFET):
         return 0;
     def RF_setrate(self,rate=0):
         return 0;
+    def RF_getsync(self):
+        return self.peek(0x14);
+    def RF_setsync(self,sync=0xa70F):
+        """Set the SYNC preamble.
+        Use 0xA70F for 0xA7."""
+        self.poke(0x14,sync);
+        return;
+    
     def RF_setfreq(self,frequency):
         """Set the frequency in Hz."""
         mhz=frequency/1000000;
-        fsctrl=self.peek(0x18)&~0x3FF;
+        fsctrl=0x8000; #self.peek(0x18)&(~0x3FF);
         fsctrl=fsctrl+int(mhz-2048)
         self.poke(0x18,fsctrl);
-        self.strobe(0x02);
+        self.strobe(0x02);#SCAL
+        self.strobe(0x03);#SRXON
     def RF_getfreq(self):
         """Get the frequency in Hz."""
         fsctrl=self.peek(0x18);
         mhz=2048+(fsctrl&0x3ff)
         return mhz*1000000;
     def RF_setchan(self,channel):
-        if channel < 11 and channel > 26:
+        """Set the ZigBee/802.15.4 channel number."""
+        if channel < 11 or channel > 26:
             print "Only 802.15.4 channels 11 to 26 are currently supported.";
         else:
             self.RF_setfreq( ( (channel-11)*5 + 2405 ) * 1000000 );
@@ -177,6 +189,20 @@ class GoodFETCCSPI(GoodFET):
         #self.strobe(0x09);
         return;
     
+    def RF_reflexjam(self,duration=0):
+        """Place the device into reflexive jamming mode."""
+        data = [duration&0xff,
+                (duration>>8)&0xff];
+        self.writecmd(self.CCSPIAPP,0xA0,len(data),data);
+        return;
+
+    def RF_reflexjam_autoack(self):
+        """Place the device into reflexive jamming mode
+           and that also sends a forged ACK if needed."""
+        data = "";
+        self.writecmd(self.CCSPIAPP,0xA1,len(data),data);
+        print "Got:", data, "and", self.data
+        return;
 
     def RF_modulated_spectrum(self):
         """Hold a carrier wave on the present frequency."""
@@ -247,7 +273,22 @@ class GoodFETCCSPI(GoodFET):
             mdmctrl0=mdmctrl0|0x800;
         self.poke(0x11,mdmctrl0);
         return;
-        
+    def RF_autocrc(self,autocrc=1):
+        mdmctrl0=self.peek(0x11);
+        if autocrc==0:
+            mdmctrl0=mdmctrl0&(~0x0020);
+        else:
+            mdmctrl0=mdmctrl0|0x0020;
+        self.poke(0x11,mdmctrl0);
+        return;
+    def RF_autoack(self,autoack=1):
+        mdmctrl0=self.peek(0x11);
+        if autoack==0:
+            mdmctrl0=mdmctrl0&(~0x0010);
+        else:
+            mdmctrl0=mdmctrl0|0x0010;
+        self.poke(0x11,mdmctrl0);
+        return;
     packetlen=16;
     def RF_setpacketlen(self,len=16):
         """Set the number of bytes in the expected payload."""
@@ -278,6 +319,7 @@ class GoodFETCCSPI(GoodFET):
         for foo in packet:
             s="%s %02x" % (s,ord(foo));
         print "#%s" % s;
+        
     def printdissect(self,packet):
         try:
             from scapy.all import Dot15d4