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

Option Explicit

'*****************************************************************************
' Module:
'        WDT
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        WDT dialog script and in the WDT codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         14.02.00     final version for C165UTAH
'
'        ---------------------------------------------------------------------
'        2.0   ahe         14.06.00     final version for C167CS, C161CS/JC/JI
'
'        ---------------------------------------------------------------------
'        2.1   ahe         02.11.00     final version for C167CS, C161CS/JC/JI
'
'        ---------------------------------------------------------------------
'        4.0   ahe         24.04.01     final version for Pegasus
'        4.1   ste         06.01.03     no changes
'
'*****************************************************************************


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

'*****************************************************************************
'Description:
'        For each function which should be used for this module a line
'        "If (Not func.AddFunction .." must be inserted in this function.
'        For each parameter that should be used for this function a
'        line "If (Not func.AddParameter .." must be inserted in this function.
'
'        Note:
'        For each added function a project variable with the name
'        "WDT_NAME_Functionname" must 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 sIndex As String
  Dim vector As Variant
  

  sVarPre = "WDT"
  sIndex = "WDT"

  ' 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 WDT_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_ServiceWDT
  '------------------------------------------------------------------------------------------------
  sDescription = "This function service the watchdog timer. If the watchdog " & _
                        "timer is used in an application, it has to be regularly " & _
                        "serviced to prevent it from overflowing. "
  sReturnValue = "None"
  If (Not func.AddFunction(sIndex & "_NAME_ServiceWDT", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_DisableWDT
  '------------------------------------------------------------------------------------------------
  sDescription = "This function disables the watchdog timer."
  sReturnValue = "None"
  If (Not func.AddFunction(sIndex & "_NAME_DisableWDT", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_EnableWDT
  '------------------------------------------------------------------------------------------------
  sDescription = "This function enables the watchdog timer."
  sReturnValue = "None"
  If (Not func.AddFunction(sIndex & "_NAME_EnableWDT", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SCU_NAME_WDTStatus
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro returns the current status of the watchdog timer. " & _
                        "The possible values for the returned status are: " & vbCrLf & _
                        "Bit 0 = 0   -> No WDT overflow error " & vbCrLf & _
                        "Bit 0 = 1   -> WDT overflow error has occurred. " & vbCrLf & _
                        "Bit 1 = 0   -> WDT Timer is enabled (default after reset). " & vbCrLf & _
                        "Bit 1 = 1   -> WDT Timer is disabled. " & vbCrLf & _
                        "Bit 2 = 0   -> Normal Mode (default after reset) " & vbCrLf & _
                        "Bit 2 = 1   -> Prewarning Mode" & vbCrLf & _
                        "Bit 8 = 0   -> Input Frequency Request bit set input frequency to fIN / 16384" & vbCrLf & _
                        "Bit 8 = 1   -> Input Frequency Request bit set input frequency to fIN / 256"
                        
  sReturnValue = "Current status of the watchdog timer"
  If (Not func.AddFunction(sIndex & "_NAME_WDTStatus", "uword", sDescription, sReturnValue, 1)) Then Exit Function

  sDescription = "This macro returns the contents of the watchdog timer. "
  sReturnValue = "Contents of the watchdog timer"
  If (Not func.AddFunction(sIndex & "_NAME_uwGetTmr", "uword", sDescription, sReturnValue, 1)) Then Exit Function
   
  If (r.v("SCU_INTNP1_WDT") = 1) Then
    vector = FormatTextOutput(107, "H2")
  Else
    vector = FormatTextOutput(108, "H2")
  End If
    
  sDescription = "This is the interrupt service routine for the WDT" & _
                " with the vector address " & vector & ".It occurs if the WDT " & _
                "timer Overflows and the interrupt enabled."
                
  sDescription = sDescription & vbCrLf & TOOLTIP_ISR_USERCODE
    
  sReturnValue = "None"
  
  If (Not func.AddFunction(sIndex & "_NAME_viINT", "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 = "WDT" & 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 module clock (in MHz) of the
'        SCU module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        module clock (in MHz)
'*****************************************************************************
Function GetSCUModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error GoTo GetSCUModuleClockError
  Dim sIndex As String
  Dim v As String
  Dim sPrefix As String

  sIndex = "SCU" ' & vIndex
  sPrefix = sIndex & "_"

  GetSCUModuleClock = 1
  GetSCUModuleClock = GetSystemClock(r)
  Exit Function

GetSCUModuleClockError:
  GetSCUModuleClock = 1
  Exit Function
End Function

'*****************************************************************************
'Description:
'        This function returns the WDT timer period (in s).
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        WDT timer period (in s)
'*****************************************************************************
Function GetWDTTimerPeriod(r As ProjectData, vIndex As Variant) As Double
  On Error GoTo GetWDTTimerPeriodError
  Dim sIndex As String
  Dim v As String
  sIndex = "SCU" & vIndex

  GetWDTTimerPeriod = 1
  If (r.v("SCU_WDTCS_IR") = 1) Then
    GetWDTTimerPeriod = (2 ^ 16 - r.sfr("SCU_WDTREL")) * 256 / GetSCUModuleClock(r, vIndex)
  Else
    GetWDTTimerPeriod = (2 ^ 16 - r.sfr("SCU_WDTREL")) * 16384 / GetSCUModuleClock(r, vIndex)
  End If

  Exit Function

GetWDTTimerPeriodError:
  GetWDTTimerPeriod = 1
  Exit Function
End Function
'*****************************************************************************
'Description:
'        This function returns the WDT timeout period (in s).
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        WDT timeout period (in s)
'*****************************************************************************
Function GetWDTTimeoutPeriod(r As ProjectData, vIndex As Variant) As Double
  On Error GoTo GetWDTTimeoutPeriodError
  Dim sIndex As String
  Dim v As String
  sIndex = "SCU" & vIndex

  GetWDTTimeoutPeriod = 1
  If (r.v("SCU_WDTCS_IR") = 1) Then
    GetWDTTimeoutPeriod = (2 ^ 16 - &HFFFC) * 256 / GetSCUModuleClock(r, vIndex)
  Else
    GetWDTTimeoutPeriod = (2 ^ 16 - &HFFFC) * 16384 / GetSCUModuleClock(r, vIndex)
  End If

  Exit Function

GetWDTTimeoutPeriodError:
  GetWDTTimeoutPeriod = 1
  Exit Function
End Function
