Attribute VB_Name = "Module1"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Port.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "Include.inc"
Option Explicit
'*****************************************************************************
' Module:
'        CAN Interrupt
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'CAN'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        ----------------------------------------------------------------------
'        0.1  sheshadri      05.03.06     initial version for XC886CLM
'
'*****************************************************************************

Sub GenerateCANIsrVar(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, uc As UserCode)
    On Error GoTo GenerateCANIsrVarError
    
    If (iSRN = 1) Then
       If ((r.v("CAN_USE_SRN1") = 1 And r.v("CAN_USE_SRN2") = 1 And r.v("CAN_EN_ISRCODE") <> 0)) Then
            Print #f, "  ubyte ubSRNFlag = 0;"
       End If
    End If
    
    If (r.v("CAN_EN_ISRCODE") <> 0) Then
       Call GenerateVarForNodeSRN(r, vIndex, f, iSRN)
       Print #f, ""
       Call GenerateVarForMsgObjxSRN(r, vIndex, f, iSRN)
    End If
     
GenerateCANIsrVarError:
    Exit Sub
End Sub


Sub GenerateCANIsr(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, uc As UserCode)
    On Error GoTo GenerateCANIsrError
    
    Dim v As Variant               ' used as temp variable for register operations
    Dim iNode As Integer
    
    'begining - code used for 1 and 2 shared vIntr
    If (iSRN = 1) Then
        If (r.v("CAN_EN_ISRCODE") <> 0) Then
           If (((r.v("CAN_USE_SRN1") = 1) And (r.v("CAN_USE_SRN2") = 1))) Then
                Print #f, "    if(ubSRNFlag != 0)"
                Print #f, "    {"
           End If
        End If
    End If
    'end - code used for 1 and 2 shared vIntr
        
    For iNode = 0 To (CAN_MAX_NODES - 1)
        If (r.v("CAN_EN_ISRCODE") = 1) Then
           Call GenerateSRNNodex_32(r, vIndex, f, iSRN, iNode, uc)
        ElseIf (r.v("CAN_EN_ISRCODE") = 2) Then
           Call GenerateSRNNodex_8(r, vIndex, f, iSRN, iNode, uc)
        End If
    Next iNode
        
    If (r.v("CAN_EN_ISRCODE") = 1) Then
       Call GenerateMOIntrObjx_32(r, vIndex, f, iSRN, uc, "SRN" & iSRN)
    ElseIf (r.v("CAN_EN_ISRCODE") = 2) Then
       Call GenerateMOIntrObjx_8(r, vIndex, f, iSRN, uc, "SRN" & iSRN)
    End If
    
    'begining - code used for 1 and 2 shared vIntr
    If (iSRN = 1) Then
        If (r.v("CAN_EN_ISRCODE") <> 0) Then
            If (((r.v("CAN_USE_SRN1") = 1) And (r.v("CAN_USE_SRN2") = 1))) Then
                Print #f, "    ubSRNFlag = 0;"
                Print #f, "    }"
            End If
        End If
    End If
    'end - code used for 1 and 2 shared vIntr
    
GenerateCANIsrError:
    Exit Sub
End Sub

'*****************************************************************************
'Description:
'        This functions generates the code and statements for the SRN CAN
'        node x.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'        f                file handle
'        iSRN             number of service request node (0 - 7)
'        vNode            number of node (A - B)
'        uc               reference to the UserCode object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Sub GenerateSRNNodex_8(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, iNode As Integer, uc As UserCode)
    On Error GoTo GenerateSRNNodex_8Error

    Dim i As Integer               ' used as index for loops
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim n As String                ' used as temp variable for string operations

    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 sNSRx As String  ' Node x Status Register

    sVarPre = "CAN" & vIndex       ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers
    
    n = "SRN" & iSRN & "_NODE" & iNode

    sNCRx = sRegPre & "NCR" & iNode
    sNIPRx = sRegPre & "NIPR" & iNode
    sNPCRx = sRegPre & "NPCR" & iNode
    sNBTRx = sRegPre & "NBTR" & iNode
    sNECNTx = sRegPre & "NECNT" & iNode
    sNFCRx = sRegPre & "NFCR" & iNode
    sNSRx = sRegPre & "NSR" & iNode

    ' if ALIE or LECIE or TRIE
    If ((r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2))) Or _
        (r.v(sNCRx & "_TRIE") = 1 And (r.v(sNIPRx & "_TRINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_TRINP") = 2))) Or _
        (r.v(sNCRx & "_LECIE") = 1 And (r.v(sNIPRx & "_LECINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_LECINP") = 2))) Or _
        (r.v(sNFCRx & "_CFCIE") = 1 And (r.v(sNIPRx & "_CFCINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_CFCINP") = 2)))) Then
            Print #f, ""
            Print #f, FormatSectionHeader(4, 0, "Interrupts of CAN node " & iNode)
            Print #f, ""
        If ((r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2))) Or _
            (r.v(sNCRx & "_TRIE") = 1 And (r.v(sNIPRx & "_TRINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_TRINP") = 2))) Or _
            (r.v(sNCRx & "_LECIE") = 1 And (r.v(sNIPRx & "_LECINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_LECINP") = 2)))) Then
            Print #f, "    CAN_vWriteCANAddress(" & sNSRx & ");"
            Print #f, "    CAN_vReadEN();"
            Print #f, "    ubNSRL = CAN_DATA0;"
        End If
        If ((r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2)))) Then
            Print #f, "    ubNSRH = CAN_DATA1;"
        End If
            Print #f, ""

        Print #f, uc.GetUserCode("(" & n & ",1)", 4)
            '###################################################################################
            '            Alert Interrupt Node Pointer Interrupt
            '###################################################################################
        If (r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2))) Then
            n = "SRN" & iSRN & "_NODE_" & iNode & "_ALIE"
            Print #f, FormatSectionHeader(4, 0, "CAN node " & iNode & " Alert Interrupt")
            Print #f, "    if (ubNSRL & 0x20)      // if ALERT"
            Print #f, "    {"
            Print #f, ""
            Print #f, FormatCommentsForSource(10, 1, "Note: The ALERT bit is set upon the occurrence of one of" & _
                    "the following events. User need to handle these in user defined section.")
            Print #f, ""
            Print #f, "          // BOFF : ubNSRL & 0x80"
            Print #f, "                // Bus OFF"
            Print #f, "          // EWRN : ubNSRL & 0x40"
            Print #f, "                // One of the error counters REC or TEC reached the"
            Print #f, "                // warning limit of " & r.v(sNECNTx & "_EWRNLVL") & "."
            Print #f, "          // LLE : ubNSRH & 0x01"
            Print #f, "                // A list length error has been detected during message"
            Print #f, "                // acceptance filtering. The number of elements in the list"
            Print #f, "                // that belongs to this CAN node differs from the list SIZE "
            Print #f, "                // given in the list termination pointer"
            Print #f, "          // LOE : ubNSRH & 0x02"
            Print #f, "                // A list object error has been detected during message"
            Print #f, "                // acceptance filtering. A message object with wrong LIST "
            Print #f, "                // index entry in the Message Object Control Register has "
            Print #f, "                // been detected"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",2)", 6)
            Print #f, "    }"
            Print #f, ""
