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, serial, 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." %(reg,
70 self.peek(reg,bytes));
74 """Read the status byte."""
75 status=self.peek(0x07);
76 print "Status=%02x" % status;
78 #Radio stuff begins here.
79 def RF_setenc(self,code="GFSK"):
80 """Set the encoding type."""
82 return "%s not supported by the NRF24L01. Try GFSK."
85 """Get the encoding type."""
88 rate=self.peek(0x06)&0x28;
90 rate=250*10**3; #256kbps
96 def RF_setrate(self,rate=2*10**6):
97 r6=self.peek(0x06); #RF_SETUP register
98 r6=r6&(~0x28); #Clear rate fields.
103 elif rate==250*10**3:
105 print "Setting r6=%02x." % r6;
106 self.poke(0x06,r6); #Write new rate.
107 def RF_setfreq(self,frequency):
108 """Set the frequency in Hz."""
110 #On the NRF24L01+, register 0x05 is the offset in
113 chan=frequency/1000000-2400;
114 self.poke(0x05,chan);
117 def RF_getfreq(self):
118 """Get the frequency in Hz."""
120 #On the NRF24L01+, register 0x05 is the offset in
123 return (2400+self.peek(0x05))*10**6
124 self.poke(0x05,chan);
125 def RF_getsmac(self):
126 """Return the source MAC address."""
128 #Register 0A is RX_ADDR_P0, five bytes.
129 mac=self.peek(0x0A, 5);
131 def RF_setsmac(self,mac):
132 """Set the source MAC address."""
134 #Register 0A is RX_ADDR_P0, five bytes.
135 self.poke(0x0A, mac, 5);
137 def RF_gettmac(self):
138 """Return the target MAC address."""
140 #Register 0x10 is TX_ADDR, five bytes.
141 mac=self.peek(0x10, 5);
143 def RF_settmac(self,mac):
144 """Set the target MAC address."""
146 #Register 0x10 is TX_ADDR, five bytes.
147 self.poke(0x10, mac, 5);
150 def RF_rxpacket(self):
151 """Get a packet from the radio. Returns None if none is waiting."""
152 if self.peek(0x07) & 0x40:
154 self.writecmd(self.NRFAPP,0x80,0,None); #RX Packet
156 self.poke(0x07,0x40);#clear bit.
158 elif self.peek(0x07)==0:
159 self.writecmd(self.NRFAPP,0x82,0,None); #Flush
160 self.poke(0x07,0x40);#clear bit.
162 def RF_txpacket(self,payload):
163 """Transmit a packet. Untested."""
164 if self.peek(0x07) & 0x40:
166 self.writecmd(self.NRFAPP,0x81,0,None); #RX Packet
168 self.poke(0x07,0x40);#clear bit.
170 elif self.peek(0x07)==0:
171 self.writecmd(self.NRFAPP,0x83,0,None); #Flush
172 self.poke(0x07,0x40);#clear bit.
175 def RF_carrier(self):
176 """Hold a carrier wave on the present frequency."""
177 # Set CONT_WAVE, PLL_LOCK, and 0dBm in RF_SETUP
178 self.poke(0x06,8+10+4+2);
181 def RF_setpacketlen(self,len=16):
182 """Set the number of bytes in the expected payload."""
185 def RF_getpacketlen(self):
186 """Set the number of bytes in the expected payload."""
191 def RF_getmaclen(self):
192 """Get the number of bytes in the MAC address."""
193 choices=[2, 3, 4, 5];
194 choice=self.peek(0x03)&3;
195 self.maclen=choices[choice];
197 def RF_setmaclen(self,len):
198 """Set the number of bytes in the MAC address."""
199 choices=["illegal", "illegal",
204 self.poke(0x03,choice);