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         18.05.01     final version for XC164CS
'
'        1.1   ahe         05.06.02     - Function description of SetTmrPeriod
'                                         corrected
'
'              ahe         06.08.02     - reset dead time generation if hall
'                                         sensor mode is selected
'        1.2   ste         18.12.02     final version for XC167CI

'        ---------------------------------------------------------------------
'        2.0               26.08.04    final version for XC164CM Pegasus light
'        2.1  Sheshadri    04.07.05    T12/13 Prescalar value is added
'					acording to UM_v1.0_2005_06_per.pdf
'
'*****************************************************************************


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

'*****************************************************************************
'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
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(vIndex As Variant, func As FunctionClass, r As projectdata) 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_12 As Integer
  Dim iTmr_13 As Integer

  sVarPre = "CCU" & vIndex

  iCh_low = 0
  iCh_high = 3
  iTmr_12 = 12
  iTmr_13 = 13

  ' 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


  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription(sVarPre)
  
  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 " & sVarPre & " 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_12) & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_13)

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " 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 " & sVarPre & " 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_12) & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_13)

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " timer to be stopped"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "ResetTmr"
  sDescription = "This macro sets the selected " & sVarPre & " timer " & _
                 "counter register to zero. Timer is not stopped." & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions for TimerNr are available: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_12) & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_13)

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " timer to be cleared"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "SetTmrPeriod"
  sDescription = "This macro loads the period register of the selected " & sVarPre & _
                 " timer." & vbCrLf & vbCrLf & _
                 "Note: Call the macro " & r.v(sVarPre & "_NAME_EnableShadowTransfer") & "() to " & _
                 "enable the shadow transfer." & _
                 "" & vbCrLf & _
                 "The following definitions for TimerNr are available: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_12) & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_13)

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
    sDescription = "16-bit Value with which the " & sVarPre & " 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 = "SetDeadTimePeriod"
  sDescription = "This macro loads the counter of the deat time register."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "counter value (ubValue = 0x01 .. 0x3F)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubValue", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "EnableShadowTransfer"
  sDescription = "This macro enables the transfer from the shadow registers to " & _
                 "the actual " & sVarPre & " registers." & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions for TimerNr are available: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_12) & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_13)

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "LoadChannelShadowRegister"
  sDescription = "This macro loads the selected " & sVarPre & " channel shadow register " & _
                 "with the parameter Value. " & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions are available for RegName: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_low) & " .. " & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_high) & _
                 " (see @Defines in header file)"

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

  sReturnValue = "Current 16-bit value of the " & sVarPre & " shadow register"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " channel to be readed"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegName", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "GetChannelRegister"
  sDescription = "This macro returns the content of the " & sVarPre & _
                 " capture/compare register of the selected channel." & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions are available for RegName: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_low) & " .. " & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_high) & _
                 " (see @Defines in header file)"

  sReturnValue = "Current 16-bit value of the " & sVarPre & " register"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " channel to be readed"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegName", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "GetTmrCounter"
  sDescription = "This macro returns the contens of the selected " & sVarPre & _
                 " timer counter register." & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions for TimerNr are available: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_12) & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_13)

  sReturnValue = "16-bit counter value"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "SetTmrCounter"
  sDescription = "This macro loads the selected " & sVarPre & _
                 " timer counter register with the parameter Value . " & _
                 "It is possible to use this macro " & _
                 "only if the selected timer is stopped." & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions for TimerNr are available: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_12) & vbCrLf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_13)

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


  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "SetStatusBit"
  sDescription = "This macro sets the capture / compare modifikation status bit of the selected " & _
                 sVarPre & " channel. " & _
                 "" & vbCrLf & _
                 "The following definitions are available for ChName: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_low) & " .. " & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_high) & _
                 " (see @Defines in header file)"

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " channel to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ChName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "ResetStatusBit"
  sDescription = "This macro resets the capture / compare modifikation status bit of the selected " & _
                 sVarPre & " channel. " & _
                 "" & vbCrLf & _
                 "The following definitions are available for ChName: " & vbCrLf & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_low) & " .. " & _
                 r.v(sVarPre & "_PARA_CHANNEL_" & iCh_high) & _
                 " (see @Defines in header file)"

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sVarPre & " channel to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ChName", "", sDescription)) Then Exit Function


  For i = 0 To 3

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "NodeI" & i
      sDescription = "This is the interrupt service routine for the " & sVarPre & _
                     " node I" & Format(i) & ". If the content of the corresponding " & _
                     "compare timer (configurable) equals the content of the " & _
                     "capture/compare register 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

  Next i


        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_12 As Integer
  Dim iTmr_13 As Integer

  sVarPre = "CCU" & vIndex
  sRegPre = sVarPre & "_"

  iCh_low = 0
  iCh_high = 3
  iTmr_12 = 12
  iTmr_13 = 13


  ' 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 " & sVarPre & " timer " & iTmr_12
  sParaName = "TIMER_" & iTmr_12
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies " & sVarPre & " timer " & iTmr_13
  sParaName = "TIMER_" & iTmr_13
  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 " & sVarPre & " channel " & i
      sParaName = "CHANNEL_" & 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 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 = GetPeripheralClock(r)