'            Print #f, uc.GetUserCode("(" & n & ",3)", 6)
'            Print #f, ""
        End If
        
    '###################################################################################
    '            Transfer OK Interrupt
    '###################################################################################
        
        If (r.v(sNCRx & "_TRIE") = 1 And (r.v(sNIPRx & "_TRINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_TRINP") = 2))) Then
            n = "SRN" & iSRN & "_NODE_" & iNode
            Print #f, ""
            Print #f, FormatSectionHeader(4, 0, "CAN node " & iNode & " Transfer Interrupt")
            Print #f, ""
            Print #f, "    if (ubNSRL & 0x18)  // if TXOK or RXOK"
            Print #f, "    {"
            Print #f, FormatCommentsForSource(10, 1, "Note: The the successful transmission/reception " & _
                    "of a frame, can cause TXOK or RXOK Interrupt. " & _
                    "User need to handle these in user defined section.")
            Print #f, ""
            Print #f, "          // Indicates that a message has been transmitted/received successfully."
            Print #f, "          // TXOK = ubNSRL & 0x08"
            Print #f, "          // RXOK = ubNSRL & 0x10"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",5)", 6)
            Print #f, "    }"
        End If
        
    '###################################################################################
    '            Last Error Code Interrupt
    '###################################################################################
        If (r.v(sNCRx & "_LECIE") = 1 And (r.v(sNIPRx & "_LECINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_LECINP") = 2))) Then
            n = "SRN" & iSRN & "_NODE" & iNode & "_LEC"
            Print #f, FormatSectionHeader(4, 0, "CAN node " & iNode & " Last Error Code")
            Print #f, "    if (ubNSRL & 0x07)  // if LEC"
            Print #f, "    {"
                Call GenerateNODE_LECIntr_8(r, vIndex, f, iSRN, iNode, uc)
            Print #f, ""
            Print #f, "      ubResetLEC = 0x38;"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",7)", 6)
            Print #f, "    }"  ' if LEC
        End If
        If ((r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2))) Or _
            (r.v(sNCRx & "_TRIE") = 1 And (r.v(sNIPRx & "_TRINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_TRINP") = 2))) Or _
            (r.v(sNCRx & "_LECIE") = 1 And (r.v(sNIPRx & "_LECINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_LECINP") = 2)))) Then
            Print #f, ""
            n = "SRN" & iSRN & "_NODE" & iNode
            Print #f, uc.GetUserCode("(" & n & ",8)", 4)
            Print #f, ""
            Print #f, "    //// Reset LEC, TXOK, RXOK, ALERT, EWRN, BOFF, LLE, LOE (if set)"
            Print #f, ""
            Print #f, "    CAN_vWriteCANAddress(" & sNSRx & "); // Addressing " & sNSRx
            Print #f, "    CAN_DATA0   =  ~(ubNSRL & ubResetLEC);       // load " & sNSRx & " status register[7-0]"
            If (r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2))) Then
                Print #f, "    CAN_DATA1   =  ~(ubNSRH);       // load " & sNSRx & " status register[15-8]"
            End If
            If (r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2))) Then
                Print #f, "    CAN_vWriteEN(D0_VALID+D1_VALID);  // Data0 and Data1 are Valid for"
            Else
                Print #f, "    CAN_vWriteEN(D0_VALID);  // Data0 Valid for"
            End If
            Print #f, "                                 // transmission and Write is Enabled."
        End If
    End If
    
    '###################################################################################
    '            Frame Counter Overflow Interrupt
    '###################################################################################
      If (r.v(sNFCRx & "_CFCIE") = 1 And (r.v(sNIPRx & "_CFCINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_CFCINP") = 2))) Then
        n = "SRN" & iSRN & "_NODE" & iNode & "_CFCIE"
        Print #f, ""
        Print #f, FormatSectionHeader(4, 0, "Interrupts of CAN node " & iNode & " Frame Counter Overflow Interrupt")
        Print #f, "    CAN_vWriteCANAddress(" & sNFCRx & ");"
        Print #f, "    CAN_vReadEN();"
        Print #f, "    ubNFCR = CAN_DATA2 ;"
        Print #f, "    if( ubNFCR & 0x80 )    // if frame counter overflow flag "
        Print #f, "    {"
        Print #f, uc.GetUserCode("(" & n & ", 38 )", 6)
        Print #f, ""
        Print #f, "      CAN_DATA2 = ubNFCR & (0x7F); // reset overflow flag "
        Print #f, "      CAN_vWriteEN(D2_VALID);  // Data2 is valid for transmission and"
        Print #f, "                               // Write is Enabled."
        Print #f, "    }"
      End If
    Exit Sub
    
GenerateSRNNodex_8Error:
    Exit Sub
End Sub


'*****************************************************************************
'Description:
'        This functions generates the code and statements for the SRN CAN
'        message object x.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'        f                file handle
'        iSRN             number of service request node (0 - 7)
'        iObj             number of message object (0 - 31)
'        uc               reference to the UserCode object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Sub GenerateObjxCode_8(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, uc As UserCode)
    On Error GoTo GenerateObjxCode_8Error

    Dim i As Integer               ' used as index for loops
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim n As String                ' used as temp variable for string operations
    
    Dim iNr As Integer               ' used as index for loops
    Dim TXIESelected As Boolean
    Dim RXIESelected As Boolean
    TXIESelected = False
    RXIESelected = False
    
    sVarPre = "CAN" & vIndex       ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers
    n = "SRN" & iSRN & "_OBJ"
      
      For iNr = 0 To 31
        If ((r.v(sRegPre & "MOIPR" & iNr & "_RXINP") = iSRN And r.v(sRegPre & "MOFCR" & iNr & "_RXIE") = 1)) Then
            RXIESelected = True
        End If
            
        If ((r.v(sRegPre & "MOIPR" & iNr & "_TXINP") = iSRN And r.v(sRegPre & "MOFCR" & iNr & "_TXIE") = 1)) Then
            TXIESelected = True
        End If
      Next iNr
      
      If (iSRN = 1) Then
        For iNr = 0 To 31
          If ((r.v(sRegPre & "MOIPR" & iNr & "_RXINP") = 2 And r.v(sRegPre & "MOFCR" & iNr & "_RXIE") = 1)) Then
              RXIESelected = True
          End If
              
          If ((r.v(sRegPre & "MOIPR" & iNr & "_TXINP") = 2 And r.v(sRegPre & "MOFCR" & iNr & "_TXIE") = 1)) Then
              TXIESelected = True
          End If
        Next iNr
      End If
      
      ' if Receive interrupt for object iObj is enabled
      If (RXIESelected = True) Then
            Print #f, ""
            Print #f, FormatCommentsForSource(10, 1, "Note: Below commented code could be copied by the user to " & _
                "user defined section for Interrupt handling.")
            Print #f, ""
            Print #f, FormatCommentsForSource(10, 1, "Handling of MO RXPND for receive data/remote frames " & _
                "(Generic Code).")
            Print #f, FormatCommentsForSource(10, 1, "(ubTempVarObjHandle & MOSTAT_RXPND)")
            Print #f, ""
            Print #f, "          // if( ubTempVarObjHandler & MOSTAT_RXPND)   // if RXPND is set"
            Print #f, "          // {"
            Print #f, "          //"
        ' if Receive Object => data frame received
            Print #f, "          //   if( ubTempVarObjHandler & MOSTAT_NEWDAT)   // if NEWDAT is set"
            Print #f, "          //   {"
            Print #f, "          //"
            Print #f, "          //     if( ubTempVarObjHandler & MOSTAT_MSGLST)  // if MSGLST is set"
            Print #f, "          //     {"
            Print #f, "          //// Indicates that the CAN controller has stored a new message into this object,"
            Print #f, "          //// while NEWDAT was still set, ie. the previously stored message is lost."
            Print #f, "          //"
            Print #f, "          //      }"
            Print #f, "          //     else"
            Print #f, "          //     {"
            Print #f, "          //// The CAN controller has stored a new message into this object."
