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

Option Explicit

'*****************************************************************************
' Module:
'        CAPCOMx
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        CAPCOMx dialog script and in the CAPCOMx codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         05.01.01     final version for S-Gold
'
'        ---------------------------------------------------------------------
'        3.0   ahe         02.05.01     final version for Pegasus
'
'        3.1   ahe         02.07.02     reset OUT-bits in function SetChannelxToDefault
'        3.2   ste         18.12.02     no changes
'                          22.01.03     bits PDS and ORSEL deleted
'
'        ---------------------------------------------------------------------
'        5.0  ahe          26.08.04     final version for XC164CM Pegasus light
'
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************
Global Const CH_HIGH = 8

'*****************************************************************************
'Description:
'          This function returns the module clock [MHz] of the
'          CAPCOM module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'      GetCAPCOMSystemClock [MHz]
'
'*****************************************************************************
Function GetCAPCOMModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim v       As String
  
  sVarPre = "CAP" & vIndex
  sRegPre = sVarPre

  GetCAPCOMModuleClock = 1
  GetCAPCOMModuleClock = GetSystemClock(r)

End Function


'*****************************************************************************
'Description:
'        This function is called from the ShowPage function
'        and returns formated text for the cboTxI.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iExp            der Index aus der For - Next - Schleife
'-----------------------------------------------------------------------------
'Returns:
'        formated text for the cboTxI
'*****************************************************************************
Function GetPrescalerResolution(r As ProjectData, vIndex As Variant, iExp As Integer) As String
  On Error Resume Next
  Dim Exponent  As Integer
  Dim sVarPre   As String
  Dim sRegPre   As String

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"
  
  GetPrescalerResolution = ""
  Exponent = iExp + 3 * (1 - r.v(sRegPre & "IOC_STAG"))
  GetPrescalerResolution = "Module Clock / " & 2 ^ Exponent
  GetPrescalerResolution = GetPrescalerResolution & " (Resolution: " & _
    Format$(2 ^ Exponent / GetCAPCOMModuleClock(r, vIndex), "0.000") & " s)"

  
End Function


'*****************************************************************************
'Description:
'        This function is called from the CAPCOM module
'        and calculate overflow time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr (e.g. 0 or 1 or 7 or 8)
'-----------------------------------------------------------------------------
'Returns:
'        overflow time [s]
'
'*****************************************************************************
Function GetOverflow(r As ProjectData, vIndex As Variant, iTmr As Integer) As Double
  On Error Resume Next
  Dim i       As Integer
  Dim s       As String
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  If (vIndex = 1) Then
      s = "01"
  Else
      s = "78"
  End If

  i = r.v(sRegPre & "T" & s & "CON_T" & iTmr & "I")

  GetOverflow = (65536 - r.v(sRegPre & "T" & iTmr & "REL_T" & iTmr & "REL")) * _
                ((8 ^ (1 - r.v(sRegPre & "IOC_STAG")) * 2 ^ (i)) / _
                GetCAPCOMModuleClock(r, vIndex))


End Function

'*****************************************************************************
'Description:
'        This function is called from the CAPCOMx module
'        and calculate the maximum resolution for timer clock.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (0,1,7 or 8)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [s]
'
'*****************************************************************************
Function GetMaxResolution(r As ProjectData, vIndex As Variant, iTmr As Integer) As Double
  On Error Resume Next
  Dim s       As String
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  If (vIndex = 1) Then
      s = "01"
  Else
      s = "78"
  End If
  
  GetMaxResolution = 65536 * 8 ^ (1 - r.v(sRegPre & "IOC_STAG")) * _
                     2 ^ (r.v(sRegPre & "T" & s & "CON_T" & iTmr & "I")) / _
                     GetCAPCOMModuleClock(r, vIndex)

End Function

