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 CCSPItrans8(self,byte):
19 """Read and write 8 bits by CCSPI."""
20 data=self.CCSPItrans([byte]);
23 def CCSPItrans(self,data):
24 """Exchange data by CCSPI."""
26 self.writecmd(self.CCSPIAPP,0x00,len(data),data);
29 def peek(self,reg,bytes=-1):
30 """Read a CCSPI Register. For long regs, result is flipped."""
33 #Automatically calibrate the len.
36 if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
38 self.writecmd(self.CCSPIAPP,0x02,len(data),data);
40 for i in range(0,bytes):
41 toret=toret|(ord(self.data[i+1])<<(8*i));
43 def poke(self,reg,val,bytes=-1):
44 """Write a CCSPI Register."""
47 #Automatically calibrate the len.
50 if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
52 for i in range(0,bytes):
53 data=data+[(val>>(8*i))&0xFF];
54 self.writecmd(self.CCSPIAPP,0x03,len(data),data);
55 if self.peek(reg,bytes)!=val and reg!=0x07:
56 print "Warning, failed to set r%02x=%02x, got %02x." %(reg,
58 self.peek(reg,bytes));
62 """Read the status byte."""
63 status=self.peek(0x07);
64 print "Status=%02x" % status;
66 #Radio stuff begins here.
67 def RF_setenc(self,code="GFSK"):
68 """Set the encoding type."""
70 return "%s not supported by the CCSPI24L01. Try GFSK."
73 """Get the encoding type."""
76 rate=self.peek(0x06)&0x28;
78 rate=250*10**3; #256kbps
84 def RF_setrate(self,rate=2*10**6):
85 r6=self.peek(0x06); #RF_SETUP register
86 r6=r6&(~0x28); #Clear rate fields.
93 print "Setting r6=%02x." % r6;
94 self.poke(0x06,r6); #Write new setting.
95 def RF_setfreq(self,frequency):
96 """Set the frequency in Hz."""
98 #On the CCSPI24L01+, register 0x05 is the offset in
101 chan=frequency/1000000-2400;
102 self.poke(0x05,chan);
103 def RF_getfreq(self):
104 """Get the frequency in Hz."""
106 #On the CCSPI24L01+, register 0x05 is the offset in
109 return (2400+self.peek(0x05))*10**6
110 self.poke(0x05,chan);
111 def RF_getsmac(self):
112 """Return the source MAC address."""
114 #Register 0A is RX_ADDR_P0, five bytes.
115 mac=self.peek(0x0A, 5);
117 def RF_setsmac(self,mac):
118 """Set the source MAC address."""
120 #Register 0A is RX_ADDR_P0, five bytes.
121 self.poke(0x0A, mac, 5);
123 def RF_gettmac(self):
124 """Return the target MAC address."""
126 #Register 0x10 is TX_ADDR, five bytes.
127 mac=self.peek(0x10, 5);
129 def RF_settmac(self,mac):
130 """Set the target MAC address."""
132 #Register 0x10 is TX_ADDR, five bytes.
133 self.poke(0x10, mac, 5);
136 def RF_rxpacket(self):
137 """Get a packet from the radio. Returns None if none is waiting."""
138 if self.peek(0x07) & 0x40:
140 self.writecmd(self.CCSPIAPP,0x80,0,None); #RX Packet
142 self.poke(0x07,0x40);#clear bit.
144 elif self.peek(0x07)==0:
145 self.writecmd(self.CCSPIAPP,0x82,0,None); #Flush
146 self.poke(0x07,0x40);#clear bit.
148 def RF_carrier(self):
149 """Hold a carrier wave on the present frequency."""
150 # Set CONT_WAVE, PLL_LOCK, and 0dBm in RF_SETUP
151 self.poke(0x06,8+10+4+2);
154 def RF_setpacketlen(self,len=16):
155 """Set the number of bytes in the expected payload."""
158 def RF_getpacketlen(self):
159 """Set the number of bytes in the expected payload."""
164 def RF_getmaclen(self):
165 """Get the number of bytes in the MAC address."""
166 choices=[0, 3, 4, 5];
167 choice=self.peek(0x03)&3;
168 self.maclen=choices[choice];
170 def RF_setmaclen(self,len):
171 """Set the number of bytes in the MAC address."""
172 choices=["illegal", "illegal", "illegal",
175 self.poke(0x03,choice);