'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\include\Format.inc"

Option Explicit

'*****************************************************************************
' Module:
'        RTC
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the 
'        RTC dialog script and in the RTC codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'             ahe            25.01.00    final version for C165 UTAH
'             ste            28.02.00    many changes for the TC1775  
'        2.1  ste            09.03.00    function "RefreshParameterDescriptions" added
'
'        ----------------------------------------------------------------------
'        4.0  ahe            24.04.01    final version Pegasus
'
'        4.1  ahe            08.08.02    no changes
'        ----------------------------------------------------------------------
'             ste            13.10.03    the XC164 uses the Main OSC as RTC
'                                        input clock
'
'*****************************************************************************


'*****************************************************************************
' 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 
'        "RTC_NAME_Functionname" must be exist in the *.vars file. 
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the ProjectData 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 sName         As String

  sVarPre = "RTC" & vIndex

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

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "RTCRun"
  sDescription = "This function starts the " & sVarPre & " timer."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "RTCStop"
  sDescription = "This function stops the " & sVarPre & " timer."

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

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "DecrementT14"
  sDescription = "This function sets the bit T14DEC. In this case timer T14 decrement " & _
                 "with the next count event." & _
                 "" & vbcrlf & _
                 "Note:" & vbcrlf & _
                 "The bit T14DEC is only set if RTC_T14REL is not equal to 0xFFFF. " & _
                 "This bit is cleared by hardware " & _
                 "after the decrement operation."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "IncrementT14"
  sDescription = "This function set the bit T14INC. In this case timer T14 increment " & _
                 "with the next count event." & _
                 "" & vbcrlf & _
                 "Note:" & vbcrlf & _
                 "The bit T14INC is only set if RTC_T14REL is not equal to 0xFFFF. " & _
                 "This bit is cleared by hardware " & _
                 "after the decrement operation."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_RTC
  '------------------------------------------------------------------------------------------------
  sName = "RTC"
  sDescription = "This is the " & sVarPre & " interrupt service routine. It is called " & _
                 "up when:" & vbcrlf & _
                 " - counter T14 overflows and is reloaded" & vbcrlf & _
                 " - counter CNT0 overflows and is reloaded" & vbcrlf & _
                 " - counter CNT1 overflows and is reloaded" & vbcrlf & _
                 " - counter CNT2 overflows and is reloaded" & vbcrlf & _
                 " - counter CNT3 overflows and is reloaded" & vbcrlf & _
                 "This interrupt request may eg. be used to provide a system " & _
                 "time tick independent of the CPU frequency, or to wake up " & _
                 "regularly from idle mode." & vbcrlf & _
                 "" & vbcrlf & _
               	 TOOLTIP_ISR_USERCODE

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

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "SetTime"
  sDescription = "This function loads the RTC reload register (RTC_RELH and RTC_RELL) " & _
                 "with the forwarded value. " & _
                 "If the parameter ubMode is '1' the RTC timer (RTC_RTCH and RTC_RTCL) " & _
                 "are reloaded too."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "RTCL and RTCH load value"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sName & "_Par1", "ulRegValue", "ulong", sDescription)) Then Exit Function
    sDescription = "1 if RTCH and RTCL should be reloaded, 0 else"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sName & "_Par2", "ubMode", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "GetTime"
  sDescription = "This function returns the contents of the " & sVarPre & _
                 " timer (RTC_RTCL and RTC_RTCH)."

  sReturnValue = "Value of the timer"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "ulong", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "Reset"
  sName = "Reset"
  sDescription = "This function loads the RTC timer (RTC_RTCH and RTC_RTCL) and the reload register " & _
                 "(RTC_RELH and RTC_RELL) with the value 0x0000."

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

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_vSetAsynchronousMode
  '------------------------------------------------------------------------------------------------
  sName = "SetAsynchronousMode"
  sDescription = "This function sets the asynchronous mode for the " & sVarPre & " timer. " & _
                 "In this mode no register write access is possible. "

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

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_vSetSynchronousMode
  '------------------------------------------------------------------------------------------------
  sName = "SetSynchronousMode"
  sDescription = "This function sets the synchronous mode for the " & sVarPre & " timer. " & _
                 "In this mode register write access is possible. "

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) 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

  sVarPre = "RTC" & 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

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
'  sDescription = TOOLTIP_ENABLED_PARAMETER_NAME
'  If (Not para.AddParameter(sVarPre & "_PARA_Para1", sDescription)) Then Exit Function

