OpenCores
URL https://opencores.org/ocsvn/openmsp430/openmsp430/trunk

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [tools/] [bin/] [openmsp430-loader.tcl] - Blame information for rev 210

Details | Compare with Previous | View Log

Line No. Rev Author Line
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 210 olivier.gi
#
26 2 olivier.gi
# 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: 210 $
33
# $LastChangedBy: olivier.girard $
34
# $LastChangedDate: 2015-11-17 10:57:08 +0100 (Tue, 17 Nov 2015) $
35
#------------------------------------------------------------------------------
36 2 olivier.gi
 
37 158 olivier.gi
global omsp_conf
38 110 olivier.gi
global omsp_info
39 2 olivier.gi
 
40 210 olivier.gi
# Detect toolchain
41
if {[catch {exec msp430-gcc --version} debug_info]} {
42
    if {[catch {exec msp430-elf-gcc --version} debug_info]} {
43
        puts "\nERROR: Could not detect MSP430 GCC toolchain"
44
        exit 1
45
    } else {
46
        set TOOLCHAIN_PFX "msp430-elf"
47
    }
48
} else {
49
    set TOOLCHAIN_PFX "msp430"
50
}
51
 
52 2 olivier.gi
###############################################################################
53 158 olivier.gi
#                            SOURCE LIBRARIES                                 #
54
###############################################################################
55
 
56
# Get library path
57
set current_file [info script]
58
if {[file type $current_file]=="link"} {
59
    set current_file [file readlink $current_file]
60
}
61
set lib_path [file dirname $current_file]/../lib/tcl-lib
62
 
63
# Source library
64
source $lib_path/dbg_functions.tcl
65
source $lib_path/dbg_utils.tcl
66
 
67
 
68
###############################################################################
69 2 olivier.gi
#                            PARAMETER CHECK                                  #
70
###############################################################################
71 158 olivier.gi
#proc GetAllowedSpeeds
72 2 olivier.gi
 
73
proc help {} {
74
    puts ""
75 158 olivier.gi
    puts "USAGE   : openmsp430-loader.tcl \[-device   <communication port>\]"
76
    puts "                                \[-adaptor  <adaptor type>\]"
77
    puts "                                \[-speed    <communication speed>\]"
78
    puts "                                \[-i2c_addr <cpu address>\]           <elf/ihex-file>"
79 2 olivier.gi
    puts ""
80 158 olivier.gi
    puts "DEFAULT : <communication port>  = /dev/ttyUSB0"
81
    puts "          <adaptor type>        = uart_generic"
82
    puts "          <communication speed> = 115200 (for UART) / I2C_S_100KHZ (for I2C)"
83
    puts "          <core address>        = 42"
84 2 olivier.gi
    puts ""
85 158 olivier.gi
    puts "EXAMPLES: openmsp430-loader.tcl -device /dev/ttyUSB0 -adaptor uart_generic -speed 9600  leds.elf"
86
    puts "          openmsp430-loader.tcl -device COM2:        -adaptor i2c_usb-iss  -speed I2C_S_100KHZ -i2c_addr 75 ta_uart.ihex"
87
    puts ""
88 2 olivier.gi
}
89
 
90
# Default values
91 158 olivier.gi
set omsp_conf(interface)  uart_generic
92
set omsp_conf(device)     /dev/ttyUSB0
93
set omsp_conf(baudrate)   [lindex [GetAllowedSpeeds] 1]
94
set omsp_conf(0,cpuaddr)  42
95
set elf_file              -1
96
set bin_file              "[clock clicks].bin"
97 2 olivier.gi
 
98
# Parse arguments
99
for {set i 0} {$i < $argc} {incr i} {
100
    switch -exact -- [lindex $argv $i] {
101 158 olivier.gi
        -device   {set omsp_conf(device)    [lindex $argv [expr $i+1]]; incr i}
102
        -adaptor  {set omsp_conf(interface) [lindex $argv [expr $i+1]]; incr i}
103
        -speed    {set omsp_conf(baudrate)  [lindex $argv [expr $i+1]]; incr i}
104
        -i2c_addr {set omsp_conf(0,cpuaddr) [lindex $argv [expr $i+1]]; incr i}
105
        default   {set elf_file             [lindex $argv $i]}
106 2 olivier.gi
    }
107
}
108
 
