096f03a8e870fc0c75fab86ba5972c8d4613df6e
[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 || \
53 [string first $cmd $a] != -1 || \
54 [string first $cmd [lindex $a 1]] != -1} \
55 {
56 set w 50
57 set cmdname [lindex $a 0]
58 set h [lindex $a 1]
59 set n 0
60 while 1 {
61 if {$n > [string length $h]} {break}
62
63 set next_a [expr $n + $w]
64 if {[string length $h] > $n + $w} \
65 {
66 set xxxx [string range $h $n [expr $n + $w]]
67 for {set lastpos [expr [string length $xxxx] - 1]} \
68 {$lastpos >= 0 && [string compare \
69 [string range $xxxx $lastpos $lastpos] " "] != 0} \
70 {set lastpos [expr $lastpos - 1]} \
71 {
72 }
73 #set next_a -1
74 if {$lastpos != -1} {
75 set next_a [expr $lastpos + $n + 1]
76 }
77 }
78
79 puts [format "%-25s %s" $cmdname \
80 [string range $h $n [expr $next_a-1]] ]
81 set cmdname ""
82 set n [expr $next_a]
83 }
84 }
85 }
86 }
87
88 add_help_text help "Tcl implementation of help command"
89
90
91 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
92 #
93 # We also support two level commands. "flash banks" is translated to
94 # flash_banks
95 proc unknown {args} {
96 # do the name mangling from "flash banks" to "flash_banks"
97 if {[llength $args]>=2} {
98 set cmd_name "[lindex $args 0]_[lindex $args 1]"
99 if {[catch {info body $cmd_name}]==0} {
100 # the command exists, try it...
101 return [eval "$cmd_name [lrange $args 2 end]"]
102 }
103 }
104 # This really is an unknown command.
105 return -code error "Unknown command: $args"
106 }
107
108 proc new_target_name { } {
109 return [target number [expr [target count] - 1 ]]
110 }
111
112 # Try flipping / and \ to find file if the filename does not
113 # match the precise spelling
114 proc find {filename} {
115 if {[catch {ocd_find $filename} t]==0} {
116 return $t
117 }
118 if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
119 return $t
120 }
121 if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
122 return $t
123 }
124 # make sure error message matches original input string
125 return -code error "Can't find $filename"
126 }
127 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
128
129 # Run script
130 proc script {filename} {
131 source [find $filename]
132 }
133
134 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
135
136 # Handle GDB 'R' packet. Can be overriden by configuration script,
137 # but it's not something one would expect target scripts to do
138 # normally
139 proc ocd_gdb_restart {target_id} {
140 # Fix!!! we're resetting all targets here! Really we should reset only
141 # one target
142 reset halt
143 }
144
145 #########
146
147 # Temporary migration aid. May be removed starting in January 2011.
148 proc armv4_5 params {
149 echo "DEPRECATED! use 'arm $params' not 'armv4_5 $params'"
150 arm $params
151 }
152
153 #########
154
155 # This reset logic may be overridden by board/target/... scripts as needed
156 # to provide a reset that, if possible, is close to a power-up reset.
157 #
158 # Exit requirements include: (a) JTAG must be working, (b) the scan
159 # chain was validated with "jtag arp_init" (or equivalent), (c) nothing
160 # stays in reset. No TAP-specific scans were performed. It's OK if
161 # some targets haven't been reset yet; they may need TAP-specific scans.
162 #
163 # The "mode" values include: halt, init, run (from "reset" command);
164 # startup (at OpenOCD server startup, when JTAG may not yet work); and
165 # potentially more (for reset types like cold, warm, etc)
166 proc init_reset { mode } {
167 jtag arp_init-reset
168 }
169
170
171 global in_process_reset
172 set in_process_reset 0
173
174 # Catch reset recursion
175 proc ocd_process_reset { MODE } {
176 global in_process_reset
177 if {$in_process_reset} {
178 set in_process_reset 0
179 return -code error "'reset' can not be invoked recursively"
180 }
181
182 set in_process_reset 1
183 set success [expr [catch {ocd_process_reset_inner $MODE} result]==0]
184 set in_process_reset 0
185
186 if {$success} {
187 return $result
188 } else {
189 return -code error $result
190 }
191 }
192
193 proc ocd_process_reset_inner { MODE } {
194 set targets [target names]
195
196 # If this target must be halted...
197 set halt -1
198 if { 0 == [string compare $MODE halt] } {
199 set halt 1
200 }
201 if { 0 == [string compare $MODE init] } {
202 set halt 1;
203 }
204 if { 0 == [string compare $MODE run ] } {
205 set halt 0;
206 }
207 if { $halt < 0 } {
208 return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
209 }
210
211 # Target event handlers *might* change which TAPs are enabled
212 # or disabled, so we fire all of them. But don't issue any
213 # target "arp_*" commands, which may issue JTAG transactions,
214 # unless we know the underlying TAP is active.
215 #
216 # NOTE: ARP == "Advanced Reset Process" ... "advanced" is
217 # relative to a previous restrictive scheme
218
219 foreach t $targets {
220 # New event script.
221 $t invoke-event reset-start
222 }
223
224 # Use TRST or TMS/TCK operations to reset all the tap controllers.
225 # TAP reset events get reported; they might enable some taps.
226 init_reset $MODE
227
228 # Examine all targets on enabled taps.
229 foreach t $targets {
230 if {[jtag tapisenabled [$t cget -chain-position]]} {
231 $t arp_examine
232 }
233 }
234
235 # Assert SRST, and report the pre/post events.
236 # Note: no target sees SRST before "pre" or after "post".
237 foreach t $targets {
238 $t invoke-event reset-assert-pre
239 }
240 foreach t $targets {
241 # C code needs to know if we expect to 'halt'
242 if {[jtag tapisenabled [$t cget -chain-position]]} {
243 $t arp_reset assert $halt
244 }
245 }
246 foreach t $targets {
247 $t invoke-event reset-assert-post
248 }
249
250 # Now de-assert SRST, and report the pre/post events.
251 # Note: no target sees !SRST before "pre" or after "post".
252 foreach t $targets {
253 $t invoke-event reset-deassert-pre
254 }
255 foreach t $targets {
256 # Again, de-assert code needs to know if we 'halt'
257 if {[jtag tapisenabled [$t cget -chain-position]]} {
258 $t arp_reset deassert $halt
259 }
260 }
261 foreach t $targets {
262 $t invoke-event reset-deassert-post
263 }
264
265 # Pass 1 - Now wait for any halt (requested as part of reset
266 # assert/deassert) to happen. Ideally it takes effect without
267 # first executing any instructions.
268 if { $halt } {
269 foreach t $targets {
270 if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
271 continue
272 }
273
274 # Wait upto 1 second for target to halt. Why 1sec? Cause
275 # the JTAG tap reset signal might be hooked to a slow
276 # resistor/capacitor circuit - and it might take a while
277 # to charge
278
279 # Catch, but ignore any errors.
280 catch { $t arp_waitstate halted 1000 }
281
282 # Did we succeed?
283 set s [$t curstate]
284
285 if { 0 != [string compare $s "halted" ] } {
286 return -error [format "TARGET: %s - Not halted" $t]
287 }
288 }
289 }
290
291 #Pass 2 - if needed "init"
292 if { 0 == [string compare init $MODE] } {
293 foreach t $targets {
294 if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
295 continue
296 }
297
298 set err [catch "$t arp_waitstate halted 5000"]
299 # Did it halt?
300 if { $err == 0 } {
301 $t invoke-event reset-init
302 }
303 }
304 }
305
306 foreach t $targets {
307 $t invoke-event reset-end
308 }
309 }
310
311 #########
312
313 # REVISIT power_restore, power_dropout, srst_deasserted, srst_asserted
314 # are currently neither documented nor supported except on ZY1000.
315
316 proc power_restore {} {
317 puts "Sensed power restore."
318 reset init
319 }
320
321 add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
322
323 proc power_dropout {} {
324 puts "Sensed power dropout."
325 }
326
327 proc srst_deasserted {} {
328 puts "Sensed nSRST deasserted."
329 reset init
330 }
331 add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
332
333 proc srst_asserted {} {
334 puts "Sensed nSRST asserted."
335 }
336
337 #########
338
339 # catch any exceptions, capture output and return output
340 proc capture_catch {a} {
341 catch {
342 capture {uplevel $a}
343 } result
344 return $result
345 }
346
347
348 # Executed during "init". Can be overridden
349 # by board/target/... scripts
350 proc jtag_init {} {
351 if {[catch {jtag arp_init} err]!=0} {
352 # try resetting additionally
353 init_reset startup
354 }
355 }