#!/usr/bin/tclsh
|
#!/usr/bin/tclsh
|
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
# Copyright (C) 2001 Authors
|
# Copyright (C) 2001 Authors
|
#
|
#
|
# This source file may be used and distributed without restriction provided
|
# This source file may be used and distributed without restriction provided
|
# that this copyright statement is not removed from the file and that any
|
# that this copyright statement is not removed from the file and that any
|
# derivative work contains the original copyright notice and the associated
|
# derivative work contains the original copyright notice and the associated
|
# disclaimer.
|
# disclaimer.
|
#
|
#
|
# This source file is free software; you can redistribute it and/or modify
|
# This source file is free software; you can redistribute it and/or modify
|
# it under the terms of the GNU Lesser General Public License as published
|
# it under the terms of the GNU Lesser General Public License as published
|
# by the Free Software Foundation; either version 2.1 of the License, or
|
# by the Free Software Foundation; either version 2.1 of the License, or
|
# (at your option) any later version.
|
# (at your option) any later version.
|
#
|
#
|
# This source is distributed in the hope that it will be useful, but WITHOUT
|
# This source is distributed in the hope that it will be useful, but WITHOUT
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
# License for more details.
|
# License for more details.
|
#
|
#
|
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
# along with this source; if not, write to the Free Software Foundation,
|
# along with this source; if not, write to the Free Software Foundation,
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
#
|
#
|
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
#
|
#
|
# File Name: openmsp430-loader.tcl
|
# File Name: openmsp430-loader.tcl
|
#
|
#
|
# Author(s):
|
# Author(s):
|
# - Olivier Girard, olgirard@gmail.com
|
# - Olivier Girard, olgirard@gmail.com
|
#
|
#
|
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
# $Rev: 14 $
|
# $Rev: 35 $
|
# $LastChangedBy: olivier.girard $
|
# $LastChangedBy: olivier.girard $
|
# $LastChangedDate: 2009-08-04 22:34:42 +0200 (Tue, 04 Aug 2009) $
|
# $LastChangedDate: 2009-12-29 21:12:19 +0100 (Tue, 29 Dec 2009) $
|
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
|
|
global serial_baudrate
|
global serial_baudrate
|
global serial_device
|
global serial_device
|
|
|
###############################################################################
|
###############################################################################
|
# PARAMETER CHECK #
|
# PARAMETER CHECK #
|
###############################################################################
|
###############################################################################
|
|
|
proc help {} {
|
proc help {} {
|
puts ""
|
puts ""
|
puts "USAGE : openmsp430-loader.tcl \[-device <communication device>\] \[-baudrate <communication speed>\] <elf-file>"
|
puts "USAGE : openmsp430-loader.tcl \[-device <communication device>\] \[-baudrate <communication speed>\] <elf-file>"
|
puts ""
|
puts ""
|
puts "Examples: openmsp430-loader.tcl -device /dev/ttyUSB0 -baudrate 9600 leds.elf"
|
puts "Examples: openmsp430-loader.tcl -device /dev/ttyUSB0 -baudrate 9600 leds.elf"
|
puts " openmsp430-loader.tcl -device COM2: -baudrate 38400 ta_uart.elf"
|
puts " openmsp430-loader.tcl -device COM2: -baudrate 38400 ta_uart.elf"
|
puts ""
|
puts ""
|
}
|
}
|
|
|
# Default values
|
# Default values
|
set serial_device /dev/ttyUSB0
|
set serial_device /dev/ttyUSB0
|
set serial_baudrate 115200
|
set serial_baudrate 115200
|
set elf_file -1
|
set elf_file -1
|
set bin_file "[clock clicks].bin"
|
set bin_file "[clock clicks].bin"
|
|
|
# Parse arguments
|
# Parse arguments
|
for {set i 0} {$i < $argc} {incr i} {
|
for {set i 0} {$i < $argc} {incr i} {
|
switch -exact -- [lindex $argv $i] {
|
switch -exact -- [lindex $argv $i] {
|
-device {set serial_device [lindex $argv [expr $i+1]]; incr i}
|
-device {set serial_device [lindex $argv [expr $i+1]]; incr i}
|
-baudrate {set serial_baudrate [lindex $argv [expr $i+1]]; incr i}
|
-baudrate {set serial_baudrate [lindex $argv [expr $i+1]]; incr i}
|
default {set elf_file [lindex $argv $i]}
|
default {set elf_file [lindex $argv $i]}
|
}
|
}
|
}
|
}
|
|
|
# Make sure arugments were specified
|
# Make sure arugments were specified
|
if {[string eq $elf_file -1]} {
|
if {[string eq $elf_file -1]} {
|
puts "ERROR: ELF file isn't specified"
|
puts "ERROR: ELF file isn't specified"
|
help
|
help
|
exit 1
|
exit 1
|
}
|
}
|
|
|
# Make sure the elf file exists
|
# Make sure the elf file exists
|
if {![file exists $elf_file]} {
|
if {![file exists $elf_file]} {
|
puts "ERROR: Specified ELF file doesn't exist"
|
puts "ERROR: Specified ELF file doesn't exist"
|
help
|
help
|
exit 1
|
exit 1
|
}
|
}
|
|
|
|
|
###############################################################################
|
###############################################################################
|
# SOURCE LIBRARIES #
|
# SOURCE LIBRARIES #
|
###############################################################################
|
###############################################################################
|
|
|
# Get library path
|
# Get library path
|
set current_file [info script]
|
set current_file [info script]
|
if {[file type $current_file]=="link"} {
|
if {[file type $current_file]=="link"} {
|
set current_file [file readlink $current_file]
|
set current_file [file readlink $current_file]
|
}
|
}
|
set lib_path [file dirname $current_file]/../lib/tcl-lib
|
set lib_path [file dirname $current_file]/../lib/tcl-lib
|
|
|
# Source library
|
# Source library
|
source $lib_path/dbg_functions.tcl
|
source $lib_path/dbg_functions.tcl
|
|
|
|
|
###############################################################################
|
###############################################################################
|
# CREATE AND READ BINARY EXECUTABLE FILE #
|
# CREATE AND READ BINARY EXECUTABLE FILE #
|
###############################################################################
|
###############################################################################
|
|
|
# Generate binary file
|
# Generate binary file
|
catch {exec msp430-objcopy -O binary $elf_file $bin_file}
|
catch {exec msp430-objcopy -O binary $elf_file $bin_file}
|
|
|
# Read file
|
# Read file
|
set fp [open $bin_file r]
|
set fp [open $bin_file r]
|
fconfigure $fp -translation binary
|
fconfigure $fp -translation binary
|
binary scan [read $fp] H* hex_data yop
|
binary scan [read $fp] H* hex_data yop
|
close $fp
|
close $fp
|
|
|
# Cleanup
|
# Cleanup
|
file delete $bin_file
|
file delete $bin_file
|
|
|
# Get program size
|
# Get program size
|
set hex_size [string length $hex_data]
|
set hex_size [string length $hex_data]
|
set byte_size [expr $hex_size/2]
|
set byte_size [expr $hex_size/2]
|
set word_size [expr $byte_size/2]
|
set word_size [expr $byte_size/2]
|
|
|
# Format data
|
# Format data
|
for {set i 0} {$i < $hex_size} {set i [expr $i+4]} {
|
for {set i 0} {$i < $hex_size} {set i [expr $i+4]} {
|
set hex_msb "[string index $hex_data [expr $i+2]][string index $hex_data [expr $i+3]]"
|
set hex_msb "[string index $hex_data [expr $i+2]][string index $hex_data [expr $i+3]]"
|
set hex_lsb "[string index $hex_data [expr $i+0]][string index $hex_data [expr $i+1]]"
|
set hex_lsb "[string index $hex_data [expr $i+0]][string index $hex_data [expr $i+1]]"
|
lappend DataArray "0x$hex_msb$hex_lsb"
|
lappend DataArray "0x$hex_msb$hex_lsb"
|
}
|
}
|
|
|
|
|
###############################################################################
|
###############################################################################
|
# LOAD PROGRAM TO OPENMSP430 TARGET #
|
# LOAD PROGRAM TO OPENMSP430 TARGET #
|
###############################################################################
|
###############################################################################
|
|
|
# Connect to target and stop CPU
|
# Connect to target and stop CPU
|
puts -nonewline "Connecting with the openMSP430 ($serial_device, $serial_baudrate\ bps)... "
|
puts -nonewline "Connecting with the openMSP430 ($serial_device, $serial_baudrate\ bps)... "
|
flush stdout
|
flush stdout
|
if {![GetDevice]} {
|
if {![GetDevice]} {
|
puts "failed"
|
puts "failed"
|
puts "Could not open $serial_device"
|
puts "Could not open $serial_device"
|
puts "Available serial ports are:"
|
puts "Available serial ports are:"
|
foreach port [dbg_list_uart] {
|
foreach port [dbg_list_uart] {
|
puts " - $port"
|
puts " - $port"
|
}
|
}
|
exit 1
|
exit 1
|
}
|
}
|
ExecutePOR_Halt
|
ExecutePOR_Halt
|
puts "done"
|
puts "done"
|
set sizes [GetCPU_ID_SIZE]
|
set sizes [GetCPU_ID_SIZE]
|
puts "Connected: target device has [lindex $sizes 0]B ROM and [lindex $sizes 1]B RAM"
|
puts "Connected: target device has [lindex $sizes 0]B Program Memory and [lindex $sizes 1]B Data Memory"
|
puts ""
|
puts ""
|
|
|
# Load ROM
|
# Load Program Memory
|
set StartAddr [format "0x%04x" [expr 0x10000-$byte_size]]
|
set StartAddr [format "0x%04x" [expr 0x10000-$byte_size]]
|
puts -nonewline "Load ROM... "
|
puts -nonewline "Load Program Memory... "
|
flush stdout
|
flush stdout
|
WriteMemQuick $StartAddr $DataArray
|
WriteMemQuick $StartAddr $DataArray
|
puts "done"
|
puts "done"
|
|
|
# Check Data
|
# Check Data
|
puts -nonewline "Verify ROM... "
|
puts -nonewline "Verify Program Memory... "
|
flush stdout
|
flush stdout
|
if {[VerifyMem $StartAddr $DataArray]} {
|
if {[VerifyMem $StartAddr $DataArray]} {
|
puts "done"
|
puts "done"
|
} else {
|
} else {
|
puts "ERROR"
|
puts "ERROR"
|
}
|
}
|
|
|
# Release device
|
# Release device
|
ReleaseDevice 0xfffe
|
ReleaseDevice 0xfffe
|
|
|