function Clocking_SysCLKControl_UICallback(hObj, hDlg, tag, ~)
%CLOCKING_SYSCLKCONTROL_UICALLBACK
% This function is called for any update on the clock configuration parameter

% Copyright 2013-2024 The MathWorks, Inc.
peripherals_data = codertarget.data.getData(hObj);
data_Clocking = codertarget.data.getParameterValue(hObj, 'Clocking');
cpu_clk_speed = str2double(data_Clocking.cpuClockRateMHz); %desired CPU clock
cs = hObj.getConfigSet();
targetData = codertarget.targethardware.getTargetHardware(cs);
subFamily = targetData.SubFamily;
chipName = getProcName(codertarget.data.getParameterValue(hObj, 'Runtime.DeviceID'));
chipName = getProcName(chipName);
[max_allowed_clock_freq, ~] = codertarget.registry.getMaximumAllowedFreq(chipName, subFamily);
ud = hDlg.getUserData(tag);

newVal = hDlg.getWidgetValue(tag);

if strcmpi (tag, 'Tag_ConfigSet_CoderTarget_CPU_Clock_in_MHz')
    cpu_clk_speed = str2double(newVal);
    peripherals_data.Clocking.cpuClockRateMHz = newVal;
end

switch tag
    case 'Tag_ConfigSet_CoderTarget_Clocking_Use_internal_oscillator'
        peripherals_data = actionWithUseInternalOsc(newVal, hDlg, peripherals_data, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq);
        if (isequal(subFamily, '2806x'))
            peripherals_data.Clocking.PLL2CLK =  str2double(peripherals_data.Clocking.PLL2MULT) * str2double(peripherals_data.Clocking.OSCCLK);
            hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL2_Clock_in_MHz', peripherals_data.Clocking.PLL2CLK);
        elseif(isequal(subFamily, '2803x'))
            peripherals_data.Clocking.PLLCLK =  str2double(peripherals_data.Clocking.PLLCR) * str2double(peripherals_data.Clocking.OSCCLK);
            hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL_Clock_in_MHz', peripherals_data.Clocking.PLLCLK);
        end
    case 'Tag_ConfigSet_CoderTarget_Clocking_Oscillator_clock_OSCCLK_frequency_in_MHz'
        peripherals_data = actionWithOSCCLK(newVal, hDlg, peripherals_data, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq);
        if (isequal(subFamily, '2806x'))
            peripherals_data.Clocking.PLL2CLK =  str2double(peripherals_data.Clocking.PLL2MULT)* str2double(peripherals_data.Clocking.OSCCLK);
            hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL2_Clock_in_MHz', peripherals_data.Clocking.PLL2CLK);
        elseif(isequal(subFamily, '2803x'))
            peripherals_data.Clocking.PLLCLK =  str2double(peripherals_data.Clocking.PLLCR)* str2double(peripherals_data.Clocking.OSCCLK);
            hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL_Clock_in_MHz', peripherals_data.Clocking.PLLCLK);
        end
    case 'Tag_ConfigSet_CoderTarget_Clocking_PLL2_Clock_in_MHz'
        if (isequal(subFamily, '2806x'))
            peripherals_data.Clocking.PLL2CLK =  str2double(peripherals_data.Clocking.PLL2MULT)* str2double(peripherals_data.Clocking.OSCCLK);
            hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL2_Clock_in_MHz', peripherals_data.Clocking.PLL2CLK);
        end
    case {'Tag_ConfigSet_CoderTarget_Clocking_Auto_set_PLL_based_on_OSCCLK_and_CPU_clock', 'Tag_ConfigSet_CoderTarget_CPU_Clock_in_MHz'}
        if (isequal(subFamily, '28M36x') || isequal(subFamily, '28M35x'))
            if(cpu_clk_speed>max_allowed_clock_freq)
                msg = DAStudio.message('TIC2000:blocks:tgtPrefsPeripheralCpuClockIncorrect','C28 CPU Clock In MHz',num2str(max_allowed_clock_freq));
                dp = DAStudio.DialogProvider;
                dp.errordlg(msg, 'Warning: Coder Target', true);
            elseif(cpu_clk_speed<=0)
                msg = DAStudio.message('TIC2000:blocks:tgtPrefsPeripheralCpuClockIncorrect','C28 CPU Clock In MHz',num2str(max_allowed_clock_freq));
                dp = DAStudio.DialogProvider;
                dp.errordlg(msg, 'Warning: Coder Target', true);
                cpu_clk_speed = str2double(data_Clocking.cpuClockRateMHz);
                hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_CPU_Clock_in_MHz', data_Clocking.cpuClockRateMHz);
                peripherals_data.Clocking.cpuClockRateMHz = data_Clocking.cpuClockRateMHz;
            end
            peripherals_data = setClosestCpuClock(cpu_clk_speed, peripherals_data, subFamily);
            lspclk_div = getDividerValueFromString(peripherals_data.Clocking.LspclkDiv);
            acibclk_div = getDividerValueFromString(peripherals_data.Clocking.AcibclkDiv);
            peripherals_data = setLspclk(cpu_clk_speed/lspclk_div, peripherals_data, subFamily);
            peripherals_data = setAcibclk(cpu_clk_speed/acibclk_div, peripherals_data, subFamily);
        elseif ((isequal(subFamily, '2837xD') || isequal(subFamily, '2838x') || isequal(subFamily, '28p65x')) && isfield(peripherals_data, 'ESB') && isfield(peripherals_data.ESB, 'ProcessingUnit') && contains(peripherals_data.ESB.ProcessingUnit, 'CPU2'))
            peripherals_data.Clocking.ClosestCpuClock = peripherals_data.Clocking.cpuClockRateMHz;
        else
            if (isequal(tag, 'Tag_ConfigSet_CoderTarget_Clocking_Auto_set_PLL_based_on_OSCCLK_and_CPU_clock'))
                peripherals_data = actionWithAutoSetPllSettings(newVal, hDlg, peripherals_data, cpu_clk_speed, subFamily,ud, cs, max_allowed_clock_freq);
            elseif (isequal(tag, 'Tag_ConfigSet_CoderTarget_CPU_Clock_in_MHz'))
                peripherals_data = actionWithCPUClock(hDlg, peripherals_data, cpu_clk_speed, subFamily,ud, cs, max_allowed_clock_freq);
            end
        end
    case 'Tag_ConfigSet_CoderTarget_Clocking_intclk_ref_divider'
        % REFDIV - DropDown
        peripherals_data = actionWithRefDIV(newVal, hDlg, peripherals_data, subFamily, ud, cs, max_allowed_clock_freq);
    case 'Tag_ConfigSet_CoderTarget_Clocking_PLL_control_register_PLLCR'
        % PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
        peripherals_data = actionWithPLLCR(newVal, hDlg, peripherals_data, subFamily, ud, cs, max_allowed_clock_freq);
        if isequal(subFamily, '2803x')
            peripherals_data.Clocking.PLLCLK = ((str2double(peripherals_data.Clocking.PLLCR))*(str2double(peripherals_data.Clocking.OSCCLK)));
            hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL_Clock_in_MHz', peripherals_data.Clocking.PLLCLK);
        end
    case 'Tag_ConfigSet_CoderTarget_Clocking_pll_output_divider'
        % ODIV - DropDown
        peripherals_data = actionWithPLLDIV(newVal, hDlg, peripherals_data, subFamily, ud, cs, max_allowed_clock_freq);
    case 'Tag_ConfigSet_CoderTarget_Clocking_Clock_divider'
        % DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
        peripherals_data = actionWithDIVSEL(newVal, hDlg, peripherals_data, subFamily, ud, cs, max_allowed_clock_freq);
    case 'Tag_ConfigSet_CoderTarget_Clocking_Low_Speed_Peripheral_Clock_Prescalar'
        peripherals_data = actionWithLspclkDiv(newVal, hDlg, peripherals_data, subFamily, ud, cs);
    case 'Tag_ConfigSet_CoderTarget_Clocking_High_Speed_Peripheral_Clock_Prescalar'
        peripherals_data = actionWithHspclkDiv(newVal, hDlg, peripherals_data, subFamily, ud, cs);
    case 'Tag_ConfigSet_CoderTarget_Clocking_ACIB_Divider'
        peripherals_data = actionWithAcibclkDiv(newVal, peripherals_data, subFamily, ud, cs);
    case 'Tag_ConfigSet_CoderTarget_Clocking_Closest_achievable_SYSCLKOUT_in_MHz1'
        peripherals_data.Clocking.ClosestCpuClock1 = newVal;
        peripherals_data.Clocking.ClosestCpuClock = peripherals_data.Clocking.ClosestCpuClock1;
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_Closest_achievable_SYSCLKOUT_in_MHz', peripherals_data.Clocking.ClosestCpuClock1);
        peripherals_data = updatePeripheralsRltdToClosestCpuClock(peripherals_data, subFamily);
        lspclk_div = getDividerValueFromString(peripherals_data.Clocking.LspclkDiv);
        peripherals_data = setLspclk(str2double(peripherals_data.Clocking.ClosestCpuClock)/lspclk_div, peripherals_data, subFamily);
    case 'Tag_ConfigSet_CoderTarget_Clocking_Low_Speed_Peripheral_Clock_Prescalar1'
        peripherals_data.Clocking.LspclkDiv1 = char(ud.Entries(newVal + 1));
        peripherals_data.Clocking.LspclkDiv = peripherals_data.Clocking.LspclkDiv1;
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_Low_Speed_Peripheral_Clock_Prescalar', newVal);
        peripherals_data = actionWithLspclkDiv(newVal, hDlg, peripherals_data, subFamily, ud, cs);
    case 'Tag_ConfigSet_CoderTarget_EPWM_Clock_Division1'
        peripherals_data.ePWM.ClockDiv1 = char(ud.Entries(newVal + 1));
        peripherals_data.ePWM.ClockDiv = peripherals_data.ePWM.ClockDiv1;
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_EPWM_Clock_Division', char(ud.Entries(newVal + 1)));
    case 'Tag_ConfigSet_CoderTarget_Runtime_CPU'
        if hDlg.getWidgetValue(tag) == 0
            if peripherals_data.Clocking.AutoSetPllSettings == 1
                peripherals_data = actionWithCPUClock(hDlg, peripherals_data, cpu_clk_speed, subFamily,ud, cs, max_allowed_clock_freq);
            else
                udPllcr = hDlg.getUserData('Tag_ConfigSet_CoderTarget_Clocking_PLL_control_register_PLLCR');
                newValPllcr = hDlg.getWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL_control_register_PLLCR');
                peripherals_data = actionWithPLLCR(newValPllcr, hDlg, peripherals_data, subFamily, udPllcr, cs, max_allowed_clock_freq);
            end
        end
        peripherals_data.Runtime.CPU = char(ud.Entries(newVal + 1));
    case 'Tag_ConfigSet_CoderTarget_Clocking_CM_Clock_Source'
        peripherals_data.Clocking.CMDIVSRCSEL = char(ud.Entries(newVal+1));
    case 'Tag_ConfigSet_CoderTarget_Clocking_CM_Clock_Divider'
        peripherals_data.Clocking.CMCLKDIV = char(ud.Entries(newVal+1));
    case 'Tag_ConfigSet_CoderTarget_Clocking_CM_Clock_Value_MHz'
        peripherals_data.Clocking.CMCLKVAL = char(newVal);
    case 'Tag_ConfigSet_CoderTarget_Clocking_PLL2_MULTFACTOR'
        peripherals_data.Clocking.PLL2MULT = char(ud.Entries(newVal + 1));
        peripherals_data.Clocking.PLL2CLK =  str2double(peripherals_data.Clocking.PLL2MULT) * str2double(peripherals_data.Clocking.OSCCLK);
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL2_Clock_in_MHz', peripherals_data.Clocking.PLL2CLK);
end

