Attribute VB_Name = "Module2"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\include\Port.Inc"
Option Explicit
'*****************************************************************************
' Module:
'        T21
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        T21 dialog script and in the T21 codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  Sunil          18-01-06    Final version
'
'*****************************************************************************

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

Global Const RATIO_FSYS_T21 = 12          ' RATIO_FSYS_T21  = fSYS / T21CounterRate
Global Const RATIO_FSYS_T21_CAPTURE = 24
'Note: this ratio is only valid for timer function, not for baudrate generation

Global Const TXT_WARNING_16_BIT_ACCESS = vbCrLf & _
    "Please take into account that an active timer 21 unit may interfere with the sequence of high and low byte access. " & _
    "Thus you should stop the timer first, unless you are sure that the access takes place at a save moment."
'*****************************************************************************
'Description:
'       The timer 2 overflow rate is returned as a formatted string.
'       The unit is micro seconds. The value depends on the timer count value
'       and the oscillator frequency.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       overflow rate   e.g. "780.500"
'*****************************************************************************
Function GetTimer2Overflow(r As ProjectData) As String
    On Error Resume Next
        
''        If r.v(T21_PREFIX & "T2MOD_PREN") = 1 Then
''            GetTimer2Overflow = FormatTextOutput(Format$((2 ^ r.l(T21_PREFIX & "T2MOD_T2PRE")) * (65536 - (r.l(T21_PREFIX & "RC2H_RC2") * 256 + _
''                                                                r.l(T21_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
''            Exit Function
''        End If
''        If r.v(T21_PREFIX & "T2CON_CP/RL2") = 1 Then
''            GetTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T21_CAPTURE * (65536 - (r.l(T21_PREFIX & "RC2H_RC2") * 256 + _
''                                                                r.l(T21_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
''        Else
''            GetTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T21 * (65536 - (r.l(T21_PREFIX & "RC2H_RC2") * 256 + _
''                                                                r.l(T21_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
''        End If
        
        If r.v(T21_PREFIX & "T2CON_CP/RL2") = 1 Then
            If r.v(T21_PREFIX & "T2MOD_PREN") = 1 Then
                GetTimer2Overflow = FormatTextOutput(Format$((2 ^ r.l(T21_PREFIX & "T2MOD_T2PRE")) * (65536 / GetCoreClock(r)), "0.000"), "F")
            Else
                GetTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T21 * (65536 / GetCoreClock(r)), "0.000"), "F")
            End If
        Else
            If r.v(T21_PREFIX & "T2MOD_PREN") = 1 Then
                GetTimer2Overflow = FormatTextOutput(Format$((2 ^ r.l(T21_PREFIX & "T2MOD_T2PRE")) * (65536 - (r.l(T21_PREFIX & "RC2H_RC2") * 256 + _
                                                                r.l(T21_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
            Else
                GetTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T21 * (65536 - (r.l(T21_PREFIX & "RC2H_RC2") * 256 + _
                                                                r.l(T21_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
            End If
        End If
        
End Function

'*****************************************************************************
'Description:
'       The timer 2 underflow rate is returned as a formatted string.
'       The unit is micro seconds. The value depends on the timer count value
'       and the oscillator frequency.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       overflow rate   e.g. "780.500"
'*****************************************************************************
Function GetTimer2Underflow(r As ProjectData) As String
    On Error Resume Next
    
    If r.v(T21_PREFIX & "T2MOD_PREN") = 1 Then
        GetTimer2Underflow = FormatTextOutput(Format$((2 ^ r.l(T21_PREFIX & "T2MOD_T2PRE")) * (1 + (r.l(T21_PREFIX & "RC2H_RC2") * 256 + _
                                                        r.l(T21_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
    Else
        GetTimer2Underflow = FormatTextOutput(Format$(RATIO_FSYS_T21 * (1 + (r.l(T21_PREFIX & "RC2H_RC2") * 256 + _
                                                        r.l(T21_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
    End If
    
End Function

'*****************************************************************************
'Description:
'       The timer 2 resolution in micro seconds is returned as a formatted
'       string. The value depends on the oscillator frequency.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       timer resolution e.g. "1.5"
'*****************************************************************************
Function GetTimer2Resolution(r As ProjectData) As String
    On Error Resume Next
    If r.v(T21_PREFIX & "T2MOD_PREN") = 1 Then
        GetTimer2Resolution = FormatTextOutput((((2 ^ r.l(T21_PREFIX & "T2MOD_T2PRE")) / GetCoreClock(r)) * 1000 \ 1) / 1000, "F")
    Else
'        GetTimer2Resolution = FormatTextOutput(((RATIO_FSYS_T21_CAPTURE / GetCoreClock(r)) * 1000 \ 1) / 1000, "F")
        GetTimer2Resolution = FormatTextOutput(((RATIO_FSYS_T21 / GetCoreClock(r)) * 1000 \ 1) / 1000, "F")
    End If
End Function

'*****************************************************************************
'Description:
'        This procedure sets the T21-Module into the default.
'-----------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'
'*****************************************************************************
Sub SetT21Default(r As ProjectData)
    On Error Resume Next

    r.v(T21_PREFIX & "T2MOD_PREN") = 0
    r.v(T21_PREFIX & "T2MOD_T2PRE") = 0
    r.v(T21_PREFIX & "RC2L_RC2") = 0
    r.v(T21_PREFIX & "RC2H_RC2") = 0
    r.v(T21_PREFIX & "T2CON_CP/RL2") = 0
    r.l(T21_PREFIX & "T2MOD_DCEN") = 0
   ' r.v(SCU_PREFIX & "BCON_T2EXIS") = 0
    If (r.v(T21_PREFIX & "T2CON_EXEN2") = 1) Then
        Call ReleaseAlternateInputPin(r, "T2EX1_0")
        Call ReleaseAlternateInputPin(r, "T2EX1_1")
        r.v(T21_PREFIX & "T2CON_EXEN2") = 0
        r.v(T21_PREFIX & "T2MOD_EDGESEL") = 0
    End If
    
    r.l(T21_PREFIX & "T2CON_C/T2") = 0
    Call ReleaseAlternateInputPin(r, "T21_1")
    Call ReleaseAlternateInputPin(r, "T21_0")
    r.v("MODPISEL2_T21IS") = 0
    
    If r.v("Timer21_EXF2_Pin_P3.5_Used") = 1 Then
        Call ReleaseAlternateOutputPin(r, "EXF21_0")
        r.v("Timer21_EXF2_Pin_P3.5_Used") = 0
    End If
    If r.v("Timer21_EXF2_Pin_P4.3_Used") = 1 Then
        Call ReleaseAlternateOutputPin(r, "EXF21_1")
        r.v("Timer21_EXF2_Pin_P4.3_Used") = 0
    End If
           
    If r.v("Timer21_T2EX_Pin_P3.4_Used") = 1 Then
        Call ReleaseAlternateInputPin(r, "T2EX1_0")
        r.v("Timer21_T2EX_Pin_P3.4_Used") = 0
    End If
    If r.v("Timer21_T2EX_Pin_P0.5_Used") = 1 Then
        Call ReleaseAlternateInputPin(r, "T2EX1_1")
        r.v("Timer21_T2EX_Pin_P0.5_Used") = 0
    End If
    
    r.v(T21_PREFIX & "T2MOD_T2RHEN") = 0
    'Call ReleaseAlternateInputPin(r, "T2EX")
    r.v(T21_PREFIX & "T2MOD_T2REGS") = 0
    
    r.l(T21_PREFIX & "T2CON_TR2") = 0
    r.v("T21_Interrupt_Select") = 0
    r.v("T21_USE_IsrTmr") = 0
    r.v("T21_USE_Init") = 0
    
    r.v(T21_PREFIX & "T2CON1_EXF2EN") = 0
    r.v(T21_PREFIX & "T2CON1_TF2EN") = 0
    r.v("T21_USE_StartTmr") = 0
    r.v("T21_USE_StopTmr") = 0
    r.v("T21_USE_ReadTmr") = 0
    r.v("T21_USE_SetRC2Reg") = 0
    r.v("T21_USE_GetRC2Reg") = 0
End Sub

'*****************************************************************************
'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
'        "T21_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 T21_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription("Timer 21") & vbCrLf & vbCrLf & _
                 "The following SFRs and SFR fields will be initialized:" & vbCrLf & _
                 T21_PREFIX & "RC2H/RC2L     - reload/capture timer 21 register" & vbCrLf & _
                 T21_PREFIX & "T2H/T2L     -  timer 21 register" & vbCrLf & _
                 "EX2           - timer 21 interrupt enable" & vbCrLf & _
                 T21_PREFIX & "T2MOD         - timer 21 mode register" & vbCrLf & _
                 "CP/RL2        - Capture/Reload select" & vbCrLf & _
                 "EXEN2         - External enable control" & vbCrLf & _
                 "TR2           - Timer21 run control"
  sReturnValue = "None"
  If (Not func.AddFunction("T21_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T21_NAME_IsrTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the Timer 21 interrupt. " & _
                "Depending on the selected operating mode it is called when TF2 is set by " & _
                "an overflow or underflow of the timer 21 register or when EXF2 " & _
                "is set by a negative transition on T2EX." & vbCrLf & _
                TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If (Not func.AddFunction("T21_NAME_IsrTmr", "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T21_NAME_StartTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro starts timer 21 by setting bit TR2."
  sReturnValue = "None"
  If (Not func.AddFunction("T21_NAME_StartTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T21_NAME_StopTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro stops timer 21 by clearing bit TR2."
  sReturnValue = "None"
  If (Not func.AddFunction("T21_NAME_StopTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T21_NAME_ReadTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro reads the contents of the T21 timer register. " & _
                 "The timer is not stopped." & vbCrLf & _
                 TXT_WARNING_16_BIT_ACCESS
  sReturnValue = "16-bit timer register contents"
  If (Not func.AddFunction("T21_NAME_ReadTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T21_NAME_SetRC2Reg
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro writes the passed value into the reload/capture timer 21 register (RC2H/RC2L)." & vbCrLf & _
                 TXT_WARNING_16_BIT_ACCESS
  sReturnValue = "None"
  If (Not func.AddFunction("T21_NAME_SetRC2Reg", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "16-bit timer reload value"
  If (Not func.AddParameter("T21_NAME_SetRC2Reg_Par1", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T21_NAME_GetRC2Reg
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro reads the contents of the reload/capture timer 21 register (RC2H/RC2L). " & vbCrLf & _
                 TXT_WARNING_16_BIT_ACCESS
  sReturnValue = "16-bit reload/capture register contents"
  If (Not func.AddFunction("T21_NAME_GetRC2Reg", "", sDescription, sReturnValue, 0)) Then Exit Function

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

  RefreshFunctionDescriptions = True
End Function

