Attribute VB_Name = "Module1"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\include\Port.inc"
'#Uses "ASCInclude.inc"
'#Uses "SSCInclude.inc"
'#Uses "IICInclude.inc"
'#Uses "IISInclude.inc"

Option Explicit
'*****************************************************************************
' Module:
'        USIC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'USIC'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author                    Date            Description
'        ---------------------------------------------------------------------
'        1.0  JaiSankar & Raja          16.04.07        XC22xx v0.01
'
'
'*****************************************************************************

' use this constant in the Format statement for all calculated outputs like
' baud rates or deviation
Global Const USIC_PRECISION = "0.000"


'*****************************************************************************
'Description:
'        For each function which should be used for this module a line
'        "If (Not func.AddFunction .." must be insert in this function.
'        For each parameter that should be used for this function a
'        line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added function a project variable with the name
'        "ASC_NAME_Functionname" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshUSICFunctionDescriptions(vIndex As Variant, func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sVarPre As String
  
  sVarPre = Format$(vIndex)

  RefreshUSICFunctionDescriptions = False

  '------------------------------------------------------------------------------------------------
  ' Description for USIC0_Init          [x = 0 or 1 or 2]
  '------------------------------------------------------------------------------------------------
  sDescription = "This function initializes USIC0 Module"
  sReturnValue = "None"

  If (Not func.AddFunction("USIC0_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for USIC1_Init          [x = 0 or 1 or 2]
  '------------------------------------------------------------------------------------------------
  sDescription = "This function initializes USIC1 Module"
  sReturnValue = "None"

  If (Not func.AddFunction("USIC1_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for USIC2_Init          [x = 0 or 1 or 2]
  '------------------------------------------------------------------------------------------------
  sDescription = "This function initializes USIC2 Module"
  sReturnValue = "None"

  If (Not func.AddFunction("USIC2_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  
  If (Not func.Sort) Then Exit Function

  RefreshUSICFunctionDescriptions = True
End Function


'*****************************************************************************
'Description:
'        For each parameter that should be used as a constant (on page "Parameters")
'        a line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added parameter a project variable with the name
'        "DUMMY_PARA_Parametername" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        vIndex           parameter of the module which was added with AddModule
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshParameterDescriptions(vIndex As Variant, para As ParameterClass, r As ProjectData) As Boolean
  On Error Resume Next

  RefreshParameterDescriptions = False

  Dim sDescription As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim sParaName As String
  Dim i As Integer

  sVarPre = Format$(vIndex)
  sRegPre = Format$(vIndex) & "_"


  ' for each parameter of this module the property AddParameter must be called
  '
  ' AddParameter(sParameterName As String, sParameterDescription As String) As Boolean
  '
  ' sParameterName         | String | parameter name as key (use the name of the variable
  '                                   for the parameter name e.g. I2C_PARA_MODE)
  ' sParameterDescription  | String | description of the parameter

  '------------------------------------------------------------------------------------------------
  ' Description for I2C_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies " & sVarPre & " IIC 7-bit addressing mode"
  sParaName = "ADR7"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  
  '------------------------------------------------------------------------------------------------
  ' Description for I2C_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies " & sVarPre & " IIC 10-bit addressing mode"
  sParaName = "ADR10"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
    
  If (Not para.Sort) Then Exit Function

  RefreshParameterDescriptions = True
End Function

'*****************************************************************************
'Description:
'        This function checks if the module clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            the module clock is disabled
'        False           else
'*****************************************************************************
Function IsModuleClockDisabled(r As ProjectData, vIndex As Variant) As Boolean
  On Error Resume Next
    
  Dim sIndex As String
  Dim sPrefix As String

  sIndex = vIndex
  sPrefix = sIndex & "_"
  
  IsModuleClockDisabled = IsModuleDisabled(r, vIndex)
  
End Function


'*******************************************************************************
'Description:
'        This function check  the KSCFG register, whether the module is disabled
'-------------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        vIndex          module index
'-------------------------------------------------------------------------------
'Returns:
'        True  -  if the modul is disabled
'        False -  else
'
'*******************************************************************************
Function IsModuleDisabled(r As ProjectData, vIndex As Variant) As Boolean

  IsModuleDisabled = False

  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = vIndex
  sRegPre = vIndex & "_"
    
  If Not (GetUSICDisebleBitName(r, vIndex) = "") Then
      If (r.v(sRegPre & "KSCFG_" & GetUSICDisebleBitName(r, vIndex)) = 1) Then
        IsModuleDisabled = False
      Else
        IsModuleDisabled = True
      End If
  End If

End Function


'************************************************************************************
'Description:
'        This function returns the disable bit name of SYSCON3 register (e.g SSCDIS).
'------------------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          module index
'
'------------------------------------------------------------------------------------
'Returns:
'       disable bit name
'************************************************************************************
Function GetUSICDisebleBitName(r As ProjectData, vIndex As Variant) As String
    On Error Resume Next
    
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = vIndex
    sRegPre = vIndex & "_"

    If (r.Regs.BitFind(sRegPre & "KSCFG_MODEN") = True) Then
        GetUSICDisebleBitName = "MODEN"
    Else
        GetUSICDisebleBitName = ""
    End If

End Function


'*****************************************************************************
'Description:
'        This function checks if enough port pins selected for the module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            not enough port pins selected
'        False           else
'*****************************************************************************
Function IsPinInterfaceSelected(r As ProjectData, vIndex As Variant) As Boolean
  On Error Resume Next
  
  Dim sVarPre As String

  sVarPre = vIndex
  
  IsPinInterfaceSelected = False

  Select Case (r.v(sVarPre & "_Proto"))
    Case "ASC"
      IsPinInterfaceSelected = (r.v(sVarPre & "_ASC_cboRxD") <> "None" Or r.v(sVarPre & "_ASC_cboTxD") <> "None")
    
    Case "SSC"
        IsPinInterfaceSelected = (r.v(sVarPre & "_SSC_cboClk") <> "None") And _
                                 (r.v(sVarPre & "_SSC_cboRxD") <> "None" Or r.v(sVarPre & "_SSC_cboTxD") <> "None")
    Case "IIC"
      IsPinInterfaceSelected = (r.v(sVarPre & "_IIC_cboSDA") <> "None" And r.v(sVarPre & "_IIC_cboSCL") <> "None")
    
    Case "IIS"
        IsPinInterfaceSelected = ((r.v(sVarPre & "_IIS_cboDOUT") <> "None" Or r.v(sVarPre & "_IIS_cboDIN") <> "None") And r.v(sVarPre & "_IIS_cboSCLKOUT") <> "None" And r.v(sVarPre & "_IIS_cboWAOUT") <> "None")
    Case Else
  End Select

End Function


'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          module index
'-----------------------------------------------------------------------------
'Returns:
'        disabled tooltip
'*****************************************************************************
Function GetTooltipModuleDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  
  GetTooltipModuleDisabled = ""
  GetTooltipModuleDisabled = GetTooltipModuleDisabled & _
                              FormatDisabledToolTip("the module is disabled")
End Function


'*****************************************************************************
'Description:
'          This function returns input frequency of BuadRate Generator (fPIN)
'
'-----------------------------------------------------------------------------
'Parameters:
'      r                reference to the Registers object
'      vIndex           As Variant
'-----------------------------------------------------------------------------
'Returns:
'      fPIN             input frequency of BuadRate Generator
'*****************************************************************************
Function GetfPIN(r As ProjectData, vIndex As Variant, Proto As String) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = Format$(vIndex)
  sRegPre = Format$(vIndex) & "_"
  
  GetfPIN = r.v(sVarPre & "_" & Proto & "_Baudrate") * _
            (r.v(sRegPre & "BRGH_PDIV") + 1) * _
            (r.v(sRegPre & "BRGL_PCTQ") + 1) * _
            (r.v(sRegPre & "BRGL_DCTQ") + 1)

End Function


'*****************************************************************************
'Description:
'          This function returns input frequency of BuadRate Generator (fPIN)
'
'-----------------------------------------------------------------------------
'Parameters:
'      r                reference to the Registers object
'      vIndex           As Variant
'-----------------------------------------------------------------------------
'Returns:
'      fPIN             input frequency of BuadRate Generator
'*****************************************************************************
Function IISGetfPIN(r As ProjectData, vIndex As Variant, Proto As String) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = Format$(vIndex)
  sRegPre = Format$(vIndex) & "_"
  
   IISGetfPIN = r.v(sVarPre & "_" & Proto & "_Baudrate") * _
                (r.v(sRegPre & "BRGH_PDIV") + 1)
            

End Function

'*****************************************************************************
'Description:
'        This function Resets UxCy module's Fifo Configuration.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ResetFifo(r As ProjectData, vIndex As Variant)
  On Error Resume Next

  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = vIndex
  sRegPre = vIndex & "_"

    'FIFO sfrs
    r.sfr(sRegPre & "TBCTRL") = &H0
    r.sfr(sRegPre & "TBCTRH") = &H0
    r.sfr(sRegPre & "RBCTRL") = &H0
    r.sfr(sRegPre & "RBCTRH") = &H0
    
    'FIFO functions
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "FillTxFIFO") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "GetRxFIFOData") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "IsRxFIFObusy") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "IsTxFIFObusy") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "IsRxFIFOfull") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "IsTxFIFOfull") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "IsRxFIFOempty") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "IsTxFIFOempty") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "GetRxFIFOFillingLevel") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "GetTxFIFOFillingLevel") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "FlushRxFIFO") = 0
    r.v(sVarPre & "_USE_" & r.v(sVarPre & "_Proto") & "FlushTxFIFO") = 0
  