109
# Make sure arugments were specified
110
if {[string eq $elf_file -1]} {
111 158 olivier.gi
    puts "\nERROR: ELF/IHEX file isn't specified"
112 2 olivier.gi
    help
113 210 olivier.gi
    exit 1
114 2 olivier.gi
}
115
 
116
# Make sure the elf file exists
117
if {![file exists $elf_file]} {
118 158 olivier.gi
    puts "\nERROR: Specified ELF/IHEX file doesn't exist"
119 2 olivier.gi
    help
120 210 olivier.gi
    exit 1
121 2 olivier.gi
}
122
 
123 158 olivier.gi
# Make sure the selected adptor is valid
124
if {![string eq $omsp_conf(interface) "uart_generic"] &
125
    ![string eq $omsp_conf(interface) "i2c_usb-iss"]} {
126
    puts "\nERROR: Specified adaptor is not valid (should be \"uart_generic\" or \"i2c_usb-iss\")"
127
    help
128 210 olivier.gi
    exit 1
129 158 olivier.gi
}
130 2 olivier.gi
 
131 158 olivier.gi
# Make sure the I2C address is an integer
132
if {![string is integer $omsp_conf(0,cpuaddr)]} {
133
    puts "\nERROR: Specified I2C address is not an integer"
134
    help
135 210 olivier.gi
    exit 1
136 158 olivier.gi
}
137 2 olivier.gi
 
138 158 olivier.gi
# Make sure the I2C address is valid
139
if {($omsp_conf(0,cpuaddr)<8) | ($omsp_conf(0,cpuaddr)>119)} {
140
    puts "\nERROR: Specified I2C address should lay between 7 and 120"
141
    help
142 210 olivier.gi
    exit 1
143 2 olivier.gi
}
144
 
145 158 olivier.gi
# If the selected interface is a UART, make sure the selected speed is an integer
146
if {[string eq $omsp_conf(interface) "uart_generic"]} {
147
    if {![string is integer $omsp_conf(baudrate)]} {
148
        puts "\nERROR: Specified UART communication speed is not an integer"
149
        help
150 210 olivier.gi
        exit 1
151 158 olivier.gi
    }
152
} elseif {[string eq $omsp_conf(interface) "i2c_usb-iss"]} {
153
    if {[lsearch [lindex [GetAllowedSpeeds] 2] $omsp_conf(baudrate)]==-1} {
154
        puts "\nERROR: Specified I2C communication speed is not valid."
155
        puts "         Allowed values are:"
156
        foreach allowedVal [lindex [GetAllowedSpeeds] 2] {
157
            puts "                              - $allowedVal"
158
        }
159
        puts ""
160 210 olivier.gi
        exit 1
161 158 olivier.gi
    }
162
}
163 2 olivier.gi
 
164
 
165
###############################################################################
166
#                  CREATE AND READ BINARY EXECUTABLE FILE                     #
167
###############################################################################
168
 
169 89 olivier.gi
# Detect the file format depending on the fil extention
170
set fileType [file extension $elf_file]
171
set fileType [string tolower $fileType]
172
regsub {\.} $fileType {} fileType
173
if {![string eq $fileType "ihex"] & ![string eq $fileType "hex"] & ![string eq $fileType "elf"]} {
174 158 olivier.gi
    puts "\nERROR: [string toupper $fileType] file format not supported"
175 89 olivier.gi
    return 0
176
}
177
if {[string eq $fileType "hex"]} {
178
    set fileType "ihex"
179
}
180
if {[string eq $fileType "elf"]} {
181
    set fileType "elf32-msp430"
182
}
183
 
184 2 olivier.gi
# Generate binary file
185 210 olivier.gi
if {[catch {exec ${TOOLCHAIN_PFX}-objcopy -I $fileType -O binary $elf_file $bin_file} errMsg]} {
186 77 olivier.gi
    puts $errMsg
187
    exit 1
188
}
189 2 olivier.gi
 
