'#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
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'CAN'.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0  ste            25.05.00    final version for TC1775A
'        1.1  ste            20.07.00    the TC1775B has a new Loop-Back mode;
'                                        new clock divider for each node;
'                                        the frame counter can be incremenzed by 
'                                        additional events (new bitfield for modes);
'                                        error warning threshold added;
'        1.2  ste            02.11.00    the TC1775B has a new Single Transmission
'                                        Try mode; new FIFO direction (FD) bit;
'                                        new FIFO and Gateway handling 
'
'        ---------------------------------------------------------------------
'        3.0  ahe            05.11.01    final version for Pegasus
'
'        3.1  ahe            02.07.02    - changed "RXIPNDH << 16) && " to "RXIPNDH << 16) + "
'                                        - correct set of MSGCFGH bits
'
'        3.2  ste            05.03.03    - "RXIPNDH << 16) + " in ISR added (string operation 
'                                          was wrong)
'                                        - Defines for Interrupt Vectors changed (gap in vectors)  
'                                        - position of _RXINP and _TXINP changed 
'        ---------------------------------------------------------------------
'             ste            01.10.03    cast to (ulong) for RXIPNDH and TXIPNDH
'                                        in ISR added
'                                        CAN_MSGCTR at the end of the MO initialization
'
'*****************************************************************************


'*****************************************************************************
'Description:
'        This functions initiates the code generation for the CAN module.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function StartGenerateCode(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo StartGenerateCodeError
    StartGenerateCode = False

    Dim sVarPre As String
    sVarPre = "CAN"  & vIndex

    ' write file CAN.h
    If (r.v(sVarPre & "_USE_Init")) Then
      If (Not CANGenerateH(r, vIndex)) Then Exit Function
      If (Not r.AddFile(r.s(sVarPre & "_HFILE"), "H")) Then Exit Function
    End if

    ' write file CAN.c
    If (r.v(sVarPre & "_USE_Init")) Then
      If (Not CANGenerateC(r, vIndex)) Then Exit Function
      If (Not r.AddFile(r.s(sVarPre & "_CFILE"), "C")) Then Exit Function
    End if

        
    StartGenerateCode = True
    Exit Function
    
StartGenerateCodeError:
    Errorbox("Code generation for " & sVarPre & " failed", Error$)
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the CAN.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function CANGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo CANGenerateCError

    CANGenerateC = False
    
    Dim i As Integer               ' used as index for loops
    Dim ii As Integer               ' used as index for loops
    Dim j As Integer               ' used as index for loops
    Dim f As Variant               ' used for file operations
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim n As String                ' used as temp variable for function names
    Dim dc As DAvECollection       ' used as temp variable for Collections
    Dim uc As UserCode             ' used for UserCode objects
    Dim func As FunctionClass      ' used for FunctionClass objects
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim str1 As String             ' used as temp variable for string operations
    Dim str2 As String             ' used as temp variable for string operations
    Dim sAlternatePinName(1 To 16) As String
    Dim Baudrate As UnitType
