#!/bin/bash

# This script loads the firmware to the FX2 USB device. It then starts
# the device, stops and reads out predefined memory area for diagnostics.
# The device is then started again.
#
# $Id: program_and_start.sh,v 1.1.1.1 2009/06/02 20:25:58 dim Exp $
# $Date: 2009/06/02 20:25:58 $
# $Author: dim $
# $Revision: 1.1.1.1 $
# $Log $

DESCR=`fx2_programmer any any dump_busses | grep UNCONFIGURED | head -n 1`
# If no unconfigured devices found, look for LLRF4
if [ -z "$DESCR" ]; then
  DESCR=`fx2_programmer any any dump_busses | grep 0xfffe | head -n 1`
fi
echo "Using device $DESCR"

BUS=`echo "$DESCR" | cut -f 3 -d \  `
DEVICE=`echo "$DESCR" | cut -f 5 -d \  `

#
# put 8051 into reset
#
fx2_programmer $BUS $DEVICE set 0xE600 1
#
# program 8051
#
fx2_programmer $BUS $DEVICE program llrf4_main.ihx
#
# take 8051 out of reset
#
fx2_programmer $BUS $DEVICE set 0xE600 0

sleep 1

# Search again - device should have renumerated
DESCR=`fx2_programmer any any dump_busses | grep UNCONFIGURED | head -n 1`
# If no unconfigured devices found, look for LLRF4
if [ -z "$DESCR" ]; then
  DESCR=`fx2_programmer any any dump_busses | grep 0xfffe | head -n 1`
fi
echo "Renumerated device: $DESCR"

BUS=`echo "$DESCR" | cut -f 3 -d \  `
DEVICE=`echo "$DESCR" | cut -f 5 -d \  `

# Stop 8051
fx2_programmer $BUS $DEVICE set 0xE600 1
# Dump the debug buffer
fx2_programmer $BUS $DEVICE dump 0x2000 0x100
# Dump the debug buffer
fx2_programmer $BUS $DEVICE dump 0x3000 0x8
# Start again
fx2_programmer $BUS $DEVICE set 0xE600 0