' 	If (Not para.Sort) Then Exit Function

  RefreshParameterDescriptions = True
End Function


'*****************************************************************************
'Description:
'          This function returns the module clock [MHz] of the 
'          RTC module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'                        
'-----------------------------------------------------------------------------
'Returns:
'      GetRTCSystemClock [MHz]
'
'*****************************************************************************
Function GetRTCModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  
  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  If (GetRTC_RefClk_Implementation(r, vIndex) = 1) Then        ' for XC164
    GetRTCModuleClock =  (r.v("MAIN_OSC") / 32) / (1 + 7 * r.v(sRegPre & "CON_PRE"))
  ElseIf (GetRTC_RefClk_Implementation(r, vIndex) = 2) Then    ' for XC167, XC161
    GetRTCModuleClock =  r.v(sVarPre & "_OSC") / (1 + 7 * r.v(sRegPre & "CON_PRE"))
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if the module clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            the module clock is disabled
'        False           else
'*****************************************************************************
Function IsModuleClockDisabled(r As ProjectData, vIndex As Variant) As Boolean
  On Error Resume Next
  Dim sIndex  As String
  Dim sPrefix As String

  sIndex = "RTC" & vIndex
  sPrefix = sIndex & "_"
  
  IsModuleClockDisabled = False            
  'IsModuleClockDisabled = r.v(sPrefix & "CLC_DISR") = 1
End Function


'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipModuleClockDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  GetTooltipModuleClockDisabled = ""            
  
End Function