codertarget.data.setData(hObj, peripherals_data);
%end %Clocking_SysCLKControl_UICallback


function peripherals_updated = actionWithUseInternalOsc(newVal, hDlg, peripherals, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq)
peripherals_updated = peripherals;
if (newVal)
    peripherals_updated.Clocking.UseInternalOsc = 1;

    % Set to Internal Oscillator frequency
    osc_freq = 10;
    hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_Oscillator_clock_OSCCLK_frequency_in_MHz', num2str(osc_freq));
    lspclk_div = getDividerValueFromString(peripherals.Clocking.LspclkDiv);
    if isfield(peripherals.Clocking, 'HspclkDiv')
        hspclk_div = getDividerValueFromString(peripherals.Clocking.HspclkDiv);
    else
        hspclk_div = 1;
    end

    if (peripherals.Clocking.AutoSetPllSettings)
        [exact_match, pllcr_closest, pllclk_div_closest, clkin_div_closest, refDiv_closest] = codertarget.tic2000.internal.autoSetPLLSettings(cpu_clk_speed, osc_freq, subFamily, max_allowed_clock_freq);

        [closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr_closest, clkin_div_closest, pllclk_div_closest, lspclk_div, hspclk_div, refDiv_closest);

        if isfield(peripherals.Clocking, 'REFDIV')
            % REFDIV - DropDown
            peripherals_updated = setREFDIV(refDiv_closest, peripherals_updated, hDlg);
        end
        % PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
        peripherals_updated = setPLLCR(pllcr_closest, hDlg, peripherals_updated, subFamily,  ud, cs);
        % ODIV - DropDown
        peripherals_updated = setPLLDIV(pllclk_div_closest, peripherals_updated, hDlg);
        % DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
        peripherals_updated = setDIVSEL(clkin_div_closest, peripherals_updated, subFamily, hDlg);

        displayWarningMessage(~exact_match, closest_cpu_clock, max_allowed_clock_freq);
    else
        % PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
        pllcr = getPLLCR(peripherals_updated, subFamily);
        % ODIV - DropDown
        plldiv = getPLLDIV (hDlg, peripherals);
        % DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
        divsel = getDIVSEL (hDlg, peripherals, subFamily);
        if isfield(peripherals.Clocking, 'REFDIV')
            refDiv = getREFDIV(hDlg, peripherals);
        else
            refDiv = 1;
        end
        %Update calcCpuClockSpeed function to now include refDiv
        [closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr, divsel, plldiv, lspclk_div, hspclk_div, refDiv);

        displayWarningMessage(closest_cpu_clock ~= cpu_clk_speed, closest_cpu_clock, max_allowed_clock_freq);
    end

    peripherals_updated = setOSCCLK(osc_freq, peripherals_updated);
    peripherals_updated = setClosestCpuClock(closest_cpu_clock, peripherals_updated, subFamily);
    peripherals_updated = setLspclk(lspclk, peripherals_updated, subFamily);
    if isfield(peripherals.Clocking, 'HSPCLK')
        peripherals_updated = setHspclk(hspclk, peripherals_updated, subFamily);
    end
