'#Uses "include.inc"

Option Explicit
'*****************************************************************************
' Module:
'        CAN
'-----------------------------------------------------------------------------
' Description:
'        This file contains the 'Update' functions for the module.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.1  ste            04.02.00    final version
'
'
'        ---------------------------------------------------------------------
'        3.0  ahe            05.11.01    final version for Pegasus
'
'        3.1  ahe            02.07.02    no changes
'
'*****************************************************************************


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

    UpdateProjectData = False

    Dim sRegPre As String
    Dim sVarPre As String
    Dim i As Integer

    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_"

    Select Case lngUpdateDescriptor
        Case 0 
            ' init reset values for registers
            r.v(sRegPre & "ACR_INIT") = 1
            r.v(sRegPre & "BCR_INIT") = 1

            r.sfr(sRegPre & "AECNTH") = &H0060
            r.sfr(sRegPre & "AECNTL") = &H0000

            r.sfr(sRegPre & "BECNTH") = &H0060
            r.sfr(sRegPre & "BECNTL") = &H0060

            For i = 0 To 31
              r.sfr(sRegPre & "MSGCTRH" & i)       = &H0000
              r.sfr(sRegPre & "MSGCTRL" & i)       = &H5555

              r.sfr(sRegPre & "MSGAMRH" & i)       = &HFFFF
              r.sfr(sRegPre & "MSGAMRL" & i)       = &HFFFF
            Next i

            ' init function, parameters and file names
            r.v(sVarPre & "_CFile") = sVarPre & ".C"
            r.v(sVarPre & "_HFile") = sVarPre & ".H"
            r.v(sVarPre & "_Notes") = "" 
            r.v(sVarPre & "_NAME_Init") = sVarPre & "_vInit"

            For i = 0 To 7
              r.v(sVarPre & "_NAME_SRN" & i)        = sVarPre & "_viSRN" & i
            Next i
            r.v(sVarPre & "_NAME_GetMsgObj")        = sVarPre & "_vGetMsgObj"
            r.v(sVarPre & "_NAME_RequestMsgObj")    = sVarPre & "_ubRequestMsgObj"
            r.v(sVarPre & "_NAME_NewData")          = sVarPre & "_ubNewData"
            r.v(sVarPre & "_NAME_Transmit")         = sVarPre & "_vTransmit"
            r.v(sVarPre & "_NAME_ConfigMsgObj")     = sVarPre & "_vConfigMsgObj"
            r.v(sVarPre & "_NAME_LoadData")         = sVarPre & "_vLoadData"
            r.v(sVarPre & "_NAME_DelMsgObj")        = sVarPre & "_ubDelMsgObj"
            r.v(sVarPre & "_NAME_MsgLost")          = sVarPre & "_ubMsgLost"
            r.v(sVarPre & "_NAME_ReleaseObj")       = sVarPre & "_vReleaseObj"
            r.v(sVarPre & "_NAME_SetMSGVAL")        = sVarPre & "_vSetMSGVAL"           
            r.v(sVarPre & "_NAME_WriteFIFO")        = sVarPre & "_ubWriteFIFO"
            r.v(sVarPre & "_NAME_ReadFIFO")         = sVarPre & "_ubReadFIFO"
            r.v(sVarPre & "_NAME_FlushReadFIFO")    = sVarPre & "_ubFlushReadFIFO"
            r.v(sVarPre & "_NAME_GetTxErrorCounter")         = sVarPre & "_ubGetTxErrorCounter"
            r.v(sVarPre & "_NAME_GetRxErrorCounter")         = sVarPre & "_ubGetRxErrorCounter"
            r.v(sVarPre & "_NAME_GetErrorTransferDirection") = sVarPre & "_ubGetErrorTransferDirection"
            r.v(sVarPre & "_NAME_GetErrorIncrement")         = sVarPre & "_ubGetErrorIncrement"



            ' set default settings
            r.v(sRegPre & "ABTRL_TSEG1")           = 4
            r.v(sRegPre & "ABTRL_TSEG2")           = 3
            r.v(sRegPre & "ABTRL_SJW")             = 1
            r.v(sVarPre & "_BaudrateNodeA")       = 100000   '100 kbaud
            Call UpdateCAN(r, vIndex, "A")
            r.v(sVarPre & "_Use_RxA") = 7
            r.v(sVarPre & "_Use_TxA") = 7

            r.v(sRegPre & "BBTRL_TSEG1")           = 4
            r.v(sRegPre & "BBTRL_TSEG2")           = 3
            r.v(sRegPre & "BBTRL_SJW")             = 1
            r.v(sVarPre & "_BaudrateNodeB")       = 100000   '100 kbaud
            Call UpdateCAN(r, vIndex, "B")
            r.v(sVarPre & "_Use_RxB") = 7
            r.v(sVarPre & "_Use_TxB") = 7


        Case 1
            Call UpdateCAN(r, vIndex, "A")
            Call UpdateCAN(r, vIndex, "B")
            '...
       'Case...
    End Select

    UpdateProjectData = True
    Exit Function

UpdateProjectDataError:
    Exit Function
End Function

