Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\include\Port.inc"

Option Explicit
'*****************************************************************************
' Module:
'        IR
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        IR dialog script and in the IR codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  Radhish        27-10-04    initial version for XC866
'        0.2  Sheshadri      22-02-08    Enhanced Pins Features for external interrupts
'                                           are added wrt to XC278
'
'*****************************************************************************

'*****************************************************************************
' Constants
'*****************************************************************************

Global Const NUMBER_OF_EXT_INTERRUPTS = 4   ' number of external interrupts
Global Const NUMBER_OF_PERI_INTERRUPTS = 10 ' number of interrupts of the peripherals (T01, ADC, USART etc.)

'*****************************************************************************
'Description:
'        This functions returns the variable name of the enable bit
'        for a specific external interrupt.
'-----------------------------------------------------------------------------
'Parameters:
'        iIndex           interrupt index (0...NUMBER_OF_EXT_INTERRUPTS-1)
'-----------------------------------------------------------------------------
'Returns:
'        variable name of the enable bit
'*****************************************************************************

'*****************************************************************************
'Description:
'        This functions returns the vector address of a specific ext interrupt
'-----------------------------------------------------------------------------
'Parameters:
'        iIndex           interrupt index (0...NUMBER_OF_EXT_INTERRUPTS-1)
'-----------------------------------------------------------------------------
'Returns:
'        address of the interrupt vector
'*****************************************************************************
Public Function ExtIntVectorAddr(iIndex As Integer) As Long
    On Error Resume Next
    ExtIntVectorAddr = 0
    If iIndex = 0 Then ExtIntVectorAddr = &H3
    If iIndex = 1 Then ExtIntVectorAddr = &H13
    If iIndex = 2 Then ExtIntVectorAddr = &H2B
    If iIndex = 3 Then ExtIntVectorAddr = &H4B
End Function

'*****************************************************************************
'Description:
'        This functions returns the Id of an external interrupt.
'        The Id is used internally to access project variables.
'        The Id is also used as the parameter's default name.
'-----------------------------------------------------------------------------
'Parameters:
'        iIndex           interrupt index (0...NUMBER_OF_EXT_INTERRUPTS-1)
'-----------------------------------------------------------------------------
'Returns:
'        internal ID of the interrupt
'*****************************************************************************
Public Function ExtIntId(iIndex As Integer, r As ProjectData) As String
    On Error Resume Next
    If (iIndex = 2) Then
       ExtIntId = "XINTR8_INT"
    ElseIf (iIndex = 3) Then
       If (Not IsCpuTypeOK(r)) Then
           ExtIntId = "EXTERNAL_INT" & iIndex
       Else
          ExtIntId = "XINTR9_INT"
       End If
    Else
      ExtIntId = "EXTERNAL_INT" & iIndex
    End If
End Function

'*****************************************************************************
'Description:
'        This functions returns the Id of a peripheral's interrupt.
'        The Id is used internally to access project variables.
'        The Id is also used as the parameter's default name.
'-----------------------------------------------------------------------------
'Parameters:
'        iIndex           interrupt index (0...NUMBER_OF_PERI_INTERRUPTS-1)
'-----------------------------------------------------------------------------
'Returns:
'        internal ID of the interrupt
'*****************************************************************************
Public Function PeriIntId(iIndex As Integer, r As ProjectData) As String
    On Error Resume Next
    PeriIntId = ""
    If (Not IsCpuTypeOK(r)) Then
        If iIndex = 0 Then PeriIntId = "ADC_INT"
        If iIndex = 1 Then PeriIntId = "CCU6_NODE0_INT"
        If iIndex = 2 Then PeriIntId = "CCU6_NODE1_INT"
        If iIndex = 3 Then PeriIntId = "CCU6_NODE2_INT"
        If iIndex = 4 Then PeriIntId = "CCU6_NODE3_INT"
    Else
        If iIndex = 0 Then PeriIntId = "XINTR6_INT"
        If iIndex = 1 Then PeriIntId = "XINTR10_INT"
        If iIndex = 2 Then PeriIntId = "XINTR11_INT"
        If iIndex = 3 Then PeriIntId = "XINTR12_INT"
        If iIndex = 4 Then PeriIntId = "XINTR13_INT"
    End If
    If iIndex = 5 Then PeriIntId = "T0_INT"
    If iIndex = 6 Then PeriIntId = "T1_INT"
    If iIndex = 7 Then PeriIntId = "XINTR5_INT"
    If iIndex = 8 Then PeriIntId = "UART_INT"
    If iIndex = 9 Then PeriIntId = "SSC_INT"
