#!/bin/bash
# Script for batch programming of several devices.
# Specifically written for msp430 devices, but if
# you want to program another device just modify
# the program() function
#
# Usage:
# ./patch_program.sh binary_file.ihex
#
# Jose Luis Honorato, 2012
# Checks if the device in a specific port was correctly programmed
# Arguments:
# 1. Dump file name
# 2. Port name
function check_output() {
local port_name=$2
local dump_file_name=$1
echo -n "Device at port $port_name "
# The string "Traceback" is present whenever an exception
# occurred during the programming. It could be replaced
# with any other string
if grep -q "Traceback" $dump_file_name; then
echo "failed!"
else
echo "OK"
fi
}
# Programs a
# Arguments:
# 1. Port name
# 2. Port number
# 3. binary file name
function program() {
local port_name=$1
local port_number=$2
local bin_file_name=$3
# Ready to program, put the output into a file. We cannot add the & symbol
# to this line because we need to wait for the programming to be finished
# in order to analyze the dump file.
echo -e "Programming port $port_name"
export dump_file=$(echo $"dump$port_number.txt")
# Programs and writes the output to the file
(./goodfet.bsl -c $port_name -r -e -I -p -S 38400 $bin_file_name) &> $dump_file
# Check if it was correctly programmed
(check_output $dump_file $port_name) | tee -a script_status.txt
exit
}
function check_input_parameters() {
case "$#" in
0) # If no arguments are passed a default file name is used
echo "No arguments passed, assuming default binary: $default_bin_file_name"
bin_file_name=$default_bin_file_name
;;
*) # If there is more than one argument then discard them
if [ "$#" -gt "1" ]; then
echo "Too many arguments ($#), using only the first one"
fi
bin_file_name=$1
# Get binary file extension and name
local bin_file_extension=$(echo $bin_file_name | cut -d . -f 2)
local bin_file_basename=$(basename $bin_file_name ".$bin_file_extension")
# If it is not ihex then transform it
if [ $bin_file_extension != "ihex" ]; then
local old_bin_file_name=$bin_file_name
bin_file_name="$bin_file_basename.ihex"
echo "Transforming $old_bin_file_name to $bin_file_name"
msp430-objcopy --output-target=ihex $old_bin_file_name $bin_file_name
fi
;;
esac
}
# Main function for batch programming
function main_task() {
echo -e "\nStarting batch programming"
default_bin_file_name="bin.ihex"
bin_bile_name=$default_bin_file_name
check_input_parameters $@
# Remove dumps from previous batches
rm ./dump*.txt
rm ./script_status.txt
# Find all available serial ports: ttyUSB (Linux) and tty.usb (Mac OS)
available_ports=$(find /dev -regextype posix-egrep -iregex "/dev/t.*(ty(USB.|\.usb\w+))")
echo "Found the following ports:"
echo $available_ports
# For each found port program the device
export port_number=0
for current_port in $(echo $available_ports)
do
program $current_port $port_number $bin_file_name &
export port_number=$(($port_number + 1))
done
# We are done here because the programming happens in parallel
echo -e "\nRun cat script_status.txt a bit later to see the results\n"
exit
}
main_task $@