startup.tcl 8.17 KB
Newer Older
1
2
3
4
5
6
7
#
# Defines basic Tcl procs that must be there for
# OpenOCD to work.
#
# Embedded into OpenOCD executable
#

oharboe's avatar
oharboe committed
8
9
10
11
12
13
14
15
16
17
18

# Help text list. A list of command + help text pairs.
#
# Commands can be more than one word and they are stored
# as "flash banks" "help text x x x"

proc add_help_text {cmd cmd_help} {
	global ocd_helptext
	lappend ocd_helptext [list $cmd $cmd_help]
}

oharboe's avatar
oharboe committed
19
20
21
22
23
proc get_help_text {} {
	global ocd_helptext
	return $ocd_helptext
}

24
25
26

# Show flash in human readable form
# This is an example of a human readable form of a low level fn
27
proc flash_banks {} { 
28
29
	set i 0 	
	set result ""
30
	foreach {a} [ocd_flash_banks] {
31
32
33
		if {$i > 0} {
			set result "$result\n"
		}
34
		set result [format "$result#%d: %s at 0x%08x, size 0x%08x, buswidth %d, chipwidth %d" $i $a(name) $a(base) $a(size) $a(bus_width) $a(chip_width)]
35
36
37
38
39
40
41
42
		set i [expr $i+1]	
	}	
	return $result
}

# We need to explicitly redirect this to the OpenOCD command
# as Tcl defines the exit proc
proc exit {} {
43
	ocd_throw exit
44
45
}

oharboe's avatar
oharboe committed
46
47
#Print help text for a command. Word wrap
#help text that is too wide inside column.
48
proc help {args} {
oharboe's avatar
oharboe committed
49
50
51
	global ocd_helptext
	set cmd $args
	foreach a [lsort $ocd_helptext] {
oharboe's avatar
oharboe committed
52
		if {[string length $cmd]==0||[string first $cmd $a]!=-1||[string first $cmd [lindex $a 1]]!=-1} {
53
			set w 50
oharboe's avatar
oharboe committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
			set cmdname [lindex $a 0]
			set h [lindex $a 1]
			set n 0
			while 1 {
				if {$n > [string length $h]} {break}
				
				set next_a [expr $n+$w]
				if {[string length $h]>$n+$w} {
					set xxxx [string range $h $n [expr $n+$w]]
					for {set lastpos [expr [string length $xxxx]-1]} {$lastpos>=0&&[string compare [string range $xxxx $lastpos $lastpos] " "]!=0} {set lastpos [expr $lastpos-1]} {
					}
					#set next_a -1
					if {$lastpos!=-1} {
						set next_a [expr $lastpos+$n+1]
					}
				}
				
				
				puts [format "%-25s %s" $cmdname [string range $h $n [expr $next_a-1]] ]
				set cmdname ""
				set n [expr $next_a]
			}
oharboe's avatar
oharboe committed
76
77
78
79
		}
	}
}

80
add_help_text help "Tcl implementation of help command"
oharboe's avatar
oharboe committed
81
82


83
# If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
84
85
86
#
# We also support two level commands. "flash banks" is translated to
# flash_banks
87
proc unknown {args} {
88
89
90
	# do the name mangling from "flash banks" to "flash_banks"
	if {[llength $args]>=2} {
		set cmd_name "[lindex $args 0]_[lindex $args 1]"
91
92
93
94
		if {[catch {info body $cmd_name}]==0} {
		    # the command exists, try it...
			return [eval "$cmd_name [lrange $args 2 end]"]
		}
95
	}
96
	# This really is an unknown command.
oharboe's avatar
oharboe committed
97
	return -code error "Unknown command: $args"
98
}
99

100
proc new_target_name { } {
101
	return [target number [expr [target count] - 1 ]]
102
103
}

104
105
106
# Try flipping / and \ to find file if the filename does not
# match the precise spelling
proc find {filename} {
107
	if {[catch {ocd_find $filename} t]==0} {
108
		return $t
109
	}
110
	if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
111
		return $t
112
	}
113
	if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
114
		return $t
115
	}
116
	# make sure error message matches original input string
117
	return -code error "Can't find $filename"
118
119
120
121
122
123
124
125
126
127
}
add_help_text find "<file> - print full path to file according to OpenOCD search rules"

# Run script
proc script {filename} {
	source [find $filename]
}

add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"

oharboe's avatar
oharboe committed
128
129
130
131
132
133
# Handle GDB 'R' packet. Can be overriden by configuration script,
# but it's not something one would expect target scripts to do
# normally
proc ocd_gdb_restart {target_num} {
	# Fix!!! we're resetting all targets here! Really we should reset only
	# one target
134
	reset halt
oharboe's avatar
oharboe committed
135
136
}