End Sub


'*****************************************************************************
'Description:
'        This function sets the UxCy module to a defined state when it is
'        disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableUxCyModule(r As ProjectData, vIndex As Variant)
  On Error Resume Next

  Dim sVarPre As String
  Dim sRegPre As String
  Dim sCC2SRN As String
  Dim sCC2Chxx As String

  sVarPre = vIndex
  sRegPre = vIndex & "_"

    'Module Disable
    r.v(sRegPre & "KSCFG_MODEN") = 0
    r.v(sRegPre & "KSCFG_BPMODEN") = 0
    
    'BaudRate
    r.sfr(sRegPre & "FDRL") = &H0
    r.sfr(sRegPre & "BRGL") = &H0
    r.sfr(sRegPre & "BRGH") = &H0
    
    'Reset Fifo Controls & Interrupts
    Call ResetFifo(r, vIndex)

  If (IsModuleDisabled(r, vIndex)) Then
    Select Case (r.v(sVarPre & "_Proto"))
        Case "ASC"
            'Reset Operation Mode
            r.v(sVarPre & "_ASC_OptOperMode") = 0
            'Half-Duplex external connection type
            r.v(sVarPre & "_ASC_optHDCT") = 0
            'Reset StopBits
            r.v(sRegPre & "PCRL_CTR1") = 0
            'Reset Parity
            r.v(sRegPre & "CCR_PM") = 0
            'AB combination mode - Trigger is disabled
            r.v(sRegPre & "DX1CR_CM") = 0
            'Irda Settings
            'Reset Inverted shift data value
            r.v(sRegPre & "SCTRL_DOCFG") = 0
            'Reset Data polartiy
            r.v(sRegPre & "DX0CR_DPOL") = 0
            'Sampling Point
            r.v(sVarPre & "_ASC_cboSp") = 0
            r.v(sRegPre & "PCRL_CTR8") = 0
            r.v(sRegPre & "PCRL_CTR9") = 0
            r.v(sRegPre & "PCRL_CTR10") = 0
            r.v(sRegPre & "PCRL_CTR11") = 0
            r.v(sRegPre & "PCRL_CTR12") = 0
            'Pulse length
            r.v(sVarPre & "_ASC_cboPl") = 0
            r.v(sRegPre & "PCRL_CTR13") = 0
            r.v(sRegPre & "PCRL_CTR14") = 0
            r.v(sRegPre & "PCRL_CTR15") = 0
            
            'Reset IO Pins (Resets Alternate Vars & Port Regs)
            Call ReleaseASCTxDPin(r, vIndex)
            Call ReleaseASCRxDPin(r, vIndex)
            'Reset Clk pin (Clk pin Used in AutoBaud)
            Call ReleaseASCClkPin(r, vIndex)
            
            'Reset Interrupts
            'Data Transfer Interrupt Enables
            r.v(sRegPre & "CCR_AIEN") = 0
            r.v(sRegPre & "CCR_RIEN") = 0
            r.v(sRegPre & "CCR_RSIEN") = 0
            r.v(sRegPre & "CCR_TBIEN") = 0
            r.v(sRegPre & "CCR_TSIEN") = 0
            r.v(sRegPre & "CCR_DLIEN") = 0
            
            'Data Transfer Interrupt node pointers
            r.v(sRegPre & "INPRL_AINP") = 0
            r.v(sRegPre & "INPRL_RINP") = 0
            r.v(sRegPre & "INPRL_TBINP") = 0
            r.v(sRegPre & "INPRL_TSINP") = 0
            r.v(sRegPre & "INPRH_PINP") = 0
            
            'DisableSRN
            Call DisableSRN(r, sVarPre, sRegPre & "0IC", r.v(sVarPre & "_Proto") & "0IC")
            Call DisableSRN(r, sVarPre, sRegPre & "1IC", r.v(sVarPre & "_Proto") & "1IC")
            Call DisableSRN(r, sVarPre, sRegPre & "2IC", r.v(sVarPre & "_Proto") & "2IC")
            sCC2SRN = GetCC2SRN(sVarPre)        'CC2 xx channel's SRN for UxCy
            sCC2Chxx = GetCC2Chxx(sVarPre)      'CC2 xx channel for UxCy
            Call DisableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
            
            'Protocol interrupt variable
            r.v(sRegPre & "ASC_ProtoInt") = 0
            
            'ASC Protocol interrupts enables
            r.v(sRegPre & "PCRL_CTR4") = 0
            r.v(sRegPre & "PCRL_CTR5") = 0
            r.v(sRegPre & "PCRL_CTR6") = 0
            r.v(sRegPre & "PCRL_CTR7") = 0

            'Reset Function (Vars)
            r.v(sVarPre & "_USE_Init") = 0
            r.v(sVarPre & "_USE_ASCSendData") = 0
            r.v(sVarPre & "_USE_ASCGetData") = 0
            r.v(sVarPre & "_USE_ASCTxBufFree") = 0
            r.v(sVarPre & "_USE_ASCRxDataReady") = 0
            r.v(sVarPre & "_USE_ASCTxDataReady") = 0
                                    
            'Reset ISR Vars
            r.v(sVarPre & "_USE_ASC0IC") = 0
            r.v(sVarPre & "_USE_ASC1IC") = 0
            r.v(sVarPre & "_USE_ASC2IC") = 0
            r.v(sVarPre & "_USE_ASC3IC") = 0
            
            'Reset Fifo Function Vars
            r.v(sVarPre & "_USE_ASCFillTxFIFO") = 0
            r.v(sVarPre & "_USE_ASCGetRxFIFOData") = 0
            r.v(sVarPre & "_USE_ASCIsRxFIFObusy") = 0
            r.v(sVarPre & "_USE_ASCIsTxFIFObusy") = 0
            r.v(sVarPre & "_USE_ASCIsRxFIFOfull") = 0
            r.v(sVarPre & "_USE_ASCIsTxFIFOfull") = 0
            r.v(sVarPre & "_USE_ASCIsRxFIFOempty") = 0
            r.v(sVarPre & "_USE_ASCIsTxFIFOempty") = 0
            r.v(sVarPre & "_USE_ASCGetRxFIFOFillingLevel") = 0
            r.v(sVarPre & "_USE_ASCGetTxFIFOFillingLevel") = 0
            r.v(sVarPre & "_USE_ASCFlushRxFIFO") = 0
            r.v(sVarPre & "_USE_ASCFlushTxFIFO") = 0

            'Reset Regs - '"Important": This is a Default setting & this Condition is valid only for DAvE Lite Version
            r.v(sRegPre & "SCTRL_TRM") = 0
            r.v(sRegPre & "SCTRL_PDL") = 0
            r.v(sRegPre & "SCTRH_WLE") = 0
            r.v(sRegPre & "SCTRH_FLE") = 0
            r.v(sRegPre & "TCSRL_TDEN") = 0
            r.v(sRegPre & "TCSRL_TDSSM") = 0
            r.v(sRegPre & "PCRL_CTR0") = 0
            r.v(sRegPre & "PSR_TBIF") = 0

        Case "SSC"
            'Reset Operation Mode
            r.v(sVarPre & "_SSC_OptOperMode") = 0

            'Reset Interrupts
            
            'r.v(sRegPre & "CCR_AIEN") = 0
            r.v(sRegPre & "CCR_RIEN") = 0
            r.v(sRegPre & "CCR_RSIEN") = 0
            r.v(sRegPre & "CCR_TBIEN") = 0
            r.v(sRegPre & "CCR_TSIEN") = 0
            r.v(sRegPre & "CCR_DLIEN") = 0
            
            'Data Transfer Interrupt node pointers
            'r.v(sRegPre & "INPRL_AINP") = 0
            r.v(sRegPre & "INPRL_RINP") = 0
            r.v(sRegPre & "INPRL_TBINP") = 0
            r.v(sRegPre & "INPRL_TSINP") = 0
            r.v(sRegPre & "INPRH_PINP") = 0
            
            
            'Protocol interrupt variable
            r.v(sRegPre & "SSC_ProtoInt") = 0
            
            'SSC Protocol interrupts enables
            r.v(sRegPre & "PCRL_CTR14") = 0
            r.v(sRegPre & "PCRL_CTR15") = 0
            

            'Reset IO Pins (Resets Alternate Vars & Port Regs)
            Call ReleaseTxDPin(r, vIndex)
            Call ReleaseRxDPin(r, vIndex)
            Call ReleaseClkPin(r, vIndex)
            Call ReleaseSlSPin(r, vIndex)
            Call ReleaseSlS0Pin(r, vIndex)
            Call ReleaseSlS1Pin(r, vIndex)
            
            'Default Word length
            r.v(sRegPre & "SCTRH_WLE") = 7
