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

Option Explicit

'*****************************************************************************
' Module:
'        SSC
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        SSC dialog script and in the SSC codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1    als         25-10-04      Initial version
'        0.2    als         26-11-04      SetSSCDefault function removed.
'                                         GetTooltipPinInterfaceDisabled function removed.
'                                         IsPinInterfaceDeselected function removed.
'                                         GetSSCDisebleBitName function removed.
'        0.3    als         02-12-04      - Function/Macro descriptions altered.
'                                         - "uByte" is changed to "ubyte" in function definitions.
'        0.4    Sunil       29-12-04      module prefix added.
'
'
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************
Global Const SSC_PRECISION = "0.000"


'*****************************************************************************
'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
'        "DUMMY_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

  RefreshFunctionDescriptions = False

  Dim sDescription  As String
  Dim sReturnValue  As String
  Dim sVarPre       As String
  Dim sRegPre       As String

  sVarPre = "SSC"
  sRegPre = sVarPre & "_"

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

  '------------------------------------------------------------------------------------------------
  ' Description for SSC_NAME_SendData
  '------------------------------------------------------------------------------------------------
  sDescription = "The master device can initiate the first data transfer by " & _
                 "writing the transmit data into transmit buffer. This value is " & _
                 "copied into the shift register (which is assumed to be empty at " & _
                 "this time), and the selected first bit of the transmit data is placed " & _
                 "onto the MTSR line on the next clock from the baud rate generator. " & vbCrLf & _
                 "A slave device immediately outputs the selected first bit (MSB or LSB " & _
                 "of the transfer data) at pin MRST, when the contents of the transmit " & _
                 "buffer are copied into the slave's shift register. "
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_SendData", "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Data to be send"
    If (Not func.AddParameter(sVarPre & "_NAME_SendData_Par1", "Data", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_GetData
  '------------------------------------------------------------------------------------------------
  sDescription = "This function returns the contents of the receive buffer. " & _
                 "When the receive interrupt request flag is set this implies " & _
                             "that data is available for reading in the receive buffer. "
  sReturnValue = "Received data"
  If (Not func.AddFunction(sVarPre & "_NAME_GetData", "ubyte", sDescription, sReturnValue, 1)) Then Exit Function

 '------------------------------------------------------------------------------------------------
 ' Description for SSCx_NAME_Error
 '------------------------------------------------------------------------------------------------
  sDescription = "This function checks if an error occured. With the help " & _
                 "of error flags it is possible to establish the cause of " & _
                 "errors. " & vbCrLf & _
                 "The possible values for the retuned error are: " & vbCrLf & _
                 "Bit 0 = 1   ->  Transmit error " & vbCrLf & _
                 "Bit 1 = 1   ->  receive error " & vbCrLf & _
                 "Bit 2 = 1   ->  phase error (only in slave mode) " & vbCrLf & _
                 "Bit 3 = 1   ->  baud rate error (only in slave mode) "

  sReturnValue = "Returns the error code"
  If (Not func.AddFunction(sVarPre & "_NAME_Error", "ubyte", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSC_NAME_Isr
  '------------------------------------------------------------------------------------------------
  sDescription = "This interrupt service routine is called when the contents " & _
                 "of the  Transmission start - Indicates that the transmit buffer " & _
                 "can be reloaded with new data." & _
                 "Transmission ends - The configured number of bits have been " & _
                 "transmitted and shifted to the receive buffer." & _
                 "error interrupt - Receive Error, Phase Error, Baud Rate Error" & _
                 "Transmit Error " & TOOLTIP_ISR_USERCODE

  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_Isr", "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSC_NAME_ProgrammingMode
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro sets the Programming Mode."
                
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_ProgrammingMode", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSC_NAME_OperatingMode
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro sets the OperatingMode."
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_OperatingMode", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSC_NAME_Busy
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro may be used for checking the status of the " & _
                        "busy flag. When a transfer starts, the busy flag " & _
                        "is set. At the end of a transfer (" & sRegPre & "TB is empty) the busy " & _
                        "flag is cleared."
  sReturnValue = "Returns a 1 if " & sVarPre & " is busy, else 0"
  If (Not func.AddFunction(sVarPre & "_NAME_Busy", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_SetLoopBackMode
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro sets the loop back mode of " & sVarPre & ". " & _
                 "In this mode the receive input is connected with transmit output (half-duplex mode). " & _
                 "To be reset this mode call the macro " & r.v(sVarPre & "_NAME_ResetLoopBackMode") & "." & _
                 "NOTE: To set LoopBack mode user has to switch to operating mode."

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

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_ResetLoopBackMode
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro resets the loop back mode of " & sVarPre & ". " & _
                 "To be set the loop back mode call the macro " & r.v(sVarPre & "_NAME_SetLoopBackMode") & "." & _
                 "NOTE: To reset LoopBack mode user has to switch to operating mode."

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


        If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function


'*****************************************************************************
'Description:

'         This function updates the SSC baudrate.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           index of module
'
'*****************************************************************************
Sub UpdateSSCBaudrate(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sVarPre   As String
    Dim sRegPre   As String
    Dim vBaudrate As Variant

    sVarPre = "SSC"
    sRegPre = sVarPre & "_"

    vBaudrate = r.d(sVarPre & "_Baudrate")
        
      If (vBaudrate < GetSSCMinBaudrate(r, vIndex)) Then
        vBaudrate = GetSSCMinBaudrate(r, vIndex)
      End If
      If (vBaudrate > GetSSCMaxBaudrate(r, vIndex)) Then
        vBaudrate = GetSSCMaxBaudrate(r, vIndex)
      End If
      
    r.d(sVarPre & "_Baudrate") = vBaudrate

    r.l(sRegPre & "BR") = GetSSCModuleClock(r, vIndex) * 1000000 / (2 * vBaudrate) - 1

End Sub


'*****************************************************************************
'Description:
'          This function returns the module clock (in MHz) of the
'          SSC module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'         r               reference to the Registers object
'         vIndex          ProjectData
'
'-----------------------------------------------------------------------------
'Returns:
'      SSC module clock [MHz]
'
'*****************************************************************************
Function GetSSCModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next

 GetSSCModuleClock = GetCoreClock(r) '26.7

End Function


'*****************************************************************************
'Description:
'          This function returns the minimum baudrate (in kbaud) of the
'          SSC module depending on the module clock and the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           module index
'
'-----------------------------------------------------------------------------
'Returns:
'      at a minimum possible baud rate [baud]
'
'*****************************************************************************
Function GetSSCMinBaudrate(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim v       As String

  sVarPre = "SSC"

  GetSSCMinBaudrate = GetSSCModuleClock(r, vIndex) * 1000000 / (2 * (65535 + 1))

End Function


'*****************************************************************************
'Description:
'          This function returns the maximum baudrate (in kbaud) of the
'          SSC module depending on the module clock and the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           module index
'
'-----------------------------------------------------------------------------
'Returns:
'      at a maximum possible baud rate [baud]
'
'*****************************************************************************
Function GetSSCMaxBaudrate(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim v       As String

  sVarPre = "SSC"
  sRegPre = sVarPre & "_"


  If (r.v(sRegPre & "CONH_P_MS") = 0) Then  ' if slave mode
    GetSSCMaxBaudrate = GetSSCModuleClock(r, vIndex) * 1000000 / (2 * (1 + 1))
  Else  ' if master mode
    GetSSCMaxBaudrate = GetSSCModuleClock(r, vIndex) * 1000000 / (2 * (0 + 1))
  End If

End Function


'*****************************************************************************
'Description:
'          This function returns the real baudrate (in kbaud) of the
'          SSC module depending on the module clock and the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           module index
'
'-----------------------------------------------------------------------------
'Returns:
'      the real-time baud rate [baud]
'
'*****************************************************************************
Function GetSSCRealBaudrate(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim v       As String

  sVarPre = "SSC"
  sRegPre = sVarPre & "_"

  GetSSCRealBaudrate = GetSSCModuleClock(r, vIndex) * 1000000 / _
                       (2 * (r.l(sRegPre & "BR") + 1))

End Function

'*****************************************************************************
'Description:
'        This function checks if the module clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            the module clock is disabled
'        False           else
'*****************************************************************************
Function IsModuleClockDisabled(r As ProjectData, vIndex As Variant) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "SSC" & vIndex
  sRegPre = sVarPre & "_"
  
  IsModuleClockDisabled = False
  
  IsModuleClockDisabled = r.v(SCU_PREFIX & "PMCON1_SSC_DIS") = 1

End Function


'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************

Function GetTooltipModuleClockDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
    Dim s As String
  
    s = ""
    s = FormatDisabledTooltip("the SSC module clock is disabled")
      
    GetTooltipModuleClockDisabled = s

End Function

'*****************************************************************************
'Description:
'        This function checks whether the pins are selected either in slave mode
' or in master mode. This function will return "True" only when any of the following
' pin combinations are selected.
'  1. Clock and Receive.
'  2. Clock and Transmit.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        True - if pins are selected else False
'*****************************************************************************
Function SSCPinSelected(r As ProjectData) As Boolean
    SSCPinSelected = False
    
    If r.v(SSC_PREFIX & "CONH_P_MS") = 0 Then
      If r.v(SSC_PREFIX & "SSEL_SIS") = 1 And r.v(SSC_PREFIX & "SSEL_CIS") = 1 Then SSCPinSelected = True
      If r.v(SSC_PREFIX & "SSEL_SIS") = 1 And r.v(SSC_PREFIX & "SSEL_MIS") = 1 Then SSCPinSelected = True
    Else
      If r.v(SSC_PREFIX & "MSEL_SIS") = 1 And r.v(SSC_PREFIX & "MSEL_CIS") = 1 Then SSCPinSelected = True
      If r.v(SSC_PREFIX & "MSEL_SIS") = 1 And r.v(SSC_PREFIX & "MSEL_MIS") = 1 Then SSCPinSelected = True
    End If

End Function
