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.
 
 
 
 
 
 

441 lines
12 KiB

  1. /** @page primertcl OpenOCD TCL Primer
  2. The @subpage scripting page provides additional TCL Primer material.
  3. @verbatim
  4. ****************************************
  5. ****************************************
  6. This is a short introduction to 'un-scare' you about the language
  7. known as TCL. It is structured as a guided tour through the files
  8. written by me [Duane Ellis] - in early July 2008 for OpenOCD.
  9. Which uses the "JIM" embedded Tcl clone-ish language.
  10. Thing described here are *totally* TCL generic... not Jim specific.
  11. The goal of this document is to encourage you to add your own set of
  12. chips to the TCL package - and most importantly you should know where
  13. you should put them - so they end up in an organized way.
  14. --Duane Ellis.
  15. duane@duaneellis.com
  16. ****************************************
  17. ****************************************
  18. Adding "chip" support - Duane Ellis July 5 - 2008.
  19. The concept is this:
  20. In your "openocd.cfg" file add something like this:
  21. source [find tcl/chip/VENDOR/FAMILY/NAME.tcl]
  22. For example...
  23. source [find tcl/chip/atmel/at91/at91sam7x256.tcl]
  24. You'll notice that it makes use of:
  25. tcl/cpu/arm/<NAME>.tcl.
  26. Yes, that is where you should put "core" specific things.
  27. Be careful and learn the difference:
  28. THE "CORE" - is not the entire chip!
  29. Definition:
  30. That "file" listed above is called a "CHIP FILE".
  31. It may be standalone, or may need to "source" other "helper" files.
  32. The reference [7/5/2008] is the at91sam7x256.tcl file.
  33. ****************************************
  34. ****************************************
  35. === TCL TOUR ===
  36. Open: at91sam7x256.tcl
  37. === TCL TOUR ===
  38. A walk through --- For those who are new to TCL.
  39. Examine the file: at91sam7x256.tcl
  40. It starts with:
  41. source [find path/filename.tcl]
  42. In TCL - this is very important.
  43. Rule #1 Everything is a string.
  44. Rule #2 If you think other wise See #1.
  45. Reminds you of:
  46. Rule #1: The wife is correct.
  47. Rule #2: If you think otherwise, See #1
  48. Any text contained inside of [square-brackets]
  49. is just like `back-ticks` in BASH.
  50. Hence, the [find FILENAME] executes the command find with a single
  51. parameter the filename.
  52. ========================================
  53. Next you see a series of:
  54. set NAME VALUE
  55. It is mostly "obvious" what is going on.
  56. Exception: The arrays.
  57. You would *THINK* Tcl supports arrays.
  58. In fact, multi-dim arrays. That is false.
  59. For the index for"FLASH(0,CHIPSELECT)" is actually the string
  60. "0,CHIPSELECT". This is problematic. In the normal world, you think
  61. of array indexes as integers.
  62. For example these are different:
  63. set foo(0x0c) 123
  64. set foo(12) 444
  65. Why? Because 0x0c {lowercase} is a string.
  66. Don't forget UPPER CASE.
  67. You must be careful - always... always... use simple decimal
  68. numbers. When in doubt use 'expr' the evaluator. These are all the
  69. same.
  70. set x 0x0c
  71. set foo([expr $x]) "twelve"
  72. set x 12
  73. set foo([expr $x]) "twelve"
  74. set x "2 * 6"
  75. set foo([expr $x]) "twelve"
  76. **************************************************
  77. ***************************************************
  78. === TCL TOUR ===
  79. Open the file: "bitsbytes.tcl"
  80. There is some tricky things going on.
  81. ===============
  82. First, there is a "for" loop - at level 0
  83. {level 0 means: out side of a proc/function}
  84. This means it is evaluated when the file is parsed.
  85. == SIDEBAR: About The FOR command ==
  86. In TCL, "FOR" is a funny thing, it is not what you think it is.
  87. Syntactically - FOR is a just a command, it is not language
  88. construct like for(;;) in C...
  89. The "for" command takes 4 parameters.
  90. (1) The "initial command" to execute.
  91. (2) the test "expression"
  92. (3) the "next command"
  93. (4) the "body command" of the FOR loop.
  94. Notice I used the words "command" and "expression" above.
  95. The FOR command:
  96. 1) executes the "initial command"
  97. 2) evaluates the expression if 0 it stops.
  98. 3) executes the "body command"
  99. 4) executes the "next command"
  100. 5) Goto Step 2.
  101. As show, each of these items are in {curly-braces}. This means they
  102. are passed as they are - KEY-POINT: un evaluated to the FOR
  103. command. Think of it like escaping the backticks in Bash so that the
  104. "under-lying" command can evaluate the contents. In this case, the FOR
  105. COMMAND.
  106. == END: SIDEBAR: About The FOR command ==
  107. You'll see two lines:
  108. LINE1:
  109. set vn [format "BIT%d" $x]
  110. Format is like "sprintf". Because of the [brackets], it becomes what
  111. you think. But here's how:
  112. First - the line is parsed - for {braces}. In this case, there are
  113. none. The, the parser looks for [brackets] and finds them. The,
  114. parser then evaluates the contents of the [brackets], and replaces
  115. them. It is alot this bash statement.
  116. EXPORT vn=`date`
  117. LINE 2 & 3
  118. set $vn [expr (1024 * $x)]
  119. global $vn
  120. In line 1, we dynamically created a variable name. Here, we are
  121. assigning it a value. Lastly Line 3 we force the variable to be
  122. global, not "local" the the "for command body"
  123. ===============
  124. The PROCS
  125. proc create_mask { MSB LSB } {
  126. ... body ....
  127. }
  128. Like "for" - PROC is really just a command that takes 3 parameters.
  129. The (1) NAME of the function, a (2) LIST of parameters, and a (3) BODY
  130. Again, this is at "level 0" so it is a global function. (Yes, TCL
  131. supports local functions, you put them inside of a function}
  132. You'll see in some cases, I nest [brackets] alot and in others I'm
  133. lazy or wanted it to be more clear... it is a matter of choice.
  134. ===============
  135. **************************************************
  136. ***************************************************
  137. === TCL TOUR ===
  138. Open the file: "memory.tcl"
  139. ===============
  140. Here is where I setup some 'memory definitions' that various targets can use.
  141. For example - there is an "unknown" memory region.
  142. All memory regions must have 2 things:
  143. (1) N_<name>
  144. (2) NAME( array )
  145. And the array must have some specific names:
  146. ( <idx>, THING )
  147. Where: THING is one of:
  148. CHIPSELECT
  149. BASE
  150. LEN
  151. HUMAN
  152. TYPE
  153. RWX - the access ability.
  154. WIDTH - the accessible width.
  155. ie: Some regions of memory are not 'word'
  156. accessible.
  157. The function "address_info" - given an address should
  158. tell you about the address.
  159. [as of this writing: 7/5/2008 I have done
  160. only a little bit with this -Duane]
  161. ===
  162. MAJOR FUNCTION:
  163. ==
  164. proc memread32 { ADDR }
  165. proc memread16 { ADDR }
  166. proc memread8 { ADDR }
  167. All read memory - and return the contents.
  168. [ FIXME: 7/5/2008 - I need to create "memwrite" functions]
  169. **************************************************
  170. ***************************************************
  171. === TCL TOUR ===
  172. Open the file: "mmr_helpers.tcl"
  173. ===============
  174. This file is used to display and work with "memory mapped registers"
  175. For example - 'show_mmr32_reg' is given the NAME of the register to
  176. display. The assumption is - the NAME is a global variable holding the
  177. address of that MMR.
  178. The code does some tricks. The [set [set NAME]] is the TCL way
  179. of doing double variable interpolation - like makefiles...
  180. In a makefile or shell script you may have seen this:
  181. FOO_linux = "Penguins rule"
  182. FOO_winXP = "Broken Glass"
  183. FOO_mac = "I like cat names"
  184. # Pick one
  185. BUILD = linux
  186. #BUILD = winXP
  187. #BUILD = mac
  188. FOO = ${FOO_${BUILD}}
  189. The "double [set] square bracket" thing is the TCL way, nothing more.
  190. ----
  191. The IF statement - and "CATCH" .
  192. Notice this IF COMMAND - (not statement) is like this:
  193. [7/5/2008 it is this way]
  194. if ![catch { command } msg ] {
  195. ...something...
  196. } else {
  197. error [format string...]
  198. }
  199. The "IF" command expects either 2 params, or 4 params.
  200. === Sidebar: About "commands" ===
  201. Take a look at the internals of "jim.c"
  202. Look for the function: Jim_IfCoreCommand()
  203. And all those other "CoreCommands"
  204. You'll notice - they all have "argc" and "argv"
  205. Yea, the entire thing is done that way.
  206. IF is a command. SO is "FOR" and "WHILE" and "DO" and the
  207. others. That is why I keep using the phase it is a "command"
  208. === END: Sidebar: About "commands" ===
  209. Parameter 1 to the IF command is expected to be an expression.
  210. As such, I do not need to wrap it in {braces}.
  211. In this case, the "expression" is the result of the "CATCH" command.
  212. CATCH - is an error catcher.
  213. You give CATCH 1 or 2 parameters.
  214. The first 1st parameter is the "code to execute"
  215. The 2nd (optional) is where to put the error message.
  216. CATCH returns 0 on success, 1 for failure.
  217. The "![catch command]" is self explaintory.
  218. The 3rd parameter to IF must be exactly "else" or "elseif" [I lied
  219. above, the IF command can take many parameters they just have to
  220. be joined by exactly the words "else" or "elseif".
  221. The 4th parameter contains:
  222. "error [format STRING....]"
  223. This lets me modify the previous lower level error by tacking more
  224. text onto the end of it. In this case, i want to add the MMR register
  225. name to make my error message look better.
  226. ---------
  227. Back to something inside show_mmr32_reg{}.
  228. You'll see something 'set fn show_${NAME}_helper' Here I am
  229. constructing a 'function name' Then - I look it up to see if it
  230. exists. {the function: "proc_exists" does this}
  231. And - if it does - I call the function.
  232. In "C" it is alot like using: 'sprintf()' to construct a function name
  233. string, then using "dlopen()" and "dlsym()" to look it up - and get a
  234. function pointer - and calling the function pointer.
  235. In this case - I execute a dynamic command. You can do some cool
  236. tricks with interpretors.
  237. ----------
  238. Function: show_mmr32_bits()
  239. In this case, we use the special TCL command "upvar" which tcl's way
  240. of passing things by reference. In this case, we want to reach up into
  241. the callers lexical scope and find the array named "NAMES"
  242. The rest of the function is pretty straight forward.
  243. First - we figure out the longest name.
  244. Then print 4 rows of 8bits - with names.
  245. **************************************************
  246. ***************************************************
  247. === TCL TOUR ===
  248. Open the file: "chips/atmel/at91/usarts.tcl"
  249. ===============
  250. First - about the AT91SAM series - all of the usarts
  251. are basically identical...
  252. Second - there can be many of them.
  253. In this case - I do some more TCL tricks to dynamically
  254. create functions out of thin air.
  255. Some assumptions:
  256. The "CHIP" file has defined some variables in a proper form.
  257. ie: AT91C_BASE_US0 - for usart0,
  258. AT91C_BASE_US1 - for usart1
  259. ... And so on ...
  260. Near the end of the file - look for a large "foreach" loop that
  261. looks like this:
  262. foreach WHO { US0 US1 US2 US3 US4 .... } {
  263. }
  264. In this case, I'm trying to figure out what USARTs exist.
  265. Step 1 - is to determine if the NAME has been defined.
  266. ie: Does AT91C_BASE_USx - where X is some number exist?
  267. The "info exists VARNAME" tells you if the variable exists. Then -
  268. inside the IF statement... There is another loop. This loop is the
  269. name of various "sub-registers" within the USART.
  270. Some more trick are played with the [set VAR] backtick evaluation stuff.
  271. And we create two variables
  272. We calculate and create the global variable name for every subregister in the USART.
  273. And - declare that variable as GLOBAL so the world can find it.
  274. Then - we dynamically create a function - based on the register name.
  275. Look carefully at how that is done. You'll notice the FUNCTION BODY is
  276. a string - not something in {braces}. Why? This is because we need TCL
  277. to evaluate the contents of that string "*NOW*" - when $vn exists not
  278. later, when the function "show_FOO" is invoked.
  279. Lastly - we build a "str" of commands - and create a single function -
  280. with the generated list of commands for the entire USART.
  281. With that little bit of code - I now have a bunch of functions like:
  282. show_US0, show_US1, show_US2, .... etc ...
  283. And show_US0_MR, show_US0_IMR ... etc...
  284. And - I have this for every USART... without having to create tons of
  285. boiler plate yucky code.
  286. ****************************************
  287. ****************************************
  288. END of the Tcl Intro and Walk Through
  289. ****************************************
  290. ****************************************
  291. FUTURE PLANS
  292. Some "GPIO" functions...
  293. @endverbatim
  294. */