End Function


'*****************************************************************************
'Description:
'        This function is called from the CAPCOM module
'        and calculate overflow time fot DTM.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr (12)
'-----------------------------------------------------------------------------
'Returns:
'        overflow time [s]
'
'*****************************************************************************
Function GetOverflowDTM(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 = "CCU" & vIndex
  sRegPre = sVarPre & "_"

        If r.v(sRegPre & "TCTR0_T" & iTmr & "PRE") = 0 Then
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")
        Else
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK") + 8
        End If

'  i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")

  GetOverflowDTM = r.l(sRegPre & "T12DTC_DTM") * 2 ^ (i) / GetCAPCOMModuleClock(r, vIndex)


End Function

'*****************************************************************************
'Description:
'        This function is called from the CAPCOMx module
'        and calculate the maximum resolution for dead time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [s]
'
'*****************************************************************************
Function GetMaxResolutionDTM(r As projectdata, vIndex As Variant, iTmr As Integer) As Double
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

    Dim i As Integer
    
  sVarPre = "CCU" & vIndex
  sRegPre = sVarPre & "_"

        If r.v(sRegPre & "TCTR0_T" & iTmr & "PRE") = 0 Then
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")
        Else
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK") + 8
        End If


  GetMaxResolutionDTM = 63 * 2 ^ (i) / _
                        GetCAPCOMModuleClock(r, vIndex)

End Function

'*****************************************************************************
'Description:
'        This function is called from the CAPCOMx module
'        and calculate the maximum resolution for dead time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [s]
'
'*****************************************************************************
Function GetMinResolutionDTM(r As projectdata, vIndex As Variant, iTmr As Integer) As Double
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

Dim i As Integer

  sVarPre = "CCU" & vIndex
  sRegPre = sVarPre & "_"

        If r.v(sRegPre & "TCTR0_T" & iTmr & "PRE") = 0 Then
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")
        Else
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK") + 8
        End If

  GetMinResolutionDTM = 2 ^ (i) / _
                        GetCAPCOMModuleClock(r, vIndex)

End Function


'*****************************************************************************
'Description:
'        This function is called from the OnShowPage function
'        and calculate the value of the DTM.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        t               entered time [s]
'        rIndex
'             True       for overflow time
'             False      for underflow time
'        TimerNr         timer number (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of DTM
'
'*****************************************************************************
Function GetTimerNrValueDTM(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 = "CCU" & vIndex
  sRegPre = sVarPre & "_"

  If (t > GetMaxResolutionDTM(r, vIndex, iTmr)) Then
    t = GetMaxResolutionDTM(r, vIndex, iTmr)
  End If
  If (t < GetMinResolutionDTM(r, vIndex, iTmr)) Then
    t = GetMinResolutionDTM(r, vIndex, iTmr)
  End If

  
  i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")

  GetTimerNrValueDTM = t / 2 ^ i * GetCAPCOMModuleClock(r, vIndex)

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. 12 or 13)
'-----------------------------------------------------------------------------
'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 = "CCU" & vIndex
  sRegPre = sVarPre & "_"

        If r.v(sRegPre & "TCTR0_T" & iTmr & "PRE") = 0 Then
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")
        Else
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK") + 8
        End If

'  i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")

  If (iTmr = 12) Then
      If (r.v(sRegPre & "TCTR0_CTM") = 0) Then
          GetOverflow = (1 + r.sfr(sRegPre & "T" & iTmr & "PR")) * _
                        2 ^ (i) / GetCAPCOMModuleClock(r, vIndex)
      Else
          GetOverflow = (2 * (r.sfr(sRegPre & "T" & iTmr & "PR") + 1)) * _
                        2 ^ (i) / GetCAPCOMModuleClock(r, vIndex)
      End If
  Else
      GetOverflow = (1 + r.sfr(sRegPre & "T" & iTmr & "PR")) * _
                    2 ^ (i) / GetCAPCOMModuleClock(r, vIndex)
  End If

    
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. (12 or 13)
'-----------------------------------------------------------------------------
'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 sRegPre As String
  Dim sVarPre As String

Dim i As Integer

  sVarPre = "CCU" & vIndex
  sRegPre = sVarPre & "_"

        If r.v(sRegPre & "TCTR0_T" & iTmr & "PRE") = 0 Then
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")
        Else
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK") + 8
        End If

  If (iTmr = 12) Then
      If (r.v(sRegPre & "TCTR0_CTM") = 0) Then
          GetMaxResolution = 65536 * 2 ^ (i) / _
                             GetCAPCOMModuleClock(r, vIndex)
      Else
          GetMaxResolution = 2 * 65535 * 2 ^ (i) / _
                             GetCAPCOMModuleClock(r, vIndex)
      End If
  Else
      GetMaxResolution = 65536 * 2 ^ (i) / _
                         GetCAPCOMModuleClock(r, vIndex)
  End If

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. (12 or 13)
'-----------------------------------------------------------------------------
'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 sRegPre As String
  Dim sVarPre As String

Dim i As Integer

  sVarPre = "CCU" & vIndex
  sRegPre = sVarPre & "_"

        If r.v(sRegPre & "TCTR0_T" & iTmr & "PRE") = 0 Then
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")
        Else
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK") + 8
        End If

  GetMinResolution = 2 ^ (i) / _
                     GetCAPCOMModuleClock(r, vIndex) * 2

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         timer number (12 or 13)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the period register
'
'*****************************************************************************
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 = "CCU" & vIndex
  sRegPre = sVarPre & "_"

  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

 
        If r.v(sRegPre & "TCTR0_T" & iTmr & "PRE") = 0 Then
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")
        Else
            i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK") + 8
        End If
 
'  i = r.v(sRegPre & "TCTR0_T" & iTmr & "CLK")

  If (iTmr = 12) Then
      If (r.v(sRegPre & "TCTR0_CTM") = 0) Then
          GetTimerNrValue = t / 2 ^ i * GetCAPCOMModuleClock(r, vIndex) - 1
      Else
          GetTimerNrValue = t / 2 ^ i * GetCAPCOMModuleClock(r, vIndex) / 2 - 1
      End If
  Else
      GetTimerNrValue = t / 2 ^ i * GetCAPCOMModuleClock(r, vIndex) - 1
  End If

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

  sVarPre = "CCU" & 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:
'        This function returns the test for the cbo TxCLK.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iTmr           timer (12 or 13)
'        i              prescaler
'-----------------------------------------------------------------------------
'Returns:
'       string for cbo
'*****************************************************************************
Function GetTextForCLKcbo(r As projectdata, vIndex As Variant, iTmr As Integer, i As Integer) As String
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    s = ""
    s = "fcpu / " & 2 ^ i & " (Resolution: " & _
        Format$(2 ^ i / GetCAPCOMModuleClock(r, vIndex), "0.000") & " s)"
        
    GetTextForCLKcbo = s
  

End Function


'*****************************************************************************
'Description:
'        This function sets the register value for MCMPS.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub LoadMCMPSValue(r As projectdata, vIndex As Variant)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
  
    r.v(sRegPre & "MCMOUTS_MCMPS") = _
      1 * r.v(sRegPre & "MCMPS_CC60") + _
      2 * r.v(sRegPre & "MCMPS_COUT60") + _
      4 * r.v(sRegPre & "MCMPS_CC61") + _
      8 * r.v(sRegPre & "MCMPS_COUT61") + _
      16 * r.v(sRegPre & "MCMPS_CC62") + _
      32 * r.v(sRegPre & "MCMPS_COUT62")

End Sub

'*****************************************************************************
'Description:
'        This function sets the register value for PSL.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub LoadPSLValue(r As projectdata, vIndex As Variant)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
  
    r.v(sRegPre & "PSLR_PSL") = _
      1 * r.v(sRegPre & "PSL_CC60") + _
      2 * r.v(sRegPre & "PSL_COUT60") + _
      4 * r.v(sRegPre & "PSL_CC61") + _
      8 * r.v(sRegPre & "PSL_COUT61") + _
      16 * r.v(sRegPre & "PSL_CC62") + _
      32 * r.v(sRegPre & "PSL_COUT62")

End Sub

'*****************************************************************************
'Description:
'        This function sets the register value for TRPEN.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub LoadTRPENValue(r As projectdata, vIndex As Variant)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
  
    r.v(sRegPre & "TRPCTR_TRPEN") = _
      1 * r.v(sRegPre & "TRPEN_CC60") + _
      2 * r.v(sRegPre & "TRPEN_COUT60") + _
      4 * r.v(sRegPre & "TRPEN_CC61") + _
      8 * r.v(sRegPre & "TRPEN_COUT61") + _
      16 * r.v(sRegPre & "TRPEN_CC62") + _
      32 * r.v(sRegPre & "TRPEN_COUT62")

End Sub

'*****************************************************************************
'Description:
'        This function sets the register value for T12MODEN.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub LoadT12MODENValue(r As projectdata, vIndex As Variant)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
  
    r.v(sRegPre & "MODCTR_T12MODEN") = _
      1 * r.v(sRegPre & "T12MODEN_CC60") + _
      2 * r.v(sRegPre & "T12MODEN_COUT60") + _
      4 * r.v(sRegPre & "T12MODEN_CC61") + _
      8 * r.v(sRegPre & "T12MODEN_COUT61") + _
      16 * r.v(sRegPre & "T12MODEN_CC62") + _
      32 * r.v(sRegPre & "T12MODEN_COUT62")

End Sub

'*****************************************************************************
'Description:
'        This function sets the register value for T13MODEN.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub LoadT13MODENValue(r As projectdata, vIndex As Variant)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
  
    r.v(sRegPre & "MODCTR_T13MODEN") = _
      1 * r.v(sRegPre & "T13MODEN_CC60") + _
      2 * r.v(sRegPre & "T13MODEN_COUT60") + _
      4 * r.v(sRegPre & "T13MODEN_CC61") + _
      8 * r.v(sRegPre & "T13MODEN_COUT61") + _
      16 * r.v(sRegPre & "T13MODEN_CC62") + _
      32 * r.v(sRegPre & "T13MODEN_COUT62")

End Sub


'*****************************************************************************
'Description:
'        This function sets the selected capture/compare mode for selected
'        CCU6 channel.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iChannel       channel nuber (0, 1, 2 or 3)
'        i              mode
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub SelectModeForCC6Channelx(r As projectdata, vIndex As Variant, iChannel As Integer, i As Integer)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim iActualMode As Integer

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    If (iChannel < 3) Then
        
        iActualMode = r.v(sRegPre & "T12MSEL_MSEL6" & iChannel)
    
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 0
        If (i = 0) Then
            r.v(sRegPre & "T12DTC_DTE" & iChannel) = 0
            'r.v(sRegPre & "IEN_ENCC6" & iChannel & "R") = 0
            'r.v(sRegPre & "IEN_ENCC6" & iChannel & "F") = 0
            'todo disable ISR

            r.v(sRegPre & "T12MODEN_CC6" & iChannel) = 0
            r.v(sRegPre & "T12MODEN_COUT6" & iChannel) = 0
            Call LoadT12MODENValue(r, vIndex)

            r.v(sRegPre & "CMPSTAT_CC6" & iChannel & "PS") = 0
            r.v(sRegPre & "CMPSTAT_COUT6" & iChannel & "PS") = 0

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 1
        ElseIf (i = 1) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 2
        ElseIf (i = 2) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 3
        ElseIf (i = 3) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 4
        ElseIf (i = 4) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 5
        ElseIf (i = 5) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 6
        ElseIf (i = 6) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 7
        ElseIf (i = 7) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 8
        ElseIf (i = 8) Then
            r.v(sRegPre & "T12MODEN_CC6" & iChannel) = 0
            r.v(sRegPre & "T12MODEN_COUT6" & iChannel) = 0
            Call LoadT12MODENValue(r, vIndex)

            r.v(sRegPre & "CMPSTAT_CC6" & iChannel & "PS") = 0
            r.v(sRegPre & "CMPSTAT_COUT6" & iChannel & "PS") = 0

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 9
        ElseIf (i = 9) Then
            r.v(sRegPre & "T12DTC_DTE" & iChannel) = 1

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 10
        ElseIf (i = 10) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 11
        ElseIf (i = 11) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 12
        ElseIf (i = 12) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 13
        ElseIf (i = 13) Then

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 14
        ElseIf (i = 14) Then

        End If

        r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = i

    'if iChannel = 3
    Else
        
        r.v(sRegPre & "PSLR_PSL63") = 0
        r.v(sRegPre & "CMPSTAT_COUT63PS") = 0
        r.v(sRegPre & "TRPCTR_TRPEN13") = 0
        r.v(sRegPre & "MODCTR_ECT13O") = 0

    End If

End Sub


'*****************************************************************************
'Description:
'        This function returns a real duty cycle for selected channel.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iChannel       channel nuber (0, 1 or 2)
'-----------------------------------------------------------------------------
'Returns:
'       real duty cycle
'*****************************************************************************
Function GetRealDutyCecle(r As projectdata, vIndex As Variant, iChannel As Integer) As Double
    Dim sVarPre As String
    Dim sRegPre As String
    Dim d As Double

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    If (iChannel < 3) Then
        d = (r.sfr(sRegPre & "T12PR") + 1 - _
          r.sfr(sRegPre & "CC6" & iChannel & "SR")) / _
          (r.sfr(sRegPre & "T12PR") + 1) * 100
    Else
        d = (r.sfr(sRegPre & "T13PR") + 1 - _
          r.sfr(sRegPre & "CC6" & iChannel & "SR")) / _
          (r.sfr(sRegPre & "T13PR") + 1) * 100
    End If
    
    If (d < 0) Then
      d = 0
    End If

   GetRealDutyCecle = d
    

End Function

'*****************************************************************************
'Description:
'        This function updates the register CC6xSR.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iChannel       channel nuber (0, 1 or 2)
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub UpdateDutyCycle(r As projectdata, vIndex As Variant, iChannel As Integer)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim l As Long

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    If (iChannel < 3) Then
        l = (r.sfr(sRegPre & "T12PR") + 1) * _
          (1 - r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) / 100)
    Else
        l = (r.sfr(sRegPre & "T13PR") + 1) * _
          (1 - r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) / 100)
    End If

    If (l > 65535) Then
        l = 65535
    End If

    r.sfr(sRegPre & "CC6" & iChannel & "SR") = l