'    Dim vMaskA As Variant
'    Dim vMaskB As Variant
    Dim vMask4A As Variant
    Dim vMask4B As Variant
    Dim vRxMask As Variant
    Dim vTxMask As Variant
    Dim s As String
    Dim iPin As Integer

    Dim l As Long
    Dim sOldSfr As String
    Dim sOldBlock As String
    Dim iHelp As Integer
    Dim sHelp As String

    Dim lBitCount As Long
    Dim szBitName As String
    Dim szSfrAddress As String
    Dim szSfrName As String
    Dim iBitPos As Integer
    Dim szBitSfrName As String
    Dim iBitWidth As Integer

    Const MAX_CHARACTER_BITNAME = 25
    Const MAX_CHARACTER_SFRNAME = 23



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

    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New  FunctionClass

    'refresh the decriptions of the functions and macros  
    RefreshFunctionDescriptions(vIndex, func, r)

    'read usercode
    uc.ReadFile((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_CFILE"))

    'open CAN.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
          
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,1)",0)    'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v(sVarPre & "_Notes")))
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, "#include """ & r.v("MAIN_HFile") & """"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,2)",0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,3)",0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, "// Structure for a single TwinCAN object"
            Print #f, "// A total of 31 such object structures exists"
            Print #f, ""
            Print #f, "struct stCanObj "
            Print #f, "{"
            Print #f, "  ubyte  ubData[8];  // Message Data 0..7"
            Print #f, "  ulong  ulCANAR;    // Arbitration Register"
            Print #f, "  ulong  ulCANAMR;   // Acceptance Mask Register"
            Print #f, "  uword  uwMSGCTR;   // Message Control Register"
            Print #f, "  uword  uwCounter;  // Frame Counter"
            Print #f, "  uword  uwMSGCFG;   // Message Configuration Register"
            Print #f, "  uword  uwINP;      // Interrupt Node Pointer"
            Print #f, "  uword  uwCANFCR;   // FIFO / Gateway Control Register "
            Print #f, "  uword  uwCANPTR;   // FIFO Pointer"
            Print #f, "  ulong  ulReserved; // Reserved"
            Print #f, "};"
            Print #f, ""
    If (r.Regs.SfrFind(sRegPre & "MSGDRL00")) Then
            Print #f, "#define CAN_HWOBJ ((struct stCanObj volatile far *) " & r.Regs.SfrAdress & ")"
    End If
            Print #f, ""
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,4)",0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,5)",0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
            Print #f, ""  'DAvE external declarations of imported global variables'
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,6)",0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
    If (r.v(sVarPre & "_USE_ReadFIFO") = 1 Or r.v(sVarPre & "_USE_FlushReadFIFO") = 1 Or r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
            Print #f, "static ubyte aubFIFOWritePtr[16];"
            Print #f, "static ubyte aubFIFOReadPtr[16];"
            Print #f, ""
    End If


            Print #f, ""
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,7)",0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,8)",0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,9)",0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vInit function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Init"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      If (r.v(sVarPre & "_USE_ReadFIFO") = 1 Or r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
        Print #f, "ubyte i;"
      End If
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""

      '###################################################################################
      '            CAN Node A and B configuration
      '###################################################################################
      str1 = "A"
      For i = 0 To 1
        '------------------------------------------------------------------------------------------------
        ' Configuration of CAN Node x:
        '------------------------------------------------------------------------------------------------
        Print #f, FormatSectionHeader(2, 1, "Configuration of CAN Node " & str1 & ":")
        Print #f, ""
        '###################################################################################
        '            CAN Node x general configuration
        '###################################################################################
        d = "General Configuration of the Node " & str1 & ":"
        Print #f, FormatCommentsForSource(2, 1, d)    

        d = "set INIT and CCE"
        Print #f, FormatCommentsForInit(1, d)    
        
        If (r.v(sRegPre & str1 & "CR_CALM") = 1) Then
          d = "enable the CAN analyzer mode; monitoring CAN bus traffic without participating in the CAN bus protocol itself"
          Print #f, FormatCommentsForInit(1, d)    
        End If
 
        ' the Loop-Back works only if both LBM bits (A and B) are set
        If (r.v(sRegPre & str1 & "BTRH_LBM") = 1) Then
          d = "enable the CAN Loop-Back mode"
          Print #f, FormatCommentsForInit(1, d)    
        End If

        If (r.v(sRegPre & str1 & "CR_SIE") = 1) Then
          d = "enable interrupt generation when a message transfer is completed"
          Print #f, FormatCommentsForInit(1, d)    
          d = "transmit / receive OK interrupt node pointer: TwinCAN SRN " & r.v(sRegPre & str1 & "GINP_TRINP")
          Print #f, FormatCommentsForInit(1, d)    
        End If
        If (r.v(sRegPre & str1 & "CR_EIE") = 1) Then
          d = "enable interrupt generation on a change of bit BOFF or EWARN"
          Print #f, FormatCommentsForInit(1, d)    
          d = "error (BusOff and ErrorWarning) interrupt node pointer: TwinCAN SRN " & r.v(sRegPre & str1 & "GINP_EINP")
          Print #f, FormatCommentsForInit(1, d)    
        End If
        If (r.v(sRegPre & str1 & "CR_LECIE") = 1) Then
          d = "enable interrupt generation on setting an error code in bit field LEC"
          Print #f, FormatCommentsForInit(1, d)    
          d = "last error code interrupt node pointer: TwinCAN SRN " & r.v(sRegPre & str1 & "GINP_LECINP")
          Print #f, FormatCommentsForInit(1, d)    
        End If
            
        ' statement for CANxCR Control register
        v = 0
        v = v + r.v(sRegPre & str1 & "CR_CALM") * 2 ^ 7
        v = v + 2 ^ 6   ' CCE allows access to Bit Timing Register
        v = v + r.v(sRegPre & str1 & "CR_LECIE") * 2 ^ 4
        v = v + r.v(sRegPre & str1 & "CR_EIE") * 2 ^ 3
        v = v + r.v(sRegPre & str1 & "CR_SIE") * 2 ^ 2
        v = v + 1       ' INIT starts the initialization
        Print #f, ""
        Print #f, FormatCodeStatemente(2, sRegPre & str1 & "CR", "=", _
                  FormatTextOutput(v, "H4"), "load global control register")
        
        If (r.v(sRegPre & str1 & "CR_INIT") = 0) Then  ' only if the CAN x module is used
          ' statement for CANxGINP register
          v = r.sfr(sRegPre & str1 & "GINP")
          Print #f, FormatCodeStatemente(2, sRegPre & str1 & "GINP", "=", _
                    FormatTextOutput(v, "H4"), "load global interrupt node pointer register")
          Print #f, ""

          ' Error Counter
          d = "Configuration of the Node " & str1 & " Error Counter:"
          Print #f, FormatCommentsForSource(2, 1, d)    

          d = "the error warning threshold value (warning level) is " & _
              Format(r.v(sRegPre & str1 & "ECNTH_EWRNLVL"))               
          Print #f, FormatCommentsForInit(1, d)    

          Print #f, ""
          ' statement for CAN_xECNT register
          v = 0
          v = r.sfr(sRegPre & str1 & "ECNTH")
          Print #f, FormatCodeStatemente(2, sRegPre & str1 & "ECNTH", "=", _
                    FormatTextOutput(v, "H4"), "load error counter register high")
          Print #f, ""

            
          '###################################################################################
          '            CAN Node x Ports
          '###################################################################################
          d = "Configuration of the used " & sVarPre & " Port Pins:"
          Print #f, FormatCommentsForSource(2, 1, d)    

          ii = 1
          For iPin = 0 To 3
              s = sVarPre & "_RxD" & str1 & "_" & iPin
              If (r.Exist("MuxFor_" & s) = True) Then
                  sAlternatePinName(ii) = s
                  ii = ii + 1
              End If
              s = sVarPre & "_TxD" & str1 & "_" & iPin
              If (r.Exist("MuxFor_" & s) = True) Then
                  sAlternatePinName(ii) = s
                  ii = ii + 1
              End If
          Next iPin

          Call SetAlternatePinStatements(f, r, sAlternatePinName())
          Print #f, ""                  
                    
          '###################################################################################
          '            CAN Node x Baudrate
          '###################################################################################
          d = "Configuration of the Node " & str1 & " Baud Rate:"
          Print #f, FormatCommentsForSource(2, 1, d)    

          Baudrate = ConvertBaudrate(r.d(sVarPre & "_BaudrateNode" & str1))
          d = "required baud rate = " & Format(Baudrate.dValue, CAN_PRECISION) & " " & Baudrate.sUnit
          Print #f, FormatCommentsForInit(1, d)    

          Baudrate = ConvertBaudrate(GetNodexClock(r, vIndex, str1) * 1000000 / _
                     ( 2^(1 - 1) * (r.v(sRegPre & str1 & "BTRL_BRP") + 1) * _
                     (3 + r.d(sRegPre & str1 & "BTRL_TSEG1") + _
                     r.d(sRegPre & str1 & "BTRL_TSEG2")) ) )

          d = "real baud rate     = " & Format(Baudrate.dValue, CAN_PRECISION) & " " & Baudrate.sUnit
          Print #f, FormatCommentsForInit(1, d)    
          d = "sample point       = " & Format((r.v(sRegPre & str1 & "BTRL_TSEG1") + 1 + 1) / _
              (r.v(sRegPre & str1 & "BTRL_TSEG1") + 1 + _
              r.v(sRegPre & str1 & "BTRL_TSEG2") + 1 + 1) * 100, "0.00") & " %"            
          Print #f, FormatCommentsForInit(1, d)    
          d = "there are " & Format(r.v(sRegPre & str1 & "BTRL_TSEG1") + 1) & _
              " time quanta before sample point"               
          Print #f, FormatCommentsForInit(1, d)    
          d = "there are " & Format(r.v(sRegPre & str1 & "BTRL_TSEG2") + 1) & _
              " time quanta after sample point"                                                                                                                 
          Print #f, FormatCommentsForInit(1, d)    
          d = "the (re)synchronization jump width is " & _
              Format(r.v(sRegPre & str1 & "BTRL_SJW") + 1) & " time quanta"                                                                                                          
          Print #f, FormatCommentsForInit(1, d)    
          If (r.v(sRegPre & str1 & "BTRL_DIV8X") = 1) Then
            d = "enable the module clock divider (by 8)"
            Print #f, FormatCommentsForInit(1, d)    
          End If

          Print #f, ""
          ' statement for CAN_xBTRL Bit Timing register
          v = 0
          v = v + r.v(sRegPre & str1 & "BTRL_DIV8X") * 2 ^ 15
          v = v + r.v(sRegPre & str1 & "BTRL_TSEG2") * 2 ^ 12
          v = v + r.v(sRegPre & str1 & "BTRL_TSEG1") * 2 ^ 8
          v = v + r.v(sRegPre & str1 & "BTRL_SJW") * 2 ^ 6
          v = v + r.v(sRegPre & str1 & "BTRL_BRP")
          Print #f, FormatCodeStatemente(2, sRegPre & str1 & "BTRL", "=", _
                    FormatTextOutput(v, "H4"), "load bit timing register low")
          Print #f, ""
                
          ' statement for CAN_xBTRH Bit Timing register
          v = 0
          v = v + r.v(sRegPre & str1 & "BTRH_LBM")
          Print #f, FormatCodeStatemente(2, sRegPre & str1 & "BTRH", "=", _
                    FormatTextOutput(v, "H4"), "load bit timing register high")
          Print #f, ""

          '###################################################################################
          '            CAN Node x Frame Counter
          '###################################################################################
          If (r.v(sRegPre & str1 & "FCRH_CFCMD") > 0) Then
            d = "Configuration of the Frame Counter:"
            Print #f, FormatCommentsForSource(2, 1, d)    
            If ((r.v(sRegPre & str1 & "FCRH_CFCMD") And 12) = 4) Then
                d = "the counter is incremented each time a frame was transmitted correctly "
                Print #f, FormatCommentsForInit(1, d)    
            End If
            If ((r.v(sRegPre & str1 & "FCRH_CFCMD") And 10) = 2) Then
                d = "the counter is incremented each time a frame was received correctly"
                Print #f, FormatCommentsForInit(1, d)    
            End If
            If ((r.v(sRegPre & str1 & "FCRH_CFCMD") And 9) = 1) Then
                d = "the counter is incremented each time a foreign frame was transfered correctly on the CAN bus "
                Print #f, FormatCommentsForInit(1, d)    
            End If
            If (r.v(sRegPre & str1 & "FCRH_CFCMD") = 8) Then
                d = "the counter is incremented with the beginning of a new bit time; the value is sampled during the SOF bit "
                Print #f, FormatCommentsForInit(1, d)    
            End If
            If (r.v(sRegPre & str1 & "FCRH_CFCMD") = 9) Then
                d = "the counter is incremented with the beginning of a new bit time; the value is sampled during the last bit of EOF "
                Print #f, FormatCommentsForInit(1, d)    
            End If

            If (r.v(sRegPre & str1 & "FCRH_CFCIE") = 1) Then
                d = "enable the frame counter overflow interrupt"
                Print #f, FormatCommentsForInit(1, d)    
                d = "frame counter interrupt node pointer: TwinCAN SRN " & _
                    r.v(sRegPre & str1 & "GINP_CFCINP")
                Print #f, FormatCommentsForInit(1, d)    
            End If
            d = "frame counter: " & FormatTextOutput(r.v(sRegPre & str1 & "FCRL_CFC"), "H4")
            Print #f, FormatCommentsForInit(1, d)    
            Print #f, ""
          End If
          ' statement for Frame Counter register
          v = r.sfr(sRegPre & str1 & "FCRL")           
          Print #f, FormatCodeStatemente(2, sRegPre & str1 & "FCRL", "=", _
                    FormatTextOutput(v, "H4"), "load frame counter timing register low")
          Print #f, ""
          v = r.sfr(sRegPre & str1 & "FCRH")           
          Print #f, FormatCodeStatemente(2, sRegPre & str1 & "FCRH", "=", _
                    FormatTextOutput(v, "H4"), "load frame counter timing register high")
          Print #f, ""
        Else
          Print #f, ""
        End If  ' only if the CAN module is used
        str1 = "B"
      Next i

      '###################################################################################
      '            Configuration of the CAN Message Objects 0 - 31
      '###################################################################################
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Message Objects 0 - 31:")
        Print #f, ""
      For i = 0 To 31
        If (r.v(sRegPre & "MSGCTRL" & i & "_MSGVAL") > 1) Then
            Call GenerateObjx(r, vIndex, f, i)
        End If
      Next i
        Print #f, ""
      
      '###################################################################################
      '            TwinCAN SRN 0 - 7  configuration
      '###################################################################################
      Print #f, FormatSectionHeader(2, 1, "Configuration of Service Request Nodes 0 - 7:")

      ' SRNx interrupt
      For i = 0 To 7
        Call GenerateIR(f, r, sRegPre & i & "IC", "SRN" & i)
      Next i
        Print #f, ""
    
    '###################################################################################
    '            Initialization of the FIFO
    '###################################################################################
      If (r.v(sVarPre & "_USE_ReadFIFO") = 1 Or r.v(sVarPre & "_USE_WriteFIFO") = 1) Then                                                         
      Print #f, FormatSectionHeader(2, 1, "Initialization of the FIFO Pointer:")
             Print #f, ""                                                                                                                  
             Print #f, "  for (i = 0; i < 16; i++)"                                                                                        
             Print #f, "  {"                                                                                                               
        If (r.v(sVarPre & "_USE_WriteFIFO") = 1) Then                                                         
             Print #f, "    aubFIFOWritePtr[i] = i * 2;"
        End If
        If (r.v(sVarPre & "_USE_ReadFIFO") = 1) Then                                                         
             Print #f, "    aubFIFOReadPtr[i]  = i * 2;"
        End If
             Print #f, "  }"              
             Print #f, ""                                                                                                                  
      End If                                                                                                                                
                                                                                                                                           
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",3)",2) 
      Print #f, ""

      Print #f, ""
      v = r.sfr(sRegPre & "PISEL")
      Print #f, FormatCodeStatemente(2, sRegPre & "PISEL", "=", _
                FormatTextOutput(v, "H4"), "load port input select register")
      Print #f, ""


      '###################################################################################
      '            Start the Nodes
      '###################################################################################
      Print #f, FormatSectionHeader(2, 0, "Start the " & sVarPre & " Nodes:")
      ' statement for CAN_xCR Control register                                                                                               

      Print #f, ""
      v = 0                                                                                                                                 
      v = v + 2 ^ 6   ' CCE allows access to Bit Timing Register                                                                            
      v = v + 1       ' INIT starts the initialization                                                                                      
      If (r.v(sRegPre & "ACR_INIT") = 0) Then  ' only if the CAN A module is used                                                              
        Print #f, FormatCodeStatemente(2, sRegPre & "ACR", "&=", "~(uword)" & FormatTextOutput(v, "H4"), "reset INIT and CCE")
      End If                                                                                                                                
      If (r.v(sRegPre & "BCR_INIT") = 0) Then  ' only if the CAN B module is used                                                              
        Print #f, FormatCodeStatemente(2, sRegPre & "BCR", "&=", "~(uword)" & FormatTextOutput(v, "H4"), "reset INIT and CCE")
      End If                                                                                                                                
            
      Print #f, ""
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",4)",2) 
      Print #f, ""
      Print #f, "} " & END_OF_FUNCTION & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vGetMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte i;"
      Print #f, ""
      Print #f, "  for(i = 0; i < (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x00f0) >> 4; i++)"
      Print #f, "  {"
      Print #f, "    pstObj->ubData[i] = CAN_HWOBJ[ubObjNr].ubData[i];"
      Print #f, "  }"
      Print #f, ""
      Print #f, "  if(CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x04)  // extended identifier"
      Print #f, "  {"
      Print #f, "    pstObj->ulID   = CAN_HWOBJ[ubObjNr].ulCANAR;"
      Print #f, "    pstObj->ulMask = CAN_HWOBJ[ubObjNr].ulCANAMR;"
      Print #f, "  }"
      Print #f, "  else                                    // standard identifier "
      Print #f, "  {"
      Print #f, "    pstObj->ulID   = CAN_HWOBJ[ubObjNr].ulCANAR >> 18;"
      Print #f, "    pstObj->ulMask = CAN_HWOBJ[ubObjNr].ulCANAMR >> 18;"
      Print #f, "  }"
      Print #f, ""
      Print #f, "  pstObj->uwCounter = CAN_HWOBJ[ubObjNr].uwCounter;"
      Print #f, "  pstObj->uwMsgCfg  = CAN_HWOBJ[ubObjNr].uwMSGCFG;"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubRequestMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "RequestMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte ubReturn;"
      Print #f, ""
      Print #f, "  ubReturn = 0;"
      Print #f, "  if((CAN_HWOBJ[ubObjNr].uwMSGCTR & 0x3000) == 0x1000)  // if reset TXRQ "
      Print #f, "  {"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfbff;               // set CPUUPD "
      Print #f, "    ubReturn = 1;"
      Print #f, "  }"
      Print #f, "  return(ubReturn);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_bNewData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "NewData"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte ubReturn;"
      Print #f, ""
      Print #f, "  ubReturn = 0;"
      Print #f, "  if((CAN_HWOBJ[ubObjNr].uwMSGCTR & 0x0300) == 0x0200)  // if NEWDAT"
      Print #f, "  {"
      Print #f, "    ubReturn = 1;"
      Print #f, "  }"
      Print #f, "  return(ubReturn);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
       
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vTransmit function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Transmit"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xe7ff;  // set TXRQ, reset CPUUPD"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vConfigMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ConfigMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte i;"
      Print #f, ""
      Print #f, "  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfb7f;     // set CPUUPD, reset MSGVAL"
      Print #f, ""
      Print #f, "  if(pstObj->uwMsgCfg & 0x0004)             // extended identifier"
      Print #f, "  {"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCFG |= 0x0004;"
      Print #f, "    CAN_HWOBJ[ubObjNr].ulCANAR   = pstObj->ulID ;"
      Print #f, "    CAN_HWOBJ[ubObjNr].ulCANAMR  = pstObj->ulMask ;"
      Print #f, "  }"
      Print #f, "  else                                      // standard identifier"
      Print #f, "  {"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCFG &= ~(uword)0x0004;"
      Print #f, "    CAN_HWOBJ[ubObjNr].ulCANAR   = pstObj->ulID << 18;"
      Print #f, "    CAN_HWOBJ[ubObjNr].ulCANAMR  = pstObj->ulMask << 18;"
      Print #f, "  }"
      Print #f, ""
      Print #f, "  CAN_HWOBJ[ubObjNr].uwCounter = pstObj->uwCounter;"
      Print #f, "" ' DIR, NODE, RMM can not be changed
      Print #f, "  CAN_HWOBJ[ubObjNr].uwMSGCFG  = (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x000f) | (pstObj->uwMsgCfg & 0x00f0);"
      Print #f, ""
      Print #f, "  if(CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x0008)  // if transmit direction"
      Print #f, "  {"
      Print #f, "    for(i = 0; i < (pstObj->uwMsgCfg & 0x00f0) >> 4; i++)"
      Print #f, "    {"
      Print #f, "      CAN_HWOBJ[ubObjNr].ubData[i] = pstObj->ubData[i];"
      Print #f, "    }"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCTR  = 0xf6bf;  // set NEWDAT, reset CPUUPD, "
      Print #f, "  }                                         // set MSGVAL"
      Print #f, "  else                                      // if receive direction"
      Print #f, "  {"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCTR  = 0xf7bf;  // reset CPUUPD, set MSGVAL"
      Print #f, "  }"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vLoadData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "LoadData"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte i;"
      Print #f, ""
      Print #f, "  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfaff;       // set CPUUPD and NEWDAT"
      Print #f, ""
      Print #f, "  for(i = 0; i < (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0xf0) >> 4; i++)"
      Print #f, "  {"
      Print #f, "    CAN_HWOBJ[ubObjNr].ubData[i] = *(pubData++);"
      Print #f, "  }"
      Print #f, ""
      Print #f, "  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xf7ff;       // reset CPUUPD"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_bMsgLost function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "MsgLost"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte ubReturn;"
      Print #f, ""
      Print #f, "  ubReturn = 0;"
      Print #f, "  if((CAN_HWOBJ[ubObjNr].uwMSGCTR & 0x0c00) == 0x0800)  // if set MSGLST "
      Print #f, "  {"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xf7ff;               // reset MSGLST "
      Print #f, "    ubReturn = 1;"
      Print #f, "  }"
      Print #f, "  return(ubReturn);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_bDelMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "DelMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte ubReturn;"
      Print #f, ""
      Print #f, "  ubReturn = 0;"
      Print #f, "  if(!(CAN_HWOBJ[ubObjNr].uwMSGCTR & 0xa200)) // if set RMTPND, TXRQ or NEWDAT"
      Print #f, "  {"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xff7f;     // reset MSGVAL"
      Print #f, "    ubReturn = 1;"
      Print #f, "  }"
      Print #f, "  return(ubReturn);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
   
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vReleaseObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReleaseObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfdff;       // reset NEWDAT"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vSetMSGVAL function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetMSGVAL"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xffbf;  // set MSGVAL"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubWriteFIFO function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "WriteFIFO"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte i,j;"
      Print #f, "  ubyte ubReturn;"
      Print #f, ""
      Print #f, "  ubReturn = 2;"
      Print #f, ""
      Print #f, "  if((CAN_HWOBJ[ubObjNr].uwCANPTR & 0x0700) == 0x0200)  // if FIFO base object "
      Print #f, "  {"
'      Print #f, "    // CANPTRn(new) = CANPTR(old) & ~(uword)FSIZEn | (CANPTRn(old) + 1) & FZIZEn"
'      Print #f, "    j = ((aubFIFOWritePtr[ubObjNr / 2] & ~(uword)(CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)) |"
'      Print #f, "        ((aubFIFOWritePtr[ubObjNr / 2] + 1) & (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)));"
      Print #f, "    j = aubFIFOWritePtr[ubObjNr / 2];"
      Print #f, ""
      Print #f, "    ubReturn = 0;"
      Print #f, "    if((CAN_HWOBJ[j].uwMSGCTR & 0x3000) == 0x1000)      // if reset TXRQ "
      Print #f, "    {"
'      Print #f, "      aubFIFOWritePtr[ubObjNr / 2] = j;       // save write pointer"
      Print #f, "      // CANPTRn(new) = CANPTR(old) & ~(uword)FSIZEn | (CANPTRn(old) + 1) & FZIZEn"
      Print #f, "      aubFIFOWritePtr[ubObjNr / 2] = ((aubFIFOWritePtr[ubObjNr / 2] & ~(uword)(CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)) |"
      Print #f, "                                     ((aubFIFOWritePtr[ubObjNr / 2] + 1) & (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)));"
      Print #f, ""
      Print #f, "      CAN_HWOBJ[j].uwMSGCTR = 0xf9ff;         // set CPUUPD, reset NEWDAT "
      Print #f, ""
      Print #f, "      if(CAN_HWOBJ[j].uwMSGCFG & 0x0004)      // if extended identifier"
      Print #f, "      {"
      Print #f, "        CAN_HWOBJ[j].ulCANAR   = pstObj->ulID ;"
      Print #f, "      }"
      Print #f, "      else                                    // if standard identifier"
      Print #f, "      {"
      Print #f, "        CAN_HWOBJ[j].ulCANAR   = pstObj->ulID << 18;"
      Print #f, "      }"
      Print #f, ""     ' DIR, XTD, NODE, RMM can not be changed
      Print #f, "      CAN_HWOBJ[j].uwMSGCFG  = (CAN_HWOBJ[j].uwMSGCFG & 0x000f) | "
      Print #f, "                               (pstObj->uwMsgCfg & 0x00f0); // set DLC"
      Print #f, ""
      Print #f, "      for(i = 0; i < (pstObj->uwMsgCfg & 0x00f0) >> 4; i++)"
      Print #f, "      {"
      Print #f, "        CAN_HWOBJ[j].ubData[i] = pstObj->ubData[i];"
      Print #f, "      }"
      Print #f, "      CAN_HWOBJ[j].uwMSGCTR  = 0x66bf;        // set TXRQ, NEWDAT, MSGVAL "
      Print #f, "                                              // reset CPUUPD, RMTPND"
      Print #f, "      ubReturn = 1;"
      Print #f, "    }"
      Print #f, "  }"
      Print #f, "  return(ubReturn);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubReadFIFO function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReadFIFO"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte i,j;"
      Print #f, "  ubyte ubReturn;"
      Print #f, ""
      Print #f, "  ubReturn = 2;"
      Print #f, ""
      Print #f, "  if((CAN_HWOBJ[ubObjNr].uwCANPTR & 0x0700) == 0x0200)  // if FIFO base object "
      Print #f, "  {"
'      Print #f, "    // CANPTRn(new) = CANPTR(old) & ~(uword)FSIZEn | (CANPTRn(old) + 1) & FZIZEn"
'      Print #f, "    j = ((aubFIFOReadPtr[ubObjNr / 2] & ~(uword)(CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)) |"
'      Print #f, "        ((aubFIFOReadPtr[ubObjNr / 2] + 1) & (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)));"
      Print #f, "    j = aubFIFOReadPtr[ubObjNr / 2];"
      Print #f, ""
      Print #f, "    ubReturn = 0;"
      Print #f, "    if((CAN_HWOBJ[j].uwMSGCTR & 0x0300) == 0x0200)    // if NEWDAT "
      Print #f, "    {"
      Print #f, "      CAN_HWOBJ[j].uwMSGCTR = 0xfdff;                 // clear NEWDAT"
'      Print #f, "      aubFIFOReadPtr[ubObjNr / 2] = j;                // save read pointer"
      Print #f, ""
      Print #f, "      // CANPTRn(new) = CANPTR(old) & ~(uword)FSIZEn | (CANPTRn(old) + 1) & FZIZEn"
      Print #f, "      aubFIFOReadPtr[ubObjNr / 2] = ((aubFIFOReadPtr[ubObjNr / 2] & ~(uword)(CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)) |"
      Print #f, "                                    ((aubFIFOReadPtr[ubObjNr / 2] + 1) & (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f)));"
      Print #f, ""
      Print #f, "      // check if the previous message was lost 
      Print #f, "      if((CAN_HWOBJ[j].uwMSGCTR & 0x0c00) == 0x0800)  // if set MSGLST "
      Print #f, "      {"
      Print #f, "        CAN_HWOBJ[j].uwMSGCTR = 0xf7ff;               // reset MSGLST "
      Print #f, "        return(3);"
      Print #f, "      }"
      Print #f, ""
      Print #f, "      for(i = 0; i < (CAN_HWOBJ[j].uwMSGCFG & 0x00f0) >> 4; i++)"
      Print #f, "      {"
      Print #f, "        pstObj->ubData[i] = CAN_HWOBJ[j].ubData[i];"
      Print #f, "      }"
      Print #f, ""
      Print #f, "      if(CAN_HWOBJ[j].uwMSGCFG & 0x04)        // if extended identifier"
      Print #f, "      {"
      Print #f, "        pstObj->ulID   = CAN_HWOBJ[j].ulCANAR;"
      Print #f, "      }"
      Print #f, "      else                                    // if standard identifier "
      Print #f, "      {"
      Print #f, "        pstObj->ulID   = CAN_HWOBJ[j].ulCANAR >> 18;"
      Print #f, "      }"
      Print #f, ""
      Print #f, "      pstObj->uwMsgCfg  = CAN_HWOBJ[j].uwMSGCFG;"
      Print #f, ""
      Print #f, "      // check if the message was corrupted "
      Print #f, "      if((CAN_HWOBJ[j].uwMSGCTR & 0x0300) == 0x0200)  // if NEWDAT "
      Print #f, "      {"
      Print #f, "        CAN_HWOBJ[j].uwMSGCTR = 0xfdff;               // clear NEWDAT"
      Print #f, "        return(4);"
      Print #f, "      }"
      Print #f, "      ubReturn = 1;"
      Print #f, "    }"
      Print #f, "  }"
      Print #f, "  return(ubReturn);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubFlushReadFIFO function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "FlushReadFIFO"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte j;"
      Print #f, "  ubyte ubReturn;"
      Print #f, ""
      Print #f, "  ubReturn = 0;"
      Print #f, ""
      Print #f, "  if((CAN_HWOBJ[ubObjNr].uwCANPTR & 0x0700) == 0x0200)  // if FIFO base object "
      Print #f, "  {"
      Print #f, "    aubFIFOReadPtr[ubObjNr / 2] = ubObjNr;"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwCANPTR = (CAN_HWOBJ[ubObjNr].uwCANPTR & ~(uword)0x001F) | ubObjNr;"
      Print #f, ""
      Print #f, "    for(j = ubObjNr; j < ubObjNr + (CAN_HWOBJ[ubObjNr].uwCANFCR & 0x001f); j++)"
      Print #f, "    {"
      Print #f, "      CAN_HWOBJ[j].uwMSGCTR = 0xf5ff;                   // reset MSGLST, NEWDAT "
      Print #f, "    }"
      Print #f, "    // make sure that no new frame was received"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwCANPTR = (CAN_HWOBJ[ubObjNr].uwCANPTR & ~(uword)0x001F) | ubObjNr;"
      Print #f, "    CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xf5ff;               // reset MSGLST, NEWDAT "
      Print #f, ""
      Print #f, "    ubReturn = 1;"
      Print #f, "  }"
      Print #f, "  return(ubReturn);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_viSRNx function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    For i = 0 To 7
      n = "SRN" & i
      If (IsISRSelected(r, sVarPre, sRegPre & i & "IC", n)) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), sVarPre & "_SRN" & i & "INT")
        Print #f, "{"
'        Print #f, ""

        vRxMask = 0
        vTxMask = 0
        For j = 0 To 31
            If ((r.v(sRegPre & "MSGCFGH" & j & "_RXINP") = i And _
                 r.v(sRegPre & "MSGCTRL" & j & "_RXIE") = 2)) Then
                    vRxMask = vRxMask + 2 ^ j
            End If
            
            If ((r.v(sRegPre & "MSGCFGH" & j & "_TXINP") = i And _
                 r.v(sRegPre & "MSGCTRL" & j & "_TXIE") = 2)) Then
                    vTxMask = vTxMask + 2 ^ j
            End If
        Next j


        vMask4A = 0
        If ((r.v(sRegPre & "ACR_LECIE") = 1 And r.v(sRegPre & "AGINP_LECINP") = i)) Then
            vMask4A = vMask4A + &H0007
        End If
        If ((r.v(sRegPre & "ACR_SIE") = 1 And r.v(sRegPre & "AGINP_TRINP") = i)) Then
            vMask4A = vMask4A + &H0018
        End If
        If ((r.v(sRegPre & "ACR_EIE") = 1 And r.v(sRegPre & "AGINP_EINP") = i)) Then
            vMask4A = vMask4A + &H00C0
        End If
        vMask4B = 0
        If ((r.v(sRegPre & "BCR_LECIE") = 1 And r.v(sRegPre & "BGINP_LECINP") = i)) Then
            vMask4B = vMask4B + &H0007
        End If
        If ((r.v(sRegPre & "BCR_SIE") = 1 And r.v(sRegPre & "BGINP_TRINP") = i)) Then
            vMask4B = vMask4B + &H0018
        End If
        If ((r.v(sRegPre & "BCR_EIE") = 1 And r.v(sRegPre & "BGINP_EINP") = i)) Then
            vMask4B = vMask4B + &H00C0
        End If
        
        If (vMask4A > 0) Then
            Print #f, "  uword uwStatusA;"
        End If
        If (vMask4B > 0) Then
            Print #f, "  uword uwStatusB;"
        End If
        Print #f, ""

        Print #f, uc.GetUserCode("(" & n & ",2)",2)
        Print #f, ""

        If (r.Exist("SDLM_PISEL_I1SEL") = True) Then
            'for SDLM ISR
            'start
            If (i = 7 And r.v("SDLM_PISEL_I1SEL") = 1 And r.v("SDLM_USE_Init") = 1) Then
                    Print #f, "  //for SDLM_I1 interrupt"
                    Print #f, ""
                If (r.v("SDLM_INTCON_HDIE") = 1) Then
                    Print #f, "  if (SDLM_TRANSSTAT & TRANSSTAT_HEADER)"
                    Print #f, "  {"
                    Print #f, "    // header received"
                    Print #f, "    // Note:"
                    Print #f, "    // To check if an IFR has to be send use"
                    Print #f, "    // " & r.v("SDLM_NAME_SingleByteHeader") & " and "
                    Print #f, "    // " & r.v("SDLM_NAME_IFRNotAllowed") & "."
                    Print #f, "    // "
                    Print #f, "    // If an IFR has to be send use"
                    Print #f, "    // " & r.v("SDLM_NAME_TransmitOneByteIFR") & " or "
                  
                    If (r.v("SDLM_BUFFCON_TXINCE") = 0) Then
                      Print #f, "    // " & r.v("SDLM_NAME_TransmitIFR")
                    Else
                      Print #f, "    // " & r.v("SDLM_NAME_TransmitIFRFIFO")
                    End If
                  
                    Print #f, "    // if automatic transmission of IFR is"
                    Print #f, "    // not enabled or possible."
                    Print #f, "    // "
                    Print #f, ""
                    Print #f, uc.GetUserCode("(" & n & ",10)",4) 
                    Print #f, ""
                    Print #f, "  }"
                    Print #f, ""
                End If

                If (r.v("SDLM_INTCON_RECIE") = 1) Then
                    Print #f, "  if (SDLM_TRANSSTAT & TRANSSTAT_MSGREC)"
                    Print #f, "  {"
                
                    If (r.v("SDLM_GLOBCON_BMEN") = 1) Then
                        Print #f, "    // message received"
                        Print #f, "    // Note:"
                        Print #f, "    // This bit MUST be reset by a read operation"
                        Print #f, "    // on the FIFO (using " & r.v("SDLM_NAME_GetRxData") & ")."
                    Else
                        Print #f, "    // message received"
                        Print #f, "    // Note:"
                      If (r.v("SDLM_BUFFCON_TXINCE") = 0) Then
                        Print #f, "    // Use " & r.v("SDLM_NAME_ReadReceiveBuffer")
                      Else
                        Print #f, "    // Use " & r.v("SDLM_NAME_ReadReceiveFIFO")
                      End If
                        Print #f, "    // to get the received data."
                        Print #f, "    // To reset the flag only use"
                        Print #f, "    // " & r.v("SDLM_NAME_ResetReceivedAndMessLostStatus")
                    End If
                
                    Print #f, ""
                    Print #f, uc.GetUserCode("(" & n & ",11)",4) 
                    Print #f, ""
                    Print #f, "  }"
                    Print #f, ""
                End If

                If (r.v("SDLM_INTCON_TRAIE") = 1) Then
                    Print #f, "  if (SDLM_TRANSSTAT & TRANSSTAT_MSGTRA)"
                    Print #f, "  {"
                
                    If (r.v("SDLM_GLOBCON_BMEN") = 1) Then
                        Print #f, "    // message transmitted"
                        Print #f, "    // Note:"
                        Print #f, "    // This bit is automatically reset by hardware"
                        Print #f, "    // on a write operation on the transmit FIFO."
                    Else
                        Print #f, "    // message transmitted"
                        Print #f, "    // Note:"
                      If (r.v("SDLM_BUFFCON_TXINCE") = 0) Then
                        Print #f, "    // Use " & r.v("SDLM_NAME_TransmitBuffer")
                      Else
                        Print #f, "    // Use " & r.v("SDLM_NAME_TransmitFIFO")
                      End If
                        Print #f, "    // to transmit the next frame immidiately."
                        Print #f, "    // To reset the flag only use"
                        Print #f, "    // " & r.v("SDLM_NAME_ResetTransmittedStatus")
                    End If
                
                    Print #f, ""
                    Print #f, uc.GetUserCode("(" & n & ",12)",4) 
                    Print #f, ""
                    Print #f, "  }"
                    Print #f, ""
                    Print #f, "  //end for SDLM_I1 interrupt"
                End If

                Print #f, ""

            End If
            'for SDLM ISR
            'stop
        End If
        
        If (Not (vRxMask = 0 And vMask4A = 0 And vTxMask = 0 And vMask4B = 0)) Then ' no node x source
'                Print #f, ""
'                Print #f, uc.GetUserCode("(" & n & ",2b)",2)
'                Print #f, ""

            s = ""
            If (vRxMask > 0) Then  ' if a RX source
                s = "(( ((ulong)" & sRegPre & "RXIPNDH << 16) + " & sRegPre & "RXIPNDL) & " & _
                    FormatTextOutput(vRxMask, "H8") & ")"
            End If
            If (vTxMask > 0) Then  ' if a TX source
                If Not(s = "") Then
                  s = s & " || "
                End If
                s = s & "(( ((ulong)" & sRegPre & "TXIPNDH << 16) + " & sRegPre & "TXIPNDL) & " & _
                    FormatTextOutput(vTxMask, "H8") & ")"
            End If
            If (vMask4A > 0) Then  ' if a Node A
                If Not(s = "") Then
                  s = s & " || "
                End If
                s = s & "(" & sRegPre & "ASR & " & FormatTextOutput(vMask4A, "H4") & ")"
            End If
            If (vMask4B > 0) Then  ' if a Node B
                If Not(s = "") Then
                  s = s & " || "
                End If
                s = s & "(" & sRegPre & "BSR & " & FormatTextOutput(vMask4B, "H4") & ")"
            End If

            Print #f, "  while(" & s & ")"
            Print #f, "  {"

                
            ' for node A sources
'            If (vRxMask > 0 Or vTxMask > 0) Then
                            
                If (vMask4A > 0) Then
                            Print #f, ""
                            Print #f, "    // status change interrupt of node A"
                            Print #f, ""
                            Call GenerateSRNNodex(r, vIndex, f, i, "A", uc)
                            Print #f, ""
                End If
                If (vMask4B > 0) Then
                            Print #f, ""
                            Print #f, "    // status change interrupt of node B"
                            Print #f, ""
                            Call GenerateSRNNodex(r, vIndex, f, i, "B", uc)
                            Print #f, ""
                End If
                
                For j = 0 To 31
                    If ((r.v(sRegPre & "MSGCFGH" & j & "_RXINP") = i And _
                         r.v(sRegPre & "MSGCTRL" & j & "_RXIE") = 2) Or _
                         (r.v(sRegPre & "MSGCFGH" & j & "_TXINP") = i And _
                         r.v(sRegPre & "MSGCTRL" & j & "_TXIE") = 2)) Then
                            Print #f, ""
                            Print #f, "    // message object " & j & " interrupt"
'                            Print #f, ""
                            Call GenerateSRNObjx(r, vIndex, f, i, j, uc)
                            Print #f, ""
                    End If
                Next j
                            Print #f, ""
                            Print #f, uc.GetUserCode("(" & n & ",3)",4)
                            Print #f, ""
                            Print #f, ""
                       
            
                            Print #f, "  }  // End of while()"
        End If  ' no node x source
                        
        If ((r.v(sRegPre & "AFCRH_CFCIE") = 1 And r.v(sRegPre & "AGINP_CFCINP") = i)) Then
                            Print #f, ""
                            Print #f, "  if(" & sRegPre & "AFCRH & " & sRegPre & "AFCRH_CFCOV)     // if frame counter overflow flag A"
                            Print #f, "  {"
                            Print #f, "    " & sRegPre & "AFCRH = " & sRegPre & "AFCRH & ~(uword)" & sRegPre & "AFCRH_CFCOV;  // reset overflow flag A"
                            Print #f, ""
                            Print #f, uc.GetUserCode("(" & n & ",5)",4)
                            Print #f, ""
                            Print #f, "  }"
        End If
        If ((r.v(sRegPre & "BFCRH_CFCIE") = 1 And r.v(sRegPre & "BGINP_CFCINP") = i)) Then
                            Print #f, ""
                            Print #f, "  if(" & sRegPre & "BFCRH & " & sRegPre & "BFCRH_CFCOV)     // if frame counter overflow flag B"
                            Print #f, "  {"
                            Print #f, "    " & sRegPre & "BFCRH = " & sRegPre & "BFCRH & ~(uword)" & sRegPre & "BFCRH_CFCOV;  // reset overflow flag B"
                            Print #f, ""
                            Print #f, uc.GetUserCode("(" & n & ",6)",4)
                            Print #f, ""
                            Print #f, "  }"
        End If
                        
                            Print #f, ""
                            Print #f, uc.GetUserCode("(" & n & ",7)",2)
                            Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
      End If
    Next i


    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,10)",0)
    Print #f, ""
    
    'close file
    Close f
    
    CANGenerateC = True
    Exit Function
    
CANGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the CAN.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function CANGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo CANGenerateHError

    CANGenerateH = False
    
    Dim i As Integer               ' used as index for loops
    Dim f As Variant               ' used for file operations
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim n As String                ' used as temp variable for function names
    Dim dc As DAvECollection       ' used as temp variable for Collections
    Dim uc As UserCode             ' used for UserCode objects
    Dim func As FunctionClass      ' used for FunctionClass objects
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim l As Long                  ' used as index for loops

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

   
    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New  FunctionClass

    'refresh the decriptions of the functions and macros  
    RefreshFunctionDescriptions(vIndex, func, r)
    
    'read usercode
    uc.ReadFile((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_HFILE"))
    
    'open CAN.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,1)",0)
    Print #f, ""
    Print #f, Rtf.GenerateNote((r.v(sVarPre & "_Notes")))
    Print #f, ""
    Print #f, "#ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    Print #f, "#define _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    Print #f, ""
    Print #f, SECTION_PROJECT_INCLUDES
    Print #f, ""  'DAvE includes'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,2)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_MACROS
    Print #f, ""  'DAvE global macro definitions; not used as functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,3)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_DEFINES
    Print #f, ""  'DAvE global #define definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,4)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type definitions'
    '//****************************************************************************"
    '// @Definition of a structure for the CAN data"
    '//****************************************************************************"
    Print #f, FormatSectionHeader(0, 1, "@Definition of a structure for the CAN data")
    Print #f, ""
    Print #f, "// The following data type serves as a software message object. Each access to"
    Print #f, "// a hardware message object has to be made by forward a pointer to a software"
    Print #f, "// message object (TCAN_SWObj). The data type has the following fields:"
    Print #f, "//"
    Print #f, "// uwMsgCfg:"
    Print #f, "// this byte has the same structure as the message configuration register of a"
    Print #f, "// hardware message object. It contains the ""Data Lenght Code"" (DLC), the "
    Print #f, "// ""Extended Identifier"" (XTD), the ""Message Direction"" (DIR), the ""Node"
    Print #f, "// Select"" and the ""Remote Monitoring Mode""."
    Print #f, "//"
    Print #f, "//"
    Print #f, "//         7     6     5      4    3     2     1     0"
    Print #f, "//      |------------------------------------------------|"
    Print #f, "//      |        DLC            | DIR | XTD | NODE | RMM |"
    Print #f, "//      |------------------------------------------------|"
    Print #f, "//"
    Print #f, "// ulID: "
    Print #f, "// this field is four bytes long and contains either the 11-bit identifier "
    Print #f, "// or the 29-bit identifier"
    Print #f, "//"
    Print #f, "// ulMask: "
    Print #f, "// this field is four bytes long and contains either the 11-bit mask "
    Print #f, "// or the 29-bit mask"
    Print #f, "//"
    Print #f, "// ubData[8]:"
    Print #f, "// 8 bytes containing the data of a frame"
    Print #f, "//"
    Print #f, "// uwCounter:"
    Print #f, "// this field is two bytes long and contains the counter value "
    Print #f, "//"
    Print #f, ""
    Print #f, "typedef struct"
    Print #f, "  {"
    Print #f, "     uword  uwMsgCfg;   // Message Configuration Register"
    Print #f, "     ulong  ulID;       // standard (11-bit)/extended (29-bit) identifier"
    Print #f, "     ulong  ulMask;     // standard (11-bit)/extended (29-bit) mask"
    Print #f, "     ubyte  ubData[8];  // 8-bit Data Bytes"
    Print #f, "     uword  uwCounter;  // Frame Counter"
    Print #f, "  }TCAN_SWObj;"
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,5)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
    Print #f, ""  'DAvE external declarations of imported global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,6)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definition of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,7)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_PROTOTYPES
    Print #f, ""  'DAvE declarations of functions which should become global'

    ' for each function in this 'func' object
    For l = 1 To func.FunctionCount
      ' get the function name which was insert in 'Include.inc' with 'AddFunction()'
      d = func.FunctionName(l)
      ' get only the third part of the function name (e.g "Init")
      n = Right(d, Len(d) - Instr(d, "NAME") - Len("NAME"))
      ' only if the use variable of the function is selected and the function is a really function
      ' not a macro or a ISR
      If (r.v(sVarPre & "_USE_" & n) = 1 AND func.FunctionSort(d) = 1) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"  
      End If
    Next l

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)",0)
    Print #f, ""
    Print #f, ""
                          'DAvE declarations of global macros'
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetTxErrorCounter macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetTxErrorCounter"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
            Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " _" & n & "##NODE_NAME()" 
            Print #f, "#define _" & n & sVarPre & "_NODEA()  (ubyte)((" & sVarPre & "_AECNTL & 0xFF00) >> 8)"
            Print #f, "#define _" & n & sVarPre & "_NODEB()  (ubyte)((" & sVarPre & "_BECNTL & 0xFF00) >> 8)"
            Print #f, ""                                                                                    
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetRxErrorCounter macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetRxErrorCounter"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
            Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " _" & n & "##NODE_NAME()" 
            Print #f, "#define _" & n & sVarPre & "_NODEA()  (ubyte)(" & sVarPre & "_AECNTL & 0x00FF)"
            Print #f, "#define _" & n & sVarPre & "_NODEB()  (ubyte)(" & sVarPre & "_BECNTL & 0x00FF)"
            Print #f, ""                                                                                    
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetErrorTransferDirection macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetErrorTransferDirection"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
            Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " _" & n & "##NODE_NAME()" 
            Print #f, "#define _" & n & sVarPre & "_NODEA()  (ubyte)((" & sVarPre & "_AECNTH & " & sVarPre & "_AECNTH_LETD) >> 8)"
            Print #f, "#define _" & n & sVarPre & "_NODEB()  (ubyte)((" & sVarPre & "_BECNTH & " & sVarPre & "_BECNTH_LETD) >> 8)"
            Print #f, ""                                                                                    
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetErrorIncrement macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetErrorIncrement"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
            Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " _" & n & "##NODE_NAME()" 
            Print #f, "#define _" & n & sVarPre & "_NODEA()  (ubyte)((" & sVarPre & "_AECNTH & " & sVarPre & "_AECNTH_LEINC) >> 9)"
            Print #f, "#define _" & n & sVarPre & "_NODEB()  (ubyte)((" & sVarPre & "_BECNTH & " & sVarPre & "_BECNTH_LEINC) >> 9)"
            Print #f, ""                                                                                    
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
    For i = 0 To 7
      n = "SRN" & i
      If (IsISRSelected(r, sVarPre, sRegPre & i & "IC", n)) Then
          If (i > 3) Then
              v = 84 + i + 1 
          Else
              v = 84 + i
          End If
          Print #f, "#define " & sVarPre & "_SRN" & i & "INT    " & FormatTextOutput(v, "H2")
      End If
    Next i

            Print #f, ""
            Print #f, uc.GetUserCode("(" & sVarPre & "_Header,9)",0)
            Print #f, ""
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    
    'close file
    Close f
    
    CANGenerateH = True
    Exit Function
    
CANGenerateHError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This functions generates the code and statements for the CAN 
'        message object x.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'        f                file handle
'        iObj             number of message object (0 - 31)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Sub GenerateObjx(r As ProjectData, vIndex As Variant, f As Variant, iObj As Integer)
    On Error GoTo GenerateObjxError

    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

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

    '------------------------------------------------------------------------------------------------
    ' Configuration of Message Object x:
    '------------------------------------------------------------------------------------------------
    Print #f, FormatSectionHeader(2, 1, "Configuration of Message Object " & iObj & ":")
    
    If (r.v(sRegPre & "MSGCTRL" & iObj & "_MSGVAL") = 1) Then
      d = "message object " & iObj & " is not valid"
      Print #f, FormatCommentsForInit(1, d)    
    Else
      d = "message object " & iObj & " is valid"
      Print #f, FormatCommentsForInit(1, d)    

      If (r.v(sRegPre & "MSGCTRL" & iObj & "_RXIE") = 2) Then
        d = "enable receive interrupt; bit INTPND is set after successfull reception of a frame"
        Print #f, FormatCommentsForInit(1, d)    
      End If
      If (r.v(sRegPre & "MSGCTRL" & iObj & "_TXIE") = 2) Then
        d = "enable transmit interrupt; bit INTPND is set after successfull transmission of a frame"
        Print #f, FormatCommentsForInit(1, d)    
      End If       
    End If
        
        

    If (r.v(sRegPre & "MSGCTRL" & iObj & "_MSGVAL") = 2) Then  ' only if Object is valid, configure the Object
        Print #f, ""          

        If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 1) Then
            d = "message object is used as transmit object"
        Else
            d = "message object is used as receive object"
        End If
        Print #f, FormatCommentsForInit(1, d)    
        
        If (r.v(sRegPre & "MSGCFGL" & iObj & "_XTD") = 1) Then
            d = "extended 29-bit identifier"
        Else
            d = "standard 11-bit identifier"
        End If
        Print #f, FormatCommentsForInit(1, d)    
    
        d = "" & r.v(sRegPre & "MSGCFGL" & iObj & "_DLC") & " valid data bytes"
        Print #f, FormatCommentsForInit(1, d)    
    
        If (r.v(sRegPre & "MSGCFGL" & iObj & "_NODE") = 1) Then
            d = "this message object works with CAN node B"
        Else
            d = "this message object works with CAN node A"
        End If
        Print #f, FormatCommentsForInit(1, d)    
    
        If (r.v(sRegPre & "MSGCFGL" & iObj & "_RMM") = 1) Then
            d = "the identifier and DLC code of a remote frame with matching identifier is copied to this transmit object"
        Else
            d = "remote monitoring is disabled"
        End If
        Print #f, FormatCommentsForInit(1, d)    
            
        If (r.v(sRegPre & "MSGCTRL" & iObj & "_TXIE") = 2) Then
            d = "transmit interrupt node pointer: TwinCAN SRN " & r.v(sRegPre & "MSGCFGH" & iObj & "_TXINP")
            Print #f, FormatCommentsForInit(1, d)    
        End If
        If (r.v(sRegPre & "MSGCTRL" & iObj & "_RXIE") = 2) Then
            d = "receive interrupt node pointer: TwinCAN SRN " & r.v(sRegPre & "MSGCFGH" & iObj & "_RXINP")
            Print #f, FormatCommentsForInit(1, d)    
        End If  


        v = 0
'        for the listen mode the DLC field is important in the receive frames (remote)
'        If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 1) Then   ' only for transmit frames
            v = v + r.v(sRegPre & "MSGCFGL" & iObj & "_DLC") * 2 ^ 4
'        End If
        v = v + r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") * 2 ^ 3
        v = v + r.v(sRegPre & "MSGCFGL" & iObj & "_XTD") * 2 ^ 2
        v = v + r.v(sRegPre & "MSGCFGL" & iObj & "_NODE") * 2 ^ 1
        'If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 1) Then   ' only for transmit frames
            v = v + r.v(sRegPre & "MSGCFGL" & iObj & "_RMM")
        'End If
            
        Print #f, ""
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGCFGL" & iObj, "=", FormatTextOutput(v, "H4"), "load message configuration register low")

        v = 0
        v = v + r.v(sRegPre & "MSGCFGH" & iObj & "_RXINP") * 2 ^ 0
        v = v + r.v(sRegPre & "MSGCFGH" & iObj & "_TXINP") * 2 ^ 4
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGCFGH" & iObj, "=", FormatTextOutput(v, "H4"), "load message configuration register high")
        Print #f, ""

        If (r.v(sRegPre & "MSGCFGL" & iObj & "_XTD") = 1) Then ' 29-bit Identifier
            v = 0
            v = v + r.v(sRegPre & "MSGAMRH" & iObj & "_AM") * 2^16 + _
                r.v(sRegPre & "MSGAMRL" & iObj & "_AM")
            d = "acceptance mask 29-bit: " & FormatTextOutput(v, "H4")
            Print #f, FormatCommentsForInit(1, d)    
            
            v = 0
            v = v + r.v(sRegPre & "MSGARH" & iObj & "_ID") * 2^16 + _
                r.v(sRegPre & "MSGARL" & iObj & "_ID")
            d = "identifier 29-bit:      " & FormatTextOutput(v, "H4")
            Print #f, FormatCommentsForInit(1, d)    
        Else          ' 11-bit Identifier
            v = 0
            v = v + r.v(sRegPre & "MSGAMRH" & iObj & "_AM")
            v = v \ 2 ^ 2
            d = "acceptance mask 11-bit: " & FormatTextOutput(v, "H3")
            Print #f, FormatCommentsForInit(1, d)    
            
            v = 0
            v = v + r.v(sRegPre & "MSGARH" & iObj & "_ID")
            v = v \ 2 ^ 2
            d = "identifier 11-bit:      " & FormatTextOutput(v, "H3")
            Print #f, FormatCommentsForInit(1, d)    
        End If
            
        v = r.sfr(sRegPre & "MSGAMRL" & iObj)
        Print #f, ""
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGAMRL" & iObj, "=", FormatTextOutput(v, "H4"), "load acceptance mask register low")        
        v = r.sfr(sRegPre & "MSGAMRH" & iObj)
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGAMRH" & iObj, "=", FormatTextOutput(v, "H4"), "load acceptance mask register high")       
        v = r.sfr(sRegPre & "MSGARL" & iObj)          
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGARL" & iObj, "=", FormatTextOutput(v, "H4"), "load arbitration register low")        
        v = r.sfr(sRegPre & "MSGARH" & iObj)          
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGARH" & iObj, "=", FormatTextOutput(v, "H4"), "load arbitration register high")        
        v = r.sfr(sRegPre & "MSGDRL" & iObj & "0")
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGDRL" & iObj & "0", "=", FormatTextOutput(v, "H4"), "load data register 0 low")        
        v = r.sfr(sRegPre & "MSGDRH" & iObj & "0")
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGDRH" & iObj & "0", "=", FormatTextOutput(v, "H4"), "load data register 0 high")     
        v = r.sfr(sRegPre & "MSGDRL" & iObj & "4")
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGDRL" & iObj & "4", "=", FormatTextOutput(v, "H4"), "load data register 4 low")
        v = r.sfr(sRegPre & "MSGDRH" & iObj & "4")
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGDRH" & iObj & "4", "=", FormatTextOutput(v, "H4"), "load data register 4 high")
        Print #f, ""

        If (r.v(sRegPre & "MSGFGCRL" & iObj & "_SDT") = 1) Then
            d = "enable single data transfer mode"
            Print #f, FormatCommentsForInit(1, d)    
        End If
        
        If (r.v(sRegPre & "MSGFGCRL" & iObj & "_STT") = 1) Then
            d = "enable single transmission try"
            Print #f, FormatCommentsForInit(1, d)    
        End If

        Select Case (r.v(sRegPre & "MSGFGCRH" & iObj & "_MMC"))
            Case 0
                d = "functionality of standard message object"
            Case 1
                ' not used
            Case 2
                d = "FIFO functionality; this is a FIFO base object " & _
                    "for a " & r.v(sRegPre & "MSGFGCRL" & iObj & "_FSIZE") + 1 & " stage FIF0"
                Print #f, FormatCommentsForInit(1, d)    
                If (r.v(sRegPre & "MSGFGCRL" & iObj & "_FD") = 0) Then
                  d = "the CANPTR is updated after a correct reception of a frame (FIFO reception)"
                Else
                  d = "the CANPTR is updated after a correct transmission of a frame (FIFO transmission)"
                End If
            Case 3
                d = "FIFO functionality; this is a FIFO slave object " & _
                    "connected to the base object " & IsObjFIFOSlave(r, vIndex, iObj)
                Print #f, FormatCommentsForInit(1, d)    
                If (r.v(sRegPre & "MSGFGCRL" & iObj & "_FD") = 0) Then
                  d = "the CANPTR is updated after a correct reception of a frame (FIFO reception)"
                Else
                  d = "the CANPTR is updated after a correct transmission of a frame (FIFO transmission)"
                End If
            Case 4
                If ((iObj Mod 2) = 0) Then
                  d = "normal gateway mode (gateway destination); object " & iObj - 1 & " is used as gateway source"
                Else
                  d = "normal gateway mode (gateway source); object " & iObj + 1 & " is used as gateway destination"
                End If
            Case 5
                If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 1) Then
                  d = "shared gateway mode; this object is initialized as transmit object on the destination side"
                Else
                  d = "shared gateway mode; this object is initialized as receive object on the source side"
                End If
        End Select
        Print #f, FormatCommentsForInit(1, d)          
       
        ' for normal gateway functionality  
        If ((r.v(sRegPre & "MSGFGCRH" & iObj & "_MMC") = 4)) Then
          If ((iObj Mod 2) = 1) Then     '  (source objects only)
            If (r.v(sRegPre & "MSGFGCRL" & iObj & "_DLCC") = 1) Then
                d = "the received source DLC information is copied to the destination object"
            Else
                d = "the received source DLC information is not copied to the destination object"
            End If
            Print #f, FormatCommentsForInit(1, d)    
            
            If (r.v(sRegPre & "MSGFGCRL" & iObj & "_IDC") = 1) Then
                d = "the received source identifier is automatically copied to the destination object to replace the identifier"
            Else
                d = "the received source identifier is not automatically copied to the destination object"
            End If
            Print #f, FormatCommentsForInit(1, d)    
          Else  ' for destination objects
            If (r.v(sRegPre & "MSGFGCRL" & iObj & "_DLCC") = 1) Then
                d = "the received destination DLC information is copied to the source object"
            Else
                d = "the received destination DLC information is not copied to the source object"
            End If
            Print #f, FormatCommentsForInit(1, d)    
            
            If (r.v(sRegPre & "MSGFGCRL" & iObj & "_IDC") = 1) Then
                d = "the received destination identifier is automatically copied to the source object to replace the identifier"
            Else
                d = "the received destination identifier is not automatically copied to the source object"
            End If
            Print #f, FormatCommentsForInit(1, d)    
          End If
        End If

        ' for shared gateway functionality and destination objects in normal gateway mode
        If (r.v(sRegPre & "MSGFGCRH" & iObj & "_MMC") = 5 Or (r.v(sRegPre & "MSGFGCRH" & iObj & "_MMC") = 4 And (iObj Mod 2) = 0)) Then
            If (r.v(sRegPre & "MSGFGCRL" & iObj & "_SRREN") = 1) Then
                d = "a matching remote frame on a destination CAN bus generate a remote frame on the source CAN bus"
            Else
                d = "a matching remote frame on a destination CAN bus is answered by a data frame from destination object"
            End If
            Print #f, FormatCommentsForInit(1, d)    
        End If

        ' for shared gateway functionality or normal gateway mode (source objects only)
        If (r.v(sRegPre & "MSGFGCRH" & iObj & "_MMC") = 5 Or (r.v(sRegPre & "MSGFGCRH" & iObj & "_MMC") = 4 And (iObj Mod 2) = 1)) Then
            If (r.v(sRegPre & "MSGFGCRL" & iObj & "_GDFS") = 1) Then
                d = "a data frame received by the source object is automatically sent by the destination object"
            Else
                d = "a data frame received by the source object is not automatically sent by the destination object"
            End If
            Print #f, FormatCommentsForInit(1, d)    
        End If
        
        v = 0
        v = v + r.sfr(sRegPre & "MSGFGCRL" & iObj)
        Print #f, ""
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGFGCRL" & iObj, "=", FormatTextOutput(v, "H4"), "load FIFO/gateway control register low")

        v = 0
        v = v + r.sfr(sRegPre & "MSGFGCRH" & iObj)
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGFGCRH" & iObj, "=", FormatTextOutput(v, "H4"), "load FIFO/gateway control register high")
        Print #f, ""


    Else    ' Workaround
        Print #f, FormatCodeStatemente(2, sRegPre & "MSGFGCRL" & iObj, "=", _
                  FormatTextOutput(0, "H4"), "load FIFO/gateway control register low")
        Print #f, ""

        Print #f, FormatCodeStatemente(2, sRegPre & "MSGFGCRH" & iObj, "=", _
                  FormatTextOutput(0, "H4"), "load FIFO/gateway control register high")
        Print #f, ""
    End If  ' if message object valid


    v = 0
    v = v + r.v(sRegPre & "MSGCTRH" & iObj & "_CFCVAL")
    Print #f, FormatCodeStatemente(2, sRegPre & "MSGCTRH" & iObj, "=", FormatTextOutput(v, "H4"), "load message control register high")

    ' for transmit frame
    v = 0
    v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_RMTPND") * 2 ^ 14
    v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_TXRQ") * 2 ^ 12
    v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_MSGLSTCPUUPD") * 2 ^ 10
   
    If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 1) Then  ' TX-Object
        v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_NEWDAT") * 2 ^ 8
' todo geht nicht fr Gateway Destination !!
'        v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_MSGVAL") * 2 ^ 8      ' NEWDAT is set if MSGVAL

    Else
        v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_NEWDAT") * 2 ^ 8
    End If
    v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_MSGVAL") * 2 ^ 6
    v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_TXIE") * 2 ^ 4
    v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_RXIE") * 2 ^ 2
    v = v + r.v(sRegPre & "MSGCTRL" & iObj & "_INTPND")
    Print #f, FormatCodeStatemente(2, sRegPre & "MSGCTRL" & iObj, "=", FormatTextOutput(v, "H4"), "load message control register low")  
    Print #f, ""

    Exit Sub
    
GenerateObjxError:
    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 successfull operation
'        False           else
'*****************************************************************************
Sub GenerateSRNNodex(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, vNode As Variant, uc As UserCode)
    On Error GoTo GenerateSRNNodexError

    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 & "_NODE" & vNode

    ' if EIE or LECIE or SIE
    If ((r.v(sRegPre & vNode & "CR_EIE") = 1 And r.v(sRegPre & vNode & "GINP_EINP") = iSRN) Or (r.v(sRegPre & vNode & "CR_SIE") = 1 And r.v(sRegPre & vNode & "GINP_TRINP") = iSRN) Or (r.v(sRegPre & vNode & "CR_LECIE") = 1 And r.v(sRegPre & vNode & "GINP_LECINP") = iSRN)) Then
            Print #f, "    uwStatus" & vNode & " = " & sRegPre & vNode & "SR;"
        If (r.v(sRegPre & vNode & "CR_EIE") = 1 And r.v(sRegPre & vNode & "GINP_EINP") = iSRN) Then
            Print #f, "    if (uwStatus" & vNode & " & 0x0080)  // if BOFF"
            Print #f, "    {"
            Print #f, "      // Indicates when the CAN controller is in busoff state."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",1)",6)
            Print #f, "    }"
            Print #f, ""
            Print #f, "    if (uwStatus" & vNode & " & 0x00040)  // if EWRN"
            Print #f, "    {"
            Print #f, "      // Indicates that the least one of the error counters in the"
            Print #f, "      // EML has reached the error warning limit of " & r.v(sRegPre & vNode & "ECNTH_EWRNLVL") &"."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",2)",6)
            Print #f, "    }"
            Print #f, ""
        End If
        If (r.v(sRegPre & vNode & "CR_SIE") = 1 And r.v(sRegPre & vNode & "GINP_TRINP") = iSRN) Then
            Print #f, "    if (uwStatus" & vNode & " & 0x0008)  // if TXOK"
            Print #f, "    {"
            Print #f, "      // Indicates that a message has been transmitted successfully"
            Print #f, "      // (error free and acknowledged by at least one other node)."
            Print #f, ""
            Print #f, "      uwStatus" & vNode & " &= 0xfff7;"
            Print #f, "      " & sRegPre & vNode & "SR    = uwStatus" & vNode & ";    // reset TXOK"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",3)",6)
            Print #f, "    }"
            Print #f, ""
            Print #f, "    if (uwStatus" & vNode & " & 0x0010)  // if RXOK"
            Print #f, "    {"
            Print #f, "      // Indicates that a message has been received successfully."
            Print #f, ""
            Print #f, "      uwStatus" & vNode & " &= 0xffef;"
            Print #f, "      " & sRegPre & vNode & "SR    = uwStatus" & vNode & ";    // reset RXOK"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",4)",6)
            Print #f, "    }"
            Print #f, ""
        End If
        If (r.v(sRegPre & vNode & "CR_LECIE") = 1 And r.v(sRegPre & vNode & "GINP_LECINP") = iSRN) Then
            Print #f, "    if (uwStatus" & vNode & " & 0x0007)  // if LEC"
            Print #f, "    {"
            Print #f, "      switch (uwStatus" & vNode & " & 0x0007)  // LEC" & vNode & " (Last Error Code" & vNode & ")"
            Print #f, "      {"
            'Print #f, "       case 0: // No Error"
            'Print #f, "               break;"
            'Print #f, ""
            Print #f, "       case 1: // Stuff Error"
            Print #f, "               // More than 5 equal bits in a sequence have occurred"
            Print #f, "               // in a part of a received message where this is not"
            Print #f, "               // allowed."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",5)",15)
            Print #f, ""
            Print #f, "               break;"
            Print #f, ""
            Print #f, "       case 2: // Form Error"
            Print #f, "               // A fixed format part of a received frame has the"
            Print #f, "               // wrong format."
            Print #f, "  "
            Print #f, uc.GetUserCode("(" & n & ",6)",15)
            Print #f, ""
            Print #f, "               break;"
            Print #f, ""
            Print #f, "       case 3: // Ack Error"
            Print #f, "               // The message this CAN controller transmitted was"
            Print #f, "               // not acknowledged by another node."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",7)",15)
            Print #f, ""
            Print #f, "               break;"
            Print #f, ""
            Print #f, "       case 4: // Bit1 Error"
            Print #f, "               // During the transmission of a message (with the"
            Print #f, "               // exeption of the arbitration field), the device"
            Print #f, "               // wanted to send a recessive level (""1""), but the"
            Print #f, "               // monitored bus value was dominant."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",8)",15)
            Print #f, ""
            Print #f, "               break;"
            Print #f, ""
            Print #f, "       case 5: // Bit0 Error"
            Print #f, "               // During the transmission of a message (or acknow-"
            Print #f, "               // ledge bit, active error flag, or overload flag),"
            Print #f, "               // the device wanted to send a dominant level (""0""),"
            Print #f, "               // but the monitored bus value was recessive. During"
            Print #f, "               // busoff recovery this staus is set each time a"
            Print #f, "               // sequence of 11 recessive bits has been monitored."
            Print #f, "               // This enables the CPU to monitor the proceeding of"
            Print #f, "               // the busoff recovery sequence (indicating the bus"
            Print #f, "               // is not stuck at dominant or continously disturbed)."
            Print #f, ""
            Print #f, "               if (uwStatus" & vNode & " & 0x0080)  // if Busoff status"
            Print #f, "               {"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",9)",17)
            Print #f, ""
            Print #f, "               }"
            Print #f, "               else"
            Print #f, "               {"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",10)",17)
            Print #f, ""
            Print #f, "               }"
            Print #f, "               break;"
            Print #f, ""
            Print #f, "       case 6: // CRC Error"
            Print #f, "               // The CRC check sum was incorrect in the message"
            Print #f, "               // received."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",11)",15)
            Print #f, ""
            Print #f, "               break;"
            Print #f, ""
            Print #f, "       default:"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",12)",15)
            Print #f, ""
            Print #f, "               break;"
            Print #f, ""
            Print #f, "      }"
            Print #f, ""
            Print #f, "      uwStatus" & vNode & " &= 0xfff8;"
            Print #f, "      " & sRegPre & vNode & "SR    = uwStatus" & vNode & ";    // reset LEC"
            Print #f, "    }"  ' if LEC
        End If
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",13)",4)
            Print #f, ""
    End If 
       
    Exit Sub
    
GenerateSRNNodexError:
    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 successfull operation
'        False           else
'*****************************************************************************
Sub GenerateSRNObjx(r As ProjectData, vIndex As Variant, f As Variant, iSRN As Integer, iObj As Integer, uc As UserCode)
    On Error GoTo GenerateSRNObjxError

    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 & "MSGCFGH" & iObj & "_RXINP") = iSRN And r.v(sRegPre & "MSGCTRL" & iObj & "_RXIE") = 2) Or _
        (r.v(sRegPre & "MSGCFGH" & iObj & "_TXINP") = iSRN And r.v(sRegPre & "MSGCTRL" & iObj & "_TXIE") = 2)) Then
            Print #f, ""
            Print #f, "    if((CAN_HWOBJ[" & iObj & "].uwMSGCTR & 0x0003) == 0x0002)         // if INTPND "
            Print #f, "    {"
      ' if Receive interrupt for object iObj is enabled
      If (r.v(sRegPre & "MSGCFGH" & iObj & "_RXINP") = iSRN And r.v(sRegPre & "MSGCTRL" & iObj & "_RXIE") = 2) Then
         If (iObj < 16) Then
            Print #f, "      if(" & sRegPre & "RXIPNDL & " & sRegPre & "RXIPNDL_RXIPND" & iObj & ")   // message object " & iObj & " receive interrupt"
         Else
            Print #f, "      if(" & sRegPre & "RXIPNDH & " & sRegPre & "RXIPNDH_RXIPND" & iObj & ")   // message object " & iObj & " receive interrupt"
         End If
            Print #f, "      {"
            Print #f, ""
        ' if Receive Object => data frame received
        If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 0) Then
            Print #f, "        if((CAN_HWOBJ[" & iObj & "].uwMSGCTR & 0x0300) == 0x0200)     // if NEWDAT is set"
            Print #f, "        {"
            Print #f, ""
            Print #f, "          if ((CAN_HWOBJ[" & iObj & "].uwMSGCTR & 0x0c00) == 0x0800)  // 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, "            CAN_HWOBJ[" & iObj & "].uwMSGCTR = 0xf7ff;  // reset MSGLST"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",1)",12)
            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)",12)
            Print #f, "          }"
            Print #f, ""
            Print #f, "          CAN_HWOBJ[" & iObj & "].uwMSGCTR = 0xfdff;    // reset NEWDAT"
            Print #f, "        }"
        End If
        
        ' if Transmit Object => Remote frame received
        If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 1) Then
            Print #f, "        if((CAN_HWOBJ[" & iObj & "].uwMSGCTR & 0xc000) == 0x8000)     // if RMTPND is set"
            Print #f, "        {"
            Print #f, "          // The CAN controller has received a remote frame"
            Print #f, "          // with the same identifier as this object."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",3)",10)
            Print #f, ""
            Print #f, "        }"
        End If
            Print #f, ""
            Print #f, "      }  // End of RXIPND" & iObj
            Print #f, ""
      End If
    
      ' if Transmit interrupt for object iObj is enabled
      If (r.v(sRegPre & "MSGCFGH" & iObj & "_TXINP") = iSRN And r.v(sRegPre & "MSGCTRL" & iObj & "_TXIE") = 2) Then
         If (iObj < 16) Then
            Print #f, "      if(" & sRegPre & "TXIPNDL & " & sRegPre & "TXIPNDL_TXIPND" & iObj & ")   // message object " & iObj & " transmit interrupt"
         Else
            Print #f, "      if(" & sRegPre & "TXIPNDH & " & sRegPre & "TXIPNDH_TXIPND" & iObj & ")   // message object " & iObj & " transmit interrupt"
         End If
            Print #f, "      {"
            Print #f, ""
       
        ' if Transmit Object => data frame transmitted
        If (r.v(sRegPre & "MSGCFGL" & iObj & "_DIR") = 1) Then
            Print #f, "        // The transmission of the last message object"
            Print #f, "        // was successful."
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",4)",8)
            Print #f, ""
            Print #f, "        CAN_HWOBJ[" & iObj & "].uwMSGCTR = 0xfdff;      // reset NEWDAT"
            Print #f, ""
        End If
        
        ' if Receive Object => remote frame transmitted
        If (r.v(sRegPre & "MSGCFGL" & 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 & ",5)",8)
            Print #f, ""
        End If
            Print #f, "      }  // End of TXIPND" & iObj
      End If
            Print #f, ""
            Print #f, "      CAN_HWOBJ[" & iObj & "].uwMSGCTR = 0xfffd;        // reset INTPND"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",6)",6)
            Print #f, ""
            Print #f, "    }"
    End If
    Exit Sub
    
GenerateSRNObjxError:
    Exit Sub
End Sub
