use of net.sourceforge.usbdm.deviceEditor.information.LongVariable in project usbdm-eclipse-plugins by podonoghue.
the class OscValidate method validate.
/**
* Class to determine oscillator settings
* @throws Exception
*/
@Override
public void validate(Variable variable) throws Exception {
super.validate(variable);
// OSC
// =================================
BooleanVariable osc_cr_erclkenVar = getBooleanVariable("osc_cr_erclken");
BooleanVariable mcg_c2_erefs0Var = getBooleanVariable("/MCG/mcg_c2_erefs0");
BooleanVariable mcg_c2_hgo0Var = getBooleanVariable("/MCG/mcg_c2_hgo0");
ChoiceVariable osc_cr_scpVar = getChoiceVariable("osc_cr_scp");
Variable osc_cr_erefstenVar = getVariable("osc_cr_erefsten");
Variable oscillatorRangeVar = getVariable("oscillatorRange");
LongVariable system_oscer_undiv_clockVar = safeGetLongVariable("oscer_undiv_clock");
LongVariable system_oscer_clockVar = null;
if (system_oscer_undiv_clockVar == null) {
system_oscer_undiv_clockVar = getLongVariable("oscer_clock");
} else {
system_oscer_clockVar = getLongVariable("oscer_clock");
}
ChoiceVariable osc_div_erpsVar = safeGetChoiceVariable("osc_div_erps");
LongVariable osc32k_clockVar = getLongVariable("osc32k_clock");
LongVariable osc_clockVar = getLongVariable("osc_clock");
LongVariable osc_input_freqVar = getLongVariable("osc_input_freq");
// Check if RTC has control of oscillator pins
boolean rtcForcing = getVariable("/SIM/rtcSharesPins").getValueAsBoolean() && getBooleanVariable("/RTC/rtc_cr_osce").getValueAsBoolean();
String rangeOrigin = "Unused";
int range = UNCONSTRAINED_RANGE;
if (rtcForcing) {
// RTC controlling XTAL pins
Status rtcInUseMessage = new Status("Feature is controlled by RTC which shares XTAL/EXTAL pins", Severity.WARNING);
oscillatorRangeVar.enable(false);
oscillatorRangeVar.setStatus(rtcInUseMessage);
osc_cr_erclkenVar.enable(false);
osc_cr_erclkenVar.setStatus(rtcInUseMessage);
osc_cr_erefstenVar.enable(false);
osc_cr_erefstenVar.setStatus(rtcInUseMessage);
osc_cr_scpVar.enable(false);
osc_cr_scpVar.setStatus(rtcInUseMessage);
mcg_c2_erefs0Var.enable(false);
mcg_c2_erefs0Var.setStatus(rtcInUseMessage);
mcg_c2_hgo0Var.enable(false);
mcg_c2_hgo0Var.setStatus(rtcInUseMessage);
rangeOrigin = "Determined by RTC";
range = 0;
} else {
// OSC controlling XTAL pins
oscillatorRangeVar.enable(true);
oscillatorRangeVar.clearStatus();
osc_cr_erclkenVar.enable(true);
osc_cr_erclkenVar.clearStatus();
osc_cr_erefstenVar.enable(osc_cr_erclkenVar.getValueAsBoolean());
osc_cr_erefstenVar.clearStatus();
mcg_c2_erefs0Var.enable(true);
mcg_c2_erefs0Var.clearStatus();
osc_input_freqVar.clearStatus();
String oscclk_clockOrg;
Status oscclk_clockStatus = null;
boolean oscillatorInUse = mcg_c2_erefs0Var.getValueAsBoolean();
long osc_input_freq = osc_input_freqVar.getValueAsLong();
if (oscillatorInUse) {
// Using oscillator - range is chosen to suit crystal frequency (or forced by RTC)
if ((osc_input_freq >= EXTERNAL_EXTAL_RANGE1_MIN) && (osc_input_freq <= EXTERNAL_EXTAL_RANGE1_MAX)) {
oscclk_clockOrg = "OSCCLK (low range oscillator)";
rangeOrigin = "Determined by Crystal Frequency";
range = 0;
} else if ((osc_input_freq >= EXTERNAL_EXTAL_RANGE2_MIN) && (osc_input_freq <= EXTERNAL_EXTAL_RANGE2_MAX)) {
oscclk_clockOrg = "OSCCLK (high range oscillator)";
rangeOrigin = "Determined by Crystal Frequency";
range = 1;
} else if ((osc_input_freq >= EXTERNAL_EXTAL_RANGE3_MIN) && (osc_input_freq <= EXTERNAL_EXTAL_RANGE3_MAX)) {
oscclk_clockOrg = "OSCCLK (very high range oscillator)";
rangeOrigin = "Determined by Crystal Frequency";
range = 2;
} else {
// Not suitable as OSC Crystal frequency
oscclk_clockOrg = "OSCCLK (invalid range)";
oscclk_clockStatus = FLL_CLOCK_ERROR_MSG;
range = UNCONSTRAINED_RANGE;
}
} else {
// Using external clock
oscclk_clockOrg = "OSCCLK (External clock)";
// Range has no effect on Oscillator
range = UNCONSTRAINED_RANGE;
// Check suitable clock range
if (osc_input_freq > EXTERNAL_CLOCK_MAX) {
// Not suitable as external clock
oscclk_clockStatus = CLOCK_RANGE_ERROR_MSG;
}
}
osc_cr_scpVar.enable(oscillatorInUse);
osc_cr_scpVar.clearStatus();
mcg_c2_hgo0Var.enable(oscillatorInUse);
mcg_c2_hgo0Var.clearStatus();
boolean oscclkOK = (oscclk_clockStatus == null) || oscclk_clockStatus.getSeverity().lessThan(Severity.WARNING);
osc_clockVar.setOrigin(oscclk_clockOrg);
osc_clockVar.setStatus(oscclk_clockStatus);
osc_clockVar.setValue(oscclkOK ? osc_input_freq : 0);
osc_clockVar.enable(oscclkOK);
}
oscillatorRangeVar.setValue(range);
oscillatorRangeVar.setOrigin(rangeOrigin);
// Check suitability of OSC for OSC32KCLK
// =========================================
// Initially assume suitable
long osc32kclk_clockFreq = osc_clockVar.getValueAsLong();
Status osc32kclk_clockStatus = osc_clockVar.getStatus();
String osc32kclk_clockOrg = osc_clockVar.getOrigin();
if ((osc32kclk_clockFreq < EXTERNAL_EXTAL_RANGE1_MIN) || (osc32kclk_clockFreq > EXTERNAL_EXTAL_RANGE1_MAX)) {
if ((osc32kclk_clockStatus == null) || osc32kclk_clockStatus.lessThan(Severity.ERROR)) {
osc32kclk_clockStatus = new Status(OSCCLK32K_CLOCK_MSG, Severity.WARNING);
}
osc32kclk_clockOrg = osc32kclk_clockOrg + "(invalid range)";
}
osc32k_clockVar.setValue((osc32kclk_clockStatus != null) ? 0 : osc32kclk_clockFreq);
osc32k_clockVar.setStatus(osc32kclk_clockStatus);
osc32k_clockVar.setOrigin(osc32kclk_clockOrg);
// Determine OSCERCLK, OSCERCLK_UNDIV
// ==================================
long osc_clockFreq = osc_clockVar.getValueAsLong();
String osc_clockOrg = osc_clockVar.getOrigin();
if (osc_cr_erclkenVar.getValueAsBoolean()) {
// Oscillator/clock enabled
system_oscer_undiv_clockVar.setValue(osc_clockFreq);
system_oscer_undiv_clockVar.setStatus(osc_clockVar.getFilteredStatus());
system_oscer_undiv_clockVar.setOrigin(osc_clockVar.getOrigin());
system_oscer_undiv_clockVar.enable(true);
long system_oscerclk = osc_clockFreq;
if (osc_div_erpsVar != null) {
// If divider exists
system_oscerclk /= 1 << osc_div_erpsVar.getValueAsLong();
osc_clockOrg += "/osc_div_erps";
system_oscer_clockVar.setValue(system_oscerclk);
system_oscer_clockVar.setStatus(osc_clockVar.getFilteredStatus());
system_oscer_clockVar.setOrigin(osc_clockVar.getOrigin());
system_oscer_clockVar.enable(true);
osc_div_erpsVar.enable(true);
}
} else {
Status osc_crMessage = new Status("Disabled by osc_cr_erclken", Severity.OK);
// Oscillator/clock disabled
// system_oscerclk_undiv_clockVar.setValue(0);
system_oscer_undiv_clockVar.setStatus(osc_crMessage);
system_oscer_undiv_clockVar.setOrigin(osc_clockOrg);
system_oscer_undiv_clockVar.enable(false);
if (osc_div_erpsVar != null) {
// system_oscer_clockVar.setValue(0);
system_oscer_clockVar.setStatus(osc_crMessage);
system_oscer_clockVar.setOrigin(osc_clockOrg);
system_oscer_clockVar.enable(false);
osc_div_erpsVar.enable(false);
}
}
// Warn if EXTAL and XTAL signals not mapped
validateMappedPins(new int[] { 0, 1 }, getPeripheral().getSignalTables().get(0).table);
}
use of net.sourceforge.usbdm.deviceEditor.information.LongVariable in project usbdm-eclipse-plugins by podonoghue.
the class PdbValidate method doChannelValidate.
/**
* Validate a PDB channel settings
*
* @param variable Variable that triggered change (may be null)
* @param channel The channel to validate e.g. "0", "1" etc
*
* @throws Exception
*/
void doChannelValidate(Variable variable, String channel) throws Exception {
// In/Out
LongVariable pdb_chX_dly0Var = safeGetLongVariable("pdb_ch" + channel + "_dly0");
if (pdb_chX_dly0Var == null) {
// Channel doesn't exit
return;
}
LongVariable pdb_chX_dly1Var = getLongVariable("pdb_ch" + channel + "_dly1");
LongVariable pdb_chX_c1_tosVar = getLongVariable("pdb_ch" + channel + "_c1_tos");
LongVariable pdb_chX_c1_enVar = getLongVariable("pdb_ch" + channel + "_c1_en");
// In/Out
DoubleVariable pdb_chX_dly0_delayVar = getDoubleVariable("pdb_ch" + channel + "_dly0_delay");
DoubleVariable pdb_chX_dly1_delayVar = getDoubleVariable("pdb_ch" + channel + "_dly1_delay");
boolean pt0Enable = (pdb_chX_c1_enVar.getValueAsLong() & (1 << 0)) != 0;
boolean pt1Enable = (pdb_chX_c1_enVar.getValueAsLong() & (1 << 1)) != 0;
boolean dly0Enable = pt0Enable && ((pdb_chX_c1_tosVar.getValueAsLong() & (1 << 0)) != 0);
boolean dly1Enable = pt1Enable && ((pdb_chX_c1_tosVar.getValueAsLong() & (1 << 1)) != 0);
// Do enable/disable first
pdb_chX_dly0Var.enable(dly0Enable);
pdb_chX_dly0_delayVar.enable(dly0Enable);
pdb_chX_dly1Var.enable(dly1Enable);
pdb_chX_dly1_delayVar.enable(dly1Enable);
// Get current values
long pdb_chX_dly0 = pdb_chX_dly0Var.getRawValueAsLong();
double pdb_chX_dly0_delay = pdb_chX_dly0_delayVar.getRawValueAsDouble();
long pdb_chX_dly1 = pdb_chX_dly1Var.getRawValueAsLong();
double pdb_chX_dly1_delay = pdb_chX_dly1_delayVar.getRawValueAsDouble();
if (clockChanged) {
pdb_chX_dly0_delayVar.setOrigin(pdbClockOrigin + " period * pdb_ch" + channel + "_dly0");
pdb_chX_dly1_delayVar.setOrigin(pdbClockOrigin + " period * pdb_ch" + channel + "_dly1");
}
if (modChanged) {
pdb_chX_dly0Var.setMax(pdb_mod);
pdb_chX_dly1Var.setMax(pdb_mod);
}
if (variable != null) {
if (variable.equals(pdb_chX_dly0_delayVar)) {
// Calculate rounded value
pdb_chX_dly0 = Math.max(0, Math.round((pdb_chX_dly0_delay / pdb_period) - 1));
// Update
pdb_chX_dly0Var.setValue(pdb_chX_dly0);
} else if (variable.equals(pdb_chX_dly1_delayVar)) {
// Calculate rounded value
pdb_chX_dly1 = Math.max(0, Math.round((pdb_chX_dly1_delay / pdb_period) - 1));
// Update
pdb_chX_dly1Var.setValue(pdb_chX_dly1);
}
}
pdb_chX_dly0_delayVar.setValue(pdb_period * (pdb_chX_dly0 + 1));
pdb_chX_dly1_delayVar.setValue(pdb_period * (pdb_chX_dly1 + 1));
pdb_chX_dly0_delayVar.setMax((pdb_mod + 1.5) * pdb_period);
pdb_chX_dly1_delayVar.setMax((pdb_mod + 1.5) * pdb_period);
}
use of net.sourceforge.usbdm.deviceEditor.information.LongVariable in project usbdm-eclipse-plugins by podonoghue.
the class PdbValidate method doCounterValidate.
/**
* Validate the PDB counter settings
*
* @param variable Variable that triggered change (may be null)
*
* @throws Exception
*/
public void doCounterValidate(Variable variable) throws Exception {
// In/Out
LongVariable pdb_modVar = getLongVariable("pdb_mod");
LongVariable pdb_idlyVar = getLongVariable("pdb_idly");
DoubleVariable pdb_mod_periodVar = getDoubleVariable("pdb_mod_period");
DoubleVariable pdb_idly_delayVar = getDoubleVariable("pdb_idly_delay");
pdb_mod = pdb_modVar.getValueAsLong();
pdb_mod_period = pdb_mod_periodVar.getValueAsDouble();
long pdb_idly = pdb_idlyVar.getValueAsLong();
double pdb_idly_delay = pdb_idly_delayVar.getValueAsDouble();
if (clockChanged) {
// Update everything
pdb_mod_period = (pdb_mod + 1) * pdb_period;
pdb_mod_periodVar.setValue(pdb_mod_period);
pdb_mod_periodVar.setOrigin(pdbClockOrigin + " period * PDB modulo");
pdb_mod_periodVar.setMax((pdb_modVar.getMax() + 1.5) * pdb_period);
pdb_idly_delayVar.setValue((pdb_idly + 1) * pdb_period);
pdb_idly_delayVar.setOrigin(pdbClockOrigin + " period * PDB idly");
modChanged = true;
} else if (variable != null) {
// Update selectively
if (variable.equals(pdb_modVar)) {
pdb_mod_period = (pdb_mod + 1) * pdb_period;
pdb_mod_periodVar.setValue(pdb_mod_period);
modChanged = true;
} else if (variable.equals(pdb_mod_periodVar)) {
// Calculate rounded value
pdb_mod = Math.max(0, Math.round((pdb_mod_period / pdb_period) - 1));
pdb_mod_period = (pdb_mod + 1) * pdb_period;
// Update
pdb_modVar.setValue(pdb_mod);
// Need to show effect of rounding
pdb_mod_periodVar.setValue(pdb_mod_period);
modChanged = true;
} else if (variable.equals(pdb_idlyVar)) {
pdb_idly_delayVar.setValue((pdb_idly + 1) * pdb_period);
} else if (variable.equals(pdb_idly_delayVar)) {
// Calculate rounded value
pdb_idly = Math.max(0, Math.round((pdb_idly_delay / pdb_period) - 1));
// Update
pdb_idlyVar.setValue(pdb_idly);
// Need to show effect of rounding
pdb_idly_delayVar.setValue((pdb_idly + 1) * pdb_period);
}
}
if (modChanged) {
pdb_idly_delayVar.setMax((pdb_mod + 1.5) * pdb_period);
pdb_idlyVar.setMax(pdb_mod);
}
}
use of net.sourceforge.usbdm.deviceEditor.information.LongVariable in project usbdm-eclipse-plugins by podonoghue.
the class RtcValidate method validate.
/**
* Class to determine RTC oscillator settings
*
* Outputs rtcclk_clock, rtcclk_gated_clock,
* @throws Exception
*/
@Override
public void validate(Variable variable) throws Exception {
// Indicates RTC uses main oscillator XTAL/EXTAL pins
Variable rtcSharesPinsVar = safeGetVariable("/SIM/rtcSharesPins");
boolean rtcSharesPins = (safeGetVariable("/SIM/rtcSharesPins") != null) && rtcSharesPinsVar.getValueAsBoolean();
super.validate(variable);
// RTC
// =================================
BooleanVariable rtc_cr_osceVar = getBooleanVariable("rtc_cr_osce");
ChoiceVariable rtc_cr_scpVar = getChoiceVariable("rtc_cr_scp");
Variable rtc_cr_umVar = getVariable("rtc_cr_um");
Variable rtc_cr_supVar = getVariable("rtc_cr_sup");
Variable rtc_cr_wpeVar = getVariable("rtc_cr_wpe");
LongVariable rtc_1hz_clockVar = getLongVariable("rtc_1hz_clock");
long osc_input_freq = 0;
LongVariable osc_input_freqVar = null;
LongVariable osc_clockVar = null;
Status status = null;
String origin = "RTCCLK";
if (rtcSharesPins) {
// RTC uses main oscillator XTAL/EXTAL pins
// ===================================================
String osc0_peripheral = getStringVariable("/SIM/osc0_peripheral").getValueAsString();
osc_input_freqVar = getLongVariable(osc0_peripheral + "/osc_input_freq");
osc_clockVar = getLongVariable(osc0_peripheral + "/osc_clock");
origin = "RTCCLK";
rtc_cr_osceVar.setToolTip("Enable main oscillator as 32kHz RTC oscillator\n" + "Note: this disables OSC control by MCG");
} else {
// RTC uses separate XTAL32/EXTAL32 pins
// ===================================================
osc_input_freqVar = getLongVariable("osc_input_freq");
osc_clockVar = getLongVariable("osc_clock");
origin = "RTCCLK";
rtc_cr_osceVar.setToolTip("Enable 32kHz RTC oscillator");
// Warn if SCL and SDA signals not mapped
validateMappedPins(new int[] { 0, 1 }, getPeripheral().getSignalTables().get(0).table);
}
osc_input_freq = osc_input_freqVar.getValueAsLong();
// =========================================
// Check input clock/oscillator ranges
//
long rtcClockFrequency = osc_input_freq;
if ((osc_input_freq < RtcValidate.EXTERNAL_EXTAL_RANGE_MIN) || (osc_input_freq > RtcValidate.EXTERNAL_EXTAL_RANGE_MAX)) {
status = OSCCLK32K_CLOCK_WARNING_MSG;
origin = origin + " (invalid range)";
rtcClockFrequency = 0L;
rtc_cr_osceVar.setValue(false);
rtc_cr_osceVar.setStatus(status);
if (!rtcSharesPins) {
osc_input_freqVar.setStatus(status);
}
} else {
rtc_cr_osceVar.setStatus((Status) null);
if (!rtcSharesPins) {
osc_input_freqVar.setStatus((Status) null);
}
if (!rtc_cr_osceVar.getValueAsBoolean()) {
status = new Status("Disabled by rtc_cr_osce", Severity.WARNING);
origin = origin + " (disabled)";
rtcClockFrequency = 0L;
}
}
// =========================================
// Check and propagate enabled
//
boolean rtc_cr_osce = rtc_cr_osceVar.isEnabled() && rtc_cr_osceVar.getValueAsBoolean();
rtc_cr_scpVar.enable(rtc_cr_osce);
rtc_cr_umVar.enable(rtc_cr_osce);
rtc_cr_supVar.enable(rtc_cr_osce);
rtc_cr_wpeVar.enable(rtc_cr_osce);
osc_clockVar.enable(rtc_cr_osce);
rtc_1hz_clockVar.enable(rtc_cr_osce);
if (!rtcSharesPins || rtc_cr_osce) {
// Only update if owned by RTC
osc_clockVar.setValue(rtcClockFrequency);
osc_clockVar.setStatus(status);
osc_clockVar.setOrigin(origin);
}
rtc_1hz_clockVar.setValue((rtcClockFrequency > 0) ? 1 : 0);
rtc_1hz_clockVar.setStatus(status);
rtc_1hz_clockVar.setOrigin(origin);
// RTC Clocks
// ==============================
// Check if gating option
BooleanVariable rtc_cr_clkoVar = getBooleanVariable("rtc_cr_clko");
LongVariable rtcclk_gated_clockVar = getLongVariable("rtcclk_gated_clock");
rtc_cr_clkoVar.enable(rtc_cr_osce);
if (rtc_cr_clkoVar.isEnabled() && rtc_cr_clkoVar.getValueAsBoolean()) {
rtcclk_gated_clockVar.setValue(rtcClockFrequency);
rtcclk_gated_clockVar.setStatus(status);
rtcclk_gated_clockVar.setOrigin(origin);
rtcclk_gated_clockVar.enable(rtc_cr_osce);
} else {
rtcclk_gated_clockVar.setValue(0L);
rtcclk_gated_clockVar.setStatus(new Status("Disabled by rtc_cr_clko", Severity.WARNING));
rtcclk_gated_clockVar.setOrigin("RTCCLK (disabled)");
rtcclk_gated_clockVar.enable(false);
}
}
use of net.sourceforge.usbdm.deviceEditor.information.LongVariable in project usbdm-eclipse-plugins by podonoghue.
the class SimValidate method validateNonindexedVariables.
/**
* Updates
* - srcVar
* - clockVar
* - system_erclk32k_clockVar
* - sim_sopt1_osc32kselVar
* - sim_sopt2_rtcclkoutselVar
* - rtc_clkoutVar
* - system_usbfs_clockVar
*
* @param variable
* @throws Exception
*/
public void validateNonindexedVariables(Variable variable) throws Exception {
super.validate(variable);
// Clock Mapping
// =================
final String osc0_peripheral = getStringVariable("osc0_peripheral").getValueAsString();
final LongVariable osc0_oscer_clockVar = getLongVariable(osc0_peripheral + "/oscer_clock");
final LongVariable osc0_osc32k_clockVar = getLongVariable(osc0_peripheral + "/osc32k_clock");
final String osc32k_peripheral = getStringVariable("/SIM/osc32k_peripheral").getValueAsString();
final LongVariable rtcclk_gated_clockVar = safeGetLongVariable(osc32k_peripheral + "/rtcclk_gated_clock");
final LongVariable rtc_1hz_clockVar = safeGetLongVariable(osc32k_peripheral + "/rtc_1hz_clock");
final LongVariable rtc_clkoutVar = safeGetLongVariable("rtc_clkout");
// MCG
// =================
final LongVariable system_low_power_clockVar = getLongVariable("/MCG/system_low_power_clock");
final LongVariable system_mcgirclk_clockVar = getLongVariable("/MCG/system_mcgirclk_clock");
final LongVariable system_usb_clkin_clockVar = safeGetLongVariable("/MCG/system_usb_clkin_clock");
final LongVariable peripheralClockVar = getLongVariable("system_peripheral_clock");
// Check if CLKDIV3 Present
// =====================================
final Long pllPostDiv3Value;
final String pllPostDiv3Origin;
final Variable system_peripheral_postdivider_clockVar = safeGetVariable("system_peripheral_postdivider_clock");
if (system_peripheral_postdivider_clockVar != null) {
// After divider
pllPostDiv3Value = system_peripheral_postdivider_clockVar.getValueAsLong();
pllPostDiv3Origin = system_peripheral_postdivider_clockVar.getOrigin();
} else {
// Direct (no divider)
pllPostDiv3Value = peripheralClockVar.getValueAsLong();
pllPostDiv3Origin = peripheralClockVar.getOrigin();
}
/**
* Clock selector used for LPUARTs, TPMs and FlexIO
*/
LpClockSelector clockSelector = new LpClockSelector() {
@Override
public void lpClockSelect(String sourceVar, String clockVarId) throws Exception {
// Clock source select (if present)
// ===================================
Variable srcVar = safeGetVariable(sourceVar);
if (srcVar != null) {
Variable clockVar = getVariable(clockVarId);
switch((int) srcVar.getValueAsLong()) {
default:
srcVar.setValue(0);
case // Disabled
0:
clockVar.setValue(0);
clockVar.setStatus((Status) null);
clockVar.setOrigin("Disabled");
break;
case // Peripheral Clock / CLKDIV3
1:
clockVar.setValue(pllPostDiv3Value);
clockVar.setStatus(peripheralClockVar.getStatus());
clockVar.setOrigin(pllPostDiv3Origin);
break;
case // OSCERCLK
2:
clockVar.setValue(osc0_oscer_clockVar.getValueAsLong());
clockVar.setStatus(osc0_oscer_clockVar.getStatus());
clockVar.setOrigin(osc0_oscer_clockVar.getOrigin());
break;
case // MCGIRCLK
3:
clockVar.setValue(system_mcgirclk_clockVar.getValueAsLong());
clockVar.setStatus(system_mcgirclk_clockVar.getStatus());
clockVar.setOrigin(system_mcgirclk_clockVar.getOrigin());
break;
}
}
}
};
// Determine ERCLK32K
// ==================================
LongVariable system_erclk32k_clockVar = getLongVariable("system_erclk32k_clock");
ChoiceVariable sim_sopt1_osc32kselVar = getChoiceVariable("sim_sopt1_osc32ksel");
switch((int) sim_sopt1_osc32kselVar.getValueAsLong()) {
case // System oscillator (OSC32KCLK)
0:
system_erclk32k_clockVar.setValue(osc0_osc32k_clockVar.getValueAsLong());
system_erclk32k_clockVar.setOrigin(osc0_osc32k_clockVar.getOrigin());
system_erclk32k_clockVar.setStatus(osc0_osc32k_clockVar.getStatus());
break;
case // RTC 32.768kHz oscillator
2:
system_erclk32k_clockVar.setValue(rtcclk_gated_clockVar.getValueAsLong());
system_erclk32k_clockVar.setOrigin(rtcclk_gated_clockVar.getOrigin());
system_erclk32k_clockVar.setStatus(rtcclk_gated_clockVar.getStatus());
break;
default:
sim_sopt1_osc32kselVar.setValue(3);
case // LPO 1 kHz
3:
system_erclk32k_clockVar.setValue(system_low_power_clockVar.getValueAsLong());
system_erclk32k_clockVar.setOrigin(system_low_power_clockVar.getOrigin());
system_erclk32k_clockVar.setStatus(system_low_power_clockVar.getStatus());
break;
}
// RTC Clock out pin select
// ============================
BooleanVariable sim_sopt2_rtcclkoutselVar = safeGetBooleanVariable("sim_sopt2_rtcclkoutsel");
if (sim_sopt2_rtcclkoutselVar != null) {
switch((int) sim_sopt2_rtcclkoutselVar.getValueAsLong()) {
default:
sim_sopt2_rtcclkoutselVar.setValue(0);
case // RTC seconds clock = 1Hz
0:
rtc_clkoutVar.setValue(rtc_1hz_clockVar.getValueAsLong());
rtc_clkoutVar.setStatus(rtc_1hz_clockVar.getStatus());
rtc_clkoutVar.setOrigin(rtc_1hz_clockVar.getOrigin());
break;
case // RTC 32.768kHz oscillator
1:
rtc_clkoutVar.setValue(rtcclk_gated_clockVar.getValueAsLong());
rtc_clkoutVar.setStatus(rtcclk_gated_clockVar.getStatus());
rtc_clkoutVar.setOrigin(rtcclk_gated_clockVar.getOrigin());
break;
}
}
// UART0 Clock source select (if present)
// ==========================================
clockSelector.lpClockSelect("sim_sopt2_uart0src", "system_uart0_clock");
// LPUARTx Clock source select (if present)
// ==========================================
final String[] lpUartInstances = { "", "0", "1", "2" };
for (String lpUartInstance : lpUartInstances) {
clockSelector.lpClockSelect("sim_sopt2_lpuart" + lpUartInstance + "src", "system_lpuart" + lpUartInstance + "_clock");
}
// TPMx Clock source select (if present)
// ==========================================
final String[] tpmInstances = { "", "0", "1", "2" };
for (String tpmInstance : tpmInstances) {
clockSelector.lpClockSelect("sim_sopt2_tpm" + tpmInstance + "src", "system_tpm" + tpmInstance + "_clock");
}
// USB FS Clock source select
// ============================
ChoiceVariable sim_sopt2_usbsrcVar = safeGetChoiceVariable("sim_sopt2_usbsrc");
if (sim_sopt2_usbsrcVar != null) {
ChoiceVariable sim_clkdiv2_usbVar = safeGetChoiceVariable("sim_clkdiv2_usb");
if (sim_clkdiv2_usbVar != null) {
// USB divider CLKDIV2 exists
int usbCalcValue = -1;
if (sim_sopt2_usbsrcVar.getValueAsLong() == 0) {
// Using USB CLKIN pin
sim_clkdiv2_usbVar.enable(false);
sim_clkdiv2_usbVar.setOrigin("Not used with external clock");
sim_clkdiv2_usbVar.setLocked(false);
} else {
// Using internal clock
// Try to auto calculate divisor
long clock = peripheralClockVar.getValueAsLong();
for (int usbdiv = 0; usbdiv <= 7; usbdiv++) {
for (int usbfrac = 0; usbfrac <= 1; usbfrac++) {
long testValue = Math.round(clock * (usbfrac + 1.0) / (usbdiv + 1.0));
if (testValue == 48000000) {
usbCalcValue = (usbdiv << 1) + usbfrac;
break;
}
}
if (usbCalcValue >= 0) {
break;
}
}
sim_clkdiv2_usbVar.enable(true);
if (usbCalcValue >= 0) {
long temp = sim_clkdiv2_usbVar.getValueAsLong();
sim_clkdiv2_usbVar.setRawValue(usbCalcValue);
if (sim_clkdiv2_usbVar.getValueAsLong() != temp) {
// Trigger update on change
sim_clkdiv2_usbVar.notifyListeners();
}
sim_clkdiv2_usbVar.setOrigin("Automatically calculated from input clock");
sim_clkdiv2_usbVar.setLocked(true);
} else {
sim_clkdiv2_usbVar.setOrigin("Manually selected");
sim_clkdiv2_usbVar.setLocked(false);
}
}
}
LongVariable system_usbfs_clockVar = getLongVariable("system_usbfs_clock");
if (sim_sopt2_usbsrcVar.getValueAsLong() == 0) {
// Using USB_CLKIN
system_usbfs_clockVar.setValue(system_usb_clkin_clockVar.getValueAsLong());
system_usbfs_clockVar.setStatus(system_usb_clkin_clockVar.getStatus());
system_usbfs_clockVar.setOrigin(system_usb_clkin_clockVar.getOrigin());
} else {
// Using internal clock
if (sim_clkdiv2_usbVar != null) {
// Peripheral Clock / CLKDIV2
int usbValue = Long.decode(sim_clkdiv2_usbVar.getSubstitutionValue()).intValue();
int usbfrac = usbValue & 0x1;
int usbdiv = (usbValue >> 1) & 0x7;
long usbPostDiv2 = peripheralClockVar.getValueAsLong() * (usbfrac + 1) / (usbdiv + 1);
system_usbfs_clockVar.setValue(usbPostDiv2);
system_usbfs_clockVar.setStatus(peripheralClockVar.getStatus());
system_usbfs_clockVar.setOrigin(peripheralClockVar.getOrigin() + " after /CLKDIV2");
} else {
// Directly using peripheral clock
system_usbfs_clockVar.setValue(peripheralClockVar.getValueAsLong());
system_usbfs_clockVar.setStatus(peripheralClockVar.getStatus());
system_usbfs_clockVar.setOrigin(peripheralClockVar.getOrigin());
}
}
}
}
Aggregations