'#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:
'        WDT
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'WDT'.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         14.02.00     final version for C165UTAH
'
'        ---------------------------------------------------------------------
'        2.0   ahe         14.06.00     final version for C167CS, C161CS/JC/JI
'             
'        ---------------------------------------------------------------------
'        2.1   ahe         02.11.00     final version for C167CS, C161CS/JC/JI
'                                       - define for SRVWDT
'                                         ( _srvwdt_() OR _srvwdt() ) in main.h
'
'        ---------------------------------------------------------------------
'        4.0   ahe         24.04.01     final version for Pegasus
'                                       - for input frequency selection is
'                                         only bitfild WDTIN
'        4.1   ste         06.01.03     WDTCON must be set in the startup file
'
'*****************************************************************************


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


    ' write files WDT.h
    If (r.v("WDT_USE_Init") = 1) Then
      If (Not WDTGenerateH(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s("WDT_HFILE"))) Then Exit Function
    End if

    ' write file WDT.c
    If (r.v("WDT_USE_Init") = 1) Then
      If (Not WDTGenerateC(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s("WDT_CFILE"))) Then Exit Function
    End if

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


'*****************************************************************************
'Description:
'        Generates the code for the WDT.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function WDTGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo WDTGenerateCError
    
    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 uTime   As UnitType
    Dim lValue  As Long
    Dim sVarPre As String

    sVarPre = "WDT"
    
    WDTGenerateC = 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'
    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, ""
    
    '------------------------------------------------------------------------------------------------
    ' WDT_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, ""

      If (r.v(sVarPre & "CON_WDTIN") = 0) Then
          lValue = 2
      ElseIf (r.v(sVarPre & "CON_WDTIN") = 1) Then
          lValue = 128
      ElseIf (r.v(sVarPre & "CON_WDTIN") = 2) Then
          lValue = 4
      ElseIf (r.v(sVarPre & "CON_WDTIN") = 3) Then
          lValue = 256
      End If

      d = "watchdog timer input frequency is Fpd / " & lValue
      Print #f, FormatCommentsForInit(1, d)

      d = "watchdog timer reload value is " & FormatTextOutput(r.v("WDTCON_WDTREL"), "H2")
      Print #f, FormatCommentsForInit(1, d)
      
      uTime = ConvertTime((2 ^ 16 - r.v(sVarPre & "CON_WDTREL") * 2 ^ 8) * _
        lValue / GetPeripheralClock(r))

      d = "watchdog timer overflow period is " & Format(uTime.dValue, "0.000") & " " & uTime.sUnit
      Print #f, FormatCommentsForInit(1, d)
      Print #f, ""

      'v = 0
      'v = v + r.sfr("WDTCON")
      'Print #f, FormatCodeStatemente(2, "WDTCON", "=", FormatTextOutput(v, "H4"), "load WDT control register")            
      'Print #f, ""

      d = "this register must be set in the start-up file"
      Print #f, FormatCommentsForSource(2, 2, d)
      d = "WDTCON  =  " & FormatTextOutput(r.sfr("WDTCON"), "H4")
      Print #f, FormatCommentsForSource(2, 2, d)
      Print #f, ""
      
      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

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

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


'*****************************************************************************
'Description:
'        Generates the code for the WDT.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function WDTGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo WDTGenerateHError
    
    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 = "WDT"
   
    WDTGenerateH = 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 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 inserted 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 used variable of the function is selected and is really a function
      ' neither a macro nor an 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'
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' WDT_USE_TriggerWDT macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "TriggerWDT"
    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) & " SRVWDT"
    End If

    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
    
    WDTGenerateH = True
    Exit Function
    
WDTGenerateHError:
    Close f
    Exit Function
End Function

