Search in sources :

Example 6 with Variable

use of net.sourceforge.usbdm.deviceEditor.information.Variable in project usbdm-eclipse-plugins by podonoghue.

the class PdbValidate method doClockValidate.

/**
 * Validate the PDB clock settings
 *
 * @param  variable   Variable that triggered change (may be null)
 *
 * @throws Exception
 */
void doClockValidate(Variable variable) throws Exception {
    // In
    Variable clockVar = getVariable("/SIM/system_bus_clock");
    Variable pdb_sc_prescalerVar = getVariable("pdb_sc_prescaler");
    Variable pdb_sc_multVar = getVariable("pdb_sc_mult");
    // Out
    DoubleVariable pdb_periodVar = getDoubleVariable("pdb_period");
    DoubleVariable pdb_frequencyVar = getDoubleVariable("pdb_frequency");
    pdbClockOrigin = "PDB Clock";
    clockChanged = (variable == null) || ((variable.equals(clockVar)) || (variable.equals(pdb_sc_prescalerVar)) || (variable.equals(pdb_sc_multVar)));
    long busFrequency = clockVar.getValueAsLong();
    long pdb_sc_prescaler = pdb_sc_prescalerVar.getValueAsLong();
    long pdb_sc_mult = pdb_sc_multVar.getValueAsLong();
    // MULT divider values
    final long[] multValues = { 1, 10, 20, 40 };
    pdb_frequency = pdb_frequencyVar.getValueAsDouble();
    pdb_period = pdb_periodVar.getValueAsDouble();
    if (clockChanged) {
        // Update everything
        pdb_frequency = busFrequency / ((1 << pdb_sc_prescaler) * multValues[(int) pdb_sc_mult]);
        if (pdb_frequency == 0) {
            // For safety
            pdb_period = 1;
        } else {
            pdb_period = 1 / pdb_frequency;
        }
        pdb_frequencyVar.setValue(pdb_frequency);
        pdb_frequencyVar.setOrigin(pdbClockOrigin + " frequency / (prescaler * divider)");
        pdb_periodVar.setValue(pdb_period);
        pdb_periodVar.setOrigin(pdbClockOrigin + " period * prescaler * divider");
    }
}
Also used : BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) DoubleVariable(net.sourceforge.usbdm.deviceEditor.information.DoubleVariable) Variable(net.sourceforge.usbdm.deviceEditor.information.Variable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) DoubleVariable(net.sourceforge.usbdm.deviceEditor.information.DoubleVariable)

Example 7 with Variable

use of net.sourceforge.usbdm.deviceEditor.information.Variable 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);
    }
}
Also used : Status(net.sourceforge.usbdm.deviceEditor.model.Status) BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable) Variable(net.sourceforge.usbdm.deviceEditor.information.Variable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable)

Example 8 with Variable

use of net.sourceforge.usbdm.deviceEditor.information.Variable 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());
            }
        }
    }
}
Also used : BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable) Variable(net.sourceforge.usbdm.deviceEditor.information.Variable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable)

Example 9 with Variable

use of net.sourceforge.usbdm.deviceEditor.information.Variable in project usbdm-eclipse-plugins by podonoghue.

the class SimValidateMKL 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 rtc_clkin = getStringVariable("rtc_clkin").getValueAsString();
    final LongVariable rtcclkin_clockVar = safeGetLongVariable(rtc_clkin);
    final LongVariable rtc_1hz_clockVar = safeGetLongVariable("/RTC/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");
    LongVariable clockVar = safeGetLongVariable("system_peripheral_clock");
    if (clockVar == null) {
        // Assume no PLL. Peripheral clock is FLL
        clockVar = getLongVariable("/MCG/system_mcgfllclk_clock");
    }
    final LongVariable peripheralClockVar = clockVar;
    // 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 = safeGetChoiceVariable("sim_sopt1_osc32ksel");
    if (sim_sopt1_osc32kselVar == null) {
        // No RTC etc
        system_erclk32k_clockVar.setValue(osc0_osc32k_clockVar.getValueAsLong());
        system_erclk32k_clockVar.setOrigin(osc0_osc32k_clockVar.getOrigin());
        system_erclk32k_clockVar.setStatus(osc0_osc32k_clockVar.getStatus());
    } else {
        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 CLK_IN
            2:
                system_erclk32k_clockVar.setValue(rtcclkin_clockVar.getValueAsLong());
                system_erclk32k_clockVar.setOrigin(rtcclkin_clockVar.getOrigin());
                system_erclk32k_clockVar.setStatus(rtcclkin_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 // OSCERCLK
            1:
                rtc_clkoutVar.setValue(osc0_oscer_clockVar.getValueAsLong());
                rtc_clkoutVar.setStatus(osc0_oscer_clockVar.getStatus());
                rtc_clkoutVar.setOrigin(osc0_oscer_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");
    }
    // FLEXIO Clock source select (if present)
    // ==========================================
    clockSelector.lpClockSelect("sim_sopt2_flexiosrc", "system_flexio_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());
            }
        }
    }
}
Also used : BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable) Variable(net.sourceforge.usbdm.deviceEditor.information.Variable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) LongVariable(net.sourceforge.usbdm.deviceEditor.information.LongVariable) BooleanVariable(net.sourceforge.usbdm.deviceEditor.information.BooleanVariable) ChoiceVariable(net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable)

