# run_lm_lte20FDD_RFtest1.sh
# Simple script to run RF test for LTE20 single carrier
# with gain setup
# arg1 - UL number or freq(Mhz)
# arg2 - DL number or freq(Mhz)
# arg3 - subtest# 1 LTE20 TM1.1, noCFR, 2, LTE20 TM1.1 CFR applied
#                 3 LTE20 TM3.1 noCFR, 4 LTE20 TM3.1 CFR applied
# arg4 - AFE7500 Tx runtime calibration can be performed
echo Start RF playback for RFtest..

RFSDK_UPLINK_CN=$1
echo RFSDK_UPLINK $1
RFSDK_DOWNLINK_CN=$2
echo RFSDK_DOWNLINK $2
SIGNAL_TYPE=$3

radio reset
# please uncomment the version desired
# aid loopback
# radio select 2x2-2xLTE20-SC-JESD3b-SD-AIDlpbk
# using external RF to AFE7500 lanes 01
# radio select 2x2-2xLTE20-SC-JESD3b-SD
# using external RF to AFE7500 lanes 03
radio select 2x2-2xLTE20-SC-JESD3b-SD-Lane03
# using external RF to AFE7500 lanes 23
# radio select 2x2-2xLTE20-SC-JESD3b-SD-Lane23

radio init
echo Starting the DSP
playback program 
playback start
sleep 3

echo LTE20 FDD, Turning the radio on in UL at $RFSDK_UPLINK_CN and DL at $RFSDK_DOWNLINK_CN
radio on $RFSDK_UPLINK_CN $RFSDK_DOWNLINK_CN

#RF test, Tx1 out -> split -> Rx1A AND SpecAnal
#         Tx2 out -> split -> Rx2A AND SpecAnal 
#the RF commanded band, or frequency must match the AFE7500 EVM type, and RF component frequency range

read -p "please connect the test equipment, depress a key to continue"
# common RF attenuation settings
radio set afe[0].tx[0].attenuation 3.0
radio set afe[0].tx[1].attenuation 3.0
radio set afe[0].rx[0].attenuation 15.0
radio set afe[0].rx[1].attenuation 15.0

# ideally we would have different optimizations of gain for signal quality EVM, PAR reduction

if [ "$3" == "1" ]
then
   echo RFtest1-1 pattern lte20 TM1.1, no CFR 
   playback load 1 1 /usr/share/radio/patterns/rf-verif/LTE20_TM1p1_C1_m6.bin
   playback load 1 2 /usr/share/radio/patterns/rf-verif/LTE20_TM1p1_C2_m6.bin
   
   # input signal is -6dbfs
   #BBTxgain
   radio set dfe.baseband.tx[0].gain -0.5
   radio set dfe.baseband.tx[1].gain -0.5
   
   #Tx stream gain
   #   SumChain Gain, PreCFR Gain, PostCFRGain
   radio set dfe.stream[0].summerGain 0.0
   radio set dfe.stream[1].summerGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   
   #Rx stream gain
   #   not adjustable currently
   
   #BBRxgain
   radio set dfe.baseband.rx[0].gain 0.0
   radio set dfe.baseband.rx[1].gain 0.0
   
elif [ "$3" == "2" ]
then
   echo RFtest1-2 pattern lte20 TM1.1, CFR applied 
   playback load 1 1 /usr/share/radio/patterns/rf-verif/LTE20_TM1p1_C1_m6.bin
   playback load 1 2 /usr/share/radio/patterns/rf-verif/LTE20_TM1p1_C2_m6.bin
   
   # input signal is -6dbfs
   #BBTxgain, setup for ~7.5dbPAR using CFR clipping
   radio set dfe.baseband.tx[0].gain 3.5
   radio set dfe.baseband.tx[1].gain 3.2
   
   #Tx stream gain, set for Gain to raise peak above clipping peak threshold
   #   SumChain Gain, PreCFR Gain, PostCFRGain
   radio set dfe.stream[0].summerGain 0.0
   radio set dfe.stream[1].summerGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   
   #Rx stream gain
   #   not adjustable currently
   
   #BBRxgain
   radio set dfe.baseband.rx[0].gain 0.0
   radio set dfe.baseband.rx[1].gain 0.0
   
elif [ "$3" == "3" ]
then
   echo pattern lte20 TM3.1, no CFR 
   playback load 1 1 /usr/share/radio/patterns/rf-verif/LTE20_TM3p1_C1_m6.bin
   playback load 1 2 /usr/share/radio/patterns/rf-verif/LTE20_TM3p1_C2_m6.bin
   
   # input signal is -6dbfs
   #BBTxgain
   radio set dfe.baseband.tx[0].gain -0.5
   radio set dfe.baseband.tx[1].gain -0.5
   
   #Tx stream gain
   #   SumChain Gain, PreCFR Gain, PostCFRGain
   radio set dfe.stream[0].summerGain 0.0
   radio set dfe.stream[1].summerGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   
   #Rx stream gain
   #   not adjustable currently
   
   #BBRxgain
   radio set dfe.baseband.rx[0].gain 0.0
   radio set dfe.baseband.rx[1].gain 0.0
   
elif [ "$3" == "4" ]
then
   echo pattern lte20 TM3.1, CFR applied 
   playback load 1 1 /usr/share/radio/patterns/rf-verif/LTE20_TM3p1_C1_m6.bin
   playback load 1 2 /usr/share/radio/patterns/rf-verif/LTE20_TM3p1_C2_m6.bin
   
   # input signal is -6dbfs
   #BBTxgain, setup for ~7.5dbPAR using CFR clipping
   radio set dfe.baseband.tx[0].gain 3.4
   radio set dfe.baseband.tx[1].gain 2.4
      
   #Tx stream gain
   #   SumChain Gain, PreCFR Gain, PostCFRGain
   radio set dfe.stream[0].summerGain 0.0
   radio set dfe.stream[1].summerGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.preGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   radio set dfe.stream[0].cfr.postGain 0.0
   
   #Rx stream gain
   #   not adjustable currently
   
   #BBRxgain
   radio set dfe.baseband.rx[0].gain 0.0
   radio set dfe.baseband.rx[1].gain 0.0
fi
sleep 1

#AFE7500 runtime calibration, note requires feedback to be connected and normalized
if [ "$4" == "1" ]
then
   afecmd calibration 0 TX1
   afecmd calibration 0 TX1
   
   afecmd calibration 0 TX2
   afecmd calibration 0 TX2
fi

echo BB Powermeter, CFR input, and DPD input
   #read bb power meter
   radio pmread

   #read capture buffer, DPD input
   dpdcmd Init
   echo read capture buffer CDFR output
   dpdcmd SignalPowerRead /etc/radio/dpd/SignalPowerRead_dpdin.json
   dpdcmd DeInit
   
read -p "please measure the 1st Tx antenna, depress a key to continue"

read -p "please measure the 2ndTx antenna, depress a key to continue"

read -p " depress a key to shutdown playback and turn radio off "

# cd /usr/share/radio/patterns/rf-verif
# rm -f LTE20_TM*.bin

playback stop
radio reset

