74be559b800994770b6c1cfc29a61d3b967f3f4c
[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         
33         #Automatically calibrate the len.
34         if bytes==-1:
35             bytes=1;
36             if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
37         
38         self.writecmd(self.NRFAPP,0x02,len(data),data);
39         toret=0;
40         for i in range(0,bytes):
41             toret=toret|(ord(self.data[i+1])<<(8*i));
42         return toret;
43     def poke(self,reg,val,bytes=1):
44         """Write an NRF Register."""
45         data=[reg];
46         for i in range(0,bytes):
47             data=data+[(val>>(8*i))&0xFF];
48         self.writecmd(self.NRFAPP,0x03,len(data),data);
49         if self.peek(reg,bytes)!=val and reg!=0x07:
50             print "Warning, failed to set register %02x." %reg;
51         return;
52     
53     def status(self):
54         """Read the status byte."""
55         status=self.peek(0x07);
56         print "Status=%02x" % status;
57     
58     #Radio stuff begins here.
59     def RF_setfreq(self,frequency):
60         """Set the frequency in Hz."""
61         
62         #On the NRF24L01+, register 0x05 is the offset in
63         #MHz above 2400.
64         
65         chan=frequency/1000000-2400;
66         self.poke(0x05,chan);
67     def RF_getfreq(self):
68         """Get the frequency in Hz."""
69         
70         #On the NRF24L01+, register 0x05 is the offset in
71         #MHz above 2400.
72         
73         return (2400+self.peek(0x05))*10**6
74         self.poke(0x05,chan);
75     def RF_getsmac(self):
76         """Return the source MAC address."""
77         
78         #Register 0A is RX_ADDR_P0, five bytes.
79         mac=self.peek(0x0A, 5);
80         return mac;
81     def RF_setsmac(self,mac):
82         """Set the source MAC address."""
83         
84         #Register 0A is RX_ADDR_P0, five bytes.
85         self.poke(0x0A, mac, 5);
86         return mac;
87     def RF_gettmac(self):
88         """Return the target MAC address."""
89         
90         #Register 0x10 is TX_ADDR, five bytes.
91         mac=self.peek(0x10, 5);
92         return mac;
93     def RF_settmac(self,mac):
94         """Set the target MAC address."""
95         
96         #Register 0x10 is TX_ADDR, five bytes.
97         self.poke(0x10, mac, 5);
98         return mac;
99
100     def RF_rxpacket(self):
101         """Get a packet from the radio.  Returns None if none is waiting."""
102         if self.peek(0x07) & 0x40:
103             #Packet has arrived.
104             self.writecmd(self.NRFAPP,0x80,0,None); #RX Packet
105             data=self.data;
106             self.poke(0x07,0x40);#clear bit.
107             return data;
108         elif self.peek(0x07)==0:
109             self.writecmd(self.NRFAPP,0x82,0,None); #Flush
110             self.poke(0x07,0x40);#clear bit.
111         return None;
112     packetlen=16;
113     def RF_setpacketlen(self,len=16):
114         """Set the number of bytes in the expected payload."""
115         self.poke(0x11,len);
116         self.packetlen=len;
117     def RF_getpacketlen(self):
118         """Set the number of bytes in the expected payload."""
119         len=self.peek(0x11);
120         self.packetlen=len;
121         return len;
122     maclen=5;
123     def RF_getmaclen(self):
124         """Get the number of bytes in the MAC address."""
125         choices=["illegal", 3, 4, 5];
126         choice=self.peek(0x03)&3;
127         self.maclen=choices[choice];
128         return self.maclen;
129     def RF_setmaclen(self,len):
130         """Set the number of bytes in the MAC address."""
131         choices=["illegal", "illegal", "illegal", 
132                  1, 2, 3];
133         choice=choices[len];
134         self.poke(0x03,choice);
135         self.maclen=len;