Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.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
'        ---------------------------------------------------------------------
'        1.0   ahe         27.04.00     final version
'        1.1   ahe         20.06.00     final version for C167CS
'
'        ---------------------------------------------------------------------
'        5.0   ahe         31.07.00     final version for C161CS/JC/JI
'
'        ---------------------------------------------------------------------
'              ahe         23.10.00     final version for C167CS and C161CS/JC/JI
'
'        ---------------------------------------------------------------------
'        6.0   ahe         05.11.01     final version for Pegasus
'
'        6.1   ahe         05.06.02     no changes
'
'        6.2   ahe         23.08.02     no changes
'
'        6.3   ste         13.12.02     XC167CI added
'                                       GetMaxFosc changed to 40 MHz
'                                       max Fin to 40 MHz
'        6.4   ste         24.02.03     GetMaxFosc changed to 50 MHz (required
'                                       by Ursula Kelling), only Fcpu is limited to 40MHz
'        ---------------------------------------------------------------------
'              ste         29.09.03     new contoller types added
'
'        ---------------------------------------------------------------------
'        7.0                            XC164CM
'                                       - GetMaxFpd() added
'
'*****************************************************************************


'*****************************************************************************
' @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."
                  
  sReturnValue = "None"
  If (Not func.AddFunction(sIndex & "_NAME_UnlockProtecReg", "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
'    sDescription = "Data to be send"
'    If (Not func.AddParameter(sIndex & "_NAME_SendData_Par1", "usData", "ushort", sDescription)) 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


'*****************************************************************************
'Description:
'        This function returns the max fcpu [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        max fcpu [MHz]
'*****************************************************************************
Function GetMaxFcpu(r As ProjectData) As Integer
    Dim sMC_Name As String

    sMC_Name = DefGetValue("Name")

    GetMaxFcpu = 40

    If (sMC_Name = "XC164CM") Then
      If (r.v("MAIN_CPU_TYPE") = sMC_Name & "-8F20") Or (r.v("MAIN_CPU_TYPE") = sMC_Name & "-4F20") Then
        GetMaxFcpu = 20
      Else
        GetMaxFcpu = 40
      End If
    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the max fpd [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        max fpd [MHz]
'*****************************************************************************
Function GetMaxFpd(r As ProjectData) As Integer
    Dim sMC_Name As String

    sMC_Name = DefGetValue("Name")

    GetMaxFpd = 40
      
    If (sMC_Name = "XC164CM") Then
      If (r.v("MAIN_CPU_TYPE") = sMC_Name & "-8F20") Or (r.v("MAIN_CPU_TYPE") = sMC_Name & "-4F20") Then
        GetMaxFpd = 20
      Else
        GetMaxFpd = 40
      End If
    End If
      
End Function
'*****************************************************************************
'Description:
'        This function returns the min fcpu [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        min fcpu [MHz]
'*****************************************************************************
Function GetMinFcpu(r As ProjectData) As Integer

    GetMinFcpu = 4

End Function


'*****************************************************************************
'Description:
'        This function returns the max fpll [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        max fpll [MHz]
'*****************************************************************************
Function GetMaxFpll(r As ProjectData) As Integer
    Dim sMC_Name As String

    sMC_Name = DefGetValue("Name")

    GetMaxFpll = 50

'    If (sMC_Name = "XC164CS") Then
'      If (r.v("MAIN_CPU_TYPE") = sMC_Name & "-16F40") Then
'        GetMaxFpll = 40
'      ElseIf (r.v("MAIN_CPU_TYPE") = sMC_Name & "-16F20") Then
'        GetMaxFpll = 20
'      ElseIf (r.v("MAIN_CPU_TYPE") = sMC_Name & "-16F") Then ' for compatibility with oder versions
'        GetMaxFpll = 40
'      End If
'    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the min fpll [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        min fpll [MHz]
'*****************************************************************************
Function GetMinFpll(r As ProjectData) As Integer

    GetMinFpll = 4

End Function

'*****************************************************************************
'Description:
'        This function returns the max fosc [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        max fosc [MHz]
'*****************************************************************************
Function GetMaxFosc(r As ProjectData) As Integer

    GetMaxFosc = 50

End Function

'*****************************************************************************
'Description:
'        This function returns the min fosc [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        min fosc [MHz]
'*****************************************************************************
Function GetMinFosc(r As ProjectData) As Integer

    GetMinFosc = 4

End Function

'*****************************************************************************
'Description:
'        This function returns the fpll clock [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fpll clock [MHz]
'*****************************************************************************
Function GetFpll(r As ProjectData) As Double
    On Error Resume Next

    If (r.v("PLLCON_PLLCTRL") = 3) Then
        GetFpll = r.v("MAIN_OSC") * (r.v("PLLCON_PLLMUL") + 1) / _
                  (r.v("PLLCON_PLLIDIV") + 1) / _
                  (r.v("PLLCON_PLLODIV") + 1)
    Else
        GetFpll = r.v("MAIN_OSC") / _
                  (r.v("PLLCON_PLLIDIV") + 1) / _
                  (r.v("PLLCON_PLLODIV") + 1)
    End If


End Function


'*****************************************************************************
'Description:
'        This function returns the fin clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fin clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************
Function Get_FIN(r As ProjectData) As Variant
    Dim dF As Double
    
    dF = r.v("MAIN_OSC") / (1 + r.v("PLLCON_PLLIDIV"))

    If (dF < 4) Then
        Get_FIN = TO_LOW
'    ElseIf (dF > 25) Then
'    ElseIf (dF > 40) Then
    ElseIf (dF > 50) Then
        Get_FIN = TO_HIGH
    Else
        Get_FIN = Format(dF, "0.000")
    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the fvco clock [MHz], or strin if the fvco is too
'        low or to high, or n.a. if calculation not pissible
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fvco clock [MHz], or
'        string "to low" or "to high", or
'        n.a. if calculation not pissible
'*****************************************************************************
Function Get_FVCO(r As ProjectData) As Variant
    Dim dF        As Double
    Dim dVCO_min  As Double
    Dim dVCO_max  As Double
    
    If (IsNumeric(Get_FIN(r)) = True) Then
        dF = Get_FIN(r) * (1 + r.v("PLLCON_PLLMUL"))

        dVCO_min = 100 + 50 * r.v("PLLCON_PLLVB")
        dVCO_max = dVCO_min + 50

        If (dF < dVCO_min) Then
            Get_FVCO = TO_LOW
        ElseIf (dF > dVCO_max) Then
            Get_FVCO = TO_HIGH
        Else
            Get_FVCO = Format(dF, "0.000")
        End If

    Else
        Get_FVCO = CALCULATION_NOT_POSSIBLE
    End If


End Function


'*****************************************************************************
'Description:
'        This function returns the fpll clock [MHz], or strin if the fpll is too
'        low or to high, or n.a. if calculation not pissible
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fpll clock [MHz], or
'        string "to low" or "to high", or
'        n.a. if calculation not possible
'*****************************************************************************
Function Get_FPLL(r As ProjectData) As Variant
    Dim dF        As Double
    
    If (r.v("PLLCON_PLLCTRL") = 3) Then
        If (IsNumeric(Get_FVCO(r)) = True) Then
            dF = Get_FVCO(r) / (1 + r.v("PLLCON_PLLODIV"))

            If (dF < GetMinFpll(r)) Then
                Get_FPLL = TO_LOW
            ElseIf (dF > GetMaxFpll(r)) Then
                Get_FPLL = TO_HIGH
            Else
                Get_FPLL = Format(dF, "0.000")
            End If

        Else
            Get_FPLL = CALCULATION_NOT_POSSIBLE
        End If
    Else
        If (IsNumeric(Get_FIN(r)) = True) Then
            dF = Get_FIN(r) / (1 + r.v("PLLCON_PLLODIV"))

            If (dF < GetMinFpll(r)) Then
                Get_FPLL = TO_LOW
            ElseIf (dF > GetMaxFpll(r)) Then
                Get_FPLL = TO_HIGH
            Else
                Get_FPLL = Format(dF, "0.000")
            End If

        Else
            Get_FPLL = CALCULATION_NOT_POSSIBLE
        End If
    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the fcpu clock [MHz], or strin if the fcpu is too
'        low or to high, or n.a. if calculation not pissible
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fcpu clock [MHz], or
'        string "to low" or "to high", or
'        n.a. if calculation not pissible
'*****************************************************************************
Function Get_FCPU(r As ProjectData) As Variant
    Dim dF  As Double
    
    If (IsNumeric(Get_FPLL(r)) = True) Then
        dF = Get_FPLL(r) / (1 + r.v("SYSCON1_CPSYS"))
        If (dF < GetMinFcpu(r)) Then
            Get_FCPU = TO_LOW
        ElseIf (dF > GetMaxFcpu(r)) Then
            Get_FCPU = TO_HIGH
        Else
            Get_FCPU = Format(dF, "0.000")
        End If
    Else
        Get_FCPU = CALCULATION_NOT_POSSIBLE
    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the fpd clock [MHz], or string if the fpd is too
'        low or to high, or n.a. if calculation not pissible
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fpd clock [MHz], or
'        string "to low" or "to high", or
'        n.a. if calculation not pissible
'*****************************************************************************
Function Get_FPD(r As ProjectData) As Variant
    Dim dF        As Double
    
    If (IsNumeric(Get_FCPU(r)) = True) Then
        dF = Get_FCPU(r) / (1 + r.v("SYSCON1_CPPD"))

        Get_FPD = Format(dF, "0.000")

    Else
        Get_FPD = CALCULATION_NOT_POSSIBLE
    End If


End Function

