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: 89 $
|
33 |
|
|
# $LastChangedBy: olivier.girard $
|
34 |
|
|
# $LastChangedDate: 2011-02-05 15:03:33 +0100 (Sat, 05 Feb 2011) $
|
35 |
|
|
#------------------------------------------------------------------------------
|
36 |
2 |
olivier.gi |
|
37 |
|
|
global serial_baudrate
|
38 |
|
|
global serial_device
|
39 |
|
|
|
40 |
|
|
###############################################################################
|
41 |
|
|
# PARAMETER CHECK #
|
42 |
|
|
###############################################################################
|
43 |
|
|
|
44 |
|
|
proc help {} {
|
45 |
|
|
puts ""
|
46 |
89 |
olivier.gi |
puts "USAGE : openmsp430-loader.tcl \[-device <communication device>\] \[-baudrate <communication speed>\] <elf/ihex-file>"
|
47 |
2 |
olivier.gi |
puts ""
|
48 |
|
|
puts "Examples: openmsp430-loader.tcl -device /dev/ttyUSB0 -baudrate 9600 leds.elf"
|
49 |
89 |
olivier.gi |
puts " openmsp430-loader.tcl -device COM2: -baudrate 38400 ta_uart.ihex"
|
50 |
2 |
olivier.gi |
puts ""
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
# Default values
|
54 |
|
|
set serial_device /dev/ttyUSB0
|
55 |
|
|
set serial_baudrate 115200
|
56 |
|
|
set elf_file -1
|
57 |
|
|
set bin_file "[clock clicks].bin"
|
58 |
|
|
|
59 |
|
|
# Parse arguments
|
60 |
|
|
for {set i 0} {$i < $argc} {incr i} {
|
61 |
|
|
switch -exact -- [lindex $argv $i] {
|
62 |
|
|
-device {set serial_device [lindex $argv [expr $i+1]]; incr i}
|
63 |
|
|
-baudrate {set serial_baudrate [lindex $argv [expr $i+1]]; incr i}
|
64 |
|
|
default {set elf_file [lindex $argv $i]}
|
65 |
|
|
}
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
# Make sure arugments were specified
|
69 |
|
|
if {[string eq $elf_file -1]} {
|
70 |
89 |
olivier.gi |
puts "ERROR: ELF/IHEX file isn't specified"
|
71 |
2 |
olivier.gi |
help
|
72 |
|
|
exit 1
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
# Make sure the elf file exists
|
76 |
|
|
if {![file exists $elf_file]} {
|
77 |
89 |
olivier.gi |
puts "ERROR: Specified ELF/IHEX file doesn't exist"
|
78 |
2 |
olivier.gi |
help
|
79 |
|
|
exit 1
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
###############################################################################
|
84 |
|
|
# SOURCE LIBRARIES #
|
85 |
|
|
###############################################################################
|
86 |
|
|
|
87 |
|
|
# Get library path
|
88 |
|
|
set current_file [info script]
|
89 |
|
|
if {[file type $current_file]=="link"} {
|
90 |
|
|
set current_file [file readlink $current_file]
|
91 |
|
|
}
|
92 |
|
|
set lib_path [file dirname $current_file]/../lib/tcl-lib
|
93 |
|
|
|
94 |
|
|
# Source library
|
95 |
|
|
source $lib_path/dbg_functions.tcl
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
###############################################################################
|
99 |
|
|
# CREATE AND READ BINARY EXECUTABLE FILE #
|
100 |
|
|
###############################################################################
|
101 |
|
|
|
102 |
89 |
olivier.gi |
# Detect the file format depending on the fil extention
|
103 |
|
|
set fileType [file extension $elf_file]
|
104 |
|
|
set fileType [string tolower $fileType]
|
105 |
|
|
regsub {\.} $fileType {} fileType
|
106 |
|
|
if {![string eq $fileType "ihex"] & ![string eq $fileType "hex"] & ![string eq $fileType "elf"]} {
|
107 |
|
|
puts "ERROR: [string toupper $fileType] file format not supported"
|
108 |
|
|
return 0
|
109 |
|
|
}
|
110 |
|
|
if {[string eq $fileType "hex"]} {
|
111 |
|
|
set fileType "ihex"
|
112 |
|
|
}
|
113 |
|
|
if {[string eq $fileType "elf"]} {
|
114 |
|
|
set fileType "elf32-msp430"
|
115 |
|
|
}
|
116 |
|
|
|
117 |
2 |
olivier.gi |
# Generate binary file
|
118 |
89 |
olivier.gi |
if {[catch {exec msp430-objcopy -I $fileType -O binary $elf_file $bin_file} errMsg]} {
|
119 |
77 |
olivier.gi |
puts $errMsg
|
120 |
|
|
exit 1
|
121 |
|
|
}
|
122 |
2 |
olivier.gi |
|
123 |
77 |
olivier.gi |
# Wait until bin file is present on the filesystem
|
124 |
|
|
set timeout 100
|
125 |
|
|
for {set i 0} {$i <= $timeout} {incr i} {
|
126 |
|
|
after 500
|
127 |
|
|
if {[file exists $bin_file]} {
|
128 |
|
|
break
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
if {$i>=$timeout} {
|
132 |
|
|
puts "Timeout: ELF to BIN file conversion problem with \"msp430-objcopy\" executable"
|
133 |
|
|
puts "$errMsg"
|
134 |
|
|
exit 1
|
135 |
|
|
}
|
136 |
|
|
|
137 |
2 |
olivier.gi |
# Read file
|
138 |
|
|
set fp [open $bin_file r]
|
139 |
|
|
fconfigure $fp -translation binary
|
140 |
|
|
binary scan [read $fp] H* hex_data yop
|
141 |
|
|
close $fp
|
142 |
|
|
|
143 |
|
|
# Cleanup
|
144 |
|
|
file delete $bin_file
|
145 |
|
|
|
146 |
|
|
# Get program size
|
147 |
|
|
set hex_size [string length $hex_data]
|
148 |
|
|
set byte_size [expr $hex_size/2]
|
149 |
|
|
set word_size [expr $byte_size/2]
|
150 |
|
|
|
151 |
|
|
# Format data
|
152 |
|
|
for {set i 0} {$i < $hex_size} {set i [expr $i+4]} {
|
153 |
|
|
set hex_msb "[string index $hex_data [expr $i+2]][string index $hex_data [expr $i+3]]"
|
154 |
|
|
set hex_lsb "[string index $hex_data [expr $i+0]][string index $hex_data [expr $i+1]]"
|
155 |
|
|
lappend DataArray "0x$hex_msb$hex_lsb"
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
###############################################################################
|
160 |
|
|
# LOAD PROGRAM TO OPENMSP430 TARGET #
|
161 |
|
|
###############################################################################
|
162 |
|
|
|
163 |
|
|
# Connect to target and stop CPU
|
164 |
|
|
puts -nonewline "Connecting with the openMSP430 ($serial_device, $serial_baudrate\ bps)... "
|
165 |
|
|
flush stdout
|
166 |
|
|
if {![GetDevice]} {
|
167 |
|
|
puts "failed"
|
168 |
|
|
puts "Could not open $serial_device"
|
169 |
|
|
puts "Available serial ports are:"
|
170 |
|
|
foreach port [dbg_list_uart] {
|
171 |
|
|
puts " - $port"
|
172 |
|
|
}
|
173 |
|
|
exit 1
|
174 |
|
|
}
|
175 |
|
|
ExecutePOR_Halt
|
176 |
|
|
puts "done"
|
177 |
|
|
set sizes [GetCPU_ID_SIZE]
|
178 |
35 |
olivier.gi |
puts "Connected: target device has [lindex $sizes 0]B Program Memory and [lindex $sizes 1]B Data Memory"
|
179 |
2 |
olivier.gi |
puts ""
|
180 |
|
|
|
181 |
77 |
olivier.gi |
# Make sure ELF program size is the same as the available program memory
|
182 |
|
|
if {[lindex $sizes 0] != [expr $hex_size/2]} {
|
183 |
|
|
puts "ERROR: ELF program size ($byte_size B) is different than the available program memory ([lindex $sizes 0] B)"
|
184 |
|
|
exit 1
|
185 |
|
|
}
|
186 |
|
|
|
187 |
35 |
olivier.gi |
# Load Program Memory
|
188 |
2 |
olivier.gi |
set StartAddr [format "0x%04x" [expr 0x10000-$byte_size]]
|
189 |
35 |
olivier.gi |
puts -nonewline "Load Program Memory... "
|
190 |
2 |
olivier.gi |
flush stdout
|
191 |
|
|
WriteMemQuick $StartAddr $DataArray
|
192 |
|
|
puts "done"
|
193 |
|
|
|
194 |
|
|
# Check Data
|
195 |
35 |
olivier.gi |
puts -nonewline "Verify Program Memory... "
|
196 |
2 |
olivier.gi |
flush stdout
|
197 |
77 |
olivier.gi |
if {[VerifyMem $StartAddr $DataArray 1]} {
|
198 |
2 |
olivier.gi |
puts "done"
|
199 |
|
|
} else {
|
200 |
|
|
puts "ERROR"
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
# Release device
|
204 |
|
|
ReleaseDevice 0xfffe
|