'*****************************************************************************
'Description:
'        This function is called from the CAPCOMx module
'        and calculate the maximum resolution for timer clock.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (0,1,7 or 8)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [s]
'
'*****************************************************************************
Function GetMinResolution(r As ProjectData, vIndex As Variant, iTmr As Integer) As Double
  On Error Resume Next
  Dim s       As String
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  If (vIndex = 1) Then
      s = "01"
  Else
      s = "78"
  End If
  
  GetMinResolution = 8 ^ (1 - r.v(sRegPre & "IOC_STAG")) * _
                     2 ^ (r.v(sRegPre & "T" & s & "CON_T" & iTmr & "I")) / _
                     GetCAPCOMModuleClock(r, vIndex)

End Function

'*****************************************************************************
'Description:
'        This function is called from the OnShowPage function
'        and calculate the value of the timer register TxREL.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        t               entered time [s]
'        rIndex
'             True       for overflow time
'             False      for underflow time
'        TimerNr
'-----------------------------------------------------------------------------
'Returns:
'        Value of TxREL
'
'*****************************************************************************
Function GetTimerNrValue(r As ProjectData, vIndex As Variant, t As Variant, rIndex As Boolean, iTmr As Integer) As Long
  On Error Resume Next
  Dim i       As Integer
  Dim s       As String
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  If (vIndex = 1) Then
      s = "01"
  Else
      s = "78"
  End If
  
  If (t > GetMaxResolution(r, vIndex, iTmr)) Then
    t = GetMaxResolution(r, vIndex, iTmr)
  End If
  If (t < GetMinResolution(r, vIndex, iTmr)) Then
    t = GetMinResolution(r, vIndex, iTmr)
  End If

  
  For i = 0 To 7
    If r.v(sRegPre & "T" & s & "CON_T" & iTmr & "I") = i Then
      If (rIndex) Then
        GetTimerNrValue = 65536 - t / ((8 ^ (1 - r.v(sRegPre & "IOC_STAG")) * _
                          2 ^ (i)) / GetCAPCOMModuleClock(r, vIndex))
        Exit Function
      Else
        GetTimerNrValue = t / ((8 ^ (1 - r.v(sRegPre & "IOC_STAG")) * _
                          2 ^ (i)) / GetCAPCOMModuleClock(r, vIndex)) - 1
        Exit Function
      End If
    End If
  Next i

End Function


'*****************************************************************************
'Description:
'        This function is called from the OnShowPage function
'        and set the CACPCOM alternate pin as input. If pin is output
'        then pin release and then set as input.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iChannel        number of the channel
'-----------------------------------------------------------------------------
'Returns:
'        none
'
'*****************************************************************************
Sub RequestCCAlternateInputPin(r As ProjectData, vIndex As Variant, iChannel As Integer)
    On Error Resume Next

    If (r.Exist("MuxFor_CC" & vIndex & "_CC" & iChannel & "IO_O") = True) Then    'if CCxIO can used as output
        If (GetPinFunction(r, "CC" & vIndex & "_CC" & iChannel & "IO_O") = 2) Then
            Call ReleaseAlternateOutputPin(r, "CC" & vIndex & "_CC" & iChannel & "IO_O")
        End If
    End If

    If (GetPinFunction(r, "CC" & vIndex & "_CC" & iChannel & "IO_I") = 0) Then
        Call RequestAlternateInputPin(r, "CC" & vIndex & "_CC" & iChannel & "IO_I")
    End If

End Sub

'*****************************************************************************
'Description:
'        This function is called from the OnShowPage function
'        and set the CACPCOM alternate pin as output. If pin is input
'        then pin release and then set as output.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iChannel        number of the channel
'-----------------------------------------------------------------------------
'Returns:
'        none
'
'*****************************************************************************
Sub RequestCCAlternateOutputPin(r As ProjectData, vIndex As Variant, iChannel As Integer)
    On Error Resume Next

    If (GetPinFunction(r, "CC" & vIndex & "_CC" & iChannel & "IO_I") = 1) Then
        Call ReleaseAlternateInputPin(r, "CC" & vIndex & "_CC" & iChannel & "IO_I")
    End If

    If (r.Exist("MuxFor_CC" & vIndex & "_CC" & iChannel & "IO_O") = True) Then    'if CCxIO can used as output
        If (GetPinFunction(r, "CC" & vIndex & "_CC" & iChannel & "IO_O") = 0) Then
            Call RequestAlternateOutputPin(r, "CC" & vIndex & "_CC" & iChannel & "IO_O")
        End If
    End If

