Getting closer on MSP430X2 support.
[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 rate.
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
104
105     def RF_getfreq(self):
106         """Get the frequency in Hz."""
107         
108         #On the NRF24L01+, register 0x05 is the offset in
109         #MHz above 2400.
110         
111         return (2400+self.peek(0x05))*10**6
112         self.poke(0x05,chan);
113     def RF_getsmac(self):
114         """Return the source MAC address."""
115         
116         #Register 0A is RX_ADDR_P0, five bytes.
117         mac=self.peek(0x0A, 5);
118         return mac;
119     def RF_setsmac(self,mac):
120         """Set the source MAC address."""
121         
122         #Register 0A is RX_ADDR_P0, five bytes.
123         self.poke(0x0A, mac, 5);
124         return mac;
125     def RF_gettmac(self):
126         """Return the target MAC address."""
127         
128         #Register 0x10 is TX_ADDR, five bytes.
129         mac=self.peek(0x10, 5);
130         return mac;
131     def RF_settmac(self,mac):
132         """Set the target MAC address."""
133         
134         #Register 0x10 is TX_ADDR, five bytes.
135         self.poke(0x10, mac, 5);
136         return mac;
137
138     def RF_rxpacket(self):
139         """Get a packet from the radio.  Returns None if none is waiting."""
140         if self.peek(0x07) & 0x40:
141             #Packet has arrived.
142             self.writecmd(self.NRFAPP,0x80,0,None); #RX Packet
143             data=self.data;
144             self.poke(0x07,0x40);#clear bit.
145             return data;
146         elif self.peek(0x07)==0:
147             self.writecmd(self.NRFAPP,0x82,0,None); #Flush
148             self.poke(0x07,0x40);#clear bit.
149         return None;
150     def RF_txpacket(self,payload):
151         """Transmit a packet.  Untested."""
152         if self.peek(0x07) & 0x40:
153             #Packet has arrived.
154             self.writecmd(self.NRFAPP,0x81,0,None); #RX Packet
155             data=self.data;
156             self.poke(0x07,0x40);#clear bit.
157             return data;
158         elif self.peek(0x07)==0:
159             self.writecmd(self.NRFAPP,0x83,0,None); #Flush
160             self.poke(0x07,0x40);#clear bit.
161         return None;
162
163     def RF_carrier(self):
164         """Hold a carrier wave on the present frequency."""
165         # Set CONT_WAVE, PLL_LOCK, and 0dBm in RF_SETUP            
166         self.poke(0x06,8+10+4+2); 
167         
168     packetlen=16;
169     def RF_setpacketlen(self,len=16):
170         """Set the number of bytes in the expected payload."""
171         self.poke(0x11,len);
172         self.packetlen=len;
173     def RF_getpacketlen(self):
174         """Set the number of bytes in the expected payload."""
175         len=self.peek(0x11);
176         self.packetlen=len;
177         return len;
178     maclen=5;
179     def RF_getmaclen(self):
180         """Get the number of bytes in the MAC address."""
181         choices=[0, 3, 4, 5];
182         choice=self.peek(0x03)&3;
183         self.maclen=choices[choice];
184         return self.maclen;
185     def RF_setmaclen(self,len):
186         """Set the number of bytes in the MAC address."""
187         choices=["illegal", "illegal", "illegal", 
188                  1, 2, 3];
189         choice=choices[len];
190         self.poke(0x03,choice);
191         self.maclen=len;