'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.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
'        ----------------------------------------------------------------------
'        1.0  rkt            10.12.99    initialize version
'        1.1  ste            04.02.00    final version
'        1.2  ste            09.03.00    - function "RefreshParameterDescriptions" added
'                                        - IrDA mode deleted
'
'        1.3  ahe            08.10.00    final version
'                                        automatic calculation of FDV 
'                                        - cganges UpdateASCxBaudrate()
'                                        - cganges GetASCxMinBaudrate()
'                                        - cganges GetASCxMaxBaudrate()
'                                        - added FDV_Calculation()
'
'        1.4  ste            14.11.00    register names changed:
'                                        _SRCTB   =>   _TBSRC
'                                        _SRCT    =>   _TSRC
'                                        _SRCR    =>   _RSRC
'                                        _SRCE    =>   _ESRC
'                                        _BG_RL   =>   _BG_BR_VALUE
'                                        _FDV_FD  =>   _FDV_FD_VALUE
'                                        _RBUF_RD =>   _RBUF_RD_VALUE
'                                        _TBUF_TD =>   _TBUF_TD_VALUE
'        ---------------------------------------------------------------------
'        3.0  ste            25.01.01    final version for S-GOLDv1
'
'        ---------------------------------------------------------------------
'        4.0  ahe            14.02.01    final version for Pegasus
'
'        4.1  ahe            08.08.02     no changes
'
'*****************************************************************************

' use this constant in the Format statement for all calculated outputs like 
' baud rates or deviation
Global Const ASC_PRECISION = "0.000"


'*****************************************************************************
'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 sIndex As String
  Dim sPrefix As String

  sIndex = "ASC" & vIndex
  sPrefix = sIndex & "_"
  
  IsModuleClockDisabled = False            
  IsModuleClockDisabled = IsModuleDisabled(r, vIndex)
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(GetASCxDisebleBitName(r, vIndex) = "") Then
      If (r.v("SYSCON3_" & GetASCxDisebleBitName(r, vIndex)) = 1) Then
        IsModuleDisabled = True
      End If
  End If

End Function

'*****************************************************************************
'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 GetASCxDisebleBitName(r As ProjectData, vIndex As Variant) As String
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "ASC" & vIndex
    sRegPre = sVarPre & "_"

    If (r.Regs.BitFind("SYSCON3_" & sVarPre & "DIS") = True) Then
        GetASCxDisebleBitName = sVarPre & "DIS"
    ElseIf (r.Regs.BitFind("SYSCON3_PERDIS2") = True) Then
        GetASCxDisebleBitName = "PERDIS2"
    Else
        GetASCxDisebleBitName = ""
    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          parameter
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipModuleClockDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  Dim sIndex As String
  Dim sPrefix As String

  sIndex = "ASC" & vIndex
  sPrefix = sIndex & "_"
  
  GetTooltipModuleClockDisabled = ""            

  If (IsModuleClockDisabled(r, vIndex)) Then
      GetTooltipModuleClockDisabled =  GetTooltipModuleDisabled(r, vIndex)
  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          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 returns the module clock (in MHz) of the 
'        ASCx module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        module clock (in MHz)
'*****************************************************************************
Function GetASCxModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sIndex As String
  Dim v As String
  Dim sPrefix As String

  sIndex = "ASC" & vIndex
  sPrefix = sIndex & "_"

  GetASCxModuleClock = GetSystemClock(r)

End Function