else
    peripherals_updated.Clocking.UseInternalOsc = 0;
end
% end %actionWithUseInternalOsc

function peripherals_updated = actionWithOSCCLK(newVal, hDlg, peripherals, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq)
peripherals_updated = peripherals;
osc_freq = str2double(newVal); % MHz

lspclk_div = getDividerValueFromString(peripherals.Clocking.LspclkDiv);
if isfield(peripherals.Clocking, 'HspclkDiv')
    hspclk_div = getDividerValueFromString(peripherals.Clocking.HspclkDiv);
else
    hspclk_div = 1;
end

if (peripherals.Clocking.AutoSetPllSettings)
    [exact_match, pllcr_closest, pllclk_div_closest, clkin_div_closest, refDiv_closest] = codertarget.tic2000.internal.autoSetPLLSettings(cpu_clk_speed, osc_freq, subFamily, max_allowed_clock_freq);

    [closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr_closest, clkin_div_closest, pllclk_div_closest, lspclk_div, hspclk_div, refDiv_closest);

    if isfield(peripherals.Clocking, 'REFDIV')
        % REFDIV - DropDown
        peripherals_updated = setREFDIV(refDiv_closest, peripherals_updated, hDlg);
    end
    % PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
    peripherals_updated = setPLLCR(pllcr_closest, hDlg, peripherals_updated, subFamily, ud, cs);
    % ODIV - DropDown
    peripherals_updated = setPLLDIV(pllclk_div_closest, peripherals_updated, hDlg);
    % DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
    peripherals_updated = setDIVSEL(clkin_div_closest, peripherals_updated, subFamily, hDlg);

    displayWarningMessage(~exact_match, closest_cpu_clock, max_allowed_clock_freq);
else
    % PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
    pllcr = getPLLCR(peripherals_updated, subFamily);
    % ODIV - DropDown
    plldiv = getPLLDIV (hDlg, peripherals);
    % DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
    divsel = getDIVSEL(hDlg, peripherals, subFamily);
    if isfield(peripherals.Clocking, 'REFDIV')
        refDiv = getREFDIV(hDlg, peripherals);
    else
        refDiv = 1;
    end

    [closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr, divsel, plldiv, lspclk_div, hspclk_div, refDiv);
    displayWarningMessage(closest_cpu_clock ~= cpu_clk_speed, closest_cpu_clock, max_allowed_clock_freq);
end

peripherals_updated = setOSCCLK(osc_freq, peripherals_updated);
peripherals_updated = setClosestCpuClock(closest_cpu_clock, peripherals_updated, subFamily);
peripherals_updated = setLspclk(lspclk, peripherals_updated, subFamily);
if isfield(peripherals.Clocking, 'HSPCLK')
    peripherals_updated = setHspclk(hspclk, peripherals_updated, subFamily);
end
% end %actionWithOSCCLK

function peripherals_updated = actionWithAutoSetPllSettings(newVal, hDlg, peripherals, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq)
peripherals_updated = peripherals;
if  (newVal)
    peripherals_updated = updateClockRelatedPheripherals(hDlg, peripherals_updated, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq);
    peripherals_updated.Clocking.AutoSetPllSettings = 1;
else
    peripherals_updated.Clocking.AutoSetPllSettings = 0;
end
% end %actionWithAutoSetPllSettings

function peripherals_updated = actionWithCPUClock(hDlg, peripherals, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq)
peripherals_updated = peripherals;
if (peripherals.Clocking.AutoSetPllSettings)
    peripherals_updated = updateClockRelatedPheripherals(hDlg, peripherals_updated, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq);
else
    %nothing to be done
end
% end %actionWithCPUClock

function clock_peripherals_updated = updateClockRelatedPheripherals(hDlg, peripherals, cpu_clk_speed, subFamily, ud, cs, max_allowed_clock_freq)
clock_peripherals_updated = peripherals;
osc_freq = str2num(clock_peripherals_updated.Clocking.OSCCLK); %#ok<ST2NM> % MHz
lspclk_div = getDividerValueFromString(clock_peripherals_updated.Clocking.LspclkDiv);
if isfield(clock_peripherals_updated.Clocking, 'HspclkDiv')
    hspclk_div = getDividerValueFromString(clock_peripherals_updated.Clocking.HspclkDiv);
else
    hspclk_div = 1;
end

[exact_match, pllcr_closest, pllclk_div_closest, clkin_div_closest, refDiv_closest] = codertarget.tic2000.internal.autoSetPLLSettings(cpu_clk_speed, osc_freq, subFamily, max_allowed_clock_freq);
[closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr_closest, clkin_div_closest, pllclk_div_closest, lspclk_div, hspclk_div, refDiv_closest);