'            r.v(sVarPre & "_SSC_WordLength") = "8"
'            Call SetWordLength(r, vIndex, r.v(sRegPre & "SSC_WordLength"))
'
            'default frame length
            r.v(sRegPre & "SCTRH_FLE") = 7
'            r.v(sVarPre & "_UD04") = "8" 'Uxcy_SSC_FrameLength is mapped to UxCy_UD04
'            Call SetFrameLength(r, vIndex, r.v(sRegPre & "UD04"))
'
            'Default polarity  and phase
            r.v(sRegPre & "BRGH_SCLKCFG") = 0
            
            'default slave select inversion
            r.v(sRegPre & "PCRL_CTR2") = 1
            'r.v(sVarPre & "_UD02") = 1
            
            'default slave select input polarity
            r.v(sRegPre & "DX2CR_DPOL") = 1

            'Reset Functions (Vars)
            r.v(sVarPre & "_USE_Init") = 0
            r.v(sVarPre & "_USE_SSCSendData") = 0
            r.v(sVarPre & "_USE_SSCGetData") = 0
            r.v(sVarPre & "_USE_SSCTxBuffer") = 0
            'r.v(sVarPre & "_USE_SSCRx") = 0
            r.v(sVarPre & "_USE_SSCTxBufFree") = 0
            r.v(sVarPre & "_USE_SSCRxDataReady") = 0
                        
            'Reset Regs - '"Important": This is a Default setting & this Condition is valid only for DAvE Lite Version
            r.v(sRegPre & "SCTRL_TRM") = 0
            r.v(sRegPre & "SCTRL_PDL") = 0
            r.v(sRegPre & "SCTRH_WLE") = 0
            r.v(sRegPre & "SCTRH_FLE") = 0
            
            r.v(sRegPre & "TCSRL_TDEN") = 0
            r.v(sRegPre & "TCSRL_TDSSM") = 0

            r.v(sRegPre & "PCRL_CTR0") = 0
            r.v(sRegPre & "PCRL_CTR1") = 0
            r.v(sRegPre & "PCRL_CTR6") = 0
            r.v(sRegPre & "PCRL_CTR8") = 0
            r.v(sRegPre & "PCRL_CTR9") = 0
            r.v(sRegPre & "PCRL_CTR10") = 0
            r.v(sRegPre & "PCRL_CTR11") = 0

            r.v(sRegPre & "PCRH_CTR16") = 0

            r.v(sRegPre & "DX0CR_INSW") = 0
            r.v(sRegPre & "DX1CR_INSW") = 0
            r.v(sRegPre & "DX2CR_INSW") = 0
            'new
             r.v(sRegPre & "DX0CR_DSEL") = 0
            
            'New
            'r.v(sRegPre & "PCRL_CTR2") = 0
            
            'disable protocol specific interrupt
            r.v(sRegPre & "PCRL_CTR14") = 0
            r.v(sRegPre & "PCRL_CTR15") = 0
            
            'disable srn
            Call DisableSRN(r, sVarPre, sRegPre & "0IC", r.v(sVarPre & "_Proto") & "0IC")
            Call DisableSRN(r, sVarPre, sRegPre & "1IC", r.v(sVarPre & "_Proto") & "1IC")
            Call DisableSRN(r, sVarPre, sRegPre & "2IC", r.v(sVarPre & "_Proto") & "2IC")
            sCC2SRN = GetCC2SRN(sVarPre)        'CC2 xx channel's SRN for UxCy
            sCC2Chxx = GetCC2Chxx(sVarPre)      'CC2 xx channel for UxCy
            Call DisableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
            
                                        
        Case "IIC"
            'Reset IO Pins (Resets Alternate Vars & Port Regs)
            Call ReleaseSCLPin(r, vIndex)
            Call ReleaseSDAPin(r, vIndex)
            
            'Disable Module
            Call DisableModule(r, vIndex)
            
    Case "IIS"
            '***************************************************************
            
             r.v(sRegPre & "IIS_OMOD") = 0
            r.v(sRegPre & "IIS_BaudIndex") = "32"
            r.v(sRegPre & "IIS_Baudrate") = 0
            r.v(sRegPre & "IIS_WordLength") = "8"
            
            r.v(sRegPre & "FDRL_STEP") = 0
            r.v(sRegPre & "BRGH_PDIV") = 0
            
            r.v(sRegPre & "SCTRL_SDIR") = 0
            r.v(sRegPre & "SCTRL_PDL") = 0
            r.v(sRegPre & "SCTRL_TRM") = 0
            r.v(sRegPre & "SCTRH_WLE") = 0
            r.v(sRegPre & "SCTRH_FLE") = 0
            r.v(sRegPre & "TCSRL_TDEN") = 0
            r.v(sRegPre & "TCSRL_TDSSM") = 0
            
            'r.sfr(sRegPre & "CCR") = &H0
            r.sfr(sRegPre & "PCRL") = &H0
            r.sfr(sRegPre & "PCRH") = &H0
            r.sfr(sRegPre & "SCTRL") = &H0
            r.sfr(sRegPre & "SCTRH") = &H0
            r.sfr(sRegPre & "TCSRL") = &H0
            r.sfr(sRegPre & "FDRL") = &H0
            r.sfr(sRegPre & "BRGL") = &H0
            r.sfr(sRegPre & "BRGH") = &H0
            
            
            
            'Reset IO Pins (Resets Alternate Vars & Port Regs)
            Call ReleaseDOUTPin(r, vIndex)
            Call ReleaseDINPin(r, vIndex)
            Call ReleaseWAOUTPin(r, vIndex)
            Call ReleaseSCLKOUTPin(r, vIndex)
            Call ReleaseWAINPin(r, vIndex)
            Call ReleaseSCLKINPin(r, vIndex)
            
            'Disable Module
            Call DisableModule(r, vIndex)
           '*********************************************************************
    End Select
  End If
    
