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

Option Explicit
'*****************************************************************************
' Module:
'        EBC
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the 
'        EBC dialog script and in the EBC codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0   ahe         27.04.00     final version  for C165UTAH
'
'        ----------------------------------------------------------------------
'        2.0   ahe         14.06.00     final version for C167CS
'
'        ----------------------------------------------------------------------
'        2.1   ahe         02.08.00     final version for C167CS and C161CS/JC/JI
'                                       - added fuction 'XPeripheralAreEnabled'
'                                       - added fuction 
'                                         'GetTooltipXPeripheralAreEnabled'
'              ahe         10.08.00     - changes fct 'SelectPinInterfaceP0H'
'                                         abd 'SelectPinInterfaceP0L'
'                                       - added fct 'Select_Pin_Interface_For_EBC'
'
'
'*****************************************************************************

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

'*****************************************************************************
'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 
'        "EBC_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 = "EBC"

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

  RefreshFunctionDescriptions = True
End Function


'*****************************************************************************
'Description:
'        This function checks if some port pins free.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        PinName         Alternatpinname
'        lowIndex        low index
'        highIndex       high index
'
'-----------------------------------------------------------------------------
'Returns:
'        True            port is free
'        False           else
'*****************************************************************************
Function IsPinInterfaceFree(r As ProjectData, PinName As String, lowIndex As Integer, highIndex As Integer) As Boolean
  On Error Resume Next
  Dim i As Integer
  
  IsPinInterfaceFree = False            

  For i = lowIndex To highIndex
    
      If (IsAlternateInputPinFree(r, PinName & i)) Then
        IsPinInterfaceFree = True
      Else
        IsPinInterfaceFree = False
        Exit Function
      End If

  Next i

End Function


'*****************************************************************************
'Description:
'        This function select the port pins on P0H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP0H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  If (r.v("COUNTER_P0H") = 0) Then
    For i = 8 To 15
      Call RequestAlternateOutputPin(r, "AD" & i)
    Next i
  End If
  r.v("COUNTER_P0H") = r.v("COUNTER_P0H") + 1

  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P0H_" & i) = 0
        r.v(GetOutputRegName("0H", i)) = 0
      Next i
    End If
  End If

End Sub

'*****************************************************************************
'Description:
'        This function select the port pins on P0L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP0L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer

  If (r.v("COUNTER_P0L") = 0) Then
    For i = 0 To 7
      Call RequestAlternateOutputPin(r, "AD" & i)
    Next i
  End If
  r.v("COUNTER_P0L") = r.v("COUNTER_P0L") + 1
  
  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P0L_" & i) = 0
        r.v(GetOutputRegName("0L", i)) = 0
      Next i
    End If
  End If

End Sub

'*****************************************************************************
'Description:
'        This function select the port pins on P1H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP1H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  If (r.v("COUNTER_P1H") = 0) Then
    For i = 8 To 15
      Call RequestAlternateOutputPin(r, "A" & i)
    Next i
  End If
  r.v("COUNTER_P1H") = r.v("COUNTER_P1H") + 1

  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P1H_" & i) = 0
      Next i
    End If
  End If

End Sub

'*****************************************************************************
'Description:
'        This function select the port pins on P1L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP1L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  If (r.v("COUNTER_P1L") = 0) Then
    For i = 0 To 7
      Call RequestAlternateOutputPin(r, "A" & i)
    Next i
  End If
  r.v("COUNTER_P1L") = r.v("COUNTER_P1L") + 1

  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P1L_" & i) = 0
      Next i
    End If
  End If

End Sub



'*****************************************************************************
'Description:
'        This function release the port pins on P0L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP0L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P0L") = r.v("COUNTER_P0L") - 1
  If (r.v("COUNTER_P0L") = 0) Then
    For i = 0 To 7
      Call ReleaseAlternateOutputPin(r, "AD" & i)  
    Next i
  End If

End Sub

'*****************************************************************************
'Description:
'        This function release the port pins on P0H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP0H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P0H") = r.v("COUNTER_P0H") - 1
  If (r.v("COUNTER_P0H") = 0) Then
    For i = 8 To 15
      Call ReleaseAlternateOutputPin(r, "AD" & i)  
    Next i
  End If

End Sub

'*****************************************************************************
'Description:
'        This function release the port pins on P1L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP1L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
    r.v("COUNTER_P1L") = r.v("COUNTER_P1L") - 1
    If (r.v("COUNTER_P1L") = 0) Then
      For i = 0 To 7
        Call ReleaseAlternateOutputPin(r, "A" & i)  
      Next i
    End If

End Sub

'*****************************************************************************
'Description:
'        This function release the port pins on P1H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP1H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
    r.v("COUNTER_P1H") = r.v("COUNTER_P1H") - 1
    If (r.v("COUNTER_P1H") = 0) Then
      For i = 8 To 15
        Call ReleaseAlternateOutputPin(r, "A" & i)  
      Next i
    End If

End Sub


