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

Option Explicit
'*****************************************************************************
' Module:
'        ASCx
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the 
'        ASCx dialog script and in the ASCx codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'   ASCx 2.0   ahe         13.06.00     final version for C167CS
'
'        ----------------------------------------------------------------------
'   ASC1 1.0   ahe         07.08.00     final version for C161CS/JC/JI
'                                       - delete the TxBuff-ISR
'                                       - changes the S0TIC to XP4IC
'                                       - changes the S0RIC to XP5IC
'                                       - changes the S0EIC to XP6IC
'                                       - changes 'RxD0_I' to 'RxD" & vIndex & "_I'
'                                       - changes 'RxD0_O' to 'RxD" & vIndex & "_O'
'                                       - changes 'TxD0' to 'TxD" & vIndex & "'
'              ahe         11.08.00     - delete fct. 'TxBufFree'
'
'*****************************************************************************


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

' use this constant in the Format statement for all calculated outputs like 
' baud rates or deviation
Global Const ASC_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 
'        "ASCx_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 

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

  
  sVarPre = "ASC" & vIndex
  sRegPre = "S" & vIndex

  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, 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 ASC_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription(sVarPre)
  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_SendData
  '------------------------------------------------------------------------------------------------
  sFunctionName = "SendData"
  sDescription = "This function writes a send data initialization word into " & _
                 "the " & sRegPre & "TBUF register." & vbCrLf & vbCrLf & _
                 "Note: " & vbCrLf & _
                 "In a multiprocessor system the master with this function " & _
                 "has the possibility to send data to the selected slave. To " & _
                 "achieve this, the 9th bit must set on zero." 
  
  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "void", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "Data to be send"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Par1", "uwData", "uword", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_GetData
  '------------------------------------------------------------------------------------------------
  sFunctionName = "GetData"
  sDescription =  "This function reads out the content of the " & sRegPre & "RBUF register " & _
                  "which contains a received data byte."
  
  sReturnValue = "data that has been received" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "uword", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_Tx
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Tx"
  sDescription = "This is the transmit interrupt service routine for the " & sVarPre & ". " & _
               	 "It is called when the sending of data is terminated " & _
               	 "(" & sRegPre & "TIR is set). " & vbcrlf & _
               	 TOOLTIP_ISR_USERCODE 

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_Rx
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Rx"
  sDescription = "This is the receive interrupt service routine for the " & sVarPre & ". " & _
            	   "It is called if a byte has been received via " & sVarPre & _
               	 " (" & sRegPre & "RIR is set). " & vbcrlf & _
               	 TOOLTIP_ISR_USERCODE 

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_Error
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Error"
  sDescription = "This is the error interrupt service routine for the " & sVarPre & ". " & _
               	 "It is called if during receipt of a data byte a framing, " & _
               	 "parity or overflow error occured (only if enabled). " & vbcrlf & _
               	 TOOLTIP_ISR_USERCODE 

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_TxDataReady
  '------------------------------------------------------------------------------------------------
  sFunctionName = "TxDataReady"
  sDescription = "This function can be used for checking up the status of the " & _
                 "" & sVarPre & " transmitter interrupt flags (" & sRegPre & "TIR). This shows when " & _
                 "the sending of a byte has terminated. By continuously " & _
                 "polling the " & sRegPre & "TIR bit after the function " & _
                 "" & r.v(sVarPre & "_NAME_SendData") & " has been called, it is " & _
                 "possible to establish when the " & sVarPre & " has terminated its task."
  
  sReturnValue = "0 if transmitter is busy, else 1" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_RxDataReady
  '------------------------------------------------------------------------------------------------
  sFunctionName = "RxDataReady"
  sDescription = "This macro can be used for calling the status of the " & sVarPre & " " & _
                 "receiver interrupt flag (" & sRegPre & "RIR). This is to recognize that " & _
                 "the " & sVarPre & " received a byte. If a byte has been received, the " & _
                 "function " & r.v(sVarPre & "_NAME_GetData") & " must be called."
  
  sReturnValue = "0 receive data is invalid, else 1" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_ErrorCheck
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ErrorCheck"
  sDescription = "This function checks if an error occured during receipt. " & _
                 "Possible error codes are: " & vbCrLf & _
                 "    NO_ERROR (all bits = 0) => no error occured" & vbCrLf & _
                 "    FRAMING  (bit 0 = 1) => an framing error occured" & vbCrLf & _
                 "    PARITY   (bit 1 = 1) => an parity error occured" & vbCrLf & _
                 "    OVERRUN  (bit 2 = 1) => an overrun error occured"
 
  sReturnValue = "error code" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "ubyte", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_ReceiverOn
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ReceiverOn"
  sDescription = "This function releases the receive function of the " & sVarPre & ". " & _
                 "After initialization this function does not need to be " & _
                 "recalled. " & vbcrlf & _
                 "Note: " & vbcrlf & _
                 "The following function must be called everytime data is to " & _
                 "be received in synchronous mode."

  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_ReceiverOff
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ReceiverOff"
  sDescription = "This macro disables the receive function of the " & sVarPre & _
                 " channel. A data that is currently being received is " & _
                 "received to completion, including generation of the " & _
                 "receive interrupt request."

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_SendSlaveAdr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "SendSlaveAdr"
  sDescription = "In a multiprocessor system the master with this macro " & _
        		 "has the possibility to address one of several slaves. To " & _
        		 "achieve this, the 9th bit is set in the address byte in " & _
        		 "order to enable all slaves for reading. This in turn " & _
        		 "triggers an interrupt for all slaves. The sending of a " & _
        		 "byte with the function " & r.v(sVarPre & "_NAME_SendData") & " to a particular " & _
        		 "addressed slave, only causes an interrupt to occur with " & _
        		 "that addressed slave."
  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "8-bit address of the slave"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Par1", "Address", "", sDescription)) Then Exit Function


  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_OwnAddress
  '------------------------------------------------------------------------------------------------
  sFunctionName = "OwnAddress"
  sDescription = "In a multiprocessor system the slave with this function has " & _
                 "the possibility to compare the received address with his " & _
                 "own one. If the slave has been addressed, it must take " & _
                 "care of receiving the next data bytes. To achieve this, " & _
                 "the function " & r.v(sVarPre & "_NAME_WakeUp") & " must be called. After all data " & _
                 "bytes have been received from the master, the function " & _
                 "" & r.v(sVarPre & "_NAME_GotoSleep") & " has to be executed."

  sReturnValue = "1 if received address is conform with own one, else 0" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "bit", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "own address"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Par1", "ubAddress", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_WakeUp
  '------------------------------------------------------------------------------------------------
  sFunctionName = "WakeUp"
  sDescription = "This macro allows the slave (in a multiprocessor system) " & _
                 "to receive data bytes from the master. The mode is switched " & _
                 "to 9-bit data asynchronous operation. Each received data " & _
                 "byte sets the interrupt request flag."

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_GotoSleep
  '------------------------------------------------------------------------------------------------
  sFunctionName = "GotoSleep"
  sDescription = "After the slave havs received all data bytes from the " & _
                 "master, this function must be called. A receive interrupt " & _
                 "request flag is only set if the master sends an address byte."

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


  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_SetBaudrate
  '------------------------------------------------------------------------------------------------
  sFunctionName = "SetBaudrate"
  sDescription = "This macro loads the reload register of the baudrate " & _
                 "generator. The baudrate generator is loaded immediately " & _
                 "with the new value."
  
  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "13 bit value for the baudrate reload register"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Par1", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_StopBaudGen
  '------------------------------------------------------------------------------------------------
  sFunctionName = "StopBaudGen"
  sDescription = "This macro stops the baudrate generator (" & sVarPre & " inactive)."

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_StartBaudGen
  '------------------------------------------------------------------------------------------------
  sFunctionName = "StartBaudGen"
  sDescription = "This macro starts the baudrate generator (" & sVarPre & " active)."

  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", 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 = "ASC" & 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 ASCx baudrate.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r          reference to the Registers object
