#!/bin/bash

slot=$1

# Check we have basic access to the register space over VME
./wr2rf -s $slot hwver

# Stop the vtus if they are currently running
./wr2rf -s $slot api-vtu-softstop 1.1
./wr2rf -s $slot api-vtu-softstop 1.2

# Basic programming of each VTU
# TU 1 programmed to generate frev, 
# TU 2 programmed to generate bunch clock
# With this setup, the first frev occurs at the start of the second turn
# and the first bunch clock occurs at the start of the third turn
#sh vtu_init.sh $slot 1.1 4620 4620 infinite

./wr2rf -s $slot api-vtu-infinite 1.1 4620 4620
./wr2rf -s $slot api-vtu-highfreq 1.2 4620    5

# Enable diag units and counters
./wr2rf -s $slot api-diag-enable 1.1 50000
./wr2rf -s $slot api-diag-enable 1.2 50000
#./wr2rf -s $slot vtu 1.1 diag en 50ms
#./wr2rf -s $slot vtu 1.2 diag en 50ms

echo "Bring trigger units out of reset" 
./wr2rf -s $slot api-vtu-output-reset 1.1 0
./wr2rf -s $slot api-vtu-output-reset 1.2 0

echo "Turn off the trigger unit shaper circuit for bunch clock duty cycle improvement"
./wr2rf -s $slot api-vtu-output-shaper 1.2 0

echo "Dump trigger units output status " 
#./wr2rf -s $slot vtu-out 1.1
#./wr2rf -s $slot vtu-out 1.2

# Use a software start signal
./wr2rf -s $slot api-vtu-softstart 1.1
./wr2rf -s $slot api-vtu-softstart 1.2

echo "Trigger Units waiting for a sync pulse - via RFframe FTW ..."