clock_peripherals_updated.Clocking.AutoSetPllSettings = 1;

% REFDIV - DropDown
if isfield(clock_peripherals_updated.Clocking, 'REFDIV')
    clock_peripherals_updated = setREFDIV(refDiv_closest, clock_peripherals_updated, hDlg);
end
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
clock_peripherals_updated = setPLLCR(pllcr_closest, hDlg, clock_peripherals_updated, subFamily, ud, cs);
% ODIV - DropDown
clock_peripherals_updated = setPLLDIV(pllclk_div_closest, clock_peripherals_updated, hDlg);
% DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
clock_peripherals_updated = setDIVSEL(clkin_div_closest, clock_peripherals_updated, subFamily, hDlg);
clock_peripherals_updated = setClosestCpuClock(closest_cpu_clock, clock_peripherals_updated, subFamily);
clock_peripherals_updated = setLspclk(lspclk, clock_peripherals_updated, subFamily);
if isfield(clock_peripherals_updated.Clocking, 'HSPCLK')
    clock_peripherals_updated = setHspclk(hspclk, clock_peripherals_updated, subFamily);
end

displayWarningMessage(~exact_match, closest_cpu_clock, max_allowed_clock_freq);
% end %updateClockRelatedPheripherals

function peripherals_updated = actionWithRefDIV(newVal, hDlg, peripherals, subFamily, ud, cs, max_allowed_clock_freq) %#ok<INUSL>
% REFDIV - DropDown
peripherals_updated = peripherals;

osc_freq = str2num(peripherals_updated.Clocking.OSCCLK); %#ok<ST2NM> % MHz

refDiv = str2double(ud.Entries{newVal+1});

%ODIV Dropdown
plldiv = getPLLDIV(hDlg, peripherals_updated);
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
pllcr = getPLLCR(peripherals_updated, subFamily);
% DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
divsel = getDIVSEL(hDlg, peripherals_updated, subFamily);
lspclk_div = getDividerValueFromString(peripherals_updated.Clocking.LspclkDiv);
if isfield(peripherals_updated.Clocking, 'HspclkDiv')
    hspclk_div = getDividerValueFromString(peripherals_updated.Clocking.HspclkDiv);
else
    hspclk_div = 1;
end

[closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr, divsel, plldiv, lspclk_div, hspclk_div, refDiv);
displayWarningMessage(closest_cpu_clock ~= str2double(peripherals_updated.Clocking.cpuClockRateMHz), closest_cpu_clock, max_allowed_clock_freq);

hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_intclk_ref_divider', num2str(refDiv));
peripherals_updated.Clocking.REFDIV = num2str(refDiv);

peripherals_updated = setClosestCpuClock(closest_cpu_clock, peripherals_updated, subFamily);
peripherals_updated = setLspclk(lspclk, peripherals_updated, subFamily);
if isfield(peripherals_updated.Clocking, 'HSPCLK')
    peripherals_updated = setHspclk(hspclk, peripherals_updated, subFamily);
end
% end %actionWithRefDIV


function peripherals_updated = actionWithPLLCR(newVal, hDlg, peripherals, subFamily, ud, cs, max_allowed_clock_freq)
peripherals_updated = peripherals;

osc_freq = str2num(peripherals_updated.Clocking.OSCCLK); %#ok<ST2NM> % MHz
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
pllcr = processPLLCR(newVal, peripherals_updated, subFamily, hDlg, ud);
switch subFamily
    case {'2837xS','2837xD', '2807x', '28004x', '2838x', '28002x', '28003x', '280013x', '280015x', '28p65x'}
        % 0.25 is the resolution of PLL multiplier for Concerto
        if isequal(subFamily, '2837xS') || isequal(subFamily, '2837xD') || isequal(subFamily, '2807x') || isequal(subFamily, '28004x')
            maxpllcr = 127.75;
            pllcrincrement = 0.25;
        else
            maxpllcr = 127;
            pllcrincrement = 1;
        end
        triggerWarning = 0;
        if (pllcr < 1)
            pllcr = 1;
            triggerWarning = 1;
        elseif(pllcr > maxpllcr)
            pllcr = maxpllcr;
            triggerWarning = 1;
        else
            pllcr = (round(pllcr/pllcrincrement))*pllcrincrement;
            if ~isequal(pllcr, str2double(newVal))
                triggerWarning = 1;
            end
        end
        if triggerWarning
            hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL_control_register_PLLCR', num2str(pllcr));
            msg = DAStudio.message('TIC2000:blocks:tgtPrefsPeripheralPLLCRDontMatch', num2str(maxpllcr), num2str(pllcrincrement), newVal, num2str(pllcr));
            warndlg(msg, 'Warning: Coder Target');
        end
    otherwise
end

%REFDIV - DropDown
if isfield(peripherals_updated.Clocking, 'REFDIV')
    refDiv = str2double(peripherals_updated.Clocking.REFDIV);
else
    refDiv = 1;
end

% ODIV - DropDown
plldiv = getPLLDIV(hDlg, peripherals);
% DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
divsel = getDIVSEL(hDlg, peripherals, subFamily);
lspclk_div = getDividerValueFromString(peripherals_updated.Clocking.LspclkDiv);
if isfield(peripherals_updated.Clocking, 'HspclkDiv')
    hspclk_div = getDividerValueFromString(peripherals_updated.Clocking.HspclkDiv);
else
    hspclk_div = 1;
end

[closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr, divsel, plldiv, lspclk_div, hspclk_div, refDiv);
displayWarningMessage(closest_cpu_clock ~= str2double(peripherals_updated.Clocking.cpuClockRateMHz), closest_cpu_clock, max_allowed_clock_freq);

peripherals_updated = setPLLCR(pllcr, hDlg, peripherals_updated, subFamily, ud,cs);
peripherals_updated = setClosestCpuClock(closest_cpu_clock, peripherals_updated, subFamily);
peripherals_updated = setLspclk(lspclk, peripherals_updated, subFamily);
if isfield(peripherals_updated.Clocking, 'HSPCLK')
    peripherals_updated = setHspclk(hspclk, peripherals_updated, subFamily);
end
% end %actionWithPLLCR

function peripherals_updated = actionWithDIVSEL(newVal, hDlg, peripherals, subFamily, ud, cs, max_allowed_clock_freq) %#ok<INUSL>
% DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
peripherals_updated = peripherals;

osc_freq = str2num(peripherals_updated.Clocking.OSCCLK); %#ok<ST2NM> % MHz
if isa(hDlg, 'DAStudio.Dialog')
    imd = DAStudio.imDialog.getIMWidgets(hDlg);
    widget = imd.find('tag','Tag_ConfigSet_CoderTarget_Clocking_Clock_divider');
    isComboBox = isa(widget, 'DAStudio.imComboBox');
