'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\classes\FunctionClass.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
'        ----------------------------------------------------------------------
'        2.0   ahe         14.06.00     final version for C167CS
'
'        ----------------------------------------------------------------------
'        3.0   ahe         02.08.00     final version for C161CS/JC/JI
'                                       - no changes
'
'
'*****************************************************************************


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

'*****************************************************************************
'Description:
'        For each function which should be used for this module a line 
'        "If (Not func.AddFunction .." must be insert in this function.
'        For each parameter that should be used for this function a  
'        line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added function a project variable with the name 
'        "RTC_NAME_Functionname" must be exist in the *.vars file. 
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(vIndex As Variant, func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next 

  RefreshFunctionDescriptions = False

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sVarPre As String
  Dim sName As String

  sVarPre = "RTC"

  ' 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_RTC
  '------------------------------------------------------------------------------------------------
  sName = "RTC"
  sDescription = "This is the RTC interrupt service routine. It is called " & _
                 "up when timer 14 register overflows and is reloaded" & vbcrlf & _
                 "" & vbcrlf & _
                 "" & 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 timer (RTCH and RTCL) with the forwarded value."
  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", "RegValue", "ulong", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "GetTime"
  sDescription = "This function returns the contents of the RTC timer (RTCH and RTCL)."

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

  '------------------------------------------------------------------------------------------------
  ' Description for RTC_NAME_...
  '------------------------------------------------------------------------------------------------
  sName = "Reset"
  sDescription = "This function loads the RTC timer (RTCH and RTCL) with the value 0x00000000."

  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
  Dim sParaName As String
  Dim i As Integer

  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

 	
 	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 Registers object
'
'-----------------------------------------------------------------------------
'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
  Dim v As String
  
  sVarPre = "RTC"  
  sRegPre = "RTC"
  If (vIndex = "frtc") Then
      GetRTCModuleClock =  r.v("MAIN_OSC") / (32)
  Else
      GetRTCModuleClock =  r.v("MAIN_OSC") / (32 * 8)
  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
  On Error Resume Next
  Dim sVarPre As String

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


'*****************************************************************************
'Description:
'          This function update the RTC timer base (reload the T14REL) 
'
'-----------------------------------------------------------------------------
'Parameters:
'       r           As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'      None
'
'*****************************************************************************
Sub UpdateRTC(r As ProjectData)
  On Error GoTo UpdateRTCError  
  'On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim vTime As Variant
  Dim dValue As Double
  Dim vIndex As Variant

  sRegPre = "RTC"
  sVarPre = sRegPre
  vTime = r.v(sVarPre & "_Base")
  If (vTime > GetMaxTimeBase(r)) Then
    vTime = GetMaxTimeBase(r)
  End If
  If (vTime < GetMinTimeBase(r)) Then
    vTime = GetMinTimeBase(r)
  End If
  r.v(sVarPre & "_Base") = vTime
  
  r.l("T14REL_T14REL") = 65536 - r.v(sVarPre & "_Base") * GetRTCModuleClock(r, vIndex)
   
  Exit Sub
  
  UpdateRTCError:
  Errorbox "Can not calculate.", Err.Description
  Exit Sub
End Sub

'*****************************************************************************
'Description:
'          This function returns the min. time base [us] of the T14 periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'       r           As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'      GetMinTimeBase [us]
'
'*****************************************************************************
Function GetMinTimeBase(r As ProjectData) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim vIndex As Variant

  sRegPre = "RTC"
  sVarPre = sRegPre

  GetMinTimeBase = 1 / GetRTCModuleClock(r, vIndex)

End Function

'*****************************************************************************
'Description:
'          This function returns the max. time base [us] of the T14INT periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'       r           As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'      GetMaxTimeBase [us]
'
'*****************************************************************************
Function GetMaxTimeBase(r As ProjectData) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String 
  Dim vIndex As Variant

  sRegPre = "RTC"
  sVarPre = sRegPre

  GetMaxTimeBase = 65536 / GetRTCModuleClock(r, vIndex)

End Function


'*****************************************************************************
'Description:
'          This function returns the real time base [us] of the T14INT periode 
'
'-----------------------------------------------------------------------------
'Parameters:
'       r           As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'      GetRealTimeBase [us]
'
'*****************************************************************************
Function GetRealTimeBase(r As ProjectData) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim vIndex As Variant

  sRegPre = "RTC"
  sVarPre = sRegPre

  GetRealTimeBase = (65536 - r.v("T14REL_T14REL")) / GetRTCModuleClock(r, vIndex)

End Function


'*****************************************************************************
'Description:
'        This function checks if the PLL request flag PLLIE is set
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'       False       if PLLIE is set
'       True        else
'*****************************************************************************
Function IsPLLSubNodeIntDisabled(r As ProjectData) As Boolean
  On Error Resume Next
  Dim sRegPre As String
  
  IsPLLSubNodeIntDisabled = False            

  If (r.v("ISNC_PLLIE") = 0) Then
    IsPLLSubNodeIntDisabled = 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      As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'       None
'
'*****************************************************************************
Sub DisableRTCInt(r As ProjectData)
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sRegPre = "RTC"
  sVarPre = "RTC"

  r.v("ISNC_" & sRegPre & "IE") = 0

  If (IsPLLSubNodeIntDisabled(r)) Then
    Call DisableSRN(r, sVarPre, "XP3IC", "XP3IE", "RTC")
  End If

End Sub 

'*****************************************************************************
'Description:
'        This function checks if all sub node interrupts checks are disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'       False       if all error cheks disabled
'       True        else
'*****************************************************************************
Function IsSubNodeIntDisabled(r As ProjectData) As Boolean
  On Error Resume Next
  Dim sRegPre As String
  sRegPre = "RTC"
  
  IsSubNodeIntDisabled = False

End Function





