'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\include\Format.inc"

Option Explicit

'*****************************************************************************
' Module:
'        OSC
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the 
'        OSC dialog script and in the OSC codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0   ahe         27.04.00     final version for C165UTAH
'
'        ----------------------------------------------------------------------
'        2.0   ahe         05.09.00     final version for C167CS, C161PI, C161CS/JC/JI
'
'        ----------------------------------------------------------------------
'        3.0   ahe         31.10.00     final version for 
'                                         C167CS
'                                         C161PI
'                                         C161CS/JC/JI
'                                         C167CR/SR r2
'
'                                       - check ISNC
'
'*****************************************************************************


'*****************************************************************************
' 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 
'        "OSC_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 sVarPre As String
  Dim sName As String

  sVarPre = "OSC"

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


  '------------------------------------------------------------------------------------------------
  ' Description for OSC_NAME_OSCH1
  '------------------------------------------------------------------------------------------------
  sName = "UnlockOWD"
  sDescription = "This is the PLL unlock/OWD interrupt service routine for " & _
                 "the PLL. It is called up when the expected oscillator " & _
                 "clock transitions are missing. "

    If (r.regs.SfrFind("ISNC") = True) Then
        If (r.v("ISNC_RTCIE") = 0 Or r.v("RTC_USE_Init") = 0) Then
                   	 sDescription = sDescription & vbcrlf & vbcrlf & TOOLTIP_ISR_USERCODE
        Else
                     sDescription = sDescription & vbcrlf & vbcrlf & "Note:" & vbcrlf & _
                     "This interrupt vector is shared with the RTC interrupt. " & _
                     "The interrupt service routine is defined in the " & r.v("RTC_CFile") & " file."
        End If           	  
    Else
        sDescription = sDescription & vbcrlf & vbcrlf & TOOLTIP_ISR_USERCODE
    End If


  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_" & sName, "void", sDescription, sReturnValue, 2)) 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 = "OSC" & 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

 	
 	If (Not para.Sort) Then Exit Function

  RefreshParameterDescriptions = True
End Function



'*****************************************************************************
'Description:
'        This function returns the bit name of the oscillator watchdog 
'        disable bit.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex 
'
'-----------------------------------------------------------------------------
'Returns:
'       OWD disable bit name
'*****************************************************************************
Function GetOWDDisebleBitName(r As ProjectData, vIndex As Variant) As String
    On Error Resume Next

    If (r.Regs.BitFind("SYSCON_OWDDIS") = True) Then
        GetOWDDisebleBitName = "OWDDIS"
    ElseIf (r.Regs.BitFind("SYSCON_OSCENBL") = True) Then
        GetOWDDisebleBitName = "OSCENBL"
    Else
        GetOWDDisebleBitName = ""
    End If


End Function



'*****************************************************************************
'Description:
'        This function checks if the PLL request flag PLLIE is set
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'       False       if PLLIE is set
'       True        else
'*****************************************************************************
Function IsRTCSubNodeIntDisabled(r As ProjectData) As Boolean
  On Error Resume Next
  Dim sRegPre As String
  
  IsRTCSubNodeIntDisabled = False            

  If (r.regs.BitFind("ISNC_RTCIE") = True) Then
      If (r.v("ISNC_RTCIE") = 0) Then
        IsRTCSubNodeIntDisabled = True
      End If
  Else
      IsRTCSubNodeIntDisabled = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function set the request flag T14 to 0 if not RTC int. selected
'        If the PLL request flag is reset then this function disabled the ISR
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'       None
'
'*****************************************************************************
Sub DisablePLLInt(r As ProjectData)
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sRegPre = "PLL"
  sVarPre = "OSC"

  If (r.regs.SfrFind("ISNC") = True) Then
      r.v("ISNC_" & sRegPre & "IE") = 0
  End If

  If (IsRTCSubNodeIntDisabled(r)) Then
    Call DisableSRN(r, sVarPre, "XP3IC", "XP3IE", "UnlockOWD")
  End If

End Sub 

