Nordic RF client library, test script.
[goodfet] / client / GoodFETNRF.py
1 #!/usr/bin/env python
2 # GoodFET Nordic RF Radio Client
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, time, string, cStringIO, struct, glob, serial, os;
9
10 from GoodFET import GoodFET;
11
12 class GoodFETNRF(GoodFET):
13     NRFAPP=0x50;
14     def NRFsetup(self):
15         """Move the FET into the NRF application."""
16         self.writecmd(self.NRFAPP,0x10,0,self.data); #NRF/SETUP
17         
18     def NRFtrans8(self,byte):
19         """Read and write 8 bits by NRF."""
20         data=self.NRFtrans([byte]);
21         return ord(data[0]);
22     
23     def NRFtrans(self,data):
24         """Exchange data by NRF."""
25         self.data=data;
26         self.writecmd(self.NRFAPP,0x00,len(data),data);
27         return self.data;
28     
29     def peek(self,reg,bytes=1):
30         """Read an NRF Register.  For long regs, result is flipped."""
31         data=[reg,0,0,0,0,0];
32         self.writecmd(self.NRFAPP,0x02,len(data),data);
33         toret=0;
34         for i in range(0,bytes):
35             toret=toret|(ord(self.data[i+1])<<(8*i));
36         return toret;
37     def poke(self,reg,val,bytes=1):
38         """Write an NRF Register."""
39         data=[reg];
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:
44             print "Warning, failed to set register %02x." %reg;
45         return;
46     
47     def status(self):
48         """Read the status byte."""
49         self.poke(0x07,0x78); #Reset status
50         status=self.peek(0x07);
51         print "Status=%02x" % status;
52     
53     #Radio stuff begins here.
54     def RF_freq(self,frequency):
55         """Set the frequency in Hz."""
56         
57         #On the NRF24L01+, register 0x05 is the offset in
58         #MHz above 2400.
59         
60         mhz=frequency/1000000-2400;
61         print "Setting channel %i." % mhz 
62         self.poke(0x05,mhz);
63     def RF_getsmac(self):
64         """Return the source MAC address."""
65         
66         #Register 0A is RX_ADDR_P0, five bytes.
67         mac=self.peek(0x0A, 5);
68         return mac;