Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
Option Explicit
'*****************************************************************************
' Module:
'        T01
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        T01 dialog script and in the T01 codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  Sunil          12-10-04    initial version
'        0.2  Sunil          29-12-04    module prefix added.
'
'
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************
Global Const NUMBER_OF_T01_PARAMETERS = 5 ' number of timer 0/1 parameters


'*****************************************************************************
'Description:
'        This functions returns the Id of a parameter.
'        The Id is used internally to access project variables.
'        The Id is also used as the parameter's default name.
'-----------------------------------------------------------------------------
'Parameters:
'        iIndex           interrupt index (0...NUMBER_OF_T01_PARAMETERS-1)
'-----------------------------------------------------------------------------
'Returns:
'        internal ID of the parameter
'*****************************************************************************
Function T01ParId(iIndex As Integer) As String
    On Error Resume Next
    T01ParId = ""
    If iIndex = 0 Then T01ParId = "T01_TIMER_0"
    If iIndex = 1 Then T01ParId = "T01_TIMER_1"
    If iIndex = 2 Then T01ParId = "T01_MODE_0"
    If iIndex = 3 Then T01ParId = "T01_MODE_1"
    If iIndex = 4 Then T01ParId = "T01_MODE_2"
End Function

'-----------------------------------------------------------------------------
' Description:
'          This function returns the currently set Timer0 mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r           Project data
'-----------------------------------------------------------------------------
'Returns:
'        Mode of Timer0
'*****************************************************************************
Function GetTimer0Mode(r As ProjectData) As Integer
    On Error Resume Next

    GetTimer0Mode = r.l(T01_PREFIX & "TMOD_T0M")
        
End Function

'-----------------------------------------------------------------------------
' Description:
'          This function returns the currently set Timer1 mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r           Project data
'-----------------------------------------------------------------------------
'Returns:
'        Mode of Timer1
'*****************************************************************************
Function GetTimer1Mode(r As ProjectData) As Integer
    On Error Resume Next

    GetTimer1Mode = r.l(T01_PREFIX & "TMOD_T1M")
    
End Function

'-----------------------------------------------------------------------------
' Description:
'          This function changes the Timer0 mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r           Project data
'        iMode       mode to set
'-----------------------------------------------------------------------------
'Returns:
'        None
'*****************************************************************************
Sub SetTimer0Mode(r As ProjectData, iMode As Integer)
    On Error Resume Next

    r.l(T01_PREFIX & "TMOD_T0M") = iMode
    
End Sub

'-----------------------------------------------------------------------------
' Description:
'          This function changes the Timer1 mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r           Project data
'        iMode       mode to set
'-----------------------------------------------------------------------------
'Returns:
'        None
'*****************************************************************************
Sub SetTimer1Mode(r As ProjectData, iMode As Integer)
    On Error Resume Next

    r.l(T01_PREFIX & "TMOD_T1M") = iMode
    
End Sub

'*****************************************************************************
'Description:
'       If UART baudrate derived from the timer 1 overflow can be determined,
'       the baudrate will be returned as a string, otherwise an empty string
'       will be returned.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       baudrate        e.g. "9600"
'*****************************************************************************
Function GetTimer1Baudrate(r As ProjectData) As String
    On Error Resume Next

'counter pin not available    If ( ((r.l("TMOD_M1_1") * 2 + r.l("TMOD_M0_1")) = 2) And (r.l("TMOD_C/T_1") = 0) ) Then
    If r.l(T01_PREFIX & "TMOD_T1M") = 2 And r.v(BRG_PREFIX & "FDCON_BGS") = 1 Then
        ' typical configuration for baudrate generation: 8-bit auto-reload mode in timer operation
        GetTimer1Baudrate = FormatTextOutput(Format$(((2 ^ r.d(UART_PREFIX & "PCON_SMOD")) * (GetCoreClock(r) * 1000000)) / (32 * RATIO_FSYS_T01 * (256 - r.d(T01_PREFIX & "TH1_VAL"))), "0"), "F")
    Else
        ' baudrate calculation with other timer modes is not supported...
        ' ...because the runtime of the user's ISR which has to reload the timer is unknown
        GetTimer1Baudrate = ""
    End If
End Function

