#!/bin/bash

slot=$1

./wr2rf -s $slot api-init

# Configure TMG IO for RF trigger units default operation
./wr2rf -s $slot api-tmgio  1 1 0
./wr2rf -s $slot api-tmgio  2 1 0
./wr2rf -s $slot api-tmgio  3 1 0
./wr2rf -s $slot api-tmgio  4 1 0
./wr2rf -s $slot api-tmgclk 1 0 1
./wr2rf -s $slot api-tmgclk 2 0 1

# TU starts are input on tmg_clk[2:1]
./wr2rf -s $slot api-vtu-start-lemo 1.1 5
./wr2rf -s $slot api-vtu-start-lemo 1.2 5
./wr2rf -s $slot api-vtu-start-lemo 2.1 6
./wr2rf -s $slot api-vtu-start-lemo 2.2 6

# TU stops are input on tmg_io[2:1] 
./wr2rf -s $slot api-vtu-stop-lemo 1.1 5
./wr2rf -s $slot api-vtu-stop-lemo 1.2 5
./wr2rf -s $slot api-vtu-stop-lemo 2.1 6
./wr2rf -s $slot api-vtu-stop-lemo 2.2 6

# TIMING DELAYS as observed in 774 lab testing crate - OTHER INSTALLATIONS MAY BE DIFFERENT  
# setup the ioupdate delay 
./wr2rf -s $slot api-dds-ioupdate-delay 2 10

# setup RF1 sync_in delay to trigger unit 1 - WARNING !!! DEPENDS ON FREQUENCY
./wr2rf -s 5 api-nco-reset-delay 1 75 5 0xa

# setup RF1 trig1_out delay to external flip-flop
./wr2rf -s 5 api-vtu-output-pad-delay 1.1 0 0x12

# setup RF1 trig2_out delay to external flip-flop
./wr2rf -s 5 api-vtu-output-pad-delay 1.2 0 0x14

# setup RF2 sync_in delay to trigger unit 1 - WARNING !!! DEPENDS ON FREQUENCY
./wr2rf -s 5 api-nco-reset-delay 2 75 4 0x14

# setup RF2 trig1_out delay to external flip-flop
./wr2rf -s 5 api-vtu-output-pad-delay 2.1 0 0xd

# setup RF2 trig2_out delay to external flip-flop
./wr2rf -s 5 api-vtu-output-pad-delay 2.2 0 0xd

# setup front panel debug signals
# RF sync signals are output on tmg_io[4:3] 
#./wr2rf -s $slot api-lemo-dbg-sel tmgio3 nco_reset
#./wr2rf -s $slot api-lemo-dbg-sel tmgio4 rf1_frev

