Attribute VB_Name = "Module1"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Port.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "Include.inc"
Option Explicit
'*****************************************************************************
' Module:
'        SSC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'SSC'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0    ahe       17.02.00     final version for
'                                         C165UTAH
'                                         C165H
'                                         C161U
'
'        ----------------------------------------------------------------------
'        2.0    ahe       05.09.00     final version for
'                                         C167CS
'                                         C161PI
'                                         C161CS/JC/JI
'
'
'        ----------------------------------------------------------------------
'        3.0    ahe       25.10.00     final version for
'                                         C167CS
'                                         C161PI
'                                         C161CS/JC/JI
'                                         C167CR/SR r2
'
'               ahe       13.02.01     - changed function CheckError
'
'        ----------------------------------------------------------------------
'        4.0    ahe       20.04.01     final version for Pegasus
'
'        4.1    ahe       08.08.02     - neu interrupt pages
'        4.2    ste       16.12.02     no changes
'        ----------------------------------------------------------------------
'        5.0    raj       14.08.04     final version for XC164CM Pegasus light
'                                      - Pin Selection Page added

'
'*****************************************************************************



'*****************************************************************************
'Description:
'        This functions initiates the code generation for the SSC 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 = "SSC" & vIndex

    ' write files SSC.h
    If (r.v(sVarPre & "_USE_Init")) Then
      If (Not SSCGenerateH(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s(sVarPre & "_HFILE"))) Then Exit Function
    End If

    ' write file SSC.c
    If (r.v(sVarPre & "_USE_Init")) Then
      If (Not SSCGenerateC(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s(sVarPre & "_CFILE"))) 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 SSC.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function SSCGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo SSCGenerateCError
    
    Dim i         As Integer
    Dim f         As Variant
    Dim v         As Variant
    Dim d         As String
    Dim n         As String
    Dim dc        As DAvECollection
    Dim uc        As UserCode
    Dim func      As FunctionClass
    Dim Baudrate  As UnitType
    Dim sAlternatePinName(1 To 6) As String

    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "SSC" & vIndex
    sRegPre = sVarPre & "_"

    
    SSCGenerateC = False

    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 SSC.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, 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_TransData") = 1) Then
      Print #f, "uword  *" & sVarPre & "_pusTxBuffer;"
      Print #f, "uword  *" & sVarPre & "_pusRxBuffer;"
      Print #f, "ubyte  " & sVarPre & "_ubTransferCounter;"
      Print #f, "ubyte  " & sVarPre & "_ubReceiveCounter;"
      Print #f, ""
    End If

    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, ""
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSC_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, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""

      Print #f, "  " & sRegPre & "CON_EN = 0;           //  enable access to control bits"
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the SSC Baud Rate Generator:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Baud Rate Generator:")

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

      Baudrate = ConvertBaudrate(GetSSCRealBaudrate(r, vIndex))
      d = "real baud rate     = " & Format(Baudrate.dValue, SSC_PRECISION) & " " & Baudrate.sUnit
      Print #f, FormatCommentsForInit(1, d)

      d = "deviation          = " & Format((GetSSCModuleClock(r, vIndex) * 1000000 / _
          (2 * (r.sfr(sRegPre & "BR") + 1)) - r.d(sVarPre & "_Baudrate")) / _
          r.d(sVarPre & "_Baudrate") * 100, "0.000") & " %"
      Print #f, FormatCommentsForInit(1, d)
      Print #f, ""
      
      '------------------------------------------------------SSCBR_SSCBR
      v = r.sfr(sRegPre & "BR")
      Print #f, FormatCodeStatemente(2, sRegPre & "BR", "=", FormatTextOutput(v, "H4"), _
                "load " & sVarPre & " baud rate time reload register")
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the SSC Operation Mode:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Operation Mode:")
      
      '------------------------------------------------------SSCCON_SSCMS
      If (r.v(sRegPre & "CON_MS") = 0) Then
        d = "this device is configured as " & sVarPre & " slave"
      Else
        d = "this device is configured as " & sVarPre & " master"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------------SSCCON_SSCBM_BC
      d = "transfer data width is " & (r.v(sRegPre & "CON_BM_BC") + 1) & " bit"
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------------SSCCON_SSCHB
      If (r.v(sRegPre & "CON_HB") = 0) Then
        d = "transfer/receive LSB first"
      Else
        d = "transfer/receive MSB first"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------------SSCCON_SSCPH
      If (r.v(sRegPre & "CON_PH") = 0) Then
        d = "shift transmit data on the leading clock edge, latch on trailing edge"
      Else
        d = "latch receive data on leading clock edge, shift on trailing edge"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------------SSCCON_SSCPO
      If (r.v(sRegPre & "CON_PO") = 0) Then
        d = "idle clock line is low, leading clock edge is low-to-high transition"
      Else
        d = "idle clock line is high, leading clock edge is high-to-low transition"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------------SSCCON_SSCTEN_TE
      If (r.v(sRegPre & "CON_MS") = 0) Then  ' slave mode
        If (r.v(sRegPre & "CON_TEN_TE") = 1) Then
          d = "check transmit error is enabled"
        Else
          d = "ignore transmit error"
        End If
        Print #f, FormatCommentsForInit(1, d)
      End If

      '------------------------------------------------------SSCCON_SSCREN_RE
      If (r.v(sRegPre & "CON_REN_RE") = 1) Then
        d = "check receive error is enabled"
      Else
        d = "ignore receive error"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------------SSCCON_SSCPEN_PE
      If (r.v(sRegPre & "CON_PEN_PE") = 1) Then
        d = "check phase error is enabled"
      Else
        d = "ignore phase error"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------------SSCCON_SSCBEN_BE
      If (r.v(sRegPre & "CON_MS") = 0) Then  ' slave mode
        If (r.v(sRegPre & "CON_BEN_BE") = 1) Then
          d = "check baudrate error is enabled"
          Print #f, FormatCommentsForInit(1, d)
          If (r.v(sRegPre & "CON_AREN_BSY") = 1) Then
            d = "" & sVarPre & " is automatically reset upon a baudrate error"
            Print #f, FormatCommentsForInit(1, d)
          End If
        Else
          d = "ignore baudrate error"
          Print #f, FormatCommentsForInit(1, d)
        End If
      End If

      Print #f, ""
    
      If (vIndex = 0) Then
          '------------------------------------------------------ if master mode
          If (r.v(sRegPre & "CON_MS") = 1) Then
              If (r.v(sRegPre & "CON_PO") = 1) Then
                  Print #f, "  P3        |= 0x2000;      //  load P3.13 output latch (SCLK) with the"
                  Print #f, "                            //  desired clock idle level(high)"
              Else
                  Print #f, "  P3        &= 0xDFFF;      //  load P3.13 output latch (SCLK) with the"
                  Print #f, "                            //  desired clock idle level(low)"
              End If
    
              If (r.v("ODP3_P13") = 1) Then
                  'Print #f, "  ODP3      |= 0x2000;      //  set P3.13 open drain control"
              End If
                  Print #f, "  ALTSEL0P3 |= 0x2000;      /// set ALtSEL0P3.P13"
                  Print #f, "  DP3       |= 0x2000;      /// set P3.13 direction control (SCLK output)"
                  Print #f, ""
          End If  ' if master mode
      End If

      '------------------------------------------------------load SSCCON
      v = 0
      v = v + r.v(sRegPre & "CON_EN") * 2 ^ 15
      v = v + r.v(sRegPre & "CON_MS") * 2 ^ 14
      v = v + r.v(sRegPre & "CON_AREN_BSY") * 2 ^ 12
      v = v + r.v(sRegPre & "CON_BEN_BE") * 2 ^ 11
      v = v + r.v(sRegPre & "CON_PEN_PE") * 2 ^ 10
      v = v + r.v(sRegPre & "CON_REN_RE") * 2 ^ 9
      v = v + r.v(sRegPre & "CON_TEN_TE") * 2 ^ 8
      v = v + r.v(sRegPre & "CON_PO") * 2 ^ 6
      v = v + r.v(sRegPre & "CON_PH") * 2 ^ 5
      v = v + r.v(sRegPre & "CON_HB") * 2 ^ 4
      v = v + r.v(sRegPre & "CON_BM_BC")

      Print #f, ""
      Print #f, FormatCodeStatemente(2, sRegPre & "CON", "=", FormatTextOutput(v, "H4"), _
                "load " & sVarPre & " control register")
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the used SSC Port Pins:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Port Pins:")
      v = 0

      If (vIndex = 0) Then
          If (r.v(sRegPre & "CON_MS") = 1 And r.v(sRegPre & "CON_PO") = 0) Then
                  Print #f, "  P3        |= 0x2000;      //  set P3.13 output latch (SCLK)"
                  Print #f, ""
          End If

          If (r.v(sRegPre & "CON_MS") = 1) Then   ' if master mode
                  Print #f, "  DP3       &= 0xFEFF;      /// reset P3.8 direction control (MRST input)"
                  Print #f, "  P3        |= 0x0200;      //  set P3.9 output latch (MTSR)"
              If (r.v("ODP3_P9") = 1) Then
                  'Print #f, "  ODP3      |= 0x0200;      //  set P3.9 open drain control"
              End If
                  Print #f, "  ALTSEL0P3 |= 0x0200;      /// set ALtSEL0P3.P9"
                  Print #f, "  DP3       |= 0x0200;      /// set P3.9 direction control (MTSR output)"
                  Print #f, ""
          End If  ' naster mode
    
    
          If (r.v(sRegPre & "CON_MS") = 0) Then   ' if slave mode
                  Print #f, "  DP3       &= 0xDDFF;      /// reset P3.9 direction control (MTSR input)"
                  Print #f, "                            /// reset P3.13 direction control (SCLK input)"
                  Print #f, "  P3        |= 0x0100;      //  set P3.8 output latch (MRST)"
              If (r.v("ODP3_P8") = 1) Then
                  'Print #f, "  ODP3      |= 0x0100;      //  set P3.8 open drain control"
              End If
                  Print #f, "  ALTSEL0P3 |= 0x0100;      /// set ALtSEL0P3.P8"
                  Print #f, "  DP3       |= 0x0100;      /// set P3.8 direction control (MRST output)"
                  Print #f, ""
          End If  ' slave mode
      Else
      
          sAlternatePinName(1) = sVarPre & "_MRST_I"
          sAlternatePinName(2) = sVarPre & "_MRST_O"
          sAlternatePinName(3) = sVarPre & "_MTSR_I"
          sAlternatePinName(4) = sVarPre & "_MTSR_O"
          sAlternatePinName(5) = sVarPre & "_SCLK_I"
          sAlternatePinName(6) = sVarPre & "_SCLK_O"

          Call SetAlternatePinStatements(f, r, sAlternatePinName())

      End If

      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the used SSC Interrupts:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Interrupts:")
      ' transmit interrupt
      Call GenerateIR(f, r, sRegPre & "TIC", "Tx")
      ' receive interrupt
      Call GenerateIR(f, r, sRegPre & "RIC", "Rx")
      ' error interrupt
      Call GenerateIR(f, r, sRegPre & "EIC", "Error")

      If (r.v(sVarPre & "_USE_TxBufFree") = 1) Then
        Print #f, ""
        '------------------------------------------------------------------------------------------------
        ' Default Settings for Service Request Flags:
        '------------------------------------------------------------------------------------------------
        Print #f, FormatSectionHeader(2, 0, "Default Settings for Service Request Flags:")
        Print #f, FormatCodeStatemente(2, sRegPre & "TIC_IR", "=", 1, "indicates that the transmit buffer is empty")
      End If
      
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",3)", 2)

      Print #f, ""
      Print #f, "} " & END_OF_FUNCTION & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSC_vSendData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SendData"
    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_Tx") = 0) Then
        Print #f, "  " & sRegPre & "TIC_IR = 0;        //  reset transmit interrupt request "
      End If
      
      Print #f, "  " & sRegPre & "TB  = uwData;   //  load transmit buffer register"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_uwGetData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetData"
    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_Rx") = 0) Then
        Print #f, "  " & sRegPre & "RIC_IR = 0;        // reset receive interrupt request"
      End If
      
      Print #f, "  return(" & sRegPre & "RB);     // return receive buffer register"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_ubErrorCheck function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ErrorCheck"
    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 ReturnValue;"
      Print #f, ""
      Print #f, "  ReturnValue = 0;"
      Print #f, ""
      Print #f, "  if(" & sRegPre & "EIC_IR)"
      Print #f, "  {"
      
      If (r.v(sRegPre & "CON_REN_RE") = 1) Then
        Print #f, "    if (" & sRegPre & "CON_REN_RE)  //  if receive error"
        Print #f, "    {"
        Print #f, "      " & sRegPre & "CON_REN_RE = 0;"
        Print #f, "      ReturnValue |= 0x01;"
        Print #f, "    }"
        Print #f, ""
      End If

      If (r.v(sRegPre & "CON_PEN_PE") = 1) Then
        Print #f, "    if (" & sRegPre & "CON_PEN_PE)  //  if phase error"
        Print #f, "    {"
        Print #f, "      " & sRegPre & "CON_PEN_PE = 0;"
        Print #f, "      ReturnValue |= 0x02;"
        Print #f, "    }"
        Print #f, ""
      End If

      If (r.v(sRegPre & "CON_BEN_BE") = 1) Then
        Print #f, "    if (" & sRegPre & "CON_BEN_BE)  //  if baud rate error"
        Print #f, "    {"
        Print #f, "      " & sRegPre & "CON_BEN_BE = 0;"
        Print #f, "      ReturnValue |= 0x04;"
        Print #f, "    }"
        Print #f, ""
      End If

      If (r.v(sRegPre & "CON_TEN_TE") = 1) Then
        Print #f, "    if (" & sRegPre & "CON_TEN_TE)  //  if transmit error"
        Print #f, "    {"
        Print #f, "      " & sRegPre & "CON_TEN_TE = 0;"
        Print #f, "      ReturnValue |= 0x08;"
        Print #f, "    }"
        Print #f, ""
      End If
      
      Print #f, "    " & sRegPre & "EIC_IR = 0;"
      Print #f, "  }"
      Print #f, ""
      Print #f, "  return(ReturnValue);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_viTx function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Tx"
    If (IsISRSelected(r, sVarPre, sRegPre & "TIC", 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 & "TINT")
      Print #f, "{"
      
      If (r.v(sVarPre & "_USE_TransData") = 1) Then
        Print #f, uc.GetUserCode("(" & n & ",2)", 2)
        Print #f, ""
        Print #f, "  if (" & sVarPre & "_ubTransferCounter)"
        Print #f, "  {"
        Print #f, "    " & sVarPre & "_ubTransferCounter --;"
        Print #f, "    " & sRegPre & "TB = *(++" & sVarPre & "_pusTxBuffer);"
        Print #f, "  }"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",3)", 2)
      Else
        Print #f, uc.GetUserCode("(" & n & ",4)", 2)
      End If
      
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_viRx function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Rx"
    If (IsISRSelected(r, sVarPre, sRegPre & "RIC", 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 & "RINT")
      Print #f, "{"
      
      If (r.v(sVarPre & "_USE_TransData") = 1) Then
        Print #f, uc.GetUserCode("(" & n & ",2)", 2)
        Print #f, ""
        Print #f, "  *(" & sVarPre & "_pusRxBuffer++) = " & sRegPre & "RB;"
        Print #f, "  " & sVarPre & "_ubReceiveCounter--;"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",3)", 2)
      Else
        Print #f, uc.GetUserCode("(" & n & ",4)", 2)
      End If
      
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_viError function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Error"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", 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 & "EINT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""

      Print #f, ""
      If (r.v(sRegPre & "CON_TEN_TE") = 1) Then
        Print #f, "  if (" & sRegPre & "CON_TEN_TE)  //  if transmit error"
        Print #f, "  {"
        Print #f, "    " & sRegPre & "CON_TEN_TE = 0;"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",3)", 4)
        Print #f, "  }"
        Print #f, ""
      End If

      If (r.v(sRegPre & "CON_REN_RE") = 1) Then
        Print #f, "  if (" & sRegPre & "CON_REN_RE)  //  if receive error"
        Print #f, "  {"
        Print #f, "    " & sRegPre & "CON_REN_RE = 0;"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",4)", 4)
        Print #f, "  }"
        Print #f, ""
      End If

      If (r.v(sRegPre & "CON_PEN_PE") = 1) Then
        Print #f, "  if (" & sRegPre & "CON_PEN_PE)  //  if phase error"
        Print #f, "  {"
        Print #f, "    " & sRegPre & "CON_PEN_PE = 0;"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",5)", 4)
        Print #f, "  }"
        Print #f, ""
      End If

      If (r.v(sRegPre & "CON_BEN_BE") = 1) Then
        Print #f, "  if (" & sRegPre & "CON_BEN_BE)  //  if baud rate error"
        Print #f, "  {"
        Print #f, "    " & sRegPre & "CON_BEN_BE = 0;"
        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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_vTransData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "TransData"
    If (r.v(sVarPre & "_USE_" & n) = 1 And _
        r.v(sRegPre & "TIC_ILVL") > 0 And _
        r.v(sRegPre & "TIC_ILVL") < 14 And _
        r.v(sRegPre & "RIC_ILVL") > 0 And _
        r.v(sRegPre & "RIC_ILVL") < 14) 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, "  " & sVarPre & "_pusTxBuffer = TxBuffer;"
      Print #f, "  " & sVarPre & "_pusRxBuffer = RxBuffer;"
      Print #f, "  " & sVarPre & "_ubTransferCounter = BufferSize;"
      Print #f, "  " & sVarPre & "_ubReceiveCounter  = BufferSize;"
      Print #f, ""
      Print #f, "  if(BufferSize)"
      Print #f, "  {"
      Print #f, "    " & sVarPre & "_ubTransferCounter--;"
      Print #f, "    " & sRegPre & "TB = *" & sVarPre & "_pusTxBuffer;"
      Print #f, "  }"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If



    '------------------------------------------------------------------------------------------------
    ' Other function
    '------------------------------------------------------------------------------------------------

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(SSC_General,10)", 0)
    Print #f, ""
    
    'close file
    Close f
    
    SSCGenerateC = True
    Exit Function
    
SSCGenerateCError:
    Close f
    Exit Function
End Function


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

    Dim i       As Integer
    Dim f       As Variant
    Dim v       As Variant
    Dim d       As String
    Dim n       As String
    Dim dc      As DAvECollection
    Dim uc      As UserCode
    Dim func    As FunctionClass
    Dim sVarPre As String
    Dim sRegPre As String
    Dim l       As Long
   
    sVarPre = "SSC" & vIndex
    sRegPre = sVarPre & "_"

    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 SSC.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'
    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'
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_ubTxBufFree macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "TxBufFree"
    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) & _
                " " & sRegPre & "TIC_IR"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_ubRxDataReady macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "RxDataReady"
    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) & _
                " " & sRegPre & "RIC_IR"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_ubBusy macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Busy"
    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) & _
                " " & sRegPre & "CON_AREN_BSY"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_vEnTransmitter macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "EnTransmitter"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)

      d = GetDirectionRegNameFromAlternatName(sVarPre & "_MRST_O")
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & d & " = 1"
      Print #f, ""
      Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_vDisTransmitter macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "DisTransmitter"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)

      d = GetDirectionRegNameFromAlternatName(sVarPre & "_MRST_O")
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & d & " = 0"

      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_vSetLoopBackMode macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetLoopBackMode"
    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) & _
                " " & sRegPre & "CON_LB = 1"

      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SSCx_vResetLoopBackMode macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ResetLoopBackMode"
    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) & _
                " " & sRegPre & "CON_LB = 0"

      Print #f, ""
      Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
    Print #f, SECTION_INTERRUPT_VECTORS
    Print #f, ""  'DAvE interrupt vector definitions
    
    n = "Tx"
    If (IsISRSelected(r, sVarPre, sRegPre & "TIC", n)) Then
      If (vIndex = 0) Then
        v = 45
      ElseIf (vIndex = 1) Then
        v = 81
      End If
      Print #f, "#define " & sVarPre & "TINT    " & FormatTextOutput(v, "H2")
    End If
    
    n = "Rx"
    If (IsISRSelected(r, sVarPre, sRegPre & "RIC", n)) Then
      If (vIndex = 0) Then
        v = 46
      ElseIf (vIndex = 1) Then
        v = 82
      End If
      Print #f, "#define " & sVarPre & "RINT    " & FormatTextOutput(v, "H2")
    End If
    
    n = "Error"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", n)) Then
      If (vIndex = 0) Then
        v = 47
      ElseIf (vIndex = 1) Then
        v = 83
      End If
      Print #f, "#define " & sVarPre & "EINT    " & FormatTextOutput(v, "H2")
    End If
    
    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
    
    SSCGenerateH = True
    Exit Function
    
SSCGenerateHError:
    Close f
    Exit Function
End Function