End Sub


'*****************************************************************************
'Description:
'        This function sets the UxCy module to a defined state when it is
'        disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableModule(r As ProjectData, vIndex As String)
  On Error Resume Next

  Dim sVarPre As String
  Dim sRegPre As String
  Dim sCC2SRN As String
  Dim sCC2Chxx As String
  Dim sCCR   As String
  Dim sINPRL As String      'Interrupt Node Pointer Register L
  Dim sINPRH As String      'Interrupt Node Pointer Register H
  
  sVarPre = vIndex
  sRegPre = vIndex & "_"
  sCCR = sRegPre & "CCR_"
  sINPRL = sRegPre & "INPRL_"
  sINPRH = sRegPre & "INPRH_"
  
    Select Case (r.v(sVarPre & "_Proto"))
        Case "ASC"
            'Reset Parity & StopBits
            r.v(sRegPre & "CCR_PM") = 0
            r.v(sRegPre & "PCRL_CTR1") = 0
            'AB combination mode - Trigger is disabled
            r.v(sRegPre & "DX1CR_CM") = 0
            'Irda Settings
            'Inverted shift data value
            r.v(sRegPre & "SCTRL_DOCFG") = 0
            'Reset Data polartiy
            r.v(sRegPre & "DX0CR_DPOL") = 0
            
            'Reset Fifo Controls & Interrupts
            Call ResetFifo(r, vIndex)
            
            If r.v(sVarPre & "_ASC_cboRxD") = "None" Then
                'Reset Rx Data Transfer Interrupts
                r.v(sRegPre & "CCR_AIEN") = 0
                r.v(sRegPre & "CCR_RIEN") = 0
                r.v(sRegPre & "CCR_RSIEN") = 0
                r.v(sRegPre & "CCR_DLIEN") = 0
                'Reset Rx Protocol Interrupts
                r.v(sRegPre & "PCRL_CTR5") = 0      'Rx Noise detection
                r.v(sRegPre & "PCRL_CTR6") = 0      'Format error
                'Sampling Point
                r.v(sVarPre & "_ASC_cboSp") = 7
                r.v(sRegPre & "PCRL_CTR8") = 1
                r.v(sRegPre & "PCRL_CTR9") = 1
                r.v(sRegPre & "PCRL_CTR10") = 1
                r.v(sRegPre & "PCRL_CTR11") = 0
                r.v(sRegPre & "PCRL_CTR12") = 0
            
                'DisableSRN (Rx) - Data Transfer Interrupts
                Call EnableOrDisableSRN(r, vIndex, sCCR & "AIEN", sINPRL & "AINP")
                Call EnableOrDisableSRN(r, vIndex, sCCR & "RIEN", sINPRL & "RINP")
                Call EnableOrDisableSRN(r, vIndex, sCCR & "RSIEN", sINPRL & "TBINP")
                Call EnableOrDisableSRN(r, vIndex, sCCR & "DLIEN", sINPRH & "PINP")
                'DisableSRN (Rx) - Protocol Interrupts
                r.v(sRegPre & "PCRL_CTR5") = 0
'                If (r.v(sRegPre & "PCRL_CTR5")) Then
'                End If
                
                'Reset Rx Functions
                r.v(sVarPre & "_USE_ASCGetData") = 0
                r.v(sVarPre & "_USE_ASCRxDataReady") = 0
                'Reset Rx fifo Functions
                r.v(sVarPre & "_USE_ASCGetRxFIFOData") = 0
                r.v(sVarPre & "_USE_ASCIsRxFIFObusy") = 0
                r.v(sVarPre & "_USE_ASCIsRxFIFOfull") = 0
                r.v(sVarPre & "_USE_ASCIsRxFIFOempty") = 0
                r.v(sVarPre & "_USE_ASCGetRxFIFOFillingLevel") = 0
                r.v(sVarPre & "_USE_ASCFlushRxFIFO") = 0
      
            End If
            
            If r.v(sVarPre & "_ASC_cboTxD") = "None" Then
                'Reset Tx Data Transfer Interrupts
                r.v(sRegPre & "CCR_TBIEN") = 0
                r.v(sRegPre & "CCR_TSIEN") = 0
                'Reset Tx Protocol Interrupts
                r.v(sRegPre & "PCRL_CTR4") = 0      'Collision Detection
                
                'Pulse length
                r.v(sVarPre & "_ASC_cboPl") = 0
                r.v(sRegPre & "PCRL_CTR13") = 0
                r.v(sRegPre & "PCRL_CTR14") = 0
                r.v(sRegPre & "PCRL_CTR15") = 0
            
                'DisableSRN (Tx) - Data Transfer Interrupts
                Call EnableOrDisableSRN(r, vIndex, sCCR & "TBIEN", sINPRL & "TBINP")
                Call EnableOrDisableSRN(r, vIndex, sCCR & "TSIEN", sINPRL & "TSINP")
                'DisableSRN (Tx) - Protocol Interrupts
                
                
                'Reset Tx Functions
                r.v(sVarPre & "_USE_ASCSendData") = 0
                r.v(sVarPre & "_USE_ASCTxBufFree") = 0
                r.v(sVarPre & "_USE_ASCTxDataReady") = 0
                'Reset Tx fifo Functions
                r.v(sVarPre & "_USE_ASCFillTxFIFO") = 0
                r.v(sVarPre & "_USE_ASCIsTxFIFObusy") = 0
                r.v(sVarPre & "_USE_ASCIsTxFIFOfull") = 0
                r.v(sVarPre & "_USE_ASCIsTxFIFOempty") = 0
                r.v(sVarPre & "_USE_ASCGetTxFIFOFillingLevel") = 0
                r.v(sVarPre & "_USE_ASCFlushTxFIFO") = 0
            End If
            
'            If r.v(sVarPre & "_ASC_cboClk") = "None" Then
'                'Reset CDIEN
'                r.v(sRegPre & "PCRL_CTR4") = 0
'
'                'Reset Tx Data Transfer Interrupt
'                r.v(sRegPre & "CCR_TSIEN") = 0
'
'                'DisableSRN (TSIEN for Autobaud)
'                Call EnableOrDisableSRN(r, vIndex, sCCR & "TSIEN", sINPRL & "TSINP")
'            End If
            
            If r.v(sVarPre & "_ASC_cboRxD") = "None" And r.v(sVarPre & "_ASC_cboTxD") = "None" Then
                'Disable Protocol Interrupts
                r.v(sRegPre & "PCRL_CTR6") = 0      'Format error
                r.v(sRegPre & "PCRL_CTR7") = 0      'Frame finished
                
                r.v(sRegPre & "ASC_ProtoInt") = 0
                'DisableSRN (TSIEN for Autobaud)
'                Call EnableOrDisableSRN(r, vIndex, sCCR & "PIEN", sINPRH & "PINP")
                Call DisableSRN(r, sVarPre, sRegPre & r.v(sINPRH & "PINP") & "IC", "ASC" & r.v(sINPRH & "PINP") & "IC")
                
                'Reset Function (Vars)
                r.v(sVarPre & "_USE_Init") = 0
                
                'Reset Regs - '"Important": This is a Default setting & this Condition is valid only for DAvE Lite Version
                r.v(sRegPre & "SCTRL_PDL") = 0
                r.v(sRegPre & "SCTRH_WLE") = 0
                r.v(sRegPre & "SCTRH_FLE") = 0
                r.v(sRegPre & "TCSRL_TDEN") = 0
                r.v(sRegPre & "TCSRL_TDSSM") = 0
                r.v(sRegPre & "PCRL_CTR0") = 0