'        vIndex     ParameterValue
'
'-----------------------------------------------------------------------------
'Returns:
'      none
'
'*****************************************************************************
Sub UpdateASCxBaudrate(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim vBaudrate As Variant


    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex
  
    vBaudrate = r.d(sVarPre & "_Baudrate")
        If (vBaudrate < GetASCxMinBaudrate(r, vIndex)) Then
            vBaudrate = GetASCxMinBaudrate(r, vIndex)
        End If
        If (vBaudrate > GetASCxMaxBaudrate(r, vIndex)) Then
            vBaudrate =  GetASCxMaxBaudrate(r, vIndex)
        End If

    r.v(sVarPre & "_Baudrate") = vBaudrate

    If (r.v(sRegPre & "CON_" & sRegPre & "M") = 0) Then                                   
        '--------------------------------------------------------------------
        'synchronous mode
        '--------------------------------------------------------------------
        r.l(sRegPre & "BG_" & sRegPre & "BG") = GetASCxModuleClock(r, vIndex) * 1000000 / _
                                       ( 4 * ( 2 + r.v(sRegPre & "CON_" & sRegPre & "BRS") ) * vBaudrate) - 1
    Else
        '--------------------------------------------------------------------
        'asynchronous mode
        '--------------------------------------------------------------------
        r.l(sRegPre & "BG_" & sRegPre & "BG") = GetASCxModuleClock(r, vIndex) * 1000000 / _
                                         ( 16 * ( 2 + r.v(sRegPre & "CON_" & sRegPre & "BRS") ) * vBaudrate) - 1
    End If

    If (GetASCxRealBaudrate(r, vIndex) > GetASCxMaxBaudrate(r, vIndex)) Then   'if after calculation real baud rate > max. baud rate
        r.l(sRegPre & "BG_" & sRegPre & "BG") = r.l(sRegPre & "BG_" & sRegPre & "BG") + 1
    End If
End Sub

'*****************************************************************************
'Description:
'          This function returns the module clock (in MHz) of the 
'          ASCx module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r          reference to the Registers object
'        vIndex     ParameterValue
'
'-----------------------------------------------------------------------------
'Returns:
'      GetSystemClock
'
'*****************************************************************************
Function GetASCxModuleClock(r As ProjectData, vIndex As Variant) As Double
    On Error Resume Next
    Dim sIndex As String
    Dim v As String
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

    GetASCxModuleClock = GetSystemClock(r) 
End Function

'*****************************************************************************
'Description:
'          This function returns the minimum baudrate value of the 
'          ASCx module depending on the module clock and the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r          reference to the Registers object
'        vIndex     ParameterValue
'
'-----------------------------------------------------------------------------
'Returns:
'      at a minimum possible baud rate
'
'*****************************************************************************
Function GetMinBR_Value(r As ProjectData, vIndex As Variant) As Long
    On Error Resume Next
    Dim sIndex As String
    Dim v As String
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

    GetMinBR_Value = 0

End Function

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

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

    If (r.v(sRegPre & "CON_" & sRegPre & "M") = 0) Then
        '-----------------------------------------------------------------
        'sychronous mode
        '-----------------------------------------------------------------
        GetASCxMinBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                           ( 4 * ( 2 + r.v(sRegPre & "CON_" & sRegPre & "BRS")) * 8192)
    Else
        '-----------------------------------------------------------------
        'asychronous mode
        '-----------------------------------------------------------------
        GetASCxMinBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                             ( 16 * ( 2 + r.v(sRegPre & "CON_" & sRegPre & "BRS")) * 8192)
    End If

End Function


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

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

    If (r.v(sRegPre & "CON_" & sRegPre & "M") = 0) Then
        '-----------------------------------------------------------------
        'sychronous mode
        '-----------------------------------------------------------------
        GetASCxMaxBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                             ( 4 * ( 2 + r.v(sRegPre & "CON_" & sRegPre & "BRS")) )
    Else
        '-----------------------------------------------------------------
        'asychronous mode
        '-----------------------------------------------------------------
        GetASCxMaxBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                             ( 16 * ( 2 + r.v(sRegPre & "CON_" & sRegPre & "BRS")) )
    End If

End Function


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

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

    If (r.v(sRegPre & "CON_" & sRegPre & "M") = 0) Then
        '-----------------------------------------------------------------
        'sychronous mode
        '-----------------------------------------------------------------
        GetASCxRealBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                              (4 * (2 + r.v(sRegPre & "CON_" & sRegPre & "BRS")) * _
                              (r.v(sRegPre & "BG_" & sRegPre & "BG") + 1) )
    Else
        '-----------------------------------------------------------------
        'asychronous mode
        '-----------------------------------------------------------------
        GetASCxRealBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                              (16 * (2 + r.v(sRegPre & "CON_" & sRegPre & "BRS")) * _
                              (r.v(sRegPre & "BG_" & sRegPre & "BG") + 1) )
    End If

End Function

'*****************************************************************************
'Description:
'        This function set the ASC-Module into the devault.
'-----------------------------------------------------------------------------
'Parameters:
'        r          As ProjectData
'        vIndex     ParameterValue
'
'-----------------------------------------------------------------------------
'Returns:
'      none
'
'*****************************************************************************
Sub SetASCxDefault(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sRegPre As String 
    Dim sVarPre As String 

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

    If (GetPinFunction(r, "TxD" & vIndex) = 2) Then
        Call ReleaseAlternateOutputPin(r, "TxD" & vIndex)
    End If
    If (GetPinFunction(r, "RxD" & vIndex & "_I") = 1) Then
        Call ReleaseAlternateInputPin(r, "RxD" & vIndex & "_I")
    End If
    If (GetPinFunction(r, "RxD" & vIndex & "_O") = 2) Then
        Call ReleaseAlternateOutputPin(r, "RxD" & vIndex & "_O")
    End If

    r.v(sRegPre & "CON_" & sRegPre & "REN") = 0
    r.v(sRegPre & "CON_" & sRegPre & "PEN") = 0
    r.v(sRegPre & "CON_" & sRegPre & "FEN") = 0
    r.v(sRegPre & "CON_" & sRegPre & "OEN") = 0
    r.v(sRegPre & "CON_" & sRegPre & "LB") = 0
    r.v(sRegPre & "CON_" & sRegPre & "STP") = 0
    r.v(sRegPre & "CON_" & sRegPre & "ODD") = 0
    r.v(sRegPre & "CON_" & sRegPre & "DIR") = 0

    r.v(sVarPre & "_USE_Init") = 0
    r.v(sVarPre & "_USE_SendData") = 0
    r.v(sVarPre & "_USE_GetData") = 0
    r.v(sVarPre & "_USE_TxDataReady") = 0
    
    r.v(sVarPre & "_USE_RxDataReady") = 0
    r.v(sVarPre & "_USE_ErrorCheck") = 0
    r.v(sVarPre & "_USE_ReceiverOn") = 0
    r.v(sVarPre & "_USE_ReceiverOff") = 0
    r.v(sVarPre & "_USE_SendSlaveAdr") = 0
    r.v(sVarPre & "_USE_OwnAddress") = 0
    r.v(sVarPre & "_USE_WakeUp") = 0
    r.v(sVarPre & "_USE_GotoSleep") = 0
    r.v(sVarPre & "_USE_SetBaudrate") = 0
    r.v(sVarPre & "_USE_StopBaudGen") = 0
    r.v(sVarPre & "_USE_StartBaudGen") = 0

    Call DisableSRN(r, sVarPre, "XP4IC", "XP4IE", "Tx")
    Call DisableSRN(r, sVarPre, "XP5IC", "XP5IE", "Rx")
    Call DisableSRN(r, sVarPre, "XP6IC", "XP6IE", "Error")


End Sub

'*****************************************************************************
'Description:
'        This function checks if all error checks are disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r          As ProjectData
'        vIndex     ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'       False       if all error cheks disabled
'       True        else
'*****************************************************************************
Function IsErrorCheckDisabled(r As ProjectData, vIndex As Variant) As Boolean
    On Error Resume Next
    Dim sRegPre As String
    Dim sVarPre As String

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex
  
    IsErrorCheckDisabled = False            
  
    If (r.v(sRegPre & "CON_" & sRegPre & "FEN") = 0 AND _
        r.v(sRegPre & "CON_" & sRegPre & "OEN") = 0 AND _
        r.v(sRegPre & "CON_" & sRegPre & "PEN") = 0) Then

        IsErrorCheckDisabled = True
    End If

End Function


'*****************************************************************************
'Description:
'          This function checks if none error enabled. Is so then release this
'          interrupt (S0EIC_S0EIE).
'
'-----------------------------------------------------------------------------
'Parameters:
'        r          ProjectData  
'        vIndex     ParameterValue
'
'*****************************************************************************
Sub ReleaseErrorInterrupt(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sRegPre As String
    Dim sVarPre As String

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

  
    If (r.v(sRegPre & "CON_" & sRegPre & "FEN") = 0 AND _
        r.v(sRegPre & "CON_" & sRegPre & "OEN") = 0 AND _
        r.v(sRegPre & "CON_" & sRegPre & "PEN") = 0) Then

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

End Sub


'*****************************************************************************
'Description:
'        This function set the ASC-Modul into the devault for asynchron mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'        vIndex ParameterValue
'
'*****************************************************************************
Sub SetAsyncMode(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sRegPre As String
    Dim sVarPre As String

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex

    r.v(sRegPre & "CON_" & sRegPre & "OEN") = 0
    r.v(sRegPre & "CON_" & sRegPre & "LB") = 0
    r.v(sRegPre & "CON_" & sRegPre & "REN") = 0
    Call ReleaseErrorInterrupt(r, vIndex)
    Call DisableSRN(r, sVarPre, "XP5IC", "XP5IE", "Rx")

End Sub


'*****************************************************************************
'Description:
'        This function returns a tolltip if the Tx pin not used
'
'-----------------------------------------------------------------------------
'Parameters:
'        r              As Objekt Data
'        PinName        As String      name of the alternativ function (e.g TxD0)
'        vIndex         ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'        Tooltip    
'*****************************************************************************
Function  GetTooltipTransmitterDisabled(r As ProjectData, PinName As String, vIndex As String) As String
    On Error Resume Next

    Dim a As String
    a = FormatDisabledTooltip("Tx pin (" & GetPinName(PinName) & ") is not selected")
    GetTooltipTransmitterDisabled = a

End Function 

'*****************************************************************************
'Description:
'        This function returns a tolltip if ASCx module not used
'
'-----------------------------------------------------------------------------
'Parameters:
'        r              As Objekt Data
'        PinName        As String      name of the alternativ function (e.g TxD0)
'        vIndex         ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'        Tooltip    
'*****************************************************************************
Function  GetTooltipReceiverDisabled(r As ProjectData, PinName As String, vIndex As String) As String
    On Error Resume Next

    Dim a As String
    a = FormatDisabledTooltip("Rx pin (" & GetPinName(PinName) & ") is not selected")
    GetTooltipReceiverDisabled = a

End Function 




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

    If (r.Regs.BitFind("SYSCON3_" & sVarPre & "DIS") = True) Then
        GetASCxDisebleBitName = sVarPre & "DIS"
    ElseIf (r.Regs.BitFind("SYSCON3_PERDIS1") = True) Then
        GetASCxDisebleBitName = "PERDIS1"
    Else
        GetASCxDisebleBitName = ""
    End If


End Function


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

  IsModuleDisabled = False

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

End Function



'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipModuleDisabled(r As ProjectData, vIndex As Variant) As String
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "ASC" & vIndex
    sRegPre = "S" & vIndex
  
    GetTooltipModuleDisabled = ""            
    GetTooltipModuleDisabled =  GetTooltipModuleDisabled & FormatDisabledToolTip("the module is disabled")
End Function
