You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

306 lines
7.8 KiB

  1. #
  2. # Defines basic Tcl procs that must be there for
  3. # OpenOCD to work.
  4. #
  5. # Embedded into OpenOCD executable
  6. #
  7. # Help text list. A list of command + help text pairs.
  8. #
  9. # Commands can be more than one word and they are stored
  10. # as "flash banks" "help text x x x"
  11. proc add_help_text {cmd cmd_help} {
  12. global ocd_helptext
  13. lappend ocd_helptext [list $cmd $cmd_help]
  14. }
  15. proc get_help_text {} {
  16. global ocd_helptext
  17. return $ocd_helptext
  18. }
  19. # Show flash in human readable form
  20. # This is an example of a human readable form of a low level fn
  21. proc flash_banks {} {
  22. set i 0
  23. set result ""
  24. foreach {a} [ocd_flash_banks] {
  25. if {$i > 0} {
  26. set result "$result\n"
  27. }
  28. set result [format "$result#%d: %s at 0x%08x, size 0x%08x, buswidth %d, chipwidth %d" $i $a(name) $a(base) $a(size) $a(bus_width) $a(chip_width)]
  29. set i [expr $i+1]
  30. }
  31. return $result
  32. }
  33. # We need to explicitly redirect this to the OpenOCD command
  34. # as Tcl defines the exit proc
  35. proc exit {} {
  36. ocd_throw exit
  37. }
  38. #Print help text for a command. Word wrap
  39. #help text that is too wide inside column.
  40. proc help {args} {
  41. global ocd_helptext
  42. set cmd $args
  43. foreach a [lsort $ocd_helptext] {
  44. if {[string length $cmd]==0||[string first $cmd $a]!=-1||[string first $cmd [lindex $a 1]]!=-1} {
  45. set w 50
  46. set cmdname [lindex $a 0]
  47. set h [lindex $a 1]
  48. set n 0
  49. while 1 {
  50. if {$n > [string length $h]} {break}
  51. set next_a [expr $n+$w]
  52. if {[string length $h]>$n+$w} {
  53. set xxxx [string range $h $n [expr $n+$w]]
  54. for {set lastpos [expr [string length $xxxx]-1]} {$lastpos>=0&&[string compare [string range $xxxx $lastpos $lastpos] " "]!=0} {set lastpos [expr $lastpos-1]} {
  55. }
  56. #set next_a -1
  57. if {$lastpos!=-1} {
  58. set next_a [expr $lastpos+$n+1]
  59. }
  60. }
  61. puts [format "%-25s %s" $cmdname [string range $h $n [expr $next_a-1]] ]
  62. set cmdname ""
  63. set n [expr $next_a]
  64. }
  65. }
  66. }
  67. }
  68. add_help_text help "Tcl implementation of help command"
  69. # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
  70. #
  71. # We also support two level commands. "flash banks" is translated to
  72. # flash_banks
  73. proc unknown {args} {
  74. # do the name mangling from "flash banks" to "flash_banks"
  75. if {[llength $args]>=2} {
  76. set cmd_name "[lindex $args 0]_[lindex $args 1]"
  77. # Fix?? add a check here if this is a command?
  78. # we'll strip away args until we fail anyway...
  79. return [eval "$cmd_name [lrange $args 2 end]"]
  80. }
  81. # This really is an unknown command.
  82. return -code error "Unknown command: $args"
  83. }
  84. proc new_target_name { } {
  85. return [target number [expr [target count] - 1 ]]
  86. }
  87. # Try flipping / and \ to find file if the filename does not
  88. # match the precise spelling
  89. proc find {filename} {
  90. if {[catch {ocd_find $filename} t]==0} {
  91. return $t
  92. }
  93. if {[catch {ocd_find [string map {\ /} $filename} t]==0} {
  94. return $t
  95. }
  96. if {[catch {ocd_find [string map {/ \\} $filename} t]==0} {
  97. return $t
  98. }
  99. # make sure error message matches original input string
  100. return -code error "Can't find $filename"
  101. }
  102. add_help_text find "<file> - print full path to file according to OpenOCD search rules"
  103. # Run script
  104. proc script {filename} {
  105. source [find $filename]
  106. }
  107. #proc daemon_reset {} {
  108. # puts "Daemon reset is obsolete. Use -c init -c \"reset halt\" at end of openocd command line instead");
  109. #}
  110. add_help_text script "<filename> - filename of OpenOCD script (tcl) to run"
  111. # Handle GDB 'R' packet. Can be overriden by configuration script,
  112. # but it's not something one would expect target scripts to do
  113. # normally
  114. proc ocd_gdb_restart {target_num} {
  115. # Fix!!! we're resetting all targets here! Really we should reset only
  116. # one target
  117. reset halt
  118. }
  119. # If RCLK is not supported, use fallback_speed_khz
  120. proc jtag_rclk {fallback_speed_khz} {
  121. if {[catch {jtag_khz 0}]!=0} {
  122. jtag_khz $fallback_speed_khz
  123. }
  124. }
  125. add_help_text jtag_rclk "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed"
  126. proc ocd_process_reset { MODE } {
  127. # If this target must be halted...
  128. set halt -1
  129. if { 0 == [string compare $MODE halt] } {
  130. set halt 1
  131. }
  132. if { 0 == [string compare $MODE init] } {
  133. set halt 1;
  134. }
  135. if { 0 == [string compare $MODE run ] } {
  136. set halt 0;
  137. }
  138. if { $halt < 0 } {
  139. return -error "Invalid mode: $MODE, must be one of: halt, init, or run";
  140. }
  141. foreach t [ target names ] {
  142. # New event script.
  143. $t invoke-event reset-start
  144. }
  145. # Init the tap controller.
  146. jtag arp_init-reset
  147. # Examine all targets.
  148. foreach t [ target names ] {
  149. $t arp_examine
  150. }
  151. # Let the C code know we are asserting reset.
  152. foreach t [ target names ] {
  153. $t invoke-event reset-assert-pre
  154. # C code needs to know if we expect to 'halt'
  155. $t arp_reset assert $halt
  156. $t invoke-event reset-assert-post
  157. }
  158. # Now de-assert reset.
  159. foreach t [ target names ] {
  160. $t invoke-event reset-deassert-pre
  161. # Again, de-assert code needs to know..
  162. $t arp_reset deassert $halt
  163. $t invoke-event reset-deassert-post
  164. }
  165. # Pass 1 - Now try to halt.
  166. if { $halt } {
  167. foreach t [target names] {
  168. # Wait upto 1 second for target to halt. Why 1sec? Cause
  169. # the JTAG tap reset signal might be hooked to a slow
  170. # resistor/capacitor circuit - and it might take a while
  171. # to charge
  172. # Catch, but ignore any errors.
  173. catch { $t arp_waitstate halted 1000 }
  174. # Did we succeed?
  175. set s [$t curstate]
  176. if { 0 != [string compare $s "halted" ] } {
  177. return -error [format "TARGET: %s - Not halted" $t]
  178. }
  179. }
  180. }
  181. #Pass 2 - if needed "init"
  182. if { 0 == [string compare init $MODE] } {
  183. foreach t [target names] {
  184. set err [catch "$t arp_waitstate halted 5000"]
  185. # Did it halt?
  186. if { $err == 0 } {
  187. $t invoke-event reset-init
  188. }
  189. }
  190. }
  191. foreach t [ target names ] {
  192. $t invoke-event reset-end
  193. }
  194. }
  195. # stubs for targets scripts that do not have production procedure
  196. proc production_info {} {
  197. return "Imagine an explanation here..."
  198. }
  199. add_help_text production_info "Displays information on production procedure for target script. Implement this procedure in target script."
  200. proc production {firmwarefile serialnumber} {
  201. puts "Imagine production procedure running successfully. Programmed $firmwarefile with serial number $serialnumber"
  202. }
  203. add_help_text production "<serialnumber> - Runs production procedure. Throws exception if procedure failed. Prints progress messages. Implement this procedure in the target script."
  204. proc production_test {} {
  205. puts "Imagine nifty test procedure having run to completion here."
  206. }
  207. add_help_text production "Runs test procedure. Throws exception if procedure failed. Prints progress messages. Implement in target script."
  208. add_help_text cpu "<name> - prints out target options and a comment on CPU which matches name"
  209. # A list of names of CPU and options required
  210. set ocd_cpu_list {
  211. {
  212. name IXP42x
  213. options {xscale -variant IXP42x}
  214. comment {IXP42x cpu}
  215. }
  216. {
  217. name arm7
  218. options {arm7tdmi -variant arm7tdmi}
  219. comment {vanilla ARM7}
  220. }
  221. }
  222. # Invoked from Tcl code
  223. proc ocd_cpu {args} {
  224. set name $args
  225. set result ""
  226. global ocd_cpu_list
  227. foreach a [lsort $ocd_cpu_list] {
  228. if {[string length $args]==0||[string first [string toupper $name] [string toupper "$a(name)$a(options)$a(comment)"]]!=-1} {
  229. lappend result $a
  230. }
  231. }
  232. return $result
  233. }
  234. proc cpu {args} {
  235. # 0123456789012345678901234567890123456789012345678901234567890123456789
  236. puts "CPU Options Comment"
  237. foreach a [lsort [ocd_cpu $args]] {
  238. puts [format "%-20s%-40s%s" $a(name) $a(options) $a(comment)]
  239. }
  240. }
  241. proc power_restore {} {
  242. puts "Sensed power restore."
  243. reset init
  244. }
  245. add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
  246. proc power_dropout {} {
  247. puts "Sensed power dropout."
  248. }
  249. proc srst_deasserted {} {
  250. puts "Sensed nSRST deasserted."
  251. reset init
  252. }
  253. add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
  254. proc srst_asserted {} {
  255. puts "Sensed nSRST asserted."
  256. }