jtagarm: a few cleanup things...
[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, 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 tune(self,tuning="aa,c78c65805e,14,09"):
30         """Tune the radio."""
31         #MAC,rA,r5,r6
32         fields=tuning.split(",");
33         ra=int(fields[1],16);
34         r5=int(fields[2],16);
35         r6=int(fields[3],16);
36         self.poke(0x0a,ra,5);
37         self.poke(0x05,r5,1);
38         self.poke(0x06,r6,1);
39         self.RF_setmaclen(3);
40         return;
41     def peek(self,reg,bytes=-1):
42         """Read an NRF Register.  For long regs, result is flipped."""
43         data=[reg,0,0,0,0,0];
44         
45         #Automatically calibrate the len.
46         if bytes==-1:
47             bytes=1;
48             if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
49         
50         self.writecmd(self.NRFAPP,0x02,len(data),data);
51         toret=0;
52         for i in range(0,bytes):
53             toret=toret|(ord(self.data[i+1])<<(8*i));
54         return toret;
55     def poke(self,reg,val,bytes=-1):
56         """Write an NRF Register."""
57         data=[reg];
58         
59         #Automatically calibrate the len.
60         if bytes==-1:
61             bytes=1;
62             if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
63         
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." %(
69                 reg,
70                 val,
71                 self.peek(reg,bytes));
72         
73         return;
74     
75     def status(self):
76         """Read the status byte."""
77         status=self.peek(0x07);
78         print "Status=%02x" % status;
79     
80     #Radio stuff begins here.
81     def RF_setenc(self,code="GFSK"):
82         """Set the encoding type."""
83         if code!=GFSK:
84             return "%s not supported by the NRF24L01.  Try GFSK."
85         return;
86     def RF_getenc(self):
87         """Get the encoding type."""
88         return "GFSK";
89     def RF_getrate(self):
90         rate=self.peek(0x06)&0x28;
91         if rate==0x20:
92             rate=250*10**3; #256kbps
93         elif rate==0x08:
94             rate=2*10**6;  #2Mbps
95         elif rate==0x00: 
96             rate=1*10**6;  #1Mbps
97         return rate;
98     def RF_setrate(self,rate=2*10**6):
99         r6=self.peek(0x06); #RF_SETUP register
100         r6=r6&(~0x28);   #Clear rate fields.
101         if rate==2*10**6:
102             r6=r6|0x08;
103         elif rate==1*10**6:
104             r6=r6;
105         elif rate==250*10**3:
106             r6=r6|0x20;
107         print "Setting r6=%02x." % r6;
108         self.poke(0x06,r6); #Write new rate.
109     def RF_setfreq(self,frequency):
110         """Set the frequency in Hz."""
111         
112         #On the NRF24L01+, register 0x05 is the offset in
113         #MHz above 2400.
114         
115         chan=frequency/1000000-2400;
116         self.poke(0x05,chan);
117
118
119     def RF_getfreq(self):
120         """Get the frequency in Hz."""
121         
122         #On the NRF24L01+, register 0x05 is the offset in
123         #MHz above 2400.
124         
125         return (2400+self.peek(0x05))*10**6
126         self.poke(0x05,chan);
127     def RF_getsmac(self):
128         """Return the source MAC address."""
129         
130         #Register 0A is RX_ADDR_P0, five bytes.
131         mac=self.peek(0x0A, 5);
132         return mac;
133     def RF_setsmac(self,mac):
134         """Set the source MAC address."""
135         
136         #Register 0A is RX_ADDR_P0, five bytes.
137         self.poke(0x0A, mac, 5);
138         return mac;
139     def RF_gettmac(self):
140         """Return the target MAC address."""
141         
142         #Register 0x10 is TX_ADDR, five bytes.
143         mac=self.peek(0x10, 5);
144         return mac;
145     def RF_settmac(self,mac):
146         """Set the target MAC address."""
147         
148         #Register 0x10 is TX_ADDR, five bytes.
149         self.poke(0x10, mac, 5);
150         return mac;
151
152     def RF_rxpacket(self):
153         """Get a packet from the radio.  Returns None if none is waiting."""
154         if self.peek(0x07) & 0x40:
155             #Packet has arrived.
156             self.writecmd(self.NRFAPP,0x80,0,None); #RX Packet
157             data=self.data;
158             self.poke(0x07,0x40);#clear bit.
159             return data;
160         elif self.peek(0x07)==0:
161             self.writecmd(self.NRFAPP,0x82,0,None); #Flush
162             self.poke(0x07,0x40);#clear bit.
163         return None;
164     def RF_txpacket(self,payload):
165         """Transmit a packet.  Untested."""
166         if self.peek(0x07) & 0x40:
167             #Packet has arrived.
168             self.writecmd(self.NRFAPP,0x81,0,None); #RX Packet
169             data=self.data;
170             self.poke(0x07,0x40);#clear bit.
171             return data;
172         elif self.peek(0x07)==0:
173             self.writecmd(self.NRFAPP,0x83,0,None); #Flush
174             self.poke(0x07,0x40);#clear bit.
175         return None;
176
177     def RF_carrier(self):
178         """Hold a carrier wave on the present frequency."""
179         # Set CONT_WAVE, PLL_LOCK, and 0dBm in RF_SETUP            
180         self.poke(0x06,8+10+4+2); 
181     
182     packetlen=16;
183     def RF_setpacketlen(self,len=16):
184         """Set the number of bytes in the expected payload."""
185         self.poke(0x11,len);
186         self.packetlen=len;
187     def RF_getpacketlen(self):
188         """Set the number of bytes in the expected payload."""
189         len=self.peek(0x11);
190         self.packetlen=len;
191         return len;
192     maclen=5;
193     def RF_getmaclen(self):
194         """Get the number of bytes in the MAC address."""
195         choices=[2, 3, 4, 5];
196         choice=self.peek(0x03)&3;
197         self.maclen=choices[choice];
198         return self.maclen;
199     def RF_setmaclen(self,len):
200         """Set the number of bytes in the MAC address."""
201         choices=["illegal", "illegal",
202                  0,       #undocumented 
203                  1, 2, 3  #documented
204                  ];
205         choice=choices[len];
206         self.poke(0x03,choice);
207         self.maclen=len;