End Sub

'*****************************************************************************
'Description:
'        This function is called from the OnShowPage function
'        and release the CACPCOM alternate pins (from output or input).
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iChannel        number of the channel
'-----------------------------------------------------------------------------
'Returns:
'        none
'
'*****************************************************************************
Sub ReleaseCCAlternatePin(r As ProjectData, vIndex As Variant, iChannel As Integer)
    On Error Resume Next

    If (GetPinFunction(r, "CC" & vIndex & "_CC" & iChannel & "IO_I") = 1) Then
        Call ReleaseAlternateInputPin(r, "CC" & vIndex & "_CC" & iChannel & "IO_I")
    End If

    If (r.Exist("MuxFor_CC" & vIndex & "_CC" & iChannel & "IO_O") = True) Then   'if CCxIO can used as output
        If (GetPinFunction(r, "CC" & vIndex & "_CC" & iChannel & "IO_O") = 2) Then
            Call ReleaseAlternateOutputPin(r, "CC" & vIndex & "_CC" & iChannel & "IO_O")
        End If
    End If

End Sub

'*****************************************************************************
'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 sVarPre As String
  Dim sRegPre As String

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"
  
  IsModuleClockDisabled = False
  IsModuleClockDisabled = r.v("SYSCON3_CC" & vIndex & "DIS") = 1
End Function


