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

Option Explicit
'*****************************************************************************
' Module:
'        GPT1
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        GPT1 dialog script and in the GPT1 codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0  ahe            27.04.00     final version for C165UTAH, C165H
'
'        ----------------------------------------------------------------------
'        1.1  ahe            28.09.00     final version for C165UTAH, C165H
'                                         - no changes
'
'
'        ----------------------------------------------------------------------
'        4.0  ahe            19.04.01     final version for Pegasus
'
'        4.1  ahe            08.08.02     no changes
'        4.2  ste            16.12.02     no changes
'
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************
Global Const GPT12_ = "GPT12E_"


'*****************************************************************************
'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
'        "GPT1_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

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sVarPre As String
  Dim sFunctionName As String
  Dim iTmr As Integer
  
  sVarPre = "GPT1"

  RefreshFunctionDescriptions = False

  ' for each function of this module the property AddFunction must be called
  '
  ' AddFunction(sFuncName As String, sFuncType As String, sFuncDescription As String,
  '             sReturnvalue As String, lSort As Long) As Boolean
  '
  ' sFuncName          | String | function name as key (use the name of the variable
  '                               for the function name e.g. GPT1_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 GPT1_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription(sVarPre)
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_StartTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "StartTmr"
  sDescription = "This macro starts the selected " & sVarPre & " timer. The timer continues " & _
                 "to count from where it had stopped. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                 
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be started"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_StopTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "StopTmr"
  sDescription = "This macro stops the selected " & sVarPre & " timer. The contents of " & _
                 "the timer register remain unchanged. The remote control of the " & _
                 "selected timer is cleared. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                 
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be stopped"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_ClearTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ClearTmr"
  sDescription = "This macro stops the selected " & sVarPre & " timer and sets the timer " & _
                 "register to 0. The remote control of the selected timer is cleared. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be cleared"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_ReadTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ReadTmr"
  sDescription = "This macro returns the contents of the selected " & sVarPre & " timer. " & _
                 "The timer is not stopped. Access of the CPU to the " & _
                 "registers of the timer is of higher priority than a timer " & _
                 "increment, a reload or a capture event. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")

  sReturnValue = "Current 16-bit value of the timer"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be readed"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_LoadTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "LoadTmr"
  sDescription = "This macro loads the selected " & sVarPre & " timer with the forwarded " & _
                 "value. The timer is not stopped. The access of the CPU to " & _
                 "the registers of the timers is of higher priority than a " & _
                 "timer increment, a reload or a capture event. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
    sDescription = "16-bit value to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para2", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_Tmrx
  '------------------------------------------------------------------------------------------------
  iTmr = 2
  sFunctionName = "Tmr" & iTmr
  sDescription = "This is the interrupt service routine for the " & sVarPre & " timer " & iTmr & ". " & _
                 "It is called up in the case of over or underflow of the " & _
                 "timer " & iTmr & " register." & vbCrLf & _
                 "If the incremental interface mode is selected and the interrupt " & _
                 "for this mode is not disabled it is called up if count edge or " & _
                 "count direction was detected." & vbCrLf & _
                 ""

                 sDescription = sDescription & vbCrLf & TOOLTIP_ISR_USERCODE

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

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_Tmr3
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Tmr3"
  sDescription = "This is the interrupt service routine for the " & sVarPre & " timer 3. " & _
                 "It is called up in the case of over or underflow of the " & _
                 "timer 3 register." & vbCrLf & _
                 "If the incremental interface mode is selected it is called " & _
                 "up if count edge or count direction was detected." & vbCrLf & _
                 ""
                 sDescription = sDescription & vbCrLf & TOOLTIP_ISR_USERCODE

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

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_Tmrx
  '------------------------------------------------------------------------------------------------
  iTmr = 4
  sFunctionName = "Tmr" & iTmr
  sDescription = "This is the interrupt service routine for the " & sVarPre & " timer " & iTmr & ". " & _
                 "It is called up in the case of over or underflow of the " & _
                 "timer " & iTmr & " register." & vbCrLf & _
                 "If the incremental interface mode is selected and the interrupt " & _
                 "for this mode is not disabled it is called up if count edge or " & _
                 "count direction was detected." & vbCrLf & _
                 ""

                 sDescription = sDescription & vbCrLf & TOOLTIP_ISR_USERCODE

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

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_SetToggLatch
  '------------------------------------------------------------------------------------------------
  sFunctionName = "SetToggLatch"
  sDescription = "This macro sets the T3OTL (timer output toggle latch) bit " & _
                 "in timer 3 control register. This bit is toggled on each " & _
                 "over/underflow of timer 3. " & _
                 "If the alternative output function is enabled, the port " & _
                 "T3OUT is set depending on T3OTL. This macro sets the bit " & _
                 "T3OTL on high. This can influence the output signal at " & _
                 "T3OUT by SW."
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_ClrToggLatch
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ClrToggLatch"
  sDescription = "This macro resets the T3OTL (timer output toggle latch) bit " & _
                 "in timer 3 control register. This bit is toggled on each " & _
                 "over/underflow of timer 3. " & _
                 "If the alternative output function is enabled, the port " & _
                 "T3OUT is set depending on T3OTL. This macro sets the bit " & _
                 "T3OTL on low. This can influence the output signal at " & _
                 "T3OUT by SW."

  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_CountUp
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CountUp"
  sDescription = "This macro sets the counter direction of the selected timer " & _
                 "to up. The timer is not stopped." & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Concerning timer x it is possible to make the counter " & _
                 "direction dependent on the level at TxEUD. If this " & _
                 "functionality has been selected, then function " & _
                 r.v(sVarPre & "_NAME_CountUp") & "() " & _
                 "set a  0 at TxEUD enabling counting up of timer x and a " & _
                 "1 at TxEUD enabling counting down of timer x. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_CountDown
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CountDown"
  sDescription = "This macro sets the counter direction of the selected timer " & _
                 "to down. The timer is not stopped." & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Concerning timer x it is possible to make the counter " & _
                 "direction dependent on the level at TxEUD. If this " & _
                 "functionality has been selected, then function " & _
                 r.v(sVarPre & "_NAME_CountDown") & "() " & _
                 "set a 0 at TxEUD enabling counting down of timer x and " & _
                 "a 1 at TxEUD enabling counting up of timer x. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_CheckRotation
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CheckRotation"
  sDescription = "This function returns the status of the bit TxCHDIR of selected " & _
                 "TimerNr and resets the status bit. If return value is 1 change " & _
                 "of count direction was detected." & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Use this function only for TimerNr wich works in " & _
                 "incremental interface mode (rotation detection mode) " & _
                 "AND the interrupt for this mode is disabled." & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "1 if count direction was changed, else 0"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "The name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_CheckEdge
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CheckEdge"
  sDescription = "This function returns the status of the bit TxEDGE of selected " & _
                 "TimerNr and resets the status bit. If return value is 1 " & _
                 "successful edge was detected." & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Use this function only for TimerNr wich works in " & _
                 "incremental interface mode (edge detection mode) " & _
                 "AND the interrupt for this mode is disabled." & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "1 if successful edge was detected, else 0"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "The name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_GetCountDirection
  '------------------------------------------------------------------------------------------------
  sFunctionName = "GetCountDirection"
  sDescription = "This macro returns the status of the bit TxRDIR of selected " & _
                 "TimerNr. If return value is 0 the selceted timer counts up, " & _
                 "else the timer counts down." & _
                 "" & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Use this macro only for TimerNr wich works in " & _
                 "incremental interface mode." & _
                 "" & vbCrLf & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_3") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "0 if timer counts up, else 1"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT1_NAME_SetTmrRemoteControl
  '------------------------------------------------------------------------------------------------
  sFunctionName = "SetTmrRemoteControl"
  sDescription = "This macro sets the remote control bit of selected timer. In this mode " & _
                 "the timer is controlled by the run bit of timer T3. To reset this " & _
                 "mode call the macros " & r.v(sVarPre & "_NAME_StopTmr") & " or " & _
                 r.v(sVarPre & "_NAME_ClearTmr") & "." & _
                 "" & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Use this macro only for TimerNr wich is stopped" & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_2") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_4")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "The name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_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 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

  sVarPre = "GPT1" & vIndex
  sRegPre = sVarPre & "_"


  ' 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

  For i = 2 To 4
      '------------------------------------------------------------------------------------------------
      ' Description for DUMMY_PARA_xx
      '------------------------------------------------------------------------------------------------
      sDescription = "This parameter identifies " & sVarPre & " timer " & i
      sParaName = "TIMER_" & 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 (in MHz) of the