'*****************************************************************************
'Description:
'         This function updates the ASCx baudrate.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'*****************************************************************************
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" & Format$(vIndex)
    sRegPre = "ASC" & Format$(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_M") = 0) Then                                   
      '--------------------------------------------------------------------
      'synchronous mode
      '--------------------------------------------------------------------
      r.l(sRegPre & "BG_BR_VALUE") = GetASCxModuleClock(r, vIndex) * 1000000 / _
                                     ( 4 * ( 2 + r.v(sRegPre & "CON_BRS") ) * vBaudrate) - 1

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

    Else
      '--------------------------------------------------------------------
      'asynchronous mode
      '--------------------------------------------------------------------
      If (r.v(sRegPre & "CON_FDE") = 0) Then                                          'no divider
        r.l(sRegPre & "BG_BR_VALUE") = GetASCxModuleClock(r, vIndex) * 1000000 / _
                                       ( 16 * ( 2 + r.v(sRegPre & "CON_BRS") ) * vBaudrate) - 1
        If (GetASCxRealBaudrate(r, vIndex) > GetASCxMaxBaudrate(r, vIndex)) Then   'if after calculation real baud rate > max. baud rate
          r.l(sRegPre & "BG_BR_VALUE") = r.l(sRegPre & "BG_BR_VALUE") + 1
        End If
    
      Else
        Call FDV_Calculation(r, vIndex)
      End If
    End If

End Sub


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

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

    If (r.v(sRegPre & "CON_M") = 0) Then
      '-----------------------------------------------------------------
      'sychronous mode
      '-----------------------------------------------------------------
      GetASCxMinBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                           ( 4 * ( 2 + r.v(sRegPre & "CON_BRS")) * 8192)
    Else
      '-----------------------------------------------------------------
      'asychronous mode
      '-----------------------------------------------------------------
      If (r.v(sRegPre & "CON_FDE") = 0) Then                                          'no divider
        GetASCxMinBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                             ( 16 * ( 2 + r.v(sRegPre & "CON_BRS")) * 8192)
      Else
        GetASCxMinBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 * _
                              1 / 512 / 16 / 8192
      End If
    End If
    Exit Function

GetASCxMinBaudrateError:
  GetASCxMinBaudrate = 1
  Exit Function
End Function


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

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  If (r.v(sRegPre & "CON_M") = 0) Then
    '-----------------------------------------------------------------
    'sychronous mode
    '-----------------------------------------------------------------
      GetASCxMaxBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                           ( 4 * ( 2 + r.v(sRegPre & "CON_BRS")) )
  Else
    '-----------------------------------------------------------------
    'asychronous mode
    '-----------------------------------------------------------------
    If (r.v(sRegPre & "CON_FDE") = 0) Then                                         'no divider
      GetASCxMaxBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                           ( 16 * ( 2 + r.v(sRegPre & "CON_BRS")) )
    Else

      GetASCxMaxBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / 16

    End If
  End If

  '-----------------------------------------------------------------
  'IrDA mode
  '-----------------------------------------------------------------
  If (r.v(sRegPre & "CON_M") = 2) Then
    If (GetASCxMaxBaudrate > 115200) Then
      GetASCxMaxBaudrate = 115200
    End If
  End If

End Function


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

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  If (r.v(sRegPre & "CON_M") = 0) Then
    '-----------------------------------------------------------------
    'sychronous mode
    '-----------------------------------------------------------------
    GetASCxRealBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                            (4 * (2 + r.v(sRegPre & "CON_BRS")) * _
                            (r.v(sRegPre & "BG_BR_VALUE") + 1) )
  Else
    '-----------------------------------------------------------------
    'asychronous mode
    '-----------------------------------------------------------------
    If (r.v(sRegPre & "CON_FDE") = 0) Then                                         'no divider
      GetASCxRealBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                            (16 * (2 + r.v(sRegPre & "CON_BRS")) * _
                            (r.v(sRegPre & "BG_BR_VALUE") + 1) )
    Else
      If (r.v(sRegPre & "FDV_FD_VALUE") = 0) Then                                  'if n = 0
        GetASCxRealBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 / _
                              (16 * (r.v(sRegPre & "BG_BR_VALUE") + 1) )
      Else
        GetASCxRealBaudrate = GetASCxModuleClock(r, vIndex) * 1000000 * _
                              r.v(sRegPre & "FDV_FD_VALUE") / _
                              (512 * 16 * (r.v(sRegPre & "BG_BR_VALUE") + 1) )
      End If
    End If
  End If

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           As Variant
'
'-----------------------------------------------------------------------------
'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" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"


  If (r.v(sRegPre & "CON_M") = 2) Then
    If (r.v(sRegPre & "CON_FDE") = 0) Then                                         'no divider
      GetMinBR_Value = GetASCxModuleClock(r, vIndex) * 1000000 / _
                            (16 * (2 + r.v(sRegPre & "CON_BRS")) * _
                            GetASCxMaxBaudrate(r, vIndex) ) - 1 + 0.4
    Else
      If (r.v(sRegPre & "FDV_FD_VALUE") = 0) Then                                  'if n = 0
        GetMinBR_Value = GetASCxModuleClock(r, vIndex) * 1000000 / _
                              (16 * GetASCxMaxBaudrate(r, vIndex) ) - 1 + 0.4
      Else
        GetMinBR_Value = GetASCxModuleClock(r, vIndex) * 1000000 * _
                              r.v(sRegPre & "FDV_FD_VALUE") / _
                              (512 * 16 * GetASCxMaxBaudrate(r, vIndex) ) - 1 + 0.4
      End If
    End If
  Else
    GetMinBR_Value = 0
  End If

End Function


'*****************************************************************************
'Description:
'        For each function which should be used for this module a line 
'        "If (Not func.AddFunction .." must be insert in this function.
'        For each parameter that should be used for this function a  
'        line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added function a project variable with the name 
'        "ASC_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
  
  sVarPre = "ASC" & Format$(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
  '------------------------------------------------------------------------------------------------
  sDescription = "This function writes a send data initialization word into " & _
                 "the transmit buffer 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_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 ASC_NAME_GetData
  '------------------------------------------------------------------------------------------------
  sDescription =  "This function reads out the content of the receive buffer register " & _
                  "which contains the received data."
  
  sReturnValue = "data that has been received" 
  If (Not func.AddFunction(sVarPre & "_NAME_GetData", "uword", sDescription, sReturnValue, 1)) Then Exit Function

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

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_Rx
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the receive interrupt service routine for the " & sVarPre & ". " & _
              	 "It is called if the data has been received. "  & vbcrlf & _
               	 TOOLTIP_ISR_USERCODE 

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

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

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_TxBuffer
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the transmit buffer interrupt service routine for " & _
                 "the " & sVarPre & ". It is called if the content of the transmit buffer has " & _
                 "been loaded into the transmit shift register." & vbcrlf & _
               	 TOOLTIP_ISR_USERCODE 

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_TxDataReady
  '------------------------------------------------------------------------------------------------
  sDescription = "This function can be used for checking up the status of the " & sVarPre & " " & _
                 "transmit interrupt flags. This shows when " & _
                 "the sending of data has terminated. By continuously " & _
                 "polling this flag 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_TxDataReady", "ubyte", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_TxBufFree
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro can be used for checking the status of the " & _
                 "transmit buffer register. The transmit buffer may only " & _
                 "be written to if it is empty, this implies that the previous " & _
                 "content of the transmit buffer have been loaded into the transmit shift " & _
                 "register."
  
  sReturnValue = "0 if buffer is not empty, else 1" 
  If (Not func.AddFunction(sVarPre & "_NAME_TxBufFree", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_RxDataReady
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro can be used for calling the status of the " & sVarPre & " " & _
                 "receiver interrupt flag. 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_RxDataReady", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_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_ErrorCheck", "ubyte", sDescription, sReturnValue, 1)) Then Exit Function

  If (IsAutobaudAvailable(r, vIndex)) Then
    '------------------------------------------------------------------------------------------------
    ' Description for ASC_NAME_Autobaud
    '------------------------------------------------------------------------------------------------
    sDescription = "This is the autobaud interrupt service routine for " & sVarPre & ". " & _
                   "It is called if the autobaud detection unit is enabled " & _
                   "and a start bit has been detected at the receive pin. " & vbcrlf & _
                   "Furthermore the autobaud interrupt is always generated after recognition of the " & _
                   "second character of the two-byte frame, this means after a successful autobaud " & _
                   "detection. If FCDETEN is set the autobaud detected interrupt is also " & _
                   "generated after the recognition of the first charecter of the two-byte frame. " & vbcrlf & _
                 	 TOOLTIP_ISR_USERCODE 

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

    '------------------------------------------------------------------------------------------------
    ' Description for ASC_NAME_GetAutobaudStatus
    '------------------------------------------------------------------------------------------------
    sDescription = "This macro returns the current status of the autobaud unit. " & vbCrLf & _
                   "The following return values are available:" & vbCrLf & _
                   " " & r.v(sVarPre & "_PARA_AUTOBAUD_WAITING") & ":" & vbCrLf & _
                   "    autobaud detection unit waits for the first 'a' or 'A'" & vbCrLf & _
                   " " & r.v(sVarPre & "_PARA_FIRST_CHARACTER_SMALL") & ":" & vbCrLf & _
                   "    small 'a' character detected (only if enabled)"  & vbCrLf & _
                   " " & r.v(sVarPre & "_PARA_FIRST_CHARACTER_CAPITAL") & ":" & vbCrLf & _
                   "    capital 'A' character detected (only if enabled)"  & vbCrLf & _
                   " " & r.v(sVarPre & "_PARA_SECOND_CHARACTER_SMALL") & ":" & vbCrLf & _
                   "    small 't' character detected"  & vbCrLf & _
                   " " & r.v(sVarPre & "_PARA_SECOND_CHARACTER_CAPITAL") & ":" & vbCrLf & _
                   "    capital 'T' character detected" 

    sReturnValue = "Current status of the autobaud unit" 
    If (Not func.AddFunction(sVarPre & "_NAME_GetAutobaudStatus", "", sDescription, sReturnValue, 0)) Then Exit Function
  End If

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

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_ReceiverOff
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro disables the receive function of the " & sVarPre & ". " & _
                 "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_ReceiverOff", "", sDescription, sReturnValue, 0)) Then Exit Function

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_StopBaudGen
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro stops the baud rate generator (" & sVarPre & " inactive)."

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

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_StartBaudGen
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro starts the baud rate generator (" & sVarPre & " active)."

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


  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_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  " & _
        		 "data 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_SendSlaveAdr", "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "8-bit address of the slave"
    If (Not func.AddParameter(sVarPre & "_NAME_SendSlaveAdr_Par1", "Address", "", sDescription)) Then Exit Function


  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_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. To achieve this, " & _
        		 "the function " & r.v(sVarPre & "_NAME_WakeUp") & " must be called. After all data " & _
        		 "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_OwnAddress", "ubyte", sDescription, sReturnValue, 1)) Then Exit Function
    sDescription = "own address"
    If (Not func.AddParameter(sVarPre & "_NAME_OwnAddress_Par1", "ubAddress", "ubyte", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_WakeUp
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro allows the slave (in a multiprocessor system) " & _
                 "to receive data from the master. The mode is switched " & _
                 "to 9-bit data asynchronous operation. Each received data " & _
                 "sets the interrupt request flag."
  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_WakeUp", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for ASC_NAME_GotoSleep
  '------------------------------------------------------------------------------------------------
  sDescription = "After the slave haves received all data 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_GotoSleep", "", sDescription, sReturnValue, 1)) Then Exit Function

  If (IsFIFOAvailable(r, vIndex)) Then
    '------------------------------------------------------------------------------------------------
    ' Description for ASC_NAME_FlushRxFIFO
    '------------------------------------------------------------------------------------------------
    sDescription = "This macro flushes the receive FIFO, this means the receive " & _
                   "FIFO is marked as empty."
    sReturnValue = "None" 
    If (Not func.AddFunction(sVarPre & "_NAME_FlushRxFIFO", "", sDescription, sReturnValue, 0)) Then Exit Function

    '------------------------------------------------------------------------------------------------
    ' Description for ASC_NAME_GetRxFIFOFillingLevel
    '------------------------------------------------------------------------------------------------
    sDescription = "This macro returns the current filling level of the receive FIFO. " & _
                   "This can be between 0 and 8 bytes." 
    sReturnValue = "Current filling level 0 - 8" 
    If (Not func.AddFunction(sVarPre & "_NAME_GetRxFIFOFillingLevel", "", sDescription, sReturnValue, 0)) Then Exit Function

    '------------------------------------------------------------------------------------------------
    ' Description for ASC_NAME_FlushTxFIFO
    '------------------------------------------------------------------------------------------------
    sDescription = "This macro flushes the transmit FIFO, this means the transmit " & _
                   "FIFO is marked as empty."
    sReturnValue = "None" 
    If (Not func.AddFunction(sVarPre & "_NAME_FlushTxFIFO", "", sDescription, sReturnValue, 0)) Then Exit Function

    '------------------------------------------------------------------------------------------------
    ' Description for ASC_NAME_GetTxFIFOFillingLevel
    '------------------------------------------------------------------------------------------------
    sDescription = "This macro returns the current filling level of the transmit FIFO. " & _
                   "This can be between 0 and 8 bytes." 
    sReturnValue = "Current filling level 0 - 8" 
    If (Not func.AddFunction(sVarPre & "_NAME_GetTxFIFOFillingLevel", "", sDescription, sReturnValue, 0)) Then Exit Function
  End If

  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

  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 (IsAutobaudAvailable(r, vIndex)) Then
    '------------------------------------------------------------------------------------------------
    ' Description for ASCx_PARA_AUTOBAUD_WAITING
    '------------------------------------------------------------------------------------------------
    sDescription = "this parameter is used as return value for the macro " & r.v(sVarPre & "_NAME_GetAutobaudStatus") &"()"
    If (Not para.AddParameter(sVarPre & "_PARA_AUTOBAUD_WAITING", sDescription)) Then Exit Function
    If (Not para.AddParameter(sVarPre & "_PARA_FIRST_CHARACTER_SMALL", sDescription)) Then Exit Function
    If (Not para.AddParameter(sVarPre & "_PARA_FIRST_CHARACTER_CAPITAL", sDescription)) Then Exit Function
    If (Not para.AddParameter(sVarPre & "_PARA_SECOND_CHARACTER_SMALL", sDescription)) Then Exit Function
    If (Not para.AddParameter(sVarPre & "_PARA_SECOND_CHARACTER_CAPITAL", sDescription)) Then Exit Function

   	If (Not para.Sort) Then Exit Function
  End If

  RefreshParameterDescriptions = True
