Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
Option Explicit
'*****************************************************************************
' Module:
'        MAIN
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        MAIN dialog script and in the MAIN codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  als            25-10-04    final version
'        0.2 radhish         31-12-04    Range of system freguency updated
'
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************
Const PCLK_MIN = 2

' use this constant for Keil Startup
Global Const MSG_KEIL_STARTUP = "Disabling of startup generation would affect by not generatig start up file." & vbCrLf & _
                                "So changes in External OSC Configurations will not be affected and inturn " & vbCrLf & _
                                "this might affect functioning of Modules." & vbCrLf & _
                                "Do you want to continue?"
'*****************************************************************************
'Description:
'        For each function which should be used for this module a line
'        "If (Not func.AddFunction .." must be insert in this function.
'        For each parameter that should be used for this function a
'        line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added function a project variable with the name
'        "MAIN_NAME_Functionname" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next

RefreshFunctionDescriptions = False

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sIndex As String
  Dim sPrefix As String

  sIndex = "MAIN"

  ' for each function of this module the property AddFunction must be called
  '
  ' AddFunction(sFuncName As String, sFuncType As String, sFuncDescription As String,
  '             sReturnvalue As String, lSort As Long) As Boolean
  '
  ' sFuncName          | String | function name as key (use the name of the variable
  '                               for the function name e.g. SSC_NAME_Init)
  ' sFuncType          | String | type of the function
  ' sFuncDescription   | String | description of the function
  ' sReturnvalue       | String | description of the return value
  ' lSort              | Long   | ' sort of the function (0 => macro, 1 => function, 2 => ISR)

    ' for each parameter of a function the property AddParameter must be called
    '
    'AddParameter(sParName As String, sPar As String, sParType As String, sParDescription As String) As Boolean
    '
    ' sParName         | String | parameter name (key)
    ' sPar             | String | parameter name
    ' sParType         | String | type of the parameter
    ' sParDescription  | String | description of the parameter


  '------------------------------------------------------------------------------------------------
  ' Description for MAIN_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = "This function initializes the microcontroller. " & _
                 "It is assumed that the SFRs are in their reset state."
  sReturnValue = "None"
  If (Not func.AddFunction("MAIN_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for MAIN_NAME_Main
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the main function."
  sReturnValue = "None"
  If (Not func.AddFunction("MAIN_NAME_Main", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
        If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function


'*****************************************************************************
'Description:
'        This function returns the fsys clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fsys clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_FSYS(r As ProjectData) As Variant
    Dim dF As Double
    
    dF = r.v("MAIN_CLK_Frequency")

    If (dF < r.v("MAIN_OSC_MIN")) Then
        Get_FSYS = TO_LOW
    ElseIf (dF > r.v("MAIN_OSC_MAX")) Then
        Get_FSYS = TO_HIGH
    Else
        Get_FSYS = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the fextOSC clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fextOSC clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_fextOSC(r As ProjectData) As Variant
    Dim dF As Double
    
    dF = r.v("MAIN_OSC")

    If (dF < r.v("MAIN_EXT_MIN")) Then
        Get_fextOSC = TO_LOW
    ElseIf (dF > r.v("MAIN_EXT_MAX")) Then
        Get_fextOSC = TO_HIGH
    Else
        Get_fextOSC = Format(dF, "0.00")
    End If

End Function


'*****************************************************************************
'Description:
'        Peripheral Frequency
'        This function returns the fPCLK clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fPCLK clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_fPCLK(r As ProjectData) As Variant
    Dim dF As Double

    dF = r.v("MAIN_PER_CLK")

    If (dF < PCLK_MIN) Then
        Get_fPCLK = TO_LOW
    ElseIf (dF > (r.v("MAIN_OSC_MAX") / 3#)) Then
        Get_fPCLK = TO_HIGH
    Else
        Get_fPCLK = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'        Core Frequency
'        This function returns the fCCLK clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fCCLK clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_fCCLK(r As ProjectData) As Variant
    Dim dF As Double

    dF = r.v("MAIN_PER_CLK") * 3
    

    If (dF < (PCLK_MIN * 3)) Then
        Get_fCCLK = TO_LOW
    ElseIf (dF > r.v("MAIN_OSC_MAX")) Then
        Get_fCCLK = TO_HIGH
    Else
        Get_fCCLK = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'        CLKOUT Frequency
'        This function returns the CLKOUT frequency [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
Function Get_fsysCLKOUT(r As ProjectData) As Variant
    Dim dF As Double
    
    dF = r.v("MAIN_CLK_Frequency") / GetValueCOREL(r)
    
'    If (dF < r.v("MAIN_OSC_MIN")) Then
'        Get_fsysCLKOUT = TO_LOW
'    ElseIf (dF > r.v("MAIN_OSC_MAX")) Then
'        Get_fsysCLKOUT = TO_HIGH
    If (r.v(SCU_PREFIX & "COCON_COUTS") = 1) And (r.v(SCU_PREFIX & "COCON_TLEN") = 1) Then
       Get_fsysCLKOUT = Format((dF / 2), "0.00")
    Else
    Get_fsysCLKOUT = Format(dF, "0.00")
    End If

End Function


'*****************************************************************************
'Description:
'        The "UpdateProjectData" functions of all peripheral modules are
'        called with parameter "lngUpdateDescriptor" being passed down.
'
'        Restrictions:
'        - The first module in collection "MODULENAMES" is the main module
'          (which has no "UpdateProjectData" function implemented)
'        - All other modules in collection "MODULENAMES" contain a file
'          "Ini.dave" with a "UpdateProjectData" function
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data
'        lngUpdateDescriptor    identification of the update context (input)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function UpdatePeripherals(r As ProjectData, lngUpdateDescriptor As Long) As Boolean
  On Error GoTo UpdatePeripheralsError

  Dim lngIndex  As Long
  Dim cp        As DAvECollection
  Dim sc        As Script

  UpdatePeripherals = False
  Set cp = ObjectCollection("MODULENAMES")

  For lngIndex = 2 To cp.Count  ' skip the first module "main"
    Set sc = New Script
    sc.Load (DefGetPath("Scripts") & cp(lngIndex).ModuleDirectory & "\Ini.dave")
    If (Not sc.Functions.UpdateProjectData(r, cp(lngIndex).ParameterValue, lngUpdateDescriptor)) Then Exit Function
  Next lngIndex

  UpdatePeripherals = True
  Exit Function

UpdatePeripheralsError:
  Exit Function
End Function
