Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
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
'
'*****************************************************************************


'*****************************************************************************
' 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 = &H43
    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) As String
    On Error Resume Next
    ExtIntId = "EXTERNAL_INT" & iIndex
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) As String
    On Error Resume Next
    PeriIntId = ""
    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"
    If iIndex = 5 Then PeriIntId = "T0_INT"
    If iIndex = 6 Then PeriIntId = "T1_INT"
    If iIndex = 7 Then PeriIntId = "T2_OVERFLOW_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)) & ", "
  Next iInt

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

  '------------------------------------------------------------------------------------------------
  ' 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