'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipModuleClockDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  Dim sIndex  As String
  Dim sPrefix As String

  sIndex = "CC" & vIndex
  sPrefix = sIndex & ""
  
  GetTooltipModuleClockDisabled = ""
  If (r.v("SYSCON3_CC" & vIndex & "DIS") = 1) Then
    GetTooltipModuleClockDisabled = GetTooltipModuleClockDisabled & _
      FormatDisabledToolTip("the module is disabled")
  End If

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
'        "CAPCOMx_NAME_Functionname" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'        iISR             interrupt number (0 if no interrupt, else ISR + 1)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
'Public Function RefreshFunctionDescriptions(vIndex As Variant, func As FunctionClass, r As ProjectData) As Boolean
Public Function RefreshFunctionDescriptions(vIndex As Variant, func As FunctionClass, r As ProjectData, iISR As Integer) As Boolean
  On Error Resume Next

  RefreshFunctionDescriptions = False

  Dim sDescription  As String
  Dim sReturnValue  As String
  Dim sVarPre       As String
  Dim sFctName      As String
  Dim i             As Integer
  Dim iCh_low       As Integer
  Dim iCh_high      As Integer
  Dim iTmr_x        As Integer
  Dim iTmr_y        As Integer

  sVarPre = "CC" & vIndex

  If (vIndex = 1) Then
      iCh_low = 0
      iCh_high = 7 + CH_HIGH
      iTmr_x = 0
      iTmr_y = 1
  Else
      iCh_low = 16
      iCh_high = 23 + CH_HIGH
      iTmr_x = 7
      iTmr_y = 8
  End If

  ' 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, 3 => TRAP)

    ' 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


  If (iISR = 0) Then
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_Init
      '------------------------------------------------------------------------------------------------
      sDescription = GetGeneralInitDescription("CAPCOM" & vIndex)
  
      sReturnValue = "None"
  
      If (Not func.AddFunction(sVarPre & "_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "StartTmr"
      sDescription = "This macro starts the selected CAPCOM" & vIndex & " timer. " & _
                     "The timer continues counting from where it stopped. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions for TimerNr are available: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " timer to be started"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "StopTmr"
      sDescription = "This macro stops the selected CAPCOM" & vIndex & " timer. " & _
                     "The content of the timer register remains unchanged. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions for TimerNr are available: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " timer to be stopped"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "ClearTmr"
      sDescription = "This macro stops the selected CAPCOM" & vIndex & " timer and " & _
                     "sets the timer register to 0. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions for TimerNr are available: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " timer to be cleared"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "ReadTmr"
      sDescription = "This macro returns the content of the CAPCOM" & vIndex & " timer. The " & _
                     "timer is not stopped. CPU access to the timer register " & _
                     "has a higher priority than a timer increment or a reload. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions for TimerNr are available: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "Current 16-bit value of the timer register"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " timer to be readed"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "LoadTmr"
      sDescription = "This macro loads the timer register of the selected CAPCOM" & vIndex & " " & _
                     "timer with the requested value. The timer need not be " & _
                     "stopped for this happen as a CPU access to the timer register " & _
                     "has a higher priority than a timer increment or a reload. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions for TimerNr are available: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " timer to be loaded"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
        sDescription = "16-bit Value with which the CAPCOM" & vIndex & " timer is to be loaded"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "Value", "", sDescription)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "SetReload"
      sDescription = "This macro loads the reload register of the selected CAPCOM" & vIndex & " timer." & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions for TimerNr are available: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & vbCrLf & _
                     r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " timer to be used"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
        sDescription = "16-bit Value with which the CAPCOM" & vIndex & " timer reload register is to be loaded"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "Value", "", sDescription)) Then Exit Function

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "SetCCxReg"
      sDescription = "This macro loads the selected CAPCOM" & vIndex & " register " & _
                     "with the parameter Value. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions are available for RegName " & _
                     "(only for enabled CAPCOM" & vIndex & " channels): " & vbCrLf & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_high) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " channel to be loaded"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegName", "", sDescription)) Then Exit Function
        sDescription = "16-bit Value with which the CAPCOM" & vIndex & " register is to be loaded"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "Value", "", sDescription)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "GetCCxReg"
      sDescription = "This macro returns the content of the selected CAPCOM" & vIndex & " register." & _
                     "" & vbCrLf & _
                     "The following definitions are available for RegName " & _
                     "(only for enabled CAPCOM" & vIndex & " channels): " & vbCrLf & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_high) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "Current 16-bit value of the CAPCOM" & vIndex & " register"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " channel to be readed"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegName", "", sDescription)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "PortLock"
      sDescription = "After the call of this macro the contents of the port register can not " & _
                     "changed by the CAPCOM" & vIndex & "."

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
     
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "PortUnlock"
      sDescription = "After the call of this macro the contents of the port register can " & _
                     "changed by the CAPCOM" & vIndex & "."

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
     
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "EnableSingleEvent"
      sDescription = "This macro enables a single event for the selected CAPCOM" & vIndex & " channel. " & _
                     "Use this macro only for channels where the single event mode is enabled." & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions are available for Channel: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_high) & vbCrLf & _
                     "(see @Defines in header file)"


      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Number of the CAPCOM" & vIndex & " channel"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "Channel", "", sDescription)) Then Exit Function
     
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "SelectDoubleRegisterCompareMode"
      sDescription = "This macro selects the double register mode for the selected CAPCOM" & vIndex & " channel. " & _
                     "Use this macro only for channels where the double register compare mode is enabled." & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions are available for Channel: " & vbCrLf & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_low + 8) & " .. " & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_high) & vbCrLf & _
                     "(see @Defines in header file)" & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions are available for Mode: " & vbCrLf & _
                     "0 - mode is controlled via channel of bank 1" & vbCrLf & _
                     "1 - mode is disabled" & vbCrLf & _
                     "2 - mode is enabled" & vbCrLf & _
                     ""

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Number of the CAPCOM" & vIndex & " channel"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "Channel", "", sDescription)) Then Exit Function
        sDescription = "Double register mode"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "Mode", "", sDescription)) Then Exit Function
     
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "SetCCxOUT"
      sDescription = "This macro sets the output of the selected CAPCOM" & vIndex & " channel to 1. " & _
                     "If the output register is connected to a port pin then the pin is set to 1 too. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions are available for Channel " & _
                     "(only for enabled CAPCOM" & vIndex & " channels): " & vbCrLf & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_high) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " channel whose output should be set"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "Channel", "", sDescription)) Then Exit Function

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "ResetCCxOUT"
      sDescription = "This macro sets the output of the selected CAPCOM" & vIndex & " channel to 0. " & _
                     "If the output register is connected to a port pin then the pin is set to 0 too. " & vbCrLf & _
                     "" & vbCrLf & _
                     "The following definitions are available for Channel " & _
                     "(only for enabled CAPCOM" & vIndex & " channels): " & vbCrLf & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_high) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " channel whose output should be reset"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "Channel", "", sDescription)) Then Exit Function

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "GetCCxOUT"
      sDescription = "This macro returns the output state of selected CAPCOM" & vIndex & " channel. " & _
                     "" & vbCrLf & _
                     "The following definitions are available for Channel " & _
                     "(only for enabled CAPCOM" & vIndex & " channels): " & vbCrLf & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & _
                     r.v(sVarPre & "_PARA_CC_" & iCh_high) & vbCrLf & _
                     "(see @Defines in header file)"

      sReturnValue = "Current value of the CAPCOM" & vIndex & " output"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "Name of the CAPCOM" & vIndex & " channel whose output should be read"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "Channel", "", sDescription)) Then Exit Function

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "Tmr" & iTmr_x
      sDescription = "This is the interrupt service routine for the CAPCOM" & vIndex & " timer " & iTmr_x & ". " & _
                     "It is called when overflow of the timer " & iTmr_x & " register occurs."

                     sDescription = sDescription & vbCrLf & TOOLTIP_ISR_USERCODE

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 2)) Then Exit Function
 
      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "Tmr" & iTmr_y
      sDescription = "This is the interrupt service routine for the CAPCOM" & vIndex & " timer " & iTmr_y & ". " & _
                     "It is called when overflow of the timer " & iTmr_y & " register occurs."

                     sDescription = sDescription & vbCrLf & TOOLTIP_ISR_USERCODE

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 2)) Then Exit Function