else
    type = hDlg.getWidgetType('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider');
    isComboBox = strcmp(type, 'combobox');
end
if isComboBox
    if isequal(subFamily, '2838x') || isequal(subFamily, '28002x') || isequal(subFamily, '28003x') || isequal(subFamily, '28p65x')
        clkin_div = str2double(ud.Entries{newVal+1});
    else
        clkin_div = getDividerValueFromString(ud.Entries{newVal+1});
    end
else
    clkin_div = str2double(newVal);

    triggerWarning = 0;
    if (clkin_div < 1)
        clkin_div = 1;
        triggerWarning = 1;
    elseif(clkin_div > 126)
        clkin_div = 126;
        triggerWarning = 1;
    else
        if (clkin_div > 1)
            if (clkin_div < 2)
                clkin_div = round(clkin_div);
            else
                clkin_div = round(clkin_div/2)*2;
            end
            if ~isequal(clkin_div, str2double(newVal))
                triggerWarning = 1;
            end
        end
    end
    if triggerWarning
        msg = DAStudio.message('TIC2000:blocks:tgtPrefsPeripheralDIVSELDontMatch', newVal, num2str(clkin_div));
        warndlg(msg, 'Warning: Coder Target');
    end
end

%REFDIV - DropDown
if isfield(peripherals_updated.Clocking, 'REFDIV')
    refDiv = str2double(peripherals_updated.Clocking.REFDIV);
else
    refDiv = 1;
end
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
pllcr = getPLLCR(peripherals_updated, subFamily);
% ODIV - DropDown
plldiv = getPLLDIV(hDlg, peripherals_updated);
lspclk_div = getDividerValueFromString(peripherals_updated.Clocking.LspclkDiv);
if isfield(peripherals_updated.Clocking, 'HspclkDiv')
    hspclk_div = getDividerValueFromString(peripherals_updated.Clocking.HspclkDiv);
else
    hspclk_div = 1;
end

[closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr, clkin_div, plldiv, lspclk_div, hspclk_div, refDiv);
displayWarningMessage(closest_cpu_clock ~= str2double(peripherals_updated.Clocking.cpuClockRateMHz), closest_cpu_clock, max_allowed_clock_freq);

if isComboBox
    peripherals_updated.Clocking.DIVSEL = calcDIVSELString(clkin_div, peripherals_updated, subFamily);
    hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider', newVal);
else
    hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider', num2str(clkin_div));
    peripherals_updated.Clocking.DIVSEL = num2str(clkin_div);
end

peripherals_updated = setClosestCpuClock(closest_cpu_clock, peripherals_updated, subFamily);
peripherals_updated = setLspclk(lspclk, peripherals_updated, subFamily);
if isfield(peripherals_updated.Clocking, 'HSPCLK')
    peripherals_updated = setHspclk(hspclk, peripherals_updated, subFamily);
end
% end %actionWithDIVSEL

function peripherals_updated = actionWithPLLDIV(newVal, hDlg, peripherals, subFamily, ud, cs, max_allowed_clock_freq) %#ok<INUSL>
% ODIV - DropDown
peripherals_updated = peripherals;

osc_freq = str2num(peripherals_updated.Clocking.OSCCLK); %#ok<ST2NM> % MHz
%REFDIV - DropDown
if isfield(peripherals_updated.Clocking, 'REFDIV')
    refDiv = str2double(peripherals_updated.Clocking.REFDIV);
else
    refDiv = 1;
end
plldiv = str2double(ud.Entries{newVal+1});
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
pllcr = getPLLCR(peripherals_updated, subFamily);
% DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
divsel = getDIVSEL(hDlg, peripherals_updated, subFamily);
lspclk_div = getDividerValueFromString(peripherals_updated.Clocking.LspclkDiv);
if isfield(peripherals_updated.Clocking, 'HspclkDiv')
    hspclk_div = getDividerValueFromString(peripherals_updated.Clocking.HspclkDiv);
else
    hspclk_div = 1;
end

[closest_cpu_clock, lspclk, hspclk] = codertarget.tic2000.internal.calcCpuClockSpeed(osc_freq, pllcr, divsel, plldiv, lspclk_div, hspclk_div, refDiv);
displayWarningMessage(closest_cpu_clock ~= str2double(peripherals_updated.Clocking.cpuClockRateMHz), closest_cpu_clock, max_allowed_clock_freq);

hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_pll_output_divider', num2str(plldiv));
peripherals_updated.Clocking.PLLDIV = num2str(plldiv);

peripherals_updated = setClosestCpuClock(closest_cpu_clock, peripherals_updated, subFamily);
peripherals_updated = setLspclk(lspclk, peripherals_updated, subFamily);
if isfield(peripherals_updated.Clocking, 'HSPCLK')
    peripherals_updated = setHspclk(hspclk, peripherals_updated, subFamily);
end
% end %actionWithPLLDIV

function peripherals_updated = actionWithLspclkDiv(newVal, hDlg, peripherals, subFamily, ud, cs) %#ok<INUSD,INUSL>
peripherals_updated = peripherals;

closest_cpu_clock = peripherals_updated.Clocking.ClosestCpuClock;
lspclkDivStr = ud.Entries{newVal+1};
% Update lspclk divider
if iscell(lspclkDivStr)
    lspclkDivStr = char(lspclkDivStr);
end

% Calculate and set low speed peripheral clock
lspclkDiv = getDividerValueFromString(lspclkDivStr);
lspclk = str2double(closest_cpu_clock) / lspclkDiv;

peripherals_updated.Clocking.LspclkDiv = lspclkDivStr;
peripherals_updated = setLspclk(lspclk, peripherals_updated, subFamily);
% end %actionWithLspclkDiv

function peripherals_updated = actionWithHspclkDiv(newVal, hDlg, peripherals, subFamily, ud, cs) %#ok<INUSD,INUSL>
peripherals_updated = peripherals;

closest_cpu_clock = peripherals_updated.Clocking.ClosestCpuClock;
hspclkDivStr = ud.Entries{newVal+1};
% Update lspclk divider
if iscell(hspclkDivStr)
    hspclkDivStr = char(hspclkDivStr);
end

% Calculate and set high speed peripheral clock
hspclkDiv = getDividerValueFromString(hspclkDivStr);
hspclk = str2double(closest_cpu_clock) / hspclkDiv;

