'#Uses "Format.inc"
'#Uses "Const.inc"
'#Uses "..\classes\Port.cls"
'#Uses "..\classes\PortPin.cls"

Option Explicit
'*****************************************************************************
' Module:
'        Port.inc
'-----------------------------------------------------------------------------
' Description:
'          This module contains functions that must be used for access
'          port pins.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0  ste            01.02.00    final version for TC10GP
'             ste            23.02.00    many changes for the TC1775
'
'        ---------------------------------------------------------------------
'        3.0  ahe            21.07.00    if Port P3 then ...  in Fkt's
'                                        - RequestAlternateInputPin
'                                        - RequestAlternateOutputPin
'                                        - ReleaseAlternateInputPin
'                                        - ReleaseAlternateOutputPin
'
'             ahe            27.07.00    - delete 'lOpDrain' in the Fct
'                                          'SetAlternatePinStatements'
'                                        - delete 'bfld' in the Fct
'                                          'SetAlternatePinStatements'
'
'             ahe            28.07.00    - changes 'Dim p As PortPin' to
'                                          'Dim p As Object'
'
'             ahe            04.08.00    - changes the function 'GetPinName'
'
'             ahe            08.08.00    - added 'sOpenDrainReg = GetOpenDrainRegName(vPort, iPin)'
'                                          and 'sOutputReg = GetOutputRegName(vPort, iPin)' in 
'                                          fct. 'ReleaseAlternateOutputPin'
'
'             ahe            04.09.00    - changes function 'GetPinFunction'
'
'
'*****************************************************************************

'*****************************************************************************
' @Global Constants
'*****************************************************************************

' 0 => unlimeted inputs can be used for the pin; input and output at same time 
'
' 1 => unlimeted inputs can be used for the pin; only input or output at same time 
'
' 2 => only one input can be used for the pin; only input or output at same time 
'
' 3 => only one input can be used for the pin; input and output at same time (tbd)

Global Const PIN_LOCKING = 1

 