'          GPT1 module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      GetPeripheralClock
'
'*****************************************************************************
Function GetGPT1ModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next

  GetGPT1ModuleClock = GetPeripheralClock(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 = "GPT1"
  
  GetTooltipModuleDisabled = ""
  GetTooltipModuleDisabled = GetTooltipModuleDisabled & _
    FormatDisabledToolTip("the module is disabled")
End Function

'*****************************************************************************
'Description:
'        This function check  the GPT12E_KSCCFG register, whether the modul is disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        sVarPre         modul name (e.g GPT1)
'-----------------------------------------------------------------------------
'Returns:
'        True  -  if the modul is disabled
'        False -  else
'
'*****************************************************************************
Function IsModuleDisabled(r As ProjectData, sVarPre As String) As Boolean

  IsModuleDisabled = False

  If Not (GetGPT1DisebleBitName(r, sVarPre) = "") Then
      If (r.v("GPT12E_KSCCFG_" & GetGPT1DisebleBitName(r, sVarPre)) = 0)Then
        IsModuleDisabled = True
      End If
  End If

End Function

'*****************************************************************************
'Description:
'        This function returns the disable bit name of SYSCON3 register (e.g GPTDIS).
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        sVarPre         module name
'
'-----------------------------------------------------------------------------
'Returns:
'       disable bit name
'*****************************************************************************
Function GetGPT1DisebleBitName(r As ProjectData, sVarPre As String) As String
    On Error Resume Next

   If (r.Regs.BitFind("GPT12E_KSCCFG_MODEN") = True) Then
        GetGPT1DisebleBitName = "MODEN"
    Else
        GetGPT1DisebleBitName = ""
    End If


End Function



'*****************************************************************************
'Description:
'        This function returns the prescaler factor from the bit field BPS1.
'-----------------------------------------------------------------------------
'Parameters:
'         i   value of the bit field BPS1.
'
'-----------------------------------------------------------------------------
'Returns:
'       prescaler factor as integer
'*****************************************************************************
Function GetBPS1Factor(i As Integer) As Integer
  Dim iaBPS1(4) As Integer

  iaBPS1(0) = 8
  iaBPS1(1) = 4
  iaBPS1(2) = 32
  iaBPS1(3) = 16

  GetBPS1Factor = iaBPS1(i)

End Function

'*****************************************************************************
'Description:
'        This function sets the GPT1 module to the default.
'-----------------------------------------------------------------------------
'Parameters:
'         r
'
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub SetModuleToDefault(r As ProjectData)

    Dim sVarPre   As String
    Dim bDisabled As Boolean
    Dim vIndex    As Variant
    Dim TimerNr   As Variant
    Dim i         As Integer
    
    vIndex = ""
    sVarPre = "GPT1"

    r.v("GPT1_USE_Init") = 0
    r.v("GPT1_USE_StartTmr") = 0
    r.v("GPT1_USE_StopTmr") = 0
    r.v("GPT1_USE_ClearTmr") = 0
    r.v("GPT1_USE_ReadTmr") = 0
    r.v("GPT1_USE_LoadTmr") = 0
    r.v("GPT1_USE_SetToggLatch") = 0
    r.v("GPT1_USE_ClrToggLatch") = 0
    r.v("GPT1_USE_CountUp") = 0
    r.v("GPT1_USE_CountDown") = 0
    r.v("GPT1_USE_SetTmrRemoteControl") = 0
    r.v("GPT1_USE_GetCountDirection") = 0
    r.v("GPT1_USE_CheckRotation") = 0
    r.v("GPT1_USE_CheckEdge") = 0
    
    'Timer 2--------------------------------------------------------------
    TimerNr = 2
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "I") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = 0

    If (GetPinFunction(r,  "GPT12E_"  & "T" & TimerNr & "IN_I") = 1) Then
      Call ReleaseAlternateInputPin(r, "GPT12E_"  & "T" & TimerNr & "IN_I")
    End If

    If (GetPinFunction(r, "GPT12E_"  & "T" & TimerNr & "EUD_I") = 1) Then
      Call ReleaseAlternateInputPin(r, "GPT12E_"  & "T" & TimerNr & "EUD_I")
    End If
    Call DisableSRN(r, "GPT1", GPT12_ & "T" & TimerNr & "IC", "Tmr" & TimerNr)
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") = 0

    'Timer 4--------------------------------------------------------------
    TimerNr = 4
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "I") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = 0

    If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "IN_I") = 1) Then
      Call ReleaseAlternateInputPin(r, "GPT12E_"  & "T" & TimerNr & "IN_I")
    End If

    If (GetPinFunction(r, "GPT12E_"  & "T" & TimerNr & "EUD_I") = 1) Then
      Call ReleaseAlternateInputPin(r,"GPT12E_"  & "T" & TimerNr & "EUD_I")
    End If
    Call DisableSRN(r, "GPT1", GPT12_ & "T" & TimerNr & "IC", "Tmr" & TimerNr)
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") = 0

    'Timer 3--------------------------------------------------------------
    TimerNr = 3
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "I") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = 0

    If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "IN_I") = 1) Then
      Call ReleaseAlternateInputPin(r,"GPT12E_" & "T" & TimerNr & "IN_I")
    End If

    If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "EUD_I") = 1) Then
      Call ReleaseAlternateInputPin(r, "GPT12E_" & "T" & TimerNr & "EUD_I")
    End If

    Call DisableSRN(r, "GPT1", GPT12_ & "T" & TimerNr & "IC", "Tmr" & TimerNr)

    If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "OUT_O_1") = 2) Then
      Call ReleaseAlternateOutputPin(r, "GPT12E_" & "T" & TimerNr & "OUT_O_1")
    End If
    If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "OUT_O_2") = 2) Then
      Call ReleaseAlternateOutputPin(r, "GPT12E_" & "T" & TimerNr & "OUT_O_2")
    End If
     If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "OUT_O_3") = 2) Then
      Call ReleaseAlternateOutputPin(r, "GPT12E_" & "T" & TimerNr & "OUT_O_3")
    End If

    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "OE") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "OTL") = 0

End Sub

