#!/bin/bash
#==============================================================
#  Name:  cavBpmRcvrRegSet
#
#  Rem:   
# 
#  Usage: 
#
#  Auth:  13-Sep-2013,  S. Hoobler (SONYA)
#  Rev:   dd-mmm-yyyy,  Whoever    (user):
#
#==============================================================

function userhelp {
    echo "   
    Set the register values for a cavity BPM receiver PLL.
    File format must be correct (output of HMC783 simulation program).
    (This only writes the registers that the BPM software suports.
     There are unused registers that are ignored.)

         Name:  cavBpmRcvrRegSet
         Usage: cavBpmRcvrRegSet [-h] bpm filename
         Arg:   bpm: BPM control system name, i.e. BPMS:UND1:3395
                filename: name of file containing register values to write
                          (include path to file if needed)
         Ret:   zero on success, non-zero on error    

    Example:

        $ cavBpmRcvrRegSet BPMS:UND1:3395 testfile
        Old : BPMS:UND1:3395:PLL_REG_RST         134
        New : BPMS:UND1:3395:PLL_REG_RST         134
        Old : BPMS:UND1:3395:PLL_REG_REFDIV      2
        New : BPMS:UND1:3395:PLL_REG_REFDIV      2
        Old : BPMS:UND1:3395:PLL_REG_FREQINT     95
        New : BPMS:UND1:3395:PLL_REG_FREQINT     95
        Old : BPMS:UND1:3395:PLL_REG_SEED        24724
        New : BPMS:UND1:3395:PLL_REG_SEED        24724
        Old : BPMS:UND1:3395:PLL_REG_CFG         7352199
        New : BPMS:UND1:3395:PLL_REG_CFG         7352199
        Old : BPMS:UND1:3395:PLL_REG_LKD         1274
        New : BPMS:UND1:3395:PLL_REG_LKD         1274
        Old : BPMS:UND1:3395:PLL_REG_ANLG        12767
        New : BPMS:UND1:3395:PLL_REG_ANLG        12767
        Old : BPMS:UND1:3395:PLL_REG_CP          4980224
        New : BPMS:UND1:3395:PLL_REG_CP          4980224
        Old : BPMS:UND1:3395:PLL_REG_OPAMP       0
        New : BPMS:UND1:3395:PLL_REG_OPAMP       0
        Old : BPMS:UND1:3395:PLL_REG_PFD         122
        New : BPMS:UND1:3395:PLL_REG_PFD         122
        Old : BPMS:UND1:3395:PLL_REG_VCO         0
        New : BPMS:UND1:3395:PLL_REG_VCO         0
        Old : BPMS:UND1:3395:PLL_REG_GPO         10
        New : BPMS:UND1:3395:PLL_REG_GPO         10
        Old : BPMS:UND1:3395:PLL_REG_FREQFRAC    11983726
        New : BPMS:UND1:3395:PLL_REG_FREQFRAC    11983726
        Old : BPMS:UND1:3395:PLL_REG_SET_GO      
        New : BPMS:UND1:3395:PLL_REG_SET_GO      
    "
}

while getopts h opt
do
  case "$opt" in
      h) userhelp; exit 0;;
      *) echo "Unknown arguments"; userhelp; exit 1;;
  esac
done

# Check for input arguments
if [ $# != 2 ]
then
    echo "Missing argument(s)"; userhelp; exit 1;
fi

bpm=$1
file=$2

go=0

while IFS=" " read -r -a tok
do
  key=${tok[0]}
  reg=${tok[1]}
  val=${tok[2]}

  if [ ${key} = "REG" ]
  then

      case ${reg} in
	  1) str="RST";;
	  2) str="REFDIV";;
	  3) str="FREQINT";;
	  4) str="FREQFRAC";;
	  5) str="SEED";;
	  6) str="CFG";;
	  7) str="LKD";;
	  8) str="ANLG";;
	  9) str="CP";;
	  A) str="OPAMP";;
	  B) str="PFD";;
	  C) str="VCO";;
	  D) str="GPO";;
	  *) str=0;;
      esac

      # Set register setpoints (actual write comes at end)
      if [ ${str} != 0 ]
      then
	  caput ${bpm}:PLL_REG_${str} 0x${val}
	  go=1
      fi

  fi

done < $file

# Only do register write if there are changes
if [ ${go} > 0 ]
then
    caput ${bpm}:PLL_REG_SET_GO 1
fi

exit 0