Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\ModuleClass.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
'        ---------------------------------------------------------------------
'        1.0   Jagadeesha    03.05.07     XC22xx v0.01
'*****************************************************************************
Global Const F_P_TARGET = 4
Global Const F_VCO_TARGET = 160
Global F_P As Variant
Global F_VCO As Variant
Global PDIV As Variant
Global NDIV As Variant
Global K2DIV As Variant

'*****************************************************************************
' @Constants
'*****************************************************************************

'*****************************************************************************
' @Function descriptions
'*****************************************************************************

'*****************************************************************************
'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
'        "DUMMY_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(vIndex As Variant, 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, 3 => TRAP)

    ' 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."
  sReturnValue = "None"
  If (Not func.AddFunction(sIndex & "_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for CSCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sDescription = "This function makes it possible to write one protected register. After calling of this function " & _
                 "and write on the protected register is the security level set to low protected mode."
'------------------------------------------------------------------------------------------------
' Description for CSCU_NAME_ ...
'------------------------------------------------------------------------------------------------
 sDescription = "This function makes it possible to write one protected register."
                 
 sReturnValue = "None"
 If (Not func.AddFunction(sIndex & "_NAME_UnlockProtecReg", "void", sDescription, sReturnValue, 1)) Then Exit Function


'------------------------------------------------------------------------------------------------
' Description for CSCU_NAME_ ...
'------------------------------------------------------------------------------------------------
 sDescription = "This function makes it possible to lock one protected register."
                 
 sReturnValue = "None"
 If (Not func.AddFunction(sIndex & "_NAME_LockProtecReg", "void", sDescription, sReturnValue, 1)) Then Exit Function
'------------------------------------------------------------------------------------------------
' Description for CSCU_NAME_ ...
'------------------------------------------------------------------------------------------------
 sDescription = "This function is used to select the external crystal and" & vbCrLf & _
                "configure the system frequency to 80Mhz/66Mhz."
                 
 sReturnValue = "None"
 If (Not func.AddFunction(sIndex & "_NAME_ChangeFreq", "void", sDescription, sReturnValue, 1)) Then Exit Function

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

  RefreshFunctionDescriptions = True
End Function

'*****************************************************************************
' @Functions
'*****************************************************************************

'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the pin #EA = 0
'        e.g. fetch code from internal ROM
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        PinName         Alternatpinname
'        lowIndex        low index
'        highIndex       high index
'
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipIfInternROM(r As ProjectData) As String

    GetTooltipIfInternROM = FormatDisabledTooltip("fetch code from internal ROM (#EA = high during reset)")

End Function


'*****************************************************************************
' @Functions
'*****************************************************************************

'*****************************************************************************
'Description:
'        This function returns a P,N & K2 divider value of PLL
'        PDIV,NDIV,K2DIV
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        None
'*****************************************************************************
Public Function Get_PLLParameters(r As ProjectData)
  On Error Resume Next

  Dim vTmp As Variant
  
  'Calculate P divider value
  vTmp = Fix((r.v("MAIN_OSC") + F_P_TARGET - 1) / F_P_TARGET)
  
  If ((r.v("MAIN_OSC") / vTmp) < F_P_TARGET) Then
      vTmp = vTmp - 1
  End If
  
  F_P = r.v("MAIN_OSC") / vTmp
 
  PDIV = vTmp
  
  'Calculate K2 divider value
  vTmp = Fix(F_VCO_TARGET / r.v("MAIN_CLK_CPU"))
   
  ' Added to limit the value of K2 in such a way that K2 * SysFreq < 160 MHz 13.09.07
  If ((r.v("MAIN_CLK_CPU") * vTmp) >= F_VCO_TARGET) Then
       vTmp = vTmp - 1
  End If
  
  K2DIV = vTmp
  
  'Calculate N divider value
  vTmp = Fix(((r.v("MAIN_CLK_CPU") * K2DIV) + F_P / 2) / F_P)
 
  If (F_P * vTmp > F_VCO_TARGET) Then
      vTmp = vTmp - 1
  End If
  
  NDIV = vTmp
  
End Function

'*****************************************************************************
'Description:
'          This function update the output frequency on port FOUT
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'       None
'
'*****************************************************************************
Sub UpdateOutputFrequency(r As ProjectData)
  On Error Resume Next
  Dim f As Double

  f = r.v("SCU_ReqFreq")
  If (f < GetMinOutputFrequency(r)) Then
    f = GetMinOutputFrequency(r)
  End If
  If (f > GetMaxOutputFrequency(r)) Then
    f = GetMaxOutputFrequency(r)
  End If
  r.v("SCU_ReqFreq") = f

  r.l("SCU_EXTCON_FORV") = GetSystemClock(r) / (f * 2 ^ (1 - r.v("SCU_EXTCON_FOSS"))) - 1

End Sub

'*****************************************************************************
'Description:
'          This function returns the minimum output frequency on port FOUT
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      minimum possible output frequency [MHz]
'
'*****************************************************************************
Function GetMinOutputFrequency(r As ProjectData)
  On Error Resume Next
  Dim f As Double

  f = GetSystemClock(r) / (64 * 2 ^ (1 - r.v("SCU_EXTCON_FOSS")))
  GetMinOutputFrequency = f
  
End Function
'*****************************************************************************
'Description:
'          This function returns the maximum output frequency on port FOUT
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      maximum possible output frequency [MHz]
'
'*****************************************************************************
Function GetMaxOutputFrequency(r As ProjectData)
  On Error Resume Next
  Dim f As Double

  f = GetSystemClock(r) / (2 ^ (1 - r.v("SCU_EXTCON_FOSS")))
  GetMaxOutputFrequency = f
  
End Function
'*****************************************************************************
'Description:
'          This function returns the real output frequency on port FOUT
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      real output frequency [MHz]
'
'*****************************************************************************
Function GetRealOutputFrequency(r As ProjectData)
  On Error Resume Next
  Dim f As Double

  f = GetSystemClock(r) / ((r.v("SCU_EXTCON_FORV") + 1) * 2 ^ (1 - r.v("SCU_EXTCON_FOSS")))
  GetRealOutputFrequency = f
  
End Function

'*****************************************************************************
'Description:
'          This function converts all module's *.C & *.H files to *.c & *.h
'          files for Tasking Viper compiler
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      converts file extension to small letters
'
'*****************************************************************************
Function ConvertFileExtension(r As ProjectData)
  On Error Resume Next
  Dim dc As DAvECollection
  Dim mo As ModuleClass
  Set dc = ObjectCollection("MODULENAMES")
        
    r.v("MAIN_CFile") = Left(r.v("MAIN_CFile"), Len(r.v("MAIN_CFile")) - 1) & LCase(Right(r.v("MAIN_CFile"), 1))
    r.v("MAIN_HFile") = Left(r.v("MAIN_HFile"), Len(r.v("MAIN_HFile")) - 1) & LCase(Right(r.v("MAIN_HFile"), 1))
    r.v("SCS_CFile") = Left(r.v("SCS_CFile"), Len(r.v("SCS_CFile")) - 1) & LCase(Right(r.v("SCS_CFile"), 1))
    r.v("SCS_HFile") = Left(r.v("SCS_HFile"), Len(r.v("SCS_HFile")) - 1) & LCase(Right(r.v("SCS_HFile"), 1))
    r.v("XC22xxREGS_HFile") = Left(r.v("XC22xxREGS_HFile"), Len(r.v("XC22xxREGS_HFile")) - 1) & LCase(Right(r.v("XC22xxREGS_HFile"), 1))
    r.v("XE16xREGS_HFile") = Left(r.v("XE16xREGS_HFile"), Len(r.v("XE16xREGS_HFile")) - 1) & LCase(Right(r.v("XE16xREGS_HFile"), 1))
        
    r.v("USIC0_CFILE") = Left(r.v("USIC0_CFILE"), Len(r.v("USIC0_CFILE")) - 1) & LCase(Right(r.v("USIC0_CFILE"), 1))
    r.v("USIC0_HFILE") = Left(r.v("USIC0_HFILE"), Len(r.v("USIC0_HFILE")) - 1) & LCase(Right(r.v("USIC0_HFILE"), 1))
    r.v("USIC1_CFILE") = Left(r.v("USIC1_CFILE"), Len(r.v("USIC1_CFILE")) - 1) & LCase(Right(r.v("USIC1_CFILE"), 1))
    r.v("USIC1_HFILE") = Left(r.v("USIC1_HFILE"), Len(r.v("USIC1_HFILE")) - 1) & LCase(Right(r.v("USIC1_HFILE"), 1))
    r.v("USIC2_CFILE") = Left(r.v("USIC2_CFILE"), Len(r.v("USIC2_CFILE")) - 1) & LCase(Right(r.v("USIC2_CFILE"), 1))
    r.v("USIC2_HFILE") = Left(r.v("USIC2_HFILE"), Len(r.v("USIC2_HFILE")) - 1) & LCase(Right(r.v("USIC2_HFILE"), 1))
    
    r.v("U0C0_CFILE") = Left(r.v("U0C0_CFILE"), Len(r.v("U0C0_CFILE")) - 1) & LCase(Right(r.v("U0C0_CFILE"), 1))
    r.v("U0C0_HFILE") = Left(r.v("U0C0_HFILE"), Len(r.v("U0C0_HFILE")) - 1) & LCase(Right(r.v("U0C0_HFILE"), 1))
    r.v("U0C1_CFILE") = Left(r.v("U0C1_CFILE"), Len(r.v("U0C1_CFILE")) - 1) & LCase(Right(r.v("U0C1_CFILE"), 1))
    r.v("U0C1_HFILE") = Left(r.v("U0C1_HFILE"), Len(r.v("U0C1_HFILE")) - 1) & LCase(Right(r.v("U0C1_HFILE"), 1))
     
    r.v("U1C0_CFILE") = Left(r.v("U1C0_CFILE"), Len(r.v("U1C0_CFILE")) - 1) & LCase(Right(r.v("U1C0_CFILE"), 1))
    r.v("U1C0_HFILE") = Left(r.v("U1C0_HFILE"), Len(r.v("U1C0_HFILE")) - 1) & LCase(Right(r.v("U1C0_HFILE"), 1))
    r.v("U1C1_CFILE") = Left(r.v("U1C1_CFILE"), Len(r.v("U1C1_CFILE")) - 1) & LCase(Right(r.v("U1C1_CFILE"), 1))
    r.v("U1C1_HFILE") = Left(r.v("U1C1_HFILE"), Len(r.v("U1C1_HFILE")) - 1) & LCase(Right(r.v("U1C1_HFILE"), 1))
    
    r.v("U2C0_CFILE") = Left(r.v("U2C0_CFILE"), Len(r.v("U2C0_CFILE")) - 1) & LCase(Right(r.v("U2C0_CFILE"), 1))
    r.v("U2C0_HFILE") = Left(r.v("U2C0_HFILE"), Len(r.v("U2C0_HFILE")) - 1) & LCase(Right(r.v("U2C0_HFILE"), 1))
    r.v("U2C1_CFILE") = Left(r.v("U2C1_CFILE"), Len(r.v("U2C1_CFILE")) - 1) & LCase(Right(r.v("U2C1_CFILE"), 1))
    r.v("U2C1_HFILE") = Left(r.v("U2C1_HFILE"), Len(r.v("U2C1_HFILE")) - 1) & LCase(Right(r.v("U2C1_HFILE"), 1))
      
    For Each mo In dc
      If (mo.ModuleName <> "MAIN" And mo.ModuleName <> "LDF" And mo.ModuleName <> "ECU" And mo.ModuleName <> "ADC Clock" And mo.ModuleName <> "U0C0" And mo.ModuleName <> "U0C1" And mo.ModuleName <> "U1C0" And mo.ModuleName <> "U1C1" And mo.ModuleName <> "U2C0" And mo.ModuleName <> "U2C1") Then
        If (mo.ModuleWithCG) Then
          If (r.v(mo.ModuleName & "_USE_Init") = 1) Then
              r.v(mo.ModuleName & "_CFile") = Left(r.v(mo.ModuleName & "_CFile"), Len(r.v(mo.ModuleName & "_CFile")) - 1) & LCase(Right(r.v(mo.ModuleName & "_CFile"), 1))
              r.v(mo.ModuleName & "_HFile") = Left(r.v(mo.ModuleName & "_HFile"), Len(r.v(mo.ModuleName & "_HFile")) - 1) & LCase(Right(r.v(mo.ModuleName & "_HFile"), 1))
          End If
        End If
      End If
    Next mo

End Function
