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

Subversion Repositories openmsp430

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

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