'                r.v(sRegPre & "PCRL_CTR8") = 0
'                r.v(sRegPre & "PCRL_CTR9") = 0
'                r.v(sRegPre & "PCRL_CTR10") = 0
                r.v(sRegPre & "PSR_TBIF") = 0
            End If

        Case "SSC"
            If (r.v(sVarPre & "_SSC_cboClk") = "None") Or (r.v(sVarPre & "_SSC_cboRxD") = "None" And r.v(sVarPre & "_SSC_cboTxD") = "None") Then
                'Reset Functions (Vars)
                r.v(sVarPre & "_USE_Init") = 0
                
                'Reset IO Pins (Resets Alternate Vars & Port Regs)
                Call ReleaseTxDPin(r, vIndex)
                Call ReleaseRxDPin(r, vIndex)
                If r.v(sRegPre & "SSC_OptOperMode") = 0 Then
                   Call ReleaseSlS0Pin(r, vIndex)
                   Call ReleaseSlS1Pin(r, vIndex)
                ElseIf r.v(sRegPre & "SSC_OptOperMode") = 1 Then
                   Call ReleaseSlSPin(r, vIndex)
                End If
                'Reset Fifo Controls & Interrupts
                Call ResetFifo(r, vIndex)
            
                'Reset Regs - '"Important": This is a Default setting & this Condition is valid only for DAvE Lite Version
                r.v(sRegPre & "SCTRL_TRM") = 0
                r.v(sRegPre & "SCTRL_PDL") = 0
                r.v(sRegPre & "SCTRH_WLE") = 0
                r.v(sRegPre & "SCTRH_FLE") = 0

                r.v(sRegPre & "TCSRL_TDEN") = 0
                r.v(sRegPre & "TCSRL_TDSSM") = 0

                r.v(sRegPre & "PCRL_CTR0") = 0
                r.v(sRegPre & "PCRL_CTR1") = 0
                
                'New
                r.v(sRegPre & "PCRL_CTR2") = 1
                'r.v(sVarPre & "_UD02") = 1
                
                'new
                r.v(sRegPre & "PCRL_CTR14") = 0
                r.v(sRegPre & "PCRL_CTR15") = 0
                
                'new default word length
                r.v(sRegPre & "SCTRH_WLE") = 7
           
                'default frame length
                r.v(sRegPre & "SCTRH_FLE") = 7

                If (r.v(sVarPre & "_SSC_cboClk") = "None") Then
                
                  'disable loop back mode
                   r.v(sRegPre & "DX0CR_DSEL") = 0
                  
                 'Default clock polarity and phase
                   r.v(sVarPre & "_SSC_CPH") = 0
                   r.v(sVarPre & "_SSC_CPL") = 0
                   r.v(sRegPre & "BRGH_SCLKCFG") = 0
                End If
                
                r.v(sRegPre & "PCRL_CTR6") = 0
                r.v(sRegPre & "PCRL_CTR8") = 0
                r.v(sRegPre & "PCRL_CTR9") = 0
                r.v(sRegPre & "PCRL_CTR10") = 0
                r.v(sRegPre & "PCRL_CTR11") = 0

                r.v(sRegPre & "PCRH_CTR16") = 0

                r.v(sRegPre & "DX0CR_INSW") = 0
                r.v(sRegPre & "DX1CR_INSW") = 0
                r.v(sRegPre & "DX2CR_INSW") = 0
                
                'DisableSRN
                Call DisableSRN(r, sVarPre, sRegPre & "0IC", r.v(sVarPre & "_Proto") & "0IC")
                Call DisableSRN(r, sVarPre, sRegPre & "1IC", r.v(sVarPre & "_Proto") & "1IC")
                Call DisableSRN(r, sVarPre, sRegPre & "2IC", r.v(sVarPre & "_Proto") & "2IC")
                sCC2SRN = GetCC2SRN(sVarPre)        'CC2 xx channel's SRN for UxCy
                sCC2Chxx = GetCC2Chxx(sVarPre)      'CC2 xx channel for UxCy
                Call DisableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
            
            End If

            'This part of the code should be called only after "Call ReleaseRxDPin(r, vIndex)"
            If r.v(sVarPre & "_SSC_cboRxD") = "None" Then
            
                'Release Rx Interrupt
                If (r.v(sRegPre & "DX0CR_DSEL") <> 6) Then
                    r.v(sRegPre & "CCR_RIEN") = 0
                    r.v(sRegPre & "CCR_RSIEN") = 0
                    r.v(sRegPre & "CCR_DLIEN") = 0
