Trouble writing a Jim-Tcl script to write a file to flash

I’m trying to create a Tcl program to write a binary file to flash, on an AT91SAM7S-EK. I’ve modelled the program after old-style .cfg files I’ve found on the web, but the program stalls after the closing init statement, without invoking the programming script. Old-style programs would get far enough to get into trouble with flash protection – which could well be legit. My new attempt stalls for about a minute, when I kill it with a keyboard interrupt.

Can anybody provide some insight?

Thanks, Mel.

The .cfg file is:

##source mpw-daemon.cfg
telnet_port 4444
gdb_port 3333

##source mpw-parport.cfg
interface parport
parport_port 0
parport_cable wiggler
jtag_speed 0

##source mpw-sam7s256.cfg
reset_config trst_and_srst

if { [info exists CHIPNAME] } {	
   set  _CHIPNAME $CHIPNAME    
} else {	 
   set  _CHIPNAME sam7s256
}

if { [info exists ENDIAN] } {	
   set  _ENDIAN $ENDIAN    
} else {	 
   set  _ENDIAN little
}

if { [info exists CPUTAPID ] } {
   set _CPUTAPID $CPUTAPID
} else {
   set _CPUTAPID 0x3f0f0f0f
}

jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID

set _TARGETNAME [format "%s.cpu" $_CHIPNAME]

target create $_TARGETNAME arm7tdmi -endian $_ENDIAN -chain-position $_TARGETNAME -variant arm7tdmi
$_TARGETNAME configure -event reset-init { 
	# disable watchdog
	mww 0xfffffd44 0x00008000	
	# enable user reset
	mww 0xfffffd08 0xa5000001	
	# CKGR_MOR : enable the main oscillator
	mww 0xfffffc20 0x00000601	
	sleep 10
	# CKGR_PLLR: 96.1097 MHz
	mww 0xfffffc2c 0x00481c0e 	
	sleep 10
	# PMC_MCKR : MCK = PLL / 2 ~= 48 MHz
	mww 0xfffffc30 0x00000007	
	sleep 10
	# MC_FMR: flash mode (FWS=1,FMCN=60)
	mww 0xffffff60 0x003c0100	
	sleep 100
}

$_TARGETNAME configure -work-area-virt 0 -work-area-phys 0x00200000 -work-area-size 0x4000 -work-area-backup 0

#flash bank <driver> <base> <size> <chip_width> <bus_width>
flash bank at91sam7 0 0 0 0 0


echo "MPW Target: $_TARGETNAME"
set _FLASHFILE "bin/getting-started-project-at91sam7s-ek-at91sam7s256-flash.bin"

$_TARGETNAME configure -event reset-init "source mpw-sam7s_flash.script"
init
reset run

The script that doesn’t seem to get run is:

# based on information from Dominic Rath 
#

halt
sleep 10

# arm7_9 force_hw_bkpts enable	# program resides in flash

# AT91SAM7 flash command-"batch"
# adapted by Martin Thomas based on information from Dominic Rath - Thanks
arm7_9 dcc_downloads enable
sleep 10
poll
flash probe 0

flash write_bank 0 $_FLASHFILE 0x0

reset run
sleep 10
shutdown

And a trace from the aborted run is

Open On-Chip Debugger 0.1.0 (2009-05-30-16:24) Release


BUGS? Read http://svn.berlios.de/svnroot/repos/openocd/trunk/BUGS


