]> git.sur5r.net Git - openocd/blob - src/startup.tcl
294dee28de75576469e90c9da53a043b465c532a
[openocd] / src / startup.tcl
1 #
2 # Defines basic Tcl procs that must be there for
3 # OpenOCD to work.
4 #
5 # Embedded into OpenOCD executable
6 #
7
8
9 # Help text list. A list of command + help text pairs.
10 #
11 # Commands can be more than one word and they are stored
12 # as "flash banks" "help text x x x"
13
14 global ocd_helptext
15 set ocd_helptext {}
16
17 proc add_help_text {cmd cmd_help} {
18         global ocd_helptext
19         lappend ocd_helptext [list $cmd $cmd_help]
20 }
21
22 # Production command
23 # FIX!!! need to figure out how to feed back relevant output
24 # from e.g. "flash banks" command...
25 proc board_produce {filename serialnumber} {
26         openocd "reset init"
27         openocd "flash write_image erase $filename [flash] bin"]]
28         openocd "verify_image $filename [flash] bin"]]
29         echo "Successfully ran production procedure"
30 }
31
32 proc board_test {} {
33         echo "Production test not implemented"
34 }
35
36 # Show flash in human readable form
37 # This is an example of a human readable form of a low level fn
38 proc flash_banks_pretty {} { 
39         set i 0         
40         set result ""
41         foreach {a} [flash_banks] {
42                 if {$i > 0} {
43                         set result "$result\n"
44                 }
45                 set result [format "$result#%d: %s at 0x%08x, size 0x%08x, buswidth %d, chipwidth %d" $i [lindex $a 0] [lindex $a 1] [lindex $a 2] [lindex $a 3] [lindex $a 4]]
46                 set i [expr $i+1]       
47         }       
48         return $result
49 }
50
51 # We need to explicitly redirect this to the OpenOCD command
52 # as Tcl defines the exit proc
53 proc exit {} {
54         openocd_throw exit
55 }
56
57 # We have currently converted only "flash banks" to tcl.
58 proc flash args {
59         if {[string compare [lindex $args 0] banks]==0} {
60                 return [flash_banks_pretty]
61         }
62         openocd_throw "flash $args"
63 }
64
65 #Print help text for a command. Word wrap
66 #help text that is too wide inside column.
67 proc help {args} {
68         global ocd_helptext
69         set cmd $args
70         foreach a [lsort $ocd_helptext] {
71                 if {[string length $cmd]==0||[string first $cmd $a]!=-1||[string first $cmd [lindex $a 1]]!=-1} {
72                         set w 50
73                         set cmdname [lindex $a 0]
74                         set h [lindex $a 1]
75                         set n 0
76                         while 1 {
77                                 if {$n > [string length $h]} {break}
78                                 
79                                 set next_a [expr $n+$w]
80                                 if {[string length $h]>$n+$w} {
81                                         set xxxx [string range $h $n [expr $n+$w]]
82                                         for {set lastpos [expr [string length $xxxx]-1]} {$lastpos>=0&&[string compare [string range $xxxx $lastpos $lastpos] " "]!=0} {set lastpos [expr $lastpos-1]} {
83                                         }
84                                         #set next_a -1
85                                         if {$lastpos!=-1} {
86                                                 set next_a [expr $lastpos+$n+1]
87                                         }
88                                 }
89                                 
90                                 
91                                 puts [format "%-25s %s" $cmdname [string range $h $n [expr $next_a-1]] ]
92                                 set cmdname ""
93                                 set n [expr $next_a]
94                         }
95                 }
96         }
97 }
98
99 add_help_text help "Tcl implementation of help command"
100
101
102 # If a fn is unknown to Tcl, we try to execute it as an OpenOCD command
103 proc unknown {args} {
104         if {[string length $args]>0} {
105                 set cmd ""
106                 # We need to add back quotes for arguments w/space
107                 # for args without space, we can add quotes anyway
108                 foreach {a} $args {
109                         set cmd "$cmd \"$a\""
110                 }
111                 openocd_throw $cmd
112         }
113         # openocd_throw outputs while running and also sets the
114         # primary return value to the output of the command
115         #
116         # The primary return value have been set by "openocd" above,
117         # so we need to clear it, lest we print out the output from
118         # the command twice.
119         return ""
120 }