'                   r.v(sRegPre & "INPRL_RINP") = 0
'                   r.v(sRegPre & "INPRH_PINP") = 0
                
                 Call EnableOrDisableSRN(r, vIndex, sCCR & "RIEN", sINPRL & "RINP")
                 Call EnableOrDisableSRN(r, vIndex, sCCR & "RSIEN", sINPRL & "TBINP")
                 Call EnableOrDisableSRN(r, vIndex, sCCR & "DLIEN", sINPRH & "PINP")
               End If
                'receive fifo
                r.v(sRegPre & "RBCTRH_SIZE") = 0
                r.sfr(sRegPre & "RBCTRL") = &H0
                r.sfr(sRegPre & "RBCTRH") = &H0
                
                'Reset Rx Functions
                r.v(sVarPre & "_USE_SSCGetData") = 0
                r.v(sVarPre & "_USE_SSCRxDataReady") = 0
                'Reset Rx fifo Functions
                r.v(sVarPre & "_USE_SSCGetRxFIFOData") = 0
                r.v(sVarPre & "_USE_SSCIsRxFIFObusy") = 0
                r.v(sVarPre & "_USE_SSCIsRxFIFOfull") = 0
                r.v(sVarPre & "_USE_SSCIsRxFIFOempty") = 0
                r.v(sVarPre & "_USE_SSCGetRxFIFOFillingLevel") = 0
                r.v(sVarPre & "_USE_SSCFlushRxFIFO") = 0
            End If

            'This part of the code should be called only after "Call ReleaseTxDPin(r, vIndex)"
            If r.v(sVarPre & "_SSC_cboTxD") = "None" Then
                'Release TxBuffer Interrupt
                
                r.v(sRegPre & "CCR_TBIEN") = 0
                r.v(sRegPre & "CCR_TSIEN") = 0

                'DisableSRN
                Call EnableOrDisableSRN(r, vIndex, sCCR & "TBIEN", sINPRL & "TBINP")
                Call EnableOrDisableSRN(r, vIndex, sCCR & "TSIEN", sINPRL & "TSINP")
                
                'Reset Tx Functions
                r.v(sVarPre & "_USE_SSCSendData") = 0
                r.v(sVarPre & "_USE_SSCTxBufFree") = 0
                r.v(sVarPre & "_USE_SSCTxDataReady") = 0
                'fifo
                r.v(sRegPre & "TBCTRH_SIZE") = 0
                r.sfr(sRegPre & "TBCTRL") = &H0
                r.sfr(sRegPre & "TBCTRH") = &H0
                
                'Reset Tx fifo Functions
                r.v(sVarPre & "_USE_SSCFillTxFIFO") = 0
                r.v(sVarPre & "_USE_SSCIsTxFIFObusy") = 0
                r.v(sVarPre & "_USE_SSCIsTxFIFOfull") = 0
                r.v(sVarPre & "_USE_SSCIsTxFIFOempty") = 0
                r.v(sVarPre & "_USE_SSCGetTxFIFOFillingLevel") = 0
                r.v(sVarPre & "_USE_SSCFlushTxFIFO") = 0
            End If
            
            If (r.v(sVarPre & "_SSC_cboSlS") = "None" And r.v(sVarPre & "_UD00") = "None" And r.v(sVarPre & "_UD01") = "None") Then
      
                r.v(sRegPre & "SSC_ProtoInt") = 0
                r.v(sRegPre & "PCRL_CTR14") = 0
                r.v(sRegPre & "PCRL_CTR15") = 0
                r.v(sRegPre & "PCRL_CTR2") = 1
                Call EnableOrDisableSRN(r, vIndex, sRegPre & "SSC_ProtoInt", sINPRH & "PINP")
                'Call DisableSRN(r, sVarPre, sRegPre & r.v(sINPRH & "PINP") & "IC", "SSC" & r.v(sINPRH & "PINP") & "IC")
            End If
            
        Case "IIC"
            'Reset SFRs
            r.sfr(sRegPre & "CCR") = &H0
            r.sfr(sRegPre & "INPRL") = &H0
            r.sfr(sRegPre & "INPRH") = &H0
            r.sfr(sRegPre & "PCRL") = &H0
            r.sfr(sRegPre & "PCRH") = &H0
            r.sfr(sRegPre & "SCTRL") = &H0
            r.sfr(sRegPre & "SCTRH") = &H0
            r.sfr(sRegPre & "TCSRL") = &H0
            r.sfr(sRegPre & "FDRL") = &H0
            r.sfr(sRegPre & "BRGL") = &H0
            r.sfr(sRegPre & "BRGH") = &H0

            'Reset internal vars
            r.v(sRegPre & "IIC_OMOD") = 0
            r.v(sRegPre & "IIC_AMOD") = 0
            Call SetIICAddr(r, vIndex, 0)
            r.v(sRegPre & "IIC_ProtoInt") = 0
            
            'DisableSRN
            Call DisableSRN(r, sVarPre, sRegPre & "0IC", r.v(sVarPre & "_Proto") & "0IC")
            Call DisableSRN(r, sVarPre, sRegPre & "1IC", r.v(sVarPre & "_Proto") & "1IC")
            Call DisableSRN(r, sVarPre, sRegPre & "2IC", r.v(sVarPre & "_Proto") & "2IC")
            sCC2SRN = GetCC2SRN(sVarPre)        'CC2 xx channel's SRN for UxCy
            sCC2Chxx = GetCC2Chxx(sVarPre)      'CC2 xx channel for UxCy
            Call DisableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
            
            'Reset Fifo Controls & Interrupts
            Call ResetFifo(r, vIndex)

            'Reset Functions Vars
            r.v(sVarPre & "_USE_Init") = 0
            r.v(sVarPre & "_USE_IICMasterTransmit") = 0
            r.v(sVarPre & "_USE_IICMasterReceive") = 0
            r.v(sVarPre & "_USE_IICWriteData") = 0
            r.v(sVarPre & "_USE_IICReadData") = 0
            r.v(sVarPre & "_USE_IICResetStatus") = 0
            r.v(sVarPre & "_USE_IICGetStatus") = 0
            r.v(sVarPre & "_USE_IICEnableACK") = 0
            r.v(sVarPre & "_USE_IICDisableACK") = 0
            r.v(sVarPre & "_USE_IICInitSlaveMode") = 0

        Case "IIS"
            
      
            Call DisableSRN(r, sVarPre, sRegPre & "0IC", r.v(sVarPre & "_Proto") & "0IC")
            Call DisableSRN(r, sVarPre, sRegPre & "1IC", r.v(sVarPre & "_Proto") & "1IC")
            Call DisableSRN(r, sVarPre, sRegPre & "2IC", r.v(sVarPre & "_Proto") & "2IC")
            sCC2SRN = GetCC2SRN(sVarPre)        'CC2 xx channel's SRN for UxCy
            sCC2Chxx = GetCC2Chxx(sVarPre)      'CC2 xx channel for UxCy
            Call DisableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
      
            
            
            'Release WAInterrupt
            r.v(sRegPre & "PCRL_CTR4") = 0
            r.v(sRegPre & "PCRL_CTR5") = 0
            r.v(sRegPre & "INPRH_PINP") = 0
            
            r.v(sRegPre & "IIS_ProtoInt") = 0
           
            'Reset Functions Vars
             r.v(sVarPre & "_USE_Init") = 0
            r.v(sVarPre & "_USE_IISWriteData") = 0
            r.v(sVarPre & "_USE_IISReadData") = 0
            r.v(sVarPre & "_USE_IISSelectChannel") = 0
            
            
            'Reset Interrupt vars
            r.v(sVarPre & "_USE_IIS0IC") = 0
            r.v(sVarPre & "_USE_IIS1IC") = 0
            r.v(sVarPre & "_USE_IIS2IC") = 0
            r.v(sVarPre & "_USE_IIS3IC") = 0
            
            'Reset FIFO
             'Reset Fifo Controls & Interrupts
            
            Call ResetFifo(r, vIndex)
            
            r.v(sVarPre & "_USE_IISFillTxFIFO") = 0
            r.v(sVarPre & "_USE_IISGetRxFIFOData") = 0
            r.v(sVarPre & "_USE_IISIsRxFIFObusy") = 0
            r.v(sVarPre & "_USE_IISIsTxFIFObusy") = 0
            r.v(sVarPre & "_USE_IISIsRxFIFOfull") = 0
            r.v(sVarPre & "_USE_IISIsTxFIFOfull") = 0
            r.v(sVarPre & "_USE_IISIsRxFIFOempty") = 0
            r.v(sVarPre & "_USE_IISIsTxFIFOempty") = 0
            r.v(sVarPre & "_USE_IISGetRxFIFOFillingLevel") = 0
            r.v(sVarPre & "_USE_IISGetTxFIFOFillingLevel") = 0
            r.v(sVarPre & "_USE_IISFlushRxFIFO") = 0
            r.v(sVarPre & "_USE_IISFlushTxFIFO") = 0

            
            
            
            
             'Reset SFRs
            r.sfr(sRegPre & "INPRL") = &H0
            r.sfr(sRegPre & "INPRH") = &H0
            
            
            r.v(sRegPre & "CCR_AIEN") = 0
            r.v(sRegPre & "CCR_RIEN") = 0
            r.v(sRegPre & "CCR_RSIEN") = 0
            r.v(sRegPre & "CCR_TBIEN") = 0
            r.v(sRegPre & "CCR_TSIEN") = 0
            r.v(sRegPre & "CCR_DLIEN") = 0
            
            r.v(sRegPre & "INPRL_AINP") = 0
            r.v(sRegPre & "INPRL_RINP") = 0
            r.v(sRegPre & "INPRL_TBINP") = 0
            r.v(sRegPre & "INPRL_TSINP") = 0
            r.v(sRegPre & "INPRH_PINP") = 0
            
           
            
            
            r.v(sVarPre & "_USE_IIS0IC") = 0
            r.v(sVarPre & "_USE_IIS1IC") = 0
            r.v(sVarPre & "_USE_IIS2IC") = 0
            r.v(sVarPre & "_USE_IIS3IC") = 0
            

    End Select
    
End Sub


'*****************************************************************************
'Description:
'        This function
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Function GetInterfacePinNo(r As ProjectData, ModuleName As Variant, Intrface As String) As String
On Error Resume Next

  Dim sVarPre As String
  Dim sRegPre As String
  
  Dim scboTxd As String
  Dim sPinArr() As String
  Dim sTxDPort As String
  Dim sTxDPin As String
  
  sVarPre = ModuleName
  sRegPre = sVarPre & "_"
  
    scboTxd = Right(Intrface, (Len(Intrface)) - 1)
    sPinArr() = SplitLine(scboTxd, ".")
    sTxDPort = sPinArr(0)
    sTxDPin = sPinArr(1)
    
    If (CInt(sTxDPin) < 10) Then
      sTxDPin = "0" & sPinArr(1)
    End If
    
    GetInterfacePinNo = "P" & sTxDPort & "_IOCR" & sTxDPin
    
End Function