End Sub


'*****************************************************************************
'Description:
'        This function checks the selected channel. If the channel is disabled
'        its returns true, else false
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iChannel       channel nuber (0, 1 or 2)
'-----------------------------------------------------------------------------
'Returns:
'       true if channel is disabled, else false
'*****************************************************************************
Function IsChannelDisabled(r As projectdata, vIndex As Variant, iChannel As Integer) As Boolean
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    IsChannelDisabled = False

    If (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 0) Then
        IsChannelDisabled = True
    Else
        IsChannelDisabled = False
    End If
    

End Function


'*****************************************************************************
'Description:
'        This function returns disabled tooltip if selected channel is disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iChannel       channel nuber (0, 1 or 2)
'-----------------------------------------------------------------------------
'Returns:
'       formated tooltip if channel is disabled
'*****************************************************************************
Function GetDisabledTooltipIfChaneelDisabled(r As projectdata, vIndex As Variant, iChannel As Integer) As String
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    s = ""
    s = FormatDisabledTooltip("capture/compare channel " & iChannel & " is disabled")

    GetDisabledTooltipIfChaneelDisabled = s

End Function


'*****************************************************************************
'Description:
'        This function sets the pages ModulationControl of Pin CC6x Or COUT6x
'        to the default.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        sPin           name of the Pin (CC or COUT)
'        iChannel       channel nuber (0, 1 or 2)
'-----------------------------------------------------------------------------
'Returns:
'       formated tooltip if channel is disabled
'*****************************************************************************
Sub SetModulationControlPagesToDefault(r As projectdata, vIndex As Variant, sPin As String, iChannel As Integer)
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    r.v(sRegPre & "TRPEN_" & sPin & "6" & iChannel) = 0
    Call LoadTRPENValue(r, vIndex)
    r.v(sRegPre & "PSL_" & sPin & "6" & iChannel) = 0
    Call LoadPSLValue(r, vIndex)
    r.v(sRegPre & "MCMPS_" & sPin & "6" & iChannel) = 0
    Call LoadMCMPSValue(r, vIndex)
    r.v(sRegPre & "T13MODEN_" & sPin & "6" & iChannel) = 0
    Call LoadT13MODENValue(r, vIndex)
    r.v(sRegPre & "T12MODEN_" & sPin & "6" & iChannel) = 0
    Call LoadT12MODENValue(r, vIndex)

