Some CC2430 definition have empty descriptions.
[goodfet] / client / GoodFET.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
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
11 class GoodFET:
12     """GoodFET Client Library"""
13     
14     GLITCHAPP=0x71;
15     
16     def __init__(self, *args, **kargs):
17         self.data=[0];
18     def timeout(self):
19         print "timeout\n";
20     def serInit(self, port=None):
21         """Open the serial port"""
22         
23         if port is None and os.environ.get("GOODFET")!=None:
24             glob_list = glob.glob(os.environ.get("GOODFET"));
25             if len(glob_list) > 0:
26                 port = glob_list[0];
27         if port is None:
28             glob_list = glob.glob("/dev/tty.usbserial*");
29             if len(glob_list) > 0:
30                 port = glob_list[0];
31         if port is None:
32             glob_list = glob.glob("/dev/ttyUSB*");
33             if len(glob_list) > 0:
34                 port = glob_list[0];
35         
36         self.serialport = serial.Serial(
37             port,
38             #9600,
39             115200,
40             parity = serial.PARITY_NONE
41             )
42         
43         #This might cause problems, but it makes failure graceful.
44         #self.serialport._timeout = 5;
45         
46         #Explicitly set RTS and DTR to halt board.
47         self.serialport.setRTS(1);
48         self.serialport.setDTR(1);
49         #Drop DTR, which is !RST, low to begin the app.
50         self.serialport.setDTR(0);
51         self.serialport.flushInput()
52         self.serialport.flushOutput()
53         
54         #Read and handle the initial command.
55         #time.sleep(1);
56         self.readcmd(); #Read the first command.
57         if(self.verb!=0x7F):
58             print "Verb %02x is wrong.  Incorrect firmware?" % self.verb;
59         #print "Connected."
60     def getbuffer(self,size=0x1c00):
61         writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
62         print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
63     def writecmd(self, app, verb, count=0, data=[]):
64         """Write a command and some data to the GoodFET."""
65         self.serialport.write(chr(app));
66         self.serialport.write(chr(verb));
67         
68         #if data!=None:
69         #    count=len(data); #Initial count ignored.
70         
71         #print "TX %02x %02x %04x" % (app,verb,count);
72         
73         #little endian 16-bit length
74         self.serialport.write(chr(count&0xFF));
75         self.serialport.write(chr(count>>8));
76         
77         #print "count=%02x, len(data)=%04x" % (count,len(data));
78         
79         if count!=0:
80             if(isinstance(data,list)):
81                 for i in range(0,count):
82                 #print "Converting %02x at %i" % (data[i],i)
83                     data[i]=chr(data[i]);
84             #print type(data);
85             outstr=''.join(data);
86             self.serialport.write(outstr);
87         if not self.besilent:
88             self.readcmd();
89         
90     besilent=0;
91     app=0;
92     verb=0;
93     count=0;
94     data="";
95
96     def readcmd(self):
97         """Read a reply from the GoodFET."""
98         while 1:
99             #print "Reading...";
100             self.app=ord(self.serialport.read(1));
101             #print "APP=%2x" % self.app;
102             self.verb=ord(self.serialport.read(1));
103             #print "VERB=%02x" % self.verb;
104             self.count=(
105                 ord(self.serialport.read(1))
106                 +(ord(self.serialport.read(1))<<8)
107                 );
108             
109             #Debugging string; print, but wait.
110             if self.app==0xFF and self.verb==0xFF:
111                 print "# DEBUG %s" % self.serialport.read(self.count);
112                 sys.stdout.flush();
113             else:
114                 self.data=self.serialport.read(self.count);
115                 return self.data;
116     #Glitching stuff.
117     def glitchApp(self,app):
118         """Glitch into a device by its application."""
119         self.data=[app&0xff];
120         self.writecmd(self.GLITCHAPP,0x80,1,self.data);
121         #return ord(self.data[0]);
122     def glitchVerb(self,app,verb,data):
123         """Glitch during a transaction."""
124         if data==None: data=[];
125         self.data=[app&0xff, verb&0xFF]+data;
126         self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
127         #return ord(self.data[0]);
128     def glitchstart(self):
129         """Glitch into the AVR application."""
130         self.glitchVerb(self.APP,0x20,None);
131     def glitchstarttime(self):
132         """Measure the timer of the START verb."""
133         return self.glitchTime(self.APP,0x20,None);
134     def glitchTime(self,app,verb,data):
135         """Time the execution of a verb."""
136         if data==None: data=[];
137         self.data=[app&0xff, verb&0xFF]+data;
138         self.writecmd(self.GLITCHAPP,0x82,len(self.data),self.data);
139         return ord(self.data[0])+(ord(self.data[1])<<8);
140     def glitchVoltages(self,low=0x0880, high=0x0fff):
141         """Set glitching voltages. (0x0fff is max.)"""
142         self.data=[low&0xff, (low>>8)&0xff,
143                    high&0xff, (high>>8)&0xff];
144         self.writecmd(self.GLITCHAPP,0x90,4,self.data);
145         #return ord(self.data[0]);
146     def glitchRate(self,count=0x0800):
147         """Set glitching count period."""
148         self.data=[count&0xff, (count>>8)&0xff];
149         self.writecmd(self.GLITCHAPP,0x91,2,
150                       self.data);
151         #return ord(self.data[0]);
152     
153     
154     #Monitor stuff
155     def silent(self,s=0):
156         """Transmissions halted when 1."""
157         self.besilent=s;
158         print "besilent is %i" % self.besilent;
159         self.writecmd(0,0xB0,1,[s]);
160         
161     def out(self,byte):
162         """Write a byte to P5OUT."""
163         self.writecmd(0,0xA1,1,[byte]);
164     def dir(self,byte):
165         """Write a byte to P5DIR."""
166         self.writecmd(0,0xA0,1,[byte]);
167     def call(self,adr):
168         """Call to an address."""
169         self.writecmd(0,0x30,2,
170                       [adr&0xFF,(adr>>8)&0xFF]);
171     def execute(self,code):
172         """Execute supplied code."""
173         self.writecmd(0,0x31,2,#len(code),
174                       code);
175     def peekbyte(self,address):
176         """Read a byte of memory from the monitor."""
177         self.data=[address&0xff,address>>8];
178         self.writecmd(0,0x02,2,self.data);
179         #self.readcmd();
180         return ord(self.data[0]);
181     def peekword(self,address):
182         """Read a word of memory from the monitor."""
183         return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
184     def pokebyte(self,address,value):
185         """Set a byte of memory by the monitor."""
186         self.data=[address&0xff,address>>8,value];
187         self.writecmd(0,0x03,3,self.data);
188         return ord(self.data[0]);
189     def dumpmem(self,begin,end):
190         i=begin;
191         while i<end:
192             print "%04x %04x" % (i, self.peekword(i));
193             i+=2;
194     def monitor_ram_pattern(self):
195         """Overwrite all of RAM with 0xBEEF."""
196         self.writecmd(0,0x90,0,self.data);
197         return;
198     def monitor_ram_depth(self):
199         """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
200         self.writecmd(0,0x91,0,self.data);
201         return ord(self.data[0])+(ord(self.data[1])<<8);
202     
203     #Baud rates.
204     baudrates=[115200, 
205                9600,
206                19200,
207                38400,
208                57600,
209                115200];
210     def setBaud(self,baud):
211         """Change the baud rate.  TODO fix this."""
212         rates=self.baudrates;
213         self.data=[baud];
214         print "Changing FET baud."
215         self.serialport.write(chr(0x00));
216         self.serialport.write(chr(0x80));
217         self.serialport.write(chr(1));
218         self.serialport.write(chr(baud));
219         
220         print "Changed host baud."
221         self.serialport.setBaudrate(rates[baud]);
222         time.sleep(1);
223         self.serialport.flushInput()
224         self.serialport.flushOutput()
225         
226         print "Baud is now %i." % rates[baud];
227         return;
228     def readbyte(self):
229         return ord(self.serialport.read(1));
230     def findbaud(self):
231         for r in self.baudrates:
232             print "\nTrying %i" % r;
233             self.serialport.setBaudrate(r);
234             #time.sleep(1);
235             self.serialport.flushInput()
236             self.serialport.flushOutput()
237             
238             for i in range(1,10):
239                 self.readbyte();
240             
241             print "Read %02x %02x %02x %02x" % (
242                 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
243     def monitortest(self):
244         """Self-test several functions through the monitor."""
245         print "Performing monitor self-test.";
246         
247         if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
248             print "ERROR Fetched wrong value from 0x0c04.";
249         self.pokebyte(0x0021,0); #Drop LED
250         if self.peekbyte(0x0021)!=0:
251             print "ERROR, P1OUT not cleared.";
252         self.pokebyte(0x0021,1); #Light LED
253         
254         print "Self-test complete.";
255     
256     
257     # The following functions ought to be implemented in
258     # every client.
259
260     def infostring(self):
261         a=self.peekbyte(0xff0);
262         b=self.peekbyte(0xff1);
263         return "%02x%02x" % (a,b);
264     def lock(self):
265         print "Locking Unsupported.";
266     def erase(self):
267         print "Erasure Unsupported.";
268     def setup(self):
269         return;
270     def start(self):
271         return;
272     def test(self):
273         return;
274     def status(self):
275         return;
276     def flash(self,file):
277         """Flash an intel hex file to code memory."""
278         print "Flash not implemented.";
279     def peek32(self,address, memory="vn"):
280         return (self.peek16(address,memory)+
281                 (self.peek16(address+2,memory)<<16));
282     def peek16(self,address, memory="vn"):
283         return (self.peek8(address,memory)+
284                 (self.peek8(address+1,memory)<<8));
285     def peek8(self,address, memory="vn"):
286         return 0xde;