Introduced jtag_init and "jtag arp_init" to allow target scripts more control over...
[openocd.git] / src / helper / startup.tcl
1 #
2 # Defines basic Tcl procs that must be there for
3 # OpenOCD to work.
4 #
5 # Embedded into OpenOCD executable
6 #
7
8
9 # Help text list. A list of command + help text pairs.
10 #
11 # Commands can be more than one word and they are stored
12 # as "flash banks" "help text x x x"
13
14 proc add_help_text {cmd cmd_help} {
15 global ocd_helptext
16 lappend ocd_helptext [list $cmd $cmd_help]
17 }
18
19 proc get_help_text {} {
20 global ocd_helptext
21 return $ocd_helptext
22 }
23
24
25 # Show flash in human readable form
26 # This is an example of a human readable form of a low level fn
27 proc flash_banks {} {
28 set i 0
29 set result ""
30 foreach {a} [ocd_flash_banks] {
31 if {$i > 0} {
32 set result "$result\n"
33 }
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 set i [expr $i+1]
36 }
37 return $result
38 }
39
40 # We need to explicitly redirect this to the OpenOCD command
41 # as Tcl defines the exit proc
42 proc exit {} {
43 ocd_throw exit
44 }
45
46 #Print help text for a command. Word wrap
47 #help text that is too wide inside column.
48 proc help {args} {
49 global ocd_helptext
50 set cmd $args
51 foreach a [lsort $ocd_helptext] {
52 if {[string length $cmd]==0||[string first $cmd $a]!=-1||[string first $cmd [lindex $a 1]]!=-1} {
53 set w 50
54 set cmdname [lindex $a 0]
55 set h [lindex $a 1]
56 set n 0
57 while 1 {
58 if {$n > [string length $h]} {break}
59
60 set next_a [expr $n+$w]
61 if {[string length $h]>$n+$w} {
62 set xxxx [string range $h $n [expr $n+$w]]
63 for {set lastpos [expr [string length $xxxx]-1]} {$lastpos>=0&&[string compare [string range $xxxx $lastpos $lastpos] " "]!=0} {set lastpos [expr $lastpos-1]} {
64 }
65 #set next_a -1
66 if {$lastpos!=-1} {
67 set next_a [expr $lastpos+$n+1]
68 }
69 }
70
71
72 puts [format "%-25s %s" $cmdname [string range $h $n [expr $next_a-1]] ]
73 set cmdname ""
74 set n [expr $next_a]
75 }
76 }
77 }
78 }
79
80 add_help_text help "Tcl implementation of help command"
81
82
83 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
84 #
85 # We also support two level commands. "flash banks" is translated to
86 # flash_banks
87 proc unknown {args} {
88 # do the name mangling from "flash banks" to "flash_banks"
89 if {[llength $args]>=2} {
90 set cmd_name "[lindex $args 0]_[lindex $args 1]"
91 if {[catch {info body $cmd_name}]==0} {
92 # the command exists, try it...
93 return [eval "$cmd_name [lrange $args 2 end]"]
94 }
95 }
96 # This really is an unknown command.
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 # Try flipping / and \ to find file if the filename does not
105 # match the precise spelling
106 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 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
120
121 # Run script
122 proc script {filename} {
123 source [find $filename]
124 }
125
126 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
127
128 # Handle GDB 'R' packet. Can be overriden by configuration script,
129 # but it's not something one would expect target scripts to do
130 # normally
131 proc ocd_gdb_restart {target_id} {
132 # Fix!!! we're resetting all targets here! Really we should reset only
133 # one target
134 reset halt
135 }
136
137 global in_process_reset
138 set in_process_reset 0
139
140 # Catch reset recursion
141 proc ocd_process_reset { MODE } {
142 global in_process_reset
143 if {$in_process_reset} {
144 set in_process_reset 0
145 return -code error "'reset' can not be invoked recursively"
146 }
147
148 set in_process_reset 1
149 set success [expr [catch {ocd_process_reset_inner $MODE} result]==0]
150 set in_process_reset 0
151
152 if {$success} {
153 return $result
154 } else {
155 return -code error $result
156 }
157 }
158
159 proc ocd_process_reset_inner { MODE } {
160 set targets [target names]
161
162 # If this target must be halted...
163 set halt -1
164 if { 0 == [string compare $MODE halt] } {
165 set halt 1
166 }
167 if { 0 == [string compare $MODE init] } {
168 set halt 1;
169 }
170 if { 0 == [string compare $MODE run ] } {
171 set halt 0;
172 }
173 if { $halt < 0 } {
174 return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
175 }
176
177 # Target event handlers *might* change which TAPs are enabled
178 # or disabled, so we fire all of them. But don't issue any
179 # target "arp_*" commands, which may issue JTAG transactions,
180 # unless we know the underlying TAP is active.
181 #
182 # NOTE: ARP == "Advanced Reset Process" ... "advanced" is
183 # relative to a previous restrictive scheme
184
185 foreach t $targets {
186 # New event script.
187 $t invoke-event reset-start
188 }
189
190 # Use TRST or TMS/TCK operations to reset all the tap controllers.
191 # TAP reset events get reported; they might enable some taps.
192 #
193 # REVISIT arp_init-reset pulses SRST (if it can) with TRST active;
194 # but SRST events aren't reported (unlike "jtag arp_reset", below)
195 jtag arp_init-reset
196
197 # Examine all targets on enabled taps.
198 foreach t $targets {
199 if {[jtag tapisenabled [$t cget -chain-position]]} {
200 $t arp_examine
201 }
202 }
203
204 # Assert SRST, and report the pre/post events.
205 # Note: no target sees SRST before "pre" or after "post".
206 foreach t $targets {
207 $t invoke-event reset-assert-pre
208 }
209 foreach t $targets {
210 # C code needs to know if we expect to 'halt'
211 if {[jtag tapisenabled [$t cget -chain-position]]} {
212 $t arp_reset assert $halt
213 }
214 }
215 foreach t $targets {
216 $t invoke-event reset-assert-post
217 }
218
219 # Now de-assert SRST, and report the pre/post events.
220 # Note: no target sees !SRST before "pre" or after "post".
221 foreach t $targets {
222 $t invoke-event reset-deassert-pre
223 }
224 foreach t $targets {
225 # Again, de-assert code needs to know if we 'halt'
226 if {[jtag tapisenabled [$t cget -chain-position]]} {
227 $t arp_reset deassert $halt
228 }
229 }
230 foreach t $targets {
231 $t invoke-event reset-deassert-post
232 }
233
234 # Pass 1 - Now wait for any halt (requested as part of reset
235 # assert/deassert) to happen. Ideally it takes effect without
236 # first executing any instructions.
237 if { $halt } {
238 foreach t $targets {
239 if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
240 continue
241 }
242
243 # Wait upto 1 second for target to halt. Why 1sec? Cause
244 # the JTAG tap reset signal might be hooked to a slow
245 # resistor/capacitor circuit - and it might take a while
246 # to charge
247
248 # Catch, but ignore any errors.
249 catch { $t arp_waitstate halted 1000 }
250
251 # Did we succeed?
252 set s [$t curstate]
253
254 if { 0 != [string compare $s "halted" ] } {
255 return -error [format "TARGET: %s - Not halted" $t]
256 }
257 }
258 }
259
260 #Pass 2 - if needed "init"
261 if { 0 == [string compare init $MODE] } {
262 foreach t $targets {
263 if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
264 continue
265 }
266
267 set err [catch "$t arp_waitstate halted 5000"]
268 # Did it halt?
269 if { $err == 0 } {
270 $t invoke-event reset-init
271 }
272 }
273 }
274
275 foreach t $targets {
276 $t invoke-event reset-end
277 }
278 }
279
280 # stubs for targets scripts that do not have production procedure
281 proc production_info {} {
282 return "Imagine an explanation here..."
283 }
284 add_help_text production_info "Displays information on production procedure for target script. Implement this procedure in target script."
285
286 proc production {firmwarefile serialnumber} {
287 puts "Imagine production procedure running successfully. Programmed $firmwarefile with serial number $serialnumber"
288 }
289
290 add_help_text production "<serialnumber> - Runs production procedure. Throws exception if procedure failed. Prints progress messages. Implement this procedure in the target script."
291
292 proc production_test {} {
293 puts "Imagine nifty test procedure having run to completion here."
294 }
295 add_help_text production_test "Runs test procedure. Throws exception if procedure failed. Prints progress messages. Implement in target script."
296
297 add_help_text cpu "<name> - prints out target options and a comment on CPU which matches name"
298
299 # A list of names of CPU and options required
300 set ocd_cpu_list {
301 {
302 name IXP42x
303 options {xscale -variant IXP42x}
304 comment {IXP42x cpu}
305 }
306 {
307 name arm7
308 options {arm7tdmi -variant arm7tdmi}
309 comment {vanilla ARM7}
310 }
311 }
312
313 # Invoked from Tcl code
314 proc ocd_cpu {args} {
315 set name $args
316 set result ""
317 global ocd_cpu_list
318 foreach a [lsort $ocd_cpu_list] {
319 if {[string length $args]==0||[string first [string toupper $name] [string toupper "$a(name)$a(options)$a(comment)"]]!=-1} {
320 lappend result $a
321 }
322 }
323 return $result
324 }
325
326 proc cpu {args} {
327 # 0123456789012345678901234567890123456789012345678901234567890123456789
328 puts "CPU Options Comment"
329 foreach a [lsort [ocd_cpu $args]] {
330 puts [format "%-20s%-40s%s" $a(name) $a(options) $a(comment)]
331 }
332 }
333
334 proc power_restore {} {
335 puts "Sensed power restore."
336 reset init
337 }
338
339 add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
340
341 proc power_dropout {} {
342 puts "Sensed power dropout."
343 }
344
345 proc srst_deasserted {} {
346 puts "Sensed nSRST deasserted."
347 reset init
348 }
349 add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
350
351 proc srst_asserted {} {
352 puts "Sensed nSRST asserted."
353 }
354
355 # catch any exceptions, capture output and return output
356 proc capture_catch {a} {
357 catch {
358 capture {uplevel $a}
359 } result
360 return $result
361 }
362
363
364 # Executed during "init". Can be implemented by target script
365 # tar
366 proc jtag_init {} {
367 if {[catch {jtag arp_init} err]!=0} {
368 # try resetting additionally
369 jtag arp_init-reset
370 }
371 }