2 # GoodFET Chipcon RF Radio Client
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
8 import sys, time, string, cStringIO, struct, glob, serial, os;
10 from GoodFET import GoodFET;
12 class GoodFETCCSPI(GoodFET):
15 """Move the FET into the CCSPI application."""
16 self.writecmd(self.CCSPIAPP,0x10,0,self.data); #CCSPI/SETUP
18 def trans8(self,byte):
19 """Read and write 8 bits by CCSPI."""
20 data=self.CCSPItrans([byte]);
24 """Exchange data by CCSPI."""
26 self.writecmd(self.CCSPIAPP,0x00,len(data),data);
28 def strobe(self,reg=0x00):
29 """Strobes a strobe register, returning the status."""
32 return ord(self.data[0]);
33 def peek(self,reg,bytes=2):
34 """Read a CCSPI Register. For long regs, result is flipped."""
37 #Automatically calibrate the len.
40 self.writecmd(self.CCSPIAPP,0x02,len(data),data);
42 #print "Status: %02x" % ord(self.data[0]);
43 for i in range(0,bytes):
44 toret=toret|(ord(self.data[i+1])<<(8*i));
46 def poke(self,reg,val,bytes=-1):
47 """Write a CCSPI Register."""
50 #Automatically calibrate the len.
53 if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
55 for i in range(0,bytes):
56 data=data+[(val>>(8*i))&0xFF];
57 self.writecmd(self.CCSPIAPP,0x03,len(data),data);
58 if self.peek(reg,bytes)!=val and reg!=0x07:
59 print "Warning, failed to set r%02x=%02x, got %02x." %(reg,
61 self.peek(reg,bytes));
65 """Read the status byte."""
66 status=self.strobe(0x00);
67 print "Status=%02x" % status;
69 #Radio stuff begins here.
70 def RF_setenc(self,code="GFSK"):
71 """Set the encoding type."""
73 return "%s not supported by the CCSPI24L01. Try GFSK."
76 """Get the encoding type."""
79 rate=self.peek(0x06)&0x28;
81 rate=250*10**3; #256kbps
87 def RF_setrate(self,rate=2*10**6):
88 r6=self.peek(0x06); #RF_SETUP register
89 r6=r6&(~0x28); #Clear rate fields.
96 print "Setting r6=%02x." % r6;
97 self.poke(0x06,r6); #Write new setting.
98 def RF_setfreq(self,frequency):
99 """Set the frequency in Hz."""
101 print "TODO write the setfreq() function.";
102 def RF_getfreq(self):
103 """Get the frequency in Hz."""
104 print "TODO write the getfreq() function.";
106 def RF_getsmac(self):
107 """Return the source MAC address."""
110 def RF_setsmac(self,mac):
111 """Set the source MAC address."""
113 def RF_gettmac(self):
114 """Return the target MAC address."""
116 def RF_settmac(self,mac):
117 """Set the target MAC address."""
120 def RF_rxpacket(self):
121 """Get a packet from the radio. Returns None if none is waiting."""
122 print "Don't know how to get a packet.";
124 def RF_carrier(self):
125 """Hold a carrier wave on the present frequency."""
126 print "Don't know how to hold a carrier.";
128 def RF_setpacketlen(self,len=16):
129 """Set the number of bytes in the expected payload."""
130 #self.poke(0x11,len);
132 def RF_getpacketlen(self):
133 """Set the number of bytes in the expected payload."""
134 #len=self.peek(0x11);
138 def RF_getmaclen(self):
139 """Get the number of bytes in the MAC address."""
140 choices=[0, 3, 4, 5];
141 choice=self.peek(0x03)&3;
142 self.maclen=choices[choice];
144 def RF_setmaclen(self,len):
145 """Set the number of bytes in the MAC address."""
146 choices=["illegal", "illegal", "illegal",
149 self.poke(0x03,choice);