]> git.sur5r.net Git - openocd/blob - src/helper/startup.tcl
Fixed gaffes in reset script handling + improved error
[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 # Production command
25 # FIX!!! need to figure out how to feed back relevant output
26 # from e.g. "flash banks" command...
27 proc board_produce {filename serialnumber} {
28         openocd "reset init"
29         openocd "flash write_image erase $filename [flash] bin"]]
30         openocd "verify_image $filename [flash] bin"]]
31         echo "Successfully ran production procedure"
32 }
33
34 proc board_test {} {
35         echo "Production test not implemented"
36 }
37
38 # Show flash in human readable form
39 # This is an example of a human readable form of a low level fn
40 proc flash_banks {} { 
41         set i 0         
42         set result ""
43         foreach {a} [ocd_flash_banks] {
44                 if {$i > 0} {
45                         set result "$result\n"
46                 }
47                 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)]
48                 set i [expr $i+1]       
49         }       
50         return $result
51 }
52
53 # We need to explicitly redirect this to the OpenOCD command
54 # as Tcl defines the exit proc
55 proc exit {} {
56         ocd_throw exit
57 }
58
59 #Print help text for a command. Word wrap
60 #help text that is too wide inside column.
61 proc help {args} {
62         global ocd_helptext
63         set cmd $args
64         foreach a [lsort $ocd_helptext] {
65                 if {[string length $cmd]==0||[string first $cmd $a]!=-1||[string first $cmd [lindex $a 1]]!=-1} {
66                         set w 50
67                         set cmdname [lindex $a 0]
68                         set h [lindex $a 1]
69                         set n 0
70                         while 1 {
71                                 if {$n > [string length $h]} {break}
72                                 
73                                 set next_a [expr $n+$w]
74                                 if {[string length $h]>$n+$w} {
75                                         set xxxx [string range $h $n [expr $n+$w]]
76                                         for {set lastpos [expr [string length $xxxx]-1]} {$lastpos>=0&&[string compare [string range $xxxx $lastpos $lastpos] " "]!=0} {set lastpos [expr $lastpos-1]} {
77                                         }
78                                         #set next_a -1
79                                         if {$lastpos!=-1} {
80                                                 set next_a [expr $lastpos+$n+1]
81                                         }
82                                 }
83                                 
84                                 
85                                 puts [format "%-25s %s" $cmdname [string range $h $n [expr $next_a-1]] ]
86                                 set cmdname ""
87                                 set n [expr $next_a]
88                         }
89                 }
90         }
91 }
92
93 add_help_text help "Tcl implementation of help command"
94
95
96 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
97 #
98 # We also support two level commands. "flash banks" is translated to
99 # flash_banks
100 proc unknown {args} {
101         # do the name mangling from "flash banks" to "flash_banks"
102         if {[llength $args]>=2} {
103                 set cmd_name "[lindex $args 0]_[lindex $args 1]"
104                 # Fix?? add a check here if this is a command?
105                 # we'll strip away args until we fail anyway...
106                 return [eval "$cmd_name [lrange $args 2 end]"]
107         }
108         # This really is an unknown command.
109         return -code error "Unknown command: $args"
110 }
111
112 proc new_target_name { } {
113     return [target number [expr [target count] - 1 ]]
114 }
115
116
117 proc target_script {target_num eventname scriptname} {
118
119     set tname [target number $target_num]
120     
121     if { 0 == [string compare $eventname "reset"] } {
122         $tname configure -event old-post_reset "script $scriptname"
123         return
124     }
125
126     if { 0 == [string compare $eventname "post_reset"] } {
127         $tname configure -event old-post_reset "script $scriptname"
128         return
129     }
130
131     if { 0 == [string compare $eventname "pre_reset"] } {
132         $tname configure -event old-pre_reset "script $scriptname"
133         return
134     }
135
136     if { 0 == [string compare $eventname "gdb_program_config"] } {
137         $tname configure -event old-gdb_program_config "script $scriptname"
138         return
139     }
140
141     return -code error "Unknown target (old) event: $eventname (try $tname configure -event NAME)"
142
143 }
144
145 add_help_text target_script "DEPRECATED please see the new TARGETNAME configure -event interface"
146
147
148 # Try flipping / and \ to find file if the filename does not
149 # match the precise spelling
150 proc find {filename} {
151         if {[catch {ocd_find $filename} t]==0} {
152                 return $t
153         }  
154         if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
155                 return $t
156         }  
157         if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
158                 return $t
159         }  
160         # make sure error message matches original input string
161         return -code error "Can't find $filename"
162 }
163 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
164
165 # Run script
166 proc script {filename} {
167         source [find $filename]
168 }
169
170 #proc daemon_reset {} {
171 #       puts "Daemon reset is obsolete. Use -c init -c \"reset halt\" at end of openocd command line instead");
172 #}
173
174 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
175
176
177
178 # Handle GDB 'R' packet. Can be overriden by configuration script,
179 # but it's not something one would expect target scripts to do
180 # normally
181 proc ocd_gdb_restart {target_num} {
182         # Fix!!! we're resetting all targets here! Really we should reset only
183         # one target
184         reset halt
185 }
186
187
188 # If RCLK is not supported, use fallback_speed_khz
189 proc jtag_rclk {fallback_speed_khz} {
190         if {[catch {jtag_khz 0}]!=0} {
191                 jtag_khz $fallback_speed_khz
192         }
193 }
194
195 add_help_text jtag_rclk "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed"
196
197 proc ocd_process_reset { MODE } {
198
199     # If this target must be halted...
200     set halt -1
201     if { 0 == [string compare $MODE halt] } {
202         set halt 1
203     }
204     if { 0 == [string compare $MODE init] } {
205         set halt 1;
206     }
207     if { 0 == [string compare $MODE run ] } {
208         set halt 0;
209     }
210     if { $halt < 0 } {
211         return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
212     }
213
214     foreach t [ target names ] {
215                 # For compatiblity with 'old scripts'
216                 $t invoke-event old-pre_reset
217         
218                 # New event script.
219                 $t invoke-event reset-start
220     }
221
222     # Init the tap controller.
223     jtag arp_init-reset
224
225     # Examine all targets.
226     foreach t [ target names ] {
227                 $t arp_examine
228     }
229
230     # Let the C code know we are asserting reset.
231     foreach t [ target names ] {
232                 $t invoke-event reset-assert-pre
233                 # C code needs to know if we expect to 'halt'
234                 $t arp_reset assert $halt
235                 $t invoke-event reset-assert-post
236     }
237
238     # Now de-assert reset.
239     foreach t [ target names ] {
240                 $t invoke-event reset-deassert-pre
241                 # Again, de-assert code needs to know..
242                 $t arp_reset deassert $halt
243                 $t invoke-event reset-deassert-post
244     }
245
246
247     # Pass 1 - Now try to halt.
248     if { $halt } {
249                 foreach t [target names] {
250         
251                     # Wait upto 1 second for target to halt.  Why 1sec? Cause
252                     # the JTAG tap reset signal might be hooked to a slow
253                     # resistor/capacitor circuit - and it might take a while
254                     # to charge
255                     
256                     # Catch, but ignore any errors.
257                     catch { $t arp_waitstate halted 1000 }
258                     
259                     # Did we succeed?
260                     set s [$t curstate]
261                     
262                     if { 0 != [string compare $s "halted" ] } {
263                                 return -error [format "TARGET: %s - Not halted" $t]
264                     }
265                 }
266     }
267
268     #Pass 2 - if needed "init"
269     if { 0 == [string compare init $MODE] } {
270                 foreach t [target names] {
271                     set err [catch "$t arp_waitstate halted 5000"]
272                     # Did it halt?
273                     if { $err == 0 } {
274                                 $t invoke-event old-post_reset
275                                 $t invoke-event reset-init              
276                     }
277                 }
278     }
279
280     foreach t [ target names ] {
281                 $t invoke-event reset-end
282     }
283 }