peripherals_updated.Clocking.HspclkDiv = hspclkDivStr;
peripherals_updated = setHspclk(hspclk, peripherals_updated, subFamily);
% end %actionWithHspclkDiv

function peripherals_updated = actionWithAcibclkDiv(newVal, peripherals, subFamily, ud, cs) %#ok<INUSD>
peripherals_updated = peripherals;

closest_cpu_clock = peripherals_updated.Clocking.ClosestCpuClock;
AcibclkDivStr = ud.Entries{newVal+1};
% Update lspclk divider
if iscell(AcibclkDivStr)
    AcibclkDivStr = char(AcibclkDivStr);
end

% Calculate and set high speed peripheral clock
AcibclkDiv = getDividerValueFromString(AcibclkDivStr);
Acibclk = str2double(closest_cpu_clock) / AcibclkDiv;

peripherals_updated.Clocking.AcibclkDiv = AcibclkDivStr;
peripherals_updated = setAcibclk(Acibclk, peripherals_updated, subFamily);
% end %actionWithHspclkDiv

function peripherals_updated = setPLLCR(pllcr, hDlg, peripherals, subFamily, ud, cs) %#ok<INUSD>
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
peripherals_updated = peripherals;

% fieldName = ud.Storage;
% codertarget.data.setParameterValue(cs, fieldName, num2str(pllcr_proc));
switch subFamily
    case '2834x'
        pllcr_proc = pllcr - 1;
    case {'280x', '281x', '2802x', '2803x', '2804x', '2805x', '2806x', '2833x', '28M34x', '28M35x'}
        if isequal(subFamily, '2806x')
            pll_entries = {'16', '15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0'};
        elseif isequal(subFamily, '2805x') || isequal(subFamily, '2803x') || isequal(subFamily, '2802x')
            pll_entries = {'12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0'};
        else
            pll_entries = {'10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0'};
        end
        pllcr_proc = pllcr;
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL_control_register_PLLCR', find(strcmpi(num2str(pllcr_proc),pll_entries), 1)-1);
    otherwise
        pllcr_proc = num2str(pllcr);
end
peripherals_updated.Clocking.PLLCR = num2str(pllcr_proc);
if isequal(subFamily, '2803x')
    peripherals_updated.Clocking.PLLCLK = ((str2double(peripherals_updated.Clocking.PLLCR))*(str2double(peripherals_updated.Clocking.OSCCLK)));
    hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_PLL_Clock_in_MHz', peripherals_updated.Clocking.PLLCLK);
end
function pllcr_proc = getPLLCR(peripherals, subFamily)
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
pllcr = str2double(peripherals.Clocking.PLLCR);
switch subFamily
    case '2834x'
        pllcr_proc = pllcr + 1;
    otherwise
        pllcr_proc = pllcr;
end


function pllcr = processPLLCR(newValPLLCR, peripherals, subFamily, hDlg, ud) %#ok<INUSL>
% PLLCR-DropDown or SYSPLLMULT(IMULT+FMULT)-EditBox parameter setting
switch subFamily
    case '2834x'
        % For this processor, the PLLCR is editable.  Hence, direct
        % conversion to numeric.
        pllcr = str2double(newValPLLCR) + 1;
        if (pllcr > 32) || (pllcr < 1)
            % PLLCR ranges from (0 .. 31) + 1
            pllcr = str2double(peripherals.Clocking.PLLCR) + 1;
        end
    case {'280x', '281x', '2802x', '2803x', '2804x', '2805x', '2806x', '2833x', '28M34x', '28M35x'}
        % For these processors, the PLLCR is a selection from a list.  The
        % 'newValPLLCR' is always an index of the list and the index
        % starts from zero.
        pllcr = str2double(char(ud.Entries(newValPLLCR + 1)));
    otherwise
        pllcr = str2double(newValPLLCR);
end

function DIVSEL_String = calcDIVSELString(divsel, peripherals, subFamily) %#ok
% DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
if isequal(subFamily, '2838x') || isequal(subFamily, '28002x') || isequal(subFamily, '28003x') || isequal(subFamily, '28p65x')
    DIVSEL_String = num2str(divsel);
else
    DIVSEL_String = ['(OSCCLK * PLLCR)/' num2str(divsel)];
end


function peripherals_updated = setLspclk(lspclkInMHz, peripherals, subFamily)
peripherals_updated = peripherals;

peripherals_updated.Clocking.LSPCLK = lspclkInMHz;

peripherals_updated = updatePeripheralsRltdToLspclk(peripherals_updated, subFamily);


function peripherals_updated = updatePeripheralsRltdToLspclk(peripherals, subFamily) %#ok
peripherals_updated = peripherals;
% Update other peripheral parameters which rely on Low speed peripheral
% clock
peripheral_lists = fieldnames(peripherals_updated);

% Change SCI
sci_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'SCI')));
for i = 1:length(sci_periph_indexes)
    sci_name = peripheral_lists{sci_periph_indexes(i)};

    [BaudRatePrescaler, BaudRate] = ...
        calcSCIBaudRatePrescaler(str2double(peripherals_updated.Clocking.ClosestCpuClock), ...
        getDividerValueFromString(peripherals_updated.Clocking.LspclkDiv), ...
        str2double(peripherals_updated.(sci_name).UserBaudRate));
    peripherals_updated.(sci_name).BaudRatePrescaler = num2str(BaudRatePrescaler);
    peripherals_updated.(sci_name).BaudRate = num2str(BaudRate);
end

% Change SPI
spi_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'SPI')));
for i = 1:length(spi_periph_indexes)
    spi_name = peripheral_lists{spi_periph_indexes(i)};

    [peripherals_updated.(spi_name).BaudRateFactor, BaudRate] = ...
        calcSPIBaudRateFactor(peripherals_updated.Clocking.ClosestCpuClock, ...
        getDividerValueFromString(peripherals_updated.Clocking.LspclkDiv), ...
        str2double(peripherals_updated.(spi_name).UserBaudRate));
    peripherals_updated.(spi_name).BaudRate = num2str(BaudRate);
end
% end %updatePeripheralsRltdToLspclk


function peripherals_updated = setClosestCpuClock(ClosestCpuClockInMHz, peripherals, subFamily)
peripherals_updated = peripherals;

peripherals_updated.Clocking.ClosestCpuClock = num2str(ClosestCpuClockInMHz);

peripherals_updated = updatePeripheralsRltdToClosestCpuClock(peripherals_updated, subFamily);
% end %setClosestCpuClock

function peripherals_updated = updatePeripheralsRltdToClosestCpuClock(peripherals, subFamily)
peripherals_updated = peripherals;

