wrap help command
[fw/openocd] / 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 # This reset logic may be overridden by board/target/... scripts as needed
147 # to provide a reset that, if possible, is close to a power-up reset.
148 #
149 # Exit requirements include:  (a) JTAG must be working, (b) the scan
150 # chain was validated with "jtag arp_init" (or equivalent), (c) nothing
151 # stays in reset.  No TAP-specific scans were performed.  It's OK if
152 # some targets haven't been reset yet; they may need TAP-specific scans.
153 #
154 # The "mode" values include:  halt, init, run (from "reset" command);
155 # startup (at OpenOCD server startup, when JTAG may not yet work); and
156 # potentially more (for reset types like cold, warm, etc)
157 proc init_reset { mode } {
158         jtag arp_init-reset
159 }
160
161
162 global in_process_reset
163 set in_process_reset 0
164
165 # Catch reset recursion
166 proc ocd_process_reset { MODE } {
167         global in_process_reset
168         if {$in_process_reset} {
169                 set in_process_reset 0
170                 return -code error "'reset' can not be invoked recursively"
171         }
172
173         set in_process_reset 1
174         set success [expr [catch {ocd_process_reset_inner $MODE} result]==0]
175         set in_process_reset 0
176
177         if {$success} {
178                 return $result
179         } else {
180                 return -code error $result
181         }
182 }
183
184 proc ocd_process_reset_inner { MODE } {
185         set targets [target names]
186
187         # If this target must be halted...
188         set halt -1
189         if { 0 == [string compare $MODE halt] } {
190                 set halt 1
191         }
192         if { 0 == [string compare $MODE init] } {
193                 set halt 1;
194         }
195         if { 0 == [string compare $MODE run ] } {
196                 set halt 0;
197         }
198         if { $halt < 0 } {
199                 return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
200         }
201
202         # Target event handlers *might* change which TAPs are enabled
203         # or disabled, so we fire all of them.  But don't issue any
204         # target "arp_*" commands, which may issue JTAG transactions,
205         # unless we know the underlying TAP is active.
206         #
207         # NOTE:  ARP == "Advanced Reset Process" ... "advanced" is
208         # relative to a previous restrictive scheme
209
210         foreach t $targets {
211                 # New event script.
212                 $t invoke-event reset-start
213         }
214
215         # Use TRST or TMS/TCK operations to reset all the tap controllers.
216         # TAP reset events get reported; they might enable some taps.
217         init_reset $MODE
218
219         # Examine all targets on enabled taps.
220         foreach t $targets {
221                 if {[jtag tapisenabled [$t cget -chain-position]]} {
222                         $t arp_examine
223                 }
224         }
225
226         # Assert SRST, and report the pre/post events.
227         # Note:  no target sees SRST before "pre" or after "post".
228         foreach t $targets {
229                 $t invoke-event reset-assert-pre
230         }
231         foreach t $targets {
232                 # C code needs to know if we expect to 'halt'
233                 if {[jtag tapisenabled [$t cget -chain-position]]} {
234                         $t arp_reset assert $halt
235                 }
236         }
237         foreach t $targets {
238                 $t invoke-event reset-assert-post
239         }
240
241         # Now de-assert SRST, and report the pre/post events.
242         # Note:  no target sees !SRST before "pre" or after "post".
243         foreach t $targets {
244                 $t invoke-event reset-deassert-pre
245         }
246         foreach t $targets {
247                 # Again, de-assert code needs to know if we 'halt'
248                 if {[jtag tapisenabled [$t cget -chain-position]]} {
249                         $t arp_reset deassert $halt
250                 }
251         }
252         foreach t $targets {
253                 $t invoke-event reset-deassert-post
254         }
255
256         # Pass 1 - Now wait for any halt (requested as part of reset
257         # assert/deassert) to happen.  Ideally it takes effect without
258         # first executing any instructions.
259         if { $halt } {
260                 foreach t $targets {
261                         if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
262                                 continue
263                         }
264
265                         # Wait upto 1 second for target to halt.  Why 1sec? Cause
266                         # the JTAG tap reset signal might be hooked to a slow
267                         # resistor/capacitor circuit - and it might take a while
268                         # to charge
269
270                         # Catch, but ignore any errors.
271                         catch { $t arp_waitstate halted 1000 }
272
273                         # Did we succeed?
274                         set s [$t curstate]
275
276                         if { 0 != [string compare $s "halted" ] } {
277                                 return -error [format "TARGET: %s - Not halted" $t]
278                         }
279                 }
280         }
281
282         #Pass 2 - if needed "init"
283         if { 0 == [string compare init $MODE] } {
284                 foreach t $targets {
285                         if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
286                                 continue
287                         }
288
289                         set err [catch "$t arp_waitstate halted 5000"]
290                         # Did it halt?
291                         if { $err == 0 } {
292                                 $t invoke-event reset-init
293                         }
294                 }
295         }
296
297         foreach t $targets {
298                 $t invoke-event reset-end
299         }
300 }
301
302 #########
303
304 # REVISIT power_restore, power_dropout, srst_deasserted, srst_asserted
305 # are currently neither documented nor supported except on ZY1000.
306
307 proc power_restore {} {
308         puts "Sensed power restore."
309         reset init
310 }
311
312 add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
313
314 proc power_dropout {} {
315         puts "Sensed power dropout."
316 }
317
318 proc srst_deasserted {} {
319         puts "Sensed nSRST deasserted."
320         reset init
321 }
322 add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
323
324 proc srst_asserted {} {
325         puts "Sensed nSRST asserted."
326 }
327
328 #########
329
330 # catch any exceptions, capture output and return output
331 proc capture_catch {a} {
332         catch {
333                 capture {uplevel $a}
334         } result
335         return $result
336 }
337
338
339 # Executed during "init". Can be overridden
340 # by board/target/... scripts
341 proc jtag_init {} {
342         if {[catch {jtag arp_init} err]!=0} {
343                 # try resetting additionally
344                 init_reset startup
345         }
346 }