Example 10 with Variable

use of net.sourceforge.usbdm.deviceEditor.information.Variable in project usbdm-eclipse-plugins by podonoghue.

the class ClockValidator_MCG_Lite method validateClocks.

protected void validateClocks(Variable variable) throws Exception {
    // System.err.println(getSimpleClassName()+" Var = "+variable);
    super.validate(variable);
    // C1
    // =================================
    Variable mcg_c1_clksVar;
    Variable mcg_c1_irclkenVar;
    Variable mcg_c1_irefstenVar;
    // C2
    // =================================
    Variable mcg_c2_rangeVar;
    Variable mcg_c2_ircsVar;
    // SC
    // =================================
    Variable mcg_sc_fcrdivVar;
    // MC
    // =================================
    Variable mcg_mc_hircenVar;
    Variable mcg_mc_lirc_div2Var;
    // LIRC
    // =================================
    Variable system_slow_irc_clockVar;
    Variable system_fast_irc_clockVar;
    Variable system_lirc_clockVar;
    Variable system_lirc_div1_clockVar;
    Variable system_mcgirclk_clockVar;
    // Internal
    // =================================
    Variable system_irc48m_clockVar;
    // Clocks and information from main oscillator
    // =================================
    Variable osc_clockVar;
    Variable osc_oscillatorRangeVar;
    // =================================
    Variable clock_modeVar;
    // =================================
    Variable system_mcgoutclk_clock_sourceVar;
    Variable system_mcgoutclk_clockVar;
    Variable system_mcgpclk_clockVar;
    mcg_c1_clksVar = getVariable("mcg_c1_clks");
    mcg_c1_irclkenVar = getVariable("mcg_c1_irclken");
    mcg_c1_irefstenVar = getVariable("mcg_c1_irefsten");
    mcg_c2_rangeVar = getVariable("mcg_c2_range");
    mcg_c2_ircsVar = getVariable("mcg_c2_ircs");
    mcg_sc_fcrdivVar = safeGetVariable("mcg_sc_fcrdiv");
    mcg_mc_hircenVar = safeGetVariable("mcg_mc_hircen");
    mcg_mc_lirc_div2Var = safeGetVariable("mcg_mc_lirc_div2");
    system_slow_irc_clockVar = getVariable("system_slow_irc_clock");
    system_fast_irc_clockVar = getVariable("system_fast_irc_clock");
    system_lirc_clockVar = getVariable("system_lirc_clock");
    system_lirc_div1_clockVar = getVariable("system_lirc_div1_clock");
    system_mcgirclk_clockVar = getVariable("system_mcgirclk_clock");
    system_irc48m_clockVar = safeGetVariable("system_irc48m_clock");
    osc_clockVar = getVariable("/OSC0/osc_clock");
    osc_oscillatorRangeVar = getVariable("/OSC0/oscillatorRange");
    clock_modeVar = getVariable("clock_mode");
    system_mcgoutclk_clock_sourceVar = getVariable("system_mcgoutclk_clock_source");
    system_mcgoutclk_clockVar = getVariable("system_mcgoutclk_clock");
    system_mcgpclk_clockVar = getVariable("system_mcgpclk_clock");
    long rangeIn = osc_oscillatorRangeVar.getValueAsLong();
    if (rangeIn != OscValidate.UNCONSTRAINED_RANGE) {
        mcg_c2_rangeVar.enable(true);
        mcg_c2_rangeVar.setValue(osc_oscillatorRangeVar.getValueAsLong());
    } else {
        mcg_c2_rangeVar.enable(false);
    }
    // Main clock mode (MCGOUTCLK)
    // =============================
    ClockMode clock_mode = ClockMode.valueOf(clock_modeVar.getSubstitutionValue());
    switch(clock_mode) {
        default:
        case ClockMode_None:
            mcg_c1_clksVar.setValue(0);
            mcg_c2_ircsVar.setLocked(false);
            system_mcgoutclk_clockVar.setValue(system_slow_irc_clockVar.getValueAsLong());
            system_mcgoutclk_clockVar.setOrigin(system_slow_irc_clockVar.getOrigin());
            system_mcgoutclk_clockVar.setStatus(new Status("No clock settings are applied", Severity.WARNING));
            system_mcgoutclk_clock_sourceVar.setValue("LIRC2");
            break;
        case ClockMode_HIRC_48M:
            mcg_c1_clksVar.setValue(0);
            mcg_c2_ircsVar.setLocked(false);
            system_mcgoutclk_clockVar.setValue(system_irc48m_clockVar.getValueAsLong());
            system_mcgoutclk_clockVar.setOrigin(system_irc48m_clockVar.getOrigin());
            system_mcgoutclk_clockVar.setStatus((Status) null);
            system_mcgoutclk_clock_sourceVar.setValue("HIRC 48M (IRCLK48MCLK)");
            break;
        case ClockMode_LIRC_2M:
            mcg_c1_clksVar.setValue(1);
            mcg_c2_ircsVar.setValue(0);
            mcg_c2_ircsVar.setLocked(true);
            system_mcgoutclk_clockVar.setValue(system_lirc_div1_clockVar.getValueAsLong());
            system_mcgoutclk_clockVar.setOrigin(system_lirc_div1_clockVar.getOrigin());
            system_mcgoutclk_clockVar.setStatus((Status) null);
            system_mcgoutclk_clock_sourceVar.setValue("LIRC2");
            break;
        case ClockMode_LIRC_8M:
            mcg_c1_clksVar.setValue(1);
            mcg_c2_ircsVar.setValue(1);
            mcg_c2_ircsVar.setLocked(true);
            system_mcgoutclk_clockVar.setValue(system_lirc_div1_clockVar.getValueAsLong());
            system_mcgoutclk_clockVar.setOrigin(system_lirc_div1_clockVar.getOrigin());
            system_mcgoutclk_clockVar.setStatus((Status) null);
            system_mcgoutclk_clock_sourceVar.setValue("LIRC8");
            break;
        case ClockMode_EXT:
            mcg_c1_clksVar.setValue(2);
            mcg_c2_ircsVar.setLocked(false);
            system_mcgoutclk_clockVar.setValue(osc_clockVar.getValueAsLong());
            system_mcgoutclk_clockVar.setOrigin(osc_clockVar.getOrigin());
            system_mcgoutclk_clockVar.setStatus((Status) null);
            system_mcgoutclk_clock_sourceVar.setValue("External Clock (OSCCLK)");
            break;
    }
    system_mcgoutclk_clock_sourceVar.setStatus(system_mcgoutclk_clockVar.getStatus());
    // ============================================
    if (mcg_mc_hircenVar.getValueAsBoolean() || (clock_mode == ClockMode.ClockMode_HIRC_48M)) {
        // HIRC Enabled
        system_mcgpclk_clockVar.setValue(system_irc48m_clockVar.getValueAsLong());
        system_mcgpclk_clockVar.enable(true);
        system_mcgpclk_clockVar.setStatus((Status) null);
    } else {
        // HIRC Disabled
        system_mcgpclk_clockVar.enable(false);
        system_mcgpclk_clockVar.setStatus(new Status("Disabled by mcg_mc_hircen", Severity.INFO));
    }
    // ========================================
    if (mcg_c1_irclkenVar.getValueAsBoolean() || (clock_mode == ClockMode.ClockMode_LIRC_2M) || (clock_mode == ClockMode.ClockMode_LIRC_8M)) {
        // LIRC Enabled
        mcg_c1_irefstenVar.enable(true);
        system_lirc_clockVar.enable(true);
        system_lirc_clockVar.setStatus((Status) null);
        if (mcg_c2_ircsVar.getValueAsBoolean()) {
            // Fast IRC selected
            system_lirc_clockVar.setValue(system_fast_irc_clockVar.getValueAsLong());
            system_lirc_clockVar.setOrigin(system_fast_irc_clockVar.getOrigin());
        } else {
            // Slow IRC selected
            system_lirc_clockVar.setValue(system_slow_irc_clockVar.getValueAsLong());
            system_lirc_clockVar.setOrigin(system_fast_irc_clockVar.getOrigin());
        }
        mcg_sc_fcrdivVar.enable(true);
        system_lirc_div1_clockVar.enable(true);
        mcg_mc_lirc_div2Var.enable(true);
        system_mcgirclk_clockVar.enable(true);
        system_mcgirclk_clockVar.setStatus((Status) null);
    } else {
        // LIRC Disabled
        mcg_c1_irefstenVar.enable(false);
        system_lirc_clockVar.enable(false);
        system_lirc_clockVar.setStatus(new Status("Disabled by mcg_c1_irclken", Severity.WARNING));
        mcg_sc_fcrdivVar.enable(false);
        system_lirc_div1_clockVar.enable(false);
        mcg_mc_lirc_div2Var.enable(false);
        system_mcgirclk_clockVar.enable(false);
        system_mcgirclk_clockVar.setStatus(new Status("Disabled by mcg_c1_irclken", Severity.WARNING));
    }
    long mcg_sc_fcrdiv = mcg_sc_fcrdivVar.getValueAsLong();
    system_lirc_div1_clockVar.setValue(system_lirc_clockVar.getValueAsLong() / (1 << mcg_sc_fcrdiv));
    system_lirc_div1_clockVar.setOrigin(system_lirc_clockVar.getOrigin() + "/LIRC_DIV1");
    long mcg_mc_lirc_div2 = mcg_mc_lirc_div2Var.getValueAsLong();
    system_mcgirclk_clockVar.setValue(system_lirc_div1_clockVar.getValueAsLong() / (1 << mcg_mc_lirc_div2));
    system_mcgirclk_clockVar.setOrigin(system_lirc_div1_clockVar.getOrigin() + "/LIRC_DIV2");
}
Also used : Status(net.sourceforge.usbdm.deviceEditor.model.Status) Variable(net.sourceforge.usbdm.deviceEditor.information.Variable)

