Attribute VB_Name = "Module1"
'#Uses "Format.inc"
'#Uses "Const.inc"
'#Uses "Global.inc"
'#Uses "..\classes\Port.cls"
'#Uses "..\classes\PortPin.cls"
'#Uses "..\io\include.inc
Option Explicit
'*****************************************************************************
' Module:
'        Port.inc
'-----------------------------------------------------------------------------
' Description:
'
'        This file contains global constants.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  Sunil          06-10-04    Initial Version
'        0.2  Sunil          20-10-04    Extra parameter "iAlternateFunction" added to function
'                                        RequestAlternateInputPin
'
'*****************************************************************************


'*****************************************************************************
'Description:
'          This function checks if the pin of the passed alternate function
'          is already in use as general purpose IO or a different alternate function
'          The result depends on the "exclusive" properties of both the passed pin
'          and the pins which are already in use.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True                  the alternate pin can be used
'        False                 else
'*****************************************************************************
Function IsAlternatePinFree(r As ProjectData, sAlternatePinName As String) As Boolean
    On Error Resume Next

    Dim p           As PortPin
    Dim sPinName    As String
    Dim sAltName    As String
    Dim l           As Long
    Dim bExcl       As Boolean

    IsAlternatePinFree = False
    Set p = ObjectCollection("PORTPINS")

    ' the exclusive property of the passed pin
    bExcl = p.AlternateExclusive(sAlternatePinName)

    ' the alternate pin's port pin name:
    sPinName = p.PinName(sAlternatePinName)

    If (r.v("MuxFor_" & sPinName) > 0) Then
        ' port pin is already used as general IO
        If (bExcl Or p.Exclusive(sPinName)) Then
            ' there is at least one pin function which can't be shared by other functions
            Exit Function
        End If
    End If

    ' search other alternate functions using the same pin
    For l = 1 To p.AlternateNameCount(sPinName)
        sAltName = p.AlternateName(sPinName, l)
        If (sAltName <> sAlternatePinName) Then
                If (r.v("MuxFor_" & sAltName) > 0) Then
                ' another alternate function already uses the pin
                If (bExcl Or p.AlternateExclusive(sAltName)) Then
                    ' there is at least one pin function which can't be shared by other functions
                    Exit Function
                End If
            End If
        End If
    Next l

    IsAlternatePinFree = True
End Function

'*****************************************************************************
'Description:
'        This function checks if the the passed port pin
'        is already in use for an alternate function
'        The result depends on the "exclusive" properties of both the passed pin
'        and the pins which are already in use.
'        Use this function only in the port module to check if the general
'        input pin function can be used.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sPinName              the name of the general input pin (specified
'                              with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True                  the general input pin can be used
'        False                 else
'*****************************************************************************
Function IsGeneralPinFree(r As ProjectData, sPinName As String) As Boolean
    On Error Resume Next

    Dim p           As PortPin
    Dim sAltName    As String
    Dim l           As Long
    Dim bExcl       As Boolean

    IsGeneralPinFree = False
    Set p = ObjectCollection("PORTPINS")

    ' the exclusive property of the passed pin
    bExcl = p.Exclusive(sPinName)

    ' search alternate functions using the same pin
    For l = 1 To p.AlternateNameCount(sPinName)
        sAltName = p.AlternateName(sPinName, l)
        If (r.v("MuxFor_" & sAltName) > 0) Then
            ' another alternate function already uses the pin
            If (bExcl Or p.AlternateExclusive(sAltName)) Then
                ' there is at least one pin function which can't be shared by other functions
                    Exit Function
            End If
        End If
        Next l

    IsGeneralPinFree = True
End Function


'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub RequestAlternateInputPin(r As ProjectData, sAlternatePinName As String)
        On Error Resume Next

        ' set: pin for alternate function is in use
        r.v("MuxFor_" & sAlternatePinName) = 1          '1 means input
        
        ' Port Direction Input is the default setting