190 77 olivier.gi
# Wait until bin file is present on the filesystem
191
set timeout 100
192
for {set i 0} {$i <= $timeout} {incr i} {
193
    after 500
194
    if {[file exists $bin_file]} {
195 158 olivier.gi
        break
196 77 olivier.gi
    }
197
}
198
if {$i>=$timeout} {
199 210 olivier.gi
    puts "\nTimeout: ELF to BIN file conversion problem with \"${TOOLCHAIN_PFX}-objcopy\" executable"
200 77 olivier.gi
    puts "$errMsg"
201
    exit 1
202
}
203
 
204 2 olivier.gi
# Read file
205
set fp [open $bin_file r]
206
fconfigure $fp -translation binary
207
binary scan [read $fp] H* hex_data yop
208
close $fp
209
 
210
# Cleanup
211
file delete $bin_file
212
 
213
# Get program size
214
set hex_size  [string length $hex_data]
215
set byte_size [expr $hex_size/2]
216
set word_size [expr $byte_size/2]
217
 
218
# Format data
219
for {set i 0} {$i < $hex_size} {set i [expr $i+4]} {
220
    set hex_msb "[string index $hex_data [expr $i+2]][string index $hex_data [expr $i+3]]"
221
    set hex_lsb "[string index $hex_data [expr $i+0]][string index $hex_data [expr $i+1]]"
222
    lappend DataArray "0x$hex_msb$hex_lsb"
223
}
224
 
225
 
226
###############################################################################
227
#                      LOAD PROGRAM TO OPENMSP430 TARGET                      #
228
###############################################################################
229
 
230
# Connect to target and stop CPU
231 158 olivier.gi
puts            ""
232
puts -nonewline "Connecting with the openMSP430 ($omsp_conf(device), $omsp_conf(baudrate)\ bps)... "
233 2 olivier.gi
flush stdout
234 158 olivier.gi
if {![GetDevice 0]} {
235 2 olivier.gi
    puts "failed"
236 158 olivier.gi
    puts "Could not open $omsp_conf(device)"
237 2 olivier.gi
    puts "Available serial ports are:"
238 158 olivier.gi
    foreach port [utils::uart_port_list] {
239 2 olivier.gi
    puts "                             -  $port"
240
    }
241 158 olivier.gi
    if {[string eq $omsp_conf(interface) "i2c_usb-iss"]} {
242
        puts "\nMake sure the specified I2C device address is correct: $omsp_conf(0,cpuaddr)\n"
243
    }
244 2 olivier.gi
    exit 1
245
}
246 158 olivier.gi
ExecutePOR_Halt 0
247 2 olivier.gi
puts "done"
248 158 olivier.gi
set sizes [GetCPU_ID_SIZE 0]
249 110 olivier.gi
 
250 158 olivier.gi
if {$omsp_info(0,alias)!=""} {
251
    puts "Connected: target device identified as $omsp_info(0,alias)."
252 110 olivier.gi
}
253 35 olivier.gi
puts "Connected: target device has [lindex $sizes 0]B Program Memory and [lindex $sizes 1]B Data Memory"
254 2 olivier.gi
puts ""
255
 
256 77 olivier.gi
# Make sure ELF program size is the same as the available program memory
257
if {[lindex $sizes 0] != [expr $hex_size/2]} {
258
    puts "ERROR: ELF program size ($byte_size B) is different than the available program memory ([lindex $sizes 0] B)"
259 210 olivier.gi
    utils::uart_close
260 77 olivier.gi
    exit 1
261
}
262
 
263 35 olivier.gi
# Load Program Memory
264 2 olivier.gi
set StartAddr [format "0x%04x" [expr 0x10000-$byte_size]]
265 35 olivier.gi
puts -nonewline "Load Program Memory... "
266 2 olivier.gi
flush stdout
267 158 olivier.gi
WriteMemQuick 0 $StartAddr $DataArray
268
after 500
269 2 olivier.gi
puts "done"
270
 
271
# Check Data
272 35 olivier.gi
puts -nonewline "Verify Program Memory... "
273 2 olivier.gi
flush stdout
274 158 olivier.gi
if {[VerifyMem 0 $StartAddr $DataArray 1]} {
275 2 olivier.gi
    puts "done"
276
} else {
277
    puts "ERROR"
278 210 olivier.gi
    utils::uart_close
279 158 olivier.gi
    exit 1
280 2 olivier.gi
}
281
 
282
# Release device
283 158 olivier.gi
ReleaseDevice 0 0xfffe

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.