1
2
3 from GoodFETAVR import GoodFETAVR
4 import sys, binascii, os, array, time, glob, struct
5
6 fmt = ("B", "<H", None, "<L")
7
9 ATMELRADIOAPP = 0x53
10 autocrc = 0
11 verbose = False
12 connected = 0
13 enable_AACK = False
14 - def serInit(self, port=None, timeout=2, attemptlimit=None):
15 if port==None:
16 port=os.environ.get("GOODFET");
17 self.pyserInit(port, timeout, attemptlimit)
18
19 - def pyserInit(self, port, timeout, attemptlimit):
20 """Open the serial port"""
21 if self.connected == 0:
22 if (not (attemptlimit == None)) and (attemptlimit <= 1):
23
24 attemptlimit == 2
25
26
27 import serial;
28
29 if os.name=='nt' and sys.version.find('64 bit')!=-1:
30 print "WARNING: PySerial requires a 32-bit Python build in Windows.";
31
32 if port is None and os.environ.get("GOODFET")!=None:
33 glob_list = glob.glob(os.environ.get("GOODFET"));
34 if len(glob_list) > 0:
35 port = glob_list[0];
36 else:
37 port = os.environ.get("GOODFET");
38 if port is None:
39 glob_list = glob.glob("/dev/tty.usbserial*");
40 if len(glob_list) > 0:
41 port = glob_list[0];
42 if port is None:
43 glob_list = glob.glob("/dev/ttyUSB*");
44 if len(glob_list) > 0:
45 port = glob_list[0];
46 if port is None:
47 glob_list = glob.glob("/dev/ttyU0");
48 if len(glob_list) > 0:
49 port = glob_list[0];
50 if port is None and os.name=='nt':
51 from scanwin32 import winScan;
52 scan=winScan();
53 for order,comport,desc,hwid in sorted(scan.comports()):
54 try:
55 if hwid.index('FTDI')==0:
56 port=comport;
57
58 except:
59
60 a=1;
61
62 baud=115200;
63
64 self.serialport = serial.Serial(
65 port,
66 baud,
67 parity = serial.PARITY_NONE,
68 timeout=timeout
69 )
70
71 self.verb=0;
72 self.data=""
73 attempts=0;
74 self.connected=0;
75
76 while self.connected==0:
77 self.serialport.setDTR(False)
78 while self.verb!=0x7F or self.data!="http://goodfet.sf.net/":
79 if attemptlimit is not None and attempts >= attemptlimit:
80 return
81
82 attempts=attempts+1;
83 self.readcmd();
84 if self.verbose:
85 print "Got %02x,%02x:'%s'" % (self.app,self.verb,self.data);
86
87
88
89 for foo in range(1,30):
90 time.sleep(1)
91 if not self.monitorecho():
92 self.connected = 0
93 if self.verbose:
94 print "Comm error on try %i." % (foo)
95 else:
96 self.connected = 1
97 break
98 if self.verbose:
99 print "Connected after %02i attempts." % attempts;
100 self.serialport.setTimeout(12);
101
105
106
107 - def writecmd(self, app, verb, count=0, data=[]):
108 """Write a command and some data to the GoodFET."""
109 self.serialport.write(chr(app));
110 self.serialport.write(chr(verb));
111 if self.verbose:
112 print "Tx: ( 0x%02x, 0x%02x, %d )" % ( app, verb, count )
113 if count > 0:
114 if(isinstance(data,list)):
115 old = data
116 data = []
117 for i in range(0,count):
118 data += chr(old[i]);
119 outstr=''.join(data);
120
121
122 count = len(outstr)
123 self.serialport.write(chr(count&0xFF));
124 self.serialport.write(chr(count>>8));
125 if count > 0:
126 if self.verbose:
127 print "sending: %s" %outstr.encode("hex")
128 self.serialport.write(outstr);
129
130
131 if not self.besilent:
132 out = self.readcmd()
133 if out and self.verbose:
134 print "read: " + out.encode("hex")
135 return out
136 else:
137 return None
138
140
141 """Read a reply from the GoodFET."""
142 app = self.serialport.read(1)
143
144 if len(app) < 1:
145 if self.verbose:
146 print "Rx: None"
147
148 self.app = 0
149 self.verb = 0
150 self.count = 0
151 self.data = ""
152 return
153
154 self.app=ord(app);
155
156 v = self.serialport.read(1);
157 if v:
158 self.verb = ord(v)
159 else:
160 self.verb = 0
161
162 c1 = self.serialport.read(1)
163 c2 = self.serialport.read(1)
164 if (c1 and c2):
165 self.count= ord(c1) + (ord(c2)<<8)
166 else:
167 self.count = 0
168 if self.verbose:
169 print "Rx: ( 0x%02x, 0x%02x, %i )" % ( self.app, self.verb, self.count )
170
171
172 if self.app==0xFF:
173 if self.verb==0xFF:
174 print "# DEBUG %s" % self.serialport.read(self.count)
175 elif self.verb==0xFE:
176 print "# DEBUG 0x%x" % struct.unpack(fmt[self.count-1], self.serialport.read(self.count))[0]
177 elif self.verb==0xFD:
178
179 print "# NOP.";
180 return ""
181 else:
182 self.data=self.serialport.read(self.count);
183 return self.data;
184
186 if (chan < 11) or (chan > 26):
187 print "Channel out of range"
188 else:
189 self.poke(0x8, chan)
190
191 - def peek(self,reg,bytes=1):
192 """Read a Register. """
193
194 if bytes != 1:
195 print "Warning, currently cannot poke more than 1 byte"
196 bytes = 1
197 data = [reg, 0, bytes%255, bytes>>8]
198 self.data = None
199 self.writecmd(self.ATMELRADIOAPP,0x02,len(data),data);
200 toret=0;
201
202 if self.data:
203
204
205
206
207 return ord(self.data)
208 else:
209 return -1
210
211 - def poke(self,reg,val,bytes=1):
212 """Write an Register."""
213 data = [reg, 0]
214 data=[reg, 0]
215 if bytes != 1:
216 print "Warning, currently cannot poke more than 1 byte"
217 bytes = 1
218 for i in range(0,bytes):
219 data=data+[(val>>(8*i))&0xFF];
220
221 self.writecmd(self.ATMELRADIOAPP,0x03,len(data),data);
222 newval = self.peek(reg,bytes)
223 if newval!=val:
224 print "Warning, failed to set r%02x=%02x, got %02x." %(
225 reg,
226 val,
227 newval);
228
229 return;
230
233
236
247
249 if type(payload) == list:
250 import array
251 payload = array.array('B', payload).tostring()
252 self.writecmd(self.ATMELRADIOAPP, 0x81, len(payload), payload)
253
254
256 """Returns the received signal strength"""
257 base = -90
258 val = self.peek(0x7) & 0x7f
259 if val == 0:
260 return base - 1
261 elif val < 0x53:
262 return val + base
263 else:
264 return 0x53 + base
265
273
274
281