End Sub

'*****************************************************************************
'Description:
'        This function disables the hall sensor mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub DisableHallSensorMode(r As projectdata, vIndex As Variant)
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    Call SelectModeForCC6Channelx(r, vIndex, 0, 0)
    Call SelectModeForCC6Channelx(r, vIndex, 1, 0)
    Call SelectModeForCC6Channelx(r, vIndex, 2, 0)


End Sub

'*****************************************************************************
'Description:
'        This function enables the hall sensor mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub EnableHallSensorMode(r As projectdata, vIndex As Variant)
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    Call SelectModeForCC6Channelx(r, vIndex, 0, 8)
    Call SelectModeForCC6Channelx(r, vIndex, 1, 8)
    Call SelectModeForCC6Channelx(r, vIndex, 2, 8)

    'disable dead time generation
    r.v(sRegPre & "T12DTC_DTE0") = 0
    r.v(sRegPre & "T12DTC_DTE1") = 0
    r.v(sRegPre & "T12DTC_DTE2") = 0



End Sub


'*****************************************************************************
'Description:
'        This function checks if the hall sensor mode is selected.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'-----------------------------------------------------------------------------
'Returns:
'       true if hall sensor mode is selected, else false
'*****************************************************************************
Function IsHallSensorModeEnable(r As projectdata, vIndex As Variant) As Boolean
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    IsHallSensorModeEnable = False
    
    If (r.sfr(sRegPre & "T12MSEL") = 2184) Then
        IsHallSensorModeEnable = True
    End If


