#!/bin/bash

freq_LHCPILOT=43347.158955
freq_SFTION1=42966.157844
freq_SFTPRO2=43278.463683
freq=$freq_SFTION1
freq=$freq_LHCPILOT
freq=$freq_SFTPRO2
freq_wrclk=125000000
prod=`echo "$freq*(2^48)" | bc -q`
ftw_h1=`echo "$prod/($freq_wrclk)" | bc -q`
ftw_hex=`echo "obase=16;ibase=10; $ftw_h1" | bc -q`

# After calling wr2rf_init.sh for both tx and rx wr2rf_vme cards. The sequence outlined below
# can be used to "simulate" a cycle of the sps. The slot numbers used are specific to cfv-774-celma4
# card configuration and should be adapted...
# slot 2 is TX, slots 4+5 RX
./wr2rf -s 4 tmgclk-oe 0x3
./wr2rf -s 4 tmgclk-term 0x0
./wr2rf -s 4 lemo-dbg-sel tmgclk1 rf_rst
./wr2rf -s 4 lemo-dbg-sel tmgio4 rf1_nco_reset_cdelayed
./wr2rf -s 4 lemo-dbg-sel tmgclk2 rf2_nco_reset_cdelayed
./wr2rf -s 4 api-vtu-reset 1.1 1
./wr2rf -s 4 api-vtu-reset 1.1 0
./wr2rf -s 4 vtu-out 1.1 trig_reset 0
./wr2rf -s 4 api-vtu-reset 2.1 1
./wr2rf -s 4 api-vtu-reset 2.1 0
./wr2rf -s 4 vtu-out 2.1 trig_reset 0

./wr2rf -s 5 tmgclk-oe 0x3
./wr2rf -s 5 tmgclk-term 0x0
./wr2rf -s 5 lemo-dbg-sel tmgclk1 rf_rst
./wr2rf -s 5 lemo-dbg-sel tmgio4 rf1_nco_reset_cdelayed
./wr2rf -s 5 lemo-dbg-sel tmgclk2 rf2_nco_reset_cdelayed
./wr2rf -s 5 api-vtu-reset 1.1 1
./wr2rf -s 5 api-vtu-reset 1.1 0
./wr2rf -s 5 vtu-out 1.1 trig_reset 0
./wr2rf -s 5 api-vtu-reset 2.1 1
./wr2rf -s 5 api-vtu-reset 2.1 0
./wr2rf -s 5 vtu-out 2.1 trig_reset 0

b=16
# reset the RX wr2rf trigger unit
while true; do    
    ./wr2rf -s 4 api-vtu-softstop 1.1 
    ./wr2rf -s 4 api-vtu-softstop 2.1 
    ./wr2rf -s 5 api-vtu-softstop 1.1 
    ./wr2rf -s 5 api-vtu-softstop 2.1 

     ./wr2rf -s 4 api-vtu-window 1.1 $b 4620 1
     ./wr2rf -s 4 api-vtu-window 2.1 $b 4620 1
     ./wr2rf -s 4 api-vtu-softstart 1.1 
     ./wr2rf -s 4 api-vtu-softstart 2.1 

     ./wr2rf -s 5 api-vtu-window 1.1 $b 4620 1
     ./wr2rf -s 5 api-vtu-window 2.1 $b 4620 1
     ./wr2rf -s 5 api-vtu-softstart 1.1 
     ./wr2rf -s 5 api-vtu-softstart 2.1 

     usleep 10
     
     ./wr2rf -s 2 ftw-send1 0x$ftw_hex reset
#     usleep 2000
     usleep 5000
     
done;