'*****************************************************************************
'Description:
'        This function checks if an alternate input pin is free or not.
'        Use this function in each module to check if an alternate input pin 
'        function can be used.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate input pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True                  the alternate input pin can be used
'        False                 else
'*****************************************************************************
Function IsAlternateInputPinFree(r As ProjectData, sAlternatePinName As String) As Boolean
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim l As Long

  Set p = ObjectCollection("PORTPINS")

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


  Select Case (PIN_LOCKING)
      Case 0  ' unlimeted inputs can be used for the pin; input and output at same time 

          ' if the pin is an exclusive general pin or if sAlternatePinName is an exclusive pin 
          If (p.Exclusive(s) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
            If (r.v("MuxFor_" & s) > 0) Then
              IsAlternateInputPinFree = False
            End If
          End If
          
          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)

            ' only if alternate pin name not the function parameter sName (alternate name)
            If (p.AlternateName(s, l) <> sAlternatePinName) Then

              ' if the found alternate pin name is an exclusive pin or if sAlternatePinName is an exclusive pin 
              If (p.AlternateExclusive(p.AlternateName(s, l)) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
                If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                  IsAlternateInputPinFree = False
                  Exit For
                End If
              End If

            End If
         	Next l

      Case 1  ' unlimeted inputs can be used for the pin; only input or output at same time 

          ' if the pin is an exclusive general pin or if sAlternatePinName is an exclusive pin 
          If (p.Exclusive(s) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
            If (r.v("MuxFor_" & s) > 0) Then
              IsAlternateInputPinFree = False
            End If
          End If

          ' if pin name is used as output
          If (r.v("MuxFor_" & s) > 1) Then
              IsAlternateInputPinFree = False
          Else
            ' for all alternate pin names from pin name
          	For l = 1 To p.AlternateNameCount(s)
              ' only if alternate pin name not the function parameter sName (alternate name)
              If (p.AlternateName(s, l) <> sAlternatePinName) Then

                ' if the found alternate pin name is an exclusive pin or if sAlternatePinName is an exclusive pin 
                If (p.AlternateExclusive(p.AlternateName(s, l)) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
                  If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                    IsAlternateInputPinFree = False
                    Exit For
                  End If
                End If

                ' if the found alternate pin name is used as output
                If (r.v("MuxFor_" & p.AlternateName(s, l)) > 1) Then
                  IsAlternateInputPinFree = False
                  Exit For
                End If

              End If
           	Next l
          End If

      Case 2  ' only one input can be used for the pin; only input or output at same time 

          ' if pin name is used as in/output
          If (r.v("MuxFor_" & s) > 0) Then
              IsAlternateInputPinFree = False
          Else
            ' for all alternate pin names from pin name
          	For l = 1 To p.AlternateNameCount(s)
              ' only if alternate pin name not the function parameter sName (alternate name)
              If (p.AlternateName(s, l) <> sAlternatePinName) Then
                ' if the found alternate pin name is used as in/output
                If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                  IsAlternateInputPinFree = False
                  Exit For
                End If
              End If
           	Next l
          End If

      Case 3  ' only one input can be used for the pin; input and output at same time 
  End Select

End Function


'*****************************************************************************
'Description:
'        This function checks if an alternate output pin is free or not.
'        Use this function in each module to check if an alternate output pin 
'        function can be used.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate output pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True                  the alternate output pin can be used
'        False                 else
'*****************************************************************************
Function IsAlternateOutputPinFree(r As ProjectData, sAlternatePinName As String) As Boolean
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim l As Long

  Set p = ObjectCollection("PORTPINS")


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

  Select Case (PIN_LOCKING)
      Case 0  ' unlimeted inputs can be used for the pin; input and output at same time 

          ' if the pin is an exclusive general pin or if sAlternatePinName is an exclusive pin 
          If (p.Exclusive(s) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
            If (r.v("MuxFor_" & s) > 0) Then
              IsAlternateOutputPinFree = False
            End If
          End If

          ' if pin name is used as output
          If (r.v("MuxFor_" & s) > 1) Then
              IsAlternateOutputPinFree = False
          Else
            ' for all alternate pin names from pin name
          	For l = 1 To p.AlternateNameCount(s)

              ' only if alternate pin name not the function parameter sAlternatePinName (alternate name)
              If (p.AlternateName(s, l) <> sAlternatePinName) Then

                ' if the found alternate pin name is an exclusive pin or if sAlternatePinName is an exclusive pin 
                If (p.AlternateExclusive(p.AlternateName(s, l)) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
                  If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                    IsAlternateOutputPinFree = False
                    Exit For
                  End If
                End If

                ' if the found alternate pin name is used as output
                If (r.v("MuxFor_" & p.AlternateName(s, l)) > 1) Then
                  IsAlternateOutputPinFree = False
                  Exit For
                End If

              End If
           	Next l
          End If

      Case 1  ' unlimeted inputs can be used for the pin; only input or output at same time 

            ' if pin name is used as in/output
            If (r.v("MuxFor_" & s) > 0) Then
                IsAlternateOutputPinFree = False
            Else
              ' for all alternate pin names from pin name
            	For l = 1 To p.AlternateNameCount(s)
                ' only if alternate pin name not the function parameter sName (alternate name)
                If (p.AlternateName(s, l) <> sAlternatePinName) Then

                  ' if the found alternate pin name is used as in/output
                  If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                    IsAlternateOutputPinFree = False
                    Exit For
                  End If
                End If
             	Next l
            End If

      Case 2  ' only one input can be used for the pin; only input or output at same time 

          ' if pin name is used as in/output
          If (r.v("MuxFor_" & s) > 0) Then
            IsAlternateOutputPinFree = False
          Else
            ' for all alternate pin names from pin name
          	For l = 1 To p.AlternateNameCount(s)
              ' only if alternate pin name not the function parameter sName (alternate name)
              If (p.AlternateName(s, l) <> sAlternatePinName) Then

                  ' if the found alternate pin name is used as in/output
                  If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                    IsAlternateOutputPinFree = False
                    Exit For
                  End If
              End If
           	Next l
          End If

      Case 3  ' only one input can be used for the pin; input and output at same time 
          ' tbd
  End Select

End Function


'*****************************************************************************
'Description:
'        This function checks if an general input pin is free or not.
'        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 IsGeneralInputPinFree(r As ProjectData, sPinName As String) As Boolean
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim l As Long

  Set p = ObjectCollection("PORTPINS")

  IsGeneralInputPinFree = TRUE

  s = sPinName
  Select Case (PIN_LOCKING)
      Case 0  ' unlimeted inputs can be used for the pin; input and output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)      
            ' if the alternate name is an exclusive pin or the pin name is an exclusive pin
            If (p.AlternateExclusive(p.AlternateName(s, l)) = True Or p.Exclusive(s) = True) Then
              If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                IsGeneralInputPinFree = False
                Exit For
              End If
            End If
         	Next l

      Case 1  ' unlimeted inputs can be used for the pin; only input or output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)      
            ' if the alternate name is an exclusive pin or the pin name is an exclusive pin
            If (p.AlternateExclusive(p.AlternateName(s, l)) = True Or p.Exclusive(s) = True) Then
              If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                IsGeneralInputPinFree = False
                Exit For
              End If
            Else
              ' if the found alternate pin name is used as output
              If (r.v("MuxFor_" & p.AlternateName(s, l)) = 2) Then
                IsGeneralInputPinFree = False
                Exit For
              End If
            End If
         	Next l

      Case 2  ' only one input can be used for the pin; only input or output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)      
            ' if the found alternate pin name is used as in/output
            If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
              IsGeneralInputPinFree = False
              Exit For
            End If
         	Next l

      Case 3  ' only one input can be used for the pin; input and output at same time 
          ' tbd
  End Select

End Function


'*****************************************************************************
'Description:
'        This function checks if an general output pin is free or not.
'        Use this function only in the port module to check if the general 
'        output pin function can be used.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sPinName              the name of the general output pin (specified
'                              with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True                  the general output pin can be used
'        False                 else
'*****************************************************************************
Function IsGeneralOutputPinFree(r As ProjectData, sPinName As String) As Boolean
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim l As Long

  Set p = ObjectCollection("PORTPINS")

  IsGeneralOutputPinFree = TRUE

  s = sPinName
  Select Case (PIN_LOCKING)
      Case 0  ' unlimeted inputs can be used for the pin; input and output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)      
            ' if the alternate name is an exclusive pin or the pin name is an exclusive pin
            If (p.AlternateExclusive(p.AlternateName(s, l)) = True Or p.Exclusive(s) = True) Then
              If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
                IsGeneralOutputPinFree = False
                Exit For
              End If
            Else
              ' if the found alternate pin name is used as output
              If (r.v("MuxFor_" & p.AlternateName(s, l)) = 2) Then
                IsGeneralOutputPinFree = False
                Exit For
              End If
            End If
         	Next l

      Case 1  ' unlimeted inputs can be used for the pin; only input or output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)      
            ' if the found alternate pin name is used as in/output
            If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
              IsGeneralOutputPinFree = False
              Exit For
            End If
         	Next l

      Case 2  ' only one input can be used for the pin; only input or output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)      
            ' if the found alternate pin name is used as in/output
            If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
              IsGeneralOutputPinFree = False
              Exit For
            End If
         	Next l

      Case 3  ' only one input can be used for the pin; input and output at same time 
          ' tbd
  End Select

End Function


'*****************************************************************************
'Description:
'        This function sets all necessary registers to configure  an alternate
'        input pin.
'        Use this function in each module to set an alternate pin to input.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate input pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub RequestAlternateInputPin(r As ProjectData, sAlternatePinName As String)
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")
 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)

  If (vPort = "3") Then
    r.v(GetOutputRegName(vPort, iPin)) = 1
  End If


  ' set Mux to input
  r.v("MuxFor_" & sAlternatePinName) = 1

End Sub


'*****************************************************************************
'Description:
'        This function sets all necessary registers to configure an alternate
'        output pin.
'        Use this function in each module to set an alternate pin to output.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate output pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub RequestAlternateOutputPin(r As ProjectData, sAlternatePinName As String)
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer
  Dim sDirectionReg As String
  Dim sAlternateSelect0Reg As String
  Dim sAlternateSelect1Reg As String

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)

  If (vPort = "3") Then
    r.v(GetOutputRegName(vPort, iPin)) = 1
  End If

  ' set Mux to output
  r.v("MuxFor_" & sAlternatePinName) = 2

  sDirectionReg = GetDirectionRegName(vPort, iPin)
  If (r.Regs.BitFind(sDirectionReg) = True) Then
    ' set direction register to output
    r.v(sDirectionReg) = 1  
  End If
  
  ' set alternate select register
  Select Case (p.AlternateFunctionNr(sAlternatePinName))
    Case 0        
        sAlternateSelect0Reg = GetAlternateSelect0RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect0Reg) = True) Then
          r.v(sAlternateSelect0Reg) = 0
        End If
        sAlternateSelect1Reg = GetAlternateSelect1RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect1Reg) = True) Then
          r.v(sAlternateSelect1Reg) = 0
        End If
    Case 1
        sAlternateSelect0Reg = GetAlternateSelect0RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect0Reg) = True) Then
          r.v(sAlternateSelect0Reg) = 1
        End If
        sAlternateSelect1Reg = GetAlternateSelect1RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect1Reg) = True) Then
          r.v(sAlternateSelect1Reg) = 0
        End If
    Case 2
        sAlternateSelect0Reg = GetAlternateSelect0RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect0Reg) = True) Then
          r.v(sAlternateSelect0Reg) = 0
        End If
        sAlternateSelect1Reg = GetAlternateSelect1RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect1Reg) = True) Then
          r.v(sAlternateSelect1Reg) = 1
        End If
    Case 3
        sAlternateSelect0Reg = GetAlternateSelect0RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect0Reg) = True) Then
          r.v(sAlternateSelect0Reg) = 1
        End If
        sAlternateSelect1Reg = GetAlternateSelect1RegName(vPort, iPin)
        If (r.Regs.BitFind(sAlternateSelect1Reg) = True) Then
          r.v(sAlternateSelect1Reg) = 1
        End If
  End Select