'*****************************************************************************
'Description:
'          This function update the RTC timer base (reload the T14REL) 
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'
'-----------------------------------------------------------------------------
'Returns:
'      None
'
'*****************************************************************************
Sub UpdateRTC(r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim sVarPre   As String
  Dim sRegPre   As String
  Dim vTime     As Variant
  Dim dValue    As Double
  Dim i         As Integer
  Dim cValue    As Currency
  Dim lMaxValue As Long

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  vTime = r.v(sVarPre & "_Base")
  If (vTime > GetMaxTimeBase(r, vIndex)) Then
    vTime = GetMaxTimeBase(r, vIndex)
  End If

  If (vTime < GetMinTimeBase(r, vIndex)) Then
    vTime = GetMinTimeBase(r, vIndex)
  End If
  r.v(sVarPre & "_Base") = vTime
  
  r.sfr(sRegPre & "T14REL") = _
    65536 - r.v(sVarPre & "_Base") * GetRTCModuleClock(r, vIndex)
   
  '-----------------------------
  'RTC0
  '-----------------------------
  i = 0
  vTime = r.v(sVarPre & "_Time" & i)
  If (vTime < GetRealTimeBase(r, vIndex)) Then
    vTime = GetRealTimeBase(r, vIndex)
  End If
  If (vTime > (GetRealTimeBase(r, vIndex) * 1024)) Then
    vTime = GetRealTimeBase(r, vIndex) * 1024
  End If
  r.v(sVarPre & "_Time" & i) = vTime

  r.l(sVarPre & "_RTC" & i) = 1024 - vTime / r.v(sVarPre & "_Base")

  If (r.l(sVarPre & "_RTC" & i) < 0) Then
    r.l(sVarPre & "_RTC" & i) = 0
  End If

  For i = 1 To 3
    If (i = 3) Then
      lMaxValue = &h3ff + 1
    Else
      lMaxValue = &h3f + 1
    End If

    '-----------------------------
    'RTCx (x = 1-3) 
    '-----------------------------
    vTime = r.v(sVarPre & "_Time" & i)
    If (vTime < GetRealTime(r, vIndex, i - 1)) Then
      vTime = GetRealTime(r, vIndex, i - 1)
    End If
    If (vTime > (GetRealTime(r, vIndex, i - 1) * lMaxValue)) Then
      vTime = GetRealTime(r, vIndex, i - 1) * lMaxValue
    End If
    r.v(sVarPre & "_Time" & i) = vTime

    r.l(sVarPre & "_RTC" & i) = lMaxValue - (vTime / r.v(sVarPre & "_Time" & i - 1))

    If (r.l(sVarPre & "_RTC" & i) < 0) Then
      r.l(sVarPre & "_RTC" & i) = 0
    End If

  Next i

  cValue = r.l(sVarPre & "_RTC0") + _
           r.l(sVarPre & "_RTC1") * 2^10

  r.sfr(sRegPre & "RELL") = cValue 

  cValue = r.l(sVarPre & "_RTC2") + _
           r.l(sVarPre & "_RTC3") * 2^6

  r.sfr(sRegPre & "RELH") = cValue 

End Sub

'*****************************************************************************
'Description:
'          This function returns the min. time base [us] of the T14 periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'
'-----------------------------------------------------------------------------
'Returns:
'      GetMinTimeBase [us]
'
'*****************************************************************************
Function GetMinTimeBase(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  GetMinTimeBase = 1 / GetRTCModuleClock(r, vIndex)

End Function

'*****************************************************************************
'Description:
'          This function returns the max. time base [us] of the T14INT periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'
'-----------------------------------------------------------------------------
'Returns:
'      GetMaxTimeBase [us]
'
'*****************************************************************************
Function GetMaxTimeBase(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String 

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  GetMaxTimeBase = 65536 / GetRTCModuleClock(r, vIndex)

End Function


'*****************************************************************************
'Description:
'          This function returns the real time base [us] of the T14INT periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'
'-----------------------------------------------------------------------------
'Returns:
'      GetRealTimeBase [us]
'
'*****************************************************************************
Function GetRealTimeBase(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  GetRealTimeBase = (65536 - r.sfr(sRegPre & "T14REL")) / GetRTCModuleClock(r, vIndex)

End Function


'*****************************************************************************
'Description:
'          This function returns the min. time base [us] of the RTCxINT periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'        iNumber      number of the counter part
'
'-----------------------------------------------------------------------------
'Returns:
'      GetMinTimeBase [us]
'
'*****************************************************************************
Function GetMinTime(r As ProjectData, vIndex As Variant, iNumber as Integer) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  If (iNumber = 0) Then
    GetMinTime = GetRealTimeBase(r, vIndex)
  Else
    GetMinTime = GetRealTime(r, vIndex, iNumber - 1)
  End If

End Function


'*****************************************************************************
'Description:
'          This function returns the max. time base [us] of the RTCxINT periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'        iNumber      number of the counter part
'
'-----------------------------------------------------------------------------
'Returns:
'      GetMaxTime [us]
'
'*****************************************************************************
Function GetMaxTime(r As ProjectData, vIndex As Variant, iNumber As Integer) As Double
  On Error Resume Next
  Dim sVarPre   As String
  Dim sRegPre   As String
  Dim lMaxValue As Long

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  If (iNumber = 0 Or iNumber = 3) Then
    lMaxValue = &h3ff + 1
  Else
    lMaxValue = &h3f + 1
  End If

  If (iNumber = 0) Then
    GetMaxTime = GetRealTimeBase(r, vIndex) * lMaxValue
  Else
    GetMaxTime = GetRealTime(r, vIndex, iNumber - 1) * lMaxValue
  End If

End Function


'*****************************************************************************
'Description:
'          This function returns the real time base [us] of the RTCxINT periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'        iNumber      number of the counter part
'
'-----------------------------------------------------------------------------
'Returns:
'      GetRealTime0 [us]
'
'*****************************************************************************
Function GetRealTime(r As ProjectData, vIndex As Variant, iNumber As Integer) As Double
  On Error Resume Next
  Dim sVarPre   As String
  Dim sRegPre   As String
  Dim lMaxValue As Long

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  If (iNumber = 0 Or iNumber = 3) Then
    lMaxValue = &h3ff + 1
  Else
    lMaxValue = &h3f + 1
  End If

  If (iNumber = 0) Then
    GetRealTime = GetRealTimeBase(r, vIndex) * _
      (lMaxValue - r.v(sVarPre & "_RTC" & iNumber))
  Else
    GetRealTime = GetRealTime(r, vIndex, iNumber - 1) * _
      (lMaxValue - r.v(sVarPre & "_RTC" & iNumber))
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if all sub node interrupts checks are disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'
'-----------------------------------------------------------------------------
'Returns:
'       False       if all error cheks disabled
'       True        else
'*****************************************************************************
Function IsSubNodeIntDisabled(r As ProjectData, vIndex As Variant) As Boolean
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"
  
  IsSubNodeIntDisabled = False            
  If (r.v(sRegPre & "ISNC_0IE") = 0 AND _
      r.v(sRegPre & "ISNC_1IE") = 0 AND _
      r.v(sRegPre & "ISNC_2IE") = 0 AND _
      r.v(sRegPre & "ISNC_3IE") = 0 AND _
      r.v(sRegPre & "ISNC_T14IE") = 0) Then

    IsSubNodeIntDisabled = True
  End If
End Function


'*****************************************************************************
'Description:
'        This function set the request flag T14 to 0 if not RTC int. selected
'        If the PLL request flag is reset then this function disabled the ISR
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'
'-----------------------------------------------------------------------------
'Returns:
'       None
'
'*****************************************************************************
Sub DisableRTCInt(r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  If (IsSubNodeIntDisabled(r, vIndex) = True) Then
    Call DisableSRN(r, sVarPre, sRegPre & "IC", "RTC")
  End If

End Sub 


'*****************************************************************************
'Description:
'        This function disable the RTC module.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r            reference to the ProjectData object
'        vIndex       parameter which was added with InstallModul in Install.inc
'
'-----------------------------------------------------------------------------
'Returns:
'       None
'
'*****************************************************************************
Sub DisableRTC(r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"

  r.v(sRegPre & "ISNC_T14IE")   = 0
  r.v(sRegPre & "ISNC_0IE")  = 0
  r.v(sRegPre & "ISNC_1IE")  = 0
  r.v(sRegPre & "ISNC_2IE")  = 0
  r.v(sRegPre & "ISNC_3IE")  = 0
  Call DisableSRN(r, sVarPre, sRegPre & "IC", "RTC")

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

End Sub

'*****************************************************************************
'Description:
'        The RTC can be clocked by the MainOscillator or by an auxillary 
'        oscillator (32KHz). This is dependend on the implementation.
'        This function return a value for the controller specific implementation. 
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        0               none
'        1               there is only the MainOscillator implementation
'        2               there is only the auxillary oscillator implementation
'        3               for both (there is currently no contoller with both)
'        
'*****************************************************************************
Function GetRTC_RefClk_Implementation(r As ProjectData, vIndex As Variant) As Integer
  On Error Resume Next
  Dim sIndex  As String
  Dim sPrefix As String
  Dim sMC_Name As String

  sIndex = "RTC" & vIndex
  sPrefix = sIndex & "_" 
  GetRTC_RefClk_Implementation = 0

  sMC_Name = DefGetValue("Name")

  If (sMC_Name = "XC167CI") Then
    GetRTC_RefClk_Implementation = 2
  ElseIf (sMC_Name = "XC161CJ") Then
    GetRTC_RefClk_Implementation = 2
  ElseIf (sMC_Name = "XC164CS") Then
    GetRTC_RefClk_Implementation = 1
  End If


End Function


