'make py2exepub' in client now uses rsync --chmod=ugo+rw to ensure the file is downlo...
[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         
47         #Automatically calibrate the len.
48         if bytes==-1:
49             bytes=1;
50             if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
51         
52         for i in range(0,bytes):
53             data=data+[(val>>(8*i))&0xFF];
54         self.writecmd(self.NRFAPP,0x03,len(data),data);
55         if self.peek(reg,bytes)!=val and reg!=0x07:
56             print "Warning, failed to set r%02x=%02x, got %02x." %(reg,
57                                                                  val,
58                                                                  self.peek(reg,bytes));
59         return;
60     
61     def status(self):
62         """Read the status byte."""
63         status=self.peek(0x07);
64         print "Status=%02x" % status;
65     
66     #Radio stuff begins here.
67     def RF_setenc(self,code="GFSK"):
68         """Set the encoding type."""
69         if code!=GFSK:
70             return "%s not supported by the NRF24L01.  Try GFSK."
71         return;
72     def RF_getenc(self):
73         """Get the encoding type."""
74         return "GFSK";
75     def RF_getrate(self):
76         rate=self.peek(0x06)&0x28;
77         if rate==0x28:
78             rate=250*10**3; #256kbps
79         elif rate==0x08:
80             rate=2*10**6;  #2Mbps
81         elif rate==0x00: 
82             rate=1*10**6;  #1Mbps
83         return rate;
84     def RF_setrate(self,rate=2*10**6):
85         r6=self.peek(0x06); #RF_SETUP register
86         r6=r6&(~0x28);   #Clear rate fields.
87         if rate==2*10**6:
88             r6=r6|0x08;
89         elif rate==1*10**6:
90             r6=r6;
91         elif rate==250*10**3:
92             r6=r6|0x20;
93         print "Setting r6=%02x." % r6;
94         self.poke(0x06,r6); #Write new setting.
95     def RF_setfreq(self,frequency):
96         """Set the frequency in Hz."""
97         
98         #On the NRF24L01+, register 0x05 is the offset in
99         #MHz above 2400.
100         
101         chan=frequency/1000000-2400;
102         self.poke(0x05,chan);
103     def RF_getfreq(self):
104         """Get the frequency in Hz."""
105         
106         #On the NRF24L01+, register 0x05 is the offset in
107         #MHz above 2400.
108         
109         return (2400+self.peek(0x05))*10**6
110         self.poke(0x05,chan);
111     def RF_getsmac(self):
112         """Return the source MAC address."""
113         
114         #Register 0A is RX_ADDR_P0, five bytes.
115         mac=self.peek(0x0A, 5);
116         return mac;
117     def RF_setsmac(self,mac):
118         """Set the source MAC address."""
119         
120         #Register 0A is RX_ADDR_P0, five bytes.
121         self.poke(0x0A, mac, 5);
122         return mac;
123     def RF_gettmac(self):
124         """Return the target MAC address."""
125         
126         #Register 0x10 is TX_ADDR, five bytes.
127         mac=self.peek(0x10, 5);
128         return mac;
129     def RF_settmac(self,mac):
130         """Set the target MAC address."""
131         
132         #Register 0x10 is TX_ADDR, five bytes.
133         self.poke(0x10, mac, 5);
134         return mac;
135
136     def RF_rxpacket(self):
137         """Get a packet from the radio.  Returns None if none is waiting."""
138         if self.peek(0x07) & 0x40:
139             #Packet has arrived.
140             self.writecmd(self.NRFAPP,0x80,0,None); #RX Packet
141             data=self.data;
142             self.poke(0x07,0x40);#clear bit.
143             return data;
144         elif self.peek(0x07)==0:
145             self.writecmd(self.NRFAPP,0x82,0,None); #Flush
146             self.poke(0x07,0x40);#clear bit.
147         return None;
148     def RF_carrier(self):
149         """Hold a carrier wave on the present frequency."""
150         # Set CONT_WAVE, PLL_LOCK, and 0dBm in RF_SETUP            
151         self.poke(0x06,8+10+4+2); 
152         
153     packetlen=16;
154     def RF_setpacketlen(self,len=16):
155         """Set the number of bytes in the expected payload."""
156         self.poke(0x11,len);
157         self.packetlen=len;
158     def RF_getpacketlen(self):
159         """Set the number of bytes in the expected payload."""
160         len=self.peek(0x11);
161         self.packetlen=len;
162         return len;
163     maclen=5;
164     def RF_getmaclen(self):
165         """Get the number of bytes in the MAC address."""
166         choices=[0, 3, 4, 5];
167         choice=self.peek(0x03)&3;
168         self.maclen=choices[choice];
169         return self.maclen;
170     def RF_setmaclen(self,len):
171         """Set the number of bytes in the MAC address."""
172         choices=["illegal", "illegal", "illegal", 
173                  1, 2, 3];
174         choice=choices[len];
175         self.poke(0x03,choice);
176         self.maclen=len;