'''' Print #f, "          // The CAN controller has received a remote frame"
            Print #f, "          //"
            Print #f, "          //      }"
            Print #f, "          //"
            Print #f, "          //    }"
            Print #f, "          //"
            Print #f, "          //// Reset RXPND, NEWDAT, MSGLST (if set)"
            Print #f, "          //"
            Print #f, "          //  CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn"
            Print #f, "          //"
            Print #f, "          //  CAN_DATA0 = (ubTempVarObjHandler & MOSTAT_RST_MNR); // load CAN Data Register 0"
            Print #f, "          //  CAN_DATA1 = 0x00; // load CAN Data Register 1"
            Print #f, "          //  CAN_DATA2 = 0x00; // load CAN Data Register 2"
            Print #f, "          //  CAN_DATA3 = 0x00; // load CAN Data Register 3"
            Print #f, "          //  CAN_vWriteEN(ALL_DATA_VALID);   // Writemode is Enabled"
            Print #f, "          // }  // End of RXPNDn"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",1)", 6)
      End If
      
      ' if Transmit interrupt for object n is enabled
      If (TXIESelected = True) Then
            Print #f, ""
            Print #f, FormatCommentsForSource(10, 1, "Note: Below commented code could be copied by the user to " & _
                "user defined section for Interrupt handling.")
            Print #f, ""
            Print #f, FormatCommentsForSource(10, 1, "Handling of MO TXPND for transmit data/remote frames " & _
                "(Generic Code).")
            Print #f, FormatCommentsForSource(10, 1, "(ubTempVarObjHandle & MOSTAT_TXPND)")
            Print #f, ""
            Print #f, "          // if( ubTempVarObjHandler & MOSTAT_TXPND)   // if TXPND is set"
            Print #f, "          // {"
            Print #f, "          //// The transmission of the last message object was successful."
            Print #f, "          //// reset TXPND, NEWDAT"
            Print #f, "          //    CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn"
            Print #f, "          //"
            Print #f, "          //    CAN_DATA0 = (MOSTAT_TXPND + MOSTAT_NEWDAT); // load CAN Data Register 0"
            Print #f, "          //    CAN_DATA1 = 0x00; // load CAN Data Register 1"
            Print #f, "          //    CAN_DATA2 = 0x00; // load CAN Data Register 2"
            Print #f, "          //    CAN_DATA3 = 0x00; // load CAN Data Register 3"
            Print #f, "          //    CAN_vWriteEN(ALL_DATA_VALID); // Writemode is Enabled"
            Print #f, "          //"
            Print #f, "          // }  // End of TXPNDn"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",2)", 6)
      End If
    Exit Sub
GenerateObjxCode_8Error:
    Exit Sub
End Sub

Public Function GenerateVarForNodeSRN(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer) As Variant
    On Error GoTo GenerateVarForNodeSRNError
    
    Dim sNCRx As String  ' Node x Control Register
    Dim sNIPRx As String ' Node x Interrupt Pointer Register
    Dim sNFCRx As String ' Node x Interrupt Pointer Register
    Dim iNode As Integer
    
    Dim LVarSelected As Boolean
    Dim HVarSelected As Boolean
    Dim FOVarSelected As Boolean
    LVarSelected = False
    HVarSelected = False
    FOVarSelected = False
    
    For iNode = 0 To (CAN_MAX_NODES - 1)
        sNCRx = "CAN_NCR" & iNode
        sNIPRx = "CAN_NIPR" & iNode
        sNFCRx = "CAN_NFCR" & iNode
        If (r.v(sNCRx & "_LECIE") = 1 And (r.v(sNIPRx & "_LECINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_LECINP") = 2))) Then
            LVarSelected = True
        End If
        If (r.v(sNCRx & "_TRIE") = 1 And (r.v(sNIPRx & "_TRINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_TRINP") = 2))) Then
            LVarSelected = True
        End If
        If (r.v(sNCRx & "_ALIE") = 1 And (r.v(sNIPRx & "_ALINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_ALINP") = 2))) Then
            LVarSelected = True
            HVarSelected = True
        End If
        If (r.v(sNFCRx & "_CFCIE") = 1 And (r.v(sNIPRx & "_CFCINP") = iSRN Or (iSRN = 1 And r.v(sNIPRx & "_CFCINP") = 2))) Then
            FOVarSelected = True
        End If
    Next iNode
        
    If (LVarSelected = True) Then
        Print #f, "ubyte ubNSRL;"
    End If
    If (HVarSelected = True) Then
        Print #f, "ubyte ubNSRH;"
    End If
    If (FOVarSelected = True) Then
        Print #f, "ubyte ubNFCR;"
    End If
    If ((LVarSelected = True) And (LVarSelected = True)) Then
        Print #f, "ubyte ubResetLEC = 0x3F;"
    End If
    
GenerateVarForNodeSRNError:
    GenerateVarForNodeSRN = ""
End Function

Public Function GenerateVarForMsgObjxSRN(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer) As Variant
    On Error GoTo GenerateVarForMsgObjxSRNError
    
    Dim v As Variant               ' used as temp variable for register operations
    Dim iObj As Integer
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim varFlag As Boolean
    
    varFlag = False
    sVarPre = "CAN" & vIndex     ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers
    
    For iObj = 0 To (CAN_MAX_OBJECTS - 1)
        If ((r.v(sRegPre & "MOIPR" & iObj & "_RXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_RXIE") = 1) Or _
            (r.v(sRegPre & "MOIPR" & iObj & "_TXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_TXIE") = 1)) Then
            varFlag = True
        End If
        If (iSRN = 1) Then
            If ((r.v(sRegPre & "MOIPR" & iObj & "_RXINP") = 2 And r.v(sRegPre & "MOFCR" & iObj & "_RXIE") = 1) Or _
                (r.v(sRegPre & "MOIPR" & iObj & "_TXINP") = 2 And r.v(sRegPre & "MOFCR" & iObj & "_TXIE") = 1)) Then
                varFlag = True
            End If
        End If
    Next iObj

    If (varFlag = True) Then
        Print #f, "ubyte ubTempMsgID = 0;"
      If (r.v("MAIN_COMPILER") = "Keil") Then
        Print #f, "ubyte ubTempVarObjHandler = 0;"
      ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        Print #f, "volatile ubyte ubTempVarObjHandler = 0;"
      ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
        Print #f, "volatile ubyte ubTempVarObjHandler = 0;"
      End If
        Print #f, "un_32bit ulBit_Pos_Mask;"
        Print #f, ""
    End If
    'REMOVED FOR COMMENTS        Print #f, "ubyte ubRstVal = 0;"

GenerateVarForMsgObjxSRNError:
    GenerateVarForMsgObjxSRN = ""
End Function

Public Function GenerateNODE_LECIntr_8(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, iNode As Integer, uc As UserCode) As Variant
    On Error GoTo GenerateNODE_LECIntr_8Error
    
    Dim n As String                ' used as temp variable for string operations
    n = "SRN" & iSRN & "_NODE" & iNode
    
        Print #f, FormatCommentsForSource(10, 1, "Note: a CAN protocol error with a last error code, " & _
              "details of bit field are mentioned below. " & _
              "User need to handle these in user defined section.")
        Print #f, ""
        Print #f, "          // Stuff Error: (ubNSRL & 0x07) == 1"
        Print #f, FormatCommentsForSource(12, 1, "More than 5 equal bits in a sequence have occurred" & _
                        "in a part of a received message where this is not allowed.")
        Print #f, ""
        Print #f, "          // Form Error: (ubNSRL & 0x07) == 2"
        Print #f, FormatCommentsForSource(12, 1, "A fixed format part of a received frame has the wrong format.")
        Print #f, ""
        Print #f, "          // Ack Error: (ubNSRL & 0x07) == 3"
        Print #f, FormatCommentsForSource(12, 1, "The message this CAN controller transmitted was" & _
                "not acknowledged by another node.")
        Print #f, ""
        Print #f, "          // Bit1 Error: (ubNSRL & 0x07) == 4"
        Print #f, FormatCommentsForSource(12, 1, "During the transmission of a message (with the" & _
                " exeption of the arbitration field), the device wanted to send a recessive level (""1""), but the" & _
                " monitored bus value was dominant.")
        Print #f, ""
        Print #f, "          // Bit0 Error: (ubNSRL & 0x07) == 5"
        Print #f, FormatCommentsForSource(12, 1, "During the transmission of a message (or acknow-" & _
                " ledge bit, active error flag, or overload flag), the device wanted to send a dominant level (""0"")," & _
                " but the monitored bus value was recessive.")
        Print #f, ""
        Print #f, FormatCommentsForSource(12, 1, "During busoff recovery this status is set each time " & _
                " a sequence of 11 recessive bits has been monitored. The CPU may use this code as indication that the " & _
                " bus is not continuously disturbed).")
        Print #f, ""
        Print #f, "          // CRC Error: (ubNSRL & 0x07) == 6"
        Print #f, FormatCommentsForSource(12, 1, "The CRC check sum was incorrect in the message received.")

GenerateNODE_LECIntr_8Error:
    GenerateNODE_LECIntr_8 = ""
End Function


Sub GenerateMOIntrObjx_8(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, uc As UserCode, n As String)
    On Error GoTo GenerateMOIntrObjx_8Error
    
    Dim m As Integer               ' used as index for loops
    Dim vRxMask(0 To 7) As Variant
    Dim vTxMask(0 To 7) As Variant
    Dim vMsgPndMask As Long
    Dim iObj As Integer
    Dim i As Integer               ' used as index for loops
    Dim j As Integer               ' used as index for loops
    Dim iNr As Integer
    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

    For m = 0 To ((CAN_MAX_OBJECTS \ 32) - 1) ' 7 ' for each Message Pending register
      vRxMask(m) = 0
      vTxMask(m) = 0
      For j = 0 To 31
        iNr = m * 32 + j
        If (((r.v(sRegPre & "MOIPR" & iNr & "_RXINP") = iSRN Or (iSRN = 1 And r.v(sRegPre & "MOIPR" & iNr & "_RXINP") = 2)) And r.v(sRegPre & "MOFCR" & iNr & "_RXIE") = 1) Or _
            ((r.v(sRegPre & "MOIPR" & iNr & "_TXINP") = iSRN Or (iSRN = 1 And r.v(sRegPre & "MOIPR" & iNr & "_TXINP") = 2)) And r.v(sRegPre & "MOFCR" & iNr & "_TXIE") = 1)) Then
            vRxMask(m) = vRxMask(m) + 2 ^ j
        End If
      Next j
      vTxMask(m) = vRxMask(m)

      If (Not (vRxMask(m) = 0)) Then
        Print #f, ""
        Print #f, FormatSectionHeader(4, 0, "Interrupts of CAN Msg Obj x ")
        Print #f, ""
        Print #f, "    CAN_vWriteCANAddress(CAN_MSIMASK);    // set message index mask register"
        Print #f, FormatCodeStatemente(4, "CAN_DATA0", "= ", "0x" & Format8BitStatement((vRxMask(m)), 3), "set message index mask register")
        Print #f, FormatCodeStatemente(4, "CAN_DATA1", "= ", "0x" & Format8BitStatement((vRxMask(m)), 2), "set message index mask register")
        Print #f, FormatCodeStatemente(4, "CAN_DATA2", "= ", "0x" & Format8BitStatement((vRxMask(m)), 1), "set message index mask register")
        Print #f, FormatCodeStatemente(4, "CAN_DATA3", "= ", "0x" & Format8BitStatement((vRxMask(m)), 0), "set message index mask register")
        Print #f, ""
        Print #f, "    CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & "," & 1 + m & ")", 4)
        Print #f, "    CAN_vWriteCANAddress(" & sRegPre & "MSID0);   // message index register"
        Print #f, "    CAN_vReadEN();               // Read Mode is enabled"
        Print #f, ""
        Print #f, "    ubTempMsgID = CAN_DATA0;"
        Print #f, ""
        Print #f, "    if(ubTempMsgID != 0x20)"
        Print #f, "    {"
        Print #f, "      do"
        Print #f, "      {"
        Print #f, "      CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn"
        Print #f, "      CAN_vReadEN();               // Read Mode is enabled"
        Print #f, "      ubTempVarObjHandler = CAN_DATA0;"
        Print #f, ""
        Print #f, "      ulBit_Pos_Mask.ulVal = ((unsigned long) 1 << ubTempMsgID);"
        Print #f, "      CAN_vWriteCANAddress(CAN_MSPND0); // Addressing CAN_MSPND0"
        Print #f, ""
       If (r.v("MAIN_COMPILER") = "Keil") Then
        Print #f, "      CAN_DATA0 = ~ulBit_Pos_Mask.ubDB[3]; // clear message pending register"
        Print #f, "      CAN_DATA1 = ~ulBit_Pos_Mask.ubDB[2]; // clear message pending register"
        Print #f, "      CAN_DATA2 = ~ulBit_Pos_Mask.ubDB[1]; // clear message pending register"
        Print #f, "      CAN_DATA3 = ~ulBit_Pos_Mask.ubDB[0]; // clear message pending register"
       ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        Print #f, "      CAN_DATA0 = ~ulBit_Pos_Mask.ubDB[0]; // clear message pending register"
        Print #f, "      CAN_DATA1 = ~ulBit_Pos_Mask.ubDB[1]; // clear message pending register"
        Print #f, "      CAN_DATA2 = ~ulBit_Pos_Mask.ubDB[2]; // clear message pending register"
        Print #f, "      CAN_DATA3 = ~ulBit_Pos_Mask.ubDB[3]; // clear message pending register"
       ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
        Print #f, "      CAN_DATA0 = ~ulBit_Pos_Mask.ubDB[0]; // clear message pending register"
        Print #f, "      CAN_DATA1 = ~ulBit_Pos_Mask.ubDB[1]; // clear message pending register"
        Print #f, "      CAN_DATA2 = ~ulBit_Pos_Mask.ubDB[2]; // clear message pending register"
        Print #f, "      CAN_DATA3 = ~ulBit_Pos_Mask.ubDB[3]; // clear message pending register"
       End If
        Print #f, "      CAN_vWriteEN(ALL_DATA_VALID);  // Write mode Enabled"

        If (r.v("CAN_EN_ISRCODE") = 2) Then
              Call GenerateObjxCode_8(r, vIndex, f, iSRN, uc)
        End If
             
        Print #f, ""
        Print #f, "      CAN_vWriteCANAddress(CAN_MSIMASK);    // set message index mask register"
        Print #f, FormatCodeStatemente(6, "CAN_DATA0", "= ", "0x" & Format8BitStatement((vRxMask(m)), 3), "set message index mask register")
        Print #f, FormatCodeStatemente(6, "CAN_DATA1", "= ", "0x" & Format8BitStatement((vRxMask(m)), 2), "set message index mask register")
        Print #f, FormatCodeStatemente(6, "CAN_DATA2", "= ", "0x" & Format8BitStatement((vRxMask(m)), 1), "set message index mask register")
        Print #f, FormatCodeStatemente(6, "CAN_DATA3", "= ", "0x" & Format8BitStatement((vRxMask(m)), 0), "set message index mask register")
        Print #f, ""
        Print #f, "      CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & "," & 5 + m & ")", 6)
        Print #f, ""
        Print #f, "      CAN_vWriteCANAddress(" & sRegPre & "MSID0);   // message index register"
        Print #f, "      CAN_vReadEN();               // Read Mode is enabled"
        Print #f, "      ubTempMsgID = CAN_DATA0;"
        Print #f, ""
        Print #f, "      }while (ubTempMsgID != 0x20); // end while"
        Print #f, ""
        Print #f, "    }  // end if"
'        Print #f, uc.GetUserCode("(" & n & "," & 7 + m & ")", 4)
'        Print #f, ""
      End If
    Next m

GenerateMOIntrObjx_8Error:
    Exit Sub
End Sub


'*****************************************************************************
'Description:
'        Same as 32-Bit
'        This functions generates the code and statements for the SRN CAN
'        node x.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'        f                file handle
'        iSRN             number of service request node (0 - 7)
'        vNode            number of node (A - B)
'        uc               reference to the UserCode object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Sub GenerateSRNNodex_32(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, iNode As Integer, uc As UserCode)
    On Error GoTo GenerateSRNNodex_32Error

    Dim i As Integer               ' used as index for loops
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim n As String                ' used as temp variable for string operations

    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 sNSRx As String  ' Node x Status Register

    sVarPre = "CAN" & vIndex       ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers
    
    n = "SRN" & iSRN & "_NODE" & iNode

    sNCRx = sRegPre & "NCR" & iNode
    sNIPRx = sRegPre & "NIPR" & iNode
    sNPCRx = sRegPre & "NPCR" & iNode
    sNBTRx = sRegPre & "NBTR" & iNode
    sNECNTx = sRegPre & "NECNT" & iNode
    sNFCRx = sRegPre & "NFCR" & iNode
    sNSRx = sRegPre & "NSR" & iNode

    ' if ALIE or LECIE or TRIE
    If ((r.v(sNCRx & "_ALIE") = 1 And r.v(sNIPRx & "_ALINP") = iSRN) Or _
        (r.v(sNCRx & "_TRIE") = 1 And r.v(sNIPRx & "_TRINP") = iSRN) Or _
        (r.v(sNCRx & "_LECIE") = 1 And r.v(sNIPRx & "_LECINP") = iSRN)) Or _
        ((r.v(sNFCRx & "_CFCIE") = 1 And r.v(sNIPRx & "_CFCINP") = iSRN)) Then
            Print #f, ""
            Print #f, FormatSectionHeader(2, 0, "Interrupts of CAN node " & iNode)
            Print #f, ""
        If ((r.v(sNCRx & "_ALIE") = 1 And r.v(sNIPRx & "_ALINP") = iSRN) Or _
            (r.v(sNCRx & "_TRIE") = 1 And r.v(sNIPRx & "_TRINP") = iSRN) Or _
            (r.v(sNCRx & "_LECIE") = 1 And r.v(sNIPRx & "_LECINP") = iSRN)) Then
            Print #f, "  CAN_vWriteCANAddress(" & sNSRx & ");"
            Print #f, "  CAN_vReadEN();"
            Print #f, "  ubNSRL = CAN_DATA0;"
            Print #f, ""
        End If
        If ((r.v(sNCRx & "_ALIE") = 1 And r.v(sNIPRx & "_ALINP") = iSRN)) Then
            Print #f, "  ubNSRH = CAN_DATA1;"
        End If

            '###################################################################################
            '            Alert Interrupt Node Pointer Interrupt
            '###################################################################################
        If (r.v(sNCRx & "_ALIE") = 1 And r.v(sNIPRx & "_ALINP") = iSRN) Then
            Print #f, "    if (ubNSRL & 0x20)      // if ALERT"
            Print #f, "    {"
            Print #f, "        if (ubNSRL & 0x80)  // if BOFF"
            Print #f, "        {"
            Print #f, "          // Indicates when the CAN controller is in busoff state."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",1)", 8)
            Print #f, "        }"
            Print #f, ""
            Print #f, "        if (ubNSRL & 0x40)  // if EWRN"
            Print #f, "        {"
            Print #f, "          // One of the error counters REC or TEC reached the"
            Print #f, "          // warning limit of " & r.v(sNECNTx & "_EWRNLVL") & "."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",2)", 8)
            Print #f, "        }"
            Print #f, ""
            Print #f, "        if (ubNSRH & 0x01)  // if LLE"
            Print #f, "        {"
            Print #f, "           // A list length error has been detected during message"
            Print #f, "           // acceptance filtering. The number of elements in the list"
            Print #f, "           // that belongs to this CAN node differs from the list SIZE "
            Print #f, "           // given in the list termination pointer"
            Print #f, ""
            Print #f, "           // reset LLE"
            Print #f, "           CAN_DATA0   =  0x3F;         // load " & sNSRx & " status register[7-0]"
            Print #f, "           CAN_DATA1   =  0x02;         // load " & sNSRx & " status register[15-8]"
            Print #f, "           CAN_vWriteEN(D0_VALID+D1_VALID); // Data0 and Data1 are Valid for"
            Print #f, "                                        // transmission and Write is EnabledCAN."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",3)", 8)
            Print #f, "         }"
            Print #f, ""
            Print #f, "        if (ubNSRH & 0x02)  // if LOE"
            Print #f, "        {"
            Print #f, "           // A list object error has been detected during message"
            Print #f, "           // acceptance filtering. A message object with wrong LIST "
            Print #f, "           // index entry in the Message Object Control Register has "
            Print #f, "           // been detected"
            Print #f, ""
            Print #f, "           // reset LOE"
            Print #f, "           CAN_DATA0   =  0x3F;         // load " & sNSRx & " status register[7-0]"
            Print #f, "           CAN_DATA1   =  0x01;         // load " & sNSRx & " status register[15-8]"
            Print #f, "           CAN_vWriteEN(D0_VALID+D1_VALID); // Data0 and Data1 are Valid for"
            Print #f, "                                        // transmission and Write is EnabledCAN."
'            Print #f, "             while(CAN_ADCON & CAN_ADCON_BSY); // Wait till transmission is finished"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",4)", 8)
            Print #f, "         }"
            Print #f, ""
            Print #f, "         CAN_vWriteCANAddress(" & sNCRx & "); // Addressing" & sNCRx
            Print #f, "         CAN_vReadEN();"
            Print #f, "         if (CAN_DATA0 & 0x01)  // if INIT"
            Print #f, "         {"
            Print #f, "           // Bit INIT has been set by the MultiCAN"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",5)", 8)
            Print #f, "         }"
            Print #f, ""
            Print #f, "         CAN_vWriteCANAddress(" & sNSRx & "); // Addressing" & sNSRx
            Print #f, "           // reset ALERT"
            Print #f, "         CAN_DATA0   =  0x1F;         // load " & sNSRx & " status register[7-0]"
            Print #f, "         CAN_DATA1   =  0x03;         // load " & sNSRx & " status register[15-8]"
            Print #f, "         CAN_vWriteEN(D0_VALID+D1_VALID); // Data0 and Data1 are Valid for"
            Print #f, "                                      // transmission and Write is EnabledCAN."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",6)", 6)
            Print #f, "     }"  ' if ALERT
            Print #f, ""
        End If
        
    '###################################################################################
    '            Transfer OK Interrupt
    '###################################################################################
        
        If (r.v(sNCRx & "_TRIE") = 1 And r.v(sNIPRx & "_TRINP") = iSRN) Then
            Print #f, "  if (ubNSRL & 0x08)  // if TXOK"
            Print #f, "  {"
            Print #f, "      // Indicates that a message has been transmitted successfully"
            Print #f, "      // (error free and acknowledged by at least another node)."
            Print #f, ""
            Print #f, "      // reset TX0K"
            Print #f, "    CAN_DATA0   =  0x37;         // load " & sNSRx & " status register[7-0]"
            Print #f, "    CAN_DATA1   =  0x03;         // load " & sNSRx & " status register[15-8]"
            Print #f, "    CAN_vWriteEN(D0_VALID+D1_VALID); // Data0 and Data1 are Valid for"
            Print #f, "                                 // transmission and Write is Enabled."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",7)", 6)
            Print #f, "  }"
            Print #f, ""
            Print #f, "  if (ubNSRL & 0x10)  // if RXOK"
            Print #f, "  {"
            Print #f, "      // Indicates that a message has been received successfully."
            Print #f, ""
            Print #f, "     // reset RXOK"
            Print #f, "    CAN_DATA0   =  0x2F;         // load " & sNSRx & " status register[7-0]"
            Print #f, "    CAN_DATA1   =  0x03;         // load " & sNSRx & " status register[15-8]"
            Print #f, "    CAN_vWriteEN(D0_VALID+D1_VALID); // Data0 and Data1 are Valid for"
            Print #f, "                                 // transmission and Write is Enabled."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",8)", 6)
            Print #f, "  }"
            Print #f, ""
        End If
        
    '###################################################################################
    '            Last Error Code Interrupt
    '###################################################################################
        If (r.v(sNCRx & "_LECIE") = 1 And r.v(sNIPRx & "_LECINP") = iSRN) Then
            Print #f, FormatSectionHeader(2, 0, "CAN node " & iNode & " Last Error Code")
            Print #f, "  if (ubNSRL & 0x07)  // if LEC"
            Print #f, "  {"
            
              Call GenerateNODE_LECIntr_32(r, vIndex, f, iSRN, iNode, uc)
'            Print #f, uc.GetUserCode("(" & n & ",28)", 8)
            Print #f, "    CAN_vWriteCANAddress(" & sNSRx & "); // Addressing " & sNSRx
            Print #f, "     // reset LEC"
            Print #f, "    CAN_DATA0   =  0x38;         // load " & sNSRx & " status register[7-0]"
            Print #f, "    CAN_DATA1   =  0x03;         // load " & sNSRx & " status register[15-8]"
            Print #f, "    CAN_vWriteEN(D0_VALID+D1_VALID); // Data0 and Data1 are Valid for"
            Print #f, "                                 // transmission and Write is Enabled."
            Print #f, "  }"  ' if LEC
        End If
    End If
       
    '###################################################################################
    '            Frame Counter Overflow Interrupt
    '###################################################################################
      If ((r.v(sNFCRx & "_CFCIE") = 1 And r.v(sNIPRx & "_CFCINP") = iSRN)) Then
        Print #f, FormatSectionHeader(2, 0, "Interrupts of CAN node " & iNode & " Frame Counter Overflow Interrupt")
        Print #f, "    CAN_vWriteCANAddress(" & sNFCRx & ");"
        Print #f, "    CAN_vReadEN();"
        Print #f, "    ubNFCR = CAN_DATA2 ;"
        Print #f, "    if( ubNFCR & 0x80 )    // if frame counter overflow flag "
        Print #f, "    {"
        Print #f, uc.GetUserCode("(" & n & ", 38 )", 6)
        Print #f, ""
        Print #f, "      CAN_DATA2 = (ubNFCR & 0x7F); // reset overflow flag "
        Print #f, "      CAN_vWriteEN(D2_VALID);  // Data2 is valid for transmission and"
        Print #f, "                               // Write is Enabled."
        Print #f, "    }"
      End If
    Exit Sub
    
GenerateSRNNodex_32Error:
    Exit Sub
End Sub

Public Function GenerateNODE_LECIntr_32(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, iNode As Integer, uc As UserCode) As Variant
    On Error GoTo GenerateNODE_LECIntr_32Error
    
    Dim n As String                ' used as temp variable for string operations
    n = "SRN" & iSRN & "_NODE" & iNode
    
        Print #f, "      switch (ubNSRL & 0x07)  // LEC" & iNode & " (Last Error Code" & iNode & ")"
        Print #f, "      {"
        Print #f, "       case 1: // Stuff Error"
        Print #f, FormatCommentsForSource(15, 1, "More than 5 equal bits in a sequence have occurred" & _
                        "in a part of a received message where this is not allowed.")
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",9)", 15)
        Print #f, ""
        Print #f, "               break;"
        Print #f, ""
        Print #f, "       case 2: // Form Error"
        Print #f, FormatCommentsForSource(15, 1, "A fixed format part of a received frame has the wrong format.")
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",10)", 15)
        Print #f, ""
        Print #f, "               break;"
        Print #f, ""
        Print #f, "       case 3: // Ack Error"
        Print #f, FormatCommentsForSource(15, 1, "The message this CAN controller transmitted was" & _
                "not acknowledged by another node.")
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",11)", 15)
        Print #f, ""
        Print #f, "               break;"
        Print #f, ""
        Print #f, "       case 4: // Bit1 Error"
        Print #f, FormatCommentsForSource(15, 1, "During the transmission of a message (with the" & _
                " exeption of the arbitration field), the device wanted to send a recessive level (""1""), but the" & _
                " monitored bus value was dominant.")
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",15)", 15)
        Print #f, ""
        Print #f, "               break;"
        Print #f, ""
        Print #f, "       case 5: // Bit0 Error"
        Print #f, FormatCommentsForSource(15, 1, "During the transmission of a message (or acknow-" & _
                " ledge bit, active error flag, or overload flag), the device wanted to send a dominant level (""0"")," & _
                " but the monitored bus value was recessive.")
        Print #f, ""
        Print #f, FormatCommentsForSource(15, 1, "During busoff recovery this status is set each time " & _
                " a sequence of 11 recessive bits has been monitored. The CPU may use this code as indication that the " & _
                " bus is not continuously disturbed).")
        Print #f, ""
        Print #f, "               if (ubNSRL & 0x80)  // if Busoff status"
        Print #f, "               {"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",13)", 17)
        Print #f, ""
        Print #f, "               }"
        Print #f, "               else"
        Print #f, "               {"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",14)", 17)
        Print #f, ""
        Print #f, "               }"
        Print #f, "               break;"
        Print #f, ""
        Print #f, "       case 6: // CRC Error"
        Print #f, FormatCommentsForSource(15, 1, "The CRC check sum was incorrect in the message received.")
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",15)", 15)
        Print #f, ""
        Print #f, "               break;"
        Print #f, ""
        Print #f, "       default:"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",16)", 15)
        Print #f, ""
        Print #f, "               break;"
        Print #f, ""
        Print #f, "      }"
        Print #f, ""
            
GenerateNODE_LECIntr_32Error:
    GenerateNODE_LECIntr_32 = ""
End Function

'*****************************************************************************
'Description:
'        This functions generates the code and statements for the SRN CAN
'        message object x.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'        f                file handle
'        iSRN             number of service request node (0 - 7)
'        iObj             number of message object (0 - 31)
'        uc               reference to the UserCode object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Sub GenerateObjxCode_32(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, iObj As Integer, uc As UserCode)
    On Error GoTo GenerateObjxCode_32Error

    Dim i As Integer               ' used as index for loops
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim n As String                ' used as temp variable for string operations
    
    sVarPre = "CAN" & vIndex       ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers
    n = "SRN" & iSRN & "_OBJ" & iObj
      
    If ((r.v(sRegPre & "MOIPR" & iObj & "_RXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_RXIE") = 1) Or _
        (r.v(sRegPre & "MOIPR" & iObj & "_TXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_TXIE") = 1)) Then
            Print #f, "      case " & iObj Mod 32 & ": // message object " & iObj & " interrupt "
            Print #f, "      {"
            Print #f, ""
            Print #f, "         CAN_vWriteCANAddress(" & sRegPre & "MOCTR(ubMsgObjVal)); // Addressing CAN_MOCTR" & iObj
            Print #f, "         CAN_vReadEN();               // Read Mode is enabled"
            Print #f, "         ubTempVarObjHandler = CAN_DATA0;"
'            Print #f, "         ubRstVal = 0;"
            Print #f, ""
      ' if Receive interrupt for object iObj is enabled
      If (r.v(sRegPre & "MOIPR" & iObj & "_RXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_RXIE") = 1) Then
            Print #f, "         if( ubTempVarObjHandler & MOSTAT_RXPND)   // if message object " & iObj & " receive interrupt"
            Print #f, "         {"
            Print #f, ""
        ' if Receive Object => data frame received
        If (r.v(sRegPre & "MOCTR" & iObj & "_DIR") = 0) Then
            Print #f, "           if( ubTempVarObjHandler & MOSTAT_NEWDAT)   // if NEWDAT is set"
            Print #f, "           {"
            Print #f, ""
            Print #f, "             if( ubTempVarObjHandler & MOSTAT_MSGLST)  // if MSGLST is set"
            Print #f, "             {"
            Print #f, "                // Indicates that the CAN controller has stored a new "
            Print #f, "                // message into this object, while NEWDAT was still set,"
            Print #f, "                // ie. the previously stored message is lost."
            Print #f, ""
'            Print #f, "                ubRstVal = MOSTAT_MSGLST; // reset MSGLST"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",1)", 16)
            Print #f, "             }"
            Print #f, "             else"
            Print #f, "             {"
            Print #f, "                // The CAN controller has stored a new message"
            Print #f, "                // into this object."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",2)", 16)
            Print #f, "             }"
            Print #f, ""
'            Print #f, "               ubRstVal |= MOSTAT_NEWDAT; // reset NEWDAT"
            Print #f, "           }"
        End If
        ' if Transmit Object => Remote frame received
        If (r.v(sRegPre & "MOCTR" & iObj & "_DIR") = 1) Then
            Print #f, "          // The CAN controller has received a remote frame"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",4)", 10)
        End If
            Print #f, ""
            Print #f, "           // reset RXPND"
'            Print #f, "           CAN_DATA0 = (ubRstVal |= MOSTAT_RXPND); // load CAN Data Register 0"
            Print #f, "           CAN_DATA1 = 0x00; // load CAN Data Register 1"
            Print #f, "           CAN_DATA2 = 0x00; // load CAN Data Register 2"
            Print #f, "           CAN_DATA3 = 0x00; // load CAN Data Register 3"
            Print #f, "             CAN_vWriteEN(); // Writemode is Enabled"
            Print #f, "        }  // End of RXPND" & iObj
            Print #f, ""
      End If
      
      ' if Transmit interrupt for object iObj is enabled
      If (r.v(sRegPre & "MOIPR" & iObj & "_TXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_TXIE") = 1) Then
            Print #f, "       if( ubTempVarObjHandler & MOSTAT_TXPND)         // if message object " & iObj & " transmit interrupt"
            Print #f, "        {"
            Print #f, ""
            ' if Transmit Object => data frame transmitted
            If (r.v(sRegPre & "MOCTR" & iObj & "_DIR") = 1) Then
                Print #f, "          // The transmission of the last message object"
                Print #f, "          // was successful."
                Print #f, ""
                Print #f, uc.GetUserCode("(" & n & ",5)", 8)
                Print #f, ""
            End If
            ' if Receive Object => remote frame transmitted
            If (r.v(sRegPre & "MOCTR" & iObj & "_DIR") = 0) Then
                Print #f, "          // The CAN controller has send a remote frame"
                Print #f, "          // with identifier and control of this object."
                Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",6)", 10)
                Print #f, ""
            End If
            If (r.v(sRegPre & "MOCTR" & iObj & "_DIR") = 1) Then
                Print #f, "           // reset TXPND, NEWDAT"
                Print #f, "           CAN_DATA0 = (MOSTAT_TXPND + MOSTAT_NEWDAT); // load CAN Data Register 0"
            Else
                Print #f, "           // reset TXPND"
                Print #f, "           CAN_DATA0 = MOSTAT_TXPND; // load CAN Data Register 0"
            End If
            Print #f, "           CAN_DATA1 = 0x00; // load CAN Data Register 1"
            Print #f, "           CAN_DATA2 = 0x00; // load CAN Data Register 2"
            Print #f, "           CAN_DATA3 = 0x00; // load CAN Data Register 3"
            Print #f, "               CAN_vWriteEN(); // Writemode is Enabled"
            Print #f, ""
            Print #f, "        }  // End of TXPND" & iObj
      End If
            Print #f, ""
            Print #f, "      CAN_vWriteCANAddress(" & sRegPre & "MSPND" & iObj \ 32 & "); // Addressing " & sRegPre & "MSPND" & iObj \ 32
            Print #f, "      CAN_vReadEN();               // Read Mode is enabled"
            Print #f, ""
            Print #f, FormatCodeStatemente(6, "CAN_DATA0", "&= ~", "0x" & Format8BitStatement((2 ^ (iObj Mod 32)), 3), "")
            Print #f, FormatCodeStatemente(6, "CAN_DATA1", "&= ~", "0x" & Format8BitStatement((2 ^ (iObj Mod 32)), 2), "")
            Print #f, FormatCodeStatemente(6, "CAN_DATA2", "&= ~", "0x" & Format8BitStatement((2 ^ (iObj Mod 32)), 1), "")
            Print #f, FormatCodeStatemente(6, "CAN_DATA3", "&= ~", "0x" & Format8BitStatement((2 ^ (iObj Mod 32)), 0), "")
            Print #f, ""
            Print #f, "      CAN_vWriteEN(); // Write mode Enabled"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",7)", 6)
            Print #f, "      }"
            Print #f, ""
            Print #f, "      break;"
    End If
    Exit Sub
    
GenerateObjxCode_32Error:
    Exit Sub
End Sub



Sub GenerateMOIntrObjx_32(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, uc As UserCode, n As String)
    On Error GoTo GenerateMOIntrObjx_32Error
    
    Dim m As Integer               ' used as index for loops
    Dim vRxMask(0 To 7) As Variant
    Dim vTxMask(0 To 7) As Variant
    Dim vMsgPndMask As Long
    Dim iObj As Integer
    Dim i As Integer               ' used as index for loops
    Dim j As Integer               ' used as index for loops
    Dim iNr As Integer
    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

    For m = 0 To ((CAN_MAX_OBJECTS \ 32) - 1) ' 7 ' for each Message Pending register
      vRxMask(m) = 0
      vTxMask(m) = 0
      For j = 0 To 31
        iNr = m * 32 + j
        If ((r.v(sRegPre & "MOIPR" & iNr & "_RXINP") = iSRN And r.v(sRegPre & "MOFCR" & iNr & "_RXIE") = 1) Or _
            (r.v(sRegPre & "MOIPR" & iNr & "_TXINP") = iSRN And r.v(sRegPre & "MOFCR" & iNr & "_TXIE") = 1)) Then
            vRxMask(m) = vRxMask(m) + 2 ^ j
        End If
      Next j
      vTxMask(m) = vRxMask(m)

      If (Not (vRxMask(m) = 0)) Then
        Print #f, ""
        Print #f, FormatSectionHeader(2, 0, "Interrupts of CAN Msg Obj x ")
        Print #f, ""
        Print #f, "    CAN_vWriteCANAddress(CAN_MSIMASK);    // set message index mask register"
        Print #f, FormatCodeStatemente(4, "CAN_DATA0", "= ", "0x" & Format8BitStatement((vRxMask(m)), 3), "set message index mask register")
        Print #f, FormatCodeStatemente(4, "CAN_DATA1", "= ", "0x" & Format8BitStatement((vRxMask(m)), 2), "set message index mask register")
        Print #f, FormatCodeStatemente(4, "CAN_DATA2", "= ", "0x" & Format8BitStatement((vRxMask(m)), 1), "set message index mask register")
        Print #f, FormatCodeStatemente(4, "CAN_DATA3", "= ", "0x" & Format8BitStatement((vRxMask(m)), 0), "set message index mask register")
        Print #f, ""
        Print #f, "    CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled"
        Print #f, ""
        Print #f, "    CAN_vWriteCANAddress(" & sRegPre & "MSID" & m & ");   // message index register"
        Print #f, "    CAN_vReadEN();               // Read Mode is enabled"
        Print #f, ""
        Print #f, "    ubTempMsgID = CAN_DATA0;"
        Print #f, ""
        Print #f, "    while(ubTempMsgID != 0x20)"
        Print #f, "    {"
'        Print #f, "       ubRstVal = 0;"
        Print #f, "       CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn"
        Print #f, "       CAN_vReadEN();               // Read Mode is enabled"
        Print #f, "       ubTempVarObjHandler = CAN_DATA0;"

        If (r.v("CAN_EN_ISRCODE") = 1) Then
            For iObj = 0 To (CAN_MAX_OBJECTS - 1)
              If ((r.v(sRegPre & "MOIPR" & iObj & "_RXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_RXIE") = 1) Or _
                  (r.v(sRegPre & "MOIPR" & iObj & "_TXINP") = iSRN And r.v(sRegPre & "MOFCR" & iObj & "_TXIE") = 1)) Then
                      Print #f, ""
                      Call GenerateObjxCode_32(r, vIndex, f, iSRN, iObj, uc)
                      Print #f, ""
              End If
            Next iObj
        End If
             
        Print #f, ""
        Print #f, "       ulBit_Pos_Mask.ulVal = ~(1 << ubTempMsgID);"
        Print #f, "       CAN_vWriteCANAddress(CAN_MSPND0); // Addressing CAN_MSPND0"
        Print #f, ""
        Print #f, "       CAN_DATA0 = ulBit_Pos_Mask.ubDB[3]; // set message pending register"
        Print #f, "       CAN_DATA1 = ulBit_Pos_Mask.ubDB[2]; // set message pending register"
        Print #f, "       CAN_DATA2 = ulBit_Pos_Mask.ubDB[1]; // set message pending register"
        Print #f, "       CAN_DATA3 = ulBit_Pos_Mask.ubDB[0]; // set message pending register"
        Print #f, "       CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled"
        Print #f, ""
        Print #f, "       CAN_vWriteCANAddress(" & sRegPre & "MSID0);   // message index register"
        Print #f, "       CAN_vReadEN();               // Read Mode is enabled"
        Print #f, "       ubTempMsgID = CAN_DATA0;"
        Print #f, ""
        Print #f, "    }  // end of while " & sRegPre & "MSID"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & "," & 7 + m & ")", 4)
        Print #f, ""
      End If
    Next m

GenerateMOIntrObjx_32Error:
    Exit Sub
End Sub