End Function


'*****************************************************************************
'Description:
'        This function returnd the text for the interrupt node cbo.
'-----------------------------------------------------------------------------
'Parameters:
'        iNode              node poninter
'-----------------------------------------------------------------------------
'Returns:
'       string for the cbo
'*****************************************************************************
Function GetCboIsrNodeText(iNode As Integer) As String
  
    GetCboIsrNodeText = "Interrupt node I" & iNode & " is selected"

End Function

'*****************************************************************************
'Description:
'        This function sets the selected node pointer
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        vIndex             module index
'        sBitName           name of bit in interrupt node pointer register
'        iNode              node poninter
'-----------------------------------------------------------------------------
'Returns:
'       string for the cbo
'*****************************************************************************
Sub SetIsrNode(r As projectdata, vIndex As Variant, sBitName, iNode As Integer)
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    r.v(sRegPre & "INP_" & sBitName) = iNode

End Sub

'*****************************************************************************
'Description:
'        This function checks if all interrupts are disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        vIndex             module index
'-----------------------------------------------------------------------------
'Returns:
'       true if all isr are disabled, else false
'*****************************************************************************
Function AllIsrDisabled(r As projectdata, vIndex As Variant) As Boolean
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    AllIsrDisabled = False

    If (r.v(sRegPre & "T12IC_IE") = 0 And _
        r.v(sRegPre & "T13IC_IE") = 0 And _
        r.v(sRegPre & "EIC_IE") = 0 And _
        r.v(sRegPre & "IC_IE") = 0) Then

        AllIsrDisabled = True

    End If

