Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\classes\FunctionClass.cls""
Option Explicit
'*****************************************************************************
' Module:
'        SCU
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        SCU dialog script and in the SCU codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        3.0   ahe          13.09.00    final version for C161CS/JC/JI (PM)
'
'        ----------------------------------------------------------------------
'        1.0   ahe          02.05.01    final version for Pegasus
'        1.1   ste          06.01.03    description correction
'
'        ----------------------------------------------------------------------
'        2.0   Raja         07.09.04    final version for XC164CM Pegasus light
'
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************

'*****************************************************************************
'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
'        "SCU_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

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sVarPre As String
  Dim sName As String
  Dim i As Integer
  
  sVarPre = "SCU"

  RefreshFunctionDescriptions = False

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

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "GetSystemStatus"
  sDescription = "This macro returns the contens of the system status register SYSSTAT. " & _
                 "For details see User's Manual."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "SetPowerDown"
  sDescription = "This function enters the power down mode. In this mode both CPU and all " & _
                 "peripherals are stopped. " & _
                 "This mode can only be terminated by an external hardware reset." & _
                 "" & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "This instruction is effective only if the #NMI pin is externally pulled low " & _
                 "while the PWRDN instruction is executed. The microcontroller will enter power " & _
                 "down mode after the PWRDN instruction has completed."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "SetIdle"
  sDescription = "This function enters the idle mode. " & _
                 "In this mode all enabled peripherals, including the watchdog " & _
                 "timer, continue to operate normally, only the CPU operation " & _
                 "is halted. " & _
                 "The idle mode is terminated by interrupt requests from any " & _
                 "enabled interrupt source whose individual interrupt enable " & _
                 "flag was set before the idle mode was entered, regardless of " & _
                 "bit IEN (globally interrupt enable bit). " & _
                 "For details see User's Manual."
                 
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "SetSleep"
  sDescription = "This function enters the sleep mode. " & _
                 "Clocking of all internal blocks (including WDT) is stopped. " & _
                 "Exit/Wakeup from sleep state is possible with all external interrupts, " & _
                 "with #NMI and with RTC interrupts."

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "EnableModule"
  sDescription = "This function enables on-chip peripherals." & vbCrLf & _
                 "" & vbCrLf & _
                 "The following definitions are available for uwModule:" & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_ADC") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_ASC0") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_SSC0") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_GPT1_2") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_PFM") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_CC2") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_CAN") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_SSC1") & vbCrLf & _
                 ""

                 If (r.Regs.BitFind("SYSCON3_SDLMDIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_SDLM") & vbCrLf & _
                     ""
                 End If
                 
                 If (r.Regs.BitFind("SYSCON3_CC1DIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_CC1") & vbCrLf & _
                     ""
                 End If

                 If (r.Regs.BitFind("SYSCON3_CC6DIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_CC6") & vbCrLf & _
                     ""
                 End If

                 If (r.Regs.BitFind("SYSCON3_I2CDIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_I2C") & vbCrLf & _
                     ""
                 End If

                 If (r.Regs.BitFind("SYSCON3_ASC1DIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_ASC1") & vbCrLf & _
                     ""
                 End If


  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Name of the module"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sName & "_Par1", "uwModule", "uword", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "DisableModule"
  sDescription = "This function disables on-chip peripherals that are not required in a given system " & _
                 "status. " & _
                 "" & vbCrLf & _
                 "The following definitions are available for uwModule:" & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_ADC") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_ASC0") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_SSC0") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_GPT1_2") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_PFM") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_CC2") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_CAN") & vbCrLf & _
                 "- " & r.v(sVarPre & "_PARA_SSC1") & vbCrLf & _
                 ""

                 If (r.Regs.BitFind("SYSCON3_SDLMDIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_SDLM") & vbCrLf & _
                     ""
                 End If
                 
                 
                 If (r.Regs.BitFind("SYSCON3_CC1DIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_CC1") & vbCrLf & _
                     ""
                 End If

                 If (r.Regs.BitFind("SYSCON3_CC6DIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_CC6") & vbCrLf & _
                     ""
                 End If

                 If (r.Regs.BitFind("SYSCON3_I2CDIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_I2C") & vbCrLf & _
                     ""
                 End If

                 If (r.Regs.BitFind("SYSCON3_ASC1DIS") = True) Then
                     sDescription = sDescription & _
                     "- " & r.v(sVarPre & "_PARA_ASC1") & vbCrLf & _
                     ""
                 End If

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Name of the module"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sName & "_Par1", "uwModule", "uword", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "StartCount"
  sDescription = "This macro starts the counting of the programmable output frequency"
                  
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "StopCount"
  sDescription = "This macro stops the counting of the programmable output frequency"
                  
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_ ...
  '------------------------------------------------------------------------------------------------
  sName = "SetReloadValue"
  sDescription = "This macro loads the reload value ( 0x00 .. 0x3F ) of the programmable output frequency"
                  
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "reload value 0x00 .. 0x3F"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sName & "_Par1", "uwValue", "", sDescription)) Then Exit Function

        If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function


'*****************************************************************************
'Description:
'        For each parameter that should be used as a constant (on page "Parameters")
'        a line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added parameter a project variable with the name
'        "DUMMY_PARA_Parametername" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        vIndex           parameter of the module which was added with AddModule
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshParameterDescriptions(vIndex As Variant, para As ParameterClass, r As ProjectData) As Boolean
  On Error Resume Next

  RefreshParameterDescriptions = False

  Dim sDescription As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim sParaName As String
  Dim i As Integer

  sVarPre = "SCU" & vIndex
  sRegPre = sVarPre & "_"

  ' for each parameter of this module the property AddParameter must be called
  '
  ' AddParameter(sParameterName As String, sParameterDescription As String) As Boolean
  '
  ' sParameterName         | String | parameter name as key (use the name of the variable
  '                                   for the parameter name e.g. SSC_PARA_MODE)
  ' sParameterDescription  | String | description of the parameter

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the ADC module"
  sParaName = "ADC"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the ASC0 module"
  sParaName = "ASC0"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the SSC0 module"
  sParaName = "SSC0"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the SSC1 module"
  sParaName = "SSC1"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the GPT1 and GPT2 module"
  sParaName = "GPT1_2"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the on-chip programm flash memory module"
  sParaName = "PFM"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function


  If (r.Regs.BitFind("SYSCON3_CC1DIS") = True) Then
        '------------------------------------------------------------------------------------------------
        ' Description for DUMMY_PARA_xx
        '------------------------------------------------------------------------------------------------
        sDescription = "This parameter identifies the CAPCOM1 module"
        sParaName = "CC1"
        If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  End If

  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the CAPCOM2 module"
  sParaName = "CC2"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function

  If (r.Regs.BitFind("SYSCON3_CC6DIS") = True) Then
      '------------------------------------------------------------------------------------------------
      ' Description for DUMMY_PARA_xx
      '------------------------------------------------------------------------------------------------
      sDescription = "This parameter identifies the CAPCOM6 module"
      sParaName = "CC6"
      If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  End If

  If (r.Regs.BitFind("SYSCON3_ASC1DIS") = True) Then
      '------------------------------------------------------------------------------------------------
      ' Description for DUMMY_PARA_xx
      '------------------------------------------------------------------------------------------------
      sDescription = "This parameter identifies the ASC1 module"
      sParaName = "ASC1"
      If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  End If

  If (r.Regs.BitFind("SYSCON3_I2CDIS") = True) Then
      '------------------------------------------------------------------------------------------------
      ' Description for DUMMY_PARA_xx
      '------------------------------------------------------------------------------------------------
      sDescription = "This parameter identifies I2C module"
      sParaName = "I2C"
      If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  End If

  If (r.Regs.BitFind("SYSCON3_SDLMDIS") = True) Then
      '------------------------------------------------------------------------------------------------
      ' Description for DUMMY_PARA_xx
      '------------------------------------------------------------------------------------------------
      sDescription = "This parameter identifies the SDLM module"
      sParaName = "SDLM"
      If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  End If
  
  '------------------------------------------------------------------------------------------------
  ' Description for DUMMY_PARA_xx
  '------------------------------------------------------------------------------------------------
  sDescription = "This parameter identifies the CAN module"
  sParaName = "CAN"
  If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  
  
        
        If (Not para.Sort) Then Exit Function

  RefreshParameterDescriptions = True
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("FOCON_FORV") = GetSystemClock(r) / (f * 2 ^ (1 - r.v("FOCON_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("FOCON_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("FOCON_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("FOCON_FORV") + 1) * 2 ^ (1 - r.v("FOCON_FOSS")))
  GetRealOutputFrequency = f


End Function

'*****************************************************************************
'Description:
'          This function returns the SDD output frequency
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      real output frequency [MHz]
'
'*****************************************************************************
Function GetSDDOutputFrequency(r As ProjectData)
  On Error Resume Next
  Dim f As Double

  If (r.v("RP0H_CLKCFG") = 1) Then
    f = r.v("MAIN_OSC") / (r.v("SYSCON2_CLKREL") + 1) / 2
  Else
    f = r.v("MAIN_OSC") / (r.v("SYSCON2_CLKREL") + 1)
  End If
  
  GetSDDOutputFrequency = f


End Function

'*****************************************************************************
'Description:
'          This function returns the SDD input frequency
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      real input frequency [MHz]
'
'*****************************************************************************
Function GetSDDInputFrequency(r As ProjectData)
  On Error Resume Next
  Dim f As Double

  f = r.v("MAIN_OSC")
  
  GetSDDInputFrequency = f

End Function
