Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls
Option Explicit
'*****************************************************************************
' Module:
'        WDT
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        FSM dialog script and in the FSM codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1 radhish        25-10-04    initial version
'        0.2 radhish        11-01-05    description added in WDT_vinit function
'        0.3 Ashish        05.12.06    WDT_vEnable/Disable description added
'*****************************************************************************


'*****************************************************************************
'Description:
'        The Watchdog Timeout Period for the passed reload value is returned.
'        The result depends on the system clock fSYS and the WD prescaler.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        lReloadValue     WD timer reload value (0...255)
'-----------------------------------------------------------------------------
'Returns:
'        WD timeout period [ms]
'*****************************************************************************
Function WdtTimeoutPeriod(r As ProjectData, lReloadValue) As Double
    On Error Resume Next

    Dim dblWdtClockPeriod As Double

        dblWdtClockPeriod = 2 ^ (1 + r.l(WDT_PREFIX & "WDTCON_WDTIN") * 6) / GetCoreClock(r) / 1000
        WdtTimeoutPeriod = dblWdtClockPeriod * 2 ^ 8 * (2 ^ 8 - lReloadValue)
        WdtTimeoutPeriod = (WdtTimeoutPeriod * 1000 / 1) / 1000

End Function

'*****************************************************************************
'Description:
'        The Watchdog Timer Reload Value for the passed timeout period is returned.
'        The result depends on the system clock fSYS and the WD prescaler.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        WD timer reload value
'*****************************************************************************
Function WdtReloadValue(r As ProjectData, dblWdtTimeoutPeriod As Double) As Long
    On Error Resume Next

    Dim dblWdtClockPeriod As Double

    dblWdtClockPeriod = 2 ^ (1 + r.l(WDT_PREFIX & "WDTCON_WDTIN") * 6) / GetCoreClock(r) / 1000
    WdtReloadValue = 2 ^ 8 - (dblWdtTimeoutPeriod / dblWdtClockPeriod / 2 ^ 8)
        
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
'        "FSM_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

  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


  '------------------------------------------------------------------------------------------------
  ' Description for WDT_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription("'Watch Dog Timer'") & vbCrLf & vbCrLf

''  If (r.v(WDT_PREFIX & "WDTCON_WDTEN") = 0) Then
''    sDescription = sDescription & _
''                     "The watchdog timer is not used." & vbCrLf & _
''                 "The following SFR fields will be initialized:" & vbCrLf & _
''                 "WDTEN - WDT disable"
''
''     Else
    sDescription = sDescription & _
                     "The watchdog timer is used." & vbCrLf & _
                 "The following SFR fields will be initialized:" & vbCrLf & _
                 "WDTREL - WDT reload value" & vbCrLf & _
                 "WDTIN  - WDT input frequency selection" & vbCrLf & _
                                 "Then the watchdog timer will be refreshed." & vbCrLf & _
                 "Note: The user has to take care that RMAP bit in SYSCON0 is to be set while using the registers of Watch dog timer." & vbCrLf & _
                 "Registers are WDTCON,WDTWINB,WDTREL,WDTH,WDTL"
''  End If
  sReturnValue = "None"
  If (Not func.AddFunction("WDT_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function
 '------------------------------------------------------------------------------------------------
  ' Description for WDT_vEnable
  '------------------------------------------------------------------------------------------------
  sDescription = "This function enables the WDT." & vbCrLf & _
                "pls refer to the Note on WDT_vInit function Description"
  sReturnValue = "None"
  If (Not func.AddFunction("WDT_WinEna", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for WDT_vDisable
  '------------------------------------------------------------------------------------------------
  sDescription = "This function disables the WDT." & vbCrLf & _
                "pls refer to the Note on WDT_vInit function Description"
  sReturnValue = "None"
  If (Not func.AddFunction("GEN_UserDef0", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for WDT_NAME_Reset
  '------------------------------------------------------------------------------------------------
  sDescription = "This function returns the contents of the WDT reset indication bit WDTRST. " & vbCrLf & _
                "pls refer to the Note on WDT_vInit function Description"
  sReturnValue = "1 = a WDT reset has occured, else 0"
  If (Not func.AddFunction("WDT_NAME_Reset", "bit", sDescription, sReturnValue, 1)) Then Exit Function

 
  '------------------------------------------------------------------------------------------------
  ' Description for WDT_NAME_Refresh
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro refreshes the watchdog timer to the reload value. " & _
                                 "The interrupts are disabled during execution of the instructions that set bit WDTRS. " & vbCrLf & _
                 "Note: The user has to take care that this macro is called before expiry of the watchdog timer." & vbCrLf & _
                "pls refer to the Note on WDT_vInit function Description"
  sReturnValue = "None"
  If (Not func.AddFunction("WDT_NAME_Refresh", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  '------------------------------------------------------------------------------------------------
  ' Description for WDT_NAME_Windowb
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro set the window boundary range. " & vbCrLf & _
                "pls refer to the Note on WDT_vInit function Description"
                
  sReturnValue = "None"
  If (Not func.AddFunction("WDT_NAME_Windowb", "", sDescription, sReturnValue, 0)) Then Exit Function

       If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function