End Function

'*****************************************************************************
'Description:
'        This function checks if all interrupts are disabled and sets the
'        node isr to the enabled interrupt.
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        vIndex             module index
'        sRegName           name of the disabled ISR
'-----------------------------------------------------------------------------
'Returns:
'       true if all isr are disabled, else false
'*****************************************************************************
Sub DeselectNodeIsr(r As projectdata, vIndex As Variant, sRegName As String)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim saBit(7) As String
    Dim i As Integer
    Dim iHelp As Integer

    saBit(0) = "INPCC60"
    saBit(1) = "INPCC61"
    saBit(2) = "INPCC62"
    saBit(3) = "INPCHE"
    saBit(4) = "INPERR"
    saBit(5) = "INPT12"
    saBit(6) = "INPT13"

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    If (r.v(sRegPre & "IC_IE") = 1) Then
        iHelp = 0
    ElseIf (r.v(sRegPre & "EIC_IE") = 1) Then
        iHelp = 1
    ElseIf (r.v(sRegPre & "T12IC_IE") = 1) Then
        iHelp = 2
    ElseIf (r.v(sRegPre & "T13IC_IE") = 1) Then
        iHelp = 3
    Else
        For i = 0 To 6
            r.v(sRegPre & "INP_" & saBit(i)) = 0
        Next i
        Exit Sub
    End If

    
    If (sRegName = "IC") Then
        For i = 0 To 6
            If (r.v(sRegPre & "INP_" & saBit(i)) = 0) Then
                r.v(sRegPre & "INP_" & saBit(i)) = iHelp
            End If
        Next i

    ElseIf (sRegName = "EIC") Then
        For i = 0 To 6
            If (r.v(sRegPre & "INP_" & saBit(i)) = 1) Then
                r.v(sRegPre & "INP_" & saBit(i)) = iHelp
            End If
        Next i

    ElseIf (sRegName = "T12IC") Then
        For i = 0 To 6
            If (r.v(sRegPre & "INP_" & saBit(i)) = 2) Then
                r.v(sRegPre & "INP_" & saBit(i)) = iHelp
            End If
        Next i

    ElseIf (sRegName = "T13IC") Then
        For i = 0 To 6
            If (r.v(sRegPre & "INP_" & saBit(i)) = 3) Then
                r.v(sRegPre & "INP_" & saBit(i)) = iHelp
            End If
        Next i

    End If


End Sub

'*****************************************************************************
'Description:
'        This function checks sets the node ISR to the first enabled interrupt.
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        vIndex             module index
'        sRegName           name of the disabled ISR
'-----------------------------------------------------------------------------
'Returns:
'       true if all isr are disabled, else false
'*****************************************************************************
Sub SelectNodeIsr(r As projectdata, vIndex As Variant, sRegName As String)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim saBit(7) As String
    Dim i As Integer
    Dim iHelp As Integer

    saBit(0) = "INPCC60"
    saBit(1) = "INPCC61"
    saBit(2) = "INPCC62"
    saBit(3) = "INPCHE"
    saBit(4) = "INPERR"
    saBit(5) = "INPT12"
    saBit(6) = "INPT13"

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    If (r.v(sRegPre & "T12IC_IE") = 0 And _
        r.v(sRegPre & "T13IC_IE") = 0 And _
        r.v(sRegPre & "EIC_IE") = 0 And _
        r.v(sRegPre & "IC_IE") = 0) Then

        If (sRegName = "IC") Then
            For i = 0 To 6
                r.v(sRegPre & "INP_" & saBit(i)) = 0
            Next i

        ElseIf (sRegName = "EIC") Then
            For i = 0 To 6
                r.v(sRegPre & "INP_" & saBit(i)) = 1
            Next i

        ElseIf (sRegName = "T12IC") Then
            For i = 0 To 6
                r.v(sRegPre & "INP_" & saBit(i)) = 2
            Next i

        ElseIf (sRegName = "T13IC") Then
            For i = 0 To 6
                r.v(sRegPre & "INP_" & saBit(i)) = 3
            Next i

        End If

    End If


End Sub


