'#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:
'        GPT2
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the 
'        GPT2 dialog script and in the GPT2 codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        2.0   ahe         13.06.00     final version for C167CS
'
'        ----------------------------------------------------------------------
'        2.1   ahe         02.08.00     final version for C167CS and C161CS/JC/JI
'                                       - check if TxEUD exist in functions
'                                         CuntUp and CountDown
'                                       - changes 'GPT2_vCountUp' to 
'                                         r.v(sVarPre & "_USE_CountUp")
'                                       - changes 'GPT2_vCountDown' to 
'                                         r.v(sVarPre & "_USE_CountDown")
'
'              ahe         13.09.00     - changes the functions descriptions
'
'        ---------------------------------------------------------------------
'              ahe         25.10.00    final version for 
'                                      - C167CS
'                                      - C161CS/JC/JI
'                                      - C167CR/SR r2
'
'              ahe         25.10.00    changes IsModuleDisabled (check SYSCON3) 
'
'        ----------------------------------------------------------------------
'        5.0   ahe         19.04.01     final version for Pegasus
'
'        5.1   ahe         08.08.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 
'        "GPT2_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
  
  sVarPre = "GPT2"


  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. GPT2_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 GPT2_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription(sVarPre)
  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_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_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                 
  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 GPT2_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_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                 
  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 GPT2_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_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  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 GPT2_NAME_ReadTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ReadTmr"
  sDescription = "This macro returns the contents of the selected GPT2 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_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")

  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 GPT2_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_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  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 GPT2_NAME_ReadCAPREL
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ReadCAPREL"
  sDescription = "This macro returns the content of the " & _
                 GPT12_ & "CAPREL register. " & _
                 "This can be the capture value of timer 5 or " & _
                 "the reload value of timer 6."

  sReturnValue = "Current 16-bit value of the " & GPT12_ & "CAPREL register"

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

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_LoadCAPREL
  '------------------------------------------------------------------------------------------------
  sFunctionName = "LoadCAPREL"
  sDescription = "This macro loads the " & GPT12_ & "CAPREL register " & _
                 "with the forwarded value."

  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "16-bit value to loaded in the " & GPT12_ & "CAPREL register"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_Tmr5
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Tmr5"
  sDescription = "This is the interrupt service routine for the " & sVarPre & " timer 5. " & _
                 "It is called up in the case of over or underflow of the " & _
                 "timer 5 register." & 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 GPT2_NAME_Tmr6
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Tmr6"
  sDescription = "This is the interrupt service routine for the " & sVarPre & " timer 6. " & _
                 "It is called up in the case of over or underflow of the " & _
                 "timer 6 register." & 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 GPT2_NAME_CAPREL
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CAPREL"
  sDescription = "This is the interrupt service routine for the " & sVarPre & " CAPREL. " & _
                 "It is called up in the case of over or underflow of the " & _
                 GPT12_ & "CAPREL register." & 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 GPT2_NAME_SetToggLatch
  '------------------------------------------------------------------------------------------------
  sFunctionName = "SetToggLatch"
  sDescription = "This macro sets the T6OTL (timer output toggle latch) bit " & _
                 "in timer 6 control register. This bit is toggled on each over/underflow of timer 6. " & _
                 "If the alternative output function is enabled, the port " & _
                 "T6OUT is set depending on T6OTL. This macro sets the bit " & _
                 "T6OTL on high. This can influence the output signal at " & _
                 "T6OUT by SW." 
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function

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

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

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_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 & _
                 ""
                 If (r.Regs.BitFind(GPT12_ & "T5CON_T5UDE") = True Or _
                     r.Regs.BitFind(GPT12_ & "T6CON_T6UDE") = True) Then
                     sDescription = sDescription & _
                     "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. "
                 End If
                 sDescription = sDescription & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  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 GPT2_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 & _
                 ""
                 If (r.Regs.BitFind(GPT12_ & "T5CON_T5UDE") = True Or _
                     r.Regs.BitFind(GPT12_ & "T6CON_T6UDE") = True) Then
                     sDescription = sDescription & _
                     "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. "
                 End If
                 sDescription = sDescription & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  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 GPT2_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 T6. 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_5")
                
  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 = "GPT2" & 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 = 5 To 6
      '------------------------------------------------------------------------------------------------
      ' 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 
