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 peek(self,reg,bytes=1):
30 """Read an NRF Register. For long regs, result is flipped."""
32 self.writecmd(self.NRFAPP,0x02,len(data),data);
34 for i in range(0,bytes):
35 toret=toret|(ord(self.data[i+1])<<(8*i));
37 def poke(self,reg,val,bytes=1):
38 """Write an NRF Register."""
40 for i in range(0,bytes):
41 data=data+[(val>>(8*i))&0xFF];
42 self.writecmd(self.NRFAPP,0x03,len(data),data);
43 if self.peek(reg,bytes)!=val and reg!=0x07:
44 print "Warning, failed to set register %02x." %reg;
48 """Read the status byte."""
49 status=self.peek(0x07);
50 print "Status=%02x" % status;
52 #Radio stuff begins here.
53 def RF_freq(self,frequency):
54 """Set the frequency in Hz."""
56 #On the NRF24L01+, register 0x05 is the offset in
59 mhz=frequency/1000000-2400;
60 print "Setting channel %i." % mhz
63 """Return the source MAC address."""
65 #Register 0A is RX_ADDR_P0, five bytes.
66 mac=self.peek(0x0A, 5);
68 def RF_setsmac(self,mac):
69 """Set the source MAC address."""
71 #Register 0A is RX_ADDR_P0, five bytes.
72 self.poke(0x0A, mac, 5);
75 """Return the target MAC address."""
77 #Register 0x10 is TX_ADDR, five bytes.
78 mac=self.peek(0x0A, 5);
80 def RF_settmac(self,mac):
81 """Set the target MAC address."""
83 #Register 0x10 is TX_ADDR, five bytes.
84 self.poke(0x10, mac, 5);
86 def RF_rxpacket(self):
87 """Get a packet from the radio. Returns None if none is waiting."""
88 if self.peek(0x07) & 0x40:
90 self.writecmd(self.NRFAPP,0x80,0,None); #RX Packet
92 self.poke(0x07,0x40);#clear bit.
94 elif self.peek(0x07)==0:
95 self.writecmd(self.NRFAPP,0x82,0,None); #Flush
96 self.poke(0x07,0x40);#clear bit.
99 def RF_setpacketlen(self,len=16):
100 """Set the number of bytes in the expected payload."""