'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the pins
'        A0 .. A15 or AD0 .. AD15 not free.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        PinName         Alternatpinname
'        lowIndex        low index
'        highIndex       high index
'
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipPinInterfaceDisabled(r As ProjectData, PinName As String, lowIndex As Integer, highIndex As Integer) As String
    On Error Resume Next
    Dim sVarPre As String
    Dim s As String
    Dim i As Integer

    sVarPre = "EBC"
  
    GetTooltipPinInterfaceDisabled = ""
    s = ""
  
    For i = lowIndex To highIndex
        If (Not(IsAlternateOutputPinFree(r, PinName & i))) Then
          s = s & GetDisabledAlternatePinTooltip(r, PinName & i)
        End If
    Next i

    GetTooltipPinInterfaceDisabled =  s
   
End Function

'*****************************************************************************
'Description:
'        This function checks if CAN module is used.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        True            CAN1 or CAN2 is enabled
'        False           else
'*****************************************************************************
Function IsCANInterfaceEnabled(r As ProjectData) As Boolean
  On Error Resume Next

  IsCANInterfaceEnabled = False

  If (r.v("SYSCON3_CAN1DIS") = 0 Or r.v("SYSCON3_CAN2DIS") = 0) Then
    IsCANInterfaceEnabled = True
  End If

End Function

'*****************************************************************************
'Description:
'        This function checks if X-peripheral are anabled in SYSCON3 register
'        (if exist)
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        True            one or more X-peripheral are enabled
'        False           else
'*****************************************************************************
Function XPeripheralAreEnabled(r As ProjectData) As Boolean
  On Error Resume Next

  XPeripheralAreEnabled = False

  If (r.Regs.BitFind("SYSCON3_ASC1DIS") = True) Then
      If (r.v("SYSCON3_ASC1DIS") = 0) Then
          goto OK
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_I2CDIS") = True) Then
      If (r.v("SYSCON3_I2CDIS") = 0) Then
          goto OK
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_SDLMDIS") = True) Then
      If (r.v("SYSCON3_SDLMDIS") = 0) Then
          goto OK
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_CAN2DIS") = True) Then
      If (r.v("SYSCON3_CAN2DIS") = 0) Then
          goto OK
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_CAN1DIS") = True) Then
      If (r.v("SYSCON3_CAN1DIS") = 0) Then
          goto OK
      End If
  End If

  Exit Function
  
  OK: 
  XPeripheralAreEnabled = True


End Function


'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the X-peripheral
'        are/is enabled
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipXPeripheralAreEnabled(r As ProjectData) As String
    On Error Resume Next
    Dim sVarPre As String
    Dim s As String

    sVarPre = "EBC"
  
    GetTooltipXPeripheralAreEnabled = ""
    s = ""

  If (r.Regs.BitFind("SYSCON3_ASC1DIS") = True) Then
      If (r.v("SYSCON3_ASC1DIS") = 0) Then
          s = s & FormatDisabledTooltip("ASC1 module is enabled")
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_I2CDIS") = True) Then
      If (r.v("SYSCON3_I2CDIS") = 0) Then
          s = s & FormatDisabledTooltip("I2C module is enabled")
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_SDLMDIS") = True) Then
      If (r.v("SYSCON3_SDLMDIS") = 0) Then
          s = s & FormatDisabledTooltip("SDLM module is enabled")
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_CAN1DIS") = True) Then
      If (r.v("SYSCON3_CAN1DIS") = 0) Then
          s = s & FormatDisabledTooltip("CAN1 module is enabled")
      End If
  End If

  If (r.Regs.BitFind("SYSCON3_CAN2DIS") = True) Then
      If (r.v("SYSCON3_CAN2DIS") = 0) Then
          s = s & FormatDisabledTooltip("CAN2 module is enabled")
      End If
  End If

  GetTooltipXPeripheralAreEnabled =  s
   
End Function



'*****************************************************************************
'Description:
'        This function select the port pins for EBC module. Do not call this
'        function if BUSCONx_BUSACTx is not = 0 !!! Only for switched between
'        BTYP's.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        iBus            number of the bus to be set
'        iValue          value of BTYP to be set
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub Select_Pin_Interface_For_EBC(r As ProjectData, iBus As Integer, iValue As Integer)
    Dim sBTYPReg As String

    sBTYPReg = "BUSCON" & iBus & "_BTYP"

    'release pin's
    If (r.v(sBTYPReg) = 0) Then
       'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP1H(r)
        Call ReleasePinInterfaceP1L(r)

    ElseIf (r.v(sBTYPReg) = 1) Then
        'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP0H(r)

    ElseIf (r.v(sBTYPReg) = 2) Then
        'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP0H(r)
        Call ReleasePinInterfaceP1H(r)
        Call ReleasePinInterfaceP1L(r)

    ElseIf (r.v(sBTYPReg) = 3) Then
        'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP0H(r)

    End If

    'select pin's
    If (iValue = 0) Then
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP1H(r)
        Call SelectPinInterfaceP1L(r)

    ElseIf (iValue = 1) Then
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP0H(r)

    ElseIf (iValue = 2) Then
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP0H(r)
        Call SelectPinInterfaceP1H(r)
        Call SelectPinInterfaceP1L(r)

    ElseIf (iValue = 3) Then
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP0H(r)

    End If

End Sub
