-- Common device setup

-- Change the directory for the firmware file
local firmware_directory_base = [[C:\ti\MMWAVE_L_SDK_05_05_02_00\firmware\mmwave_dfp\]]
local rfeval_firmware = firmware_directory_base .. [[rfevalfirmware\appss\mmwave_plt_rfeval_rprc.bin]]
local rfs_firmware = firmware_directory_base .. [[rfsfirmware\xWRL1432\mmwave_rfs_rprc.bin]]
local atecal_firmware = firmware_directory_base .. [[rfevalfirmware\rfsatecal\xWRL1432\mmwave_rfs_atecal_rprc.bin]]
local rfs_patch_firmware = firmware_directory_base .. [[rfsfirmware\xWRL1432\mmwave_rfs_patch_rprc.bin]]

-- Check that Matlab Postproc is at least version 5.02
local postproc_version = tonumber(mws.GetPostProcVersion())
if (postproc_version < 5.02) then
    error("ERROR: Matlab Postproc must be at least version 5.02")
end

-- Reset studio internal variables
status = mws.ftdiResetAllDevices()

-- Board config
local c_boardType = 0  -- 0=EVM, 1=CZ
xw_status = mws.selectBDType(c_boardType, 0, 1, 0)
assert(xw_status == 0, "ERROR: API failed")
xw_status = mws.selectComIfType(1, 1000, 0, 2, 0)
assert(xw_status == 0, "ERROR: API failed")

-- DCA1000 config
xw_status = mws.SelectCaptureDevice("DCA1000")
assert(xw_status == 0, "ERROR: API failed")
xw_status = mws.captureCardCfgResetFPGA()
assert(xw_status == 0, "ERROR: API failed")
xw_status = mws.captureCardEthernetCfg("192.168.33.30", "192.168.33.180", "12.34.56.78.90.12", 4096, 4098, 1)
assert(xw_status == 0, "ERROR: API failed")
xw_status = mws.captureCardModeCfg(1, 1, 1, 2, 1, 25)
assert(xw_status == 0, "ERROR: API failed")
xw_status, s_version, s_dllVersion = mws.getCaptureCardFPGAVersion()
assert(xw_status == 0, "ERROR: API failed")

-- SOP config
xw_status = mws.sopControl(0, 3)
assert(xw_status == 0, "ERROR: API failed")

-- Device reset
xw_status = mws.nReset(0)
assert(xw_status == 0, "ERROR: API failed")

-- RS232 connect
local rs232_com_port = 15
xw_status = mws.rs232Config(0, rs232_com_port, 0, 0)
assert(xw_status == 0, "ERROR: API failed")

-- Load firmware
xw_status = mws.sopDebugFwPreBootCfg(0, 0, 0, 0)
assert(xw_status == 0, "ERROR: API failed")
xw_status = mws.fwRprcDownload(0, 2, rfeval_firmware)
assert(xw_status == 0, "ERROR: API failed")
xw_status = mws.sopDebugFwPostBootCfg(0, 0, 0, 0, 0, 0)
assert(xw_status == 0, "ERROR: API failed")
xw_status = mws.fwRprcDownload(0, 1, rfs_patch_firmware)
assert(xw_status == 0, "ERROR: API failed")

-- Powerup device
xw_status, t_deviceStatus = mws.devicePowerup(0, 0, 0, 0)
assert(xw_status == 0, "ERROR: API failed")
xw_status=mws.fecDevicePowerOn(0,{h_XtalClkFreq=10240,c_ClkSourceSel=10,c_PowerMode=0,c_ChirpTimerResol=0,c_FecBootCfg=0,h_Reserved2=0,w_Reserved3=0})
assert(xw_status == 0, "ERROR: API failed")

-- Special calibration configuration
xw_status=mws.fecRfsDbgCtrl(0,{w_RfsDbgLogAddress=575029248,w_Reserved0=0,w_Reserved1=0})
assert(xw_status == 0, "ERROR: API failed")

-- Setup clocks
xw_status=mws.fecDevClkctrl(0,{c_DevClkCtrl=10,c_FtClkCtrl=0,c_ApllClkCtrl=170,c_Reserved1=0,w_Reserved2=0})
assert(xw_status == 0, "ERROR: API failed")