'*****************************************************************************
'Description:
'        This function adds SDA pins and returns the listindex to the cboSDA.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        ModuleName      USIC Channel name
'        cboCrtl         SDA combo box control
'-----------------------------------------------------------------------------
'Returns: ListIndex to SDA Pins combobox
'*****************************************************************************
Function AddINTnodes(r As ProjectData, ModuleName As String, cboCrtl As Variant, sINP As String) As Integer
On Error Resume Next
  
  Dim i As Integer
  
  cboCrtl.Clear
                                                'UxCy xIC USIC interrupt nodes
  cboCrtl.AddItem ModuleName & "_0IC INT"
  cboCrtl.AddItem ModuleName & "_1IC INT"
  cboCrtl.AddItem ModuleName & "_2IC INT"
  
  Select Case (ModuleName)                      'CC2 interrupt nodes
    Case ("U0C0")
      If r.v("CC2_Int20_Use") = 0 Then cboCrtl.AddItem ("CC2_CC20IC INT")
    Case ("U0C1")
      If r.v("CC2_Int21_Use") = 0 Then cboCrtl.AddItem ("CC2_CC21IC INT")
    Case ("U1C0")
      If r.v("CC2_Int22_Use") = 0 Then cboCrtl.AddItem ("CC2_CC22IC INT")
    Case ("U1C1")
      If r.v("CC2_Int23_Use") = 0 Then cboCrtl.AddItem ("CC2_CC23IC INT")
    Case ("U2C0")
      If r.v("CC2_Int28_Use") = 0 Then cboCrtl.AddItem ("CC2_CC28IC INT")
    Case ("U2C1")
      If r.v("CC2_Int29_Use") = 0 Then cboCrtl.AddItem ("CC2_CC29IC INT")
    Case Else
  End Select
  
  AddINTnodes = r.v(ModuleName & "_" & sINP)
  
End Function


'*****************************************************************************
'Description:
'        This function is used to check the status of the INT's allocation in
'        Nodes
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        sxINP           the node pointer
'-----------------------------------------------------------------------------
'Returns:  True if current SRN is occupied by any one of the Node or Obj
'          false if Current SRN is Free
'*****************************************************************************
Function GetNodexStatus(r As ProjectData, vIndex As Variant, sxINP As String) As Boolean
        On Error Resume Next

  GetNodexStatus = False

  Dim i As Integer
  Dim sVarPre As String
  Dim sRegPre As String
  Dim sIEN(12) As String
  Dim sINP(12) As String

  sVarPre = vIndex
  sRegPre = sVarPre & "_"

  sIEN(0) = sRegPre & "CCR_AIEN"            '0-5 Data transfer interrupts IENs
  sIEN(1) = sRegPre & "CCR_RIEN"
  sIEN(2) = sRegPre & "CCR_RSIEN"
  sIEN(3) = sRegPre & "CCR_TBIEN"
  sIEN(4) = sRegPre & "CCR_TSIEN"
  sIEN(5) = sRegPre & "CCR_DLIEN"
  sIEN(6) = sRegPre & r.v(sVarPre & "_Proto") & "_ProtoInt"     '6 Protocol interrups IENs
  sIEN(7) = sRegPre & "TBCTRH_STBIEN"       '7-11 FIFO interrupts IENs
  sIEN(8) = sRegPre & "TBCTRH_TBERIEN"
  sIEN(9) = sRegPre & "RBCTRH_SRBIEN"
  sIEN(10) = sRegPre & "RBCTRH_ARBIEN"
  sIEN(11) = sRegPre & "RBCTRH_RBERIEN"

  sINP(0) = sRegPre & "INPRL_AINP"          '0-5 Data transfer interrupts INPs
  sINP(1) = sRegPre & "INPRL_RINP"
  sINP(2) = sRegPre & "INPRL_TBINP"
  sINP(3) = sRegPre & "INPRL_TBINP"
  sINP(4) = sRegPre & "INPRL_TSINP"
  sINP(5) = sRegPre & "INPRH_PINP"
  sINP(6) = sRegPre & "INPRH_PINP"          '6 Protocol interrups INPs
  sINP(7) = sRegPre & "TBCTRH_STBINP"
  sINP(8) = sRegPre & "TBCTRH_ATBINP"      '7-11 FIFO interrupts INPs
  sINP(9) = sRegPre & "RBCTRH_SRBINP"
  sINP(10) = sRegPre & "RBCTRH_ARBINP"
  sINP(11) = sRegPre & "RBCTRH_ARBINP"
  
  
  For i = 0 To 11
    If (r.v(sIEN(i)) = 1) Then          '"UxCy_CCR_xIEN"
        If InStr(sINP(i), sxINP) Then   'Chk same int INP
            GetNodexStatus = False
        Else
            If (r.v(sINP(i)) = r.v(sxINP)) Then
                GetNodexStatus = True   'Yes,Another interrupt points to the same node
                Exit For
            End If
        End If
    End If
  Next i

End Function


'*****************************************************************************
'Description:
'        This function Enable/Disable the interrupt node assigned when interrupt
'        is enabled/disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          USIC channel no# UxCy,  x=0-2,y=0-1
'        sIEN            Interrupt Enable/Disable bit
'        sINP            Interrupt node pointer
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub EnableOrDisableSRN(r As ProjectData, vIndex As Variant, sIEN As String, sINP As String)
  On Error Resume Next
  
  Dim sVarPre     As String
  Dim sRegPre     As String
  Dim sCC2SRN     As String
  Dim sCC2Chxx    As String
  Dim sCC2INTx    As String
  Dim sISSRISSx   As String
  
  sVarPre = vIndex
  sRegPre = sVarPre & "_"

  If Not (r.v(sINP) = 3) Then          'UxCy xIC USIC interrupt nodes
    If ((r.v(sIEN) = 0) And (Not GetNodexStatus(r, vIndex, sINP))) Then
        Call DisableSRN(r, sVarPre, sRegPre & r.v(sINP) & "IC", r.v(sVarPre & "_Proto") & r.v(sINP) & "IC")
        r.v(sINP) = 0
    Else
        Call EnableSRN(r, sVarPre, sRegPre & r.v(sINP) & "IC", r.v(sVarPre & "_Proto") & r.v(sINP) & "IC")
    End If
  Else                                  'CC2 interrupt node
    sCC2SRN = GetCC2SRN(sVarPre)        'CC2 xx channel's SRN for UxCy
    sCC2Chxx = GetCC2Chxx(sVarPre)      'CC2 xx channel for UxCy
    
    Select Case (vIndex)
      Case ("U0C0")
          sCC2INTx = "U0C0_CC2_ISSR4"
          sISSRISSx = "SCU_" & "ISSR_ISS4"
      Case ("U0C1")
          sCC2INTx = "U0C1_CC2_ISSR5"
          sISSRISSx = "SCU_" & "ISSR_ISS5"
      Case ("U1C0")
          sCC2INTx = "U1C0_CC2_ISSR6"
          sISSRISSx = "SCU_" & "ISSR_ISS6"
      Case ("U1C1")
          sCC2INTx = "U1C1_CC2_ISSR7"
          sISSRISSx = "SCU_" & "ISSR_ISS7"
      Case ("U2C0")
          sCC2INTx = "U2C0_CC2_ISSR12"
          sISSRISSx = "SCU_" & "ISSR_ISS12"
      Case ("U2C1")
          sCC2INTx = "U2C1_CC2_ISSR13"
          sISSRISSx = "SCU_" & "ISSR_ISS13"
      Case Else
    End Select
    
    If ((r.v(sIEN) = 0) And (Not GetNodexStatus(r, vIndex, sINP))) Then
        r.v(sINP) = 0
        r.v(sCC2INTx) = 0
        r.v(sISSRISSx) = 0
        Call DisableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
    Else
        r.v(sCC2INTx) = 1
        r.v(sISSRISSx) = 1
        Call EnableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
    End If
  End If