'-----------------------------------------------------------------------------
' Description:
'          This function returns the overflow rate of Timer0 or Timer1.
'-----------------------------------------------------------------------------
'Parameters:
'        r           Project data
'        iTimer      Timer
'        iMode       Timer mode
'-----------------------------------------------------------------------------
'Returns:
'        Overflow rate
'*****************************************************************************
Function GetTimerOverflow(r As ProjectData, iTimer As Integer, iMode As Integer) As String
    On Error Resume Next

    Dim dblTLx As Double
    Dim dblTHx As Double

    If (iTimer = 0) Then
        dblTLx = r.d(T01_PREFIX & "TL0_VAL")
        dblTHx = r.d(T01_PREFIX & "TH0_VAL")
    Else
        dblTLx = r.d(T01_PREFIX & "TL1_VAL")
        dblTHx = r.d(T01_PREFIX & "TH1_VAL")
    End If

    Select Case iMode
        Case 0
             ' Note: the "5-bit prescaler" does not work as an ordinary prescaler, but as the least significant 5 bits of a
             ' 13-bit timer with THx presenting the most significant bits.
             '                                                                      13-bit timer
            GetTimerOverflow = FormatTextOutput(Format$(RATIO_FSYS_T01 * (&H2000 - (dblTHx * &H20 + dblTLx)) / GetCoreClock(r), "0.000"), "F")
        Case 1
             '                                                                      16-bit timer
            GetTimerOverflow = FormatTextOutput(Format$(RATIO_FSYS_T01 * (&H10000 - (dblTHx * &H100 + dblTLx)) / GetCoreClock(r), "0.000"), "F")
        Case 2
             '                                                        8-bit timer (reload value)
            GetTimerOverflow = FormatTextOutput(Format$(RATIO_FSYS_T01 * (&H100 - dblTHx) / GetCoreClock(r), "0.000"), "F")
        Case 3
            If (iTimer = 0) Then
                '                                                           first 8-bit timer
                GetTimerOverflow = FormatTextOutput(Format$(RATIO_FSYS_T01 * (&H100 - r.d(T01_PREFIX & "TL0_VAL")) / GetCoreClock(r), "0.000"), "F")
            Else
            '                                                               second 8-bit timer
                GetTimerOverflow = FormatTextOutput(Format$(RATIO_FSYS_T01 * (&H100 - r.d(T01_PREFIX & "TH0_VAL")) / GetCoreClock(r), "0.000"), "F")
            End If
        Case Else
            GetTimerOverflow = ""
    End Select
    

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
'        "T01_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(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 T01_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription("Timer 0/1") & vbCrLf & vbCrLf & _
                 "The following SFR fields will be initialized for both timers 0 and 1:" & vbCrLf & _
                 "T0M            - Timer 0 mode" & vbCrLf & _
                 "T1M            - Timer 1 mode" & vbCrLf & _
                 "GATE0/GATE1    - gating control" & vbCrLf & _
                 "TLx, THx       - timer low/high registers" & vbCrLf & _
                 "ETx            - interrupt enable" & vbCrLf & _
                 "TRx            - timer run control (this is the final action)"