% Update other peripheral parameters which rely on Low speed peripheral
% clock
peripheral_lists = fieldnames(peripherals_updated);


% Change ADC
if (strcmp(subFamily, '2802x') || strcmp(subFamily, '2803x') || ...
        strcmp(subFamily, '2805x') || strcmp(subFamily, '2806x') || ...
        strcmp(subFamily, '28M36x') || strcmp(subFamily, '28M35x'))
    adc_indexes = find(cellfun(@any,strfind(peripheral_lists,'ADC')));
    for i = 1:length(adc_indexes)
        adc_name = peripheral_lists{adc_indexes(i)};

        [peripherals_updated.(adc_name).ClockFrequency] = ...
            str2double(peripherals_updated.Clocking.ClosestCpuClock) / ...
            getDividerValueFromString(peripherals_updated.(adc_name).ClockDiv);
    end
end

% Change I2C
i2c_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'I2C')));
for i = 1:length(i2c_periph_indexes)
    i2c_name = peripheral_lists{i2c_periph_indexes(i)};

    [peripherals_updated.(i2c_name).ModuleClockFrequency, ...
        peripherals_updated.(i2c_name).MasterClockFrequency, ~] = ...
        calcI2CFrequencies(str2double(peripherals_updated.Clocking.ClosestCpuClock), ...
        peripherals_updated.(i2c_name).ModuleClockPrescaler, ...
        peripherals_updated.(i2c_name).MasterClkLowTime, ...
        peripherals_updated.(i2c_name).MasterClkHighTime);
end

% Change eCAN
eCAN_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'eCAN')));
for i = 1:length(eCAN_periph_indexes)
    eCAN_name = peripheral_lists{eCAN_periph_indexes(i)};

    [peripherals_updated.(eCAN_name).ModuleClockFrequency, ...
        peripherals_updated.(eCAN_name).BaudRatePrescaler, ...
        peripherals_updated.(eCAN_name).BaudRate] = ...
        calceCANBaudRatePrescaler(str2double(peripherals_updated.Clocking.ClosestCpuClock), ...
        str2double(peripherals_updated.(eCAN_name).TSEG1), ...
        str2double(peripherals_updated.(eCAN_name).TSEG2), ...
        peripherals_updated.(eCAN_name).BaudRatePrescaler, ...
        peripherals_updated.(eCAN_name).SAM, subFamily);
end

% Change LIN
LIN_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'LIN')));
for i = 1:length(LIN_periph_indexes)
    lin_name = peripheral_lists{LIN_periph_indexes(i)};

    [peripherals_updated.(lin_name).ModuleClockFrequency, peripherals_updated.(lin_name).BaudRate] = ...
        calcLINBaudRate(str2double(peripherals_updated.Clocking.ClosestCpuClock), ...
        peripherals_updated.(lin_name).BaudRatePrescaler, ...
        peripherals_updated.(lin_name).BaudRateM);
end

% Change EMIF
emif_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'EMIF')));
for i = 1:length(emif_periph_indexes)
    emif_name = peripheral_lists{emif_periph_indexes(i)};
    peripherals_updated = codertarget.registry.EMIFClockCallback(peripherals_updated,emif_name);
end
% end %updatePeripheralsRltdToClosestCpuClock

function peripherals_updated = setHspclk(hspclkInMHz, peripherals, subFamily)
peripherals_updated = peripherals;

peripherals_updated.Clocking.HSPCLK = hspclkInMHz;

peripherals_updated = updatePeripheralsRltdToHspclk(peripherals_updated, subFamily);
% end %setHspclk

function peripherals_updated = setAcibclk(AcibclkInMHz, peripherals, ~)
peripherals_updated = peripherals;

ADCClkDiv1 = getDividerValueFromString(peripherals.ADC1.ClockDiv);
ADCClkDiv2 = getDividerValueFromString(peripherals.ADC2.ClockDiv);
if(ADCClkDiv1>=ADCClkDiv2)
    ADCClkDiv = ADCClkDiv2;
else
    ADCClkDiv = ADCClkDiv1;
end
peripherals_updated.Clocking.Acibclk = AcibclkInMHz;
peripherals_updated.ADC1.ClockFrequency = AcibclkInMHz/ADCClkDiv1;
peripherals_updated.ADC2.ClockFrequency = AcibclkInMHz/ADCClkDiv2;
if((peripherals_updated.Clocking.Acibclk/ADCClkDiv)>37.5)
    msg = DAStudio.message('TIC2000:blocks:tgtPrefsPeripheralConcertoCpuClockHigh','Analog Subsystem Clock Prescaler (ASYSCLK)','Analog Subsystem Clock',num2str(37.5));
    dp = DAStudio.DialogProvider;
    dp.errordlg(msg, 'Warning: Coder Target', true);
end
% end %setAcibclk

function peripherals_updated = updatePeripheralsRltdToHspclk(peripherals, subFamily)
peripherals_updated = peripherals;

% Update other peripheral parameters which rely on Low speed peripheral
% clock
peripheral_lists = fieldnames(peripherals_updated);

% Change ADC
if ~(strcmp(subFamily, '2802x') || strcmp(subFamily, '2803x') || ...
        strcmp(subFamily, '2805x') || strcmp(subFamily, '2806x') || ...
        strcmp(subFamily, '28M36x') || strcmp(subFamily, '28M35x'))
    adc_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'ADC')));
    for i = 1:length(adc_periph_indexes)
        adc_name = peripheral_lists{adc_periph_indexes(i)};

        [peripherals_updated.(adc_name).ADCCLK, peripherals_updated.(adc_name).AcquisitionWindow] = ...
            calcADCClockAndAcquistionWindow(str2double(peripherals_updated.Clocking.ClosestCpuClock), ...
            getDividerValueFromString(peripherals_updated.Clocking.HspclkDiv), ...
            str2double(peripherals_updated.(adc_name).ADCLKPS), ...
            str2double(peripherals_updated.(adc_name).CPS), ...
            str2double(peripherals_updated.(adc_name).ACQ_PS));
    end
end
% end %updatePeripheralsRltdToHspclk

function peripherals_updated = setOSCCLK(OSCCLKInMHz, peripherals)
peripherals_updated = peripherals;

peripherals_updated.Clocking.OSCCLK = num2str(OSCCLKInMHz);

% Update other peripheral parameters which rely on Low speed peripheral
% clock
peripheral_lists = fieldnames(peripherals_updated);