End Sub
            
            
'*****************************************************************************
'Description:
'        This function enables the interrupt node selected and disables the
'        previously assigned interrupt node if not used by others
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          USIC channel no# UxCy,  x=0-2,y=0-1
'        cboCrtl         combo control of the interrupt (list of SRNs)
'        sINP            Interrupt node pointer
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SelectSRN(r As ProjectData, vIndex As Variant, cboCrtl As Variant, sINP As String)
  On Error Resume Next
  
  Dim sVarPre     As String
  Dim sRegPre     As String
  Dim sCC2SRN     As String
  Dim sCC2Chxx    As String
  Dim sCC2INTx    As String
  Dim sISSRISSx   As String
  
  sVarPre = vIndex
  sRegPre = sVarPre & "_"
  
  If InStr(sINP, "_PINP") Then
    If (Ask("The Protocol interrupt and Data lost interrupt shares the same interrupt node." & vbCrLf & _
            "The change of interrupt node will reflect in the other interrupt also. Do you" & vbCrLf & _
            "really want to continue?")) Then
    Else
        Exit Sub
    End If
  End If
  
  If InStr(sINP, "_TBINP") Then
    If (Ask("The Transmit buffer interrupt and Receiver start interrupt shares the same" & vbCrLf & _
            "interrupt node. The change of interrupt node will reflect in the other interrupt also." & vbCrLf & _
            "Do you really want to continue?")) Then
    Else
        Exit Sub
    End If
  End If
  '_ARBINP
  If InStr(sINP, "_ARBINP") And (r.v(sRegPre & "RBCTRH_RNM") = 1) Then
    If (Ask("The Alternate receive buffer interrupt and Receiver buffer error interrupt shares the same" & vbCrLf & _
            "interrupt node. The change of interrupt node will reflect in the other interrupt also." & vbCrLf & _
            "Do you really want to continue?")) Then
    Else
        Exit Sub
    End If
  End If
  
  sCC2SRN = GetCC2SRN(sVarPre)        'CC2 xx channel's SRN for UxCy
  sCC2Chxx = GetCC2Chxx(sVarPre)      'CC2 xx channel for UxCy
  
  Select Case (vIndex)
    Case ("U0C0")
        sCC2INTx = "U0C0_CC2_ISSR4"
        sISSRISSx = "SCU_" & "ISSR_ISS4"
    Case ("U0C1")
        sCC2INTx = "U0C1_CC2_ISSR5"
        sISSRISSx = "SCU_" & "ISSR_ISS5"
    Case ("U1C0")
        sCC2INTx = "U1C0_CC2_ISSR6"
        sISSRISSx = "SCU_" & "ISSR_ISS6"
    Case ("U1C1")
        sCC2INTx = "U1C1_CC2_ISSR7"
        sISSRISSx = "SCU_" & "ISSR_ISS7"
    Case ("U2C0")
        sCC2INTx = "U2C0_CC2_ISSR12"
        sISSRISSx = "SCU_" & "ISSR_ISS12"
    Case ("U2C1")
        sCC2INTx = "U2C1_CC2_ISSR13"
        sISSRISSx = "SCU_" & "ISSR_ISS13"
    Case Else
  End Select
                                    'Disable the previous/already assigned interrupt node
  If Not (r.v(sINP) = 3) Then       'UxCy xIC USIC interrupt nodes
    If (Not GetNodexStatus(r, vIndex, sINP)) Then
        Call DisableSRN(r, sVarPre, sRegPre & r.v(sINP) & "IC", r.v(sVarPre & "_Proto") & r.v(sINP) & "IC")
    End If
    r.v(sINP) = cboCrtl.ListIndex
  Else                              'CC2 interrupt node
    If (Not GetNodexStatus(r, vIndex, sINP)) Then
        r.v(sCC2INTx) = 0
        r.v(sISSRISSx) = 0
        Call DisableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
    End If
    r.v(sINP) = cboCrtl.ListIndex
  End If
                                    'Enable the selected interrupt node
  If Not (r.v(sINP) = 3) Then       'UxCy xIC USIC interrupt nodes
    Call EnableSRN(r, sVarPre, sRegPre & r.v(sINP) & "IC", r.v(sVarPre & "_Proto") & r.v(sINP) & "IC")
  Else                              'CC2 interrupt node
    r.v(sCC2INTx) = 1
    r.v(sISSRISSx) = 1
    Call EnableSRN(r, "CC2", sCC2SRN, sCC2Chxx)
  End If

End Sub


'*****************************************************************************
'Description:
'          This function returns the CC2 Channel's SRN
'          which is shared with USIC channel
'-----------------------------------------------------------------------------
'Parameters:
'        vIndex           USIC channel no# UxCy,  x=0-2,y=0-1
'-----------------------------------------------------------------------------
'Returns:
'      CC2 Channel's SRN IC sfr
'*****************************************************************************
Function GetCC2SRN(vIndex As Variant) As String
  On Error Resume Next
  
  Select Case (vIndex)      'Refer User Manual
    Case ("U0C0")
        GetCC2SRN = "CC2_CC20IC"
    Case ("U0C1")
        GetCC2SRN = "CC2_CC21IC"
    Case ("U1C0")
        GetCC2SRN = "CC2_CC22IC"
    Case ("U1C1")
        GetCC2SRN = "CC2_CC23IC"
    Case ("U2C0")
        GetCC2SRN = "CC2_CC28IC"
    Case ("U2C1")
        GetCC2SRN = "CC2_CC29IC"
    Case Else
  End Select
   
End Function


'*****************************************************************************
'Description:
'          This function returns the CC2 Channel's name
'          which is shares Intterupt node with USIC channel
'-----------------------------------------------------------------------------
'Parameters:
'        vIndex           USIC channel no# UxCy,  x=0-2,y=0-1
'-----------------------------------------------------------------------------
'Returns:
'      CC2 Channel's name
'*****************************************************************************
Function GetCC2Chxx(vIndex As Variant) As String
  On Error Resume Next
  
  Select Case (vIndex)      'Refer User Manual
    Case ("U0C0")
        GetCC2Chxx = "CC20"
    Case ("U0C1")
        GetCC2Chxx = "CC21"
    Case ("U1C0")
        GetCC2Chxx = "CC22"
    Case ("U1C1")
        GetCC2Chxx = "CC23"
    Case ("U2C0")
        GetCC2Chxx = "CC28"
    Case ("U2C1")
        GetCC2Chxx = "CC29"
    Case Else
  End Select
   
End Function


'*****************************************************************************
'Description:
'          This function returns Interrupt vector number for SRN x
'-----------------------------------------------------------------------------
'Parameters:
'        vIndex           USIC channel no# UxCy,  x=0-2,y=0-1
'        SRNno            Interrupt vector no in vector table
'-----------------------------------------------------------------------------
'Returns:
'      Int vector no #
'*****************************************************************************
Function GetUxCyIntVector(vIndex As Variant, SRNno As Integer) As Variant
  On Error Resume Next

    Select Case (vIndex)      'Refer User Manual
      Case ("U0C0")
          If SRNno = 0 Then GetUxCyIntVector = 80
          If SRNno = 1 Then GetUxCyIntVector = 81
          If SRNno = 2 Then GetUxCyIntVector = 82
          If SRNno = 3 Then GetUxCyIntVector = 20
      Case ("U0C1")
          If SRNno = 0 Then GetUxCyIntVector = 83
          If SRNno = 1 Then GetUxCyIntVector = 84
          If SRNno = 2 Then GetUxCyIntVector = 85
          If SRNno = 3 Then GetUxCyIntVector = 21
      Case ("U1C0")
          If SRNno = 0 Then GetUxCyIntVector = 86
          If SRNno = 1 Then GetUxCyIntVector = 87
          If SRNno = 2 Then GetUxCyIntVector = 88
          If SRNno = 3 Then GetUxCyIntVector = 22
      Case ("U1C1")
          If SRNno = 0 Then GetUxCyIntVector = 89
          If SRNno = 1 Then GetUxCyIntVector = 90
          If SRNno = 2 Then GetUxCyIntVector = 91
          If SRNno = 3 Then GetUxCyIntVector = 23
      Case ("U2C0")
          If SRNno = 0 Then GetUxCyIntVector = 92
          If SRNno = 1 Then GetUxCyIntVector = 93
          If SRNno = 2 Then GetUxCyIntVector = 94
          If SRNno = 3 Then GetUxCyIntVector = 28
      Case ("U2C1")
          If SRNno = 0 Then GetUxCyIntVector = 95
          If SRNno = 1 Then GetUxCyIntVector = 96
          If SRNno = 2 Then GetUxCyIntVector = 97
          If SRNno = 3 Then GetUxCyIntVector = 29
    End Select
  
End Function


'*****************************************************************************
'Description:
'          This function returns the module clock [MHz] of the
'          USIC module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'      GetUSICModuleClock [MHz]
'
'*****************************************************************************
Function GetUSICModuleClock(r As ProjectData) As Double
  On Error Resume Next
  GetUSICModuleClock = GetPeripheralClock(r)
   
End Function