$URL: https://kc8apf@svn.berlios.de/svnroot/repos/openocd/tags/openocd-0.1.0/src/openocd.c $
User : 5 0 command.c:494 command_run_line(): 
Debug: 6 1 configuration.c:88 find_file(): found mpw-run-flash.cfg
Debug: 8 1 command.c:91 script_command(): script_command - telnet_port
Debug: 9 1 command.c:108 script_command(): script_command - telnet_port, argv[0]=ocd_telnet_port
Debug: 10 1 command.c:108 script_command(): script_command - telnet_port, argv[1]=4444
Debug: 12 1 command.c:91 script_command(): script_command - gdb_port
Debug: 13 1 command.c:108 script_command(): script_command - gdb_port, argv[0]=ocd_gdb_port
Debug: 14 1 command.c:108 script_command(): script_command - gdb_port, argv[1]=3333
Debug: 16 1 command.c:91 script_command(): script_command - interface
Debug: 17 1 command.c:108 script_command(): script_command - interface, argv[0]=ocd_interface
Debug: 18 1 command.c:108 script_command(): script_command - interface, argv[1]=parport
Debug: 20 1 command.c:91 script_command(): script_command - parport_port
Debug: 21 1 command.c:108 script_command(): script_command - parport_port, argv[0]=ocd_parport_port
Debug: 22 1 command.c:108 script_command(): script_command - parport_port, argv[1]=0
Debug: 24 1 command.c:91 script_command(): script_command - parport_cable
Debug: 25 1 command.c:108 script_command(): script_command - parport_cable, argv[0]=ocd_parport_cable
Debug: 26 1 command.c:108 script_command(): script_command - parport_cable, argv[1]=wiggler
Debug: 28 1 command.c:91 script_command(): script_command - jtag_speed
Debug: 29 1 command.c:108 script_command(): script_command - jtag_speed, argv[0]=ocd_jtag_speed
Debug: 30 1 command.c:108 script_command(): script_command - jtag_speed, argv[1]=0
Debug: 31 1 jtag.c:2620 handle_jtag_speed_command(): handle jtag speed
User : 32 1 command.c:383 command_print(): jtag_speed: 0
Debug: 34 1 command.c:91 script_command(): script_command - reset_config
Debug: 35 1 command.c:108 script_command(): script_command - reset_config, argv[0]=ocd_reset_config
Debug: 36 1 command.c:108 script_command(): script_command - reset_config, argv[1]=trst_and_srst
Debug: 37 1 jtag.c:1892 jim_newtap_cmd(): Creating New Tap, Chip: sam7s256, Tap: cpu, Dotted: sam7s256.cpu, 8 params
Debug: 38 1 jtag.c:1911 jim_newtap_cmd(): Processing option: -irlen
Debug: 39 1 jtag.c:1911 jim_newtap_cmd(): Processing option: -ircapture
Debug: 40 1 jtag.c:1911 jim_newtap_cmd(): Processing option: -irmask
Debug: 41 2 jtag.c:1911 jim_newtap_cmd(): Processing option: -expected-id
Debug: 42 2 jtag.c:2024 jim_newtap_cmd(): Created Tap: sam7s256.cpu @ abs position 0, irlen 4, capture: 0x1 mask: 0xf
Debug: 43 2 target.c:3911 jim_target(): Target command params:
Debug: 44 2 target.c:3912 jim_target(): target create sam7s256.cpu arm7tdmi -endian little -chain-position sam7s256.cpu -variant arm7tdmi 
Debug: 46 2 command.c:91 script_command(): script_command - bank
Debug: 47 2 command.c:108 script_command(): script_command - bank, argv[0]=ocd_flash_bank
Debug: 48 2 command.c:108 script_command(): script_command - bank, argv[1]=at91sam7
Debug: 49 2 command.c:108 script_command(): script_command - bank, argv[2]=0
Debug: 50 2 command.c:108 script_command(): script_command - bank, argv[3]=0
Debug: 51 2 command.c:108 script_command(): script_command - bank, argv[4]=0
Debug: 52 2 command.c:108 script_command(): script_command - bank, argv[5]=0
Debug: 53 2 command.c:108 script_command(): script_command - bank, argv[6]=0
User : 54 2 command.c:559 jim_echo(): MPW Target: sam7s256.cpu
Debug: 56 2 command.c:91 script_command(): script_command - init
Debug: 57 2 command.c:108 script_command(): script_command - init, argv[0]=ocd_init
Debug: 58 3 openocd.c:144 handle_init_command(): target init complete
Debug: 59 3 parport.c:353 parport_init(): opening /dev/parport0...
Debug: 60 3 parport.c:365 parport_init(): ...open
Debug: 61 47 parport.c:237 parport_reset(): trst: 0, srst: 0
Debug: 62 48 openocd.c:151 handle_init_command(): jtag interface init complete
Debug: 63 48 jtag.c:2240 jtag_init_inner(): Init JTAG chain
Debug: 64 48 jtag.c:434 jtag_call_event_callbacks(): jtag event: JTAG controller reset (RESET or TRST)
Debug: 65 48 jtag.c:1487 jtag_reset_callback(): -
Debug: 66 48 jtag.c:434 jtag_call_event_callbacks(): jtag event: JTAG controller reset (RESET or TRST)
Debug: 67 48 jtag.c:1487 jtag_reset_callback(): -
Info : 68 51 jtag.c:1607 jtag_examine_chain(): JTAG tap: sam7s256.cpu tap/device found: 0x3f0f0f0f (Manufacturer: 0x787, Part: 0xf0f0, Version: 0x3)
Info : 69 51 jtag.c:1645 jtag_examine_chain(): JTAG Tap/device matched
Debug: 70 51 jtag.c:434 jtag_call_event_callbacks(): jtag event: JTAG controller reset (RESET or TRST)
Debug: 71 51 jtag.c:1487 jtag_reset_callback(): -
Debug: 72 51 openocd.c:157 handle_init_command(): jtag init complete
Debug: 73 52 embeddedice.c:401 embeddedice_write_reg(): 12: 0x00000000
Debug: 74 52 embeddedice.c:401 embeddedice_write_reg(): 20: 0x00000000
Debug: 75 52 openocd.c:160 handle_init_command(): jtag examine complete
Debug: 76 53 openocd.c:166 handle_init_command(): flash init complete
Debug: 77 53 openocd.c:170 handle_init_command(): mflash init complete
Debug: 78 53 openocd.c:174 handle_init_command(): NAND init complete
Debug: 79 53 openocd.c:178 handle_init_command(): pld init complete
Debug: 80 53 gdb_server.c:2225 gdb_init(): gdb service for target arm7tdmi at port 3333
Warn : 81 53 tcl_server.c:178 tcl_init(): no tcl port specified, using default port 6666
Debug: 83 54 command.c:91 script_command(): script_command - reset
Debug: 84 54 command.c:108 script_command(): script_command - reset, argv[0]=ocd_reset
Debug: 85 54 command.c:108 script_command(): script_command - reset, argv[1]=run
Debug: 86 54 target.c:3911 jim_target(): Target command params:
Debug: 87 54 target.c:3912 jim_target(): target names

