tcl: introduce using_(jtag|swd|hla) helpers and use them in reset handler
[openocd.git] / 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"]
69 if { $err == 0 } {
70 $t invoke-event examine-end
71 }
72 }
73 }
74
75 # Assert SRST, and report the pre/post events.
76 # Note: no target sees SRST before "pre" or after "post".
77 foreach t $targets {
78 $t invoke-event reset-assert-pre
79 }
80 foreach t $targets {
81 # C code needs to know if we expect to 'halt'
82 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
83 $t arp_reset assert $halt
84 }
85 }
86 foreach t $targets {
87 $t invoke-event reset-assert-post
88 }
89
90 # Now de-assert SRST, and report the pre/post events.
91 # Note: no target sees !SRST before "pre" or after "post".
92 foreach t $targets {
93 $t invoke-event reset-deassert-pre
94 }
95 foreach t $targets {
96 # Again, de-assert code needs to know if we 'halt'
97 if {![using_jtag] || [jtag tapisenabled [$t cget -chain-position]]} {
98 $t arp_reset deassert $halt
99 }
100 }
101 foreach t $targets {
102 $t invoke-event reset-deassert-post
103 }
104
105 # Pass 1 - Now wait for any halt (requested as part of reset
106 # assert/deassert) to happen. Ideally it takes effect without
107 # first executing any instructions.
108 if { $halt } {
109 foreach t $targets {
110 if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
111 continue
112 }
113
114 # Wait upto 1 second for target to halt. Why 1sec? Cause
115 # the JTAG tap reset signal might be hooked to a slow
116 # resistor/capacitor circuit - and it might take a while
117 # to charge
118
119 # Catch, but ignore any errors.
120 catch { $t arp_waitstate halted 1000 }
121
122 # Did we succeed?
123 set s [$t curstate]
124
125 if { 0 != [string compare $s "halted" ] } {
126 return -code error [format "TARGET: %s - Not halted" $t]
127 }
128 }
129 }
130
131 #Pass 2 - if needed "init"
132 if { 0 == [string compare init $MODE] } {
133 foreach t $targets {
134 if {[using_jtag] && ![jtag tapisenabled [$t cget -chain-position]]} {
135 continue
136 }
137
138 set err [catch "$t arp_waitstate halted 5000"]
139 # Did it halt?
140 if { $err == 0 } {
141 $t invoke-event reset-init
142 }
143 }
144 }
145
146 foreach t $targets {
147 $t invoke-event reset-end
148 }
149 }
150
151 proc using_jtag {} {
152 set _TRANSPORT [ transport select ]
153 expr { [ string first "jtag" $_TRANSPORT ] != -1 }
154 }
155
156 proc using_swd {} {
157 set _TRANSPORT [ transport select ]
158 expr { [ string first "swd" $_TRANSPORT ] != -1 }
159 }
160
161 proc using_hla {} {
162 set _TRANSPORT [ transport select ]
163 expr { [ string first "hla" $_TRANSPORT ] != -1 }
164 }
165
166 #########
167
168 # Temporary migration aid. May be removed starting in January 2011.
169 proc armv4_5 params {
170 echo "DEPRECATED! use 'arm $params' not 'armv4_5 $params'"
171 arm $params
172 }
173
174 # Target/chain configuration scripts can either execute commands directly
175 # or define a procedure which is executed once all configuration
176 # scripts have completed.
177 #
178 # By default(classic) the config scripts will set up the target configuration
179 proc init_targets {} {
180 }
181
182 proc set_default_target_event {t e s} {
183 if {[$t cget -event $e] == ""} {
184 $t configure -event $e $s
185 }
186 }
187
188 proc init_target_events {} {
189 set targets [target names]
190
191 foreach t $targets {
192 set_default_target_event $t gdb-flash-erase-start "reset init"
193 set_default_target_event $t gdb-flash-write-end "reset halt"
194 }
195 }
196
197 # Additionally board config scripts can define a procedure init_board that will be executed after init and init_targets
198 proc init_board {} {
199 }
200
201 # deprecated target name cmds
202 proc cortex_m3 args {
203 echo "DEPRECATED! use 'cortex_m' not 'cortex_m3'"
204 eval cortex_m $args
205 }
206
207 proc cortex_a8 args {
208 echo "DEPRECATED! use 'cortex_a' not 'cortex_a8'"
209 eval cortex_a $args
210 }