#!/bin/bash
# example
#./smalltpc-test-lvcard2-ssh.sh "0 1" 0 0 1008 0 300 1
#
# inputs:
# 1 FECs
# 2 gain
# 3 shaper
# 4 samples
# 5 channels
# 6 events
# 7 runnb
#fecs="0 1 30"
RMTHOST=192.168.4.12
RMTUSER=mholl
RMTDIR=/home/mholl/smalltpc/lvcard2
CSVDIR=${RMTDIR}/csv
DEVICE=/dev/lvcard2_1
#
mcms="--mcm4 --mcm5"
#mcms="--mcm3"
fecs=$1
gain=$2
shaper=$3
samples=$4
channels=$5
events=$6
nb=$7
#source=$7
fecro=""
for fec  in $fecs
do
    echo "DEC $fec"
    fecro="${fecro} --fec ${fec}"
#    ${bindir}/mcmwrreg --fec $fec --register 7 --write --value 1
done
echo "FECRO $fecro"
for mcm  in $mcms
do
    echo "MCM $mcm"
done
title="chamber-fec01-debug"
#title="lvcard-mcm1-fec0-debug"
echo $gain
echo $shaper
echo $samples
echo $channels
echo $events
echo $source
polarity=1
mydir=/home/jum/wdir/ilcdaq/root/files
mydata=/home/jum/wdir/ilcdaq/data
bindir=/home/jum/ilc/ilcdaq/chiptests
for fec  in $fecs
do
    ${bindir}/mcmwrreg --fec $fec --register 7 --write --value 1
done
#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
#
shutdown=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
traceid=`${bindir}/mcmdumpregs --fec $1 | grep TRACEID0 | cut -f 4 -d ' '`
#${bindir}/mcmwrreg --fec $1 --register 1 --write --value $clkdiv
# 0x100 (4) = din-vpd-fpd
mode=0
#pedestal=0
#threshold=0
#pedestal=50
#threshold=150
ped=0
#ped=50
#ped=100
pedestal=`printf '%x' ${ped}`
thr=0
#thr=150
#thr=100
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
#===============
for fec in $fecs
do
    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
    for chip in {0..7}
    do
	mcmregister=`printf '%x' $((0x10 + $chip))`
	${bindir}/mcmwrreg --fec $fec --register $mcmregister --write --value $channels
    done
    ${bindir}/mcmpca16 --fec $fec --preamp 0 --gain ${gain} --shaper ${shaper} --shutdown $shutdown --polarity $polarity --biasctrl 1 --grst 1 --write --verify
    echo "MCM${fec}"
    ${bindir}/mcmdumpregs --fec $fec >> smalltpc-${title}.out
done

#
# 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
for fec in $fecs
do
    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 0 --register B --channel 0 --write $value --verify
    ${bindir}/saltrowrrdreg --bcast --fec $fec --chip 1 --register B --channel 0 --write $value --verify
    ${bindir}/saltrowrrdreg --bcast --fec $fec --chip 2 --register B --channel 0 --write $value --verify
    ${bindir}/saltrowrrdreg --bcast --fec $fec --chip 3 --register B --channel 0 --write $value --verify
    ${bindir}/saltrowrrdreg --bcast --fec $fec --chip 4 --register B --channel 0 --write $value --verify
    ${bindir}/saltrowrrdreg --bcast --fec $fec --chip 5 --register B --channel 0 --write $value --verify
    ${bindir}/saltrowrrdreg --bcast --fec $fec --chip 6 --register B --channel 0 --write $value --verify
    ${bindir}/saltrowrrdreg --bcast --fec $fec --chip 7 --register B --channel 0 --write $value --verify
    for ch in {0..15}; do
