-- Chirp mode setup and RX data capture/postproc

RDIF_SCRAMBLER_ENABLE = 0  -- 0/1



local tx_channel_en_map = 3
local num_rx_channels = 3

local sample_rate_divider = 20  -- 5Msps
local slope_mhz_per_us = 16.67
local ramp_end_time_us = 60
local idle_time_us = 6
local adc_start_time_samples = 24 -- 25

local num_samples_per_chirp = 256
local num_chirps_per_burst = 2
local burst_period_us = 300
local num_bursts_per_frame = 32
local frame_period_us = 200000
local num_frames = 10
local mimoMode = 1; 
local CRD_Slope = 0xB00;

local rf_fact_cal_start_freq_mhz = 76000 -- 78500 -- 60500
local lo_freq_mhz = 76000
local tx_backoff_db = 0
local rx_gain_db = 36

local resolution_mode = 0  -- 0=low, 1=high
local device_is_14xx = 1  -- 0=64xx, 1=14xx
local enable_sideband_info = 0  -- 0=ADC_ONLY, 1=ADC+sideband_info


-- TX/RX channel config
xw_status=mws.fecRfPwrOnOff(0,{h_RxChCtrlBitMask=(2^num_rx_channels)-1,h_TxChCtrlBitMask=tx_channel_en_map,c_MiscCtrl=1,h_Reserved1=0,w_Reserved2=0,c_Reserved=0})
assert(xw_status == 0, "ERROR: API failed")

-- RF Factory Cal to set RX gain, TX backoff
local h_CalCtrlBitMask
if (LOAD_ATE_CAL_FW_PATCH == 1) then
    -- Enable RX IFA calibration (requires ATE_CAL patch)
    h_CalCtrlBitMask = 238
else
    -- Disable RX IFA calibration
    h_CalCtrlBitMask = 206
end
xw_status, w_freqCode = mws.sensConvFreqToCode(0, resolution_mode, device_is_14xx, rf_fact_cal_start_freq_mhz)
assert(xw_status == 0, "ERROR: API failed")
xw_status, response=mws.fecRfBootCal(0,{h_CalCtrlBitMask=h_CalCtrlBitMask,c_MiscCalCtrl=0,c_CalRxGainSel=rx_gain_db,c_CalTxBackOffSel0=tx_backoff_db,c_CalTxBackOffSel1=tx_backoff_db,c_CalTxBackOffSel2=0,c_CalTxBackOffSel3=0,h_CalRfFreq=w_freqCode,xh_CalRfSlope=0,c_TxPwrCalTxEnaMask0=3,c_TxPwrCalTxEnaMask1=1,c_TxPwrCalTxEnaMask2=0,c_TxPwrCalTxEnaMask3=0,w_Reserved1=0,c_Reserved2=0,w_Reserved3=0,w_Reserved4=0})
assert(xw_status == 0, "ERROR: API failed")

xw_status, response=mws.fecRfRuntimeCal(0,{h_CalCtrlBitMask=206,h_Reserved0=0,c_TempBinIndex=8,c_Reserved1=0,h_Reserved2=0,w_Reserved3=0,w_Reserved4=0,w_Reserved5=0})
assert(xw_status == 0, "ERROR: API failed")

-- Chirp/profile common configuration
xw_status, hpf_fast_init_duration_us = mws.sensConvTimeToCode(0,0,1.5)  -- parameter uses low resolution (100ns LSB) only
assert(xw_status == 0, "ERROR: API failed")
xw_status, ramp_end_time_code = mws.sensConvTimeToCode(0,resolution_mode,ramp_end_time_us)
assert(xw_status == 0, "ERROR: API failed")
xw_status=mws.sensChirpPfComnCfg(0,{c_DigOutputSampRate=sample_rate_divider,c_DigOutputBitsSel=0,c_DfeFirSel=0,c_VcoMultiChipMode=0,h_NumOfAdcSamples=num_samples_per_chirp,c_ChirpTxMimoPatSel=mimoMode,c_MiscSettings=0,c_HpfFastInitDuration=hpf_fast_init_duration_us,c_Reserved=0,h_CrdNSlopeMag=CRD_Slope,w_Reserved=0,h_ChirpRampEndTime=ramp_end_time_code,c_ChirpRxHpfSel=1,c_Reserved2=0,w_Reserved3=0,w_Reserved4=0,w_Reserved5=0,w_Reserved6=0})
assert(xw_status == 0, "ERROR: API failed")

-- Chirp/profile timing configuration
xw_status, idle_time_code = mws.sensConvTimeToCode(0,resolution_mode,idle_time_us)
assert(xw_status == 0, "ERROR: API failed")
xw_status, tx_start_time_us = mws.sensConvTimeToCode(0,1,0)  -- parameter uses high resolution (20ns LSB) only
assert(xw_status == 0, "ERROR: API failed")
xw_status, w_slopeCode = mws.sensConvSlopeToCode(0,device_is_14xx,slope_mhz_per_us)
assert(xw_status == 0, "ERROR: API failed")
xw_status, w_freqCode = mws.sensConvFreqToCode(0, resolution_mode, device_is_14xx, lo_freq_mhz)
assert(xw_status == 0, "ERROR: API failed")
adc_start_time_code = adc_start_time_samples * 2^10  -- Set Num ADC Skip Samples (bits[15:10])
xw_status=mws.sensChirpPfTimeCfg(0,{h_ChirpIdleTime=idle_time_code,h_ChirpAdcStartTime=adc_start_time_code,xh_ChirpTxStartTime=tx_start_time_us,xh_ChirpRfFreqSlope=w_slopeCode,w_ChirpRfFreqStart=w_freqCode,h_ChirpTxEnSel=tx_channel_en_map,h_ChirpTxBpmEnSel=0,w_Reserved1=0,w_Reserved2=0,w_Reserved3=0,w_Reserved4=0,w_Reserved5=0})
assert(xw_status == 0, "ERROR: API failed")