End Sub

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub RequestAlternateOutputPin(r As ProjectData, sAlternatePinName As String, iAlternateFunction As Integer)
        On Error Resume Next

        Dim p           As PortPin
        Dim s           As String
        Dim iPort   As Integer
        Dim iPin        As Integer

        Set p = ObjectCollection("PORTPINS")

        ' get the pin name from alternate pin name
        s = p.PinName(sAlternatePinName)

        ' set: pin for alternate function is in use
        r.v("MuxFor_" & sAlternatePinName) = 2                  '2 means output

        ' the port number of the alternate pin:
        iPort = p.Port(s)

        
        iPin = p.Pin(s)

        ' Port2 doesnot have Alternative selection registers
        If iPort = 2 Then Exit Sub
        
        ' Set the Alternate Function SFR Bits:
        Select Case iAlternateFunction
            Case 1
                r.v("P" & iPort & "_ALTSEL0_P" & iPin) = 1
                r.v("P" & iPort & "_ALTSEL1_P" & iPin) = 0
            Case 2
                r.v("P" & iPort & "_ALTSEL0_P" & iPin) = 0
                r.v("P" & iPort & "_ALTSEL1_P" & iPin) = 1
            Case 3
                r.v("P" & iPort & "_ALTSEL0_P" & iPin) = 1
                r.v("P" & iPort & "_ALTSEL1_P" & iPin) = 1
        End Select

        ' Set the Port Direction SFR Bit to Output:
         r.v("P" & iPort & "_DIR_P" & iPin) = 1
        

End Sub

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseAlternateInputPin(r As ProjectData, sAlternatePinName As String)
  On Error Resume Next

        Dim p           As PortPin
        Dim s           As String
        Dim iPort   As Integer
        Dim iPin        As Integer
        Dim l       As Long
        Dim sPinName As String
        Dim sAltName As String

        Set p = ObjectCollection("PORTPINS")

        ' get the pin name from alternate pin name
        s = p.PinName(sAlternatePinName)

        ' set: pin for alternate function is not in use
        r.v("MuxFor_" & sAlternatePinName) = 0
  
        ' the port number of the alternate pin:
        iPort = p.Port(s)

        
        ' Manage the alternate function SFR:
         iPin = p.Pin(s)

        ' check if any alternate function is using this pin:
         sPinName = "P" & iPort & "_" & iPin
        For l = 1 To p.AlternateNameCount(sPinName)
           sAltName = p.AlternateName(sPinName, l)
           If (r.v("MuxFor_" & sAltName) > 0) Then
                Exit Sub        'at least one alternate function still uses the pin
           End If
        Next l

        ' Port2 doesnot have Alternative selection registers
        If iPort = 2 Then Exit Sub
                
        ' Port Direction Input is the default setting

        'reset the pull device settings(PUDSEL and PUDEN regs)
        Call ResetPullDevice (r, iPort, iPin)        

End Sub

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseAlternateOutputPin(r As ProjectData, sAlternatePinName As String)
  On Error Resume Next

        Dim p           As PortPin
        Dim s           As String
        Dim iPort   As Integer
        Dim iPin        As Integer
        Dim l       As Long
        Dim sPinName As String
        Dim sAltName As String

        Set p = ObjectCollection("PORTPINS")

        ' get the pin name from alternate pin name
        s = p.PinName(sAlternatePinName)

        ' set: pin for alternate function is not in use
        r.v("MuxFor_" & sAlternatePinName) = 0
  
        ' the port number of the alternate pin:
        iPort = p.Port(s)

        
        ' Manage the alternate function SFR:
        iPin = p.Pin(s)

        ' check if any alternate function is using this pin:
        sPinName = "P" & iPort & "_" & iPin
        For l = 1 To p.AlternateNameCount(sPinName)
            sAltName = p.AlternateName(sPinName, l)
            If (r.v("MuxFor_" & sAltName) > 0) Then
                Exit Sub        'at least one alternate function still uses the pin
            End If
        Next l
            
        ' Port2 doesnot have Alternative selection registers
        If iPort = 2 Then Exit Sub
        
        ' Set the Alternate Function SFR Bit to General I/O:
        r.v("P" & iPort & "_ALTSEL0_P" & iPin) = 0
        r.v("P" & iPort & "_ALTSEL1_P" & iPin) = 0

        ' Set the Port Direction SFR Bit to Input:
        r.v("P" & iPort & "_DIR_P" & iPin) = 0
        
        ' Reset the Open drain option
        r.v("P" & iPort & "_OD_P" & iPin) = 0
        
        'reset the pull device settings(PUDSEL and PUDEN regs)
        Call ResetPullDevice (r, iPort, iPin)