It seems the fundamental problem is in having two “configure -event reset-init” statements at the same time, or perhaps for the same device. Moving the “source mpw-sam7s_flash.script” statment inside the block of the first -event statement got the script executed.

There’s no documented way of passing arguments through a source statement, so better just include those commands in the original cfg file.

The new command file, which is now running into trouble with memory protection, as expected is

##source mpw-daemon.cfg
telnet_port 4444
gdb_port 3333

##source mpw-parport.cfg
interface parport
parport_port 0
parport_cable wiggler
jtag_speed 0

proc program_flash {flashname} {
	# based on information from Dominic Rath 
	#

	halt
	sleep 10

	# arm7_9 force_hw_bkpts enable	# program resides in flash

	# AT91SAM7 flash command-"batch"
	# adapted by Martin Thomas based on information from Dominic Rath - Thanks
	arm7_9 dcc_downloads enable
	sleep 10
	poll
	flash probe 0

	flash write_bank 0 $flashname 0x0

	reset run
	sleep 10
	shutdown
}

##source mpw-sam7s256.cfg
reset_config trst_and_srst

if { [info exists CHIPNAME] } {	
   set  _CHIPNAME $CHIPNAME    
} else {	 
   set  _CHIPNAME sam7s256
}

if { [info exists ENDIAN] } {	
   set  _ENDIAN $ENDIAN    
} else {	 
   set  _ENDIAN little
}

if { [info exists CPUTAPID ] } {
   set _CPUTAPID $CPUTAPID
} else {
   set _CPUTAPID 0x3f0f0f0f
}

jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID

set _TARGETNAME [format "%s.cpu" $_CHIPNAME]

target create $_TARGETNAME arm7tdmi -endian $_ENDIAN -chain-position $_TARGETNAME -variant arm7tdmi
$_TARGETNAME configure -event reset-init { 
	# disable watchdog
	mww 0xfffffd44 0x00008000	
	# enable user reset
	mww 0xfffffd08 0xa5000001	
	# CKGR_MOR : enable the main oscillator
	mww 0xfffffc20 0x00000601	
	sleep 10
	# CKGR_PLLR: 96.1097 MHz
	mww 0xfffffc2c 0x00481c0e 	
	sleep 10
	# PMC_MCKR : MCK = PLL / 2 ~= 48 MHz
	mww 0xfffffc30 0x00000007	
	sleep 10
	# MC_FMR: flash mode (FWS=1,FMCN=60)
	mww 0xffffff60 0x003c0100	
	sleep 100
	
	#source mpw-sam7s_flash.script
	program_flash "bin/getting-started-project-at91sam7s-ek-at91sam7s256-flash.bin"
}

$_TARGETNAME configure -work-area-virt 0 -work-area-phys 0x00200000 -work-area-size 0x4000 -work-area-backup 0

#flash bank <driver> <base> <size> <chip_width> <bus_width>
flash bank at91sam7 0 0 0 0 0


echo "MPW Target: $_TARGETNAME"
set _FLASHFILE "bin/getting-started-project-at91sam7s-ek-at91sam7s256-flash.bin"

##$_TARGETNAME configure -event reset-init "source mpw-sam7s_flash.script"
init
##reset run
reset init

This is something I should have seen from the schematics. Sure, my JTAG adapter supports nTRST, but the AT91SAM7S-EK board doesn’t handle it. The proper reset_config should have

reset_config srst_only

Running an AT91SAM7S256 on a product seems to work with

reset_config srst_only srst_pulls_trst