% let Common   = system.getScript("/driverlib/Common.js");
% var nameOfModule = "cmpss";
%
//*****************************************************************************
//
// CMPSS Configurations
//
//*****************************************************************************
% let raData = system.resourceAllocation.data
void CMPSS_init(){
%if(Common.isContextCPU1()){
    CMPSS_MUX_config();
%}
% var module = system.modules['/driverlib/cmpss.js'];
% if (module != null) {
%   for(var i = 0; i < module.$instances.length; i++) 
%   {
%      var instance = module.$instances[i];
    `instance.$name`_init();
%   }
% }
}

% if (Common.isContextCPU1()){
void CMPSS_MUX_config(){
% if(Common.isAllocationOff()){
 % var contextNames = Common.getContextNames()
    % for (var cntx of contextNames)
    % {
        % module = Common.getModuleForCore('/driverlib/' + nameOfModule + '.js', cntx);                  
        %if (module != null)
        % {
        % for(var i = 0; i < module.$instances.length; i++) {
            % var instance = module.$instances[i];
            % var instanceNumber = instance.cmpssBase.match(/\d+/)[0]
    //
    // Select the value for CMP`instanceNumber`HPMXSEL.
    //
            % var tempPositiveInstance = "ASYSCTL_CMP"+instanceNumber+"_HP_"+instance.asysCMPHPMXSELValue
    ASysCtl_configCMPMux(`tempPositiveInstance`);
        %if (instance.highCompNegative != "CMPSS_INSRC_DAC"){
    //
    // Select the value for CMP`instanceNumber`HNMXSEL.
    //
            % var tempNegativeInstance = "ASYSCTL_CMP"+instanceNumber+"_HN_"+instance.asysCMPHNMXSELValue
    ASysCtl_configCMPMux(`tempNegativeInstance`);
        %}
    //
    // Select the value for CMP`instanceNumber`LPMXSEL.
    //
            % tempPositiveInstance = "ASYSCTL_CMP"+instanceNumber+"_LP_"+instance.asysCMPLPMXSELValue
    ASysCtl_configCMPMux(`tempPositiveInstance`);
        %if (instance.lowCompNegative != "CMPSS_INSRC_DAC"){
    //
    // Select the value for CMP`instanceNumber`LNMXSEL.
    //
            % tempNegativeInstance = "ASYSCTL_CMP"+instanceNumber+"_LN_"+instance.asysCMPLNMXSELValue
    ASysCtl_configCMPMux(`tempNegativeInstance`);
        %}
        %}
        % }
    % }
% }
% else {
% for (let context of raData.contexts)
% {
	% let module = context.modules.find(x => x.moduleName === '/driverlib/' + nameOfModule + '.js')              
    %if (module != null)
    % {
        % for(var i = 0; i < module.instances.length; i++) {
            % var instance = module.instances[i];
            % var instanceNumber = instance.requiredArgs.cmpssBase.match(/\d+/)[0]
    
    //
    // Select the value for CMP`instanceNumber`HPMXSEL.
    //
            % var tempPositiveInstance = "ASYSCTL_CMP"+instanceNumber+"_HP_"+instance.requiredArgs.asysCMPHPMXSELValue
    ASysCtl_configCMPMux(`tempPositiveInstance`);
        %if (instance.highCompNegative != "CMPSS_INSRC_DAC"){
    //
    // Select the value for CMP`instanceNumber`HNMXSEL.
    //
            % var tempNegativeInstance = "ASYSCTL_CMP"+instanceNumber+"_HN_"+instance.requiredArgs.asysCMPHNMXSELValue
    ASysCtl_configCMPMux(`tempNegativeInstance`);
        %}
    //
    // Select the value for CMP`instanceNumber`LPMXSEL.
    //
            % tempPositiveInstance = "ASYSCTL_CMP"+instanceNumber+"_LP_"+instance.requiredArgs.asysCMPLPMXSELValue
    ASysCtl_configCMPMux(`tempPositiveInstance`);
        %if (instance.lowCompNegative != "CMPSS_INSRC_DAC"){
    //
    // Select the value for CMP`instanceNumber`LNMXSEL.
    //
            % tempNegativeInstance = "ASYSCTL_CMP"+instanceNumber+"_LN_"+instance.requiredArgs.asysCMPLNMXSELValue
    ASysCtl_configCMPMux(`tempNegativeInstance`);
        %}
        %}
        % }
    % }
% }
}
% }
%
% var module = system.modules['/driverlib/cmpss.js'];                 
%if (module != null)
% {
        % for(var i = 0; i < module.$instances.length; i++) {
            % var instance = module.$instances[i];
            % var instanceNumber = instance.cmpssBase.match(/\d+/)[0]

void `instance.$name`_init(){
        % var baseName = instance.$name + "_BASE";
%%{
    var configHighString = "("
    configHighString += instance.highCompNegative
    if (instance.highCompInvert){
        configHighString += " | CMPSS_INV_INVERTED"
    }
    if (instance.highCompAsynch){
        configHighString += " | CMPSS_OR_ASYNC_OUT_W_FILT"
    }
    configHighString += ")"
%%}
    //
    // Sets the configuration for the high comparator.
    //
    CMPSS_configHighComparator(`baseName`,`configHighString`);
%%{
    var configLowString = "("
    configLowString += instance.lowCompNegative
    if (instance.lowCompInvert){
        configLowString += " | CMPSS_INV_INVERTED"
    }
    if (instance.lowCompAsynch){
        configLowString += " | CMPSS_OR_ASYNC_OUT_W_FILT"
    }
    configLowString += ")"
%%}
    //
    // Sets the configuration for the low comparator.
    //
    CMPSS_configLowComparator(`baseName`,`configLowString`);
    //
    // Sets the configuration for the internal comparator DACs.
    //
        % if(instance.dacValLoad == "CMPSS_DACVAL_PWMSYNC"){
    //
    //  - ePWM module must be configured before using here.
    //
        % }

    CMPSS_configDACHigh(`baseName`,(`instance.dacValLoad` | `instance.dacRefVoltage` | `instance.dacValSource`));

    CMPSS_configDACLow(`baseName`, `instance.lowDacValSource`);

    //
    // Sets the value of the internal DAC of the high comparator.
    //
    CMPSS_setDACValueHigh(`baseName`,`instance.dacValHigh`U);
    //
    // Sets the value of the internal DAC of the low comparator.
    //
    CMPSS_setDACValueLow(`baseName`,`instance.dacValLow`U);
    % if((instance.deEnable == true)){
    //
    // Enable Diode Emulation Support for CMPSS
    //
    CMPSS_enableDEmode(`baseName`);
    %}
    //
    // Sets the value of the internal DAC of the high comparator for Diode Emulation Support.
    //
    CMPSS_configHighDACShadowValueDE(`baseName`, `instance.deDACValHigh`U);
    //
    // Sets the value of the internal DAC of the low comparator for Diode Emulation Support.
    //
    CMPSS_configLowDACShadowValueDE(`baseName`, `instance.deDACValLow`U);
    //
    // Set the DEACTIVE signal source for CMPSS
    //
    CMPSS_selectDEACTIVESource(`baseName`, `instance.deactiveSel`);
    //
    //  Configures the digital filter of the high comparator.
    //
    CMPSS_configFilterHigh(`baseName`, `instance.samplePrescaleHigh`U, `instance.sampleWindowHigh`U, `instance.thresholdHigh`U);
    //
    // Configures the digital filter of the low comparator.
    //
    CMPSS_configFilterLow(`baseName`, `instance.samplePrescaleLow`U, `instance.sampleWindowLow`U, `instance.thresholdLow`U);
        % if(instance.initFilterHigh){
    //
    // Initializes the digital filter of the high comparator.
    //
    CMPSS_initFilterHigh(`baseName`);
        % }
        % if(instance.initFilterLow){
    //
    // Initializes the digital filter of the low comparator.
    //
    CMPSS_initFilterLow(`baseName`);
        % }
    //
    // Sets the output signal configuration for the high comparator.
    //
    CMPSS_configOutputsHigh(`baseName`,(`instance.highCTRIPOUT` | `instance.highCTRIP`));
    //
    // Sets the output signal configuration for the low comparator.
    //
    CMPSS_configOutputsLow(`baseName`,(`instance.lowCTRIPOUT` | `instance.lowCTRIP`));
    //
    // Sets the comparator hysteresis settings.
    //
    CMPSS_setHysteresis(`baseName`,`instance.hysteresisVal`U);
    //
    // Configures the comparator subsystem's high ramp generator.
    //
    CMPSS_configRampHigh(`baseName`, `instance.rampHighDir`, `instance.maxRampVal`U,`instance.ramDecVal`U,`instance.rampDelayVal`U,`instance.pwmSyncSrc`U,`instance.useRampValShdw`);
    //
    // Configures the comparator subsystem's low ramp generator.
    //
    CMPSS_configRampLow(`baseName`, `instance.rampLowDir`, `instance.maxRampValLow`U,`instance.ramDecValLow`U,`instance.rampDelayValLow`U,`instance.pwmSyncSrcLow`U,`instance.useRampValShdwLow`);

    //
    // Configures the high comparator's ramp generator clock divider
    //
    CMPSS_setRampClockDividerHigh(`baseName`, `instance.rampClkDiv`);
    //
    // Configures the low comparator's ramp generator clock divider
    //
    CMPSS_setRampClockDividerLow(`baseName`, `instance.rampClkDivLow`);

    % if(instance.latchResetHigh){
    //
    // Enables reset of HIGH comparator digital filter output latch on PWMSYNC
    //
    CMPSS_enableLatchResetOnPWMSYNCHigh(`baseName`);
        % }
        % else{
    //
    // Disables reset of HIGH comparator digital filter output latch on PWMSYNC
    //
    CMPSS_disableLatchResetOnPWMSYNCHigh(`baseName`);
        % }
        % if(instance.latchResetLow){
    //
    // Enables reset of LOW comparator digital filter output latch on PWMSYNC
    //
    CMPSS_enableLatchResetOnPWMSYNCLow(`baseName`);
        % }
        % else{
    //
    // Disables reset of LOW comparator digital filter output latch on PWMSYNC
    //
    CMPSS_disableLatchResetOnPWMSYNCLow(`baseName`);
        % }
    //
    // Sets the ePWM module blanking signal that holds trip in reset.
    //
    CMPSS_configBlanking(`baseName`,`instance.configBlanking`U);
            % if(instance.enableBlanking){
    //
    // Enables an ePWM blanking signal to hold trip in reset.
    //
    CMPSS_enableBlanking(`baseName`);
            % }
            % else{
    //
    // Disables an ePWM blanking signal from holding trip in reset.
    //
    CMPSS_disableBlanking(`baseName`);
            % }
    //
    // Configures whether or not the digital filter latches are reset by PWMSYNC
    //
    CMPSS_configLatchOnPWMSYNC(`baseName`,`instance.configLatchHigh`,`instance.configLatchLow`);
        % if(instance.enableModule){
    //
    // Enables the CMPSS module.
    //
    CMPSS_enableModule(`baseName`);
        % }
        % else{
    //
    // Disables the CMPSS module.
    //
    CMPSS_disableModule(`baseName`);

        % }
    //
    // Delay for CMPSS DAC to power up.
    //
    DEVICE_DELAY_US(500);
        % if(instance.clearFilterLatchHigh){
    //
    // Causes a software reset of the high comparator digital filter output latch.
    //
    CMPSS_clearFilterLatchHigh(`baseName`);
    % }
        % if(instance.clearFilterLatchLow){
    //
    // Causes a software reset of the low comparator digital filter output latch.
    //
    CMPSS_clearFilterLatchLow(`baseName`);
    % }
}
    % }
% }