End Function


Function IsSynchronMode(r As ProjectData, vIndex As Variant)	As Boolean
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sRegPre = "ASC" & Format$(vIndex) & "_"
  sVarPre = "ASC" & Format$(vIndex)

  IsSynchronMode = False
  IsSynchronMode = (r.v(sRegPre & "CON_M") = 0)
End Function


'*****************************************************************************
'Description:
'        This function calculate the optimum for baud rate if FDV is use.
'-----------------------------------------------------------------------------
'Parameters:
'        r        As ProjectData
'        vIndex   module identifier
'*****************************************************************************
Sub FDV_Calculation(r As ProjectData, vIndex As Variant)
  On Error Goto SubError

    Dim lBG_low As Long
    Dim lBG_high As Long
    Dim lMin_FDV As Long
    Dim lFDV_Counter As Long
    Dim lFDV As Long
    Dim lBG As Long
    Dim vADB_BG_l As Variant
    Dim vADB_BG_h As Variant
    Dim vADB_BG As Variant
    Dim iFDV_Start As Integer
    Dim dClock As Double

    Dim sASC_Req_Baudrate_Reg As String
    Dim sASC_BG_Reg As String
    Dim sASC_FDV_Reg As String
    Dim sVarPre As String
    Dim sRegPre As String

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '!!! BEI 16-BIT und 32-BIT UNTERSCHIEDLICH !!!
    '!!! BITTE ANPASSEN !!!
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    sVarPre = "ASC" & Format$(vIndex)
    sRegPre = "ASC" & Format$(vIndex) & "_"

    sASC_Req_Baudrate_Reg = sVarPre & "_Baudrate"   'rquired bazd rate
    sASC_BG_Reg = sRegPre & "BG_BR_VALUE"           'baud rate Register
    sASC_FDV_Reg = sRegPre & "FDV_FD_VALUE"         'FDV Register
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    dClock = GetASCxModuleClock(r, vIndex) * 1000000
    
    lMin_FDV = Fix((r.v(sASC_Req_Baudrate_Reg) * 16 * 512) / dClock)   'Ermittlung von FDV_min und Abschneiden der Nachkomastellen

    If (lMin_FDV = 0) Then            'wenn FDV = 0 dann lMin_FDV = 1
        lMin_FDV = 1
    End If

    lBG_low = Fix(lMin_FDV * dClock / (r.v(sASC_Req_Baudrate_Reg) * 512 *16) - 1)
    lBG_high = lBG_low + 1

    If (abs(lMin_FDV / 512 * dClock / (16 * (lBG_low + 1)) - r.v(sASC_Req_Baudrate_Reg)) < abs(lMin_FDV / 512 * dClock / (16 * (lBG_high + 1)) - r.v(sASC_Req_Baudrate_Reg))) Then
        lBG = lBG_low
    Else
        lBG = lBG_high
    End If
    
    If (lBG > 8191) Then    '8191 ist der max-Wert fr das baut rate - Register
        lBG = 0             'lBG = 0 ist wilkrlich gewhlt
        lFDV = 512          'lFDV = 512 ist wilkrlich gewhlt
    Else
        lFDV = lMin_FDV     'lFDV gehrt mit oben berechneten lBG zu einem Rechenpaar
    End If

    iFDV_Start = lMin_FDV + 1     'starten der For-Next-Schleife von FDV_Min + 1

    For lFDV_Counter = iFDV_Start To 512
        lBG_low = Fix(lFDV_Counter * dClock / (r.v(sASC_Req_Baudrate_Reg) * 512 *16) - 1)  'Berechnung von lBG oberhalb und unterhalb der 0-Achse
        lBG_high = lBG_low + 1

        If (lBG_low < 8192 Or lBG_high < 8192) Then   ' nur wenn 1 Wert kleiner als max-Wert fr das baud rate - Register

            vADB_BG_l = (abs(lFDV_Counter / 512 * dClock / (16 * (lBG_low  + 1)) - r.v(sASC_Req_Baudrate_Reg)))
            vADB_BG_h = (abs(lFDV_Counter / 512 * dClock / (16 * (lBG_high + 1)) - r.v(sASC_Req_Baudrate_Reg)))
            vADB_BG =   (abs(lFDV         / 512 * dClock / (16 * (lBG      + 1)) - r.v(sASC_Req_Baudrate_Reg)))

            If (vADB_BG_l < vADB_BG And lBG_low < 8192) Then   ' nur wenn kleiner als max-Wert fr das baud rate - Register
                lBG = lBG_low
                lFDV = lFDV_Counter
            End If

            If (vADB_BG_h < vADB_BG And lBG_high < 8192) Then  ' nur wenn kleiner als max-Wert fr das baud rate - Register 
                lBG = lBG_high
                lFDV = lFDV_Counter
            End If

        End If

    Next lFDV_Counter

    
    r.l(sASC_BG_Reg) = lBG
    r.v(sASC_FDV_Reg) = lFDV
    
    If (r.v(sASC_FDV_Reg) = 512) Then    'weil max-Wert 511, wenn jedoch 512 dann FDV = 0
        r.v(sASC_FDV_Reg) = 0
    End If
    
    
    Exit Sub

    SubError:

    r.l(sASC_BG_Reg) = 0
    r.v(sASC_FDV_Reg) = 0

