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