2 # Defines basic Tcl procs that must be there for
5 # Embedded into OpenOCD executable
9 # Help text list. A list of command + help text pairs.
11 # Commands can be more than one word and they are stored
12 # as "flash banks" "help text x x x"
14 proc add_help_text {cmd cmd_help} {
16 lappend ocd_helptext [list $cmd $cmd_help]
19 proc get_help_text {} {
25 # Show flash in human readable form
26 # This is an example of a human readable form of a low level fn
30 foreach {a} [ocd_flash_banks] {
32 set result "$result\n"
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)]
40 # We need to explicitly redirect this to the OpenOCD command
41 # as Tcl defines the exit proc
46 #Print help text for a command. Word wrap
47 #help text that is too wide inside column.
51 foreach a [lsort $ocd_helptext] {
52 if {[string length $cmd] == 0 || \
53 [string first $cmd $a] != -1 || \
54 [string first $cmd [lindex $a 1]] != -1} \
57 set cmdname [lindex $a 0]
61 if {$n > [string length $h]} {break}
63 set next_a [expr $n + $w]
64 if {[string length $h] > $n + $w} \
66 set xxxx [string range $h $n [expr $n + $w]]
67 for {set lastpos [expr [string length $xxxx] - 1]} \
68 {$lastpos >= 0 && [string compare \
69 [string range $xxxx $lastpos $lastpos] " "] != 0} \
70 {set lastpos [expr $lastpos - 1]} \
75 set next_a [expr $lastpos + $n + 1]
79 puts [format "%-25s %s" $cmdname \
80 [string range $h $n [expr $next_a-1]] ]
88 add_help_text help "Tcl implementation of help command"
91 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
93 # We also support two level commands. "flash banks" is translated to
96 # do the name mangling from "flash banks" to "flash_banks"
97 if {[llength $args]>=2} {
98 set cmd_name "[lindex $args 0]_[lindex $args 1]"
99 if {[catch {info body $cmd_name}]==0} {
100 # the command exists, try it...
101 return [eval "$cmd_name [lrange $args 2 end]"]
104 # This really is an unknown command.
105 return -code error "Unknown command: $args"
108 proc new_target_name { } {
109 return [target number [expr [target count] - 1 ]]
112 # Try flipping / and \ to find file if the filename does not
113 # match the precise spelling
114 proc find {filename} {
115 if {[catch {ocd_find $filename} t]==0} {
118 if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
121 if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
124 # make sure error message matches original input string
125 return -code error "Can't find $filename"
127 add_help_text find "<file> - print full path to file according to OpenOCD search rules"
130 proc script {filename} {
131 source [find $filename]
134 add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
136 # Handle GDB 'R' packet. Can be overriden by configuration script,
137 # but it's not something one would expect target scripts to do
139 proc ocd_gdb_restart {target_id} {
140 # Fix!!! we're resetting all targets here! Really we should reset only
146 # This reset logic may be overridden by board/target/... scripts as needed
147 # to provide a reset that, if possible, is close to a power-up reset.
149 # Exit requirements include: (a) JTAG must be working, (b) the scan
150 # chain was validated with "jtag arp_init" (or equivalent), (c) nothing
151 # stays in reset. No TAP-specific scans were performed. It's OK if
152 # some targets haven't been reset yet; they may need TAP-specific scans.
154 # The "mode" values include: halt, init, run (from "reset" command);
155 # startup (at OpenOCD server startup, when JTAG may not yet work); and
156 # potentially more (for reset types like cold, warm, etc)
157 proc init_reset { mode } {
162 global in_process_reset
163 set in_process_reset 0
165 # Catch reset recursion
166 proc ocd_process_reset { MODE } {
167 global in_process_reset
168 if {$in_process_reset} {
169 set in_process_reset 0
170 return -code error "'reset' can not be invoked recursively"
173 set in_process_reset 1
174 set success [expr [catch {ocd_process_reset_inner $MODE} result]==0]
175 set in_process_reset 0
180 return -code error $result
184 proc ocd_process_reset_inner { MODE } {
185 set targets [target names]
187 # If this target must be halted...
189 if { 0 == [string compare $MODE halt] } {
192 if { 0 == [string compare $MODE init] } {
195 if { 0 == [string compare $MODE run ] } {
199 return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
202 # Target event handlers *might* change which TAPs are enabled
203 # or disabled, so we fire all of them. But don't issue any
204 # target "arp_*" commands, which may issue JTAG transactions,
205 # unless we know the underlying TAP is active.
207 # NOTE: ARP == "Advanced Reset Process" ... "advanced" is
208 # relative to a previous restrictive scheme
212 $t invoke-event reset-start
215 # Use TRST or TMS/TCK operations to reset all the tap controllers.
216 # TAP reset events get reported; they might enable some taps.
219 # Examine all targets on enabled taps.
221 if {[jtag tapisenabled [$t cget -chain-position]]} {
226 # Assert SRST, and report the pre/post events.
227 # Note: no target sees SRST before "pre" or after "post".
229 $t invoke-event reset-assert-pre
232 # C code needs to know if we expect to 'halt'
233 if {[jtag tapisenabled [$t cget -chain-position]]} {
234 $t arp_reset assert $halt
238 $t invoke-event reset-assert-post
241 # Now de-assert SRST, and report the pre/post events.
242 # Note: no target sees !SRST before "pre" or after "post".
244 $t invoke-event reset-deassert-pre
247 # Again, de-assert code needs to know if we 'halt'
248 if {[jtag tapisenabled [$t cget -chain-position]]} {
249 $t arp_reset deassert $halt
253 $t invoke-event reset-deassert-post
256 # Pass 1 - Now wait for any halt (requested as part of reset
257 # assert/deassert) to happen. Ideally it takes effect without
258 # first executing any instructions.
261 if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
265 # Wait upto 1 second for target to halt. Why 1sec? Cause
266 # the JTAG tap reset signal might be hooked to a slow
267 # resistor/capacitor circuit - and it might take a while
270 # Catch, but ignore any errors.
271 catch { $t arp_waitstate halted 1000 }
276 if { 0 != [string compare $s "halted" ] } {
277 return -error [format "TARGET: %s - Not halted" $t]
282 #Pass 2 - if needed "init"
283 if { 0 == [string compare init $MODE] } {
285 if {[jtag tapisenabled [$t cget -chain-position]] == 0} {
289 set err [catch "$t arp_waitstate halted 5000"]
292 $t invoke-event reset-init
298 $t invoke-event reset-end
304 # REVISIT power_restore, power_dropout, srst_deasserted, srst_asserted
305 # are currently neither documented nor supported except on ZY1000.
307 proc power_restore {} {
308 puts "Sensed power restore."
312 add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
314 proc power_dropout {} {
315 puts "Sensed power dropout."
318 proc srst_deasserted {} {
319 puts "Sensed nSRST deasserted."
322 add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
324 proc srst_asserted {} {
325 puts "Sensed nSRST asserted."
330 # catch any exceptions, capture output and return output
331 proc capture_catch {a} {
339 # Executed during "init". Can be overridden
340 # by board/target/... scripts
342 if {[catch {jtag arp_init} err]!=0} {
343 # try resetting additionally