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