% Change Watchdog
watchdog_periph_indexes = find(cellfun(@any,strfind(peripheral_lists,'Watchdog')));
for i = 1:length(watchdog_periph_indexes)
    watchdog_name = peripheral_lists{watchdog_periph_indexes(i)};

    peripherals_updated.(watchdog_name).Time_period = calcWatchdogParemeters(OSCCLKInMHz, ...
        peripherals_updated.(watchdog_name).Watchdogclock, 8);
end
% end %setOSCCLK

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

function displayWarningMessage(no_exact_match, closest_cpu_clock, max_allowed_freq)
if no_exact_match || (closest_cpu_clock > max_allowed_freq)
    msg = DAStudio.message('TIC2000:blocks:tgtPrefsPeripheralCpuClockDontMatch', num2str(max_allowed_freq));
    dp = DAStudio.DialogProvider;
    % The warning image can be shown by updating DilagImage with warning
    % picture. In the below line there the file used corresponds to icon
    % but not dialog image.
    % dp.DialogImage = fullfile(matlabroot,'toolbox','shared','dastudio','resources','warning.bmp');
    dp.errordlg(msg, 'Warning: Coder Target', true);
end% end %displayWarningMessage

function peripherals_updated = setDIVSEL(clkin_div, peripherals, subFamily, hDlg)
peripherals_updated = peripherals;
if isa(hDlg, 'DAStudio.Dialog')
    imd = DAStudio.imDialog.getIMWidgets(hDlg);
    widget = imd.find('tag','Tag_ConfigSet_CoderTarget_Clocking_Clock_divider');
    if isa(widget, 'DAStudio.imComboBox')
        peripherals_updated.Clocking.DIVSEL = calcDIVSELString(clkin_div, peripherals, subFamily);
        divsel_idx = find(strcmpi(hDlg.getUserData('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider').Entries, peripherals_updated.Clocking.DIVSEL), 1) - 1;
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider', divsel_idx);
    else
        peripherals_updated.Clocking.DIVSEL = num2str(clkin_div);
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider', num2str(clkin_div));
    end
else
    type = hDlg.getWidgetType('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider');
    if strcmp(type, 'combobox')
        if isequal(subFamily, '2838x') || isequal(subFamily, '28002x') || isequal(subFamily, '28003x') || isequal(subFamily, '28p65x')
            peripherals_updated.Clocking.DIVSEL = num2str(clkin_div);
        else
            peripherals_updated.Clocking.DIVSEL = calcDIVSELString(clkin_div, peripherals, subFamily);
        end
    else
        peripherals_updated.Clocking.DIVSEL = num2str(clkin_div);
    end
end
% end %setDIVSEL

function peripherals_updated = setPLLDIV(pllclk_div, peripherals, hDlg)
% ODIV - Dropdown
peripherals_updated = peripherals;
if isa(hDlg, 'DAStudio.Dialog')
    imd = DAStudio.imDialog.getIMWidgets(hDlg);
    widget = imd.find('tag','Tag_ConfigSet_CoderTarget_Clocking_pll_output_divider');
    if ~isempty(widget)
        peripherals_updated.Clocking.PLLDIV = num2str(pllclk_div);
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_pll_output_divider', num2str(pllclk_div));
    end
else
    type = hDlg.getWidgetType('Tag_ConfigSet_CoderTarget_Clocking_pll_output_divider');
    if ~isempty(type)
        peripherals_updated.Clocking.PLLDIV = num2str(pllclk_div);
    end
end
% end %setPLLDIV

function divsel = getDIVSEL (hDlg, peripherals, subFamily)
% DIVSEL-(OSCCLK*PLLCR/DIVSEL)-DropDown or SYSDIVSEL - EditBox
if isa(hDlg, 'DAStudio.Dialog')
    imd = DAStudio.imDialog.getIMWidgets(hDlg);
    widget = imd.find('tag','Tag_ConfigSet_CoderTarget_Clocking_Clock_divider');
    isComboBox = isa(widget, 'DAStudio.imComboBox');
else
    type = hDlg.getWidgetType('Tag_ConfigSet_CoderTarget_Clocking_Clock_divider');
    isComboBox = strcmp(type, 'combobox');
end
if isComboBox
    if isequal(subFamily, '2838x') || isequal(subFamily, '28002x') || isequal(subFamily, '28003x') || isequal(subFamily, '28p65x')
        divsel = str2double(peripherals.Clocking.DIVSEL);
    else
        divsel = getDividerValueFromString(peripherals.Clocking.DIVSEL);
    end
else
    divsel = str2double(peripherals.Clocking.DIVSEL);
end

% end %getDIVSEL

function plldiv = getPLLDIV (hDlg, peripherals)
% ODIV - Dropdown
if isa(hDlg, 'DAStudio.Dialog')
    imd = DAStudio.imDialog.getIMWidgets(hDlg);
    widget = imd.find('tag','Tag_ConfigSet_CoderTarget_Clocking_pll_output_divider');
else
    widget = hDlg.getWidgetType('Tag_ConfigSet_CoderTarget_Clocking_pll_output_divider');
end
if ~isempty(widget)
    plldiv = str2double(peripherals.Clocking.PLLDIV);
else
    plldiv = 1;
end
% end %getDIVSEL


function peripherals_updated = setREFDIV(refDiv, peripherals, hDlg)
% REFDIV - Dropdown
peripherals_updated = peripherals;
if isa(hDlg, 'DAStudio.Dialog')
    imd = DAStudio.imDialog.getIMWidgets(hDlg);
    widget = imd.find('tag','Tag_ConfigSet_CoderTarget_Clocking_intclk_ref_divider');
    if ~isempty(widget)
        peripherals_updated.Clocking.REFDIV = num2str(refDiv);
        hDlg.setWidgetValue('Tag_ConfigSet_CoderTarget_Clocking_intclk_ref_divider', num2str(refDiv));
    end
else
    type = hDlg.getWidgetType('Tag_ConfigSet_CoderTarget_Clocking_intclk_ref_divider');
    if ~isempty(type)
        peripherals_updated.Clocking.REFDIV = num2str(refDiv);
    end
end
% end %setREFDIV


function refDiv = getREFDIV (hDlg, peripherals)
% REFDIV - Dropdown
if isa(hDlg, 'DAStudio.Dialog')
    imd = DAStudio.imDialog.getIMWidgets(hDlg);
    widget = imd.find('tag','Tag_ConfigSet_CoderTarget_Clocking_intclk_ref_divider');
else
    widget = hDlg.getWidgetType('Tag_ConfigSet_CoderTarget_Clocking_intclk_ref_divider');
end
if ~isempty(widget)
    refDiv = str2double(peripherals.Clocking.REFDIV);
else
    refDiv = 1;
end
% end %getREFDIV
