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

Option Explicit

'*****************************************************************************
' Module:
'        OSC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'OSC'.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0   ahe         27.04.00     final version for C165UTAH
'
'        ----------------------------------------------------------------------
'        2.0   ahe         05.09.00     final version for C167CS, C161PI, C161CS/JC/JI
'             
'        ----------------------------------------------------------------------
'        3.0   ahe         31.10.00     final version for 
'                                         C167CS
'                                         C161PI
'                                         C161CS/JC/JI
'                                         C167CR/SR r2
'
'                                       - check ISNC
'
'
'        ----------------------------------------------------------------------
'        4.0   ahe         07.08.01     final version for 
'                                         C167CS
'                                         C161PI
'                                         C161CS/JC/JI
'                                         C167CR/SR r2
'                                         Pegasus
'                                       - add user code in SECTION_GLOBAL_PROTOTYPES
'
'*****************************************************************************


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


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

    ' write file OSC.c
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not OSCGenerateC(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 OSC failed", Error$)
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the OSC.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OSCGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo OSCGenerateCError
    
    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
    sRegPre = "OSC"
    sVarPre = "OSC"


    
    OSCGenerateC = 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 OSC.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, ""
    
    '------------------------------------------------------------------------------------------------
    ' OSC_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, ""

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


      If (r.regs.SfrFind("ISNC") = True) Then
          If (r.v("ISNC_RTCIE") = 0 Or r.v("RTC_USE_Init") = 0) Then
            Print #f, "  ISNC_PLLIE  =  1;   //enable PLL interrupt in sub-node register"
            Print #f, ""
            Call GenerateIR(f, r, "XP3IC", "XP3IE", "OSC_PLL_OWD")
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",3)",2) 
            Print #f, ""
          Else
            d = "see configuration of RTC interrupt"
            Print #f, FormatCommentsForInit(1, d)

          End If
      Else
          Print #f, ""
          Call GenerateIR(f, r, "XP3IC", "XP3IE", "OCS_PLL_OWD")
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",3)",2) 
          Print #f, ""
      End If

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' OSC_viUnlockOWD function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "UnlockOWD"
    If (r.regs.SfrFind("ISNC") = True) Then
        If (r.v("ISNC_PLLIE") = 1) Then  
          If (IsISRSelected(r, sVarPre, "XP3IC", "XP3IE", 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, ""

            If (r.v("ISNC_RTCIE") = 0 Or r.v("RTC_USE_Init") = 0) Then

              Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), "XP3INT")
              Print #f, "{"
              Print #f, "  ISNC_PLLIR = 0;"
              Print #f, ""
              Print #f, uc.GetUserCode("(" & n & ",2)",2)
              Print #f, ""
              Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
              Print #f, ""

            End If

            Print #f, ""
          End If
        End If

    Else

        If (IsISRSelected(r, sVarPre, "XP3IC", "XP3IE", 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), "XP3INT")
          Print #f, "{"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",2)",2)
          Print #f, ""
          Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
          Print #f, ""
        End If

    End If


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

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


'*****************************************************************************
'Description:
'        Generates the code for the OSC.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OSCGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo OSCGenerateHError
    
    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 l As Long
    Dim v As Variant


    sVarPre = "OSC"
   
    OSCGenerateH = 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 & "_HFILE"))
    
    'open OSC.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'


    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
    Print #f, SECTION_INTERRUPT_VECTORS
    Print #f, ""  'DAvE interrupt vector definitions

    n = "UnlockOWD"
    If (IsISRSelected(r, sVarPre, "XP3IC", "XP3IE", n)) Then
      v = 67
      Print #f, "#define XP3INT    " & FormatTextOutput(v, "H2")
    End If

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)",0)
    Print #f, ""
    Print #f, ""
    Print #f, "#endif  // ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"

    'close file
    Close f
    
    OSCGenerateH = True
    Exit Function
    
OSCGenerateHError:
    Close f
    Exit Function
End Function

