#!/bin/bash
# example
#./smalltpc-setup-mcmx.sh 2 0 0 1008 0 7FFE
#
# inputs:
# 1 FEC
# 2 gain
# 3 shaper
# 4 samples
# 5 mcmchip
# 6 channels
#
fec=$1
gain=$2
shaper=$3
samples=$4
mcmchip=$5
channels=$6
#
echo $fec
echo $gain
echo $shaper
echo $samples
echo $mcmchip
echo $channels
title="setup-mcmx"
polarity=1
shutdown=0
bindir=/home/jum/ilc/ilcdaq/chiptests
${bindir}/mcmwrreg --fec $fec --register 7 --write --value 1
#ADC CLKDIV
#2 bits. Used to select which clock to use as adc_clk...
#00-" 0 = div by 2 (20 MHz)" Current: 0.367A, 3.05A
#01-" 1 = div by 4 (10 MHz)" Current: 0.258A, 2.9A
#10-" 2 = div by 8 ( 5 MHz)" Current: 0.203A,2.84A
#11-" 3 = div by 1 (40 MHz)"
clkdiv=0
#
# Read same number of samples and channels from all 8 chips
# samples is in decimal
# "channels" are a 16-bit bitmap in hex, 0 is an active channel
# we shift the sampling between chips
shift1=`printf '%x' ${samples}`
shift2=`printf '%x' $((samples + 0x400))`
shift3=`printf '%x' $((samples + 0x800))`
shift4=`printf '%x' $((samples + 0xC00))`
echo $shift1
echo $shift2
echo $shift3
echo $shift4
# 0x100 (4) = din-vpd-fpd
mode=0
ped=0
pedestal=`printf '%x' ${ped}`
thr=0
threshold=`printf '%x' ${thr}`
post=$((7 << 14))
pre=$((3 << 17))
above=$((0 << 12))
#above=$((1 << 12))
#
#
date > smalltpc-${title}.out
echo "SMALLTPC MCM $fecs GAIN $gain SHAPER $shaper SAMPLES $samples CHANNELS $channels" >> smalltpc-${title}.out
#
#
# Prepare MCM
#===============
traceid=`${bindir}/mcmdumpregs --fec $fec | grep TRACEID0 | cut -f 4 -d ' '`
echo "TRACEID0 $fec : $traceid" >> smalltpc-${title}.out
${bindir}/mcmwrreg --fec $fec --register 1 --write --value $clkdiv
#Disable all channels
for chip in {0..7}
do
    mcmregister=`printf '%x' $((0x10 + $chip))`
    ${bindir}/mcmwrreg --fec $fec --register $mcmregister --write --value FFFF
done
# Chip to use
mcmregister=`printf '%x' $((0x10 + $mcmchip))`
${bindir}/mcmwrreg --fec $fec --register $mcmregister --write --value $channels
#
${bindir}/mcmpca16 --fec $fec --preamp 0 --gain ${gain} --shaper ${shaper} --shutdown $shutdown --polarity $polarity --biasctrl 1 --grst 1 --write --verify
${bindir}/mcmdumpregs --fec $fec >> smalltpc-${title}.out
#
# prepare SALTRO chip
#================================
# Load DPCFG with mode = f(t) - fpd = 10,  postsamples (0-7), presamples (0-3), above threshold (0-3)
# To set back to normal mode mode=0.
value=`printf '%x' $((mode + post + pre + above))`
echo "VALUE $value" >>  smalltpc-${title}.out
echo "SALTRo FEC $fec"  >> smalltpc-${title}.out
# we shift the sampling between chips
${bindir}/saltrowrrdreg --fec $fec --chip 0 --register A --channel 0 --write $shift1 --verify
${bindir}/saltrowrrdreg --fec $fec --chip 1 --register A --channel 0 --write $shift2 --verify
${bindir}/saltrowrrdreg --fec $fec --chip 2 --register A --channel 0 --write $shift3 --verify
${bindir}/saltrowrrdreg --fec $fec --chip 3 --register A --channel 0 --write $shift4 --verify
${bindir}/saltrowrrdreg --fec $fec --chip 4 --register A --channel 0 --write $shift1 --verify
${bindir}/saltrowrrdreg --fec $fec --chip 5 --register A --channel 0 --write $shift2 --verify
${bindir}/saltrowrrdreg --fec $fec --chip 6 --register A --channel 0 --write $shift3 --verify
${bindir}/saltrowrrdreg --fec $fec --chip 7 --register A --channel 0 --write $shift4 --verify
# Mode
${bindir}/saltrowrrdreg --bcast --fec $fec --chip $mychip --register B --channel 0 --write $value --verify
for ch in {0..15}; do
# common pedestal
    ${bindir}/saltrowrrdreg  --fec $fec --chip $mychip --register 13 --channel $ch --write $pedestal --verify
# common threshold
    ${bindir}/saltrowrrdreg  --fec $fec --chip $mychip --register 8 --channel $ch --write $threshold --verify
done
date >> smalltpc-${title}.out
echo "DONE"
