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