'          GPT2 module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      GetPeripheralClock
'
'*****************************************************************************
Function GetGPT2ModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Goto GetGPT2ModuleClockError
  GetGPT2ModuleClock = GetPeripheralClock(r)
  Exit Function

  GetGPT2ModuleClockError:
  GetGPT2ModuleClock = 1
  Exit Function

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 GetGPT2DisebleBitName(r As ProjectData, sVarPre As String) As String
    On Error Resume Next

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


End Function

'*****************************************************************************
'Description:
'        This function check  the SYSCON3 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(GetGPT2DisebleBitName(r, sVarPre) = "") Then
      If (r.v("GPT12E_KSCCFG_" & GetGPT2DisebleBitName(r, sVarPre)) = 0) Then
        IsModuleDisabled = True
      End If
  End If



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

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



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

  iaBPS2(0) = 4
  iaBPS2(1) = 2
  iaBPS2(2) = 16
  iaBPS2(3) = 8

  GetBPS2Factor = iaBPS2(i)

End Function

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

    Dim TimerNr   As Variant
    Dim i         As Integer
    
    '---------------------------------------- GPT2_vInit = 0
    r.v("GPT2_USE_Init") = 0
    r.v("GPT2_USE_StartTmr") = 0
    r.v("GPT2_USE_StopTmr") = 0
    r.v("GPT2_USE_ClearTmr") = 0
    r.v("GPT2_USE_ReadTmr") = 0
    r.v("GPT2_USE_LoadTmr") = 0
    r.v("GPT2_USE_ReadCAPREL") = 0
    r.v("GPT2_USE_LoadCAPREL") = 0
    r.v("GPT2_USE_SetToggLatch") = 0
    r.v("GPT2_USE_ClrToggLatch") = 0
    r.v("GPT2_USE_CountUp") = 0
    r.v("GPT2_USE_CountDown") = 0
    r.v("GPT2_USE_SetTmrRemoteControl") = 0
    

    'Timer 5--------------------------------------------------------------
    TimerNr = 5             
    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 & "SC") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CLR") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CC") = 0

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

    Call DisableSRN(r, "GPT2", 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

    If (r.Regs.BitFind(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = True) Then
        r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = 0 
        If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "EUD_I") = 1) Then
          Call ReleaseAlternateInputPin(r, "GPT12E_"  &  "T" & TimerNr & "EUD_I")
        End If
    End If


    '---------------------------------------- T5CON_CI = 0
    If (r.v(GPT12_ & "T5CON_CT3") = 1) Then
      If (r.v(GPT12_ & "T5CON_CI") = 1) Then
        Call ReleaseAlternateInputPin(r, "GPT12E_T3IN_I")
      ElseIf (r.v(GPT12_ & "T5CON_CI") = 2) Then
        Call ReleaseAlternateInputPin(r, "GPT12E_T3EUD_I")
      ElseIf (r.v(GPT12_ & "T5CON_CI") = 3) Then
        Call ReleaseAlternateInputPin(r, "GPT12E_T3EUD_I")
        Call ReleaseAlternateInputPin(r, "GPT12E_T3IN_I")
      End If
    Else
      If (r.v(GPT12_ & "T5CON_CI") > 0) Then
        Call ReleaseAlternateInputPin(r, "GPT12E_CAPIN_I")
      End If
    End If
    r.v(GPT12_ & "T5CON_CI") = 0

    'Timer 6--------------------------------------------------------------
    TimerNr = 6
    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 & "CLR") = 0
    r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "SR") = 0

    If (r.Regs.BitFind(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = True) Then
        r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = 0 
        If (GetPinFunction(r, "GPT12E_" & "T" & TimerNr & "EUD_I") = 1) Then
          Call ReleaseAlternateInputPin(r, "GPT12E_" & "T" & TimerNr & "EUD_I")
        End If
    End If

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

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

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

   If (GetPinFunction(r, "GPT12E_"  & "T" & TimerNr & "OUT_o") = 2) Then
       Call ReleaseAlternateOutputPin(r,"GPT12E_"  &  "T" & TimerNr & "OUT_o")
     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

    '---------------------------------------- CAPREL ISR = 0
    Call DisableSRN(r, "GPT2", GPT12_ & "CRIC", "CAPREL")



End Sub