End Function

'*****************************************************************************
'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
'        "IR_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 RefreshFunctionDescriptions(func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next

  Dim sDescription As String
  Dim sReturnValue As String
  Dim iInt As Integer
  Dim sIntList As String

  RefreshFunctionDescriptions = False

  ' for each function of this module the property AddFunction must be called
  '
  ' AddFunction(sFuncName As String, sFuncType As String, sFuncDescription As String,
  '             sReturnvalue As String, lSort As Long) As Boolean
  '
  ' sFuncName          | String | function name as key (use the name of the variable
  '                               for the function name e.g. SSC_NAME_Init)
  ' sFuncType          | String | type of the function
  ' sFuncDescription   | String | description of the function
  ' sReturnvalue       | String | description of the return value
  ' lSort              | Long   | ' sort of the function (0 => macro, 1 => function, 2 => ISR)

    ' for each parameter of a function the property AddParameter must be called
    '
    'AddParameter(sParName As String, sPar As String, sParType As String, sParDescription As String) As Boolean
    '
    ' sParName         | String | parameter name (key)
    ' sPar             | String | parameter name
    ' sParType         | String | type of the parameter
    ' sParDescription  | String | description of the parameter


  ' Generate list of the available Interrupt Names:
  sIntList = ""
  For iInt = 0 To NUMBER_OF_PERI_INTERRUPTS - 1
      sIntList = sIntList & r.v("IR_PARA_" & PeriIntId(iInt, r)) & ", "
  Next iInt

  For iInt = 0 To NUMBER_OF_EXT_INTERRUPTS - 2
      sIntList = sIntList & r.v("IR_PARA_" & ExtIntId(iInt, r)) & ", "
  Next iInt
      sIntList = sIntList & r.v("IR_PARA_" & ExtIntId(NUMBER_OF_EXT_INTERRUPTS - 1, r))

  '------------------------------------------------------------------------------------------------
  ' Description for IR_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription("IR") & vbCrLf & vbCrLf & _
                 "The project settings regarding the external interrupts " & _
                 "are taken over into the corresponding special function registers " & _
                 "IENx (interrupt enabling), TCON and EXICONx (trigger selection)(x=0 or 1). " & _
                 "The global enable Bit EA is not changed. " & _
                 "The initialization of the interrupt priorities is located in function " & r.v("MAIN_NAME_Init") & "."
  sReturnValue = "None"
  If (Not func.AddFunction("IR_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IR_NAME_Ext(i)
  '------------------------------------------------------------------------------------------------
  For iInt = 0 To NUMBER_OF_EXT_INTERRUPTS - 1
      sDescription = "This is the service routine for the External interrupt " & iInt & ". " & vbCrLf & _
                     TOOLTIP_ISR_USERCODE
      sReturnValue = "None"
      If (Not func.AddFunction("IR_NAME_IsrExt" & iInt, "void", sDescription, sReturnValue, 2)) Then Exit Function
  Next iInt
'------------------------------------------------------------------------------------------------
  ' Description for IR_NAME_NMI
  '------------------------------------------------------------------------------------------------
      sDescription = "This is the service routine for the Non Maskable Interrupts. " & vbCrLf & _
                     TOOLTIP_ISR_USERCODE
      sReturnValue = "None"
      If (Not func.AddFunction("IR_NAME_IsrNMI", "void", sDescription, sReturnValue, 2)) Then Exit Function
  '------------------------------------------------------------------------------------------------
  ' Description for IR_NAME_EnableAll
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro globally enables all interrupts by setting bit EA."
  sReturnValue = "None"
  If (Not func.AddFunction("IR_NAME_EnableAll", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IR_NAME_DisableAll
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro globally disables all interrupts by clearing bit EA."
  sReturnValue = "None"
  If (Not func.AddFunction("IR_NAME_DisableAll", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IR_NAME_EnableInterrupt
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro enables a specific interrupt which is passed in a parameter. " & _
                 "The following constants are available for 'IntName':" & vbCrLf & sIntList
  sReturnValue = "None"
  If (Not func.AddFunction("IR_NAME_EnableInterrupt", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Name of the Interrupt"
  If (Not func.AddParameter("IR_NAME_EnableInterrupt_Par1", "IntName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IR_NAME_DisableInterrupt
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro disables a specific interrupt which is passed in a parameter. " & _
                 "The following constants are available for 'IntName':" & vbCrLf & sIntList
  sReturnValue = "None"
  If (Not func.AddFunction("IR_NAME_DisableInterrupt", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Name of the Interrupt"
  If (Not func.AddParameter("IR_NAME_DisableInterrupt_Par1", "IntName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
        If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function

'*****************************************************************************
'Description:
'        This function adds Ext pins and returns the listindex to the cboSCL.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        IntrName        Ext Intr name
'        cboCrtl         SCL combo box control
'-----------------------------------------------------------------------------
'Returns: ListIndex to Ext Pins combobox
'*****************************************************************************

Function AddExtPinGetIndex(r As ProjectData, IntrName As String, cboCrtl As Variant) As Integer
On Error Resume Next

  Dim i As Integer
    
  cboCrtl.Clear
  cboCrtl.AddItem ("None")

  Select Case (IntrName)
    Case ("EXINT0")
         If (IsAlternatePinFree(r, "EXINT0_0")) Then cboCrtl.AddItem ("P0.5")
         If (IsAlternatePinFree(r, "EXINT0_1")) Then cboCrtl.AddItem ("P1.4")
    
    Case ("EXINT1")
         If (IsAlternatePinFree(r, "EXINT1_0")) Then cboCrtl.AddItem ("P5.3")
         If (IsAlternatePinFree(r, "EXINT1_1")) Then cboCrtl.AddItem ("P5.0")
    
    Case ("EXINT2")
         If (IsAlternatePinFree(r, "EXINT2_0")) Then cboCrtl.AddItem ("P5.4")
         If (IsAlternatePinFree(r, "EXINT2_1")) Then cboCrtl.AddItem ("P5.1")
         
    Case ("EXINT3")
         If (IsAlternatePinFree(r, "EXINT3_0")) Then cboCrtl.AddItem ("P1.1")
         If (IsAlternatePinFree(r, "EXINT3_1")) Then cboCrtl.AddItem ("P4.0")
         If (IsAlternatePinFree(r, "EXINT3_2")) Then cboCrtl.AddItem ("P3.0")
         If (IsAlternatePinFree(r, "EXINT3_3")) Then cboCrtl.AddItem ("P5.5")
         
    Case ("EXINT4")
         If (IsAlternatePinFree(r, "EXINT4_0")) Then cboCrtl.AddItem ("P3.7")
         If (IsAlternatePinFree(r, "EXINT4_1")) Then cboCrtl.AddItem ("P4.1")
         If (IsAlternatePinFree(r, "EXINT4_2")) Then cboCrtl.AddItem ("P3.2")
         If (IsAlternatePinFree(r, "EXINT4_3")) Then cboCrtl.AddItem ("P5.6")
         
    Case ("EXINT5")
         If (IsAlternatePinFree(r, "EXINT5_0")) Then cboCrtl.AddItem ("P1.5")
         If (IsAlternatePinFree(r, "EXINT5_1")) Then cboCrtl.AddItem ("P4.4")
         If (IsAlternatePinFree(r, "EXINT5_2")) Then cboCrtl.AddItem ("P3.3")
         If (IsAlternatePinFree(r, "EXINT5_3")) Then cboCrtl.AddItem ("P5.2")
         
    Case ("EXINT6")
         If (IsAlternatePinFree(r, "EXINT6_0")) Then cboCrtl.AddItem ("P1.6")
         If (IsAlternatePinFree(r, "EXINT6_1")) Then cboCrtl.AddItem ("P4.2")
         If (IsAlternatePinFree(r, "EXINT6_2")) Then cboCrtl.AddItem ("P4.5")
         If (IsAlternatePinFree(r, "EXINT6_3")) Then cboCrtl.AddItem ("P3.4")
         If (IsAlternatePinFree(r, "EXINT6_4")) Then cboCrtl.AddItem ("P5.7")
         
    Case Else
    
  End Select
  
  For i = 0 To cboCrtl.ListCount - 1
    If (cboCrtl.List(i) = r.v(IntrName & "_USE_PIN")) Then
        AddExtPinGetIndex = i
    End If
  Next i

  If ((cboCrtl.ListCount) = 1) Then cboCrtl.Enabled = False

End Function

'*****************************************************************************
'Description:
'        This function releases the selected Int Pin
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        IntName      Name of the Int
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseExtIntPin(r As ProjectData, IntNum As Integer, ChangePin As Integer)
  On Error Resume Next
    
    '--select the pin to be released--
    Select Case (r.v("EXINT" & IntNum & "_USE_PIN"))
      Case ("P0.5")
             Call ReleaseAlternateInputPin(r, "EXINT0_0")
      Case ("P1.4")
             Call ReleaseAlternateInputPin(r, "EXINT0_1")
      
      Case ("P5.3")
             Call ReleaseAlternateInputPin(r, "EXINT1_0")
      Case ("P5.0")
             Call ReleaseAlternateInputPin(r, "EXINT1_1")
      
      Case ("P5.4")
             Call ReleaseAlternateInputPin(r, "EXINT2_0")
      Case ("P5.1")
             Call ReleaseAlternateInputPin(r, "EXINT2_1")
             
      Case ("P1.1")
             Call ReleaseAlternateInputPin(r, "EXINT3_0")
      Case ("P4.0")
             Call ReleaseAlternateInputPin(r, "EXINT3_1")
      Case ("P3.0")
             Call ReleaseAlternateInputPin(r, "EXINT3_2")
      Case ("P5.5")
             Call ReleaseAlternateInputPin(r, "EXINT3_3")
             
      Case ("P3.7")
             Call ReleaseAlternateInputPin(r, "EXINT4_0")
      Case ("P4.1")
             Call ReleaseAlternateInputPin(r, "EXINT4_1")
      Case ("P3.2")
             Call ReleaseAlternateInputPin(r, "EXINT4_2")
      Case ("P5.6")
             Call ReleaseAlternateInputPin(r, "EXINT4_3")
             
      Case ("P1.5")
             Call ReleaseAlternateInputPin(r, "EXINT5_0")
      Case ("P4.4")
             Call ReleaseAlternateInputPin(r, "EXINT5_1")
      Case ("P3.3")
             Call ReleaseAlternateInputPin(r, "EXINT5_2")
      Case ("P5.2")
             Call ReleaseAlternateInputPin(r, "EXINT5_3")
       
      Case ("P1.6")
             Call ReleaseAlternateInputPin(r, "EXINT6_0")
      Case ("P4.2")
             Call ReleaseAlternateInputPin(r, "EXINT6_1")
      Case ("P4.5")
             Call ReleaseAlternateInputPin(r, "EXINT6_2")
      Case ("P3.4")
             Call ReleaseAlternateInputPin(r, "EXINT6_3")
      Case ("P5.7")
             Call ReleaseAlternateInputPin(r, "EXINT6_4")
              
    End Select
    
        r.v("EXINT" & IntNum & "_USE_PIN") = "None"
    If (ChangePin = 1) Then
        If ((IntNum = 0) Or (IntNum = 1) Or (IntNum = 2)) Then
            r.v("IR_USE_IsrExt" & IntNum) = 0
            r.v(SCU_PREFIX & "EXICON0_EXINT" & IntNum) = 0
        ElseIf (((IntNum = 3) And (r.v("EXINT3_USE_PIN") = "None"))) Then
            r.v(SCU_PREFIX & "EXICON0_EXINT" & IntNum) = 0
        ElseIf (((IntNum = 4) And (r.v("EXINT4_USE_PIN") = "None")) Or ((IntNum = 5) And (r.v("EXINT5_USE_PIN") = "None"))) Then
            r.v(SCU_PREFIX & "EXICON1_EXINT" & IntNum) = 0
        ElseIf (((IntNum = 6) And (r.v("EXINT6_USE_PIN") = "None"))) Then
            r.v(SCU_PREFIX & "EXICON1_EXINT" & IntNum) = 0
        End If
    Else '
        If ((IntNum = 0) Or (IntNum = 1) Or (IntNum = 2)) Then
            r.v("IR_USE_IsrExt" & IntNum) = 0
            r.v(SCU_PREFIX & "MODPISEL_EXINT" & IntNum & "IS") = 0
           If ((IntNum = 0) Or (IntNum = 1)) Then
            r.v(SCU_PREFIX & "EXICON0_EXINT" & IntNum) = 0
           Else
            r.v(SCU_PREFIX & "EXICON0_EXINT" & IntNum) = 3
           End If
        ElseIf (((IntNum = 3) And (r.v("EXINT3_USE_PIN") = "None"))) Then
            r.v(SCU_PREFIX & "MODPISEL4_EXINT" & IntNum & "IS") = 0
            r.v(SCU_PREFIX & "EXICON0_EXINT" & IntNum) = 3
        ElseIf (((IntNum = 4) And (r.v("EXINT4_USE_PIN") = "None")) Or ((IntNum = 5) And (r.v("EXINT5_USE_PIN") = "None"))) Then
            r.v(SCU_PREFIX & "MODPISEL4_EXINT" & IntNum & "IS") = 0
            r.v(SCU_PREFIX & "EXICON1_EXINT" & IntNum) = 3
        ElseIf (((IntNum = 6) And (r.v("EXINT6_USE_PIN") = "None"))) Then
            r.v(SCU_PREFIX & "MODPISEL1_EXINT" & IntNum & "IS") = 0
            r.v(SCU_PREFIX & "EXICON1_EXINT" & IntNum) = 3
        End If
    End If

    If ((r.v("EXINT3_USE_PIN") = "None") And (r.v("EXINT4_USE_PIN") = "None") And (r.v("EXINT5_USE_PIN") = "None") And (r.v("EXINT6_USE_PIN") = "None")) Then
        r.v(CORE_PREFIX & "IEN1_EXM") = 0
        r.v("IR_USE_IsrExt3") = 0
    End If
    
End Sub

'*****************************************************************************
'Description:
'        This function set the selected Int pin for shift clock
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        IntName         Int name
'
'        PinNo           Selected pin
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetExtIntPin(r As ProjectData, IntName As String, PinNo As String)
  On Error Resume Next
   
  Select Case (IntName)
  
    Case ("EXINT0")
            If (PinNo = "P0.5") Then
                Call RequestAlternateInputPin(r, "EXINT0_0")
                 r.v(SCU_PREFIX & "MODPISEL_EXINT0IS") = 0
            ElseIf (PinNo = "P1.4") Then
                Call RequestAlternateInputPin(r, "EXINT0_1")
                 r.v(SCU_PREFIX & "MODPISEL_EXINT0IS") = 1
            End If
            
    Case ("EXINT1")
            If (PinNo = "P5.3") Then
                Call RequestAlternateInputPin(r, "EXINT1_0")
                 r.v(SCU_PREFIX & "MODPISEL_EXINT1IS") = 0
            ElseIf (PinNo = "P5.0") Then
                Call RequestAlternateInputPin(r, "EXINT1_1")
                 r.v(SCU_PREFIX & "MODPISEL_EXINT1IS") = 1
            End If

    Case ("EXINT2")
            If (PinNo = "P5.4") Then
                Call RequestAlternateInputPin(r, "EXINT2_0")
                 r.v(SCU_PREFIX & "MODPISEL_EXINT2IS") = 0
            ElseIf (PinNo = "P5.1") Then
                Call RequestAlternateInputPin(r, "EXINT2_1")
                 r.v(SCU_PREFIX & "MODPISEL_EXINT2IS") = 1
            End If

    Case ("EXINT3")
            If (PinNo = "P1.1") Then
                Call RequestAlternateInputPin(r, "EXINT3_0")
                r.v(SCU_PREFIX & "MODPISEL4_EXINT3IS") = 0
            ElseIf (PinNo = "P4.0") Then
                Call RequestAlternateInputPin(r, "EXINT3_1")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT3IS") = 1
            ElseIf (PinNo = "P3.0") Then
                Call RequestAlternateInputPin(r, "EXINT3_2")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT3IS") = 2
            ElseIf (PinNo = "P5.5") Then
                Call RequestAlternateInputPin(r, "EXINT3_3")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT3IS") = 3
            End If

    Case ("EXINT4")
            If (PinNo = "P3.7") Then
                Call RequestAlternateInputPin(r, "EXINT4_0")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT4IS") = 0
            ElseIf (PinNo = "P4.1") Then
                Call RequestAlternateInputPin(r, "EXINT4_1")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT4IS") = 1
            ElseIf (PinNo = "P3.2") Then
                Call RequestAlternateInputPin(r, "EXINT4_2")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT4IS") = 2
            ElseIf (PinNo = "P5.6") Then
                Call RequestAlternateInputPin(r, "EXINT4_3")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT4IS") = 3
            End If

    Case ("EXINT5")
            If (PinNo = "P1.5") Then
                Call RequestAlternateInputPin(r, "EXINT5_0")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT5IS") = 0
            ElseIf (PinNo = "P4.4") Then
                Call RequestAlternateInputPin(r, "EXINT5_1")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT5IS") = 1
            ElseIf (PinNo = "P3.3") Then
                Call RequestAlternateInputPin(r, "EXINT5_2")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT5IS") = 2
            ElseIf (PinNo = "P5.2") Then
                Call RequestAlternateInputPin(r, "EXINT5_3")
                 r.v(SCU_PREFIX & "MODPISEL4_EXINT5IS") = 3
            End If
            
    Case ("EXINT6")
            If (PinNo = "P1.6") Then
                Call RequestAlternateInputPin(r, "EXINT6_0")
                 r.v(SCU_PREFIX & "MODPISEL1_EXINT6IS") = 0
            ElseIf (PinNo = "P4.2") Then
                Call RequestAlternateInputPin(r, "EXINT6_1")
                 r.v(SCU_PREFIX & "MODPISEL1_EXINT6IS") = 1
            ElseIf (PinNo = "P4.5") Then
                Call RequestAlternateInputPin(r, "EXINT6_2")
                 r.v(SCU_PREFIX & "MODPISEL1_EXINT6IS") = 2
            ElseIf (PinNo = "P3.4") Then
                Call RequestAlternateInputPin(r, "EXINT6_3")
                 r.v(SCU_PREFIX & "MODPISEL1_EXINT6IS") = 3
            ElseIf (PinNo = "P5.7") Then
                Call RequestAlternateInputPin(r, "EXINT6_4")
                 r.v(SCU_PREFIX & "MODPISEL1_EXINT6IS") = 4
            End If

 End Select

End Sub


'***************************************************************************
'***************************************************************************

Sub SetEdgeSelection(r As ProjectData, IntrNum As Integer, SetNo As String)
  On Error Resume Next
   
    Select Case (SetNo)
        Case ("Interrupt on falling edge")
            If ((IntrNum = 0) Or (IntrNum = 1) Or (IntrNum = 2) Or (IntrNum = 3)) Then
              r.v(SCU_PREFIX & "EXICON0_EXINT" & IntrNum) = 0
            Else
              r.v(SCU_PREFIX & "EXICON1_EXINT" & IntrNum) = 0
            End If
            
         Case ("Interrupt on rising edge")
            If ((IntrNum = 0) Or (IntrNum = 1) Or (IntrNum = 2) Or (IntrNum = 3)) Then
              r.v(SCU_PREFIX & "EXICON0_EXINT" & IntrNum) = 1
            Else
              r.v(SCU_PREFIX & "EXICON1_EXINT" & IntrNum) = 1
            End If
            
         Case ("Interrupt on both rising and falling edge")
            If ((IntrNum = 0) Or (IntrNum = 1) Or (IntrNum = 2) Or (IntrNum = 3)) Then
              r.v(SCU_PREFIX & "EXICON0_EXINT" & IntrNum) = 2
            Else
              r.v(SCU_PREFIX & "EXICON1_EXINT" & IntrNum) = 2
            End If
            
         Case ("Bypass the edge detection")
            If ((IntrNum = 0) Or (IntrNum = 1)) Then
              r.v(SCU_PREFIX & "EXICON0_EXINT" & IntrNum) = 3
            End If
            
         Case ("External interrupt " & IntrNum & " is disabled")
            If ((IntrNum = 2) Or (IntrNum = 3)) Then
              r.v(SCU_PREFIX & "EXICON0_EXINT" & IntrNum) = 3
            Else
              r.v(SCU_PREFIX & "EXICON1_EXINT" & IntrNum) = 3
            End If

    End Select
End Sub

'***************************************************************************
'***************************************************************************

Function AddEdgeSeleIndex(r As ProjectData, IntrNum As Integer, cboCrtl As Variant) As Integer
On Error Resume Next
    'commented and modified by sankar on 10 Oct'08
    If ((IntrNum = 3) And (r.v("T2CCU_CCEN_CCM0") = 2)) Or ((IntrNum = 4) And (r.v("T2CCU_CCEN_CCM1") = 2)) Or _
       ((IntrNum = 5) And (r.v("T2CCU_CCEN_CCM2") = 2)) Or ((IntrNum = 6) And (r.v("T2CCU_CCEN_CCM3") = 2)) Then
         cboCrtl.AddItem ("Interrupt on rising edge")
    Else
'Below are common to all
         cboCrtl.AddItem ("Interrupt on falling edge")
         cboCrtl.AddItem ("Interrupt on rising edge")
         cboCrtl.AddItem ("Interrupt on both rising and falling edge")
    End If

    If ((IntrNum = 0) Or (IntrNum = 1)) Then
         cboCrtl.AddItem ("Bypass the edge detection")
    Else 'If ((IntrNum = 2) Or (IntrNum = 3) Or (IntrNum = 4) Or (IntrNum = 5) Or (IntrNum = 6)) Then
      If ((IntrNum = 3) And (r.v("T2CCU_CCEN_CCM0") = 2)) Or ((IntrNum = 4) And (r.v("T2CCU_CCEN_CCM1") = 2)) Or _
         ((IntrNum = 5) And (r.v("T2CCU_CCEN_CCM2") = 2)) Or ((IntrNum = 6) And (r.v("T2CCU_CCEN_CCM3") = 2)) Then
      Else
         cboCrtl.AddItem ("External interrupt " & IntrNum & " is disabled")
      End If
    End If
    

    If ((IntrNum = 3) And (r.v("T2CCU_CCEN_CCM0") = 2)) Or ((IntrNum = 4) And (r.v("T2CCU_CCEN_CCM1") = 2)) Or _
       ((IntrNum = 5) And (r.v("T2CCU_CCEN_CCM2") = 2)) Or ((IntrNum = 6) And (r.v("T2CCU_CCEN_CCM3") = 2)) Then

       AddEdgeSeleIndex = 0
    Else
      If ((IntrNum = 0) Or (IntrNum = 1) Or (IntrNum = 2) Or (IntrNum = 3)) Then
        AddEdgeSeleIndex = r.v(SCU_PREFIX & "EXICON0_EXINT" & IntrNum)
      Else
        AddEdgeSeleIndex = r.v(SCU_PREFIX & "EXICON1_EXINT" & IntrNum)
      End If
    End If
End Function


'***************************************************************************
'***************************************************************************

Sub SetLvlTrigerSelection(r As ProjectData, IntName As String, SetNo As String)
  On Error Resume Next
     
    Select Case (SetNo)
         Case ("Low level triggered")
            If (IntName = "EXINT0") Then
              r.v(SCU_PREFIX & "TCON_IT0") = 0
            Else
              r.v(SCU_PREFIX & "TCON_IT1") = 0
            End If
         Case ("Falling edge triggered")
            If (IntName = "EXINT0") Then
              r.v(SCU_PREFIX & "TCON_IT0") = 1
            Else
              r.v(SCU_PREFIX & "TCON_IT1") = 1
            End If
    End Select
End Sub

'***************************************************************************
'***************************************************************************

Function AddLvlTrgIndex(r As ProjectData, IntrName As Integer, cboCrtl As Variant) As Integer
On Error Resume Next

  cboCrtl.Clear
  
  If ((IntrName = 0) Or (IntrName = 1)) Then
        cboCrtl.AddItem ("Low level triggered")
        cboCrtl.AddItem ("Falling edge triggered")
  End If
  
  AddLvlTrgIndex = r.v(SCU_PREFIX & "TCON_IT" & IntrName)
  
End Function
