'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "Include.inc"

Option Explicit

'*****************************************************************************
' Module:
'        RTC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'RTC'.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.0  ahe            25.01.00    final version for C165 UTAH
'        2.1  ste            28.02.00    many changes for the TC1775  
'
'        ----------------------------------------------------------------------
'        4.0  ahe            24.04.01    final version Pegasus
'
'        4.1  ahe            08.08.02    no changes
'        ----------------------------------------------------------------------
'             ste            13.10.03    the XC164 uses the Main OSC as RTC
'                                        input clock
'
'*****************************************************************************


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


    ' write files RTC.h
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not RTCGenerateH(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s(sVarPre & "_HFILE"))) Then Exit Function
    End if

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

    Dim sVarPre   As String
    Dim sRegPre   As String

    sVarPre = "RTC" & vIndex
    sRegPre = sVarPre & "_"

    
    RTCGenerateC = 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 RTC.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'
    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, ""
    
    '------------------------------------------------------------------------------------------------
    ' RTC_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, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();       //unlock SYSCON0"
      Print #f, FormatCodeStatemente(2, "SYSCON0_RTCCM", "=", 0, "enable access to control bits")
      Print #f, ""
      '------------------------------------------------------------------------------------------------
      ' Configuration of the RTC Counters:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Counter:") 

      If (GetRTC_RefClk_Implementation(r, vIndex) = 1) Then        ' for XC164
        d = sVarPre & " input frequency is " & Format((r.v("MAIN_OSC") / 32)* 1000, "0.000") & " kHz"
      ElseIf (GetRTC_RefClk_Implementation(r, vIndex) = 2) Then    ' for XC167, XC161
        d = sVarPre & " input frequency is " & Format(r.v(sVarPre & "_OSC") * 1000, "0.000") & " kHz"
      End If

      Print #f, FormatCommentsForInit(1, d)
      If (r.v(sRegPre & "CON_PRE") = 1) Then
        d = "the input clock is divided by factor 8"
      Else
        d = "the input clock is not divided"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "CON_RUN") = 1) Then
        d = "counter is started after initialization"
      Else
        d = "counter is not started after initialization"
      End If
      Print #f, FormatCommentsForInit(1, d)
      Print #f, ""
    
      v = 0
      v = v + r.v(sRegPre & "CON_PRE") * 2
      Print #f, "  while((RTC_CON & 0x8000) == 0x0000);      // wait until access is possible"
      Print #f, FormatCodeStatemente(2, sRegPre & "CON", "=", FormatTextOutput(v, "H4"), "load " & sVarPre & " control register")

      Print #f, ""

      Overflow = ConvertTime(GetRealTimeBase(r, vIndex))
      d = "overflow period on T14:  " & Format(Overflow.dValue, "0.0000") & " [" & Overflow.sUnit & "]"
      Print #f, FormatCommentsForInit(1, d)
    
      Overflow = ConvertTime(GetRealTime(r, vIndex, 0))
      d = "overflow period on CNT0: " & Format(Overflow.dValue, "0.0000") & " [" & Overflow.sUnit & "]"
      Print #f, FormatCommentsForInit(1, d)
      
      Overflow = ConvertTime(GetRealTime(r, vIndex, 1))
      d = "overflow period on CNT1: " & Format(Overflow.dValue, "0.0000") & " [" & Overflow.sUnit & "]"
      Print #f, FormatCommentsForInit(1, d)
      
      Overflow = ConvertTime(GetRealTime(r, vIndex, 2))
      d = "overflow period on CNT2: " & Format(Overflow.dValue, "0.0000") & " [" & Overflow.sUnit & "]"
      Print #f, FormatCommentsForInit(1, d)
      
      Overflow = ConvertTime(GetRealTime(r, vIndex, 3))
      d = "overflow period on CNT3: " & Format(Overflow.dValue, "0.0000") & " [" & Overflow.sUnit & "]"
      Print #f, FormatCommentsForInit(1, d)
      Print #f, ""

      v = 0
      v = v + r.sfr(sRegPre & "T14REL")
      Print #f, FormatCodeStatemente(2, sRegPre & "T14REL", "=", FormatTextOutput(v, "H4"), "load T14 count/reload register")
      
      v = 0
      v = v + r.sfr(sRegPre & "RELL")
      Print #f, FormatCodeStatemente(2, sRegPre & "RTCL", "=", FormatTextOutput(v, "H4"), "load " & sVarPre & " low register")
      Print #f, FormatCodeStatemente(2, sRegPre & "RELL", "=", FormatTextOutput(v, "H4"), "load " & sVarPre & " reload low register")   

      v = 0
      v = v + r.sfr(sRegPre & "RELH")
      Print #f, FormatCodeStatemente(2, sRegPre & "RTCH", "=", FormatTextOutput(v, "H4"), "load " & sVarPre & " high register")
      Print #f, FormatCodeStatemente(2, sRegPre & "RELH", "=", FormatTextOutput(v, "H4"), "load " & sVarPre & " reload high register")   
      Print #f, ""


      '------------------------------------------------------------------------------------------------
      ' Configuration of the used RTC Interrupts:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Interrupts:")

      Call GenerateIR(f, r, sRegPre & "IC", "RTC")

      v = 0
      v = v + r.sfr(sRegPre & "ISNC")
      Print #f, FormatCodeStatemente(2, sRegPre & "ISNC", "=", FormatTextOutput(v, "H4"), "load " & sVarPre & " interrupt sub node register")

      Print #f, ""

      If (r.v(sRegPre & "CON_RUN") = 1) Then
        Print #f, FormatCodeStatemente(2, sRegPre & "CON_RUN", "=", 1, "starts the counter")
      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


    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_vRTCRun function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "RTCRun"
    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, ""
      Print #f, "  " & sRegPre & "CON_RUN = 1;"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_vRTCStop function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "RTCStop"
    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, ""
      Print #f, "  " & sRegPre & "CON_RUN = 0;"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_RTC function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "RTC"
    If (IsISRSelected(r, sVarPre, sRegPre & "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 & "INT")
        Print #f, "{"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",2)",2)
        Print #f, ""
        If (r.v(sRegPre & "ISNC_T14IE") = 1) Then
          Print #f, "  if(" & sRegPre & "ISNC_T14IR)             // if counter T14 overflow"
          Print #f, "  {"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",3)",4)
          Print #f, ""
          Print #f, "    " & sRegPre & "ISNC_T14IR = 0;"
          Print #f, ""
          Print #f, "  }"
          Print #f, ""
        End If

        For i = 0 To 3
          If (r.v(sRegPre & "ISNC_" & i & "IE") = 1) Then
            Print #f, "  if(" & sRegPre & "ISNC_" & i & "IR)            // if counter CNT" & i & " overflow"
            Print #f, "  {"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & "," & 4 + i & ")",4)
            Print #f, ""
            Print #f, "    " & sRegPre & "ISNC_" & i & "IR = 0;"
            Print #f, ""
            Print #f, "  }"
            Print #f, ""
          End If
        Next i

        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",8)",2)
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_DecrementT14 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "DecrementT14"
    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, "  "
      Print #f, "  if(" & sRegPre & "T14REL < 0xFFFF)"
      Print #f, "  {"
      Print #f, "    " & sRegPre & "CON_T14DEC = 1;"
      Print #f, "  }"
      Print #f, "  "
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_IncrementT14 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "IncrementT14"
    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, "  "
      Print #f, "  if(" & sRegPre & "T14REL < 0xFFFF)"
      Print #f, "  {"
      Print #f, "    " & sRegPre & "CON_T14INC = 1;"
      Print #f, "  }"
      Print #f, "  "
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_SetTime function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetTime"
    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, "  ulong tmp;
      Print #f, ""
      Print #f, "  tmp  = ulRegValue % 65536;"
      Print #f, "  " & sRegPre & "RELH = (ulRegValue >> 16);"
      Print #f, "  " & sRegPre & "RELL = tmp;"
      Print #f, ""
      Print #f, "  if(ubMode)"
      Print #f, "  {"
      Print #f, "    " & sRegPre & "RTCH = " & sRegPre & "RELH;"
      Print #f, "    " & sRegPre & "RTCL = " & sRegPre & "RELL;"
      Print #f, "  }"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_GetTime function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetTime"
    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, "  return(" & sRegPre & "RTCH * 65536 + " & sRegPre & "RTCL);
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_Reset function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Reset"
    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, "  " & sRegPre & "RTCL = 0x0000;
      Print #f, "  " & sRegPre & "RTCH = 0x0000;
      Print #f, "  " & sRegPre & "RELL = 0x0000;
      Print #f, "  " & sRegPre & "RELH = 0x0000;
      Print #f, ""
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_vSetSynchronousMode function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetSynchronousMode"
    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, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();       //unlock SYSCON0"
      Print #f, ""
      Print #f, FormatCodeStatemente(2, "SYSCON0_RTCCM", "=", 0, "enable access to control bits")
      Print #f, ""
      Print #f, "  while((RTC_CON & 0x8000) == 0x8000);    // wait until access is possible"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' RTC_vSetAsynchronousMode function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetAsynchronousMode"
    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, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();       //unlock SYSCON0"
      Print #f, ""
      Print #f, FormatCodeStatemente(2, "SYSCON0_RTCCM", "=", 1, "disable access to control bits")
      Print #f, ""
      Print #f, "  while((RTC_CON & 0x8000) == 0x0000);    // wait until access is not possible"
      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("(" & sVarPre & "_General,10)",0)
    Print #f, ""
    
    'close file
    Close f
    
    RTCGenerateC = True
    Exit Function
    
RTCGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the RTC.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function RTCGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo RTCGenerateHError
    
    Dim i       As Integer
    Dim f       As Variant
    Dim d       As String
    Dim dc      As DAvECollection
    Dim uc      As UserCode
    Dim func    As FunctionClass
    Dim n       As String
    Dim sVarPre As String
    Dim sRegPre As String
    Dim l       As Long
    Dim v       As Variant

    RTCGenerateH = False

    sVarPre = "RTC" & 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 RTC.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, uc.GetUserCode("(" & sVarPre & "_Header,8)",0)
    Print #f, ""
                          'DAvE declarations of global macros'

    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
    Print #f, SECTION_INTERRUPT_VECTORS
    Print #f, ""  'DAvE interrupt vector definitions
    n = "RTC"
    If (IsISRSelected(r, sVarPre, sRegPre & "IC", n)) Then
            v = 93
            Print #f, "#define " & sVarPre & "INT    " & FormatTextOutput(v, "H2")
    End If

    Print #f, ""
    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
    
    RTCGenerateH = True
    Exit Function
    
RTCGenerateHError:
    Close f
    Exit Function
End Function

