#!/bin/bash
#udevadm info --name=/dev/ttyACMx
# /etc/udev/rules/99-usb-serial.rules
#KERNEL=="ttyUSB*", ATTRS{idVendor}=="0557", SYMLINK+="5to1card_0"
#KERNEL=="ttyUSB*", ATTRS{idVendor}=="1a86", SYMLINK+="chipaddress_1"
#udevadm control --reload-rules
#
# select chipaddress \x0n n = 0-7
# stty -F /dev/ttyUSB1 230400 cs8 -cstopb -parenb ixoff -icrnl -opost -onlcr -isig -icanon -echo -echoe
# echo -n \x0n > /dev/ttyUSB1
# if arguments:
# 1 - chipnumber (as written on chip)
datadir=/home/jum/wdir/ilcdaq/data
#datadir=/home/jum/work/ilc/readout/data
# VXI settings
#=============
vxipath=$HOME/ilc/ilcdaq/chiptests/vxi11/bin
IP="192.168.4.10"
#
# arguments
#===========
if [ $# == 0 ]; then
    chipnumber=0
elif [ $# == 1 ]; then
    chipnumber=$1
else
    echo "Wrong number of arguments: " $#
    exit
fi
#
# create datadirectiry - if not existing
if [ ! -e ${datadir}/chip${chipnumber} ]; then
    echo " ${datadir}/chip${chipnumber} does not exist - creating it"
    mkdir ${datadir}/chip${chipnumber}
    iret=$?
    if [ $iret != 0 ]; then
	echo mkdir exiting with status $iret
	exit
    fi
fi
/bin/rm ${datadir}/chip${chipnumber}/*.csv
if [ -e infiles.csv ]; then
    /bin/cat csv/infiles.csv | sed -e "s/saltro0/saltro${1}/"g > ${datadir}/chip${chipnumber}/infiles.csv
    /bin/cp csv/*.txt ${datadir}/chip${chipnumber}/.
fi
#
# setup serial interface
# baud rate to 38400, 8 bits, 1 stop bit, no parity:
stty -F $LVCARD 38400 cs8 -cstopb -parenb ixoff -icrnl -opost -onlcr -isig -icanon -echo -echoe
iret=$?
if [ $iret != 0 ]; then
    echo 5to1card stty exiting with status $iret
    exit
fi
#
# setup chipaddress adapter
stty -F $CHIPADDRESS 230400 cs8 -cstopb -parenb ixoff -icrnl -opost -onlcr -isig -icanon -echo -echoe
iret=$?
if [ $iret != 0 ]; then
    echo chipaddress stty exiting with status $iret
    exit
fi
echo -n \x07 > $CHIPADDRESS
#
# initialize SRU
./srupup --sruif $SRUIF
#
# initialize lvcard
filename=${datadir}/chip${chipnumber}/lvcard.csv
echo $filename
./lvcard --init --filecsv $filename
iret=$?
if [ $iret != 0 ]; then
    echo lvcard --init exiting with status $iret
    exit
fi
#
# power on protomcm
./lvcard --power 1 --filecsv $filename
iret=$?
if [ $iret != 0 ]; then
    echo lvcard --power 1 exiting with status $iret
    # lvcard --power 1 exiting with status 2
    # output 20220827,122250,1,3108,3099,818,947,2,0,0
#    exit
fi
#
# switch on SALTRO
echo switch on SALTRO
./mcmpca16 --fec 0 --preamp 0 --gain 0 --shaper 0 --shutdown 0 --polarity 1 --biasctrl 1 --grst 1 --write
iret=$?
if [ $iret != 0 ]; then
    echo mcmpca16 exiting with status $iret
#    exit
fi
./lvcard --saltro 1 --filecsv $filename
iret=$?
if [ $iret != 0 ]; then
    echo lvcard --saltro 1 exiting with status $iret
    echo will try to power off
#    ./lvcard --saltro 0 --filecsv $filename
    ./lvcard --power 0 --filecsv $filename
    exit
fi
#
# do a global reset of SALTRO
./mcmwrreg --fec 0 --register 7 --write --value 1
#
# Recall setup 1 on pulser
$vxipath/vxi11_send $IP "*RCL 1"
sleep 1
$vxipath/vxi11_send $IP "OUTPUT1:STATE ON"
$vxipath/vxi11_send $IP "OUTPUT2:STATE ON"
date > ${datadir}/chip${chipnumber}/chiptester.log
echo "INIT DONE" >> ${datadir}/chip${chipnumber}/chiptester.log
