Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\include\Port.inc"


Option Explicit
'*****************************************************************************
' Module:
'        PS
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        PS dialog script and in the PS codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  Radhish        18-10-04    initial version
'        0.2  radhish        17-12-04    Modification made as per observation
'                                          made during verification
'        0.3 Radhish         10-01-05   in slowdown and reset slowdown macroes were changed to function.
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************
Global Const NUMBER_OF_PM_MODULES = 9 ' number of modules that can be enabled/disabled

Global Const TXT_WAKE_UP_SOURCE_0 = "No Wakeup source is selected"
Global Const TXT_WAKE_UP_SOURCE_1 = "Wake-up via pin RxD_0"
Global Const TXT_WAKE_UP_SOURCE_5 = "Wake-up via pin RxD_1"

Global Const TXT_WAKE_UP_SOURCE_2 = "Wake-up via pin EXINT0"
Global Const TXT_WAKE_UP_SOURCE_3 = "Wake-up via pin RxD_0 and EXINT0 is selected"
Global Const TXT_WAKE_UP_SOURCE_4 = "Wake-up via pin RxD_1 and EXINT0 is selected"
 
Global Const TXT_NOTE_SLOW_DOWN_MODE = _
    "Note: DAvE's project settings for the on-chip peripheral components are only valid for normal clock operation. " & _
    "During slow down mode the behavior of the peripherals can differ from these settings due to the reduced clock rate!"

'*****************************************************************************
'Description:
'        This functions returns the Id of a module that can be enabled/disabled.
'        The Id is used internally to access project variables.
'        The Id is also used as the parameter's default name.
'-----------------------------------------------------------------------------
'Parameters:
'        iIndex           module index (0...NUMBER_OF_PM_MODULES-1)
'-----------------------------------------------------------------------------
'Returns:
'        internal ID of the module
'*****************************************************************************
Public Function ModuleId(iIndex As Integer) As String
    On Error Resume Next
    ModuleId = ""
    If iIndex = 0 Then ModuleId = "ADC"
    If iIndex = 1 Then ModuleId = "T2"
    If iIndex = 2 Then ModuleId = "CCU"
    If iIndex = 3 Then ModuleId = "SSC"
    If iIndex = 4 Then ModuleId = "MDU"
    If iIndex = 5 Then ModuleId = "CAN"
    If iIndex = 6 Then ModuleId = "CDC"
    If iIndex = 7 Then ModuleId = "T21"
    If iIndex = 8 Then ModuleId = "UART1"
End Function

'*****************************************************************************
'Description:
'        This functions returns the variable name of the disable bit
'        for a specific module
'-----------------------------------------------------------------------------
'Parameters:
'        iIndex           module index (0...NUMBER_OF_PM_MODULES-1)
'-----------------------------------------------------------------------------
'Returns:
'        variable name of the disable bit
'*****************************************************************************
Public Function ModuleDisableBit(iIndex As Integer) As String
    On Error Resume Next
    ModuleDisableBit = ""
    If iIndex = 0 Then ModuleDisableBit = SCU_PREFIX & "PMCON1_ADC_DIS"
    If iIndex = 1 Then ModuleDisableBit = SCU_PREFIX & "PMCON1_T2_DIS"
    If iIndex = 2 Then ModuleDisableBit = SCU_PREFIX & "PMCON1_CCU_DIS"
    If iIndex = 3 Then ModuleDisableBit = SCU_PREFIX & "PMCON1_SSC_DIS"
    If iIndex = 4 Then ModuleDisableBit = SCU_PREFIX & "PMCON1_MDU_DIS"
    If iIndex = 5 Then ModuleDisableBit = SCU_PREFIX & "PMCON1_CAN_DIS"
    If iIndex = 6 Then ModuleDisableBit = SCU_PREFIX & "PMCON1_CDC_DIS"
    If iIndex = 7 Then ModuleDisableBit = SCU_PREFIX & "PMCON2_T21_DIS"
    If iIndex = 8 Then ModuleDisableBit = SCU_PREFIX & "PMCON2_UART1_DIS"
End Function



'*****************************************************************************
'Description:
'       The slow down clock frequency is returned as a formatted string.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       SDD Clock frequency in MHz, e.g. "1.500"
'*****************************************************************************
Function GetSDDClock(r As ProjectData) As String
    On Error Resume Next

    GetSDDClock = FormatTextOutput(Format$(GetSystemClock(r) / 2 ^ (r.l(SCU_PREFIX & "CMCON_CLKREL")), "0.00000"), "F")