End Sub

'*****************************************************************************
'Description:
'        This function returns a string with the pin name (e.g. P3.2) for an
'        alternate pin.
'        Use this function in each module to get the pin name of an alternate
'        name.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The pin name of the alternate pin.
'*****************************************************************************
Function GetPinName(sAlternatePinName As String) As String

  Dim p As PortPin
  Dim s As String
  Dim iPort As Integer
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")

  ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  iPort = p.Port(s)
  iPin = p.Pin(s)
  GetPinName = "P" & iPort & "." & iPin

End Function

'*****************************************************************************
'Description:
'        This function returns a formatted string with the disabled tooltip
'        for an alternate pin. It checks all pins which are requested the
'        pin of this alternate pin.
'        Use this function in each module to get the disabled tooltip of an
'        alternate pin.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The disabled tooltip for the alternate pin.
'*****************************************************************************
Function GetDisabledAlternatePinTooltip(r As ProjectData, sAlternatePinName As String) As String
  On Error Resume Next

  Dim p As PortPin
  Dim s As String
  Dim l As Long
  Dim sTooltip As String

  Set p = ObjectCollection("PORTPINS")

  ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  sTooltip = ""

  ' if pin name is used as general IO
  If (r.v("MuxFor_" & s) > 0) Then
    sTooltip = sTooltip & FormatDisabledToolTip("pin " & sAlternatePinName & " is used as general IO " & GetPinName(sAlternatePinName))
  Else
        For l = 1 To p.AlternateNameCount(s)
        If (p.AlternateName(s, l) <> sAlternatePinName) Then
                If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                        sTooltip = sTooltip & FormatDisabledToolTip("the " & sAlternatePinName & " pin is used as " & p.AlternateDescription(p.AlternateName(s, l)))
                End If
        End If
        Next l
  End If
  GetDisabledAlternatePinTooltip = sTooltip

End Function

'*****************************************************************************
'Description:
'        This function returns a formatted string with the disabled tooltip
'        for an general pin. It checks all pins which are requested the
'        pin of this general pin.
'        Use this function only in the port module to get the disabled tooltip
'        of an general pin.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        PinName               the name of the general pin (specified
'                              with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The disabled tooltip for the general pin.
'*****************************************************************************
Function GetDisabledGeneralPinTooltip(r As ProjectData, PinName As String) As String
  On Error Resume Next

  Dim p As PortPin
  Dim s As String
  Dim l As Long
  Dim sTooltip As String

  Set p = ObjectCollection("PORTPINS")

  s = PinName
  sTooltip = ""

  For l = 1 To p.AlternateNameCount(s)
        If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
        sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as " & p.AlternateDescription(p.AlternateName(s, l)))
    End If
  Next l
  GetDisabledGeneralPinTooltip = sTooltip

End Function


'*****************************************************************************
'Description:
'        This function returns a formatted string with the complete enabled
'        tooltip for a general pin. The tooltip contains a list of all alternate
'        functions of the general pin. If no alternate functions are defined
'        for the pin, an empty string will be returned.
'        Use this function only in the port module to get the enabled tooltip
'        of a general pin.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sPinName              the name of the general pin (specified
'                              with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        the enabled tooltip for the general pin
'*****************************************************************************
Function GetEnabledGeneralPinTooltip(r As ProjectData, sPinName As String) As String
    On Error GoTo ErrorToolTip

    Dim p           As PortPin
    Dim lAltFun     As Long
    Dim lNumAltFunc As Long
    Dim sTooltip    As String

    Set p = ObjectCollection("PORTPINS")
    lNumAltFunc = p.AlternateNameCount(sPinName)

    If (lNumAltFunc > 0) Then
        sTooltip = "Alternate Functions of Port " & sPinName & ":"
        For lAltFun = 1 To lNumAltFunc
                sTooltip = sTooltip & FormatDisabledToolTip(p.AlternateDescription(p.AlternateName(sPinName, lAltFun)))
        Next lAltFun
    Else
        sTooltip = ""
    End If

    GetEnabledGeneralPinTooltip = sTooltip
    
    Exit Function

ErrorToolTip:
    errorbox "Error GetEnabledGeneralPinTooltip", Err.Description
End Function