End Sub


'*****************************************************************************
'Description:
'        This function sets all necessary registers to release an alternate
'        input pin which was requested with the function 
'        'RequestAlternateInputPin'.
'        Use this function in each module to release an alternate input pin.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate input pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseAlternateInputPin(r As ProjectData, sAlternatePinName As String)
  On Error Resume Next

  Dim p As Object

  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer
  Dim l As Long
  Dim iHelp As Integer

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)



  If (vPort = "3") Then
    If (p.Exclusive(sAlternatePinName)) Then
      r.v(GetOutputRegName(vPort, iPin)) = 0
    Else
  	  For l = 1 To p.AlternateNameCount(s)
        ' only if alternate pin name not the function parameter sName (alternate name)
        If (p.AlternateName(s, l) <> sAlternatePinName) Then
          ' if the found alternate pin name is used as output
          'If (r.v("MuxFor_" & p.AlternateName(s, l)) = 2) Then

          ' if the found alternate pin name is used as in/output
          If (r.v("MuxFor_" & p.AlternateName(s, l)) > 0) Then
            iHelp = 1
          End If
        End If
   	  Next l
    End If
    If (iHelp = 0) Then
      r.v(GetOutputRegName(vPort, iPin)) = 0
    End If
  End If

  ' set Mux to input
  r.v("MuxFor_" & sAlternatePinName) = 0

End Sub