'*****************************************************************************
'Description:
'        This function sets the CC6 module to the default.
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        vIndex             module index
'        sRegName           name of the disabled ISR
'-----------------------------------------------------------------------------
'Returns:
'       true if all isr are disabled, else false
'*****************************************************************************
Sub DisableCC6Module(r As projectdata, vIndex As Variant)
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim i As Integer

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    r.v(sVarPre & "_USE_Init") = 0

    Call DisableSRN(r, sVarPre, sRegPre & "T12IC", "NodeI0")
    Call DisableSRN(r, sVarPre, sRegPre & "T13IC", "NodeI1")
    Call DisableSRN(r, sVarPre, sRegPre & "EIC", "NodeI2")
    Call DisableSRN(r, sVarPre, sRegPre & "IC", "NodeI3")

    r.sfr(sRegPre & "TRPCTR") = 0
    Call ReleaseAlternateInputPin(r, sVarPre & "_CTRAP_I")
    r.sfr(sRegPre & "CMPSTAT") = 0
    r.v(sRegPre & "PSLR_PSL63") = 0

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

    r.v(sRegPre & "T12DTC_DTE0") = 0
    r.v(sRegPre & "T12DTC_DTE1") = 0
    r.v(sRegPre & "T12DTC_DTE2") = 0

    r.sfr(sRegPre & "IEN") = 0
    
    r.v(sRegPre & "PSL_CC60") = 0
    r.v(sRegPre & "PSL_CC61") = 0
    r.v(sRegPre & "PSL_CC62") = 0
    r.v(sRegPre & "PSL_COUT60") = 0
    r.v(sRegPre & "PSL_COUT61") = 0
    r.v(sRegPre & "PSL_COUT62") = 0
    Call LoadPSLValue(r, vIndex)

    r.v(sRegPre & "TRPEN_CC60") = 0
    r.v(sRegPre & "TRPEN_CC61") = 0
    r.v(sRegPre & "TRPEN_CC62") = 0
    r.v(sRegPre & "TRPEN_COUT60") = 0
    r.v(sRegPre & "TRPEN_COUT61") = 0
    r.v(sRegPre & "TRPEN_COUT62") = 0
    Call LoadTRPENValue(r, vIndex)

    r.v(sRegPre & "MCMPS_CC60") = 0
    r.v(sRegPre & "MCMPS_CC61") = 0
    r.v(sRegPre & "MCMPS_CC62") = 0
    r.v(sRegPre & "MCMPS_COUT60") = 0
    r.v(sRegPre & "MCMPS_COUT61") = 0
    r.v(sRegPre & "MCMPS_COUT62") = 0
    Call LoadMCMPSValue(r, vIndex)

    r.v(sRegPre & "T12MODEN_CC60") = 0
    r.v(sRegPre & "T12MODEN_CC61") = 0
    r.v(sRegPre & "T12MODEN_CC62") = 0
    r.v(sRegPre & "T12MODEN_COUT60") = 0
    r.v(sRegPre & "T12MODEN_COUT61") = 0
    r.v(sRegPre & "T12MODEN_COUT62") = 0
    Call LoadT12MODENValue(r, vIndex)

    r.v(sRegPre & "T13MODEN_CC60") = 0
    r.v(sRegPre & "T13MODEN_CC61") = 0
    r.v(sRegPre & "T13MODEN_CC62") = 0
    r.v(sRegPre & "T13MODEN_COUT60") = 0
    r.v(sRegPre & "T13MODEN_COUT61") = 0
    r.v(sRegPre & "T13MODEN_COUT62") = 0
    Call LoadT13MODENValue(r, vIndex)

    Call SelectModeForCC6Channelx(r, vIndex, 0, 0)
    Call SelectModeForCC6Channelx(r, vIndex, 1, 0)
    Call SelectModeForCC6Channelx(r, vIndex, 2, 0)
    Call SelectModeForCC6Channelx(r, vIndex, 3, 0)

    r.v(sRegPre & "MODCTR_MCMEN") = 0
    r.v(sRegPre & "MCMCTR_SWSEL") = 0
    r.v(sRegPre & "MCMCTR_SWSYN") = 0
    r.v(sRegPre & "IEN_ENIDLE") = 0
    r.v(sRegPre & "IEN_ENCHE") = 0
    r.v(sRegPre & "IEN_ENWHE") = 0

    r.sfr(sRegPre & "TCTR2") = 0
    r.v(sRegPre & "TCTR2_T13TED") = 1

    r.v(sRegPre & "TCTR4_T12RS") = 0
    r.v(sRegPre & "TCTR4_T13RS") = 0
    r.v(sRegPre & "TCTR0_CTM") = 0

    For i = 0 To 2
            If (GetPinFunction(r, sVarPre & "_CC6" & i & "_I") = 1) Then
                Call ReleaseAlternateInputPin(r, sVarPre & "_CC6" & i & "_I")
            ElseIf (GetPinFunction(r, sVarPre & "_CC6" & i & "_O") = 2) Then
                Call ReleaseAlternateOutputPin(r, sVarPre & "_CC6" & i & "_O")
            End If
            
            Call ReleaseAlternateOutputPin(r, sVarPre & "_COUT6" & i & "_O")

            Call ReleaseAlternateInputPin(r, sVarPre & "_CC6POS" & i & "_I")
    Next i

    i = 3
    Call ReleaseAlternateOutputPin(r, sVarPre & "_COUT6" & i & "_O")

End Sub
