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(0x13,HALTCPU,0,self.data)
69 def ARMreleasecpu(self):
71 self.writecmd(0x13,RESUMECPU,0,self.data)
72 def ARMsetModeArm(self):
73 self.writecmd(0x13,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 #Set and Check Registers
88 regs = [1024+x for x in range(0,15)]
90 for x in range(len(regs)):
91 self.ARMset_register(x, regs[x])
93 for x in range(len(regs)):
94 regr.append(self.ARMget_register(x))
96 for x in range(len(regs)):
97 if regs[x] != regr[x]:
98 print "Error, R%d fail: %x != %x"%(x,regs[x],regr[x])
105 #Single step, printing PC.
106 print "Tracing execution at startup."
109 byte=self.ARMpeekcodebyte(i)
110 #print "PC=%04x, %02x" % (pc, byte)
113 print "Verifying that debugging a NOP doesn't affect the PC."
114 for i in range(1,15):
116 self.ARMdebuginstr([NOP])
117 if(pc!=self.ARMgetPC()):
118 print "ERROR: PC changed during ARMdebuginstr([NOP])!"
120 print "Checking pokes to XRAM."
121 for i in range(0xf000,0xf020):
122 self.ARMpokedatabyte(i,0xde)
123 if(self.ARMpeekdatabyte(i)!=0xde):
124 print "Error in DATA at 0x%04x" % i
126 #print "Status: %s." % self.ARMstatusstr()
132 """Move the FET into the JTAG ARM application."""
133 #print "Initializing ARM."
134 self.writecmd(0x13,SETUP,0,self.data)
135 def ARMget_dbgstate(self):
136 """Read the config register of an ARM."""
137 self.writecmd(0x13,GET_DEBUG_STATE,0,self.data)
138 retval = struct.unpack("<L", self.data[:4])[0]
140 def ARMget_dbgctrl(self):
141 """Read the config register of an ARM."""
142 self.writecmd(0x13,GET_DEBUG_CTRL,0,self.data)
143 retval = struct.unpack("B", self.data)[0]
145 def ARMset_dbgctrl(self,config):
146 """Write the config register of an ARM."""
147 self.writecmd(0x13,SET_DEBUG_CTRL,1,[config&7])
148 def ARMlockchip(self):
149 """Set the flash lock bit in info mem."""
150 self.writecmd(0x13, LOCKCHIP, 0, [])
153 def ARMidentstr(self):
154 ident=self.ARMident()
156 partno = (ident >> 12) & 0x10
157 mfgid = ident & 0xfff
158 return "mfg: %x\npartno: %x\nver: %x\n(%x)" % (ver, partno, mfgid, ident);
160 """Get an ARM's ID."""
161 self.writecmd(0x13,GET_CHIP_ID,0,[])
162 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
165 """Get an ARM's PC."""
166 self.writecmd(0x13,GET_PC,0,[])
167 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
169 def ARMget_register(self, reg):
170 """Get an ARM's Register"""
171 self.writecmd(0x13,GET_REGISTER,1,[reg&0xff])
172 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
174 def ARMset_register(self, reg, val):
175 """Get an ARM's Register"""
176 self.writecmd(0x13,SET_REGISTER,8,[val&0xff, (val>>8)&0xff, (val>>16)&0xff, val>>24, reg,0,0,0])
177 #self.writecmd(0x13,SET_REGISTER,8,[reg,0,0,0, (val>>16)&0xff, val>>24, val&0xff, (val>>8)&0xff])
178 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
180 def ARMget_registers(self):
181 """Get ARM Registers"""
182 self.writecmd(0x13,GET_REGISTERS,0, [])
184 for x in range(0,len(self.data), 4):
185 retval.append(struct.unpack("<L", self.data[x:x+4])[0])
187 def ARMset_registers(self, regs):
188 """Set ARM Registers"""
191 regarry.extend([reg&0xff, (reg>>8)&0xff, (reg>>16)&0xff, reg>>24])
192 self.writecmd(0x13,SET_REGISTERS,16*4,regarry)
193 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
195 def ARMcmd(self,phrase):
196 self.writecmd(0x13,READ,len(phrase),phrase)
197 val=ord(self.data[0])
198 print "Got %02x" % val
200 def ARMdebuginstr(self,instr):
201 if type (instr) == int:
202 instr = struct.pack("<L", instr)
203 self.writecmd(0x13,DEBUG_INSTR,len(instr),instr)
204 return (self.data[0])
205 def ARMpeekcodebyte(self,adr,words):
206 """Read the contents of code memory at an address."""
207 self.data=[adr&0xff, (adr>>8)&0xff, (adr>>16)&0xff, (adr>>24)&0xff, words&0xff, (words>>8)&0xff, (words>>16)&0xff, (words>>24)&0xff ]
208 self.writecmd(0x13,PEEK,2,self.data)
210 retval.append(self.serialport.read(words*4))
211 #retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
212 return "".join(retval)
213 def ARMpeekdatabyte(self,adr):
214 """Read the contents of data memory at an address."""
215 self.data=[adr&0xff, (adr&0xff00)>>8, (adr&0xff0000)>>16, (adr&0xff000000)>>24]
216 self.writecmd(0x13, PEEK, 2, self.data)
217 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
219 def ARMpokedatabyte(self,adr,val):
220 """Write a byte to data memory."""
221 self.data=[adr&0xff, (adr&0xff00)>>8, val]
222 self.writecmd(0x13, POKE, 3, self.data)
223 retval = struct.unpack("<L", "".join(self.data[0:4]))[0]
225 def ARMchiperase(self):
226 """Erase all of the target's memory."""
227 self.writecmd(0x13,CHIP_ERASE,0,[])
229 """Check the status."""
230 self.writecmd(0x13,GET_DEBUG_STATE,0,[])
231 return ord(self.data[0])
235 0x04 : "Interrupts Enabled (or not?)",
240 0x04 : "disable interrupts",
241 0x02 : "force dbgrq",
242 0x01 : "force dbgack"
245 def ARMstatusstr(self):
246 """Check the status as a string."""
247 status=self.ARMstatus()
252 str="%s %s" %(self.ARMstatusbits[i],str)
256 """Start debugging."""
257 self.writecmd(0x13,START,0,self.data)
258 #ident=self.ARMidentstr()
259 #print "Target identifies as %s." % ident
260 #print "Status: %s." % self.ARMstatusstr()
261 #self.ARMreleasecpu()
263 #print "Status: %s." % self.ARMstatusstr()
266 """Stop debugging."""
267 self.writecmd(0x13,STOP,0,self.data)
268 def ARMstep_instr(self):
269 """Step one instruction."""
270 self.writecmd(0x13,STEP_INSTR,0,self.data)
271 def ARMflashpage(self,adr):
272 """Flash 2kB a page of flash from 0xF000 in XDATA"""
277 print "Flashing buffer to 0x%06x" % adr
278 self.writecmd(0x13,MASS_FLASH_PAGE,4,data)
280 def writecmd(self, app, verb, count=0, data=[]):
281 """Write a command and some data to the GoodFET."""
282 self.serialport.write(chr(app))
283 self.serialport.write(chr(verb))
286 # count=len(data); #Initial count ignored.
288 #print "TX %02x %02x %04x" % (app,verb,count)
290 #little endian 16-bit length
291 self.serialport.write(chr(count&0xFF))
292 self.serialport.write(chr(count>>8))
294 #print "count=%02x, len(data)=%04x" % (count,len(data))
297 if(isinstance(data,list)):
298 for i in range(0,count):
299 #print "Converting %02x at %i" % (data[i],i)
303 self.serialport.write(outstr)
304 if not self.besilent: