2 # GoodFET Client Library
4 # (C) 2009 Matthew Carpenter <matt at inguardians.com>
6 # Good luck with alpha / beta code.
7 # Contributions and bug reports welcome.
10 import sys, binascii, struct
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"%repr(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,8,[reg,0,0,0,val>>24, (val>>16)&0xff, (val>>8)&0xff, val&0xff])
161 print "DEBUG:SET_REGISTER: %s"%repr(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 self.writecmd(0x33,GET_REGISTERS,0,[])
167 print "DEBUG:GET_REGISTER: %s"%repr(self.data)
168 #retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
170 def ARMset_registers(self, regs):
171 """Get an ARM's Register"""
174 regarray.merge([reg>>24, (reg>>16)&0xff, (reg>>8)&0xff, reg&0xff])
175 self.writecmd(0x33,GET_REGISTER,16*4,regarray)
176 print "DEBUG:SET_REGISTER: %s"%repr(self.data)
177 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
179 def ARMcmd(self,phrase):
180 self.writecmd(0x33,READ,len(phrase),phrase)
181 val=ord(self.data[0])
182 print "Got %02x" % val
184 def ARMdebuginstr(self,instr):
185 if type (instr) == int:
186 instr = struct.pack("<L", instr)
187 self.writecmd(0x33,DEBUG_INSTR,len(instr),instr)
188 return (self.data[0])
189 def ARMpeekcodebyte(self,adr):
190 """Read the contents of code memory at an address."""
191 self.data=[adr&0xff, (adr&0xff00)>>8]
192 self.writecmd(0x33,PEEK,2,self.data)
193 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
195 def ARMpeekdatabyte(self,adr):
196 """Read the contents of data memory at an address."""
197 self.data=[adr&0xff, (adr&0xff00)>>8]
198 self.writecmd(0x33, PEEK, 2, self.data)
199 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
201 def ARMpokedatabyte(self,adr,val):
202 """Write a byte to data memory."""
203 self.data=[adr&0xff, (adr&0xff00)>>8, val]
204 self.writecmd(0x33, POKE, 3, self.data)
205 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
207 def ARMchiperase(self):
208 """Erase all of the target's memory."""
209 self.writecmd(0x33,CHIP_ERASE,0,[])
211 """Check the status."""
212 self.writecmd(0x33,GET_DEBUG_STATE,0,[])
213 return ord(self.data[0])
217 0x04 : "Interrupts Enabled (or not?)",
222 0x04 : "disable interrupts",
223 0x02 : "force dbgrq",
224 0x01 : "force dbgack"
227 def ARMstatusstr(self):
228 """Check the status as a string."""
229 status=self.ARMstatus()
234 str="%s %s" %(self.ARMstatusbits[i],str)
238 """Start debugging."""
239 self.writecmd(0x33,START,0,self.data)
240 #ident=self.ARMidentstr()
241 #print "Target identifies as %s." % ident
242 #print "Status: %s." % self.ARMstatusstr()
243 #self.ARMreleasecpu()
245 #print "Status: %s." % self.ARMstatusstr()
248 """Stop debugging."""
249 self.writecmd(0x33,STOP,0,self.data)
250 def ARMstep_instr(self):
251 """Step one instruction."""
252 self.writecmd(0x33,STEP_INSTR,0,self.data)
253 def ARMflashpage(self,adr):
254 """Flash 2kB a page of flash from 0xF000 in XDATA"""
259 print "Flashing buffer to 0x%06x" % adr
260 self.writecmd(0x33,MASS_FLASH_PAGE,4,data)
262 def writecmd(self, app, verb, count=0, data=[]):
263 """Write a command and some data to the GoodFET."""
264 self.serialport.write(chr(app))
265 self.serialport.write(chr(verb))
268 # count=len(data); #Initial count ignored.
270 #print "TX %02x %02x %04x" % (app,verb,count)
272 #little endian 16-bit length
273 self.serialport.write(chr(count&0xFF))
274 self.serialport.write(chr(count>>8))
276 #print "count=%02x, len(data)=%04x" % (count,len(data))
279 if(isinstance(data,list)):
280 for i in range(0,count):
281 #print "Converting %02x at %i" % (data[i],i)
285 self.serialport.write(outstr)
286 if not self.besilent: