#!/bin/sh
#OpenOCD simple flasher script to use with "make program". Version 0.3.
#It generates temporary command script to the openocd in openocd.flash file, then runs openocd.
#This script requires proper openocd.cfg file in the invokation path.
#It converts elf file to bin format. Path to the *.elf file can be passed as an argument (eg. ../ )
#Any comments are welcome. CeDeROM 2008 ( http://www.tomek.cedro.info )

echo "Searching for *.elf object file in: $1"
TARGET_NAME=`ls $1*.elf | awk '/elf/{split($0,filename,".elf"); print filename[1]}' -`
if [ -z $TARGET_NAME ]; then echo "Cannot find *.elf object file. Exiting..."; exit -1; fi

echo "Converting elf file to binary..."
arm-elf-objcopy $TARGET_NAME".elf"  -O binary $TARGET_NAME".bin"

echo "Generating temporary flasher script for OpenOCD..."

echo "init" 	> openocd.flash
echo "poll on" 		>> openocd.flash
echo "reset halt"	>> openocd.flash
echo "flash probe 0" 	>> openocd.flash
echo "flash erase_sector 0 0 26" >> openocd.flash
echo "flash write_image erase "$TARGET_NAME".bin 0x0" >> openocd.flash
echo "poll off" 	>> openocd.flash
echo "reset run" 	>> openocd.flash

echo "Programming $TARGET_NAME.bin with OpenOCD..."
openocd

rm openocd.flash
