'#Uses "Include.inc"

Option Explicit
'*****************************************************************************
' Module:
'        RTC
'-----------------------------------------------------------------------------
' Description:
'        This file contains the 'Update' functions for the module.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.0  ahe            25.01.00    final version for C165 UTAH
'        2.1  ste            28.02.00    many changes for the TC1775  
'
'        ----------------------------------------------------------------------
'        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
'
'*****************************************************************************


'*****************************************************************************
'Description:
'        This function is called by the main module in order to reset or update
'        the project variables and register contents of this peripheral
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data variables
'        vIndex                 a parameter
'        lngUpdateDescriptor    identification of the update context
'                  0          = initialization for a new project
'                  1, 2, 3... = value that identifies which project settings have
'                               been changed (e.g. CPU-Type, CPU-Clock etc.)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateProjectData(r As ProjectData, vIndex As Variant, lngUpdateDescriptor As Long) As Boolean
    On Error GoTo UpdateProjectDataError

    Dim sVarPre As String
    Dim sRegPre As String
    Dim dFactor As Double

    UpdateProjectData = False

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

    Select Case lngUpdateDescriptor
        Case 0 
            ' init reset values for registers
            
            r.v(sVarPre & "_CFile")             = sVarPre & ".C"
            r.v(sVarPre & "_HFile")             = sVarPre & ".H"
            r.v(sVarPre & "_Notes")             = ""
            r.v(sVarPre & "_NAME_Init")         = sVarPre & "_vInit"
            r.v(sVarPre & "_NAME_RTCRun")       = sVarPre & "_vRTCRun"
            r.v(sVarPre & "_NAME_RTCStop")      = sVarPre & "_vRTCStop"
            r.v(sVarPre & "_NAME_DecrementT14") = sVarPre & "_vDecrementT14"
            r.v(sVarPre & "_NAME_IncrementT14") = sVarPre & "_vIncrementT14"
            r.v(sVarPre & "_NAME_RTC")          = sVarPre & "_viRTC"
            r.v(sVarPre & "_NAME_SetTime")      = sVarPre & "_vSetTime"
            r.v(sVarPre & "_NAME_GetTime")      = sVarPre & "_ulGetTime"
            r.v(sVarPre & "_NAME_Reset")        = sVarPre & "_vReset"
            r.v(sVarPre & "_NAME_SetAsynchronousMode") = sVarPre & "_vSetAsynchronousMode"
            r.v(sVarPre & "_NAME_SetSynchronousMode") = sVarPre & "_vSetSynchronousMode"

            dFactor = 3600
            r.v(sRegPre & "CON_PRE") = 1
            r.v(sRegPre & "CON_RUN") = 1
            r.d(sVarPre & "_Base")  = 1 * 10^6   ' 1s
            r.d(sVarPre & "_Time0") = 60 * 10^6  ' 1 min
            r.d(sVarPre & "_Time1") = dFactor * 10^6' 1h
            r.d(sVarPre & "_Time2") = 24 * dFactor * 10^6' 1 day
            r.d(sVarPre & "_Time3") = 365 * 24 * dFactor * 10^6' 1 year
            r.d(sVarPre & "_OSC") = 0.032768
            
            Call UpdateRTC(r, vIndex)
            '...

        Case 1
            If (GetRTC_RefClk_Implementation(r, vIndex) = 1) Then        ' for XC164
              Call UpdateRTC(r, vIndex)
            ElseIf (GetRTC_RefClk_Implementation(r, vIndex) = 2) Then    ' for XC167, XC161
              ' rtc is independant from the CPU frequenz
            End If

            '...
       'Case...
    End Select

    UpdateProjectData = True
    Exit Function

UpdateProjectDataError:
    ErrorBox "Error in function UpdateProjectData", Err.Description
    Exit Function
End Function

