1 |
2 |
olivier.gi |
#!/usr/bin/tclsh
|
2 |
|
|
#------------------------------------------------------------------------------
|
3 |
|
|
# Copyright (C) 2001 Authors
|
4 |
|
|
#
|
5 |
|
|
# This source file may be used and distributed without restriction provided
|
6 |
|
|
# that this copyright statement is not removed from the file and that any
|
7 |
|
|
# derivative work contains the original copyright notice and the associated
|
8 |
|
|
# disclaimer.
|
9 |
|
|
#
|
10 |
|
|
# This source file is free software; you can redistribute it and/or modify
|
11 |
|
|
# it under the terms of the GNU Lesser General Public License as published
|
12 |
|
|
# by the Free Software Foundation; either version 2.1 of the License, or
|
13 |
|
|
# (at your option) any later version.
|
14 |
|
|
#
|
15 |
|
|
# This source is distributed in the hope that it will be useful, but WITHOUT
|
16 |
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
17 |
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
18 |
|
|
# License for more details.
|
19 |
|
|
#
|
20 |
|
|
# You should have received a copy of the GNU Lesser General Public License
|
21 |
|
|
# along with this source; if not, write to the Free Software Foundation,
|
22 |
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
23 |
|
|
#
|
24 |
|
|
#------------------------------------------------------------------------------
|
25 |
|
|
#
|
26 |
|
|
# File Name: openmsp430-loader.tcl
|
27 |
14 |
olivier.gi |
#
|
28 |
|
|
# Author(s):
|
29 |
|
|
# - Olivier Girard, olgirard@gmail.com
|
30 |
|
|
#
|
31 |
2 |
olivier.gi |
#------------------------------------------------------------------------------
|
32 |
14 |
olivier.gi |
# $Rev: 110 $
|
33 |
|
|
# $LastChangedBy: olivier.girard $
|
34 |
|
|
# $LastChangedDate: 2011-05-19 22:33:51 +0200 (Thu, 19 May 2011) $
|
35 |
|
|
#------------------------------------------------------------------------------
|
36 |
2 |
olivier.gi |
|
37 |
|
|
global serial_baudrate
|
38 |
|
|
global serial_device
|
39 |
110 |
olivier.gi |
global omsp_info
|
40 |
2 |
olivier.gi |
|
41 |
|
|
###############################################################################
|
42 |
|
|
# PARAMETER CHECK #
|
43 |
|
|
###############################################################################
|
44 |
|
|
|
45 |
|
|
proc help {} {
|
46 |
|
|
puts ""
|
47 |
89 |
olivier.gi |
puts "USAGE : openmsp430-loader.tcl \[-device <communication device>\] \[-baudrate <communication speed>\] <elf/ihex-file>"
|
48 |
2 |
olivier.gi |
puts ""
|
49 |
|
|
puts "Examples: openmsp430-loader.tcl -device /dev/ttyUSB0 -baudrate 9600 leds.elf"
|
50 |
89 |
olivier.gi |
puts " openmsp430-loader.tcl -device COM2: -baudrate 38400 ta_uart.ihex"
|
51 |
2 |
olivier.gi |
puts ""
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
# Default values
|
55 |
|
|
set serial_device /dev/ttyUSB0
|
56 |
|
|
set serial_baudrate 115200
|
57 |
|
|
set elf_file -1
|
58 |
|
|
set bin_file "[clock clicks].bin"
|
59 |
|
|
|
60 |
|
|
# Parse arguments
|
61 |
|
|
for {set i 0} {$i < $argc} {incr i} {
|
62 |
|
|
switch -exact -- [lindex $argv $i] {
|
63 |
|
|
-device {set serial_device [lindex $argv [expr $i+1]]; incr i}
|
64 |
|
|
-baudrate {set serial_baudrate [lindex $argv [expr $i+1]]; incr i}
|
65 |
|
|
default {set elf_file [lindex $argv $i]}
|
66 |
|
|
}
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
# Make sure arugments were specified
|
70 |
|
|
if {[string eq $elf_file -1]} {
|
71 |
89 |
olivier.gi |
puts "ERROR: ELF/IHEX file isn't specified"
|
72 |
2 |
olivier.gi |
help
|
73 |
|
|
exit 1
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
# Make sure the elf file exists
|
77 |
|
|
if {![file exists $elf_file]} {
|
78 |
89 |
olivier.gi |
puts "ERROR: Specified ELF/IHEX file doesn't exist"
|
79 |
2 |
olivier.gi |
help
|
80 |
|
|
exit 1
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
###############################################################################
|
85 |
|
|
# SOURCE LIBRARIES #
|
86 |
|
|
###############################################################################
|
87 |
|
|
|
88 |
|
|
# Get library path
|
89 |
|
|
set current_file [info script]
|
90 |
|
|
if {[file type $current_file]=="link"} {
|
91 |
|
|
set current_file [file readlink $current_file]
|
92 |
|
|
}
|
93 |
|
|
set lib_path [file dirname $current_file]/../lib/tcl-lib
|
94 |
|
|
|
95 |
|
|
# Source library
|
96 |
|
|
source $lib_path/dbg_functions.tcl
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
###############################################################################
|
100 |
|
|
# CREATE AND READ BINARY EXECUTABLE FILE #
|
101 |
|
|
###############################################################################
|
102 |
|
|
|
103 |
89 |
olivier.gi |
# Detect the file format depending on the fil extention
|
104 |
|
|
set fileType [file extension $elf_file]
|
105 |
|
|
set fileType [string tolower $fileType]
|
106 |
|
|
regsub {\.} $fileType {} fileType
|
107 |
|
|
if {![string eq $fileType "ihex"] & ![string eq $fileType "hex"] & ![string eq $fileType "elf"]} {
|
108 |
|
|
puts "ERROR: [string toupper $fileType] file format not supported"
|
109 |
|
|
return 0
|
110 |
|
|
}
|
111 |
|
|
if {[string eq $fileType "hex"]} {
|
112 |
|
|
set fileType "ihex"
|
113 |
|
|
}
|
114 |
|
|
if {[string eq $fileType "elf"]} {
|
115 |
|
|
set fileType "elf32-msp430"
|
116 |
|
|
}
|
117 |
|
|
|
118 |
2 |
olivier.gi |
# Generate binary file
|
119 |
89 |
olivier.gi |
if {[catch {exec msp430-objcopy -I $fileType -O binary $elf_file $bin_file} errMsg]} {
|
120 |
77 |
olivier.gi |
puts $errMsg
|
121 |
|
|
exit 1
|
122 |
|
|
}
|
123 |
2 |
olivier.gi |
|
124 |
77 |
olivier.gi |
# Wait until bin file is present on the filesystem
|
125 |
|
|
set timeout 100
|
126 |
|
|
for {set i 0} {$i <= $timeout} {incr i} {
|
127 |
|
|
after 500
|
128 |
|
|
if {[file exists $bin_file]} {
|
129 |
|
|
break
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
if {$i>=$timeout} {
|
133 |
|
|
puts "Timeout: ELF to BIN file conversion problem with \"msp430-objcopy\" executable"
|
134 |
|
|
puts "$errMsg"
|
135 |
|
|
exit 1
|
136 |
|
|
}
|
137 |
|
|
|
138 |
2 |
olivier.gi |
# Read file
|
139 |
|
|
set fp [open $bin_file r]
|
140 |
|
|
fconfigure $fp -translation binary
|
141 |
|
|
binary scan [read $fp] H* hex_data yop
|
142 |
|
|
close $fp
|
143 |
|
|
|
144 |
|
|
# Cleanup
|
145 |
|
|
file delete $bin_file
|
146 |
|
|
|
147 |
|
|
# Get program size
|
148 |
|
|
set hex_size [string length $hex_data]
|
149 |
|
|
set byte_size [expr $hex_size/2]
|
150 |
|
|
set word_size [expr $byte_size/2]
|
151 |
|
|
|
152 |
|
|
# Format data
|
153 |
|
|
for {set i 0} {$i < $hex_size} {set i [expr $i+4]} {
|
154 |
|
|
set hex_msb "[string index $hex_data [expr $i+2]][string index $hex_data [expr $i+3]]"
|
155 |
|
|
set hex_lsb "[string index $hex_data [expr $i+0]][string index $hex_data [expr $i+1]]"
|
156 |
|
|
lappend DataArray "0x$hex_msb$hex_lsb"
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
###############################################################################
|
161 |
|
|
# LOAD PROGRAM TO OPENMSP430 TARGET #
|
162 |
|
|
###############################################################################
|
163 |
|
|
|
164 |
|
|
# Connect to target and stop CPU
|
165 |
|
|
puts -nonewline "Connecting with the openMSP430 ($serial_device, $serial_baudrate\ bps)... "
|
166 |
|
|
flush stdout
|
167 |
|
|
if {![GetDevice]} {
|
168 |
|
|
puts "failed"
|
169 |
|
|
puts "Could not open $serial_device"
|
170 |
|
|
puts "Available serial ports are:"
|
171 |
|
|
foreach port [dbg_list_uart] {
|
172 |
|
|
puts " - $port"
|
173 |
|
|
}
|
174 |
|
|
exit 1
|
175 |
|
|
}
|
176 |
|
|
ExecutePOR_Halt
|
177 |
|
|
puts "done"
|
178 |
|
|
set sizes [GetCPU_ID_SIZE]
|
179 |
110 |
olivier.gi |
|
180 |
|
|
if {$omsp_info(alias)!=""} {
|
181 |
|
|
puts "Connected: target device identified as $omsp_info(alias)."
|
182 |
|
|
}
|
183 |
35 |
olivier.gi |
puts "Connected: target device has [lindex $sizes 0]B Program Memory and [lindex $sizes 1]B Data Memory"
|
184 |
2 |
olivier.gi |
puts ""
|
185 |
|
|
|
186 |
77 |
olivier.gi |
# Make sure ELF program size is the same as the available program memory
|
187 |
|
|
if {[lindex $sizes 0] != [expr $hex_size/2]} {
|
188 |
|
|
puts "ERROR: ELF program size ($byte_size B) is different than the available program memory ([lindex $sizes 0] B)"
|
189 |
|
|
exit 1
|
190 |
|
|
}
|
191 |
|
|
|
192 |
35 |
olivier.gi |
# Load Program Memory
|
193 |
2 |
olivier.gi |
set StartAddr [format "0x%04x" [expr 0x10000-$byte_size]]
|
194 |
35 |
olivier.gi |
puts -nonewline "Load Program Memory... "
|
195 |
2 |
olivier.gi |
flush stdout
|
196 |
|
|
WriteMemQuick $StartAddr $DataArray
|
197 |
|
|
puts "done"
|
198 |
|
|
|
199 |
|
|
# Check Data
|
200 |
35 |
olivier.gi |
puts -nonewline "Verify Program Memory... "
|
201 |
2 |
olivier.gi |
flush stdout
|
202 |
77 |
olivier.gi |
if {[VerifyMem $StartAddr $DataArray 1]} {
|
203 |
2 |
olivier.gi |
puts "done"
|
204 |
|
|
} else {
|
205 |
|
|
puts "ERROR"
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
# Release device
|
209 |
|
|
ReleaseDevice 0xfffe
|