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