target: re-examine before arp_waitstate in ocd_process_reset_inner
[openocd.git] / src / target / startup.tcl
1 # SPDX-License-Identifier: GPL-2.0-or-later
2
3 # Defines basic Tcl procs for OpenOCD target module
4
5 proc new_target_name { } {
6 return [target number [expr {[target count] - 1}]]
7 }
8
9 global in_process_reset
10 set in_process_reset 0
11
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"
18 }
19
20 set in_process_reset 1
21 set success [expr {[catch {ocd_process_reset_inner $MODE} result] == 0}]
22 set in_process_reset 0
23
24 if {$success} {
25 return $result
26 } else {
27 return -code error $result
28 }
29 }
30
31 proc ocd_process_reset_inner { MODE } {
32 set targets [target names]
33
34 # If this target must be halted...
35 switch $MODE {
36 halt -
37 init {
38 set halt 1
39 }
40 run {
41 set halt 0
42 }
43 default {
44 return -code error "Invalid mode: $MODE, must be one of: halt, init, or run";
45 }
46 }
47
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.
52 #
53 # NOTE: ARP == "Advanced Reset Process" ... "advanced" is
54 # relative to a previous restrictive scheme
55
56 foreach t $targets {
57 # New event script.
58 $t invoke-event reset-start
59 }
60
61 # Use TRST or TMS/TCK operations to reset all the tap controllers.
62 # TAP reset events get reported; they might enable some taps.
63 init_reset $MODE
64
65 # Examine all targets on enabled taps.
66 foreach t $targets {
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"]
70 if { $err } {
71 $t invoke-event examine-fail
72 } else {
73 $t invoke-event examine-end
74 }
75 }
76 }
77
78 # Assert SRST, and report the pre/post events.
79 # Note: no target sees SRST before "pre" or after "post".
80 foreach t $targets {
81 $t invoke-event reset-assert-pre
82 }
83 foreach t $targets {
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
87 }
88 }
89 foreach t $targets {
90 $t invoke-event reset-assert-post
91 }
92
93 # Now de-assert SRST, and report the pre/post events.
94 # Note: no target sees !SRST before "pre" or after "post".
95 foreach t $targets {
96 $t invoke-event reset-deassert-pre
97 }
98 foreach t $targets {
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
102 }
103 }
104 foreach t $targets {
105 $t invoke-event reset-deassert-post
106 }
107
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.
111 if { $halt } {
112 foreach t $targets {
113 if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
114 continue
115 }
116
117 if { ![$t was_examined] } {
118 # don't wait for targets where examination is deferred
119 # they can not be halted anyway at this point
120 if { [$t examine_deferred] } {
121 continue
122 }
123 # try to re-examine or target state will be unknown
124 $t invoke-event examine-start
125 set err [catch "$t arp_examine allow-defer"]
126 if { $err } {
127 $t invoke-event examine-fail
128 return -code error [format "TARGET: %s - Not examined" $t]
129 } else {
130 $t invoke-event examine-end
131 }
132 }
133
134 # Wait up to 1 second for target to halt. Why 1sec? Cause
135 # the JTAG tap reset signal might be hooked to a slow
136 # resistor/capacitor circuit - and it might take a while
137 # to charge
138
139 # Catch, but ignore any errors.
140 catch { $t arp_waitstate halted 1000 }
141
142 # Did we succeed?
143 set s [$t curstate]
144
145 if { $s != "halted" } {
146 return -code error [format "TARGET: %s - Not halted" $t]
147 }
148 }
149 }
150
151 #Pass 2 - if needed "init"
152 if { $MODE == "init" } {
153 foreach t $targets {
154 if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
155 continue
156 }
157
158 # don't wait for targets where examination is deferred
159 # they can not be halted anyway at this point
160 if { ![$t was_examined] && [$t examine_deferred] } {
161 continue
162 }
163
164 set err [catch "$t arp_waitstate halted 5000"]
165 # Did it halt?
166 if { $err == 0 } {
167 $t invoke-event reset-init
168 }
169 }
170 }
171
172 foreach t $targets {
173 $t invoke-event reset-end
174 }
175 }
176
177 proc using_jtag {} {
178 set _TRANSPORT [ transport select ]
179 expr { [ string first "jtag" $_TRANSPORT ] != -1 }
180 }
181
182 proc using_swd {} {
183 set _TRANSPORT [ transport select ]
184 expr { [ string first "swd" $_TRANSPORT ] != -1 }
185 }
186
187 proc using_hla {} {
188 set _TRANSPORT [ transport select ]
189 expr { [ string first "hla" $_TRANSPORT ] != -1 }
190 }
191
192 #########
193
194 # Target/chain configuration scripts can either execute commands directly
195 # or define a procedure which is executed once all configuration
196 # scripts have completed.
197 #
198 # By default(classic) the config scripts will set up the target configuration
199 proc init_targets {} {
200 }
201
202 proc set_default_target_event {t e s} {
203 if {[$t cget -event $e] == ""} {
204 $t configure -event $e $s
205 }
206 }
207
208 proc init_target_events {} {
209 set targets [target names]
210
211 foreach t $targets {
212 set_default_target_event $t gdb-flash-erase-start "reset init"
213 set_default_target_event $t gdb-flash-write-end "reset halt"
214 set_default_target_event $t gdb-attach "halt 1000"
215 }
216 }
217
218 # Additionally board config scripts can define a procedure init_board that will be executed after init and init_targets
219 proc init_board {} {
220 }
221
222 proc mem2array {arrayname bitwidth address count {phys ""}} {
223 echo "DEPRECATED! use 'read_memory' not 'mem2array'"
224
225 upvar $arrayname $arrayname
226 set $arrayname ""
227 set i 0
228
229 foreach elem [read_memory $address $bitwidth $count {*}$phys] {
230 set ${arrayname}($i) $elem
231 incr i
232 }
233 }
234
235 proc array2mem {arrayname bitwidth address count {phys ""}} {
236 echo "DEPRECATED! use 'write_memory' not 'array2mem'"
237
238 upvar $arrayname $arrayname
239 set data ""
240
241 for {set i 0} {$i < $count} {incr i} {
242 lappend data [expr $${arrayname}($i)]
243 }
244
245 write_memory $address $bitwidth $data {*}$phys
246 }
247
248 # smp_on/smp_off were already DEPRECATED in v0.11.0 through http://openocd.zylin.com/4615
249 lappend _telnet_autocomplete_skip "aarch64 smp_on"
250 proc "aarch64 smp_on" {args} {
251 echo "DEPRECATED! use 'aarch64 smp on' not 'aarch64 smp_on'"
252 eval aarch64 smp on $args
253 }
254
255 lappend _telnet_autocomplete_skip "aarch64 smp_off"
256 proc "aarch64 smp_off" {args} {
257 echo "DEPRECATED! use 'aarch64 smp off' not 'aarch64 smp_off'"
258 eval aarch64 smp off $args
259 }
260
261 lappend _telnet_autocomplete_skip "cortex_a smp_on"
262 proc "cortex_a smp_on" {args} {
263 echo "DEPRECATED! use 'cortex_a smp on' not 'cortex_a smp_on'"
264 eval cortex_a smp on $args
265 }
266
267 lappend _telnet_autocomplete_skip "cortex_a smp_off"
268 proc "cortex_a smp_off" {args} {
269 echo "DEPRECATED! use 'cortex_a smp off' not 'cortex_a smp_off'"
270 eval cortex_a smp off $args
271 }
272
273 lappend _telnet_autocomplete_skip "mips_m4k smp_on"
274 proc "mips_m4k smp_on" {args} {
275 echo "DEPRECATED! use 'mips_m4k smp on' not 'mips_m4k smp_on'"
276 eval mips_m4k smp on $args
277 }
278
279 lappend _telnet_autocomplete_skip "mips_m4k smp_off"
280 proc "mips_m4k smp_off" {args} {
281 echo "DEPRECATED! use 'mips_m4k smp off' not 'mips_m4k smp_off'"
282 eval mips_m4k smp off $args
283 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)