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

Subversion Repositories openmsp430

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

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: 77 $
33
# $LastChangedBy: olivier.girard $
34
# $LastChangedDate: 2010-11-21 20:50:55 +0100 (Sun, 21 Nov 2010) $
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
    puts "USAGE   : openmsp430-loader.tcl \[-device <communication device>\] \[-baudrate <communication speed>\] <elf-file>"
47
    puts ""
48
    puts "Examples: openmsp430-loader.tcl -device /dev/ttyUSB0 -baudrate  9600  leds.elf"
49
    puts "          openmsp430-loader.tcl -device COM2:        -baudrate 38400  ta_uart.elf"
50
    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
    puts "ERROR: ELF file isn't specified"
71
    help
72
    exit 1
73
}
74
 
75
# Make sure the elf file exists
76
if {![file exists $elf_file]} {
77
    puts "ERROR: Specified ELF file doesn't exist"
78
    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
# Generate binary file
103 77 olivier.gi
if {[catch {exec msp430-objcopy -O binary $elf_file $bin_file} errMsg]} {
104
    puts $errMsg
105
    exit 1
106
}
107 2 olivier.gi
 
108 77 olivier.gi
# Wait until bin file is present on the filesystem
109
set timeout 100
110
for {set i 0} {$i <= $timeout} {incr i} {
111
    after 500
112
    if {[file exists $bin_file]} {
113
        break
114
    }
115
}
116
if {$i>=$timeout} {
117
    puts "Timeout: ELF to BIN file conversion problem with \"msp430-objcopy\" executable"
118
    puts "$errMsg"
119
    exit 1
120
}
121
 
122 2 olivier.gi
# Read file
123
set fp [open $bin_file r]
124
fconfigure $fp -translation binary
125
binary scan [read $fp] H* hex_data yop
126
close $fp
127
 
128
# Cleanup
129
file delete $bin_file
130
 
131
# Get program size
132
set hex_size  [string length $hex_data]
133
set byte_size [expr $hex_size/2]
134
set word_size [expr $byte_size/2]
135
 
136
# Format data
137
for {set i 0} {$i < $hex_size} {set i [expr $i+4]} {
138
    set hex_msb "[string index $hex_data [expr $i+2]][string index $hex_data [expr $i+3]]"
139
    set hex_lsb "[string index $hex_data [expr $i+0]][string index $hex_data [expr $i+1]]"
140
    lappend DataArray "0x$hex_msb$hex_lsb"
141
}
142
 
143
 
144
###############################################################################
145
#                      LOAD PROGRAM TO OPENMSP430 TARGET                      #
146
###############################################################################
147
 
148
# Connect to target and stop CPU
149
puts -nonewline "Connecting with the openMSP430 ($serial_device, $serial_baudrate\ bps)... "
150
flush stdout
151
if {![GetDevice]} {
152
    puts "failed"
153
    puts "Could not open $serial_device"
154
    puts "Available serial ports are:"
155
    foreach port [dbg_list_uart] {
156
    puts "                             -  $port"
157
    }
158
    exit 1
159
}
160
ExecutePOR_Halt
161
puts "done"
162
set sizes [GetCPU_ID_SIZE]
163 35 olivier.gi
puts "Connected: target device has [lindex $sizes 0]B Program Memory and [lindex $sizes 1]B Data Memory"
164 2 olivier.gi
puts ""
165
 
166 77 olivier.gi
# Make sure ELF program size is the same as the available program memory
167
if {[lindex $sizes 0] != [expr $hex_size/2]} {
168
    puts "ERROR: ELF program size ($byte_size B) is different than the available program memory ([lindex $sizes 0] B)"
169
    exit 1
170
}
171
 
172 35 olivier.gi
# Load Program Memory
173 2 olivier.gi
set StartAddr [format "0x%04x" [expr 0x10000-$byte_size]]
174 35 olivier.gi
puts -nonewline "Load Program Memory... "
175 2 olivier.gi
flush stdout
176
WriteMemQuick $StartAddr $DataArray
177
puts "done"
178
 
179
# Check Data
180 35 olivier.gi
puts -nonewline "Verify Program Memory... "
181 2 olivier.gi
flush stdout
182 77 olivier.gi
if {[VerifyMem $StartAddr $DataArray 1]} {
183 2 olivier.gi
    puts "done"
184
} else {
185
    puts "ERROR"
186
}
187
 
188
# Release device
189
ReleaseDevice 0xfffe

powered by: WebSVN 2.1.0

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