+
+
+######### command line stuff #########
+
+from GoodFETARM7 import *
+
+def at91x40_main():
+ ''' this function should be called from command line app '''
+ #Initialize FET and set baud rate
+ client=GoodFETAT91X40()
+ client.serInit()
+
+ client.setup()
+ client.start()
+
+ at91x40_cli_handler(client, sys.argv)
+
+def at91x40_cli_handler(client, argv):
+
+ if(argv[1]=="chipRegStr"):
+ client.halt()
+ print "#####"
+ print client.getChipSelectRegstr(int(argv[2]))
+ print "#####"
+ client.resume()
+
+ if(argv[1]=="chipRegList"):
+ client.halt()
+ print "#####"
+ print client.getChipSelectReglist(int(argv[2]))
+ print "#####"
+ client.resume()
+
+ if(argv[1]=="chipRegValue"):
+ client.halt()
+ print "#####"
+ print "Chip Register Value:",hex(client.getChipSelectReg(int(argv[2])))
+ print "#####"
+
+ if(argv[1]=="ecdump"):
+ f = argv[2]
+ start=0x00000000
+ stop=0xFFFFFFFF
+ if(len(argv)>3):
+ start=int(argv[3],16)
+ if(len(argv)>4):
+ stop=int(argv[4],16)
+
+ ##############################3
+ # Error checking requires a special register
+ # Should an error occur while reading from the chip's memory
+ # These values will help to test if the chip is working and
+ # the memory is mapped properly before continuing
+ # Use the chipRegStr verb to determine the value of the
+ # Special Register when the chip is operating normally
+ # Example: ./goodfet.at91x40 chipRegValue 1
+ ##############################3
+ # user$ ./goodfet.at91x40 chipRegValue 1
+ # Identifying Target:
+ # # DEBUG 0x120
+ # Chip IDCODE: 0x1f0f0f0f
+ # ver: 1
+ # partno: f0f0
+ # mfgid: 787
+ #
+ # Debug Status: Interrupts Enabled (or not?)
+ #
+ # #####
+ # Chip Register Value: 0x10000000
+ # #####
+ ##############################
+ special_reg_num=1
+ special_addr=0x010000000
+ if(len(argv)>5):
+ # Yes, this requires that you set the start and stop addresses
+ special_reg_num=int(argv[5])
+ special_addr=int(argv[6],16)
+ err_list = []
+
+ print "Dumping from %04x to %04x as %s." % (start,stop,f)
+ # FIXME: get mcu state and return it to that state
+ client.halt()
+
+ h = IntelHex(None)
+ i=start
+ err_cnt = 0
+ reset_cnt = 0
+ while i<=stop:
+ try:
+ data=client.ARMreadChunk(i, 48, verbose=0)
+ print "Dumped %06x."%i
+ for dword in data:
+ if i<=stop and dword != 0xdeadbeef:
+ h.puts( i, struct.pack("<I", dword) )
+ err_cnt = 0
+ i+=4
+ # FIXME: get mcu state and return it to that state
+ except:
+ # Handle exceptions by counting errors after pausing to let ARM settle
+ err_cnt = 1
+ fail = 0
+ while err_cnt:
+ time.sleep(.25)
+ if err_cnt == 100:
+ print "Unknown error occurred at least 100 times. Resync did not work. Writing incomplete data to file."
+ fail = 1
+ break
+ else:
+ try:
+ print "Unknown error during read. Resync and retry."
+ err_list.append("0x%06x"%i)
+
+ # If we error out several times then reset the chip and restart
+ # This uses a special register value from a Chip Select Register
+ # to test that the chip is in the operation state we expect
+ if not ((err_cnt+1) % 2):
+ while True:
+ print " Reset:",reset_cnt
+ check_addr = client.getChipSelectReg(special_reg_num)
+ print " Special Addr:",hex(special_addr)
+ print " Check Addr:",hex(check_addr)
+ if (special_addr == check_addr):
+ break
+ if reset_cnt == 10:
+ reset_cnt = 0
+ print " Resetting Target"
+ client.ARMresettarget(1000)
+ client.halt()
+ reset_cnt += 1
+ time.sleep(.25)
+ else:
+ #Resync ARM and Watch Dog
+ client.resume()
+ client.halt()
+
+ err_cnt = 0
+ except:
+ err_cnt += 1
+ pass
+ if fail:
+ break
+
+ client.resume()
+ h.write_hex_file(f)
+ print "Addresses that required resync:"
+ if err_list:
+ for e in err_list:
+ print " ",e
+ else:
+ print " None"
+
+
+
+ if(argv[1]=="memorymap"):
+ client.halt()
+ print "=============================================="
+ print client.getEBIMCRstr()
+ print ""
+ print client.getEBIMemoryMapstr()
+ client.resume()
+
+ if(argv[1]=="memorycontrolreg"):
+ client.halt()
+ print client.getEBIMCRstr()
+ client.resume()
+
+
+
+ if(argv[1]=="stat_watchdog"):
+ client.halt()
+ print "Watch Dog Status:"
+ print "--"
+ client.statWatchDog()
+ client.resume()
+
+ if(argv[1]=="test_disable_watchdog"):
+ client.halt()
+ print "Status Watch Dog:"
+ client.statWatchDog()
+ print "--"
+ print "Disabling Watchdog Timer:"
+ client.disableWatchDog()
+ time.sleep(2) # pause to settle
+ print "\nChecking:"
+ client.statWatchDog()
+ print "--"
+ print "Done. Resume may re-enable Watch Dog."
+ client.resume()
+
+ # anything we didn't provide from arm7:
+ arm7_cli_handler(client, argv)
+
+ #client.ARMreleasecpu()
+ #client.ARMstop()
+
+
+if __name__ == "__main__":
+ if(len(sys.argv)==1):
+ at91x40_syntax()
+
+ else:
+ at91x40_main()
+
+