cd98d68e44356d47d432ff173794ec63e7a17797
[fw/openocd] / src / target / startup.tcl
1 # Defines basic Tcl procs for OpenOCD target module
2
3 proc new_target_name { } {
4         return [target number [expr {[target count] - 1}]]
5 }
6
7 global in_process_reset
8 set in_process_reset 0
9
10 # Catch reset recursion
11 proc ocd_process_reset { MODE } {
12         global in_process_reset
13         if {$in_process_reset} {
14                 set in_process_reset 0
15                 return -code error "'reset' can not be invoked recursively"
16         }
17
18         set in_process_reset 1
19         set success [expr {[catch {ocd_process_reset_inner $MODE} result] == 0}]
20         set in_process_reset 0
21
22         if {$success} {
23                 return $result
24         } else {
25                 return -code error $result
26         }
27 }
28
29 proc ocd_process_reset_inner { MODE } {
30         set targets [target names]
31
32         # If this target must be halted...
33         switch $MODE {
34                 halt -
35                 init {
36                         set halt 1
37                 }
38                 run {
39                         set halt 0
40                 }
41                 default {
42                         return -code error "Invalid mode: $MODE, must be one of: halt, init, or run";
43                 }
44         }
45
46         # Target event handlers *might* change which TAPs are enabled
47         # or disabled, so we fire all of them.  But don't issue any
48         # target "arp_*" commands, which may issue JTAG transactions,
49         # unless we know the underlying TAP is active.
50         #
51         # NOTE:  ARP == "Advanced Reset Process" ... "advanced" is
52         # relative to a previous restrictive scheme
53
54         foreach t $targets {
55                 # New event script.
56                 $t invoke-event reset-start
57         }
58
59         # Use TRST or TMS/TCK operations to reset all the tap controllers.
60         # TAP reset events get reported; they might enable some taps.
61         init_reset $MODE
62
63         # Examine all targets on enabled taps.
64         foreach t $targets {
65                 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
66                         $t invoke-event examine-start
67                         set err [catch "$t arp_examine allow-defer"]
68                         if { $err } {
69                                 $t invoke-event examine-fail
70                         } else {
71                                 $t invoke-event examine-end
72                         }
73                 }
74         }
75
76         # Assert SRST, and report the pre/post events.
77         # Note:  no target sees SRST before "pre" or after "post".
78         foreach t $targets {
79                 $t invoke-event reset-assert-pre
80         }
81         foreach t $targets {
82                 # C code needs to know if we expect to 'halt'
83                 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
84                         $t arp_reset assert $halt
85                 }
86         }
87         foreach t $targets {
88                 $t invoke-event reset-assert-post
89         }
90
91         # Now de-assert SRST, and report the pre/post events.
92         # Note:  no target sees !SRST before "pre" or after "post".
93         foreach t $targets {
94                 $t invoke-event reset-deassert-pre
95         }
96         foreach t $targets {
97                 # Again, de-assert code needs to know if we 'halt'
98                 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
99                         $t arp_reset deassert $halt
100                 }
101         }
102         foreach t $targets {
103                 $t invoke-event reset-deassert-post
104         }
105
106         # Pass 1 - Now wait for any halt (requested as part of reset
107         # assert/deassert) to happen.  Ideally it takes effect without
108         # first executing any instructions.
109         if { $halt } {
110                 foreach t $targets {
111                         if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
112                                 continue
113                         }
114
115                         # don't wait for targets where examination is deferred
116                         # they can not be halted anyway at this point
117                         if { ![$t was_examined] && [$t examine_deferred] } {
118                                 continue
119                         }
120
121                         # Wait up to 1 second for target to halt. Why 1sec? Cause
122                         # the JTAG tap reset signal might be hooked to a slow
123                         # resistor/capacitor circuit - and it might take a while
124                         # to charge
125
126                         # Catch, but ignore any errors.
127                         catch { $t arp_waitstate halted 1000 }
128
129                         # Did we succeed?
130                         set s [$t curstate]
131
132                         if { $s != "halted" } {
133                                 return -code error [format "TARGET: %s - Not halted" $t]
134                         }
135                 }
136         }
137
138         #Pass 2 - if needed "init"
139         if { $MODE == "init" } {
140                 foreach t $targets {
141                         if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
142                                 continue
143                         }
144
145                         # don't wait for targets where examination is deferred
146                         # they can not be halted anyway at this point
147                         if { ![$t was_examined] && [$t examine_deferred] } {
148                                 continue
149                         }
150
151                         set err [catch "$t arp_waitstate halted 5000"]
152                         # Did it halt?
153                         if { $err == 0 } {
154                                 $t invoke-event reset-init
155                         }
156                 }
157         }
158
159         foreach t $targets {
160                 $t invoke-event reset-end
161         }
162 }
163
164 proc using_jtag {} {
165         set _TRANSPORT [ transport select ]
166         expr { [ string first "jtag" $_TRANSPORT ] != -1 }
167 }
168
169 proc using_swd {} {
170         set _TRANSPORT [ transport select ]
171         expr { [ string first "swd" $_TRANSPORT ] != -1 }
172 }
173
174 proc using_hla {} {
175         set _TRANSPORT [ transport select ]
176         expr { [ string first "hla" $_TRANSPORT ] != -1 }
177 }
178
179 #########
180
181 # Target/chain configuration scripts can either execute commands directly
182 # or define a procedure which is executed once all configuration
183 # scripts have completed.
184 #
185 # By default(classic) the config scripts will set up the target configuration
186 proc init_targets {} {
187 }
188
189 proc set_default_target_event {t e s} {
190         if {[$t cget -event $e] == ""} {
191                 $t configure -event $e $s
192         }
193 }
194
195 proc init_target_events {} {
196         set targets [target names]
197
198         foreach t $targets {
199                 set_default_target_event $t gdb-flash-erase-start "reset init"
200                 set_default_target_event $t gdb-flash-write-end "reset halt"
201                 set_default_target_event $t gdb-attach "halt 1000"
202         }
203 }
204
205 # Additionally board config scripts can define a procedure init_board that will be executed after init and init_targets
206 proc init_board {} {
207 }
208
209 # smp_on/smp_off were already DEPRECATED in v0.11.0 through http://openocd.zylin.com/4615
210 lappend _telnet_autocomplete_skip "aarch64 smp_on"
211 proc "aarch64 smp_on" {args} {
212         echo "DEPRECATED! use 'aarch64 smp on' not 'aarch64 smp_on'"
213         eval aarch64 smp on $args
214 }
215
216 lappend _telnet_autocomplete_skip "aarch64 smp_off"
217 proc "aarch64 smp_off" {args} {
218         echo "DEPRECATED! use 'aarch64 smp off' not 'aarch64 smp_off'"
219         eval aarch64 smp off $args
220 }
221
222 lappend _telnet_autocomplete_skip "cortex_a smp_on"
223 proc "cortex_a smp_on" {args} {
224         echo "DEPRECATED! use 'cortex_a smp on' not 'cortex_a smp_on'"
225         eval cortex_a smp on $args
226 }
227
228 lappend _telnet_autocomplete_skip "cortex_a smp_off"
229 proc "cortex_a smp_off" {args} {
230         echo "DEPRECATED! use 'cortex_a smp off' not 'cortex_a smp_off'"
231         eval cortex_a smp off $args
232 }
233
234 lappend _telnet_autocomplete_skip "mips_m4k smp_on"
235 proc "mips_m4k smp_on" {args} {
236         echo "DEPRECATED! use 'mips_m4k smp on' not 'mips_m4k smp_on'"
237         eval mips_m4k smp on $args
238 }
239
240 lappend _telnet_autocomplete_skip "mips_m4k smp_off"
241 proc "mips_m4k smp_off" {args} {
242         echo "DEPRECATED! use 'mips_m4k smp off' not 'mips_m4k smp_off'"
243         eval mips_m4k smp off $args
244 }