'#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         13.06.00     final version for C167CS
'             
'        ---------------------------------------------------------------------
'        2.0   ahe         13.06.00     final version for C161CS/JC/JI
'                                       - no changes
'
'
'        ---------------------------------------------------------------------
'        2.1   ahe         23.08.02     no changes
'
'*****************************************************************************


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

'*****************************************************************************
'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_x As Integer
  Dim iTmr_y As Integer

  sVarPre = "CC" & vIndex

  If (vIndex = 1) Then
      iCh_low = 0
      iCh_high = 15
      iTmr_x = 0
      iTmr_y = 1
  Else
      iCh_low = 16
      iCh_high = 31
      iTmr_x = 7
      iTmr_y = 8
  End If

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

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


  '------------------------------------------------------------------------------------------------
  ' 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 CAPCOM timer. " & _
                 "The timer continues counting from where it stopped. " & vbcrlf & _
                 "" & vbcrlf & _
                 "The following definitions for TimerNr are available: " & vbcrlf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & vbcrlf & _
                 r.v(sVarPre & "_PARA_TIMER_" & iTmr_y)

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

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

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

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

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

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

  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "SetCCxReg"
  sDescription = "This macro loads the selected capture/compare register " & _
                 "with the parameter Value. " & vbcrlf & _
                 "" & vbcrlf & _
                 "The following definitions are available for RegName " & _
                 "(only for enabled CAPCOM channels): " & vbcrlf & _
                 r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & r.v(sVarPre & "_PARA_CC_" & 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 CAPCOM" & vIndex & " channel to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegName", "", sDescription)) Then Exit Function
    sDescription = "16-bit Value with which the CCx register is to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "Value", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "GetCCxReg"
  sDescription = "This macro returns the content of the selected capture/compare register." & _
                 "" & vbcrlf & _
                 "The following definitions are available for RegName " & _
                 "(only for enabled CAPCOM channels): " & vbcrlf & _
                 r.v(sVarPre & "_PARA_CC_" & iCh_low) & " .. " & r.v(sVarPre & "_PARA_CC_" & iCh_high) & _
                 " (see @Defines in header file)"

  sReturnValue = "Current 16-bit value of the CCx register"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "name of the CAPCOM" & vIndex & " channel to be readed"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegName", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAPCOMx_NAME_...
  '------------------------------------------------------------------------------------------------
  sFctName = "Tmr" & iTmr_x
  sDescription = "This is the interrupt service routine for the CAPCOM timer " & iTmr_x & ". " & _
                 "It is called when overflow of the timer " & iTmr_x & " register occurs."

                 sDescription = sDescription & vbcrlf & TOOLTIP_ISR_USERCODE

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

                 sDescription = sDescription & vbcrlf & TOOLTIP_ISR_USERCODE

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

      '------------------------------------------------------------------------------------------------
      ' Description for CAPCOMx_NAME_...
      '------------------------------------------------------------------------------------------------
      sFctName = "CC" & i
      sDescription = "This is the interrupt service routine for the capture/ " & _
                     "compare register CC" & Format(i) & ". If the content of the corresponding " & _
                     "compare timer (configurable) equals the content of the " & _
                     "capture/compare register CC" & Format(i) & " or if a capture event " & _
                     "occurs at the associated port pin, the interrupt request " & _
                     "flag is set and an interrupt or PEC transfer 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_x As Integer
  Dim iTmr_y As Integer

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

  If (vIndex = 1) Then
      iCh_low = 0
      iCh_high = 15
      iTmr_x = 0
      iTmr_y = 1
  Else
      iCh_low = 16
      iCh_high = 31
      iTmr_x = 7
      iTmr_y = 8
  End If


  ' for each parameter of this module the property AddParameter must be called
  '
  ' AddParameter(sParameterName As String, sParameterDescription As String) As Boolean
  '
  ' sParameterName         | String | parameter name as key (use the name of the variable 
  '                                   for the parameter name e.g. SSC_PARA_MODE) 
  ' sParameterDescription  | String | description of the parameter

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

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

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

  RefreshParameterDescriptions = True
End Function



'*****************************************************************************
'Description:
'          This function 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 = "CAPCOM" & vIndex
  sRegPre = "CC"
  GetCAPCOMModuleClock = GetSystemClock(r) 
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 GetTooltipModuleDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  Dim sVarPre As String

  sVarPre = "CAPCOM" & vIndex
  
  GetTooltipModuleDisabled = ""            
  GetTooltipModuleDisabled =  GetTooltipModuleDisabled & FormatDisabledToolTip("the " & sVarPre & " module is disabled")
End Function

'*****************************************************************************
'Description:
'        This function returns the disable bit name of SYSCON3 register (e.g SSCDIS).
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'
'-----------------------------------------------------------------------------
'Returns:
'       disable bit name
'*****************************************************************************
Function GetCAPCOMDisebleBitName(r As ProjectData, vIndex As Variant) As String
    On Error Resume Next
    Dim sRegPre As String
    sRegPre = "CC" & vIndex

    If (r.Regs.BitFind("SYSCON3_" & sRegPre & "DIS") = True) Then
        GetCAPCOMDisebleBitName = sRegPre & "DIS"
    Else
        GetCAPCOMDisebleBitName = ""
    End If


End Function


'*****************************************************************************
'Description:
'        This function check  the SYSCON3 register, whether the modul is disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        vIndex          ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'        True  -  if the modul is disabled
'        False -  else
'
'*****************************************************************************
Function IsModuleDisabled(r As ProjectData, vIndex As Variant) As Boolean

  IsModuleDisabled = False

  If (r.v("SYSCON3_" & GetCAPCOMDisebleBitName(r, vIndex)) = 1) Then
    IsModuleDisabled = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function is called from the ShowPage function 
'        und ist notwendig fuer die Anzeige von cboTxI.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iExp            der Index aus der For - Next - Schleife 
'-----------------------------------------------------------------------------
'Returns:
'        Text           vom Typ String
'*****************************************************************************
Function GetPrescalerResolution(r As ProjectData, vIndex As Variant, iExp As Integer) As String
  On Error Resume Next
  Dim Exponent As Integer
  
  GetPrescalerResolution = ""
  Exponent = 3 + iExp
  GetPrescalerResolution = "Fcpu / " & 2^Exponent
  GetPrescalerResolution = GetPrescalerResolution & " (Resolution: " & Format$(2^Exponent / GetCAPCOMModuleClock(r, vIndex), "0.000") & " µs)"

  
End Function 


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

  For i = 0 To 7
    If r.v("T" & s & "CON_T" & iTmr & "I") = i Then
      GetOverflow = (65536 - r.v("T" & iTmr & "REL_T" & iTmr & "REL")) * ( (8 * 2^(i) ) / GetCAPCOMModuleClock(r, vIndex) )
    End If
  Next i

End Function

'*****************************************************************************
'Description:
'        This function is called from the GPT1 module  
'        and calculate the maximum resolution [ms] for timer clock.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (0,1,7 or 8)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [µs]
'
'*****************************************************************************
Function GetMaxResolution(r As ProjectData, vIndex As Variant, iTmr As Integer) As Double
  On Error Resume Next
  Dim s As String
  If (vIndex = 1) Then
      s = "01"
  Else
      s = "78"
  End If
  
  GetMaxResolution = 65536 * 8 * 2^( r.v("T" & s & "CON_T" & iTmr & "I") ) / GetCAPCOMModuleClock(r, vIndex)

End Function 

'*****************************************************************************
'Description:
'        This function is called from the GPT1 module  
'        and calculate the maximum resolution [ms] for timer clock.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (0,1,7 or 8)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [µs]
'
'*****************************************************************************
Function GetMinResolution(r As ProjectData, vIndex As Variant, iTmr As Integer) As Double
  On Error Resume Next
  Dim s As String
  If (vIndex = 1) Then
      s = "01"
  Else
      s = "78"
  End If
  
  GetMinResolution = 8 * 2^( r.v("T" & s & "CON_T" & iTmr & "I") ) / GetCAPCOMModuleClock(r, vIndex)

End Function 

'*****************************************************************************
'Description:
'        This function is called from the OnShowPage function 
'        and calculate the value of the timer gegister TxREL.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        t               entered time [µs]
'        rIndex          
'             True       for overflow time
'             False      for underflow time    
'        TimerNr
'-----------------------------------------------------------------------------
'Returns:
'        Value of TxREL
'
'*****************************************************************************
Function GetTimerNrValue(r As ProjectData, vIndex As Variant, t As Variant, rIndex As Boolean, iTmr As Integer) As Long
  On Error Resume Next
  Dim i As Integer
  Dim s As String
  If (vIndex = 1) Then
      s = "01"
  Else
      s = "78"
  End If
  
  If (t > GetMaxResolution(r, vIndex, iTmr)) Then
    t = GetMaxResolution(r, vIndex, iTmr)
  End If
  If (t < GetMinResolution(r, vIndex, iTmr)) Then
    t = GetMinResolution(r, vIndex, iTmr)
  End If

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

End Function 


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

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

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

End Sub 

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

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

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

End Sub 

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

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

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

End Sub 