'counter pin not available:
'                "C/T      - counter/timer operation"                       & vbCrLf & _

  sReturnValue = "None"
  If (Not func.AddFunction("T01_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_StartTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro starts the chosen timer. The timer continues " & _
                 "to count from where it has been stopped the last time. " & vbCrLf & _
                 "The following constants are available for 'TimerNr':" & vbCrLf & _
                 r.v("T01_PARA_T01_TIMER_0") & ", " & r.v("T01_PARA_T01_TIMER_1") & vbCrLf & vbCrLf & _
                 "Special feature when timer 0 is in mode 3:" & vbCrLf & _
                 "'" & r.v("T01_PARA_T01_TIMER_1") & "' selects the second 8-bit timer TH0. " & vbCrLf & _
                 "'" & r.v("T01_NAME_StartTmr1") & "' must be used in order to start TL1/TH1."
  sReturnValue = "None"
  If (Not func.AddFunction("T01_NAME_StartTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Number of timer to be started"
  If (Not func.AddParameter("T01_NAME_StartTmr_Par1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_StopTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro stops the chosen timer. The timer contents of " & _
                 "the timer register remains unchanged." & vbCrLf & _
                 "The following constants are available for 'TimerNr':" & vbCrLf & _
                 r.v("T01_PARA_T01_TIMER_0") & ", " & r.v("T01_PARA_T01_TIMER_1") & vbCrLf & vbCrLf & _
                 "Special feature when timer 0 is in mode 3:" & vbCrLf & _
                 "'" & r.v("T01_PARA_T01_TIMER_1") & "' selects the second 8-bit timer TH0. " & vbCrLf & _
                 "'" & r.v("T01_NAME_StopTmr1") & "' must be used in order to stop TL1/TH1."
  sReturnValue = "None"
  If (Not func.AddFunction("T01_NAME_StopTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Number of timer to be stopped"
  If (Not func.AddParameter("T01_NAME_StopTmr_Par1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_ClearTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro sets the timer registers TLx and THx of the chosen timer to zero." & vbCrLf & _
                 "Macro '" & r.v("T01_NAME_ClearTmr") & "H' is the 8-bit version " & _
                 "that accesses timer register THx only." & vbCrLf & _
                 "Macro '" & r.v("T01_NAME_ClearTmr") & "L' is the 8-bit version " & _
                 "that accesses timer register TLx only." & vbCrLf & _
                 "The following constants are available for 'TimerNr':" & vbCrLf & _
                 r.v("T01_PARA_T01_TIMER_0") & ", " & r.v("T01_PARA_T01_TIMER_1")
  sReturnValue = "None"
  If (Not func.AddFunction("T01_NAME_ClearTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Number of timer to be cleared"
  If (Not func.AddParameter("T01_NAME_ClearTmr_Par1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_ReadTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro returns the 16-bit contents of the chosen timer by reading " & _
                 "the timer registers THx and TLx. The timer will not be stopped. The access " & _
                 "of the CPU to the registers of the timer is of higher priority than a timer increment. " & vbCrLf & _
                 "Macro '" & r.v("T01_NAME_ReadTmr") & "H' is the 8-bit version " & _
                 "that accesses timer register THx only." & vbCrLf & _
                 "Macro '" & r.v("T01_NAME_ReadTmr") & "L' is the 8-bit version " & _
                 "that accesses timer register TLx only." & vbCrLf & _
                 "The following constants are available for 'TimerNr':" & vbCrLf & _
                 r.v("T01_PARA_T01_TIMER_0") & ", " & r.v("T01_PARA_T01_TIMER_1")
  sReturnValue = "Current value of timer"
  If (Not func.AddFunction("T01_NAME_ReadTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Number of timer to be read"
  If (Not func.AddParameter("T01_NAME_ReadTmr_Par1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_LoadTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro loads the chosen timer with the passed 16-bit value by " & _
                 "writing it to the timer registers THx and TLx. " & _
                 "The timer will not be stopped. The access of the CPU to the " & _
                 "registers of the timer is of higher priority than a timer increment. " & vbCrLf & _
                 "Macro '" & r.v("T01_NAME_LoadTmr") & "H' is the 8-bit version " & _
                 "that accesses timer register THx only." & vbCrLf & _
                 "Macro '" & r.v("T01_NAME_LoadTmr") & "L' is the 8-bit version " & _
                 "that accesses timer register TLx only." & vbCrLf & _
                 "The following constants are available for 'TimerNr':" & vbCrLf & _
                 r.v("T01_PARA_T01_TIMER_0") & ", " & r.v("T01_PARA_T01_TIMER_1")
  sReturnValue = "None"
  If (Not func.AddFunction("T01_NAME_LoadTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Number of timer to be loaded"
  If (Not func.AddParameter("T01_NAME_LoadTmr_Par1", "TimerNr", "", sDescription)) Then Exit Function

  sDescription = "Value to be loaded"
  If (Not func.AddParameter("T01_NAME_LoadTmr_Par2", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_StartTmr1
  '------------------------------------------------------------------------------------------------
  sDescription = "When timer 0 is in mode 3 this macro can be used to turn timer 1 on by switching it " & _
                 "out of its own mode 3 into another mode. " & vbCrLf & _
                 "The following constants can be passed as parameter 'Mode':" & vbCrLf & _
                  r.v("T01_PARA_T01_MODE_0") & ", " & r.v("T01_PARA_T01_MODE_1") & ", " & r.v("T01_PARA_T01_MODE_2")
  sReturnValue = "None"
  If (Not func.AddFunction("T01_NAME_StartTmr1", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Mode to be switched to"
  If (Not func.AddParameter("T01_NAME_StartTmr1_Par1", "Mode", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_StopTmr1
  '------------------------------------------------------------------------------------------------
  sDescription = "When timer 0 is in mode 3 this macro can be used to stop timer 1 by switching " & _
                 "the mode of timer 1 to 3. " & _
                 "The contents of the timer registers TL1 and TH1 remains unchanged."

  sReturnValue = "None"
  If (Not func.AddFunction("T01_NAME_StopTmr1", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_IsrTmr0
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the timer 0 interrupt. " & _
                "It is called " & vbCrLf & _
                "- when TH0 overflows if timer 0 is in mode 0 or 1" & vbCrLf & _
                "- when TL0 overflows if timer 0 is in mode 2 or 3" & vbCrLf & _
                TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If (Not func.AddFunction("T01_NAME_IsrTmr0", "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T01_NAME_IsrTmr1
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the timer 1 interrupt. " & _
                "It is called " & vbCrLf & _
                "- when TH1 overflows if timer 1 is in mode 0 or 1 and timer 0 is not in mode 3" & vbCrLf & _
                "- when TL1 overflows if timer 1 is in mode 2 and timer 0 is not in mode 3" & vbCrLf & _
                "- when TH0 overflows if timer 0 is in mode 3" & vbCrLf & _
                TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If (Not func.AddFunction("T01_NAME_IsrTmr1", "void", sDescription, sReturnValue, 2)) Then Exit Function

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

  RefreshFunctionDescriptions = True
End Function

