1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Defines basic Tcl procs for OpenOCD target module
5 proc new_target_name { } {
6 return [target number [expr {[target count] - 1}]]
9 global in_process_reset
10 set in_process_reset 0
12 # Catch reset recursion
13 proc ocd_process_reset { MODE } {
14 global in_process_reset
15 if {$in_process_reset} {
16 set in_process_reset 0
17 return -code error "'reset' can not be invoked recursively"
20 set in_process_reset 1
21 set success [expr {[catch {ocd_process_reset_inner $MODE} result] == 0}]
22 set in_process_reset 0
27 return -code error $result
31 proc ocd_process_reset_inner { MODE } {
32 set targets [target names]
34 # If this target must be halted...
44 return -code error "Invalid mode: $MODE, must be one of: halt, init, or run";
48 # Target event handlers *might* change which TAPs are enabled
49 # or disabled, so we fire all of them. But don't issue any
50 # target "arp_*" commands, which may issue JTAG transactions,
51 # unless we know the underlying TAP is active.
53 # NOTE: ARP == "Advanced Reset Process" ... "advanced" is
54 # relative to a previous restrictive scheme
58 $t invoke-event reset-start
61 # Use TRST or TMS/TCK operations to reset all the tap controllers.
62 # TAP reset events get reported; they might enable some taps.
65 # Examine all targets on enabled taps.
67 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
68 $t invoke-event examine-start
69 set err [catch "$t arp_examine allow-defer"]
71 $t invoke-event examine-fail
73 $t invoke-event examine-end
78 # Assert SRST, and report the pre/post events.
79 # Note: no target sees SRST before "pre" or after "post".
81 $t invoke-event reset-assert-pre
84 # C code needs to know if we expect to 'halt'
85 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
86 $t arp_reset assert $halt
90 $t invoke-event reset-assert-post
93 # Now de-assert SRST, and report the pre/post events.
94 # Note: no target sees !SRST before "pre" or after "post".
96 $t invoke-event reset-deassert-pre
99 # Again, de-assert code needs to know if we 'halt'
100 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
101 $t arp_reset deassert $halt
105 $t invoke-event reset-deassert-post
108 # Pass 1 - Now wait for any halt (requested as part of reset
109 # assert/deassert) to happen. Ideally it takes effect without
110 # first executing any instructions.
113 if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
117 # don't wait for targets where examination is deferred
118 # they can not be halted anyway at this point
119 if { ![$t was_examined] && [$t examine_deferred] } {
123 # Wait up to 1 second for target to halt. Why 1sec? Cause
124 # the JTAG tap reset signal might be hooked to a slow
125 # resistor/capacitor circuit - and it might take a while
128 # Catch, but ignore any errors.
129 catch { $t arp_waitstate halted 1000 }
134 if { $s != "halted" } {
135 return -code error [format "TARGET: %s - Not halted" $t]
140 #Pass 2 - if needed "init"
141 if { $MODE == "init" } {
143 if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
147 # don't wait for targets where examination is deferred
148 # they can not be halted anyway at this point
149 if { ![$t was_examined] && [$t examine_deferred] } {
153 set err [catch "$t arp_waitstate halted 5000"]
156 $t invoke-event reset-init
162 $t invoke-event reset-end
167 set _TRANSPORT [ transport select ]
168 expr { [ string first "jtag" $_TRANSPORT ] != -1 }
172 set _TRANSPORT [ transport select ]
173 expr { [ string first "swd" $_TRANSPORT ] != -1 }
177 set _TRANSPORT [ transport select ]
178 expr { [ string first "hla" $_TRANSPORT ] != -1 }
183 # Target/chain configuration scripts can either execute commands directly
184 # or define a procedure which is executed once all configuration
185 # scripts have completed.
187 # By default(classic) the config scripts will set up the target configuration
188 proc init_targets {} {
191 proc set_default_target_event {t e s} {
192 if {[$t cget -event $e] == ""} {
193 $t configure -event $e $s
197 proc init_target_events {} {
198 set targets [target names]
201 set_default_target_event $t gdb-flash-erase-start "reset init"
202 set_default_target_event $t gdb-flash-write-end "reset halt"
203 set_default_target_event $t gdb-attach "halt 1000"
207 # Additionally board config scripts can define a procedure init_board that will be executed after init and init_targets
211 proc mem2array {arrayname bitwidth address count {phys ""}} {
212 echo "DEPRECATED! use 'read_memory' not 'mem2array'"
214 upvar $arrayname $arrayname
218 foreach elem [read_memory $address $bitwidth $count {*}$phys] {
219 set ${arrayname}($i) $elem
224 proc array2mem {arrayname bitwidth address count {phys ""}} {
225 echo "DEPRECATED! use 'write_memory' not 'array2mem'"
227 upvar $arrayname $arrayname
230 for {set i 0} {$i < $count} {incr i} {
231 lappend data [expr $${arrayname}($i)]
234 write_memory $address $bitwidth $data {*}$phys
237 # smp_on/smp_off were already DEPRECATED in v0.11.0 through http://openocd.zylin.com/4615
238 lappend _telnet_autocomplete_skip "aarch64 smp_on"
239 proc "aarch64 smp_on" {args} {
240 echo "DEPRECATED! use 'aarch64 smp on' not 'aarch64 smp_on'"
241 eval aarch64 smp on $args
244 lappend _telnet_autocomplete_skip "aarch64 smp_off"
245 proc "aarch64 smp_off" {args} {
246 echo "DEPRECATED! use 'aarch64 smp off' not 'aarch64 smp_off'"
247 eval aarch64 smp off $args
250 lappend _telnet_autocomplete_skip "cortex_a smp_on"
251 proc "cortex_a smp_on" {args} {
252 echo "DEPRECATED! use 'cortex_a smp on' not 'cortex_a smp_on'"
253 eval cortex_a smp on $args
256 lappend _telnet_autocomplete_skip "cortex_a smp_off"
257 proc "cortex_a smp_off" {args} {
258 echo "DEPRECATED! use 'cortex_a smp off' not 'cortex_a smp_off'"
259 eval cortex_a smp off $args
262 lappend _telnet_autocomplete_skip "mips_m4k smp_on"
263 proc "mips_m4k smp_on" {args} {
264 echo "DEPRECATED! use 'mips_m4k smp on' not 'mips_m4k smp_on'"
265 eval mips_m4k smp on $args
268 lappend _telnet_autocomplete_skip "mips_m4k smp_off"
269 proc "mips_m4k smp_off" {args} {
270 echo "DEPRECATED! use 'mips_m4k smp off' not 'mips_m4k smp_off'"
271 eval mips_m4k smp off $args