2 # GoodFET Client Library
5 # Good luck with alpha / beta code.
6 # Contributions and bug reports welcome.
9 import sys, binascii, struct
10 import atlasutils.smartprint as asp
25 # ARM7TDMI JTAG commands
31 GET_DEBUG_STATE = 0x85
43 READ_CODE_MEMORY = 0x91 # ??
44 WRITE_FLASH_PAGE = 0x92 # ??
45 READ_FLASH_PAGE = 0x93 # ??
46 MASS_ERASE_FLASH = 0x94 # ??
49 CHIP_ERASE = 0x97 # can do?
50 # Really ARM specific stuff
58 from GoodFET import GoodFET
59 from intelhex import IntelHex
64 class GoodFETARM(GoodFET):
65 """A GoodFET variant for use with ARM7TDMI microprocessor."""
68 self.writecmd(0x33,HALTCPU,0,self.data)
69 def ARMreleasecpu(self):
71 self.writecmd(0x33,RESUMECPU,0,self.data)
72 def ARMsetModeArm(self):
73 self.writecmd(0x33,SET_MODE_ARM,0,self.data)
77 #print "Status: %s" % self.ARMstatusstr()
79 #Grab ident three times, should be equal.
80 ident1=self.ARMident()
81 ident2=self.ARMident()
82 ident3=self.ARMident()
83 if(ident1!=ident2 or ident2!=ident3):
84 print "Error, repeated ident attempts unequal."
85 print "%04x, %04x, %04x" % (ident1, ident2, ident3)
87 #Single step, printing PC.
88 print "Tracing execution at startup."
91 byte=self.ARMpeekcodebyte(i)
92 #print "PC=%04x, %02x" % (pc, byte)
95 print "Verifying that debugging a NOP doesn't affect the PC."
98 self.ARMdebuginstr([NOP])
99 if(pc!=self.ARMgetPC()):
100 print "ERROR: PC changed during ARMdebuginstr([NOP])!"
102 print "Checking pokes to XRAM."
103 for i in range(0xf000,0xf020):
104 self.ARMpokedatabyte(i,0xde)
105 if(self.ARMpeekdatabyte(i)!=0xde):
106 print "Error in DATA at 0x%04x" % i
108 #print "Status: %s." % self.ARMstatusstr()
114 """Move the FET into the JTAG ARM application."""
115 #print "Initializing ARM."
116 self.writecmd(0x33,SETUP,0,self.data)
117 def ARMget_dbgstate(self):
118 """Read the config register of an ARM."""
119 self.writecmd(0x33,GET_DEBUG_STATE,0,self.data)
120 print "DEBUGGING get_dbgstate: %s"%repr(self.data)
121 retval = struct.unpack("<L", self.data[:4])[0]
123 def ARMget_dbgctrl(self):
124 """Read the config register of an ARM."""
125 self.writecmd(0x33,GET_DEBUG_CTRL,0,self.data)
126 retval = struct.unpack("B", self.data)[0]
128 def ARMset_dbgctrl(self,config):
129 """Write the config register of an ARM."""
130 self.writecmd(0x33,SET_DEBUG_CTRL,1,[config&7])
131 def ARMlockchip(self):
132 """Set the flash lock bit in info mem."""
133 self.writecmd(0x33, LOCKCHIP, 0, [])
136 def ARMidentstr(self):
137 ident=self.ARMident()
139 partno = (ident >> 12) & 0x10
140 mfgid = ident & 0xfff
141 return "mfg: %x\npartno: %x\nver: %x\n(%x)" % (ver, partno, mfgid, ident);
143 """Get an ARM's ID."""
144 self.writecmd(0x33,GET_CHIP_ID,0,[])
145 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
148 """Get an ARM's PC."""
149 self.writecmd(0x33,GET_PC,0,[])
150 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
152 def ARMget_register(self, reg):
153 """Get an ARM's Register"""
154 self.writecmd(0x33,GET_REGISTER,1,[reg&0xff])
155 print "DEBUG:GET_REGISTER: %s"%asp.hexText(self.data)
156 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
158 def ARMset_register(self, reg, val):
159 """Get an ARM's Register"""
160 self.writecmd(0x33,GET_REGISTER,20,[reg,0,0,0,val>>24, (val>>16)&0xff, (val>>8)&0xff, val&0xff,9,8,7,6,5,4,3,2,1,0,2,3])
161 print "DEBUG:SET_REGISTER: %s"%asp.hexText(self.data)
162 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
164 def ARMget_registers(self):
165 """Get an ARM's Register"""
166 clear = [x for x in range(20)]
167 self.writecmd(0x33,GET_REGISTERS,20,clear)
168 print "DEBUG:GET_REGISTER: %s"%asp.hexText(self.data)
170 for x in range(0,len(self.data), 4):
171 retval.append(struct.unpack("<L", self.data[x:x+4])[0])
172 #retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
174 def ARMset_registers(self, regs):
175 """Get an ARM's Register"""
178 regarry.extend([reg>>24, (reg>>16)&0xff, (reg>>8)&0xff, reg&0xff])
179 self.writecmd(0x33,GET_REGISTER,16*4,regarry)
180 print "DEBUG:SET_REGISTER: %s"%asp.hexText(self.data)
181 #retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
183 def ARMcmd(self,phrase):
184 self.writecmd(0x33,READ,len(phrase),phrase)
185 val=ord(self.data[0])
186 print "Got %02x" % val
188 def ARMdebuginstr(self,instr):
189 if type (instr) == int:
190 instr = struct.pack("<L", instr)
191 self.writecmd(0x33,DEBUG_INSTR,len(instr),instr)
192 return (self.data[0])
193 def ARMpeekcodebyte(self,adr):
194 """Read the contents of code memory at an address."""
195 self.data=[adr&0xff, (adr&0xff00)>>8]
196 self.writecmd(0x33,PEEK,2,self.data)
197 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
199 def ARMpeekdatabyte(self,adr):
200 """Read the contents of data memory at an address."""
201 self.data=[adr&0xff, (adr&0xff00)>>8]
202 self.writecmd(0x33, PEEK, 2, self.data)
203 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
205 def ARMpokedatabyte(self,adr,val):
206 """Write a byte to data memory."""
207 self.data=[adr&0xff, (adr&0xff00)>>8, val]
208 self.writecmd(0x33, POKE, 3, self.data)
209 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
211 def ARMchiperase(self):
212 """Erase all of the target's memory."""
213 self.writecmd(0x33,CHIP_ERASE,0,[])
215 """Check the status."""
216 self.writecmd(0x33,GET_DEBUG_STATE,0,[])
217 return ord(self.data[0])
221 0x04 : "Interrupts Enabled (or not?)",
226 0x04 : "disable interrupts",
227 0x02 : "force dbgrq",
228 0x01 : "force dbgack"
231 def ARMstatusstr(self):
232 """Check the status as a string."""
233 status=self.ARMstatus()
238 str="%s %s" %(self.ARMstatusbits[i],str)
242 """Start debugging."""
243 self.writecmd(0x33,START,0,self.data)
244 #ident=self.ARMidentstr()
245 #print "Target identifies as %s." % ident
246 #print "Status: %s." % self.ARMstatusstr()
247 #self.ARMreleasecpu()
249 #print "Status: %s." % self.ARMstatusstr()
252 """Stop debugging."""
253 self.writecmd(0x33,STOP,0,self.data)
254 def ARMstep_instr(self):
255 """Step one instruction."""
256 self.writecmd(0x33,STEP_INSTR,0,self.data)
257 def ARMflashpage(self,adr):
258 """Flash 2kB a page of flash from 0xF000 in XDATA"""
263 print "Flashing buffer to 0x%06x" % adr
264 self.writecmd(0x33,MASS_FLASH_PAGE,4,data)
266 def writecmd(self, app, verb, count=0, data=[]):
267 """Write a command and some data to the GoodFET."""
268 self.serialport.write(chr(app))
269 self.serialport.write(chr(verb))
272 # count=len(data); #Initial count ignored.
274 #print "TX %02x %02x %04x" % (app,verb,count)
276 #little endian 16-bit length
277 self.serialport.write(chr(count&0xFF))
278 self.serialport.write(chr(count>>8))
280 #print "count=%02x, len(data)=%04x" % (count,len(data))
283 if(isinstance(data,list)):
284 for i in range(0,count):
285 #print "Converting %02x at %i" % (data[i],i)
289 self.serialport.write(outstr)
290 if not self.besilent: