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.1  ahe            13.08.02     final version for TC1775B, TC1765
'                                         - changed function IsObjFIFOSlave()
'        ----------------------------------------------------------------------
'        4.0  ste            21.08.02     final version for TC1796
'        ----------------------------------------------------------------------
'             ste            01.08.03     version for TC1766
'                                         only 2 node available (new Const added)
'                                         only 64 message objects available (new Const added)
'                                         only 6 SRNs available (new Const added)
'             ste            28.08.03     function IsTTCANAvailable added
'                                         function DisableNodex changed (TTCAN
'                                         disable added)
'                                         4 node available (new Const added)
'                                         128 message objects available (new Const added)
'                                         16 SRNs available (new Const added)
'             ste            18.09.03     function ConfigureSchedulerEntries added
'        ----------------------------------------------------------------------
'             ste            14.11.03     - TC1766 and TC1796 merged
'                                         - CONST CAN_MAX_NODES,CAN_MAX_OBJECTS,
'                                           CAN_MAX_SRNS and CAN_MAX_SENTRIES replaced
'                                           with public functions
'        ----------------------------------------------------------------------
'        ----------------------------------------------------------------------
'             sheshadri      14.03.06     - modified wrt to XC886CLM
'
'*****************************************************************************


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

Global Const SECTION_SFR_DECLARATION = _
"//****************************************************************************" & vbCrLf & _
"// @Declaration of SFRs" & vbCrLf & _
"//****************************************************************************"
'*****************************************************************************
' Controller dependent values (TC1766, TC1796)
'*****************************************************************************

Public Function CAN_MAX_NODES() As Integer ' number of max. CAN nodes (0 to max - 1)
         CAN_MAX_NODES = 2  ' 0 - 1
End Function

Public Function CAN_MAX_OBJECTS() As Integer ' number of max. CAN objects (0 to max - 1)
         CAN_MAX_OBJECTS = 32   ' 0 - 31
End Function

Public Function CAN_MAX_SRNS() As Integer ' number of max. CAN service request nodes (0 to max - 1)
         CAN_MAX_SRNS = 8       ' 0 - 7
End Function

Public Function MSG_NODE_INIT_DIS(iNr As Integer) As String		'message for Node initialization disabled
    MSG_NODE_INIT_DIS = "All message objects which are connected to node " & iNr & " will be disabled . " & vbCrLf & _
                        "Do you really want to continue ?"
End Function

Public Function MSG_NODE_TXPIN_DIS(iNr As Integer) As String    ' message for LBM selected & TXPIN disabled
    MSG_NODE_TXPIN_DIS = "Node" & iNr & " TXPIN will be disabled . " & vbCrLf & _
                         "Do you really want to continue ?"
End Function

Public Function MSG_NODE_RXPIN_DIS(iNr As Integer) As String    ' message for LBM selected & RXPIN disabled
    MSG_NODE_RXPIN_DIS = "Node" & iNr & " RXPIN will be disabled . " & vbCrLf & _
                         "Do you really want to continue ?"
End Function

'*****************************************************************************
' @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 successful operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(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
  Dim vindex As Variant
  
  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

  For i = 0 To (CAN_MAX_SRNS - 1)
    '------------------------------------------------------------------------------------------------
    ' Description for CAN_NAME_SRNx
    '------------------------------------------------------------------------------------------------
    sFctName = "SRN" & i
    sDescription = "This is the interrupt service routine for the Service " & _
                        "Request Node " & i & " of the MultiCAN module. " & vbCrLf & _
                        TOOLTIP_ISR_USERCODE


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

'  If (Not func.Sort) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for CAN_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription(sVarPre)
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function


  '------------------------------------------------------------------------------------------------
  ' 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 st" & 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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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", "st" & 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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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 IDE-bit (standard or extended identifier).  " & _
                 "The direction bit (DIR) 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 st" & 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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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", "st" & 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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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", "*ulpubData", "ulong", 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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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-" & (CAN_MAX_OBJECTS - 1) & ")"
    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) and the IDE-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 st" & 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", "st" & 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 st" & 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", "st" & sVarPre & "_SWObj", sDescription)) Then Exit Function


  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetTxErrorCounter
  '------------------------------------------------------------------------------------------------
  sFctName = "GetTxErrorCounter"
  sDescription = "This function returns the current value of the transmit error counter of the selected node. " & vbCrLf & vbCrLf & _
                 "The following definitions for NODE_NAME are available:" & vbCrLf
    For i = 0 To (CAN_MAX_NODES - 1)
      sDescription = sDescription & sVarPre & "_NODE" & i & "        (node " & i & ")" & vbCrLf
    Next i

  sReturnValue = "Value of the transmit error counter"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubNodeNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetRxErrorCounter
  '------------------------------------------------------------------------------------------------
  sFctName = "GetRxErrorCounter"
  sDescription = "This function returns the current value of the receive error counter of the selected node. " & vbCrLf & vbCrLf & _
                 "The following definitions for NODE_NAME are available:" & vbCrLf
    For i = 0 To (CAN_MAX_NODES - 1)
      sDescription = sDescription & sVarPre & "_NODE" & i & "        (node " & i & ")" & vbCrLf
    Next i

  sReturnValue = "Value of the receive error counter"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubNodeNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetErrorTransferDirection
  '------------------------------------------------------------------------------------------------
  sFctName = "GetErrorTransferDirection"
  sDescription = "This function 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
    For i = 0 To (CAN_MAX_NODES - 1)
      sDescription = sDescription & sVarPre & "_NODE" & i & "        (node " & i & ")" & vbCrLf
    Next i

  sReturnValue = "0 receive error, else transmit error"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubNodeNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_ubGetErrorIncrement
  '------------------------------------------------------------------------------------------------
  sFctName = "GetErrorIncrement"
  sDescription = "This function 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
    For i = 0 To (CAN_MAX_NODES - 1)
      sDescription = sDescription & sVarPre & "_NODE" & i & "        (node " & i & ")" & vbCrLf
    Next i

  sReturnValue = "0 counter is incrementd by 1, counter is incremented by 8"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Name of the node"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubNodeNr", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_vWriteCANAddress
  '------------------------------------------------------------------------------------------------
  sFctName = "WriteCANAddress"
  sDescription = "This macro writes 16-bit CAN address to CAN Address Register " & _
                 "Low and High respectively. "
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "16-bit Address"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "uwAdr", "uword", sDescription)) Then Exit Function

'  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for void CAN_vWriteCANData(ulong)
  '------------------------------------------------------------------------------------------------
  sFctName = "WriteCANData"
  sDescription = "This macro writes 32-bit Data to CAN Data Register's 0-3" & _
                 "respectively. " & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Write Process :" & vbCrLf & _
                 "->Write the address of the MultiCAN kernel register to the CAN_ADL" & _
                 " and CAN_ADH registers." & vbCrLf & _
                 "use macro : " & r.v(sVarPre & "_NAME_WriteCANAddress") & ". " & vbCrLf & _
                 "->Write the data to the CAN_DATA0/CAN_DATA1/CAN_DATA2/CAN_DATA3" & _
                 " registers." & vbCrLf & _
                 "->Write the register CAN_ADCON, including setting the valid bit of" & _
                 " the data registers and setting register bit RWEN to 1." & vbCrLf & _
                 "->The valid data will be written to the MultiCAN kernel only once." & _
                 " Register bit BSY will become 1." & vbCrLf & _
                 "->When Register bit BSY becomes 0, the transmission is finished."
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "32-bit Data"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ulValue", "ulong", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for void CAN_vWriteAMData(ulong)
  '------------------------------------------------------------------------------------------------
  ' #### MACRO for CAN_vWriteAMData
  sFctName = "vWriteAMData"
  sDescription = "This function writes 32-bit Data to CAN Data Register's 0-3" & _
                 "respectively. " & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "Write Process :" & vbCrLf & _
                 "->Write the address of the MultiCAN kernel register to the CAN_ADL" & _
                 " and CAN_ADH registers." & vbCrLf & _
                 "use macro : " & r.v(sVarPre & "_NAME_WriteCANAddress") & ". " & vbCrLf & _
                 "->Write the data to the CAN_DATA0/CAN_DATA1/CAN_DATA2/CAN_DATA3" & _
                 " registers." & vbCrLf & _
                 "->Write the register CAN_ADCON, including setting the valid bit of" & _
                 " the data registers and setting register bit RWEN to 1." & vbCrLf & _
                 "->The valid data will be written to the MultiCAN kernel only once." & _
                 " Register bit BSY will become 1." & vbCrLf & _
                 "->When Register bit BSY becomes 0, the transmission is finished."
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_WriteAMData", "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "32-bit Data"
    If (Not func.AddParameter(sVarPre & "_NAME_WriteAMData_Para1", "ulValue", "ulong", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ulong CAN_ulGetCANData(void)
  '------------------------------------------------------------------------------------------------
  sFctName = "GetCANData"
  sDescription = "This function return's 32-bit Data from CAN Kernel Address." & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "This Function read's data from CAN_DATA0/CAN_DATA1/CAN_DATA2/CAN_DATA3 registers" & vbCrLf & _
                 " and return's 32-bit Data assuming that CAN Kernel Address and" & _
                 "Read Mode is Enabled." & vbCrLf & _
                 "Read Process:" & vbCrLf & _
                 "->Write the address of the MultiCAN kernel register to the CAN_ADL and CAN_ADH" & _
                 " registers." & vbCrLf & _
                 "use macro : " & r.v(sVarPre & "_NAME_WriteCANAddress") & ". " & vbCrLf & _
                 "->Write the register CAN_ADCON, setting register bit RWEN to 0." & vbCrLf & _
                 "->The 32-bit data will be read from the MultiCAN kernel only once." & _
                 "Register bit BSY will become 1." & vbCrLf & _
                 "->When register bit BSY becomes 0, the transmission is finished." & vbCrLf & _
                 "->Read the data from the CAN_DATA0/CAN_DATA1/CAN_DATA2/CAN_DATA3" & vbCrLf & _
                 "registers." & vbCrLf & _
                 ""
  sReturnValue = "32-bit Data"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ulong", sDescription, sReturnValue, 1)) Then Exit Function


  '------------------------------------------------------------------------------------------------
  ' Description for void CAN_vSetListCommand(ulong)
  '------------------------------------------------------------------------------------------------
  sFctName = "SetListCommand"
  sDescription = "This function write's 32-bit Data to CAN_PANCTR Register." & vbCrLf
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "32-bit Data"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ulVal", "ulong", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_vCAN_vReadEN()
  '------------------------------------------------------------------------------------------------
  sFctName = "ReadEN"
  sDescription = "This macro enables Read mode (CAN Address/Data Control Register)." & vbCrLf
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CAN_vWriteRWEN()
  '------------------------------------------------------------------------------------------------
  sFctName = "WriteEN"
  sDescription = "This macro enables Write mode (CAN Address/Data Control Register)." & vbCrLf
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "Data Control Flags"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubDCtrl", "ubyte", sDescription)) Then Exit Function
  If (Not func.Sort) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for push/pop
  'CAN_pushAMRegs()
  'CAN_popAMRegs()
  '------------------------------------------------------------------------------------------------
  sFctName = "push/pop"
  sDescription = "The macro CAN_pushAMRegs() PUSH the CAN Access Mediator Registers." & vbCrLf & _
                 "The macro CAN_popAMRegs() POP the CAN Access Mediator Registers." & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "This macro is used in CAN ISR/Function's to protect Access Mediator Register" & vbCrLf & _
                 "MultiCAN Access Mediator Registers:" & vbCrLf & _
                 "    ADL, ADH, DATA0, DATA1, DATA2, DATA3. " & vbCrLf & _
                 "" & vbCrLf & _
                 "The _push_/_pop_ routine inserts a PUSH/POP instruction into the program saving the contents" & _
                 " of the Special Function Register(sfr) on the Stack."
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_PushPop", "", sDescription, sReturnValue, 0)) 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 successful 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 Variant
    On Error GoTo GetCANModuleClockError

    If (r.v(CORE_PREFIX & "CMCON_FCCFG") = 1) Then
      GetCANModuleClock = (GetSystemClock(r) / GetValueCLKREL(r)) * 2
    Else
      GetCANModuleClock = GetSystemClock(r) / GetValueCLKREL(r)
    End If

    Exit Function
    
GetCANModuleClockError:
    Errorbox "Error"
End Function

      


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

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"
  sNBTRx = sRegPre & "NBTR" & iNr

  GetNodexClock = 1

  If (r.v(sNBTRx & "_DIV8") = 0) Then
    GetNodexClock = GetCANModuleClock(r, vindex)
  Else
    GetNodexClock = GetCANModuleClock(r, vindex) / 8
  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 sVarPre As String
  Dim sRegPre As String

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"
  IsModuleClockDisabled = False
  IsModuleClockDisabled = r.v(SCU_PREFIX & "PMCON1_CAN_DIS") = 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 = "CAN module clock is disabled"
End Function



'*****************************************************************************
'Description:
'        This function checks if enough port pins selected for the node.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the CAN node
'-----------------------------------------------------------------------------
'Returns:
'        True            not enough port pins selected
'        False           else
'*****************************************************************************
Function IsPinInterfaceDeselected(r As ProjectData, vindex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "CAN_USE" & vindex
  sRegPre = sVarPre & "_"

  IsPinInterfaceDeselected = False

   If ((r.v("CAN_USE_NODE" & iNr & "_RXD_PIN") = 0) Or (r.v("CAN_USE_NODE" & iNr & "_TXD_PIN") = 0)) Then
    IsPinInterfaceDeselected = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function returns the min value of bit TSEG1.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        vValue          input value
'        iNr             number of the node
'-----------------------------------------------------------------------------
'Returns:
'       min value of TSEG1
'*****************************************************************************
Function GetMinTseg1Value(r As ProjectData, vindex As Variant, vValue As Variant, iNr As Integer) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim iMin As Integer
    Dim sNBTRx As String

    sVarPre = "CAN" & vindex
    sRegPre = sVarPre & "_"
    sNBTRx = sRegPre & "NBTR" & iNr
    iMin = r.d(sNBTRx & "_SJW") + 1

    If (vValue < iMin) Then
        If (vValue < 3) Then
            GetMinTseg1Value = 3
        Else
            GetMinTseg1Value = r.d(sNBTRx & "_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
'        iNr             number of the node
'
'-----------------------------------------------------------------------------
'Returns:
'       min value of TSEG2
'*****************************************************************************
Function GetMinTseg2Value(r As ProjectData, vindex As Variant, vValue As Variant, iNr As Integer) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim iMin As Integer
    Dim sNBTRx As String

    sVarPre = "CAN" & vindex
    sRegPre = sVarPre & "_"
    sNBTRx = sRegPre & "NBTR" & iNr
    iMin = r.d(sNBTRx & "_SJW") + 1

    If (vValue < iMin) Then
        If (vValue < 2) Then
            GetMinTseg2Value = 2
        Else
            GetMinTseg2Value = r.d(sNBTRx & "_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
'        iNr             number of the node
'
'-----------------------------------------------------------------------------
'Returns:
'       min value of SJW
'*****************************************************************************
Function GetMinSJWValue(r As ProjectData, vindex As Variant, vValue As Variant, iNr As Integer) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "CAN" & vindex
    sRegPre = sVarPre & "_"

    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
'        iNr             number of the node
'
'-----------------------------------------------------------------------------
'Returns:
'       max value of SJW
'*****************************************************************************
Function GetMaxSJWValue(r As ProjectData, vindex As Variant, vValue As Variant, iNr As Integer) As Integer
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sNBTRx As String

    sVarPre = "CAN" & vindex
    sRegPre = sVarPre & "_"
    sNBTRx = sRegPre & "NBTR" & iNr

    If (((vValue - 1) > r.d(sNBTRx & "_TSEG1")) Or ((vValue - 1) > r.d(sNBTRx & "_TSEG2"))) Then
        If (vValue > 4) Then
            GetMaxSJWValue = 4
        Else
            If (vValue > (r.d(sNBTRx & "_TSEG1") + 1)) Then
                GetMaxSJWValue = r.d(sNBTRx & "_TSEG1") + 1
            End If
            If (vValue > (r.d(sNBTRx & "_TSEG2") + 1)) Then
                GetMaxSJWValue = r.d(sNBTRx & "_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
'        iNr             number of the node
'
'-----------------------------------------------------------------------------
'Returns:
'       min required baud rate [Baud]
'*****************************************************************************
Function GetMinReqBR(r As ProjectData, vindex As Variant, iNr As Integer) As Long
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sNBTRx As String

    sVarPre = "CAN" & vindex
    sRegPre = sVarPre & "_"
    sNBTRx = sRegPre & "NBTR" & iNr

    GetMinReqBR = GetNodexClock(r, vindex, iNr) * 1000000 / (64 * 2 ^ (1 - 1) * _
                  (3 + r.d(sNBTRx & "_TSEG1") + r.d(sNBTRx & "_TSEG2")))

End Function

'*****************************************************************************
'Description:
'        This function returns the max required baud rate [Baud].
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        iNr             number of the node
'
'-----------------------------------------------------------------------------
'Returns:
'       max required baud rate [Baud]
'*****************************************************************************
Function GetMaxReqBR(r As ProjectData, vindex As Variant, iNr As Integer) As Long
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sNBTRx As String

    sVarPre = "CAN" & vindex
    sRegPre = sVarPre & "_"
    sNBTRx = sRegPre & "NBTR" & iNr

    GetMaxReqBR = GetNodexClock(r, vindex, iNr) * 1000000 / (2 ^ (1 - 1) * _
                  (3 + r.d(sNBTRx & "_TSEG1") + r.d(sNBTRx & "_TSEG2")))

End Function


'*****************************************************************************
'Description:
'        This function checks if all nodes are deselected.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Return:
'        True            the module clock is disabled
'        False           else
'*****************************************************************************
Function IsAllNodesDeselected(r As ProjectData, vindex As Variant) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"
  
  IsAllNodesDeselected = True
  For i = 0 To (CAN_MAX_NODES - 1)
    If (r.v(sRegPre & "NCR" & i & "_INIT") = 0) Then
      IsAllNodesDeselected = False
      Exit For
    End If
  Next i
End Function


'*****************************************************************************
'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 iFirstNr As Integer
  Dim iLastNr As Integer
  Dim iNextNr As Integer
  Dim iBase As Integer

  IsObjFIFOSlave = -1

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"
                        
' fr alle MO's
' if MO = FIFO Base (MMC = 1)
' dann suche alle Slaves
  For iBase = 0 To (CAN_MAX_OBJECTS - 1)
    If (r.v(sRegPre & "MOFCR" & iBase & "_MMC") = 1 Or r.v(sRegPre & "MOFCR" & iBase & "_MMC") = 2 Or r.v(sRegPre & "MOFCR" & iBase & "_MMC") = 4) Then  ' only if the FIFO or Gateway mode is selected
      ' suche alle MOs von BOT bis TOP des MOs iBase
      iFirstNr = r.v(sRegPre & "MOFGPR" & iBase & "_BOT")
      iLastNr = r.v(sRegPre & "MOFGPR" & iBase & "_TOP")
      If (iFirstNr <= (CAN_MAX_OBJECTS - 1) And iLastNr <= (CAN_MAX_OBJECTS - 1)) Then
        iNextNr = iFirstNr

        Do
          If (iNextNr = iNr And iNr <> iBase) Then
            IsObjFIFOSlave = iBase
          End If
          If (iNextNr = iLastNr) Then
          Else
            iNextNr = GetPNEXT(r, vindex, iNextNr)
          End If
        Loop Until iNextNr = iLastNr

        If (iLastNr = iNr And iNr <> iBase) Then
          IsObjFIFOSlave = iBase
        End If
      End If
    End If
  Next iBase

End Function


'*****************************************************************************
'Description:
'        This function checks if a object is connected to a node which is
'        configured for analyzer mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number message object
'-----------------------------------------------------------------------------
'Returns: true if assigned node is in analyzer mode else false
'*****************************************************************************
Function IsObjConnectedToAnalyzer(r As ProjectData, vindex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim iNode As Integer

  IsObjConnectedToAnalyzer = False

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  iNode = r.v(sRegPre & "MOCTR" & iNr & "_LIST") - 1 ' number of the node
  If (iNode >= 0) Then
    If (r.v(sRegPre & "NCR" & iNode & "_CALM") = 1) Then ' Analyser mode
      IsObjConnectedToAnalyzer = True
    End If
  End If
End Function



'*****************************************************************************
'Description:
'        This function updates the node x baud rate.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        iNr             number of the node
'
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub UpdateNodexBaudrate(r As ProjectData, vindex As Variant, iNr As Integer)
    On Error GoTo UpdateNodexBaudrateError
    Dim sVarPre As String
    Dim sRegPre As String
    Dim sNBTRx As String

    sVarPre = "CAN" & vindex
    sRegPre = sVarPre & "_"
    sNBTRx = sRegPre & "NBTR" & iNr

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

    r.l(sNBTRx & "_BRP") = (1000000 * GetNodexClock(r, vindex, iNr) / (r.v(sVarPre & "_BaudrateNode" & iNr) * _
                           2 ^ (1 - 1) * (3 + r.d(sNBTRx & "_TSEG1") + r.d(sNBTRx & "_TSEG2"))) - 1)
    Exit Sub

UpdateNodexBaudrateError:
    r.d(sNBTRx & "_BRP") = 10
    Exit Sub
End Sub


'*****************************************************************************
'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 (CAN_MAX_SRNS - 1)
    Call DisableSRN(r, sVarPre, sRegPre & "SRC" & i, "SRN" & i)
  Next i

End Sub

'*****************************************************************************
'Description:
'        This function disables all CAN objects which are connected to the
'        node specified with 'iNodeNr' and sets all registers to default values.
'        Also all objects configured for gateway mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNodeNr         number of the node
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableObjx(r As ProjectData, vindex As Variant, iNodeNr As Integer)
  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 (CAN_MAX_OBJECTS - 1)
    If (r.v(sRegPre & "MOCTR" & iNr & "_LIST") = (iNodeNr + 1)) Then ' only if the object is assigned to the Node iNodeNr
      ' if the MO is configured as FIFO base object then all slaves must be released
      SetStandardMode(r, vIndex, iNr)

      r.sfr(sRegPre & "MOCTR" & iNr) = 0
      r.sfr(sRegPre & "MOFCR" & iNr) = 0
      r.v(sVarPre & "_MO" & iNr & "_LISTPOS") = 0
    End If
  Next iNr

End Sub


'*****************************************************************************
'Description:
'        This function disables the CAN Node x and sets all registers to
'        default values. All node dependend objects are disabled too.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the node
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableNodex(r As ProjectData, vindex As Variant, iNr As Integer)
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim sNCRx As String  ' Node x Control Register
  Dim sNIPRx As String ' Node x Interrupt Pointer Register
  Dim sNPCRx As String ' Node x Port Control Register
  Dim sNBTRx As String ' Node x Bit Timing Register
  Dim sNECNTx As String ' Node x Error Counter Register
  Dim sNFCRx As String ' Node x Frame Counter Register
  Dim sTTCRx As String
  Dim sTTCFGRx As String
  Dim sTTIERx As String
  Dim sTURRx As String
  Dim i As Integer
  Dim j As Integer

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  sNCRx = sRegPre & "NCR" & iNr
  sNIPRx = sRegPre & "NIPR" & iNr
  sNPCRx = sRegPre & "NPCR" & iNr
  sNBTRx = sRegPre & "NBTR" & iNr
  sNECNTx = sRegPre & "NECNT" & iNr
  sNFCRx = sRegPre & "NFCR" & iNr
                   
  sTTCRx = sRegPre & "TTCR" ' & iNr
  sTTCFGRx = sRegPre & "TTCFGR" ' & iNr
  sTTIERx = sRegPre & "TTIER" ' & iNr
  sTURRx = sRegPre & "TURR" ' & iNr
  If (r.v(sNCRx & "_INIT") = 0) Then ' only if the node was selected
    ' set defaults for page "Control"
    r.sfr(sNCRx) = &H1
    r.sfr(sNIPRx) = &H0
   ' r.v(sNPCRx & "_LBM") = 0 --->by ashish for LBM
      
     ' set defaults for page "Baud Rate"
    Call UpdateNodexBaudrate(r, vindex, iNr)

    ' set defaults for page "Frame Counter"
    r.v(sNFCRx & "_CFSEL") = 0
    r.v(sNFCRx & "_CFCIE") = 0

    ' disable all objects which are connected to node "iNr"
    Call DisableObjx(r, vindex, iNr)

    ' check if no node is active, if so then disable the Init function
    ' and all interrupts
    j = 0
    For i = 0 To (CAN_MAX_NODES - 1)
      If (r.v(sRegPre & "NCR" & i & "_INIT") = 1) Then
        j = j + 1
      End If
    Next i
    If (j = CAN_MAX_NODES) Then
      r.v(sVarPre & "_USE_Init") = 0
      Call DisableSRNs(r, vindex)
    End If
    
  End If

End Sub


'*****************************************************************************
'Description:
'        This function disables the CAN module and sets all registers to
'        default values. All requested pins are released.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableModule(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 (CAN_MAX_NODES - 1)
 Call ReleaseAllAlternatePins(r, sVarPre & "_USE_NODE" & i & "_RXD_PIN")
 Call ReleaseAllAlternatePins(r, sVarPre & "_USE_NODE" & i & "_TXD_PIN")
    Call DisableNodex(r, vindex, i)
  Next i
  Call DisableSRNs(r, vindex)
  
  Call NodeRXDPinSelectionReset(r, vIndex, 0)
  Call NodeTXDPinSelectionReset(r, vIndex, 0)
  Call NodeRXDPinSelectionReset(r, vIndex, 1)
  Call NodeTXDPinSelectionReset(r, vIndex, 1)
End Sub


'*****************************************************************************
'Description:
'        This function returns the PNEXT pointer from the given MO.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        MOIndex         index of the Message Object
'-----------------------------------------------------------------------------
'Returns:
'        PNEXT
'*****************************************************************************
Function GetPNEXT(r As ProjectData, vindex As Variant, MOIndex As Integer) As Integer
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  GetPNEXT = r.v(sRegPre & "MOCTR" & MOIndex & "_CTRL") \ 2 ^ 8

End Function

'*****************************************************************************
'Description:
'        This function returns the PPREV pointer from the given MO.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'        MOIndex         index of the Message Object
'-----------------------------------------------------------------------------
'Returns:
'        PPREV
'*****************************************************************************
Function GetPPREV(r As ProjectData, vindex As Variant, MOIndex As Integer) As Integer
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  GetPPREV = r.v(sRegPre & "MOCTR" & MOIndex & "_CTRL") Mod 2 ^ 8

End Function


Function ResetValuesForFIFOSlave(r As ProjectData, vindex As Variant, iNr As Integer, iNextNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer

  ResetValuesForFIFOSlave = True

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  ' if iNr = iNextNr then the base object is part of the FIFO and must not be changed
  If (iNr <> iNextNr) Then
    Debug.Print "Reset FIFO slave " & iNextNr
    ' receive FIFO slave for a receive base object
    If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 1) Then
      r.v(sRegPre & "MOCTR" & iNextNr & "_MSGVAL") = 0
      r.v(sRegPre & "MOFCR" & iNextNr & "_MMC") = 0
      r.v(sRegPre & "MOFGPR" & iNextNr & "_CUR") = 0
    End If

    ' transmit FIFO slave for a transmit base object
    If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 2) Then
      r.v(sRegPre & "MOCTR" & iNextNr & "_MSGVAL") = 0
      r.v(sRegPre & "MOFCR" & iNextNr & "_MMC") = 0
      r.v(sRegPre & "MOFGPR" & iNextNr & "_CUR") = 0
    End If

    ' FIFO Settings for a Gateway destination
    If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 4) Then
      r.v(sRegPre & "MOCTR" & iNextNr & "_MSGVAL") = 0
      r.v(sRegPre & "MOFCR" & iNextNr & "_MMC") = 0
      r.v(sRegPre & "MOFCR" & iNextNr & "_FRREN") = 0
      r.v(sRegPre & "MOFGPR" & iNextNr & "_CUR") = 0
    End If

  End If

End Function

Function SetValuesForFIFOSlave(r As ProjectData, vindex As Variant, iNr As Integer, iNextNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer

  SetValuesForFIFOSlave = True

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  ' if iNr = iNextNr then the base object is part of the FIFO and must not be changed
  If (iNr <> iNextNr) Then
    ' receive FIFO slave for a receive base object
    If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 1) Then
      r.v(sRegPre & "MOCTR" & iNextNr & "_MSGVAL") = 1
      r.v(sRegPre & "MOFGPR" & iNextNr & "_CUR") = &HFF
      ' the values for MMC, DIR, ID ... are ignored, but it looks better if
      ' they are set
      r.v(sRegPre & "MOCTR" & iNextNr & "_DIR") = 0
      r.sfr(sRegPre & "MOFCR" & iNextNr) = 0 ' MMC = 0
      r.sfr(sRegPre & "MOAMR" & iNextNr) = r.sfr(sRegPre & "MOAMR" & iNr)
      r.sfr(sRegPre & "MOAR" & iNextNr) = r.sfr(sRegPre & "MOAR" & iNr)
    End If

    ' transmit FIFO slave for a transmit base object
    If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 2) Then
      r.v(sRegPre & "MOCTR" & iNextNr & "_MSGVAL") = 1
      r.v(sRegPre & "MOCTR" & iNextNr & "_DIR") = 1
      r.sfr(sRegPre & "MOFCR" & iNextNr) = 3 ' MMC = 3
      r.v(sRegPre & "MOFGPR" & iNextNr & "_CUR") = iNr
      r.sfr(sRegPre & "MOAMR" & iNextNr) = r.sfr(sRegPre & "MOAMR" & iNr)
      r.sfr(sRegPre & "MOAR" & iNextNr) = r.sfr(sRegPre & "MOAR" & iNr)
    End If

    ' FIFO Settings for a Gateway destination
    If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 4) Then
      r.v(sRegPre & "MOCTR" & iNextNr & "_MSGVAL") = 1
      r.v(sRegPre & "MOCTR" & iNextNr & "_DIR") = 1 - r.v(sRegPre & "MOCTR" & iNr & "_DIR")
      r.sfr(sRegPre & "MOFCR" & iNextNr) = 0 ' MMC = 0
      r.v(sRegPre & "MOFGPR" & iNextNr & "_CUR") = iNr
      r.sfr(sRegPre & "MOAR" & iNextNr) = r.sfr(sRegPre & "MOAR" & iNr)
    End If

  End If
End Function


'*****************************************************************************
'Description:
'        This function sets the standard mode for the selected object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the gateway object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made, else false
'
'*****************************************************************************
Function SetStandardMode(r As ProjectData, vindex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim bReturn As Boolean

  SetStandardMode = False

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  bReturn = True
  If (bReturn) Then
    ' if the last mode was FIFO mode then all slaves are unused
    ' and the pointer are cleared
    bReturn = ResetFIFOBaseMode(r, vindex, iNr)
  End If

  If (bReturn) Then
    '---------------------------------
    ' settings for the standard mode
    '---------------------------------
    Call ResetMofcrBitFields(r, vIndex, iNr)
   ''r.sfr(sRegPre & "MOFCR" & iNr) = 0 ' MMC = 0
    ' mark SEL, BOT, TOP and CUR as unused
    r.sfr(sRegPre & "MOFGPR" & iNr) = &HFFFFFFFF

    SetStandardMode = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function makes all settings for the FIFO base mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made, else false
'
'*****************************************************************************
Function SetFIFOBaseMode(r As ProjectData, vindex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim iOldMMC As Integer
  Dim bReturn As Boolean

  SetFIFOBaseMode = False

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  bReturn = True

  If (bReturn) Then
    ' save the current value of MMC for the case that the function
    ' ChangeFIFOSettings returns a False
'    iOldMMC = r.v(sRegPre & "MOFCR" & iNr & "_MMC")
'    r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 1
    bReturn = SetFIFOSlaves(r, vindex, iNr)

    If (bReturn) Then
      ' settings for the FIFO base object
      r.v(sRegPre & "MOFCR" & iNr & "_DLCC") = 0
      r.v(sRegPre & "MOFCR" & iNr & "_IDC") = 0
      r.v(sRegPre & "MOFCR" & iNr & "_GDFS") = 0
      r.v(sRegPre & "MOFCR" & iNr & "_DATC") = 0
      r.v(sRegPre & "MOFCR" & iNr & "_FRREN") = 0

      SetFIFOBaseMode = True
    Else
      ' restore old value of MMC
'      r.v(sRegPre & "MOFCR" & iNr & "_MMC") = iOldMMC
    End If
  End If

End Function


'*****************************************************************************
'Description:
'        This function resets all slave objects for a FIFO which are no
'        longer part of the FIFO.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made or no FIFO mode selected, else
'          false
'*****************************************************************************
Function ResetFIFOBaseMode(r As ProjectData, vindex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim bReturn As Boolean

  ResetFIFOBaseMode = True

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 1 Or r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 2 Or r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 4) Then  ' only if the FIFO or Gateway mode is selected
    
    ' clear all used FIFO slaves
    bReturn = ResetFIFOSlaves(r, vindex, iNr)

    If (bReturn) Then
      ' default settings for the FIFO base object
'      r.v(sRegPre & "MOFCR" & iNr & "_FD") = 0
    Else
      ResetFIFOBaseMode = False
    End If
  End If
End Function


'*****************************************************************************
'Description:
'        This function changes the settings of the slave FIFO objects when
'        they are changed in the base object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made or no FIFO mode selected, else
'          false
'*****************************************************************************
Function SetFIFOSlaves(r As ProjectData, vindex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer
  Dim iFirstNr As Integer
  Dim iLastNr As Integer
  Dim iNextNr As Integer

  SetFIFOSlaves = True

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 1 Or r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 2 Or r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 4) Then  ' only if the FIFO or Gateway mode is selected
    ' suche alle MOs von BOT bis TOP des MOs iNr
    iFirstNr = r.v(sRegPre & "MOFGPR" & iNr & "_BOT")
    iLastNr = r.v(sRegPre & "MOFGPR" & iNr & "_TOP")
    If (iFirstNr <= (CAN_MAX_OBJECTS - 1) And iLastNr <= (CAN_MAX_OBJECTS - 1)) Then
      If (iFirstNr <> iLastNr) Then
        iNextNr = iFirstNr
        Do
          SetValuesForFIFOSlave(r, vIndex, iNr, iNextNr)
          If (iNextNr = iLastNr) Then
          Else
            iNextNr = GetPNEXT(r, vindex, iNextNr)
          End If
        Loop Until iNextNr = iLastNr
      End If
      SetValuesForFIFOSlave(r, vIndex, iNr, iLastNr)
    End If

  End If
End Function


'*****************************************************************************
'Description:
'        This function resets all slave objects for a FIFO which are no
'        longer part of the FIFO.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made or no FIFO mode selected, else
'          false
'*****************************************************************************
Function ResetFIFOSlaves(r As ProjectData, vindex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim bReturn As Boolean
  Dim iFirstNr As Integer
  Dim iLastNr As Integer
  Dim iNextNr As Integer

  ResetFIFOSlaves = True

  sVarPre = "CAN" & vindex
  sRegPre = sVarPre & "_"

  If (r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 1 Or r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 2 Or r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 4) Then  ' only if the FIFO or Gateway mode is selected
    ' suche alle MOs von BOT bis TOP des MOs iNr
    iFirstNr = r.v(sRegPre & "MOFGPR" & iNr & "_BOT")
    iLastNr = r.v(sRegPre & "MOFGPR" & iNr & "_TOP")
    If (iFirstNr <= (CAN_MAX_OBJECTS - 1) And iLastNr <= (CAN_MAX_OBJECTS - 1)) Then
      If (iFirstNr <> iLastNr) Then
        iNextNr = iFirstNr
        Do
          ResetValuesForFIFOSlave(r, vIndex, iNr, iNextNr)
          If (iNextNr = iLastNr) Then
          Else
            iNextNr = GetPNEXT(r, vindex, iNextNr)
          End If
        Loop Until iNextNr = iLastNr
      End If
      ResetValuesForFIFOSlave(r, vIndex, iNr, iLastNr)
    End If

  End If
End Function


'*****************************************************************************
'Description:
'        This function sets the .Enabled, .DisabledTooltipText and
'        .EnabledTooltipText properties for all controls on the frame
'        with the caption szFramecaption of a peripherals object.
'-----------------------------------------------------------------------------
'Parameters:
'        szFramecaption     frame caption
'        c                  reference to the collection object
'        e                  reference to the extender collection object
'        bDisabled          disable condition
'        szDisabledTooltip  tooltip if control is disabled
'        szEnabledTooltip   tooltip if control is enabled
'
'*****************************************************************************
Sub SetDefaultPropertiesForAllControlsOnFrame(szFramecaption As String, c As Object, e As Object, bDisabled As Boolean, szDisabledTooltip As String, szEnabledTooltip As String)
  On Error Resume Next

  Dim o As Object
  Dim s As String

  ' for each control
  For Each o In c
    ' on this frame
    s = ""
    s = o.Container.Caption
    If (s = szFramecaption) Then
      o.Enabled = Not bDisabled
      ' determine tooltip
      If (bDisabled) Then
        ' tooltip if control is disabled
        e(o.Tag).DisabledTooltipText = szDisabledTooltip
      Else
        ' tooltip if control is enabled
        e(o.Tag).EnabledTooltipText = szEnabledTooltip
      End If
    End If
  Next
End Sub

'*****************************************************************************
'Description:
'        This function converts a time in usec to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the time in usec
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the time
'        .sUnit           the new unit of the time
'        .bDivider        the new divider of the new time, referring to s
'*****************************************************************************
Function ConvertTime(dValue As Double) As baudratetype
  On Error GoTo ConvertTimeError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "usec"
  dDivider = 1

  If (dHelp < 1) Then
    dHelp = dHelp * 1000
    sHelp = "ns"
    dDivider = dDivider / 1000
  End If

  If (dHelp >= 1000) Then
    dHelp = dHelp / 1000
    sHelp = "ms"
    dDivider = 1000
    If (dHelp >= 1000) Then
      dHelp = dHelp / 1000
      sHelp = "s"
      dDivider = dDivider * 1000
      If (dHelp >= 60) Then
        dHelp = dHelp / 60
        sHelp = "min"
        dDivider = dDivider * 60
        If (dHelp >= 60) Then
          dHelp = dHelp / 60
          sHelp = "h"
          dDivider = dDivider * 60
          If (dHelp >= 24) Then
            dHelp = dHelp / 24
            sHelp = "days"
            dDivider = dDivider * 24
            If (dHelp >= 30) Then
              dHelp = dHelp / 30
              sHelp = "months"
              dDivider = dDivider * 30
              If (dHelp >= 12) Then
                dHelp = dHelp / 12
                sHelp = "years"
                dDivider = dDivider * 12
              End If
            End If
          End If
        End If
      End If
    End If
  End If

  ConvertTime.dValue = dHelp
  ConvertTime.sUnit = sHelp
  ConvertTime.dDivider = dDivider


  Exit Function

ConvertTimeError:
  ConvertTime.dValue = dValue
  ConvertTime.sUnit = "usec"
  ConvertTime.dDivider = 1
  Exit Function
End Function

'*****************************************************************************
'Description:
'        This function checks if the requested interrupt service routine
'        is selected.
'        Example:
'        IsISRSelected(r, "SSC0", "SSC0_SRCT", "Tx")
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        VarPre           variable prefix (e.g. SSC0)
'        SRCRegName       the name of the service request control register
'                         (e.g. SSC0_SRCT)
'        ISRName          the name of the service request routine (e.g Tx)
'-----------------------------------------------------------------------------
'Returns:
'        True             the ISR is selected
'        False            else
'*****************************************************************************
Function IsISRSelected(r As ProjectData, VarPre As String, SRCRegName As String, ISRName As String) As Boolean
On Error Resume Next
  IsISRSelected = False
  IsISRSelected = (r.v(VarPre & "_USE_" & ISRName) = 1 And r.v(VarPre & "_USE_Init") = 1) '''And r.v(SRCRegName & "_TOS") = 0) '' And r.v(SRCRegName & "_SRPN") > 0)---->by ashish
End Function
'*****************************************************************************
'Description:
'        This functions convert 32-bit Data to 8-Bit Data.
'-----------------------------------------------------------------------------
Public Function Format8BitStatement(v As Variant, iNr As Integer) As Variant
    On Error GoTo Format8BitStatementError
    Dim tVal As String               ' used as index for loops
    Dim val As String
tVal = FormatTextOutput(v, "H8")
val = Mid(tVal, 2, Len(tVal))
        Format8BitStatement = Mid(val, (2 + (iNr * 2)), 2)
  Exit Function
Format8BitStatementError:
  Format8BitStatement = ""
End Function
'*****************************************************************************
'Description:
'        This functions disable LBM .
'-----------------------------------------------------------------------------
Sub DisableLBMx(r As ProjectData, vindex As Variant, iNr As Integer)
On Error Resume Next

Dim bDisabled As Boolean
Dim sVarPre As String
Dim sRegPre As String
Dim sNPCRx As String ' Node x Port Control Register


sVarPre = "CAN" & vindex
sRegPre = sVarPre & "_"
sNPCRx = sRegPre & "NPCR" & iNr


r.v(sNPCRx & "_LBM") = 0

End Sub
'*****************************************************************************
'Description:
'        This functions Reset MOFCR reg. bits.
'-----------------------------------------------------------------------------
Function ResetMofcrBitFields(r As ProjectData, vIndex As Variant, iNr As Integer)

On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String

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

r.v(sRegPre & "MOFCR" & iNr & "_MMC") = 0
r.v(sRegPre & "MOFCR" & iNr & "_GDFS") = 0
r.v(sRegPre & "MOFCR" & iNr & "_IDC") = 0
r.v(sRegPre & "MOFCR" & iNr & "_DLCC") = 0
r.v(sRegPre & "MOFCR" & iNr & "_DATC") = 0
'r.v(sRegPre & "MOFCR" & iNr & "_RXIE") = 0
'r.v(sRegPre & "MOFCR" & iNr & "_TXIE") = 0
r.v(sRegPre & "MOFCR" & iNr & "_OVIE") = 0
r.v(sRegPre & "MOFCR" & iNr & "_FRREN") = 0
r.v(sRegPre & "MOFCR" & iNr & "_RMM") = 0
r.v(sRegPre & "MOFCR" & iNr & "_SDT") = 0
r.v(sRegPre & "MOFCR" & iNr & "_STT") = 0
'r.v(sRegPre & "MOFCR" & iNr & "_DLC") = 0

End Function
'*****************************************************************************
'Description:
'        This functions used for PUSH/IEN relate
'-----------------------------------------------------------------------------
Public Function GenerateAMprotection_FunBeg(r As ProjectData, vIndex As Variant, f As Variant, iNr As Integer) As Variant
    On Error GoTo GenerateAMprotection_FunBegError
    
    Dim v As Variant               ' used as temp variable for register operations
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    
    sVarPre = "CAN" & vIndex     ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers

        If (r.v("MAIN_COMPILER") = "Keil") Then
                If ((r.v(sRegPre & "USE_AMP") = 0) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    CAN_pushAMRegs();    //   PUSH the CAN Access Mediator Register"
                ElseIf ((r.v(sRegPre & "USE_AMP") = 1) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    EA = 0;    //   disable global interrupts"
                End If
        ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
                If ((r.v(sRegPre & "USE_AMP") = 0) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    CAN_pushAMRegs();    //   PUSH the CAN Access Mediator Register"
                ElseIf ((r.v(sRegPre & "USE_AMP") = 1) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    EA = 0;    //   disable global interrupts"
                End If
        ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
                If ((r.v(sRegPre & "USE_AMP") = 0) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    CAN_pushAMRegs();    //   PUSH the CAN Access Mediator Register"
                ElseIf ((r.v(sRegPre & "USE_AMP") = 1) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    EA = 0;    //   disable global interrupts"
                End If
        End If
    
GenerateAMprotection_FunBegError:
    GenerateAMprotection_FunBeg = ""
End Function

'*****************************************************************************
'Description:
'        This functions used for POP/IEN relate
'-----------------------------------------------------------------------------
Public Function GenerateAMprotection_FunEnd(r As ProjectData, vIndex As Variant, f As Variant, iNr As Integer) As Variant
    On Error GoTo GenerateAMprotection_FunEndError
    
    Dim v As Variant               ' used as temp variable for register operations
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    
    sVarPre = "CAN" & vIndex     ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers
    
        If (r.v("MAIN_COMPILER") = "Keil") Then
                If ((r.v(sRegPre & "USE_AMP") = 0) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    CAN_popAMRegs();    //   POP the CAN Access Mediator Register"
                ElseIf ((r.v(sRegPre & "USE_AMP") = 1) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    EA = 1;    //   enable global interrupts"
                End If
        ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
                If ((r.v(sRegPre & "USE_AMP") = 0) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    CAN_popAMRegs();    //   POP the CAN Access Mediator Register"
                ElseIf ((r.v(sRegPre & "USE_AMP") = 1) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    EA = 1;    //   enable global interrupts"
                End If
        ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
                If ((r.v(sRegPre & "USE_AMP") = 0) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    CAN_popAMRegs();    //   POP the CAN Access Mediator Register"
                ElseIf ((r.v(sRegPre & "USE_AMP") = 1) And (r.v(sRegPre & "DIS_AMP") = 0)) Then
                        Print #f, "    EA = 1;    //   enable global interrupts"
                End If
        End If

GenerateAMprotection_FunEndError:
    GenerateAMprotection_FunEnd = ""
End Function

'*****************************************************************************
'Description:
'        This functions Releases the current used pin in receiver side.
'-----------------------------------------------------------------------------
Sub NodeRXDPinSelectionReset(r As projectData, vIndex As Variant, INr As Integer)
Dim pinid As Integer
If (INr = 0) Then
    pinid = r.v("CAN_USE_NODE0_RXD_PIN")
     If (pinid <> 0) Then
       pinid = pinid - 1
       Call ReleaseAlternateInputPin(r, "RXDC0_" & pinid)
    End If
Else
    pinid = r.v("CAN_USE_NODE1_RXD_PIN")
    If (pinid <> 0) Then
      If (pinid = 3) Then
     Call ReleaseAlternateInputPin(r, "RXDC1_" & pinid)
   Else
    pinid = pinid - 1
    Call ReleaseAlternateInputPin(r, "RXDC1_" & pinid)
      End If
  End If
End If

End Sub

'*****************************************************************************
'Description:
'        This functions Releases the current used pin in transmitter side.
'-----------------------------------------------------------------------------
Sub NodeTXDPinSelectionReset(r As projectData, vIndex As Variant, INr As Integer)
Dim pinid As Integer
If (INr = 0) Then
pinid = r.v("CAN_USE_NODE0_TXD_PIN")
    If (pinid <> 0) Then
    pinid = pinid - 1
 Call ReleaseAlternateOutputPin(r, "TXDC0_" & pinid)
End If
Else
pinid = r.v("CAN_USE_NODE1_TXD_PIN")

 If (pinid <> 0) Then
If (pinid = 3) Then
   Call ReleaseAlternateOutputPin(r, "TXDC1_" & pinid)
 Else
pinid = pinid - 1
Call ReleaseAlternateOutputPin(r, "TXDC1_" & pinid)
 End If
 End If
End If
End Sub
