Cleaning up the Telos B port.
[goodfet] / client / GoodFETCCSPI.py
1 #!/usr/bin/env python
2 # GoodFET Chipcon 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 GoodFETCCSPI(GoodFET):
13     CCSPIAPP=0x51;
14     def setup(self):
15         """Move the FET into the CCSPI application."""
16         self.writecmd(self.CCSPIAPP,0x10,0,self.data); #CCSPI/SETUP
17         
18     def trans8(self,byte):
19         """Read and write 8 bits by CCSPI."""
20         data=self.CCSPItrans([byte]);
21         return ord(data[0]);
22     
23     def trans(self,data):
24         """Exchange data by CCSPI."""
25         self.data=data;
26         self.writecmd(self.CCSPIAPP,0x00,len(data),data);
27         return self.data;
28     def strobe(self,reg=0x00):
29         """Strobes a strobe register, returning the status."""
30         data=[reg];
31         self.trans(data);
32         return ord(self.data[0]);
33     def peek(self,reg,bytes=2):
34         """Read a CCSPI Register.  For long regs, result is flipped."""
35         data=[reg,0,0];
36         
37         #Automatically calibrate the len.
38         bytes=2;
39         
40         self.writecmd(self.CCSPIAPP,0x02,len(data),data);
41         toret=0;
42         #print "Status: %02x" % ord(self.data[0]);
43         for i in range(0,bytes):
44             toret=toret|(ord(self.data[i+1])<<(8*i));
45         return toret;
46     def poke(self,reg,val,bytes=-1):
47         """Write a CCSPI Register."""
48         data=[reg];
49         
50         #Automatically calibrate the len.
51         if bytes==-1:
52             bytes=1;
53             if reg==0x0a or reg==0x0b or reg==0x10: bytes=5;
54         
55         for i in range(0,bytes):
56             data=data+[(val>>(8*i))&0xFF];
57         self.writecmd(self.CCSPIAPP,0x03,len(data),data);
58         if self.peek(reg,bytes)!=val and reg!=0x07:
59             print "Warning, failed to set r%02x=%02x, got %02x." %(reg,
60                                                                    val,
61                                                                    self.peek(reg,bytes));
62         return;
63     
64     def status(self):
65         """Read the status byte."""
66         status=self.strobe(0x00);
67         print "Status=%02x" % status;
68     
69     #Radio stuff begins here.
70     def RF_setenc(self,code="GFSK"):
71         """Set the encoding type."""
72         if code!=GFSK:
73             return "%s not supported by the CCSPI24L01.  Try GFSK."
74         return;
75     def RF_getenc(self):
76         """Get the encoding type."""
77         return "GFSK";
78     def RF_getrate(self):
79         rate=self.peek(0x06)&0x28;
80         if rate==0x28:
81             rate=250*10**3; #256kbps
82         elif rate==0x08:
83             rate=2*10**6;  #2Mbps
84         elif rate==0x00: 
85             rate=1*10**6;  #1Mbps
86         return rate;
87     def RF_setrate(self,rate=2*10**6):
88         r6=self.peek(0x06); #RF_SETUP register
89         r6=r6&(~0x28);   #Clear rate fields.
90         if rate==2*10**6:
91             r6=r6|0x08;
92         elif rate==1*10**6:
93             r6=r6;
94         elif rate==250*10**3:
95             r6=r6|0x20;
96         print "Setting r6=%02x." % r6;
97         self.poke(0x06,r6); #Write new setting.
98     def RF_setfreq(self,frequency):
99         """Set the frequency in Hz."""
100         
101         print "TODO write the setfreq() function.";
102     def RF_getfreq(self):
103         """Get the frequency in Hz."""
104         print "TODO write the getfreq() function.";
105         return 0;
106     def RF_getsmac(self):
107         """Return the source MAC address."""
108         
109         return 0xdeadbeef;
110     def RF_setsmac(self,mac):
111         """Set the source MAC address."""
112         return 0xdeadbeef;
113     def RF_gettmac(self):
114         """Return the target MAC address."""
115         return 0xdeadbeef;
116     def RF_settmac(self,mac):
117         """Set the target MAC address."""
118         return 0xdeadbeef;
119
120     def RF_rxpacket(self):
121         """Get a packet from the radio.  Returns None if none is waiting."""
122         print "Don't know how to get a packet.";
123         return None;
124     def RF_carrier(self):
125         """Hold a carrier wave on the present frequency."""
126         print "Don't know how to hold a carrier.";
127     packetlen=16;
128     def RF_setpacketlen(self,len=16):
129         """Set the number of bytes in the expected payload."""
130         #self.poke(0x11,len);
131         self.packetlen=len;
132     def RF_getpacketlen(self):
133         """Set the number of bytes in the expected payload."""
134         #len=self.peek(0x11);
135         self.packetlen=len;
136         return len;
137     maclen=5;
138     def RF_getmaclen(self):
139         """Get the number of bytes in the MAC address."""
140         choices=[0, 3, 4, 5];
141         choice=self.peek(0x03)&3;
142         self.maclen=choices[choice];
143         return self.maclen;
144     def RF_setmaclen(self,len):
145         """Set the number of bytes in the MAC address."""
146         choices=["illegal", "illegal", "illegal", 
147                  1, 2, 3];
148         choice=choices[len];
149         self.poke(0x03,choice);
150         self.maclen=len;