-- Frame configuration
xw_status, burst_period_code = mws.sensConvTimeToCode(0,resolution_mode,burst_period_us)
assert(xw_status == 0, "ERROR: API failed")
xw_status, w_ftPeriodCode = mws.sensConvFtPeriodToCode(0, 10240, frame_period_us)
assert(xw_status == 0, "ERROR: API failed")
xw_status=mws.sensFrameCfg(0,{h_NumOfChirpsInBurst=num_chirps_per_burst,c_NumOfChirpsAccum=0,c_MiscSetupMask=0,w_BurstPeriodicity=burst_period_code,h_NumOfBurstsInFrame=num_bursts_per_frame,h_Reserved1=0,w_FramePeriodicity=w_ftPeriodCode,h_NumOfFrames=num_frames,h_Reserved2=0,w_FrameEvent0TimeCfg=0,w_FrameEvent1TimeCfg=0,w_Reserved3=0})
assert(xw_status == 0, "ERROR: mws.sensFrameCfg API failed")

-- Enable RDIF
if (enable_sideband_info == 1) then
    xw_status=mws.fecRdifctrl(0,{c_RdifEnable=10,c_RdifCfg=41,h_RdifSampleCount=num_samples_per_chirp,c_TestPatternEn=0,c_LaneRateCfg=0,h_Reserved2=0,h_TestPatrnInitCodeCh0=20,h_TestPatrnInitCodeCh1=20,h_TestPatrnInitCodeCh2=20,h_Reserved3=0,h_TestPatrnIncrCodeCh0=20,h_TestPatrnIncrCodeCh1=20,h_TestPatrnIncrCodeCh2=20,h_Reserved4=0,w_Reserved5=0})
    assert(xw_status == 0, "ERROR: API failed")
else
    xw_status=mws.fecRdifctrl(0,{c_RdifEnable=10,c_RdifCfg=40,h_RdifSampleCount=num_samples_per_chirp,c_TestPatternEn=0,c_LaneRateCfg=0,h_Reserved2=0,h_TestPatrnInitCodeCh0=20,h_TestPatrnInitCodeCh1=20,h_TestPatrnInitCodeCh2=20,h_Reserved3=0,h_TestPatrnIncrCodeCh0=20,h_TestPatrnIncrCodeCh1=20,h_TestPatrnIncrCodeCh2=20,h_Reserved4=0,w_Reserved5=0})
    assert(xw_status == 0, "ERROR: API failed")
end

-- ******************* CAPTURE SEQUENCE STARTS HERE ***********************
local adc_bin_file_directory = RSTD.GetWorkingDirectory() .. [[\..\PostProc\]]
local adc_bin_file = adc_bin_file_directory .. [[adc_data.bin]]
if (CAPTURE_DATA ~= 0) then
    -- Remove old ADC bin file if it exists (DCA1000 adds "_Raw_0" to filename)
    os.remove(adc_bin_file)
    os.remove(adc_bin_file_directory .. [[adc_data_Raw_0.bin]])

    -- Arm DCA1000 for capture (using "Infinite" stop mode which will stop capture automatically once data from device stops)
    xw_status=mws.captureCardStartRecord(adc_bin_file, 1, 1, 4096)
    assert(xw_status == 0, "ERROR: API failed")
    p(1000)
end

-- Trigger start of framing
xw_status=mws.sensorStart(0,{c_FrameTrigMode=0,c_ChirpStartSigLbEn=0,c_FrameLivMonEn=0,c_Reserved=0,w_FrameTrigTimerVal=0,w_Reserved1=0})
assert(xw_status == 0, "ERROR: API failed")

if (CAPTURE_DATA ~= 0) then
    -- <<<  CAPTURE HAPPENS HERE >>>

    -- Wait for data capture to complete or timeout
    print("Waiting for data capture to complete or timeout")
    print("Calling mws.GetDCAStartRecordStatus()")
    local xw_status = mws.GetDCAStartRecordStatus()
    assert(xw_status == 0, "ERROR: mws.GetDCAStartRecordStatus() API failed")
    p(1000)

    xw_status, exp_num_bytes = mws.GetNumOfCaptureBytes()
    assert(xw_status == 0, "mws.GetNumOfCaptureBytes failed")
    print("Expected number of bytes in ADC bin file: "..exp_num_bytes)

    -- ******************* CAPTURE SEQUENCE ENDS HERE ***********************

    -- Start Matlab Postproc
    print("Calling mws.StartMatlabPostProc("..adc_bin_file..", 0)")
    xw_status = mws.StartMatlabPostProc(adc_bin_file, 0)
    assert(xw_status == 0, "ERROR: mws.StartMatlabPostProc() API failed")
end
