Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\CC6FunctionClass.cls"
'#Uses "..\classes\CC6ParameterClass.cls"
'#Uses "..\include\CC6Format.inc"
'#Uses "..\include\Port.inc"
'#Uses "8BitAdapter.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
'        ---------------------------------------------------------------------
'        0.1   als          20.11.04     initial version for XC866
'
'*****************************************************************************

'*****************************************************************************
' 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 CC6FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(vIndex As Variant, func As CC6FunctionClass, 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
  Dim sCC6 As String

  sVarPre = "CCU" & vIndex
  sCC6 = "CC6"

  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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & _
                 " 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 " & sCC6 & " timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
    sDescription = "16-bit Value with which the " & sCC6 & " timer reload register is to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "uwValue", "", 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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & " channel to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegName", "", sDescription)) Then Exit Function
    sDescription = "16-bit Value with which the " & sCC6 & " shadow register is to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "uwValue", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "GetChannelShadowRegister"
  sDescription = "This macro returns the content of the " & sCC6 & _
                 " 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 " & sCC6 & " shadow register"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sCC6 & " 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 " & sCC6 & _
                 " 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 " & sCC6 & " register"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sCC6 & " 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 " & sCC6 & _
                 " 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 " & sCC6 & " 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 " & sCC6 & _
                 " 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 " & sCC6 & " timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
    sDescription = "16-bit value with which the " & sCC6 & " counter register is to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "uwValue", "", sDescription)) Then Exit Function


  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "SetStatusBit"
  sDescription = "This macro sets the capture / compare modification status bit of the selected " & _
                 sCC6 & " 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 " & sCC6 & " 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 modification status bit of the selected " & _
                 sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & _
                     " 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

  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
    sFctName = "WriteCANAddress_I"
    sDescription = "This macro returns the period register of the selected " & sCC6 & _
                 " timer." & 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 period value"
  If (Not func.AddFunction("CAN_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the " & sCC6 & " timer to be used"
    If (Not func.AddParameter("CAN_NAME_" & sFctName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

        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 CC6FunctionClass 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
  Dim sCC6 As String

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

  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 " & sCC6 & " 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 " & sCC6 & " 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 " & sCC6 & " 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 = GetCoreClock(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 [usec]
'
'*****************************************************************************
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 & "_"

  i = r.v(sRegPre & "TCTR0L_T12CLK")

  GetOverflowDTM = r.l(sRegPre & "T12DTCL_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 [usec]
'
'*****************************************************************************
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

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

  GetMaxResolutionDTM = 63 * 2 ^ (r.v(sRegPre & "TCTR0L_T12CLK")) / _
                        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 [usec]
'
'*****************************************************************************
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

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

  GetMinResolutionDTM = 2 ^ (r.v(sRegPre & "TCTR0L_T12CLK")) / _
                        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 [usec]
'        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 & "TCTR0L_T12CLK")

  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 [usec]
'
'*****************************************************************************
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
  Dim sHelp As String

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

  If (iTmr = 12) Then
      sHelp = "L"
  Else
      sHelp = "H"
  End If

  i = r.v(sRegPre & "TCTR0" & sHelp & "_T" & iTmr & "CLK")

  If (iTmr = 12) Then
      If (r.v(sRegPre & "TCTR0" & sHelp & "_CTM") = 0) Then
          GetOverflow = (1 + GetRegisterValue_L_and_H(r, "T" & iTmr & "PR")) * _
                        2 ^ (i) / GetCAPCOMModuleClock(r, vIndex)
      Else
          GetOverflow = (2 * (GetRegisterValue_L_and_H(r, "T" & iTmr & "PR") + 1)) * _
                        2 ^ (i) / GetCAPCOMModuleClock(r, vIndex)
      End If
  Else
      GetOverflow = (1 + GetRegisterValue_L_and_H(r, "T" & iTmr & "PR")) * _
                    2 ^ (i) / GetCAPCOMModuleClock(r, vIndex)
  End If
  If (r.v(sRegPre & "TCTR0" & sHelp & "_T" & iTmr & "PRE") = 1) Then
    GetOverflow = GetOverflow * 256
  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 [usec]
'
'*****************************************************************************
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

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

  If (iTmr = 12) Then
      If (r.v(sRegPre & "TCTR0L_CTM") = 0) Then
          GetMaxResolution = 65536 * 2 ^ (r.v(sRegPre & "TCTR0L_T" & iTmr & "CLK")) / _
                             GetCAPCOMModuleClock(r, vIndex)
      Else
          GetMaxResolution = 2 * 65535 * 2 ^ (r.v(sRegPre & "TCTR0L_T" & iTmr & "CLK")) / _
                             GetCAPCOMModuleClock(r, vIndex)
      End If
  Else
      GetMaxResolution = 65536 * 2 ^ (r.v(sRegPre & "TCTR0H_T" & iTmr & "CLK")) / _
                         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 [usec]
'
'*****************************************************************************
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 sHelp As String

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

  If (iTmr = 12) Then
      sHelp = "L"
  Else
      sHelp = "H"
  End If


  GetMinResolution = 2 ^ (r.v(sRegPre & "TCTR0" & sHelp & "_T" & iTmr & "CLK")) / _
                     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 [usec]
'        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
  Dim sHelp As String

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

  If (iTmr = 12) Then
      sHelp = "L"
  Else
      sHelp = "H"
  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

  
  i = r.v(sRegPre & "TCTR0" & sHelp & "_T" & iTmr & "CLK")

  If (iTmr = 12) Then
      If (r.v(sRegPre & "TCTR0L_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(SCU_PREFIX & "PMCON1_CCU_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 s As String
  
    s = ""
    s = FormatDisabledTooltip("the CCU6 module clock is disabled")
      
    GetTooltipModuleClockDisabled = s

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 = "fclk / " & 2 ^ i & " (Resolution: " & _
        Format$(2 ^ i / GetCAPCOMModuleClock(r, vIndex), "0.000") & " usec)"
        
    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("CCU6_MCMOUTSL_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("CCU6_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("CCU6_TRPCTRH_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("CCU6_MODCTRL_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("CCU6_MODCTRH_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
    Dim sHelp As String

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

    If (iChannel < 2) Then
        sHelp = "L"
    Else
        sHelp = "H"
    End If


    If (iChannel < 3) Then
        
        iActualMode = r.v("CCU6_T12MSEL" & sHelp & "_MSEL6" & iChannel)
    
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 0
        If (i = 0) Then
            r.v("CCU6_T12DTCH_DTE" & iChannel) = 0

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

            r.v("CCU6_CMPSTATH_CC6" & iChannel & "PS") = 0
            r.v("CCU6_CMPSTATH_COUT6" & iChannel & "PS") = 0
            r.v("CCU6_IENL_ENCC6" & iChannel & "R") = 0
            r.v("CCU6_IENL_ENCC6" & iChannel & "F") = 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("CCU6_CMPSTATH_CC6" & iChannel & "PS") = 0
            r.v("CCU6_CMPSTATH_COUT6" & iChannel & "PS") = 0

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'soll mode = 9
        ElseIf (i = 9) Then
            r.v("CCU6_T12DTCH_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("CCU6_T12MSEL" & sHelp & "_MSEL6" & iChannel) = i

    'if iChannel = 3
    Else
        
        r.v("CCU6_PSLR_PSL63") = 0
        r.v("CCU6_CMPSTATH_COUT63PS") = 0
        r.v("CCU6_TRPCTRH_TRPEN13") = 0
        r.v("CCU6_MODCTRH_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 = (GetRegisterValue_L_and_H(r, "T12PR") + 1 - _
          GetRegisterValue_L_and_H(r, "CC6" & iChannel & "SR")) / _
          (GetRegisterValue_L_and_H(r, "T12PR") + 1) * 100
    Else
        d = (GetRegisterValue_L_and_H(r, "T13PR") + 1 - _
          GetRegisterValue_L_and_H(r, "CC6" & iChannel & "SR")) / _
          (GetRegisterValue_L_and_H(r, "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 = (GetRegisterValue_L_and_H(r, "T12PR") + 1) * _
          (1 - r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) / 100)
    Else
        l = (GetRegisterValue_L_and_H(r, "T13PR") + 1) * _
          (1 - r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) / 100)
    End If

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

    Call SetCC6Register_L_and_H(r, "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
    Dim sHelp As String

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

    IsChannelDisabled = False

    If (iChannel < 2) Then
        sHelp = "L"
    Else
        sHelp = "H"
    End If

    If (r.v(sRegPre & "T12MSEL" & sHelp & "_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)

    r.v("CCU6_T12DTCH_DTE0") = 1

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 (GetRegisterValue_L_and_H(r, "T12MSEL") = 2184) Then
        IsHallSensorModeEnable = True
    End If


End Function


'*****************************************************************************
'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 SetCC6Register_L_and_H(r, "TRPCTR", 0)
    Call SetCC6Register_L_and_H(r, "CMPSTAT", 0)
    r.v("CCU6_PSLR_PSL63") = 0

    r.v("CCU6_MODCTRH_ECT13O") = 0

    r.v("CCU6_T12DTCH_DTE0") = 0
    r.v("CCU6_T12DTCH_DTE1") = 0
    r.v("CCU6_T12DTCH_DTE2") = 0

    Call SetCC6Register_L_and_H(r, "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("CCU6_MODCTRL_MCMEN") = 0
    r.v("CCU6_MCMCTR_SWSEL") = 0
    r.v("CCU6_MCMCTR_SWSYN") = 0

    Call SetCC6Register_L_and_H(r, "TCTR2", 0)
    r.v("CCU6_TCTR2L_T13TED") = 0

    r.v("CCU6_TCTR4L_T12RS") = 0
    r.v("CCU6_TCTR4H_T13RS") = 0
    r.v("CCU6_TCTR0L_CTM") = 0


        If (r.v("CCU6_USE_NODEI0") = 1) Then
                r.v("CCU6_USE_NODEI0") = 0
        End If

        If (r.v("IEN1_ECCIP0") = 1) Then
                r.v("IEN1_ECCIP0") = 0
        End If

        If (r.v("CCU6_USE_NODEI1") = 1) Then
                r.v("CCU6_USE_NODEI1") = 0
        End If

        If (r.v("IEN1_ECCIP1") = 1) Then
                r.v("IEN1_ECCIP1") = 0
        End If

        If (r.v("CCU6_USE_NODEI2") = 1) Then
                r.v("CCU6_USE_NODEI2") = 0
        End If

        If (r.v("IEN1_ECCIP2") = 1) Then
                r.v("IEN1_ECCIP2") = 0
        End If

        If (r.v("CCU6_USE_NODEI3") = 1) Then
                r.v("CCU6_USE_NODEI3") = 0
        End If

        If (r.v("IEN1_ECCIP3") = 1) Then
                r.v("IEN1_ECCIP3") = 0
        End If
        r.v("CCU6_USE_StartTmr") = 0
        r.v("CCU6_USE_StopTmr") = 0
        r.v("CCU6_USE_ResetTmr") = 0
        r.v("CCU6_USE_SetTmrPeriod") = 0
        r.v("CCU6_USE_GetTmrCounter") = 0
        r.v("CCU6_USE_SetTmrCounter") = 0
        r.v("CCU6_USE_SetDeadTimePeriod") = 0
        r.v("CCU6_USE_EnableShadowTransfer") = 0
        r.v("CCU6_USE_LoadChannelShadowRegister") = 0
        r.v("CCU6_USE_GetChannelShadowRegister") = 0
        r.v("CCU6_USE_GetChannelRegister") = 0
        r.v("CCU6_USE_SetStatusBit") = 0
        r.v("CCU6_USE_ResetStatusBit") = 0
        r.v("CAN_USE_WriteCANAddress_I") = 0

End Sub


'*****************************************************************************
'Description:
'        This function sets the low and high register of the CC6 module.
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        sRegName           name of the register (e.g. T12PR)
'        dValue             value to be set (0 .. 65535)
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub SetCC6Register_L_and_H(r As ProjectData, sRegName As String, dValue As Variant)

    Dim dHigh As Double
    Dim dLow As Double

    dHigh = (dValue \ 2 ^ 8)
    dLow = dValue - dHigh * 2 ^ 8

    r.sfr("CCU6_" & sRegName & "H") = dHigh
    r.sfr("CCU6_" & sRegName & "L") = dLow



End Sub

'*****************************************************************************
'Description:
'        This function retrund the value of the regiser H+L.
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        sRegName           name of the register (e.g. T12PR)
'-----------------------------------------------------------------------------
'Returns:
'       value of register
'*****************************************************************************
Function GetRegisterValue_L_and_H(r As ProjectData, sRegName As String) As Variant

    GetRegisterValue_L_and_H = r.sfr("CCU6_" & sRegName & "H") * 2 ^ 8 + r.sfr("CCU6_" & sRegName & "L")

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 " & 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
    sRegPre = "CCU6_"
    r.v(sRegPre & 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(CORE_PREFIX & "IEN1_ECCIP0") = 0 And _
        r.v(CORE_PREFIX & "IEN1_ECCIP1") = 0 And _
        r.v(CORE_PREFIX & "IEN1_ECCIP2") = 0 And _
        r.v(CORE_PREFIX & "IEN1_ECCIP3") = 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

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    saBit(0) = "INPL_INPCC60"
    saBit(1) = "INPL_INPCC61"
    saBit(2) = "INPL_INPCC62"
    saBit(3) = "INPL_INPCHE"
    saBit(4) = "INPH_INPERR"
    saBit(5) = "INPH_INPT12"
    saBit(6) = "INPH_INPT13"
    
    If (r.v(CORE_PREFIX & "IEN1_ECCIP0") = 1) Then
        iHelp = 0
    ElseIf (r.v(CORE_PREFIX & "IEN1_ECCIP1") = 1) Then
        iHelp = 1
    ElseIf (r.v(CORE_PREFIX & "IEN1_ECCIP2") = 1) Then
        iHelp = 2
    ElseIf (r.v(CORE_PREFIX & "IEN1_ECCIP3") = 1) Then
        iHelp = 3
    Else
        For i = 0 To 6
            r.v(sRegPre & saBit(i)) = 0
        Next i
        Exit Sub
    End If

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

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

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

    ElseIf (sRegName = CORE_PREFIX & "IEN1_ECCIP3") Then
        For i = 0 To 6
            If (r.v(sRegPre & saBit(i)) = 3) Then
                r.v(sRegPre & 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

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

    saBit(0) = sRegPre & "INPL_INPCC60"
    saBit(1) = sRegPre & "INPL_INPCC61"
    saBit(2) = sRegPre & "INPL_INPCC62"
    saBit(3) = sRegPre & "INPL_INPCHE"
    saBit(4) = sRegPre & "INPH_INPERR"
    saBit(5) = sRegPre & "INPH_INPT12"
    saBit(6) = sRegPre & "INPH_INPT13"
    
    If (r.v(CORE_PREFIX & "IEN1_ECCIP0") = 0 And _
        r.v(CORE_PREFIX & "IEN1_ECCIP1") = 0 And _
        r.v(CORE_PREFIX & "IEN1_ECCIP2") = 0 And _
        r.v(CORE_PREFIX & "IEN1_ECCIP3") = 0) Then

        If (sRegName = CORE_PREFIX & "IEN1_ECCIP0") Then
            For i = 0 To 6
                r.v(saBit(i)) = 0
            Next i

        ElseIf (sRegName = CORE_PREFIX & "IEN1_ECCIP1") Then
            For i = 0 To 6
                r.v(saBit(i)) = 1
            Next i

        ElseIf (sRegName = CORE_PREFIX & "IEN1_ECCIP2") Then
            For i = 0 To 6
                r.v(saBit(i)) = 2
            Next i

        ElseIf (sRegName = CORE_PREFIX & "IEN1_ECCIP3") Then
            For i = 0 To 6
                r.v(saBit(i)) = 3
            Next i
        End If
    End If
End Sub



'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'        Then the variables and SFRs will be reset to DAvE's default state.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub CheckResetCCU6_Pins(r As ProjectData)
  On Error Resume Next
  
    Dim i As Integer

    If (r.v(SCU_PREFIX & "PMCON1_CCU_DIS") = 1) Then
    ' all pins to Reserved mode
        r.sfr("CCU6_PISEL0H") = &H3F
        
        r.v("CCU6_USE_CCPOS0_PIN") = 0
        r.v("CCU6_USE_CCPOS1_PIN") = 0
        r.v("CCU6_USE_CCPOS2_PIN") = 0
        r.v("CCU6_USE_#CTRAP_PIN") = 0
        r.v("CCU6_USE_T12HR_PIN") = 0
        r.v("CCU6_USE_T13HR_PIN") = 0
        
        'here the code is replaced by the following 7 Functions.
        Call Release_CC60_Pins(r)
        Call Release_COUT60_Pins_Module_Clock(r)
        Call Release_CC61_Pins(r)
        Call Release_COUT61_Pins(r)
        Call Release_CC62_Pins(r)
        Call Release_COUT62_Pins(r)
        Call Release_COUT63_Pins(r)
        
        For i = 0 To 3
            Call ReleaseAlternateInputPin(r, "#CTRAP_" & i)
            Call ReleaseAlternateInputPin(r, "CCPOS0_" & i)
            Call ReleaseAlternateInputPin(r, "CCPOS1_" & i)
            Call ReleaseAlternateInputPin(r, "CCPOS2_" & i)
        Next i
        For i = 0 To 2
            Call ReleaseAlternateInputPin(r, "T12HR_" & i)
            Call ReleaseAlternateInputPin(r, "T13HR_" & i)
        Next i
            
    End If

End Sub


'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************


Function Release_CC60_Pins(r As ProjectData) As Boolean

    Release_CC60_Pins = False
    
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    '-------------------------------------------------------------------------
    i = 0
    sHelp = "L"
    '-------------------------------------------------------------------------
    If (r.v("CCU6_USE_CC60_PIN") = 1) Then
        Call ReleaseAlternateInputPin(r, "CC60_0(Input)")
        If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 4 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 5 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 6 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 7 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 10 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 11 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 12 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 13 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 14) Then
        
            Call SelectModeForCC6Channelx(r, vIndex, i, 0)
        End If
    
    ElseIf (r.v("CCU6_USE_CC60_PIN") = 2) Then
        Call ReleaseAlternateOutputPin(r, "CC60_0(Output)")
        
        If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 1 Or _
            r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 3) Then
            Call SelectModeForCC6Channelx(r, vIndex, i, 0)
        End If
        Call SetModulationControlPagesToDefault(r, vIndex, "CC", i)
    
    ElseIf (r.v("CCU6_USE_CC60_PIN") = 3) Then
        Call ReleaseAlternateInputPin(r, "CC60_3(Input)")
    ElseIf (r.v("CCU6_USE_CC60_PIN") = 4) Then
        Call ReleaseAlternateOutputPin(r, "CC60_1")
    End If
    
    r.v("CCU6_USE_CC60_PIN") = 0
    
    r.v(sRegPre & "PISEL0L_ISCC60") = 0
    
    Release_CC60_Pins = True

End Function


'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************


Function Release_COUT60_Pins(r As ProjectData) As Boolean

    Release_COUT60_Pins = False
    
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    i = 0
    sHelp = "L"
    
    Call ReleaseAlternateOutputPin(r, "COUT60_0")
    Call ReleaseAlternateOutputPin(r, "COUT60_1")
    r.v("CCU6_USE_COUT60_PIN") = 0
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 2 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 3) Then
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    End If
    Call SetModulationControlPagesToDefault(r, vIndex, "COUT", i)
    
    Release_COUT60_Pins = True

End Function

'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Function Release_CC61_Pins(r As ProjectData) As Boolean
    
    Release_CC61_Pins = False
    
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    '-------------------------------------------------------------------------
        i = 1
        sHelp = "L"
    '-------------------------------------------------------------------------
    sTempVar = "CC61"
    Call ReleasePins(r, sTempVar)
    r.v("CCU6_USE_CC61_PIN") = 0
    r.v(sRegPre & "PISEL0L_ISCC61") = 0
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 4 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 5 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 6 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 7 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 10 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 11 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 12 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 13 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 14) Then
        
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    
    End If
    
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 1 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 3) Then
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    End If
    
    Call SetModulationControlPagesToDefault(r, vIndex, "CC", i)
    
    Release_CC61_Pins = True

End Function




'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Function Release_COUT61_Pins(r As ProjectData) As Boolean

    Release_COUT61_Pins = False
    
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    '-------------------------------------------------------------------------
        i = 1
        sHelp = "L"
    '-------------------------------------------------------------------------
    sTempVar = "COUT61"
    Call ReleaseOutputPins(r, sTempVar)
    r.v("CCU6_USE_COUT61_PIN") = 0
    
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 2 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 3) Then
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    End If
    Call SetModulationControlPagesToDefault(r, vIndex, "COUT", i)
    
    Release_COUT61_Pins = True

End Function

'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************



Function Release_CC62_Pins(r As ProjectData) As Boolean

    Release_CC62_Pins = False
    
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    '-------------------------------------------------------------------------
    i = 2
    sHelp = "H"
    '-------------------------------------------------------------------------
    sTempVar = "CC62"
    Call ReleasePins(r, sTempVar)
    r.v("CCU6_USE_CC62_PIN") = 0
    r.v(sRegPre & "PISEL0L_ISCC62") = 0
    
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 4 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 5 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 6 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 7 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 10 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 11 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 12 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 13 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 14) Then
        
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    
    End If
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 1 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 3) Then
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    End If
    Call SetModulationControlPagesToDefault(r, vIndex, "CC", i)
    
    Release_CC62_Pins = True
        
End Function


'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************


Function Release_COUT62_Pins(r As ProjectData) As Boolean

    Release_COUT62_Pins = False
    
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    i = 2
    sHelp = "H"
    
    sTempVar = "COUT62"
    Call ReleaseOutputPins(r, sTempVar)
    r.v("CCU6_USE_COUT62_PIN") = 0
    
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 2 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 3) Then
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    End If
    Call SetModulationControlPagesToDefault(r, vIndex, "COUT", i)
    
    Release_COUT62_Pins = True

End Function

'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Function Release_COUT63_Pins(r As ProjectData) As Boolean

    Release_COUT63_Pins = False
    
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    i = 3
    sTempVar = "COUT63"
    Call ReleaseOutputPins(r, sTempVar)
    r.v("CCU6_USE_COUT63_PIN") = 0
    Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    
    Release_COUT63_Pins = True

End Function

'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************



Function Release_COUT60_Pins_Module_Clock(r As ProjectData) As Boolean

    Release_COUT60_Pins_Module_Clock = False
 
    Dim vIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sTempVar As String
    Dim i As Integer
    Dim sHelp As String
    
    vIndex = "6"
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    i = 0
    sHelp = "L"
    
    Call ReleaseAlternateOutputPin(r, "COUT60_0")
    Call ReleaseAlternateOutputPin(r, "COUT60_1")
    r.v("CCU6_USE_COUT60_PIN") = 0
    
    If (r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 2 Or _
        r.v(sRegPre & "T12MSEL" & sHelp & "_MSEL6" & i) = 3) Then
        Call SelectModeForCC6Channelx(r, vIndex, i, 0)
    End If
    
    Call SetModulationControlPagesToDefault(r, vIndex, "COUT", i)
    
    Release_COUT60_Pins_Module_Clock = True

End Function

'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************

Sub ReleaseInputPins(r As ProjectData, vName As Variant)
  On Error Resume Next
  
 Dim aValue As Integer
 
 aValue = r.v("CCU6_USE_" & vName & "_PIN")
 Select Case (aValue)
 
    Case (1)
         Call ReleaseAlternateInputPin(r, vName & "_0")
    Case (2)
         Call ReleaseAlternateInputPin(r, vName & "_1")
    Case (3)
         Call ReleaseAlternateInputPin(r, vName & "_2")
    Case (4)
         Call ReleaseAlternateInputPin(r, vName & "_3")
 
 End Select
 
End Sub

'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************

Sub ReleaseOutputPins(r As ProjectData, vName As Variant)
  On Error Resume Next
  
 Dim aValue As Integer
 
 aValue = r.v("CCU6_USE_" & vName & "_PIN")
 Select Case (aValue)
 
    Case (1)
         Call ReleaseAlternateOutputPin(r, vName & "_0")
    Case (2)
         Call ReleaseAlternateOutputPin(r, vName & "_1")
    Case (3)
         Call ReleaseAlternateOutputPin(r, vName & "_2")
 
 End Select
 
End Sub


'*****************************************************************************
'Description:
'        This subroutine is called when the Pin's are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************

Sub ReleasePins(r As ProjectData, vName As Variant)
  On Error Resume Next
  
 Dim aValue As Integer

 aValue = r.v("CCU6_USE_" & vName & "_PIN")
 Select Case (aValue)
 
    Case (1)
         Call ReleaseAlternateInputPin(r, vName & "_0(Input)")
    Case (2)
         Call ReleaseAlternateInputPin(r, vName & "_1(Input)")
    Case (3)
         Call ReleaseAlternateOutputPin(r, vName & "_0(Output)")
    Case (4)
         Call ReleaseAlternateOutputPin(r, vName & "_1(Output)")
    Case (5)
         Call ReleaseAlternateInputPin(r, vName & "_2(Input)")
    Case (6)
         Call ReleaseAlternateOutputPin(r, vName & "_2(Output)")
    Case (7)
         Call ReleaseAlternateInputPin(r, vName & "_3(Input)")
    Case (8)
         Call ReleaseAlternateOutputPin(r, vName & "_4(Output)")
 
 End Select
 
End Sub




