]> git.sur5r.net Git - openocd/blob - src/helper/startup.tcl
execute reset init upon power restore / srst deassert
[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                 # Fix?? add a check here if this is a command?
92                 # we'll strip away args until we fail anyway...
93                 return [eval "$cmd_name [lrange $args 2 end]"]
94         }
95         # This really is an unknown command.
96         return -code error "Unknown command: $args"
97 }
98
99 proc new_target_name { } {
100         return [target number [expr [target count] - 1 ]]
101 }
102
103
104 proc target_script {target_num eventname scriptname} {
105
106         set tname [target number $target_num]
107         
108         if { 0 == [string compare $eventname "reset"] } {
109                 $tname configure -event reset-init "script $scriptname"
110                 return
111         }
112
113         if { 0 == [string compare $eventname "post_reset"] } {
114                 $tname configure -event reset-init "script $scriptname"
115                 return
116         }
117
118         if { 0 == [string compare $eventname "pre_reset"] } {
119                 $tname configure -event reset-start "script $scriptname"
120                 return
121         }
122
123         if { 0 == [string compare $eventname "gdb_program_config"] } {
124                 $tname configure -event old-gdb_program_config "script $scriptname"
125                 return
126         }
127
128         return -code error "Unknown target (old) event: $eventname (try $tname configure -event NAME)"
129
130 }
131
132 add_help_text target_script "DEPRECATED please see the new TARGETNAME configure -event interface"
133
134 # Try flipping / and \ to find file if the filename does not
135 # match the precise spelling
136 proc find {filename} {
137         if {[catch {ocd_find $filename} t]==0} {
138                 return $t
139         }
140         if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
141                 return $t
142         }
143         if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
144                 return $t
145         }
146         # make sure error message matches original input string
147         return -code error "Can't find $filename"
148 }
149 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
150
151 # Run script
152 proc script {filename} {
153         source [find $filename]
154 }
155
156 #proc daemon_reset {} {
157 #       puts "Daemon reset is obsolete. Use -c init -c \"reset halt\" at end of openocd command line instead");
158 #}
159
160 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
161
162 # Handle GDB 'R' packet. Can be overriden by configuration script,
163 # but it's not something one would expect target scripts to do
164 # normally
165 proc ocd_gdb_restart {target_num} {
166         # Fix!!! we're resetting all targets here! Really we should reset only
167         # one target
168         reset halt
169 }
170
171 # If RCLK is not supported, use fallback_speed_khz
172 proc jtag_rclk {fallback_speed_khz} {
173         if {[catch {jtag_khz 0}]!=0} {
174                 jtag_khz $fallback_speed_khz
175         }
176 }
177
178 add_help_text jtag_rclk "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed"
179
180 proc ocd_process_reset { MODE } {
181
182         # If this target must be halted...
183         set halt -1
184         if { 0 == [string compare $MODE halt] } {
185                 set halt 1
186         }
187         if { 0 == [string compare $MODE init] } {
188                 set halt 1;
189         }
190         if { 0 == [string compare $MODE run ] } {
191                 set halt 0;
192         }
193         if { $halt < 0 } {
194                 return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
195         }
196
197         foreach t [ target names ] {
198                 # New event script.
199                 $t invoke-event reset-start
200         }
201
202         # Init the tap controller.
203         jtag arp_init-reset
204
205         # Examine all targets.
206         foreach t [ target names ] {
207                 $t arp_examine
208         }
209
210         # Let the C code know we are asserting reset.
211         foreach t [ target names ] {
212                 $t invoke-event reset-assert-pre
213                 # C code needs to know if we expect to 'halt'
214                 $t arp_reset assert $halt
215                 $t invoke-event reset-assert-post
216         }
217
218         # Now de-assert reset.
219         foreach t [ target names ] {
220                 $t invoke-event reset-deassert-pre
221                 # Again, de-assert code needs to know..
222                 $t arp_reset deassert $halt
223                 $t invoke-event reset-deassert-post
224         }
225
226         # Pass 1 - Now try to halt.
227         if { $halt } {
228                 foreach t [target names] {
229         
230                         # Wait upto 1 second for target to halt.  Why 1sec? Cause
231                         # the JTAG tap reset signal might be hooked to a slow
232                         # resistor/capacitor circuit - and it might take a while
233                         # to charge
234                         
235                         # Catch, but ignore any errors.
236                         catch { $t arp_waitstate halted 1000 }
237                         
238                         # Did we succeed?
239                         set s [$t curstate]
240                         
241                         if { 0 != [string compare $s "halted" ] } {
242                                 return -error [format "TARGET: %s - Not halted" $t]
243                         }
244                 }
245         }
246
247         #Pass 2 - if needed "init"
248         if { 0 == [string compare init $MODE] } {
249                 foreach t [target names] {
250                         set err [catch "$t arp_waitstate halted 5000"]
251                         # Did it halt?
252                         if { $err == 0 } {
253                                 $t invoke-event reset-init              
254                         }
255                 }
256         }
257
258         foreach t [ target names ] {
259                 $t invoke-event reset-end
260         }
261 }
262
263 # stubs for targets scripts that do not have production procedure
264 proc production_info {} {
265         return "Imagine an explanation here..."
266 }
267 add_help_text production_info "Displays information on production procedure for target script. Implement this procedure in target script."
268
269 proc production {firmwarefile serialnumber} {
270         puts "Imagine production procedure running successfully. Programmed $firmwarefile with serial number $serialnumber"
271 }
272
273 add_help_text production "<serialnumber> - Runs production procedure. Throws exception if procedure failed. Prints progress messages. Implement this procedure in the target script."
274
275 proc production_test {} {
276         puts "Imagine nifty test procedure having run to completion here."
277 }
278 add_help_text production "Runs test procedure. Throws exception if procedure failed. Prints progress messages. Implement in target script."
279
280 proc load {args} {
281         return [eval "load_image $args"]
282 }
283 add_help_text load "synonym to load_image"
284
285 proc verify {args} {
286         return [eval "verify_image $args"]
287 }
288
289 add_help_text verify "synonym to verify_image"
290
291
292
293 add_help_text cpu "<name> - prints out target options and a comment on CPU which matches name"
294
295 # A list of names of CPU and options required
296 set ocd_cpu_list {
297         {
298                 name IXP42x 
299                 options {xscale -variant IXP42x} 
300                 comment {IXP42x cpu}
301         }
302         {
303                 name arm7 
304                 options {arm7tdmi -variant arm7tdmi} 
305                 comment {vanilla ARM7}
306         }
307 }
308
309 # Invoked from Tcl code
310 proc ocd_cpu {args} {
311         set name $args
312         set result ""
313         global ocd_cpu_list
314         foreach a [lsort $ocd_cpu_list] {
315                 if {[string length $args]==0||[string first [string toupper $name] [string toupper "$a(name)$a(options)$a(comment)"]]!=-1} {
316                         lappend result $a 
317                 }
318         }
319         return $result
320 }
321
322 proc cpu {args} {
323     #     0123456789012345678901234567890123456789012345678901234567890123456789
324         puts "CPU                 Options                                 Comment"
325         foreach a [lsort [ocd_cpu $args]] {
326                 puts [format "%-20s%-40s%s" $a(name) $a(options) $a(comment)]
327         }
328 }
329
330 proc power_restore {} {
331         puts "Sensed power restore."
332         reset init
333 }
334
335 add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
336
337 proc power_dropout {} {
338         puts "Sensed power dropout."
339 }
340
341 proc srst_deasserted {} {
342         puts "Sensed nSRST deasserted."
343         reset init
344 }
345 add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
346
347 proc srst_asserted {} {
348         puts "Sensed nSRST asserted."
349 }