2 # GoodFET Nordic 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, os;
10 from GoodFET import GoodFET;
12 class GoodFETNRF(GoodFET):
15 """Move the FET into the NRF application."""
16 self.writecmd(self.NRFAPP,0x10,0,self.data); #NRF/SETUP
18 def NRFtrans8(self,byte):
19 """Read and write 8 bits by NRF."""
20 data=self.NRFtrans([byte]);
23 def NRFtrans(self,data):
24 """Exchange data by NRF."""
26 self.writecmd(self.NRFAPP,0x00,len(data),data);
29 def tune(self,tuning="aa,c78c65805e,14,09"):
32 fields=tuning.split(",");
41 def peek(self,reg,bytes=-1):
42 """Read an NRF Register. For long regs, result is flipped."""
45 #Automatically calibrate the len.
48 if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
50 self.writecmd(self.NRFAPP,0x02,len(data),data);
52 for i in range(0,bytes):
53 toret=toret|(ord(self.data[i+1])<<(8*i));
55 def poke(self,reg,val,bytes=-1):
56 """Write an NRF Register."""
59 #Automatically calibrate the len.
62 if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
64 for i in range(0,bytes):
65 data=data+[(val>>(8*i))&0xFF];
66 self.writecmd(self.NRFAPP,0x03,len(data),data);
67 if self.peek(reg,bytes)!=val and reg!=0x07:
68 print "Warning, failed to set r%02x=%02x, got %02x." %(
71 self.peek(reg,bytes));
76 """Read the status byte."""
77 status=self.peek(0x07);
78 print "Status=%02x" % status;
80 #Radio stuff begins here.
81 def RF_setenc(self,code="GFSK"):
82 """Set the encoding type."""
84 return "%s not supported by the NRF24L01. Try GFSK."
87 """Get the encoding type."""
90 rate=self.peek(0x06)&0x28;
92 rate=250*10**3; #256kbps
98 def RF_setrate(self,rate=2*10**6):
99 r6=self.peek(0x06); #RF_SETUP register
100 r6=r6&(~0x28); #Clear rate fields.
105 elif rate==250*10**3:
107 print "Setting r6=%02x." % r6;
108 self.poke(0x06,r6); #Write new rate.
109 def RF_setfreq(self,frequency):
110 """Set the frequency in Hz."""
112 #On the NRF24L01+, register 0x05 is the offset in
115 chan=frequency/1000000-2400;
116 self.poke(0x05,chan);
119 def RF_getfreq(self):
120 """Get the frequency in Hz."""
122 #On the NRF24L01+, register 0x05 is the offset in
125 return (2400+self.peek(0x05))*10**6
126 self.poke(0x05,chan);
127 def RF_getsmac(self):
128 """Return the source MAC address."""
130 #Register 0A is RX_ADDR_P0, five bytes.
131 mac=self.peek(0x0A, 5);
133 def RF_setsmac(self,mac):
134 """Set the source MAC address."""
136 #Register 0A is RX_ADDR_P0, five bytes.
137 self.poke(0x0A, mac, 5);
139 def RF_gettmac(self):
140 """Return the target MAC address."""
142 #Register 0x10 is TX_ADDR, five bytes.
143 mac=self.peek(0x10, 5);
145 def RF_settmac(self,mac):
146 """Set the target MAC address."""
148 #Register 0x10 is TX_ADDR, five bytes.
149 self.poke(0x10, mac, 5);
152 def RF_rxpacket(self):
153 """Get a packet from the radio. Returns None if none is waiting."""
154 if self.peek(0x07) & 0x40:
156 self.writecmd(self.NRFAPP,0x80,0,None); #RX Packet
158 self.poke(0x07,0x40);#clear bit.
160 elif self.peek(0x07)==0:
161 self.writecmd(self.NRFAPP,0x82,0,None); #Flush
162 self.poke(0x07,0x40);#clear bit.
164 def RF_txpacket(self,payload):
165 """Transmit a packet. Untested."""
166 if self.peek(0x07) & 0x40:
168 self.writecmd(self.NRFAPP,0x81,0,None); #RX Packet
170 self.poke(0x07,0x40);#clear bit.
172 elif self.peek(0x07)==0:
173 self.writecmd(self.NRFAPP,0x83,0,None); #Flush
174 self.poke(0x07,0x40);#clear bit.
177 def RF_carrier(self):
178 """Hold a carrier wave on the present frequency."""
179 # Set CONT_WAVE, PLL_LOCK, and 0dBm in RF_SETUP
180 self.poke(0x06,8+10+4+2);
183 def RF_setpacketlen(self,len=16):
184 """Set the number of bytes in the expected payload."""
187 def RF_getpacketlen(self):
188 """Set the number of bytes in the expected payload."""
193 def RF_getmaclen(self):
194 """Get the number of bytes in the MAC address."""
195 choices=[2, 3, 4, 5];
196 choice=self.peek(0x03)&3;
197 self.maclen=choices[choice];
199 def RF_setmaclen(self,len):
200 """Set the number of bytes in the MAC address."""
201 choices=["illegal", "illegal",
206 self.poke(0x03,choice);