function DeviceIDSelectionCallback(hObj, hDlg, tag, ~) 
%DEVICEIDSELECTIONCALLBACK

% Copyright 2013-2020 MathWorks, Inc.

cs = hObj.getConfigSet();
targetData = codertarget.targethardware.getTargetHardware(cs);
SubFamily = targetData.SubFamily;
newVal = hDlg.getWidgetValue(tag);
ud = hDlg.getUserData(tag);
chipName = ud.Entries{newVal+1};
codertarget.data.setParameterValue(hObj, 'Runtime.DeviceID', chipName);
chipName = getProcName(chipName);
[max_allowed_clock_freq, internal_osc] = codertarget.registry.getMaximumAllowedFreq(chipName, SubFamily);
editBoxTag = 'Tag_ConfigSet_CoderTarget_CPU_Clock_in_MHz';
if ~isequal(hDlg.getWidgetValue(editBoxTag), num2str(max_allowed_clock_freq))
    if (internal_osc > 0)
        hDlg.setWidgetValue('Tag_ConfigSet_Clocking_Oscillator_clock_OSCCLK_frequency_in_MHz', num2str(internal_osc));
        codertarget.data.setParameterValue(hObj, 'Clocking.OSCCLK', num2str(internal_osc));
    end
    hDlg.setWidgetValue(editBoxTag, num2str(max_allowed_clock_freq));
    codertarget.registry.Clocking_SysCLKControl_UICallback(hObj, hDlg, editBoxTag);
end
editBoxTag = 'Tag_ConfigSet_CoderTarget_Runtime_CCS_HW_Configuration_File';
if (2 == exist(fullfile('+codertarget', '+tic2000', '+procinfo', ['F' chipName '.m']), 'file'))
    procData = codertarget.tic2000.procinfo.(['F' chipName]);
    if isfield (procData, 'ccxmlFile')
        hDlg.setWidgetValue(editBoxTag, procData.ccxmlFile);
        codertarget.data.setParameterValue(hObj, 'Runtime.LoadCommandArg', procData.ccxmlFile);
    end
end

function procName = getProcName(dsptgtprocName)
procName_ = regexp(dsptgtprocName, '(F|C|R)(\w+)', 'tokens');
if isempty(procName_)
    procName = '';
else
    procName = strtok(procName_{1}{2}, '_');
end