function eCANTSEGCallback(hObj, hDlg, tag, ~)
%ECANTSEGCALLBACK Callback to check I2C Own address register validity

% Copyright 2013 The MathWorks, Inc.


if codertarget.target.isCoderTarget(hObj.getConfigSet())
    cs = hObj.getConfigSet();
    ud = hDlg.getUserData(tag);
    fieldName = ud.Storage;
    if isempty(fieldName)
        fieldName = strrep(tag, 'Tag_ConfigSet_CoderTarget_', '');
    end
    curVal = codertarget.data.getParameterValue(cs, fieldName);
    newVal = str2num(hDlg.getComboBoxText(tag));   %#ok<ST2NM>
    
    if strfind(tag, 'Time_segment_1_TSEG1')
        TSEG1 = newVal;
        TSEG2_TAG = tag;
        TSEG2_TAG(length(TSEG2_TAG)-6:end) = '2_TSEG2';
        TSEG2_UD = hDlg.getUserData(TSEG2_TAG);
        fieldName_new = TSEG2_UD.Storage;
        if isempty(fieldName_new)
            fieldName_new = strrep(TSEG2_TAG, 'Tag_ConfigSet_CoderTarget_', '');
        end
        TSEG2 = str2num(codertarget.data.getParameterValue(cs, fieldName_new)); %#ok<ST2NM>
    else
        TSEG2 = newVal;
        TSEG1_TAG = tag;
        TSEG1_TAG(length(TSEG1_TAG)-6:end) = '1_TSEG1';
        TSEG1_UD = hDlg.getUserData(TSEG1_TAG);
        fieldName_new = TSEG1_UD.Storage;
        if isempty(fieldName_new)
            fieldName_new = strrep(TSEG1_TAG, 'Tag_ConfigSet_CoderTarget_', '');
        end
        TSEG1 = str2num(codertarget.data.getParameterValue(cs, fieldName_new)); %#ok<ST2NM>
    end
    
    if (TSEG1 < TSEG2)
        hDlg.setWidgetValue(tag, find(strcmpi(ud.Entries,curVal))-1);
        errorID = 'tgtPrefsPeripheralTSEGsCompare';
        msg = DAStudio.message(['TIC2000:blocks:' errorID]);
        dp = DAStudio.DialogProvider;
        dp.errordlg(msg, 'Error: Coder Target', true);
        return;
    else
        codertarget.data.setParameterValue(cs, fieldName, num2str(newVal));
        hDlg.setWidgetValue(tag, find(strcmpi(ud.Entries,num2str(newVal)))-1); 
    end
end

end