'      '------------------------------------------------------------------------------------------------
'      ' Description for CAPCOMx_NAME_...
'      '------------------------------------------------------------------------------------------------
'      sFctName = "SetPortDirectionOutput"
'      sDescription = "This macro sets the port direction of " & sVarPre & " to output." & _
'                     "" & vbCrLf & _
'                     "Note: Use this macro only if " & sVarPre & " is neither in capture nor compare mode."
'
'      sReturnValue = "None"
'      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
'
'      '------------------------------------------------------------------------------------------------
'      ' Description for CAPCOMx_NAME_...
'      '------------------------------------------------------------------------------------------------
'      sFctName = "SetPortDirectionInput"
'      sDescription = "This macro sets the port direction of " & sVarPre & " to input. " & _
'                     "This mode allows to read and write directly to the CCx registers and use these as " & _
'                     "a type of port registers." & _
'                     "" & vbCrLf & _
'                     "Note: Use this macro only if " & sVarPre & " is neither in capture nor compare mode."
'
'      sReturnValue = "None"
'      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
'

  End If
 
  
  If (iISR > 0) Then
      i = iISR - 1

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "CC" & i
      sDescription = "This is the interrupt service routine for the CAPCOM" & vIndex & _
                     " register CC" & Format(i) & ". If the content of the corresponding " & _
                     "compare timer (configurable) equals the content of the " & _
                     "capture/compare register CC" & Format(i) & " or if a capture event " & _
                     "occurs at the associated port pin, the interrupt request " & _
                     "flag is set and an interrupt is triggered " & _
                     "(only if enabled)."

                     sDescription = sDescription & vbCrLf & TOOLTIP_ISR_USERCODE

      sReturnValue = "None"
      If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 2)) Then Exit Function

  End If


        If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = 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
  Dim iCh_low       As Integer
  Dim iCh_high      As Integer
  Dim iTmr_x        As Integer
  Dim iTmr_y        As Integer

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  If (vIndex = 1) Then
      iCh_low = 0
      iCh_high = 7 + CH_HIGH
      iTmr_x = 0
      iTmr_y = 1
  Else
      iCh_low = 16
      iCh_high = 23 + CH_HIGH
      iTmr_x = 7
      iTmr_y = 8
  End If


  ' 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. SSC_PARA_MODE)
  ' sParameterDescription  | String | description of the parameter

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies CAPCOM" & vIndex & " timer " & iTmr_x
  sParaName = "TIMER_" & iTmr_x
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies CAPCOM" & vIndex & " timer " & iTmr_y
  sParaName = "TIMER_" & iTmr_y
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  For i = iCh_low To iCh_high
      '------------------------------------------------------------------------------------------------
      ' Description for DUMMY_PARA_xx
      '------------------------------------------------------------------------------------------------
      sDescription = "This parameter identifies CAPCOM" & vIndex & " channel " & i
      sParaName = "CC_" & i
      If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  Next i
        
        If (Not para.Sort) Then Exit Function

  RefreshParameterDescriptions = True
