less weird error messages for unknown commands. Check if command exists before trying it.
[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 #proc daemon_reset {} {
127 # puts "Daemon reset is obsolete. Use -c init -c \"reset halt\" at end of openocd command line instead");
128 #}
129
130 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
131
132 # Handle GDB 'R' packet. Can be overriden by configuration script,
133 # but it's not something one would expect target scripts to do
134 # normally
135 proc ocd_gdb_restart {target_num} {
136 # Fix!!! we're resetting all targets here! Really we should reset only
137 # one target
138 reset halt
139 }
140
141 # If RCLK is not supported, use fallback_speed_khz
142 proc jtag_rclk {fallback_speed_khz} {
143 if {[catch {jtag_khz 0}]!=0} {
144 jtag_khz $fallback_speed_khz
145 }
146 }
147
148 add_help_text jtag_rclk "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed"
149
150 proc ocd_process_reset { MODE } {
151
152 # If this target must be halted...
153 set halt -1
154 if { 0 == [string compare $MODE halt] } {
155 set halt 1
156 }
157 if { 0 == [string compare $MODE init] } {
158 set halt 1;
159 }
160 if { 0 == [string compare $MODE run ] } {
161 set halt 0;
162 }
163 if { $halt < 0 } {
164 return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
165 }
166
167 foreach t [ target names ] {
168 # New event script.
169 $t invoke-event reset-start
170 }
171
172 # Init the tap controller.
173 jtag arp_init-reset
174
175 # Examine all targets.
176 foreach t [ target names ] {
177 $t arp_examine
178 }
179
180 # Let the C code know we are asserting reset.
181 foreach t [ target names ] {
182 $t invoke-event reset-assert-pre
183 # C code needs to know if we expect to 'halt'
184 $t arp_reset assert $halt
185 $t invoke-event reset-assert-post
186 }
187
188 # Now de-assert reset.
189 foreach t [ target names ] {
190 $t invoke-event reset-deassert-pre
191 # Again, de-assert code needs to know..
192 $t arp_reset deassert $halt
193 $t invoke-event reset-deassert-post
194 }
195
196 # Pass 1 - Now try to halt.
197 if { $halt } {
198 foreach t [target names] {
199
200 # Wait upto 1 second for target to halt. Why 1sec? Cause
201 # the JTAG tap reset signal might be hooked to a slow
202 # resistor/capacitor circuit - and it might take a while
203 # to charge
204
205 # Catch, but ignore any errors.
206 catch { $t arp_waitstate halted 1000 }
207
208 # Did we succeed?
209 set s [$t curstate]
210
211 if { 0 != [string compare $s "halted" ] } {
212 return -error [format "TARGET: %s - Not halted" $t]
213 }
214 }
215 }
216
217 #Pass 2 - if needed "init"
218 if { 0 == [string compare init $MODE] } {
219 foreach t [target names] {
220 set err [catch "$t arp_waitstate halted 5000"]
221 # Did it halt?
222 if { $err == 0 } {
223 $t invoke-event reset-init
224 }
225 }
226 }
227
228 foreach t [ target names ] {
229 $t invoke-event reset-end
230 }
231 }
232
233 # stubs for targets scripts that do not have production procedure
234 proc production_info {} {
235 return "Imagine an explanation here..."
236 }
237 add_help_text production_info "Displays information on production procedure for target script. Implement this procedure in target script."
238
239 proc production {firmwarefile serialnumber} {
240 puts "Imagine production procedure running successfully. Programmed $firmwarefile with serial number $serialnumber"
241 }
242
243 add_help_text production "<serialnumber> - Runs production procedure. Throws exception if procedure failed. Prints progress messages. Implement this procedure in the target script."
244
245 proc production_test {} {
246 puts "Imagine nifty test procedure having run to completion here."
247 }
248 add_help_text production "Runs test procedure. Throws exception if procedure failed. Prints progress messages. Implement in target script."
249
250 add_help_text cpu "<name> - prints out target options and a comment on CPU which matches name"
251
252 # A list of names of CPU and options required
253 set ocd_cpu_list {
254 {
255 name IXP42x
256 options {xscale -variant IXP42x}
257 comment {IXP42x cpu}
258 }
259 {
260 name arm7
261 options {arm7tdmi -variant arm7tdmi}
262 comment {vanilla ARM7}
263 }
264 }
265
266 # Invoked from Tcl code
267 proc ocd_cpu {args} {
268 set name $args
269 set result ""
270 global ocd_cpu_list
271 foreach a [lsort $ocd_cpu_list] {
272 if {[string length $args]==0||[string first [string toupper $name] [string toupper "$a(name)$a(options)$a(comment)"]]!=-1} {
273 lappend result $a
274 }
275 }
276 return $result
277 }
278
279 proc cpu {args} {
280 # 0123456789012345678901234567890123456789012345678901234567890123456789
281 puts "CPU Options Comment"
282 foreach a [lsort [ocd_cpu $args]] {
283 puts [format "%-20s%-40s%s" $a(name) $a(options) $a(comment)]
284 }
285 }
286
287 proc power_restore {} {
288 puts "Sensed power restore."
289 reset init
290 }
291
292 add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
293
294 proc power_dropout {} {
295 puts "Sensed power dropout."
296 }
297
298 proc srst_deasserted {} {
299 puts "Sensed nSRST deasserted."
300 reset init
301 }
302 add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
303
304 proc srst_asserted {} {
305 puts "Sensed nSRST asserted."
306 }
307
308 # catch any exceptions, capture output and return output
309 proc capture_catch {a} {
310 catch {
311 capture {uplevel $a}
312 } result
313 return $result
314 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)