# common pedestal
	${bindir}/saltrowrrdreg  --fec $fec  --chip 0 --register 13 --channel $ch --write $pedestal --verify
	${bindir}/saltrowrrdreg  --fec $fec  --chip 1 --register 13 --channel $ch --write $pedestal --verify
	${bindir}/saltrowrrdreg  --fec $fec  --chip 2 --register 13 --channel $ch --write $pedestal --verify
	${bindir}/saltrowrrdreg  --fec $fec  --chip 3 --register 13 --channel $ch --write $pedestal --verify
	${bindir}/saltrowrrdreg  --fec $fec  --chip 4 --register 13 --channel $ch --write $pedestal --verify
	${bindir}/saltrowrrdreg  --fec $fec  --chip 5 --register 13 --channel $ch --write $pedestal --verify
	${bindir}/saltrowrrdreg  --fec $fec  --chip 6 --register 13 --channel $ch --write $pedestal --verify
	${bindir}/saltrowrrdreg  --fec $fec  --chip 7 --register 13 --channel $ch --write $pedestal --verify
# common threshold
	${bindir}/saltrowrrdreg  --fec $fec --chip 0 --register 8 --channel $ch --write $threshold --verify
	${bindir}/saltrowrrdreg  --fec $fec --chip 1 --register 8 --channel $ch --write $threshold --verify
	${bindir}/saltrowrrdreg  --fec $fec --chip 2 --register 8 --channel $ch --write $threshold --verify
	${bindir}/saltrowrrdreg  --fec $fec --chip 3 --register 8 --channel $ch --write $threshold --verify
	${bindir}/saltrowrrdreg  --fec $fec --chip 4 --register 8 --channel $ch --write $threshold --verify
	${bindir}/saltrowrrdreg  --fec $fec --chip 5 --register 8 --channel $ch --write $threshold --verify
	${bindir}/saltrowrrdreg  --fec $fec --chip 6 --register 8 --channel $ch --write $threshold --verify
	${bindir}/saltrowrrdreg  --fec $fec --chip 7 --register 8 --channel $ch --write $threshold --verify
    done
done
echo "reading LVCARD temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --lvtemp 4 --mcmaddress 0 -f  ${CSVDIR}/lvtemp.csv"
echo "reading MCM temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --mcmtemp 4 --mcmaddress 0 -f ${CSVDIR}/mcmtemp.csv"
echo "Reading ADCs"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --readadc 4 --mcmaddress 0 -f  ${CSVDIR}/readadc.csv"
echo "Set trigger..."
#time ${bindir}/srureadout-test --fec 0  --data=${mydata}/readout.dat --rate 100 --event $events --softtrigger --sruif $SRUIF > srureadout-test.out
# software trigger
echo "Software trigger..."
echo ${fecro}
time ${bindir}/srureadout ${fecro}  --data=${mydata}/readout.dat --rate 0 --event $events --softtrigger --sruif $SRUIF > srureadout.out
# hardware trigger
#time ${bindir}/srureadout ${fecro}  --data=${mydata}/readout.dat --rate 0 --event $events  --sruif $SRUIF > srureadout.out
date >> smalltpc-${title}.out
echo "reading LVCARD temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --lvtemp 5 --mcmaddress 0 -f  ${CSVDIR}/lvtemp.csv"
echo "reading MCM temperatures"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --mcmtemp 5 --mcmaddress 0 -f ${CSVDIR}/mcmtemp.csv"
#for fec in $fecs
#do
#    ${bindir}/mcmpca16 --fec $fec --preamp 0 --gain 0 --shaper 0 --shutdown 1 --polarity $polarity --biasctrl 0 --grst 1 --write --verify
#done
echo "Reading ADCs"
ssh ${RMTUSER}@${RMTHOST} "${RMTDIR}/lvcard2 --readadc 5 --mcmaddress 0 -f  ${CSVDIR}/readadc.csv"

#${bindir}/sruunpack --data ${mydata}/readout.dat --root


${bindir}/sruunpack --data ${mydata}/readout.dat --root > sruunpack.out
cp -a ${mydata}/readout.root ${mydata}/smalltpc/${title}-clk${clkdiv}-ped${ped}-thr${thr}-g${gain}-s${shaper}-p${polarity}-${samples}-${nb}.root
cp -a ${mydata}/readout.dat ${mydata}/smalltpc/${title}-clk${clkdiv}-ped${ped}-thr${thr}-g${gain}-s${shaper}-p${polarity}-${samples}-${nb}.dat
echo "DONE"