End Function


'*****************************************************************************
'Description:
'        This function sets the CCxDRM_DRyM = 0.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iCh_Group       for channel 0 ..3/16 .. 19 iCh_Group = 1
'                        for channel 4 ..7/20 .. 23 iCh_Group = 2
'        iChannel        channel number
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub DeselectDRM(r As ProjectData, vIndex As Variant, iCh_Group As Integer, iChannel As Integer)

    Dim sRegPre As String

    sRegPre = "CC" & vIndex & "_"
    
    If (iCh_Group = 2) Then
        r.v(sRegPre & "DRM_DR" & iChannel - 8 & "M") = 0
    End If

End Sub

'*****************************************************************************
'Description:
'        This function sets the CAPCOMx channel y to default
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iCh_Group       for channel 0 ..3/16 .. 19 iCh_Group = 1
'                        for channel 4 ..7/20 .. 23 iCh_Group = 2
'        iChannel        channel number
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub SetChannelxToDefault(r As ProjectData, vIndex As Variant, iCh_Group As Integer, iChannel As Integer)
    Dim sRegPre   As String
    Dim sVarPre   As String
    Dim lRegPre   As Long
    Dim sCCx      As String
    Dim iChannel_for_CC2 As Integer

    If (vIndex = 1) Then
        iChannel_for_CC2 = iChannel
    Else
        iChannel_for_CC2 = iChannel - 16
    End If

    lRegPre = iChannel \ 4
    sVarPre = "CC" & vIndex
    sRegPre = sVarPre & "_"
    sCCx = sRegPre & "M" & lRegPre & "_"

    r.v(sCCx & "MOD" & iChannel) = 0
    r.v(sVarPre & "_CC" & iChannel & "_Use") = 0
    r.v(sCCx & "ACC" & iChannel) = 0
    r.v(sRegPre & "SEM_SEM" & iChannel_for_CC2) = 0
    r.v(sRegPre & "SEE_SEE" & iChannel_for_CC2) = 0

    If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
        
        Call ReleaseCCAlternatePin(r, vIndex, iChannel)
    End If

    Call DisableSRN(r, sVarPre, sRegPre & "CC" & iChannel & "IC", "CC" & iChannel)
    r.v(sVarPre & "_Int" & iChannel & "_Use") = 0

    Call DeselectDRM(r, vIndex, iCh_Group, iChannel_for_CC2)
    r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 0


End Sub