'*****************************************************************************
'Description:
'        This function sets all necessary registers to release an alternate
'        output pin which was requested with the function 
'        'RequestAlternateOutputPin'.
'        Use this function in each module to release an alternate output pin.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate output pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseAlternateOutputPin(r As ProjectData, sAlternatePinName As String)
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim l As Long
  Dim vPort As Variant
  Dim iPin As Integer
  Dim sDirectionReg As String
  Dim sAlternateSelect0Reg As String
  Dim sAlternateSelect1Reg As String
  Dim sOpenDrainReg As String
  Dim sOutputReg As String
  Dim sOutputDriverEnableReg As String


  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)

  If (vPort = "3") Then
    r.v(GetOutputRegName(vPort, iPin)) = 0
  End If

  ' set Mux to input
  r.v("MuxFor_" & sAlternatePinName) = 0

  sDirectionReg = GetDirectionRegName(vPort, iPin)
  If (r.Regs.BitFind(sDirectionReg) = True) Then
    ' set direction register to input
    r.v(sDirectionReg) = 0
  End If
  
  ' set alternate select register to a default value (general output function)
  sAlternateSelect0Reg = GetAlternateSelect0RegName(vPort, iPin)
  If (r.Regs.BitFind(sAlternateSelect0Reg) = True) Then
    r.v(sAlternateSelect0Reg) = 0
  End If
  sAlternateSelect1Reg = GetAlternateSelect1RegName(vPort, iPin)
  If (r.Regs.BitFind(sAlternateSelect1Reg) = True) Then
    r.v(sAlternateSelect1Reg) = 0
  End If

  sOpenDrainReg = GetOpenDrainRegName(vPort, iPin)
  If (r.Regs.BitFind(sOpenDrainReg) = True) Then
    ' set open drain register to push/pull
    r.v(sOpenDrainReg) = 0
  End If

  sOutputReg = GetOutputRegName(vPort, iPin)
  If (r.Regs.BitFind(sOutputReg) = True) Then
    ' set output register to low
    r.v(sOutputReg) = 0
  End If

  If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
    r.v(sOutputDriverEnableReg) = 0
  End If

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
  On Error Resume Next

  Dim p As Object
  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  If Not(s = "") Then
    vPort = p.Port(s)
    iPin = p.Pin(s)
    GetPinName = "P" & vPort & "." & iPin
  Else
    GetPinName = ""
  End If

End Function


'*****************************************************************************
'Description:
'        This function sets the output register (OUT) dependent from an 
'        alternate pin name.
'        Use this function in each module to set the output register of an 
'        alternate pin.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'        value                 value for the register (0 or 1)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetOUTRegister(r As ProjectData, sAlternatePinName As String, Value As Integer)
  Dim p As Object

  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)
  r.v(GetOutputRegName(vPort, iPin)) = Value
End Sub