End Sub

'*****************************************************************************
'Description:
'          This function update the IrDA mode 
'          
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'-----------------------------------------------------------------------------
'Returns:
'      none
'
'*****************************************************************************
Sub UpdateIrDA(r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim b As Double

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  If (IsIrDAAvailable(r, vIndex)) Then 

    b = r.v(sRegPre & "IRDA_REQ")
    
    If (b < GetIrDAReqMin(r, vIndex)) Then
      b = GetIrDAReqMin(r, vIndex)
    End If
    If (b > GetIrDAReqMax(r, vIndex)) Then
      b = GetIrDAReqMax(r, vIndex)
    End If
    
    r.v(sVarPre & "_IRDA_REQ") = b

    If (r.v(sRegPre & "PMW_IRPW") = 0) Then
      r.l(sRegPre & "PMW_PW_VALUE") = (b * GetASCxModuleClock(r, vIndex)) * 2
    Else  
      r.l(sRegPre & "PMW_PW_VALUE") = b * GetASCxModuleClock(r, vIndex)
    End If
  End If
  
End Sub

'*****************************************************************************
'Description:
'          This function returns the real puls width Or real min. PW (s) of the 
'          ASCx module depending on the baud rate and the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'-----------------------------------------------------------------------------
'Returns:
'      real puls width Or real min. puls width
'
'*****************************************************************************
Function GetIrDARealPW(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  If (r.v(sRegPre & "PMW_IRPW") = 0) Then
    GetIrDARealPW = (r.v(sRegPre & "PMW_PW_VALUE") \ 2) / GetASCxModuleClock(r, vIndex)
  Else
    GetIrDARealPW = r.v(sRegPre & "PMW_PW_VALUE") / GetASCxModuleClock(r, vIndex)
  End IF

End Function


'*****************************************************************************
'Description:
'          This function returns the puls width Or min. PW (s) of the 
'          ASCx module depending on the baud rate and the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'-----------------------------------------------------------------------------
'Returns:
'      puls width Or min. puls width
'
'*****************************************************************************
Function GetIrDAPW(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"
  GetIrDAPW = 1
  '--------------------------------------------
  'GetIrDAPW = PW
  '--------------------------------------------
  If (r.v(sRegPre & "PMW_IRPW") = 0) Then
    GetIrDAPW = 3 * 1000000 / (16 * GetASCxRealBaudrate(r, vIndex))

  '--------------------------------------------
  'GetIrDAPW = min. PW
  '--------------------------------------------  
  Else
    GetIrDAPW = (r.l(sRegPre & "PMW_PW_VALUE") \ 2) / GetASCxModuleClock(r, vIndex)
  End If

End Function


'*****************************************************************************
'Description:
'          This function returns min. input of the required text-box (us)
'          
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'-----------------------------------------------------------------------------
'Returns:
'      min. input (us)
'
'*****************************************************************************
Function GetIrDAReqMin(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "ASC"  & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  GetIrDAReqMin = 1 / GetASCxModuleClock(r, vIndex)

End Function


'*****************************************************************************
'Description:
'          This function returns max. input of the required text-box (us)
'          
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'-----------------------------------------------------------------------------
'Returns:
'      max. input (us)
'
'*****************************************************************************
Function GetIrDAReqMax(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim b As Double

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  If (r.v(sRegPre & "PMW_IRPW") = 0) Then
    GetIrDAReqMax = ((3 * 1000000 * GetASCxModuleClock(r, vIndex) / (GetASCxRealBaudrate(r, vIndex) _
                      * 8) - 1) \ 2) / GetASCxModuleClock(r, vIndex)
  
  b = (255 \ 2) / GetASCxModuleClock(r, vIndex)
  
  If (GetIrDAReqMax > b) Then
    GetIrDAReqMax = b
  End If


  '--------------------------------------------
  'GetIrDAReqMax = max. for min. PW-box
  '--------------------------------------------  
  Else
    GetIrDAReqMax = 255 / GetASCxModuleClock(r, vIndex)
  End If

End Function


'*****************************************************************************
'Description:
'          This function returns min. input of the PW_VALUE
'          
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex          
'
'-----------------------------------------------------------------------------
'Returns:
'      min. input of the PW_VALUE
'
'*****************************************************************************
Function GetIrDAMinPW_V(r As ProjectData, vIndex As Variant) As Long
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  If (r.v(sRegPre & "PMW_IRPW") = 0) Then
    GetIrDAMinPW_V = 2

  '--------------------------------------------
  'GetIrDAReqMax = max. for min. PW-box
  '--------------------------------------------  
  Else
    GetIrDAMinPW_V = 1
  End If

End Function

'*****************************************************************************
'Description:
'          This function returns max. input of the PW_VALUE
'          
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'-----------------------------------------------------------------------------
'Returns:
'      max. input of the PW_VALUE
'
'*****************************************************************************
Function GetIrDAMaxPW_V(r As ProjectData, vIndex As Variant) As Long
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim lValue As Long

  sVarPre = "ASC" & Format$(vIndex)
  sRegPre = "ASC" & Format$(vIndex) & "_"

  If (r.v(sRegPre & "PMW_IRPW") = 0) Then
    lValue = 1000000 * 6 * GetASCxModuleClock(r, vIndex) / (16 * GetASCxRealBaudrate(r, vIndex)) - 1
    If (lValue > 255) Then
      lValue = 255
    End If
    GetIrDAMaxPW_V = lValue
     
  '--------------------------------------------
  'GetIrDAReqMax = max. for min. PW-box
  '--------------------------------------------  
  Else
    GetIrDAMaxPW_V = 255
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if the IrDA mode is available for the
'        ASCx module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True if the IrDA mode exist, else False 
'*****************************************************************************
Function IsIrDAAvailable(r As ProjectData, vIndex As Variant)	As Boolean
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sVarPre = "ASC" & vIndex
  sRegPre = sVarPre & "_"

  IsIrDAAvailable = False            
  If (r.Regs.BitFind(sRegPre & "PMW_IRPW") = True) Then
    IsIrDAAvailable = True            
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if the Autobaud mode is available for the
'        ASCx module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True if the Autobaud mode exist, else False 
'*****************************************************************************
Function IsAutobaudAvailable(r As ProjectData, vIndex As Variant)	As Boolean
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sVarPre = "ASC" & vIndex
  sRegPre = sVarPre & "_"

  IsAutobaudAvailable = False            
  If (r.Regs.BitFind(sRegPre & "ABCON_ABEN") = True) Then
    IsAutobaudAvailable = True            
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if the Autobaud mode is selected for the
'        ASCx module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True if the Autobaud mode is selected, else False 
'*****************************************************************************
Function IsAutobaudSelected(r As ProjectData, vIndex As Variant)	As Boolean
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sVarPre = "ASC" & vIndex
  sRegPre = sVarPre & "_"

  If (IsAutobaudAvailable(r, vIndex)) Then
    IsAutobaudSelected = False            
    If (r.v(sRegPre & "ABCON_ABEN") = 1) Then
      IsAutobaudSelected = True            
    End If
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if the FIFO mode is available for the
'        ASCx module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True if the FIFO mode exist, else False 
'*****************************************************************************
Function IsFIFOAvailable(r As ProjectData, vIndex As Variant)	As Boolean
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sVarPre = "ASC" & vIndex
  sRegPre = sVarPre & "_"

  IsFIFOAvailable = False            
  If (r.Regs.BitFind(sRegPre & "RXFCON_RXFEN") = True And _
      r.Regs.BitFind(sRegPre & "TXFCON_TXFEN") = True) Then
    IsFIFOAvailable = True            
  End If

End Function


'*****************************************************************************
'Description:
'          This function sets a default value for the fractional divider
'          for the autobaud detection. If no standard baud rate can be set
'          then the next possible baudrates are set.
'          
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           As Variant
'
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetDefaultFDVForAutobaud(r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim lFDV As Long
  Dim i As Integer

  sVarPre = "ASC" & vIndex
  sRegPre = "ASC" & vIndex & "_"

  If (IsAutobaudAvailable(r, vIndex)) Then
    r.v(sRegPre & "CON_FDE") = 1
    For i = 0 To 8
      lFDV = (512 * (0.001200  * GetDivideFactor(8 - i))) / GetASCxModuleClock(r, vIndex)
      If (lFDV <= 511) Then 
        r.v(sRegPre & "FDV_FD_VALUE") = lFDV
        Exit For
      Else
        r.v(sRegPre & "FDV_FD_VALUE") = 511
      End If
    Next i
  End If

End Sub


'*****************************************************************************
'Description:
'        This function returns the FIXED divide factor between the prescaler
'        output and the baud rate to be detected.
'-----------------------------------------------------------------------------
'Parameters:
'        index of the baud rate numbering (0-9)
'-----------------------------------------------------------------------------
'Returns:
'        FIXED divide factor
'*****************************************************************************
Function GetDivideFactor(i As Integer)
  On Error Resume Next
  Dim Df(8) As Integer
  Df(0) = 48
  Df(1) = 96
  Df(2) = 192
  Df(3) = 288
  Df(4) = 576
  Df(5) = 1152
  Df(6) = 2304
  Df(7) = 4608
  Df(8) = 9216 
  GetDivideFactor = Df(i)
End Function


'*****************************************************************************
'Description:
'        This function checks if enough port pins selected for the module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            not enough port pins selected
'        False           else
'*****************************************************************************
Function IsPinInterfaceDeselected(r As ProjectData, vIndex As Variant) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "ASC" & vIndex
  sRegPre = sVarPre & "_"
  
  IsPinInterfaceDeselected = False            

  If (GetPinFunction(r, sVarPre & "_TxD_1") <> 2 Or _
      (GetPinFunction(r, sVarPre & "_RxD_1_I") <> 1 And _
       GetPinFunction(r, sVarPre & "_RxD_1_O") <> 2) _
      ) Then
    IsPinInterfaceDeselected = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function checks if the Autobaud mode is available for the
'        ASCx module and if so then the Page Autobaud will be set to default
'        values.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetPageAutobaudToDefault(r As ProjectData, vIndex As Variant)
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sRegPre = "ASC" & vIndex & "_"
  sVarPre = "ASC" & vIndex

  If (IsAutobaudAvailable(r, vIndex)) Then
    r.sfr(sRegPre & "ABCON") = 0
    Call DisableSRN(r, sVarPre, sRegPre &  "ABIC", "Autobaud")
    r.v(sRegPre & "CON_R") = 1
    r.v(sVarPre & "_USE_GetAutobaudStatus") = 0
  End If

End Sub

'*****************************************************************************
'Description:
'        This function sets the controls on page Control to a default value.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetPageControlToDefault(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sRegPre As String 
    Dim sVarPre As String 

    sRegPre = "ASC" & vIndex & "_"
    sVarPre = "ASC" & vIndex
  
    ' release all interrupts
    Call DisableSRN(r, sVarPre, sRegPre & "RIC", "Rx")
    Call DisableSRN(r, sVarPre, sRegPre & "EIC", "Error")
    Call DisableSRN(r, sVarPre, sRegPre & "TIC", "Tx")
    If (r.Regs.SfrFind(sRegPre & "TBIC") = True) Then
        Call DisableSRN(r, sVarPre, sRegPre & "TBIC", "TxBuffer")
    End If

    ' set default values
    r.v(sRegPre & "CON_M") = 1
    r.v(sRegPre & "CON_REN") = 0
    r.v(sRegPre & "CON_PEN_RXDI") = 0
    r.v(sRegPre & "CON_FEN") = 0
    r.v(sRegPre & "CON_OEN") = 0
    r.v(sRegPre & "CON_LB") = 0
    r.v(sRegPre & "CON_FDE") = 0
    r.v(sRegPre & "CON_STP") = 0
    r.v(sRegPre & "CON_ODD") = 0
    r.v(sRegPre & "FDV_FD_VALUE") = 0
End Sub


'*****************************************************************************
'Description:
'        This function set the module into the default.
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'        vIndex As String
'
'*****************************************************************************
Sub SetASCMode(r As ProjectData, vIndex As Variant, iMode As Integer)
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sRegPre = "ASC" & Format$(vIndex) & "_"
  sVarPre = "ASC" & Format$(vIndex)

  ' is the sync mode requested
  If (iMode = 0) Then 
    r.v(sRegPre & "CON_REN") = 0
    r.v(sRegPre & "CON_LB") = 0
    r.v(sRegPre & "CON_STP") = 0
    r.v(sRegPre & "CON_ODD") = 0
    r.v(sRegPre & "CON_PEN_RXDI") = 0
    r.v(sRegPre & "CON_FEN") = 0
    r.v(sRegPre & "CON_FDE") = 0
    r.v(sRegPre & "FDV_FD_VALUE") = 0
  End If

  ' is the IrDA mode requested
  If (iMode = 2) Then 
    r.v(sRegPre & "CON_STP") = 0
    r.v(sRegPre & "CON_ODD") = 0
    r.v(sRegPre & "CON_PEN_RXDI") = 0
    r.v(sRegPre & "CON_FEN") = 0
    r.v(sRegPre & "CON_FDE") = 0
  End If

  ' if a no parity mode requested or the previous mode was IrDA
  If (iMode = 0 Or iMode = 1 Or iMode = 4 Or iMode = 5 Or iMode = 2 Or r.v(sRegPre & "CON_M") = 2) Then 
    r.v(sRegPre & "CON_ODD") = 0
    r.v(sRegPre & "CON_PEN_RXDI") = 0
    Call ReleaseErrorInterrupt(r, vIndex)
  End If

  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(sRegPre & "CON_M") = iMode

  ' re-calculate values for baud rate
  Call UpdateASCxBaudrate(r, vIndex)
  Call UpdateIrDA(r, vIndex)

End Sub


'*****************************************************************************
'Description:
'        This function checks if the FIFO mode is available for the
'        ASCx module and if so then the Page FIFO will be set to default
'        values.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetPageFIFOToDefault(r As ProjectData, vIndex As Variant)
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sRegPre = "ASC" & vIndex & "_"
  sVarPre = "ASC" & vIndex

  If (IsFIFOAvailable(r, vIndex)) Then
    r.sfr(sRegPre & "RXFCON") = &H0100
    r.sfr(sRegPre & "TXFCON") = &H0100
    r.v(sVarPre & "_USE_FlushRxFIFO") = 0
    r.v(sVarPre & "_USE_GetRxFIFOFillingLevel") = 0
    r.v(sVarPre & "_USE_FlushTxFIFO") = 0
    r.v(sVarPre & "_USE_GetTxFIFOFillingLevel") = 0
  End If

End Sub


'*****************************************************************************
'Description:
'        This function sets the ASCx module to a defined state when it is 
'        disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetASCxToDefault(r As ProjectData, vIndex As Variant)
  On Error Resume Next

  Dim sRegPre As String 
  Dim sVarPre As String 

  sRegPre = "ASC" & vIndex & "_"
  sVarPre = "ASC" & vIndex

  If (IsModuleDisabled(r, vIndex)) Then
      Call ReleaseSyncPins(r, vIndex)
      Call ReleaseAsyncPins(r, vIndex)
  End If
  
  Call SetPageAutobaudToDefault(r, vIndex)
  Call SetPageControlToDefault(r, vIndex)
  Call SetPageFIFOToDefault(r, vIndex)

  ' re-calculate values for baud rate
  Call UpdateASCxBaudrate(r, vIndex)
  Call UpdateIrDA(r, vIndex)

  ' this is for page Functions !!
  r.v(sVarPre & "_USE_Init") = 0

End Sub


'*****************************************************************************
'Description:
'        This function checks if all error checks are disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r      As ProjectData
'        vIndex As String       number of ASC (e.g ASC0)
'-----------------------------------------------------------------------------
'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
  sRegPre = "ASC" & Format$(vIndex) & "_"
  
  IsErrorCheckDisabled = False 
  If (r.v(sRegPre & "CON_M") = 2) Then   ' if IrDA mode
    If (r.v(sRegPre & "CON_FEN") = 0 AND r.v(sRegPre & "CON_OEN") = 0) Then
      IsErrorCheckDisabled = True
    End If
  Else
    If (r.v(sRegPre & "CON_FEN") = 0 AND _
        r.v(sRegPre & "CON_OEN") = 0 AND _
        r.v(sRegPre & "CON_PEN_RXDI") = 0) Then
      IsErrorCheckDisabled = True
    End If
  End If
End Function


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

    sRegPre = "ASC" & Format$(vIndex) & "_"
    sVarPre = "ASC" & Format$(vIndex)

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

End Sub


'*****************************************************************************
'Description:
'        This function release all selected pins for the ASCx sync mode
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseSyncPins(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String
    Dim v As String

    sVarPre = "ASC" & vIndex
    sRegPre = sVarPre & "_"

    Call ReleaseAlternateOutputPin(r, sVarPre & "_TxD_1")
    Call ReleaseAlternateOutputPin(r, sVarPre & "_RxD_1_O")

End Sub


'*****************************************************************************
'Description:
'        This function release all selected pins for the ASCx async mode
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseAsyncPins(r As ProjectData, vIndex As Variant)
    On Error Resume Next
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s As String
    Dim v As String

    sVarPre = "ASC" & vIndex
    sRegPre = sVarPre & "_"

    Call ReleaseAlternateOutputPin(r, sVarPre & "_TxD_1")
    Call ReleaseAlternateInputPin(r, sVarPre & "_RxD_1_I")

End Sub