137
138
proc ocd_process_reset { MODE } {

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	# If this target must be halted...
	set halt -1
	if { 0 == [string compare $MODE halt] } {
		set halt 1
	}
	if { 0 == [string compare $MODE init] } {
		set halt 1;
	}
	if { 0 == [string compare $MODE run ] } {
		set halt 0;
	}
	if { $halt < 0 } {
		return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
	}

zwelch's avatar
zwelch committed
154
155
156
157
158
	# Target event handlers *might* change which TAPs are enabled
	# or disabled, so we fire all of them.  But don't issue any
	# of the "arp_*" commands, which may issue JTAG transactions,
	# unless we know the underlying TAP is active.

159
	foreach t [ target names ] {
160
161
		# New event script.
		$t invoke-event reset-start
162
	}
163

164
165
	# Init the tap controller.
	jtag arp_init-reset
166

zwelch's avatar
zwelch committed
167
	# Examine all targets on enabled taps.
168
	foreach t [ target names ] {
zwelch's avatar
zwelch committed
169
170
171
		if {[jtag tapisenabled [$t cget -chain-position]]} {
			$t arp_examine
		}
172
	}
173

174
175
	# Let the C code know we are asserting reset.
	foreach t [ target names ] {
176
177
		$t invoke-event reset-assert-pre
		# C code needs to know if we expect to 'halt'
zwelch's avatar
zwelch committed
178
179
180
		if {[jtag tapisenabled [$t cget -chain-position]]} {
			$t arp_reset assert $halt
		}
181
		$t invoke-event reset-assert-post
182
	}
183

184
185
	# Now de-assert reset.
	foreach t [ target names ] {
186
187
		$t invoke-event reset-deassert-pre
		# Again, de-assert code needs to know..
zwelch's avatar
zwelch committed
188
189
190
		if {[jtag tapisenabled [$t cget -chain-position]]} {
			$t arp_reset deassert $halt
		}
191
		$t invoke-event reset-deassert-post
192
	}
193

194
195
	# Pass 1 - Now try to halt.
	if { $halt } {
196
		foreach t [target names] {
zwelch's avatar
zwelch committed
197
198
199
200
			if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
				continue
			}

201
202
203
204
205
206
207
208
209
210
211
212
			# Wait upto 1 second for target to halt.  Why 1sec? Cause
			# the JTAG tap reset signal might be hooked to a slow
			# resistor/capacitor circuit - and it might take a while
			# to charge
			
			# Catch, but ignore any errors.
			catch { $t arp_waitstate halted 1000 }
			
			# Did we succeed?
			set s [$t curstate]
			
			if { 0 != [string compare $s "halted" ] } {
213
				return -error [format "TARGET: %s - Not halted" $t]
214
			}
215
		}
216
	}
217

218
219
	#Pass 2 - if needed "init"
	if { 0 == [string compare init $MODE] } {
220
		foreach t [target names] {
zwelch's avatar
zwelch committed
221
222
223
224
			if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
				continue
			}

225
226
227
			set err [catch "$t arp_waitstate halted 5000"]
			# Did it halt?
			if { $err == 0 } {
228
				$t invoke-event reset-init		
229
			}
230
		}
231
	}
232

233
	foreach t [ target names ] {
234
		$t invoke-event reset-end
235
	}
236
}
oharboe's avatar
oharboe committed
237
238
239
240
241

# stubs for targets scripts that do not have production procedure
proc production_info {} {
	return "Imagine an explanation here..."
}
oharboe's avatar
oharboe committed
242
add_help_text production_info "Displays information on production procedure for target script. Implement this procedure in target script."
oharboe's avatar
oharboe committed
243
244
245

proc production {firmwarefile serialnumber} {
	puts "Imagine production procedure running successfully. Programmed $firmwarefile with serial number $serialnumber"
246
}
oharboe's avatar
oharboe committed
247

oharboe's avatar
oharboe committed
248
add_help_text production "<serialnumber> - Runs production procedure. Throws exception if procedure failed. Prints progress messages. Implement this procedure in the target script."
oharboe's avatar
oharboe committed
249
250
251
252

proc production_test {} {
	puts "Imagine nifty test procedure having run to completion here."
}
oharboe's avatar
oharboe committed
253
add_help_text production "Runs test procedure. Throws exception if procedure failed. Prints progress messages. Implement in target script."
oharboe's avatar
oharboe committed
254

oharboe's avatar
oharboe committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
add_help_text cpu "<name> - prints out target options and a comment on CPU which matches name"

# A list of names of CPU and options required
set ocd_cpu_list {
	{
		name IXP42x 
		options {xscale -variant IXP42x} 
		comment {IXP42x cpu}
	}
	{
		name arm7 
		options {arm7tdmi -variant arm7tdmi} 
		comment {vanilla ARM7}
	}
}

# Invoked from Tcl code
proc ocd_cpu {args} {
	set name $args
	set result ""
	global ocd_cpu_list
	foreach a [lsort $ocd_cpu_list] {
		if {[string length $args]==0||[string first [string toupper $name] [string toupper "$a(name)$a(options)$a(comment)"]]!=-1} {
			lappend result $a 
		}
	}
	return $result
}

proc cpu {args} {
    #     0123456789012345678901234567890123456789012345678901234567890123456789
	puts "CPU                 Options                                 Comment"
	foreach a [lsort [ocd_cpu $args]] {
		puts [format "%-20s%-40s%s" $a(name) $a(options) $a(comment)]
	}
}

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
proc power_restore {} {
	puts "Sensed power restore."
	reset init
}

add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."

proc power_dropout {} {
	puts "Sensed power dropout."
}

proc srst_deasserted {} {
	puts "Sensed nSRST deasserted."
	reset init
}
add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."

proc srst_asserted {} {
	puts "Sensed nSRST asserted."
}
oharboe's avatar
oharboe committed
312
313
314
315
316
317
318
319

# catch any exceptions, capture output and return output
proc capture_catch {a} {
	catch {
		capture {uplevel $a}
	} result
	return $result 
}