'*****************************************************************************
'Description:
'        This function gets the contants of the output register (OUT) 
'        dependent from an alternate pin name.
'        Use this function in each module to get the output register 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 value of the output register.
'*****************************************************************************
Function GetOUTRegister(r As ProjectData, sAlternatePinName As String) As Integer
  Dim p As Object

  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)
  GetOUTRegister = r.v(GetOutputRegName(vPort, 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 Object

  Dim s As String
  Dim l As Long
  Dim sTooltip As String
  Dim sAlternateName As String

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  sTooltip = ""
  s = p.PinName(sAlternatePinName)
  If Not(s = "") Then  ' only if the alternate pins exist
    Select Case (PIN_LOCKING)
        Case 0  ' unlimeted inputs can be used for the pin; input and output at same time 

            ' if the pin is an exclusive general pin or if sAlternatePinName is an exclusive pin
            If (p.Exclusive(s) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
              ' if pin name is used as input
              If (r.v("MuxFor_" & s) = 1) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as general input " & GetPinName(sAlternatePinName) & " (see Port module)")
              End If
              ' if pin name is used as output
              If (r.v("MuxFor_" & s) = 2) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as general output " & GetPinName(sAlternatePinName) & " (see Port module)")
              End If
            End If

            ' for all alternate pin names from pin name
          	For l = 1 To p.AlternateNameCount(s)
              sAlternateName = p.AlternateName(s, l)
              ' only if alternate pin name not the function parameter sName (alternate name)
              If (sAlternateName <> sAlternatePinName) Then

                ' if the found alternate pin name is an exclusive pin or if sAlternatePinName is an exclusive pin
                If (p.AlternateExclusive(sAlternateName) = True Or p.AlternateExclusive(sAlternatePinName) = True) Then
                  ' if the found alternate pin name is used as input
                  If (r.v("MuxFor_" & sAlternateName) = 1) Then
                    sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate input for the " & p.AlternateDescription(sAlternateName))
                  End If
                  ' if the found alternate pin name is used as output
                  If (r.v("MuxFor_" & sAlternateName) = 2) Then
                    sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
                  End If
                End If
              
              End If
           	Next l

        Case 1  ' unlimeted inputs can be used for the pin; only input or output at same time 

            ' if the pin is an exclusive general pin or if sAlternatePinName is an exclusive pin or sAlternatePinName is output
            If (p.Exclusive(s) = True Or p.AlternateExclusive(sAlternatePinName) = True Or p.AlternateFunctionNr(sAlternatePinName) > - 1) Then
              ' if pin name is used as input
              If (r.v("MuxFor_" & s) = 1) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as general input " & GetPinName(sAlternatePinName) & " (see Port module)")
              End If
              ' if pin name is used as output
              If (r.v("MuxFor_" & s) = 2) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as general output " & GetPinName(sAlternatePinName) & " (see Port module)")
              End If
            Else
              ' if pin name is used as output
              If (r.v("MuxFor_" & s) = 2) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as general output " & GetPinName(sAlternatePinName) & " (see Port module)")
              End If
            End If

            ' for all alternate pin names from pin name
          	For l = 1 To p.AlternateNameCount(s)
              sAlternateName = p.AlternateName(s, l)
              ' only if alternate pin name not the function parameter sName (alternate name)
              If (sAlternateName <> sAlternatePinName) Then

                ' if the found alternate pin name is an exclusive pin or if sAlternatePinName is an exclusive pin or  sAlternatePinName is output
                If (p.AlternateExclusive(sAlternateName) = True Or p.AlternateExclusive(sAlternatePinName) = True Or p.AlternateFunctionNr(sAlternatePinName) > - 1) Then
                  ' if the found alternate pin name is used as input
                  If (r.v("MuxFor_" & sAlternateName) = 1) Then
                    sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate input for the " & p.AlternateDescription(sAlternateName))
                  End If
                  ' if the found alternate pin name is used as output
                  If (r.v("MuxFor_" & sAlternateName) = 2) Then
                    sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
                  End If
                Else
                  ' if the found alternate pin name is used as output
                  If (r.v("MuxFor_" & sAlternateName) = 2) Then
                    sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
                  End If

                End If
              
              End If
           	Next l

        Case 2  ' only one input can be used for the pin; only input or output at same time 

            ' if pin name is used as input
            If (r.v("MuxFor_" & s) = 1) Then
              sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as general input " & GetPinName(sAlternatePinName) & " (see Port module)")
            End If
            ' if pin name is used as output
            If (r.v("MuxFor_" & s) = 2) Then
              sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as general output " & GetPinName(sAlternatePinName) & " (see Port module)")
            End If

            ' for all alternate pin names from pin name
          	For l = 1 To p.AlternateNameCount(s)
              sAlternateName = p.AlternateName(s, l)
              ' only if alternate pin name not the function parameter sName (alternate name)
              If (sAlternateName <> sAlternatePinName) Then
                ' if the found alternate pin name is used as input
                If (r.v("MuxFor_" & sAlternateName) = 1) Then
                  sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate input for the " & p.AlternateDescription(sAlternateName))
                End If
                ' if the found alternate pin name is used as output
                If (r.v("MuxFor_" & sAlternateName) = 2) Then
                  sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
                End If
              End If
           	Next l

        Case 3  ' only one input can be used for the pin; input and output at same time 
    End Select
  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 Object

  Dim s As String
  Dim l As Long
  Dim sTooltip As String
  Dim sAlternateName As String

  Set p = ObjectCollection("PORTPINS")

  s = PinName
  sTooltip = ""

  Select Case (PIN_LOCKING)
      Case 0  ' unlimeted inputs can be used for the pin; input and output at same time 

        	For l = 1 To p.AlternateNameCount(s)
            sAlternateName = p.AlternateName(s, l)

            ' if the found alternate pin name is an exclusive pin or if PinName is an exclusive pin
            If (p.AlternateExclusive(sAlternateName) = True Or p.Exclusive(PinName) = True) Then
              ' if the found alternate pin name is used as input
              If (r.v("MuxFor_" & sAlternateName) = 1) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate input for the " & p.AlternateDescription(sAlternateName))
              End If
              ' if the found alternate pin name is used as output
              If (r.v("MuxFor_" & sAlternateName) = 2) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
              End If
            End If
         	Next l

      Case 1  ' unlimeted inputs can be used for the pin; only input or output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)
            sAlternateName = p.AlternateName(s, l)

            ' if the found alternate pin name is an exclusive pin or if PinName is an exclusive pin
            If (p.AlternateExclusive(sAlternateName) = True Or p.Exclusive(PinName) = True) Then
              ' if the found alternate pin name is used as input
              If (r.v("MuxFor_" & sAlternateName) = 1) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate input for the " & p.AlternateDescription(sAlternateName))
              End If
              ' if the found alternate pin name is used as output
              If (r.v("MuxFor_" & sAlternateName) = 2) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
              End If
            Else
              ' if the found alternate pin name is used as output
              If (r.v("MuxFor_" & sAlternateName) = 2) Then
                sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
              End If

            End If
         	Next l

      Case 2  ' only one input can be used for the pin; only input or output at same time 

          ' for all alternate pin names from pin name
        	For l = 1 To p.AlternateNameCount(s)
            sAlternateName = p.AlternateName(s, l)
            ' if the found alternate pin name is used as input
            If (r.v("MuxFor_" & sAlternateName) = 1) Then
              sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate input for the " & p.AlternateDescription(sAlternateName))
            End If
            ' if the found alternate pin name is used as output
            If (r.v("MuxFor_" & sAlternateName) = 2) Then
              sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
            End If
         	Next l

      Case 3  ' only one input can be used for the pin; input and output at same time 
  End Select

  GetDisabledGeneralPinTooltip = sTooltip
End Function


'*****************************************************************************
'Description:
'        This function returns a formatted string with the disabled tooltip
'        for an direction control of a 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 the direction control of general pins.
'-----------------------------------------------------------------------------
'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 direction control of general pin.
'*****************************************************************************
Function GetDisabledGeneralDirectionTooltip (r As ProjectData, PinName As String) As String
  On Error Resume Next
  Dim p As Object

  Dim s As String
  Dim l As Long
  Dim sTooltip As String
  Dim sAlternateName As String

  Set p = ObjectCollection("PORTPINS")

  s = PinName
  sTooltip = ""

  ' for all alternate pin names from pin name
	For l = 1 To p.AlternateNameCount(s)
    sAlternateName = p.AlternateName(s, l)
    ' if the found alternate pin name is used as input
    If (r.v("MuxFor_" & sAlternateName) = 1) Then
      sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate input for the " & p.AlternateDescription(sAlternateName))
    End If
    ' if the found alternate pin name is used as output
    If (r.v("MuxFor_" & sAlternateName) = 2) Then
      sTooltip = sTooltip & FormatDisabledToolTip("the pin is used as alternate output for the " & p.AlternateDescription(sAlternateName))
    End If
 	Next l

  GetDisabledGeneralDirectionTooltip = sTooltip
End Function


'*****************************************************************************
'Description:
'        This function returns the current function of the selected pin
'
'        0 : no function
'        1 : pin is used as input
'        2 : pin is used as output
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sPinName              the name of the pin (specified with function
'                              'AddPin' or 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The number of the pin function.
'        0 : no function
'        1 : pin is used as input
'        2 : pin is used as output
'*****************************************************************************
Function GetPinFunction(r As ProjectData, sPinName As String) As Integer
  If (r.exist("MuxFor_" & sPinName) = True) Then
      GetPinFunction = r.v("MuxFor_" & sPinName)
  Else
      GetPinFunction = 0
  End If
End Function


'*****************************************************************************
'Description:
'        This function return the name of the direction register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetDirectionRegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetDirectionRegName = ""
  GetDirectionRegName = "DP" & vPort & "_" & "DP" & vPort & "_" & iPin    ' e.g. DP2_DP2_0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the OpenDrain register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOpenDrainRegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetOpenDrainRegName = ""
  GetOpenDrainRegName = "ODP" & vPort & "_" & "ODP" & vPort & "_" & iPin    ' e.g. ODP2_ODP2_0
End Function
    

'*****************************************************************************
'Description:
'        This function return the name of the Output register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputRegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetOutputRegName = ""
  GetOutputRegName = "P" & vPort & "_" & "P" & vPort & "_" & iPin    ' e.g. P2_P2_0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the PullupPulldownEnable register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetPullupPulldownEnableRegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetPullupPulldownEnableRegName = ""
  GetPullupPulldownEnableRegName = "P" & vPort & "_PUDEN_" & iPin    ' e.g. P2_PUDEN_0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the PullupPulldownSelect register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetPullupPulldownSelectRegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetPullupPulldownSelectRegName = ""
  GetPullupPulldownSelectRegName = "P" & vPort & "_PUDSEL_" & iPin    ' e.g. P2_PUDSEL_0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the OutputDriverEnable register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputDriverEnableRegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetOutputDriverEnableRegName = ""
  GetOutputDriverEnableRegName = "P" & vPort & "_PHEN_" & iPin    ' e.g. P2_PHEN_0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the AlternateSelect0 register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetAlternateSelect0RegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetAlternateSelect0RegName = ""
  GetAlternateSelect0RegName = "P" & vPort & "_ALTSEL0_" & iPin    'e.g. P2_ALTSEL0_0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the AlternateSelect1 register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetAlternateSelect1RegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetAlternateSelect1RegName = ""
  GetAlternateSelect1RegName = "P" & vPort & "_ALTSEL1_" & iPin    'e.g. P2_ALTSEL1_0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the Px Input Configuration register
'        for each pin.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetInputConfigurationRegNamePinwise(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetInputConfigurationRegNamePinwise = ""
  GetInputConfigurationRegNamePinwise = "P" & vPort & "_PICON_" & iPin    'e.g. P2_PICON_0
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px Input Configuration register
'        for each nible.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iNible          number of the nible
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetInputConfigurationRegNameNiblewise(vPort As Variant, iNible As Integer) As String
  On Error Resume Next

  GetInputConfigurationRegNameNiblewise = ""
  GetInputConfigurationRegNameNiblewise = "PICON_P" & vPort & iNible & "IN"   'e.g. PICON_P3LIN
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px Input Configuration register
'        for each byte.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iByte           number of the byte
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetInputConfigurationRegNameBytewise(vPort As Variant, iByte As Integer) As String
  On Error Resume Next

  GetInputConfigurationRegNameBytewise = ""
  If (iByte = 0) Then  ' for low byte
    GetInputConfigurationRegNameBytewise = "PICON_P" & vPort & "LIN"   'e.g. PICON_P3LIN
  Else                 ' for high byte 
    GetInputConfigurationRegNameBytewise = "PICON_P" & vPort & "HIN"   'e.g. PICON_P3HIN
  End If
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px Input Configuration register
'        for each port.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetInputConfigurationRegNamePortwise(vPort As Variant) As String
  On Error Resume Next

  GetInputConfigurationRegNamePortwise = ""
  GetInputConfigurationRegNamePortwise = "CPC_PICON_PICP" & vPort    'e.g. CPC_PICON_PICP0
End Function


'*****************************************************************************
'Description:
'        This function return the name of the Px output driver characteristic
'        register for each pin.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputDriverCharcRegNamePinwise(vPort As Variant, iPin As Integer) As String
  On Error Resume Next
  Dim i As Integer

  i = iPin \ 4
  GetOutputDriverCharcRegNamePinwise = ""
  GetOutputDriverCharcRegNamePinwise = "P" & vPort & "_POCON" & i & "_PDC" & iPin    'e.g. P2_POCON1_PDC1
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px output driver characteristic
'        register for each nible.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iNible          number of the nible
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputDriverCharcRegNameNiblewise(vPort As Variant, iNible As Integer) As String
  On Error Resume Next

  GetOutputDriverCharcRegNameNiblewise = ""
  GetOutputDriverCharcRegNameNiblewise = "POCON" & vPort & "_PN" & iNible & "DC"   'e.g. POCON3_PN3DC
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px output driver characteristic
'        register for each byte.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iByte           number of the byte
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputDriverCharcRegNameBytewise(vPort As Variant, iByte As Integer) As String
  On Error Resume Next

  GetOutputDriverCharcRegNameBytewise = ""
  If (iByte = 0) Then  ' for low byte
    GetOutputDriverCharcRegNameBytewise = "POCON_P" & vPort & "LDC"   'e.g. POCON_P3LDC
  Else                 ' for high byte 
    GetOutputDriverCharcRegNameBytewise = "POCON_P" & vPort & "HDC"   'e.g. POCON_P3HDC
  End If
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px output driver characteristic
'        register for each port.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputDriverCharcRegNamePortwise(vPort As Variant) As String
  On Error Resume Next
  Dim i As Integer

  If (vPort <= 3) Then
    i = 0
  Else
    If (vPort <= 9) Then
        i = 1
    Else
      If (vPort <= 11) Then
        i = 2
      Else
        i = 30 ' dummy value
      End If
    End If
  End If

  GetOutputDriverCharcRegNamePortwise = ""
  GetOutputDriverCharcRegNamePortwise = "CPC_POCON" & i & "_P" & vPort & "DC"    'e.g. CPC_POCON1_P1DC
End Function


'*****************************************************************************
'Description:
'        This function return the name of the Px output edge characteristic
'        register for each pin.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputEdgeCharcRegNamePinwise(vPort As Variant, iPin As Integer) As String
  On Error Resume Next
  Dim i As Integer

  i = iPin \ 4
  GetOutputEdgeCharcRegNamePinwise = ""
  GetOutputEdgeCharcRegNamePinwise = "P" & vPort & "_POCON" & i & "_PEC" & iPin    'e.g. P2_POCON1_PEC1
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px output edge characteristic
'        register for each nible.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iNible          number of the nible
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputEdgeCharcRegNameNiblewise(vPort As Variant, iNible As Integer) As String
  On Error Resume Next
  Dim i As Integer

  GetOutputEdgeCharcRegNameNiblewise = ""
  GetOutputEdgeCharcRegNameNiblewise = "POCON" & vPort & "_PN" & iNible & "EC"   'e.g. POCON3_PN3EC
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px output edge characteristic
'        register for each byte.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iByte           number of the byte
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputEdgeCharcRegNameBytewise(vPort As Variant, iByte As Integer) As String
  On Error Resume Next

  GetOutputEdgeCharcRegNameBytewise = ""
  If (iByte = 0) Then  ' for low byte
    GetOutputEdgeCharcRegNameBytewise = "POCON_P" & vPort & "LEC"   'e.g. POCON_P3LEC
  Else                 ' for high byte 
    GetOutputEdgeCharcRegNameBytewise = "POCON_P" & vPort & "HEC"   'e.g. POCON_P3HEC
  End If
End Function

'*****************************************************************************
'Description:
'        This function return the name of the Px output edge characteristic
'        register for each port.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOutputEdgeCharcRegNamePortwise(vPort As Variant) As String
  On Error Resume Next
  Dim i As Integer

  If (vPort <= 3) Then
    i = 0
  Else
    If (vPort <= 9) Then
        i = 1
    Else
      If (vPort <= 11) Then
        i = 2
      Else
        i = 30 ' dummy value
      End If
    End If
  End If

  GetOutputEdgeCharcRegNamePortwise = ""
  GetOutputEdgeCharcRegNamePortwise = "CPC_POCON" & i & "_P" & vPort & "EC"    'e.g. CPC_POCON1_P1EC
End Function


'*****************************************************************************
'Description:
'        This function generates the description statements for the used 
'        alternate port pins in the generated code.
'        Example:
'        Call GetAlternatePinFunctionDescription(f, r, "SSC0_CLK_I0")
'-----------------------------------------------------------------------------
'Parameters:         
'        f                     file handle
'        r                     reference to the ProjectData object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub GetAlternatePinFunctionDescription(f As Variant, r As ProjectData, sAlternatePinName As String)
  On Error Resume Next

  Dim p As Object
  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)

  If (r.v("MuxFor_" & sAlternatePinName) > 0) Then  ' only if the alternate function is used
      Print #f, FormatCommentsForInit(1, "P" & vPort & "." & iPin & " is used for " & p.AlternateDescription(sAlternatePinName))    
  End If
End Sub


'*****************************************************************************
'Description:
'        This function generates all statements for the used alternate port
'        pins in the generated code. This includes the description and the 
'        code statements. All alternate pin names in the parameter sName()
'        are check if it used as alternate pin function. If so than this 
'        functions checks if the alternate pin function is input or output.
'        Example:
'        Call SetAlternatePinStatements(f, r, sName())
'-----------------------------------------------------------------------------
'Parameters:         
'        f                     file handle
'        r                     reference to the ProjectData object
'        sName                 an array with all alternate pin names (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetAlternatePinStatements(f As Variant, r As ProjectData, sName() As String)
  On Error Resume Next

  Dim p As Object
  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer
  Dim vPortArr() As Variant
  Dim lDirection As Long
  Dim lData As Long
  Dim i As Integer
  Dim j As Integer
  Dim bfld As String
  Set p = ObjectCollection("PORTPINS")

  ' store the port of the first alternate pin name in the temporary 
  ' field vPortArr()
  If (sName(1) <> "") Then   ' only if a name exist
    Call GetAlternatePinFunctionDescription(f, r, sName(1))
    s = p.PinName(sName(1))
    vPort = p.Port(s)
    ' redim arrays in order to add the new entry
    ReDim Preserve vPortArr(1)
    vPortArr(1) = vPort
  End If

  ' only if the field sName is greater than one
  If (UBound(sName) > 1) Then
    ' store the port of each alternate pin name in the temporary
    ' field vPortArr() but each port only ones
    For i = 2 To Ubound(sName)   ' for each alternate pin function
      If (sName(i) <> "") Then   ' only if a name exist
        Call GetAlternatePinFunctionDescription(f, r, sName(i))
        s = p.PinName(sName(i))
        vPort = p.Port(s)
        For j = 1 To Ubound(vPortArr)   ' for each inserted port
          If (vPort = vPortArr(j)) Then
            Exit For
          End If
        Next j

        If (vPort <> vPortArr(j)) Then
          ' redim arrays in order to add the new entry
          ReDim Preserve vPortArr(j)
          vPortArr(j) = vPort
        End If
      End If
    Next i
  End If

  Print #f, ""
  For j = 1 To Ubound(vPortArr)   ' for each inserted port search in sName

    lDirection = 0
    lData = 0

    For i = 1 To Ubound(sName)    ' for each alternate pin function
      If (sName(i) <> "") Then    ' only if a name exist
        s = p.PinName(sName(i))
        vPort = p.Port(s)        ' get the port from the alternate pin name
        ' only if the port of the current alternate pin name 
        ' the same as the current port in vPortArr()
        If (vPort = vPortArr(j)) Then  
          iPin = p.Pin(s)        ' get the pin from the alternate pin name      

          If (r.v("MuxFor_" & sName(i)) = 2) Then  ' if output

            ' calculate the value for the direction and data register
            lDirection = lDirection + 2^ iPin 
            lData = lData + 2^iPin

            ' calculate the value for the alternate output select register
          End If  ' if output

          If (r.v("MuxFor_" & sName(i)) = 1) Then  ' if input
            ' after reset all pins are inputs !!
            ' currently no initialization for inputs are made
          End If

        End If
      End If
    Next i

    If (lData > 0) Then
      If (vPortArr(j) = 3) Then
          Print #f, "  P" & vPortArr(j) & "   = (P" & vPortArr(j) & "   & ~(uword)" & FormatTextOutput(lData, "H4") & ") | " & FormatTextOutput(lData, "H4") & ";    //set data register"
      End If
    End If
    If (lDirection > 0) Then
      Print #f, "  DP" & vPortArr(j) & "  = (DP" & vPortArr(j) & "  & ~(uword)" & FormatTextOutput(lDirection, "H4") & ") | " & FormatTextOutput(lDirection, "H4") & ";    //set direction register"
    End If

  Next j
End Sub


'*****************************************************************************
'Description:
'        This function return the name of the direction register from 
'        alternate pin name.
'-----------------------------------------------------------------------------
'Parameters:
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetDirectionRegNameFromAlternatName(sAlternatePinName As String) As String
  On Error Resume Next

  Dim p As Object
  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")
  GetDirectionRegNameFromAlternatName = ""

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)
  GetDirectionRegNameFromAlternatName = "DP" & vPort & "_DP" & vPort & "_" & iPin    ' e.g. DP2_0

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 Resume Next 

    Dim p           As Object
    Dim l           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 Pin P" & p.Port(sPinName) & "." & p.Pin(sPinName) & ":" 
        For l = 1 To lNumAltFunc 
         sTooltip = sTooltip & FormatDisabledToolTip(p.AlternateDescription(p.AlternateName(sPinName, l))) 
        Next l 
    Else 
        sTooltip = "" 
    End If 

    GetEnabledGeneralPinTooltip = sTooltip 

End Function 
  

'*****************************************************************************
'Description:
'        This function return the name of the output register from 
'        alternate pin name for dialog script.
'-----------------------------------------------------------------------------
'Parameters:
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetOtputRegBitNameFromAlternatNameForDialog(sAlternatePinName As String) As String
  On Error Resume Next

  Dim p As Object
  Dim s As String
  Dim vPort As Variant
  Dim iPin As Integer

  Set p = ObjectCollection("PORTPINS")
  GetOtputRegBitNameFromAlternatNameForDialog = ""

 ' get the pin name from alternate pin name
  s = p.PinName(sAlternatePinName)
  vPort = p.Port(s)
  iPin = p.Pin(s)
  GetOtputRegBitNameFromAlternatNameForDialog = "P" & vPort & "_" & "P" & vPort & "_" & iPin    ' e.g. P7_P7_0

End Function

