Attribute VB_Name = "Module1"
'#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
'        ----------------------------------------------------------------------
'        1.0    ahe       17.02.00     final version for
'                                         C165UTAH
'                                         C165H
'                                         C161U
'
'        ----------------------------------------------------------------------
'        2.0    ahe       05.09.00     final version for
'                                         C167CS
'                                         C161PI
'                                         C161CS/JC/JI
'
'
'        ----------------------------------------------------------------------
'        3.0    ahe       25.10.00     final version for
'                                         C167CS
'                                         C161PI
'                                         C161CS/JC/JI
'                                         C167CR/SR r2
'
'                                      - changes IsModuleDisabled (check SYSCON3)
'
'               ahe       31.10.00     - changes the description for SendData
'                                      - changes the description for RxDataReady
'
'        ----------------------------------------------------------------------
'        4.0    ahe       20.04.01     final version for Pegasus
'
'        4.1    ahe       08.08.02     - neu interrupt pages
'        4.2    ste       16.12.02     function description of ..LoopBackMode changed
'        ----------------------------------------------------------------------
'        5.0    raj         14.08.04     final version for XC164CM Pegasus light
'                                      - Pin Selection Page 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(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 sRegPre       As String

  sVarPre = "SSC" & vIndex
  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 SSCx_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", "uwData", "uword", 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", "uword", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_TxBufFree
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro can be used for checking the status of the " & _
                 "transmit buffer register (" & sRegPre & "TB). The transmit buffer may only " & _
                 "be written to if it is empty, that means if the previous " & _
                 "contents of the transmit buffer have been loaded into the " & _
                 "shift register. "

  sReturnValue = "Returns a 1 if the transmit buffer is empty, else 0"
  If (Not func.AddFunction(sVarPre & "_NAME_TxBufFree", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_RxDataReady
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro can be used for calling the status of the " & sVarPre & " " & _
                 "receiver interrupt flag (" & sRegPre & "RIC_IE). This is to recognize " & _
                 "that the " & sVarPre & " received data. If data has been received, " & _
                 "the function " & r.v(sVarPre & "_Name_GetData") & " must be called. "

  sReturnValue = "Returns a 1 if received data is valid, else 0"
  If (Not func.AddFunction(sVarPre & "_NAME_RxDataReady", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_ErrorCheck
  '------------------------------------------------------------------------------------------------
  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   ->  receive error " & vbCrLf & _
                 "Bit 1 = 1   ->  phase error " & vbCrLf & _
                 "Bit 2 = 1   ->  baudrate error (only in slave mode) " & vbCrLf & _
                 "Bit 3 = 1   ->  transmit error (only in slave mode) "

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

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_Tx
  '------------------------------------------------------------------------------------------------
  sDescription = "This interrupt service routine is called when the contents " & _
                 "of the transmit buffer have been loaded into the shift register. " & _
                 "This means that the transmit buffer can now be loaded with the " & _
                 "next data. " & TOOLTIP_ISR_USERCODE

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

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_Rx
  '------------------------------------------------------------------------------------------------
  sDescription = "This interrupt service routine is called when the contents " & _
                   "of the shift register have been loaded into the receive buffer. " & _
                 "The receive buffer must be read before receiving the next data. " & _
                 "If the receive buffer is not read before receiving new " & _
                 "data, the receive error flag is set. " & vbCrLf & _
                 TOOLTIP_ISR_USERCODE

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

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_Error
  '------------------------------------------------------------------------------------------------
  sDescription = "This interrupt service routine is called when during the " & _
                        sVarPre & " tranfer an error occured. With the help of the error " & _
                        "flags it is possible to find out the exact cause of error. " & _
                        "Possible errors include reveice error, phase error, " & _
                        "baudrate error (only slave) and transmit error (only slave). " & vbCrLf & _
                        TOOLTIP_ISR_USERCODE

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

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_TransData
  '------------------------------------------------------------------------------------------------
  sDescription = "This function initiates a data transfer. This means up to " & _
                        "255 bytes can be sent without application control. The " & _
                        "data transfer must be carried out within the ISR. " & _
                        "Both forwarded pointers can reference the same " & _
                        "buffer, then the sent data is written over. " & _
                        "In slave mode this function may be used to enable the " & _
                        "master to access the data. " & _
                        "This function can only be used if the " & r.v(sVarPre & "_Name_Tx") & " and " & _
                        r.v(sVarPre & "_Name_Rx") & " functions are selected. "
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_TransData", "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Pointer to transmit buffer"
    If (Not func.AddParameter(sVarPre & "_NAME_TransData_Par1", "*TxBuffer", "uword", sDescription)) Then Exit Function
    sDescription = "Pointer to receive buffer"
    If (Not func.AddParameter(sVarPre & "_NAME_TransData_Par2", "*RxBuffer", "uword", sDescription)) Then Exit Function
    sDescription = "Size of buffer"
    If (Not func.AddParameter(sVarPre & "_NAME_TransData_Par3", "BufferSize", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_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_EnTransmitter
  '------------------------------------------------------------------------------------------------
  sDescription = "In slave mode a device may only switch its MRST pin to " & _
                 "output mode if chosen by the master (by chip select or " & _
                 "SW command). If the device is not chosen, the MRST pin has " & _
                 "to be switched to input mode (not valid for Wired-AND). " & _
                 "This macro configures the MRST pin as output and so " & _
                 "releases the transmitter function. " & vbCrLf & _
                 "Note: " & vbCrLf & _
                 "This need only apply when several slaves are available in " & _
                 "the system."
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_EnTransmitter", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SSCx_NAME_DisTransmitter
  '------------------------------------------------------------------------------------------------
  sDescription = "In slave mode a device may only switch its MRST pin to " & _
                 "output mode if chosen by the master (by chip select or " & _
                 "SW command). If the device is not chosen, the MRST pin has " & _
                 "to be switched to input mode (not valid for Wired-AND). " & _
                 "This macro configures the MRST pin as input and so " & _
                 "blocks the transmitter function. " & vbCrLf & _
                 "Note: " & vbCrLf & _
                 "This need only apply when several slaves are available in " & _
                 "the system."
  sReturnValue = "None"
  If (Not func.AddFunction(sVarPre & "_NAME_DisTransmitter", "", 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") & "."

  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") & "."

  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:
'        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 = "SSC" & 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 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" & vIndex
    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.sfr(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 = GetPeripheralClock(r)

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" & sVarPre

  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" & vIndex
  sRegPre = sVarPre & "_"


  If (r.v(sRegPre & "CON_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" & vIndex
  sRegPre = sVarPre & "_"

  GetSSCRealBaudrate = GetSSCModuleClock(r, vIndex) * 1000000 / _
                       (2 * (r.sfr(sRegPre & "BR") + 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          module index
'-----------------------------------------------------------------------------
'Returns:
'        disabled tooltip
'*****************************************************************************
Function GetTooltipModuleDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  
  GetTooltipModuleDisabled = ""
  GetTooltipModuleDisabled = GetTooltipModuleDisabled & _
                              FormatDisabledToolTip("the module is disabled")

End Function


'*****************************************************************************
'Description:
'        This function check  the SYSCON3 register and SSCEN,
'        whether the modul is disabled.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r              As ProjectData
'        vIndex          module index
'-----------------------------------------------------------------------------
'Returns:
'        disabled tooltip
'*****************************************************************************
Function GetTooltipSSCModuleDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next

  Dim sRegPre As String
  Dim sVarPre As String
  Dim a       As String

  a = ""
  sVarPre = "SSC" & vIndex
  sRegPre = sVarPre & "_"

  If (IsModuleDisabled(r, sVarPre)) Then
    a = a & GetTooltipModuleDisabled(r, sVarPre)
  End If

  If (IsPinInterfaceDeselected(r, vIndex) = True) Then
    a = a & GetTooltipPinInterfaceDisabled(r, vIndex)
  End If
  GetTooltipSSCModuleDisabled = a

End Function


'*****************************************************************************
'Description:
'        This function check  the SYSCON3 register, whether the modul is disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        vIndex          module index
'-----------------------------------------------------------------------------
'Returns:
'        True  -  if the modul is disabled
'        False -  else
'
'*****************************************************************************
Function IsModuleDisabled(r As ProjectData, vIndex As Variant) As Boolean

  IsModuleDisabled = False

  If Not (GetSSCDisebleBitName(r, vIndex) = "") Then
      If (r.v("SYSCON3_" & GetSSCDisebleBitName(r, vIndex)) = 1) Then
        IsModuleDisabled = True
      End If
  End If

End Function



'*****************************************************************************
'Description:
'        This function checks if all error checks are disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r                As ProjectData
'        vIndex           module index
'-----------------------------------------------------------------------------
'Returns:
'       False       if all error cheks disabled
'       True        else
'*****************************************************************************
Function IsErrorCheckDisabled(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 & "_"
  
  IsErrorCheckDisabled = False
  If (r.v(sRegPre & "CON_TEN_TE") = 0 And _
      r.v(sRegPre & "CON_REN_RE") = 0 And _
      r.v(sRegPre & "CON_PEN_PE") = 0 And _
      r.v(sRegPre & "CON_BEN_BE") = 0) Then

    IsErrorCheckDisabled = True
  End If
End Function



'*****************************************************************************
'Description:
'          This function checks if none error enabled. Is so then release this
'          interrupt (SSCx_EIC_EIE).
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                ProjectData
'        vIndex           module index
'
'*****************************************************************************

Sub ReleaseErrorInterrupt(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String

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

    If (r.v(sRegPre & "CON_TEN_TE") = 0 And _
        r.v(sRegPre & "CON_REN_RE") = 0 And _
        r.v(sRegPre & "CON_PEN_PE") = 0 And _
        r.v(sRegPre & "CON_BEN_BE") = 0) Then

        Call DisableSRN(r, sVarPre, sRegPre & "EIC", "Error")
        r.v(sVarPre & "_USE_ErrorCheck") = 0
    End If

End Sub

'*****************************************************************************
'Description:
'        This function set the SSC-Modul into the devault.
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'        vIndex          module index
'
'*****************************************************************************
Sub SetSSCDefault(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String

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

    If r.v(sRegPre & "CON_MS") = 0 Then
      Call ReleaseAlternateOutputPin(r, sVarPre & "_MRST_O")
      Call ReleaseAlternateInputPin(r, sVarPre & "_MTSR_I")
      Call ReleaseAlternateInputPin(r, sVarPre & "_SCLK_I")
    Else
      Call ReleaseAlternateInputPin(r, sVarPre & "_MRST_I")
      Call ReleaseAlternateOutputPin(r, sVarPre & "_MTSR_O")
      Call ReleaseAlternateOutputPin(r, sVarPre & "_SCLK_O")
    End If

    'default control page
    r.v(sRegPre & "CON_BM_BC") = 7  'BM Reg = 1 => 8 bit Data
                
    r.v(sRegPre & "CON_HB") = 0
    r.v(sRegPre & "CON_PH") = 0
    r.v(sRegPre & "CON_PO") = 0
    Call DisableSRN(r, sVarPre, sRegPre & "TIC", "Tx")
    Call DisableSRN(r, sVarPre, sRegPre & "RIC", "Rx")
    r.v(sRegPre & "CON_TEN_TE") = 0
    r.v(sRegPre & "CON_REN_RE") = 0
    r.v(sRegPre & "CON_PEN_PE") = 0
    r.v(sRegPre & "CON_BEN_BE") = 0
    r.v(sRegPre & "CON_AREN_BSY") = 0
    Call ReleaseErrorInterrupt(r, vIndex)    'unbedingt hier, da ErrorInterrupt muss erst = 0 sein

    'default function page
    r.v(sVarPre & "_USE_Init") = 0
    r.v(sVarPre & "_USE_SendData") = 0
    r.v(sVarPre & "_USE_GetData") = 0
    r.v(sVarPre & "_USE_TxBufFree") = 0
    r.v(sVarPre & "_USE_RxDataReady") = 0
    r.v(sVarPre & "_USE_ErrorCheck") = 0
    r.v(sVarPre & "_USE_TransData") = 0
    r.v(sVarPre & "_USE_Busy") = 0
    r.v(sVarPre & "_USE_EnTransmitter") = 0
    r.v(sVarPre & "_USE_DisTransmitter") = 0
  
  
    'default baud rat page
    r.v(sVarPre & "_Baudrate") = 100000
    Call UpdateSSCBaudrate(r, vIndex)

End Sub

'*****************************************************************************
'Description:
'        This function returns the disable bit name of SYSCON3 register (e.g SSCDIS).
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          module index
'
'-----------------------------------------------------------------------------
'Returns:
'       disable bit name
'*****************************************************************************
Function GetSSCDisebleBitName(r As ProjectData, vIndex As Variant) As String
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String

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

    If (r.Regs.BitFind("SYSCON3_" & sVarPre & "DIS") = True) Then
        GetSSCDisebleBitName = sVarPre & "DIS"
    ElseIf (r.Regs.BitFind("SYSCON3_PERDIS2") = True) Then
        GetSSCDisebleBitName = "PERDIS2"
    Else
        GetSSCDisebleBitName = ""
    End If


End Function

'*****************************************************************************
'Description:
'        This function check the SSCCON_SSCEN => pins not used
'-----------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        vIndex          module index
'-----------------------------------------------------------------------------
'Returns:
'        True  -  if SSCEN = 0 => pins not used
'        False -  else
'
'*****************************************************************************
Function IsPinInterfaceDeselected(r As ProjectData, vIndex As Variant) As Boolean

    IsPinInterfaceDeselected = False
    Dim sVarPre As String
    Dim sRegPre As String

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

    If (r.v(sRegPre & "CON_EN") = 0) Then
      IsPinInterfaceDeselected = True
    End If

End Function

'*****************************************************************************
'Description:
'        This function returns tooltip if the pins not use.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r              As ProjectData
'        vIndex          module index
'
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipPinInterfaceDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next

  Dim a As String
  a = ""
  'a = a & FormatDisabledToolTip("MRST, MTSR and SCLK pin's are not used")
  a = a & FormatDisabledToolTip("interface pin's are not used")

  GetTooltipPinInterfaceDisabled = a

End Function

'*****************************************************************************************************
'Description:
'        This function checks for the pins SSCx_SCIS, SSCx_SRIS and SSCx_STOS and variable SSCx_CON_MS
'-----------------------------------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        vIndex          module index
'-----------------------------------------------------------------------------------------------------
'Returns:
'        True  -  if  (SSCx_CON_MS = 0) AND ((SSCx_SCIS AND (SSCx_SRIS OR SSCx_STOS)) = 0)
'        False -  else
'
'*****************************************************************************************************
Function IsSlaveModeConfigurationCorrectForToolTip(r As ProjectData, vIndex As Variant) As Boolean

    IsSlaveModeConfigurationCorrectForToolTip = False
    Dim sVarPre As String
    Dim sRegPre As String

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

    If (r.v(sRegPre & "CON_MS") = 0) And (((r.v(sRegPre & "SCIS")) And ((r.v(sRegPre & "SRIS")) Or (r.v(sRegPre & "STOS")))) = 0) Then
        IsSlaveModeConfigurationCorrectForToolTip = True
    End If

End Function

'*****************************************************************************************************
'Description:
'        This function checks for the pins SSCx_MCOS, SSCx_MRIS and SSCx_MTOS and variable SSCx_CON_MS
'-----------------------------------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        vIndex          module index
'-----------------------------------------------------------------------------------------------------
'Returns:
'        True  -  if  (SSCx_CON_MS = 1) AND ((SSCx_MCOS AND (SSCx_MRIS OR SSCx_MTOS)) = 0)
'        False -  else
'
'*****************************************************************************************************
Function IsMasterModeConfigurationCorrectForToolTip(r As ProjectData, vIndex As Variant) As Boolean

    IsMasterModeConfigurationCorrectForToolTip = False
    Dim sVarPre As String
    Dim sRegPre As String

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

    If (r.v(sRegPre & "CON_MS") = 1) And (((r.v(sRegPre & "MCOS")) And ((r.v(sRegPre & "MRIS")) Or (r.v(sRegPre & "MTOS")))) = 0) Then
        IsMasterModeConfigurationCorrectForToolTip = True
    End If

End Function

'*****************************************************************************************************
'Description:
'        This function checks for the pins SSCx_SCIS, SSCx_SRIS and SSCx_STOS and variable SSCx_CON_MS
'-----------------------------------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        vIndex          module index
'-----------------------------------------------------------------------------------------------------
'Returns:
'        True  -  if  (SSCx_CON_MS = 0) AND ((SSCx_SCIS AND (SSCx_SRIS OR SSCx_STOS)) = 0)
'        False -  else
'
'*****************************************************************************************************
Function GetMasterSlaveConfigurationdisabled(r As ProjectData, vIndex As Variant) As Boolean

    GetMasterSlaveConfigurationdisabled = False
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "SSC" & vIndex
    sRegPre = sVarPre & "_"
    
    GetMasterSlaveConfigurationdisabled = Not ((r.v("SYSCON3_" & GetSSCDisebleBitName(r, vIndex)) = 0) And _
        (r.v(sRegPre & "MCOS") = 1) And ((r.v(sRegPre & "MRIS") = 1) Or (r.v(sRegPre & "MTOS") = 1)) Or _
        (r.v(sRegPre & "SCIS") = 1) And ((r.v(sRegPre & "SRIS") = 1) Or (r.v(sRegPre & "STOS") = 1)))

End Function