Aggregations

Variable (net.sourceforge.usbdm.deviceEditor.information.Variable)21 LongVariable (net.sourceforge.usbdm.deviceEditor.information.LongVariable)20 BooleanVariable (net.sourceforge.usbdm.deviceEditor.information.BooleanVariable)17 ChoiceVariable (net.sourceforge.usbdm.deviceEditor.information.ChoiceVariable)17 StringVariable (net.sourceforge.usbdm.deviceEditor.information.StringVariable)11 DoubleVariable (net.sourceforge.usbdm.deviceEditor.information.DoubleVariable)10 BitmaskVariable (net.sourceforge.usbdm.deviceEditor.information.BitmaskVariable)7 IndexedCategoryVariable (net.sourceforge.usbdm.deviceEditor.information.IndexedCategoryVariable)7 IrqVariable (net.sourceforge.usbdm.deviceEditor.information.IrqVariable)7 NumericListVariable (net.sourceforge.usbdm.deviceEditor.information.NumericListVariable)7 PinListVariable (net.sourceforge.usbdm.deviceEditor.information.PinListVariable)7 Status (net.sourceforge.usbdm.deviceEditor.model.Status)7 FileNotFoundException (java.io.FileNotFoundException)6 UsbdmException (net.sourceforge.usbdm.jni.UsbdmException)6 VariableModel (net.sourceforge.usbdm.deviceEditor.model.VariableModel)2 Node (org.w3c.dom.Node)2 ArrayList (java.util.ArrayList)1 Pair (net.sourceforge.usbdm.deviceEditor.information.Variable.Pair)1 Severity (net.sourceforge.usbdm.deviceEditor.model.Status.Severity)1 ProjectAction (net.sourceforge.usbdm.packageParser.ProjectAction)1