Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Port.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\ParameterClass.cls
Option Explicit
'*****************************************************************************
' Module:
'        CAN
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        CAN dialog script and in the CAN codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0  ste            05.05.00    final version for TC1775A
'        1.1  ste            20.07.00    function GetNodexClock added, because
'                                        the TC1775B has a new clock divider for
'                                        each node;
'                                        4 new functions added (...Error..)
'        1.2  ste            02.11.00    1 new function added (SetMSGVAL)
'
'        ---------------------------------------------------------------------
'        3.0  ahe            05.11.01    final version for Pegasus
'
'        3.1  ahe            02.07.02    no changes
'        ---------------------------------------------------------------------
'        4.0                 26.08.04    final version for XC164CM Pegasus light
'
'*****************************************************************************


'*****************************************************************************
' @Constants
'*****************************************************************************
' use this constant in the Format statement for all calculated outputs like
' baud rates or deviation
Global Const CAN_PRECISION = "0.000"


'*****************************************************************************
' @Function descriptions
'*****************************************************************************

'*****************************************************************************
'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
'        "CAN_NAME_Functionname" 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 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 sRegPre As String
  Dim i As Integer
  Dim sFctName As String

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


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

  For i = 0 To 7
    '------------------------------------------------------------------------------------------------
    ' Description for CAN_NAME_SRNx
    '------------------------------------------------------------------------------------------------
    sFctName = "SRN" & i
    sDescription = "This is the interrupt service routine for the Service " & _
                        "Request Node " & i & " of the TwinCAN module."

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

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_GetMsgObj
  '------------------------------------------------------------------------------------------------
  sFctName = "GetMsgObj"
  sDescription = "This function fills the forwarded SW message object with the " & _
                 "content of the chosen HW message object." & vbCrLf & _
                 "" & vbCrLf & _
                 "The structure of the SW message object is defined in the " & vbCrLf & _
                 "header file " & r.v(sVarPre & "_HFILE") & " (see T" & sVarPre & "_SWObj)."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object to be read (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
    sDescription = "Pointer on a message object to be filled by this function"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "*pstObj", "T" & sVarPre & "_SWObj", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_RequestMsgObj
  '------------------------------------------------------------------------------------------------
  sFctName = "RequestMsgObj"
  sDescription = "If a TRANSMIT OBJECT is to be reconfigured it must first be " & _
                 "accessed. The access to the transmit object is exclusive. " & _
                 "This function checks whether the choosen message object is " & _
                 "still executing a transmit request, or if the object can " & _
                 "be accessed exclusively." & vbCrLf & _
                 "After the message object is reserved, it can be reconfigured " & _
                 "by using the function " & r.v(sVarPre & "_NAME_ConfigMsgObj") & " or " & _
                 r.v(sVarPre & "_NAME_LoadData") & "." & vbCrLf & _
                 "Both functions enable access to the object for the " & sVarPre & _
                 " controller. " & vbCrLf & _
                 "By calling the function " & r.v(sVarPre & "_NAME_Transmit") & " transfering of data " & _
                 "is started."

  sReturnValue = "0 message object is busy (a transfer is active), else 1"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_NewData
  '------------------------------------------------------------------------------------------------
  sFctName = "NewData"
  sDescription = "This function checks whether the selected RECEIVE OBJECT has " & _
                 "received a new message. If so the function returns the " & _
                 "value '1'."

  sReturnValue = "1 the message object has received a new message, else 0"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_Transmit
  '------------------------------------------------------------------------------------------------
  sFctName = "Transmit"
  sDescription = "This function triggers the " & sVarPre & " controller to send the " & _
                 "selected message." & vbCrLf & _
                 "If the selected message object is a TRANSMIT OBJECT then " & _
                 "this function triggers the sending of a data frame. " & _
                 "If however the selected message object is a RECEIVE OBJECT " & _
                 "this function triggers the sending of a remote frame."
        
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_ConfigMsgObj
  '------------------------------------------------------------------------------------------------
  sFctName = "ConfigMsgObj"
  sDescription = "This function sets up the message objects. This includes " & _
                 "the 8 data bytes, the identifier (11- or 29-bit), the acceptance mask (11- or 29-bit), the data " & _
                 "number (0-8 bytes), the frame counter value and the XTD-bit (standard or extended identifier).  " & _
                 "The direction bit (DIR), the NODE bit and the RMM (remote monitoring) bit can not be changed. " & vbCrLf & _
                 "The message is not sent; for this the function " & _
                 r.v(sVarPre & "_NAME_Transmit") & " must be called." & vbCrLf & _
                 "" & vbCrLf & _
                 "The structure of the SW message object is defined in the " & _
                 "header file " & r.v(sVarPre & "_HFILE") & " (see T" & sVarPre & "_SWObj)."
        
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object to be configured (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
    sDescription = "Pointer on a message object"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "*pstObj", "T" & sVarPre & "_SWObj", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_LoadData
  '------------------------------------------------------------------------------------------------
  sFctName = "LoadData"
  sDescription = "If a hardware TRANSMIT OBJECT has to be loaded with data " & _
                 "but not with a new identifier, this function may be used " & _
                 "instead of the function " & r.v(sVarPre & "_NAME_ConfigMsgObj") & ". " & _
                 "The message object should be accessed by calling the function " & _
                 r.v(sVarPre & "_NAME_RequestMsgObj") & " before calling this function. This " & _
                 "prevents the " & sVarPre & " controller from working with invalid data."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object to be configured (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
    sDescription = "Pointer on a data buffer"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "*pubData", "ubyte", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_MsgLost
  '------------------------------------------------------------------------------------------------
  sFctName = "MsgLost"
  sDescription = "If a RECEIVE OBJECT receives new data before the old object " & _
                 "has been read, the old object is lost. The " & sVarPre & " controller " & _
                 "indicates this by setting the message lost bit (MSGLST). " & _
                 "This function returns the status of this bit. " & vbCrLf & _
                 "" & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "This function resets the message lost bit (MSGLST)."
        
  sReturnValue = "1 the message object has lost a message, else 0"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_DelMsgObj
  '------------------------------------------------------------------------------------------------
  sFctName = "DelMsgObj"
  sDescription = "This function marks the selected message object as not valid. " & _
                 "This means that this object cannot be sent or receive data. " & _
                 "If the selected object is busy (meaning the object is " & _
                 "transmitting a message or has received a new message) this " & _
                 "function returns the value ""0"" and the object is not deleted."
        
  sReturnValue = "1 the message object was deleted, else 0"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_ReleaseObj
  '------------------------------------------------------------------------------------------------
  sFctName = "ReleaseObj"
  sDescription = "This function resets the NEWDAT flag of the selected RECEIVE " & _
                 "OBJECT, so that the " & sVarPre & " controller have access to it. " & _
                 "This function must be called if the function " & r.v(sVarPre & "_NAME_NewData") & _
                 " detects, that new data are present in the message object and " & _
                 "the actual data have been read by calling the function " & _
                 r.v(sVarPre & "_NAME_GetMsgObj") & ". "

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_SetMSGVAL
  '------------------------------------------------------------------------------------------------
  sFctName = "SetMSGVAL"
  sDescription = "This function sets the MSGVAL flag of the selected object. " & _
                 "This is only necessary if the single data transfer mode (SDT) for the selected " & _
                 "object is enabled. If SDT is set to '1', the " & sVarPre & " controller automatically " & _
                 "resets bit MSGVAL after receiving or tranmission of a frame."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the message object (0-31)"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_WriteFIFO
  '------------------------------------------------------------------------------------------------
  sFctName = "WriteFIFO"
  sDescription = "This function sets up the next free TRANSMIT message object " & _
                 "which is part of a FIFO. This includes the 8 data bytes, " & _
                 "the identifier (11- or 29-bit) and the data number (0-8 bytes). " & _
                 "The direction bit (DIR), the XTD-bit, the NODE bit and " & _
                 "the RMM bit can not be changed. The acceptance mask register " & _
                 "and the Frame Counter remains unchanged. " & _
                 "This function checks whether the choosen message object is " & _
                 "still executing a transmit request, or if the object can " & _
                 "be accessed exclusively. " & vbCrLf & _
                 "The structure of the SW message object is defined in the " & _
                 "header file " & r.v(sVarPre & "_HFILE") & " (see T" & sVarPre & "_SWObj)." & vbCrLf & _
                 "Note: " & vbCrLf & _
                 "This function can only used for TRANSMIT objects which are " & _
                 "configured for FIFO base functionality. "
        
  sReturnValue = "0: message object is busy (a transfer is active); " & _
                 "1: the message object was configured and the transmite is requested; " & _
                 "2: this is not a FIFO base object"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the FIFO base object"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
    sDescription = "Pointer on a message object"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "*pstObj", "T" & sVarPre & "_SWObj", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_ReadFIFO
  '------------------------------------------------------------------------------------------------
  sFctName = "ReadFIFO"
  sDescription = "This function reads the next RECEIVE message object " & _
                 "which is part of a FIFO. " & _
                 "It checks whether the selected RECEIVE OBJECT has " & _
                 "received a new message. If so the forwarded SW message " & _
                 "object is filled with the content of the HW message object " & _
                 "and the functions returns the value ""1"". " & _
                 "The structure of the SW message object is defined in the " & _
                 "header file " & r.v(sVarPre & "_HFILE") & " (see T" & sVarPre & "_SWObj)." & vbCrLf & _
                 "Note: " & vbCrLf & _
                 "This function can only used for RECEIVE objects which are " & _
                 "configured for FIFO base functionality. " & vbCrLf & _
                 "Be sure that no interrupt is enabled for the FIFO objects. "
        
  sReturnValue = "0: the message object has not received a new message; " & _
                 "1: the message object has received a new message; " & _
                 "2: this is not a FIFO base object; " & _
                 "3: a previous message was lost; " & _
                 "4: the received message is corrupted"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the FIFO base object"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function
    sDescription = "Pointer on a message object to be filled by this function"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "*pstObj", "T" & sVarPre & "_SWObj", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_FlushReadFIFO
  '------------------------------------------------------------------------------------------------
  sFctName = "FlushReadFIFO"
  sDescription = "This function flushes the selected RECEIVE FIFO. " & _
                 "After a lost or corrupted message in the receive FIFO " & _
                 "the hardware and software FIFO-pointers are no longer synchron. " & vbCrLf & _
                 "Note: " & vbCrLf & _
                 "This function can only used for RECEIVE objects which are " & _
                 "configured for FIFO base functionality. "
        
  sReturnValue = "0: this is not a FIFO base object; " & _
                 "1: the receive FIFO was flushed; "
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Number of the FIFO base object"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubObjNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetTxErrorCounter
  '------------------------------------------------------------------------------------------------
  sFctName = "GetTxErrorCounter"
  sDescription = "This macro returns the current value of the transmit error counter " & _
                 "of the selected node. " & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions for NODE_NAME are available:" & vbCrLf & _
                 sVarPre & "_NODEA        (node A)" & vbCrLf & _
                 sVarPre & "_NODEB        (node B)"
                                   
  sReturnValue = "Value of the transmit error counter"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "NODE_NAME", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetRxErrorCounter
  '------------------------------------------------------------------------------------------------
  sFctName = "GetRxErrorCounter"
  sDescription = "This macro returns the current value of the receive error counter " & _
                 "of the selected node. " & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions for NODE_NAME are available:" & vbCrLf & _
                 sVarPre & "_NODEA        (node A)" & vbCrLf & _
                 sVarPre & "_NODEB        (node B)"
        
  sReturnValue = "Value of the receive error counter"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "NODE_NAME", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetErrorTransferDirection
  '------------------------------------------------------------------------------------------------
  sFctName = "GetErrorTransferDirection"
  sDescription = "This macro returns the current value of the Last Error Transfer Direction flag. " & _
                 "0 indicates that the last error occurred while the selected CAN node was receiving a message. " & _
                 "The receive error counter has been incremented. " & _
                 "1 indicates that the last error occurred while the selected CAN node was transmitting a message. " & _
                 "The transmit error counter has been incremented. " & vbCrLf & vbCrLf & _
                 "The following definitions for NODE_NAME are available:" & vbCrLf & _
                 sVarPre & "_NODEA        (node A)" & vbCrLf & _
                 sVarPre & "_NODEB        (node B)"
        
  sReturnValue = "0 receive error, else transmit error"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "NODE_NAME", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetErrorIncrement
  '------------------------------------------------------------------------------------------------
  sFctName = "GetErrorIncrement"
  sDescription = "This macro returns the current value of the Last Error Increment flag. " & _
                 "If the Bit Stream Processor itself detects an error while a transmit operation " & _
                 "is running, the Transmit Error Counter is incremented by 8. An increment of 1 is " & _
                 "used, when the error condition was reported by an external CAN node via an error " & _
                 "frame generation. " & vbCrLf & vbCrLf & _
                 "The following definitions for NODE_NAME are available:" & vbCrLf & _
                 sVarPre & "_NODEA        (node A)" & vbCrLf & _
                 sVarPre & "_NODEB        (node B)"
        
  sReturnValue = "0 counter is incrementd by 1, counter is incremented by 8"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "NODE_NAME", "", 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
'        "CAN_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 = "CAN" & 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 CAN_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


'*****************************************************************************
' @Functions
'*****************************************************************************


'*****************************************************************************
'Description:
'        This function returns the module clock (in MHz) of the
'        CAN module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        module clock (in MHz)
'*****************************************************************************
Function GetCANModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim v As String
  Dim sRegPre As String

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

  GetCANModuleClock = GetSystemClock(r) * (1 + r.v("SYSCON1_CPSYS"))

End Function


'*****************************************************************************
'Description:
'        This function returns the clock (in MHz) of the
'        requested node.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        sChr            character of the node (A or B)
'-----------------------------------------------------------------------------
'Returns:
'        node x clock (in MHz)
'*****************************************************************************
Function GetNodexClock(r As ProjectData, vIndex As Variant, sChr As String) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim v As String
  Dim sRegPre As String

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

  GetNodexClock = 1

  If (r.v(sRegPre & "BTRL_DIV8X") = 0) Then
    GetNodexClock = GetCANModuleClock(r, vIndex)
  Else
    GetNodexClock = GetCANModuleClock(r, vIndex) / 8
  End If

End Function


'*****************************************************************************
'Description:
'        This function returns the text for the combobox cboCANRMC.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        iDivider        current value of the clock divider
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Function GetCboCANRMCText(r As ProjectData, iDivider As Integer) As String
  On Error Resume Next
  GetCboCANRMCText = ""
  GetCboCANRMCText = "System clock / " & iDivider & " (= " & Format(GetSystemClock(r) / iDivider, "0.0000") & " MHz; " & Format(1 / (GetSystemClock(r) / iDivider) * 1000, "0.000") & " ns )"
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 sVarPre As String
  Dim sRegPre As String

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"
  
  IsModuleClockDisabled = False
  IsModuleClockDisabled = (r.v("SYSCON3_CANDIS") = 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
'-----------------------------------------------------------------------------
'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 = "CAN" & vIndex
  sRegPre = sVarPre & "_"
  
  GetTooltipModuleClockDisabled = ""
  If (IsModuleClockDisabled(r, vIndex)) Then
      GetTooltipModuleClockDisabled = _
        GetTooltipModuleClockDisabled & _
        FormatDisabledTooltip("the module is disabled")
  End If

End Function


'*****************************************************************************
'Description:
'        This function disables the CAN Node x and sets all registers to
'        default values.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        sChr            character of the node (A or B)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableNodex(r As ProjectData, vIndex As Variant, sChr As String)
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

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

  If (r.v(sRegPre & "ACR_INIT") = 1 And r.v(sRegPre & "BCR_INIT") = 1) Then
      r.v(sVarPre & "_USE_Init") = 0
  End If

  If (r.v(sRegPre & sChr & "CR_INIT") = 0) Then ' only if the node was selected
    r.v(sRegPre & sChr & "CR_INIT") = 1

    ' set defaults for page "Control"
    r.v(sRegPre & sChr & "CR_SIE") = 0
    r.v(sRegPre & sChr & "CR_EIE") = 0
    r.v(sRegPre & sChr & "CR_LECIE") = 0
    r.v(sRegPre & sChr & "CR_CALM") = 0
    r.v(sRegPre & sChr & "GINP_TRINP") = 0
    r.v(sRegPre & sChr & "GINP_LECINP") = 0
    r.v(sRegPre & sChr & "GINP_EINP") = 0

    ' set defaults for page "Frame Counter"
    r.v(sRegPre & sChr & "FCRH_CFCMD") = 0
    r.v(sRegPre & sChr & "FCRH_CFCIE") = 0
    r.v(sRegPre & sChr & "GINP_CFCINP") = 0

    ' reset the Loop-Back mode
    r.v(sRegPre & sChr & "BTRH_LBM") = 0

    ' disable all objects which are connected to node "sChr"
    Call DisableObjx(r, vIndex, sChr)
  End If

End Sub

'*****************************************************************************
'Description:
'        This function returns the min value of bit TSEG1.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        vValue          input value
'        sChr            character of the node (A or B)
'-----------------------------------------------------------------------------
'Returns:
'       min value of TSEG1
'*****************************************************************************
Function GetMinTseg1Value(r As ProjectData, vIndex As Variant, vValue As Variant, sChr As String) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim iMin As Integer
    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_" & sChr
    iMin = r.d(sRegPre & "BTRL_SJW") + 1

    If (vValue < iMin) Then
        If (vValue < 3) Then
            GetMinTseg1Value = 3
        Else
            GetMinTseg1Value = r.d(sRegPre & "BTRL_SJW") + 1
        End If
    Else
        GetMinTseg1Value = 3
    End If
 
End Function

'*****************************************************************************
'Description:
'        This function returns the min value of bit TSEG2.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        vValue          input value
'        sChr            character of the node (A or B)
'
'-----------------------------------------------------------------------------
'Returns:
'       min value of TSEG2
'*****************************************************************************
Function GetMinTseg2Value(r As ProjectData, vIndex As Variant, vValue As Variant, sChr As String) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim iMin As Integer
    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_" & sChr
    iMin = r.d(sRegPre & "BTRL_SJW") + 1

    If (vValue < iMin) Then
        If (vValue < 2) Then
            GetMinTseg2Value = 2
        Else
            GetMinTseg2Value = r.d(sRegPre & "BTRL_SJW") + 1
        End If
    Else
        GetMinTseg2Value = 2
    End If

End Function


'*****************************************************************************
'Description:
'        This function returns the min value of bit SJW.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        vValue          input value
'        sChr            character of the node (A or B)
'
'-----------------------------------------------------------------------------
'Returns:
'       min value of SJW
'*****************************************************************************
Function GetMinSJWValue(r As ProjectData, vIndex As Variant, vValue As Variant, sChr As String) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_" & sChr

    GetMinSJWValue = 1

End Function

'*****************************************************************************
'Description:
'        This function returns the max value of bit SJW.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        vValue          input value
'        sChr            character of the node (A or B)
'
'-----------------------------------------------------------------------------
'Returns:
'       max value of SJW
'*****************************************************************************
Function GetMaxSJWValue(r As ProjectData, vIndex As Variant, vValue As Variant, sChr As String) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_" & sChr

    If (((vValue - 1) > r.d(sRegPre & "BTRL_TSEG1")) Or ((vValue - 1) > r.d(sRegPre & "BTRL_TSEG2"))) Then
        If (vValue > 4) Then
            GetMaxSJWValue = 4
        Else
            If (vValue > (r.d(sRegPre & "BTRL_TSEG1") + 1)) Then
                GetMaxSJWValue = r.d(sRegPre & "BTRL_TSEG1") + 1
            End If
            If (vValue > (r.d(sRegPre & "BTRL_TSEG2") + 1)) Then
                GetMaxSJWValue = r.d(sRegPre & "BTRL_TSEG2") + 1
            End If
        End If
    Else
        GetMaxSJWValue = 4
    End If


End Function


'*****************************************************************************
'Description:
'        This function returns the min required baud rate [Baud].
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        sChr            character of the node (A or B)
'
'-----------------------------------------------------------------------------
'Returns:
'       min required baud rate [Baud]
'*****************************************************************************
Function GetMinReqBR(r As ProjectData, vIndex As Variant, sChr As String) As Long
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_" & sChr

    GetMinReqBR = GetNodexClock(r, vIndex, sChr) * 1000000 / (64 * 2 ^ (1 - 1) * _
                  (3 + r.d(sRegPre & "BTRL_TSEG1") + r.d(sRegPre & "BTRL_TSEG2")))

End Function

'*****************************************************************************
'Description:
'        This function returns the max required baud rate [Baud].
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        sChr            character of the node (A or B)
'
'-----------------------------------------------------------------------------
'Returns:
'       max required baud rate [Baud]
'*****************************************************************************
Function GetMaxReqBR(r As ProjectData, vIndex As Variant, sChr As String) As Long
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_" & sChr

    GetMaxReqBR = GetNodexClock(r, vIndex, sChr) * 1000000 / (2 ^ (1 - 1) * _
                  (3 + r.d(sRegPre & "BTRL_TSEG1") + r.d(sRegPre & "BTRL_TSEG2")))

End Function


'*****************************************************************************
'Description:
'        This function updates the CANx module
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        sChr            character of the node (A or B)
'
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub UpdateCAN(r As ProjectData, vIndex As Variant, sChr As String)
    On Error GoTo UpdateCANError
    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "CAN" & vIndex
    sRegPre = sVarPre & "_" & sChr

    If (r.v(sVarPre & "_BaudrateNode" & sChr) > GetMaxReqBR(r, vIndex, sChr)) Then
        r.v(sVarPre & "_BaudrateNode" & sChr) = GetMaxReqBR(r, vIndex, sChr)
    End If
    If (r.v(sVarPre & "_BaudrateNode" & sChr) < GetMinReqBR(r, vIndex, sChr)) Then
        r.v(sVarPre & "_BaudrateNode" & sChr) = GetMinReqBR(r, vIndex, sChr)
    End If

    r.l(sRegPre & "BTRL_BRP") = (1000000 * GetNodexClock(r, vIndex, sChr) / (r.v(sVarPre & "_BaudrateNode" & sChr) * _
                               2 ^ (1 - 1) * (3 + r.d(sRegPre & "BTRL_TSEG1") + _
                               r.d(sRegPre & "BTRL_TSEG2"))) - 1)
    Exit Sub

UpdateCANError:
    r.d(sRegPre & "BTRL_BRP") = 10
    Exit Sub
End Sub


'*****************************************************************************
'Description:
'        This function disables all CAN objects which are connected to the
'        node specified with 'sChr' and sets all registers to default values.
'        Also all objects configured for gateway mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        sChr            character of the node (A or B)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableObjx(r As ProjectData, vIndex As Variant, sChr As String)
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim iNr As Integer

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

  For iNr = 0 To 31
    If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") = 2) Then  ' only if the object was selected
      If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") > 3 Or (r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 0 And sChr = "A") Or (r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 1 And sChr = "B")) Then
        r.sfr(sRegPre & "MSGCTRH" & iNr) = &H0000
        r.sfr(sRegPre & "MSGCTRL" & iNr) = &H5555

        r.sfr(sRegPre & "MSGCFGH" & iNr) = &H0000
        r.sfr(sRegPre & "MSGCFGL" & iNr) = &H0000

        r.sfr(sRegPre & "MSGFGCRH" & iNr) = &H0000
        r.sfr(sRegPre & "MSGFGCRL" & iNr) = &H0000
      End If
    End If
  Next iNr

End Sub


'*****************************************************************************
'Description:
'        This function checks if a object is used as a FIFO slave and which
'        object is the base FIFO object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number message object
'-----------------------------------------------------------------------------
'Returns: -1 if the object is not used as FIFO slave, else returns the number
'         of the base object
'*****************************************************************************
Function IsObjFIFOSlave(r As ProjectData, vIndex As Variant, iNr As Integer) As Integer
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer
  Dim iStage As Integer
  Dim iBase As Integer

  IsObjFIFOSlave = -1

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

  iStage = 1 + r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE")

  ' if MMC = 3 (FIFO Slave)
  If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 3) Then
    ' get the base object for the "iNr" object
    iBase = iNr - (iNr Mod iStage)
    
    'msgbox "iNr = " & iNr
    'msgbox "iNr = " & iStage
    'msgbox "(iNr Mod iStage) = " & (iNr Mod iStage)
    
    ' this object is only a slave object if iBase <> iNr else it is the base object
    If (iBase <> iNr) Then
      IsObjFIFOSlave = iBase
    End If
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if a object is used as a gateway destination and
'        which object is the gateway source object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number message object
'-----------------------------------------------------------------------------
'Returns: -1 if the object is not used as gateway destination, else returns
'         the number of the source object
'*****************************************************************************
Function IsObjGatewayDestination(r As ProjectData, vIndex As Variant, iNr As Integer) As Integer
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer
  Dim iStage As Integer
  Dim iBase As Integer

  IsObjGatewayDestination = -1

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

  If (iNr > 1 And (iNr Mod 2) = 0) Then
    If (r.v(sRegPre & "MSGFGCRH" & iNr - 1 & "_MMC") = 4) Then
      IsObjGatewayDestination = iNr - 1
    End If
  End If

End Function


'*****************************************************************************
'Description:
'        This function disables all CAN interrupts.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableSRNs(r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim i As Integer
  Dim sVarPre As String
  Dim sRegPre As String

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

  For i = 0 To 6
    Call DisableSRN(r, sVarPre, sRegPre & i & "IC", "SRN" & i)
  Next i

  i = 7
  r.v("CAN_IntEN") = 0
  If (r.Exist("SDLM_PISEL_I1SEL") = True) Then
      If (r.v("SDLM_PISEL_I1SEL") = 0) Then
        Call DisableSRN(r, sVarPre, sRegPre & i & "IC", "SRN" & i)
      End If
  Else
      Call DisableSRN(r, sVarPre, sRegPre & i & "IC", "SRN" & i)
  End If


End Sub

'*****************************************************************************
'Description:
'        This function selects or deselects the CAN pin interface.
'-----------------------------------------------------------------------------
'Parameters:
'       r             reference to the ProjectData object
'       vIndex        parameter
'       sChr          node number (A or B)
'       sPin          Rx or Tx
'       iMode         7 for deselect pins and 0 .. 3
'-----------------------------------------------------------------------------
'Returns:
'     none
'*****************************************************************************
Sub SelectPinInterface(r As ProjectData, vIndex As Variant, sChr As String, sPin As String, iMode As Integer)
    Dim iOldMode  As Integer
    Dim sVarPre   As String
    Dim sRegPre   As String

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

    iOldMode = r.v(sVarPre & "_Use_" & sPin & sChr)

    If (sPin = "Rx") Then
        If (iOldMode < 7) Then
            Call ReleaseAlternateInputPin(r, sVarPre & "_" & sPin & "D" & sChr & "_" & iOldMode)
        End If

        If (iMode < 7) Then
            Call RequestAlternateInputPin(r, sVarPre & "_" & sPin & "D" & sChr & "_" & iMode)
            r.v(sRegPre & "PISEL_RIS" & sChr) = iMode
            r.v(sVarPre & "_Use_" & sPin & sChr) = iMode
        ElseIf (iMode = 7) Then
            r.v(sVarPre & "_Use_" & sPin & sChr) = 7
            r.v(sRegPre & "PISEL_RIS" & sChr) = 0
            'set CAN to default
        End If
        
    End If

    If (sPin = "Tx") Then
        If (iOldMode < 7) Then
            Call ReleaseAlternateOutputPin(r, sVarPre & "_" & sPin & "D" & sChr & "_" & iOldMode)
        End If

        If (iMode < 7) Then
            Call RequestAlternateOutputPin(r, sVarPre & "_" & sPin & "D" & sChr & "_" & iMode)
            r.v(sVarPre & "_Use_" & sPin & sChr) = iMode
        ElseIf (iMode = 7) Then
            r.v(sVarPre & "_Use_" & sPin & sChr) = iMode
            'set CAN to default
        End If
        
    End If

End Sub

'*****************************************************************************
'Description:
'        This function checks the pin interface.
'-----------------------------------------------------------------------------
'Parameters:
'       r             reference to the ProjectData object
'       vIndex        parameter
'       sChr          node number (A or B)
'-----------------------------------------------------------------------------
'Returns:
'     True if pin interface is selected, else False
'*****************************************************************************
Function IsPinInterfaceSelected(r As ProjectData, vIndex As Variant, sChr As String) As Boolean
    Dim sVarPre   As String
    Dim sRegPre   As String

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

    If (r.v(sVarPre & "_Use_Rx" & sChr) = 7 Or _
        r.v(sVarPre & "_Use_Tx" & sChr) = 7) Then

        IsPinInterfaceSelected = False
    End If
  

End Function

'*****************************************************************************
'Description:
'        This function returns a disabled tooltip if pin interface is
'        not selected.
'-----------------------------------------------------------------------------
'Parameters:
'       r             reference to the ProjectData object
'       vIndex        parameter
'       sChr          node number (A or B)
'-----------------------------------------------------------------------------
'Returns:
'     formated tooltip if pin interface is not selected
'*****************************************************************************
Function GetTooltipIfPinInterfaceNotSelected(r As ProjectData, vIndex As Variant, sChr As String) As String
    Dim sVarPre   As String
    Dim sRegPre   As String
    Dim s         As String

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

    If (r.v(sVarPre & "_Use_Rx" & sChr) = 7) Then
        s = s & FormatDisabledTooltip("there is not pin selected as RXDC" & sChr)
    End If
  
    If (r.v(sVarPre & "_Use_Tx" & sChr) = 7) Then
        s = s & FormatDisabledTooltip("there is not pin selected as TXDC" & sChr)
    End If

    GetTooltipIfPinInterfaceNotSelected = s

End Function