End Function

'*****************************************************************************
'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
'        "PS_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 successful operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next

  Dim sDescription As String
  Dim sReturnValue As String
  Dim iModule      As Integer
  Dim sModuleList  As String

  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)

    ' 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

  ' Generate list of the available Module Names:
  sModuleList = ""
  For iModule = 0 To NUMBER_OF_PM_MODULES - 1
        If (iModule > 0) Then
                sModuleList = sModuleList & ", "
        End If
    sModuleList = sModuleList & r.v("PS_PARA_MODULE_" & ModuleId(iModule))
  Next iModule

  '------------------------------------------------------------------------------------------------
  ' Description for PS_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription("Power Saving") & vbCrLf & vbCrLf & _
                 "This function does not contain any generated C-code, but it is " & _
                 "possible to insert application specific source lines. "
  sReturnValue = "None"
  If (Not func.AddFunction("PS_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for PS_NAME_SetPowerDown
  '------------------------------------------------------------------------------------------------
  sDescription = "This function invokes the software power down mode by execution of following steps:" & vbCrLf

  If (r.v(SCU_PREFIX & "PMCON0_PD") = 1) Then
    sDescription = sDescription & _
                 "- the external wake-up from software power down will be enabled" & vbCrLf & _
                 "- the wake-up source will be selected" & vbCrLf
  Else
    sDescription = sDescription & _
                 "- the external wake-up from software power down remains disabled" & vbCrLf
  End If

    sDescription = sDescription & _
                 "- bit PD in SFR PMCON0 will be set" & vbCrLf & _
                 "  (this will stop all controller functions)"

  

'  If (r.v(SCU_PREFIX &"PMCON0_PD") = 1) Then
'    sDescription = sDescription & vbCrLf & _
'                 "This function will not return before the controller has been restarted by " & _
'                 "an external wake-up signal and the execution of the wake-up interrupt " & _
'                 "service routine."
'  End If

  sReturnValue = "None"
  If (Not func.AddFunction("PS_NAME_SetPowerDown", "void", sDescription, sReturnValue, 1)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for PS_NAME_SetIdle
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro enters the idle mode by setting bit IDLE in SFR PCON. " & _
                 "The instruction will stop the CPU immediately. Thus any following code will not be " & _
                 "executed before the controller has been restarted by an interrupt."
  sReturnValue = "None"
  If (Not func.AddFunction("PS_NAME_SetIdle", "void", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for PS_NAME_SetSlowDown
  '------------------------------------------------------------------------------------------------
  sDescription = "This function activates the slow down mode by setting bit SD in SFR PMCON0." & vbCrLf & _
                 TXT_NOTE_SLOW_DOWN_MODE
  sReturnValue = "None"
  If (Not func.AddFunction("PS_NAME_SetSlowDown", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for PS_NAME_ResetSlowDown
  '------------------------------------------------------------------------------------------------
  sDescription = "This function terminates the slow down mode by clearing bit SD in SFR PMCON0."
  sReturnValue = "None"
  If (Not func.AddFunction("PS_NAME_ResetSlowDown", "void", sDescription, sReturnValue, 1)) Then Exit Function

  
  '------------------------------------------------------------------------------------------------
  ' Description for PS_NAME_DisableModule
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro disables a specific peripheral module which is passed in a parameter.Switch the page to 1 using the" & _
                  " statementSFR_PAGE(_su1, SST1);    before calling this Disable macro.After that again switch back the page to 0 with the macro SFR_PAGE(_su1, RST1);." & _
                 "The following constants are available for 'ModuleName':" & vbCrLf & sModuleList
  sReturnValue = "None"
  If (Not func.AddFunction("PS_NAME_DisableModule", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Name of the Module"
  If (Not func.AddParameter("PS_NAME_DisableModule_Par1", "ModuleName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for PS_NAME_EnableModule
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro enables a specific peripheral module which is passed in a parameter. Switch the page to 1 using the" & _
                 " statement SFR_PAGE(_su1, SST1);    before calling this Enable macro.After that again switch back the page to 0 with the macro SFR_PAGE(_su1, RST1);." & _
                 "The following constants are available for 'ModuleName':" & vbCrLf & sModuleList
  sReturnValue = "None"
  If (Not func.AddFunction("PS_NAME_EnableModule", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Name of the Module"
  If (Not func.AddParameter("PS_NAME_EnableModule_Par1", "ModuleName", "", sDescription)) Then Exit Function

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

  RefreshFunctionDescriptions = True
End Function

