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
'
'*****************************************************************************


'*****************************************************************************
' @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 = 0

' bit positions in the bitfield PCx in register Pn_IOCRm
Global Const DIRECTION_BIT_POS = 3
Global Const OPEN_DRAIN_BIT_POS = 2
Global Const PULL_DEVICE_L_BIT_POS = 0
Global Const PULL_DEVICE_H_BIT_POS = 1
Global Const ALT_OUTPUT_L_BIT_POS = 0
Global Const ALT_OUTPUT_H_BIT_POS = 1

' this constant specifies the max number of pins (GPIO's) for a alternate
' pin function. For example the alternate pin function ASC0_RX can be connected
' to the GPIO_0, GPIO_3 and GPIO_6
Global Const MAX_PINS_FOR_ALTERNATE_FUNCTIONS = 4

Global Const NO_PIN_SELECTED = "none"


'*****************************************************************************

'*****************************************************************************
'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-->port 2 is input\output  in XC856CLM
        
        ' 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
        
        ' Set the Drive strength option
         r.v("P" & iPort & "_DS_P" & iPin) = 1

        '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

'*****************************************************************************
'Description:
'        This function returns a string with the alternate pin name
'        (e.g. ASC1_RX) like named in the specification.
'        Use this function in each module to get the external 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 external pin name of the alternate pin function (specified
'        with function 'AddAlternateName' in install.inc).
'*****************************************************************************
Function GetExternalAlternatePinName(sAlternatePinName As String) As String
  On Error Resume Next

  Dim p As Object
  Dim s As String

  Set p = ObjectCollection("PORTPINS")

 ' get the pin name from alternate pin name
  s = p.ExternalPinName(sAlternatePinName)
  If Not (s = "") Then
    GetExternalAlternatePinName = s
  Else
    GetExternalAlternatePinName = ""
  End If

End Function


'*****************************************************************************
'Description:
'        This function returns the current selected pin name of an alternate
'        pin. If no pin is selected the string "none" will be returned, else
'        "GPIO_x".
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAllAlternatePinName  part of the name (without _1 ..) of the
'                              alternate pin (specified with function
'                              'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns: If no pin is selected the string "none" will be returned, else
'        "GPIO_x".
'*****************************************************************************
Function GetCurrentPinFromAlternatePin(r As ProjectData, sAllAlternatePinName As String) As String
  On Error Resume Next
  Dim i As Integer
  Dim s As String

  GetCurrentPinFromAlternatePin = NO_PIN_SELECTED


  For i = 1 To MAX_PINS_FOR_ALTERNATE_FUNCTIONS
    s = GetPinName(sAllAlternatePinName & "_" & i)
    If Not (s = "") Then
      If (GetPinFunction(r, sAllAlternatePinName & "_" & i) > 0) Then
        GetCurrentPinFromAlternatePin = s
        Exit For
      End If
    End If
  Next i
End Function





'*****************************************************************************
'Description:
'        This function sets the the bit on position iPos in the register
'        sRegName (bitfield).
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sRegName              the name of the bitfield
'        iPos                  position of the bit in the bitfield, beginning
'                              with 0
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub SetBit(r As ProjectData, sRegName As String, iPos As Integer)
  On Error Resume Next

  Dim tmp As Integer
  tmp = r.v(sRegName) \ (2 ^ iPos)     ' check bit position
  If (tmp Mod 2) Then                  ' bit is set
  Else                                 ' bit is not set
    r.v(sRegName) = r.v(sRegName) + (2 ^ iPos)
  End If
End Sub


'*****************************************************************************
'Description:
'        This function reset the the bit on position iPos in the register
'        sRegName (bitfield).
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sRegName              the name of the bitfield
'        iPos                  position of the bit in the bitfield, beginning
'                              with 0
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ResetBit(r As ProjectData, sRegName As String, iPos As Integer)
  On Error Resume Next

  Dim tmp As Integer
  tmp = r.v(sRegName) \ (2 ^ iPos)     ' check bit position
  If (tmp Mod 2) Then                  ' bit is set
    r.v(sRegName) = r.v(sRegName) - (2 ^ iPos)
  Else                                 ' bit is not set
  End If
End Sub

'*****************************************************************************
'Description:
'        This function checks if the the bit on position iPos in the register
'        sRegName (bitfield) is set.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sRegName              the name of the bitfield
'        iPos                  position of the bit in the bitfield, beginning
'                              with 0
'-----------------------------------------------------------------------------
'Returns: true if the requested bit is set, else false
'*****************************************************************************
Function IsBitSet(r As ProjectData, sRegName As String, iPos As Integer) As Boolean
  On Error Resume Next
  Dim tmp As Integer

  IsBitSet = False
  tmp = r.v(sRegName) \ (2 ^ iPos)     ' check bit position
  If (tmp Mod 2) Then                  ' bit is set
    IsBitSet = True
  Else                                 ' bit is not set
  End If
End Function

'*****************************************************************************
'Description:
'        This function sets all necessary registers to release all pins from
'        an alternate function which was requested with the function
'        'RequestAlternatePin'.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAllAlternatePinName  part of the name (without _1 ..) of the
'                              alternate pin (specified with function
'                              'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseAllAlternatePins(r As ProjectData, sAllAlternatePinName As String)
  On Error Resume Next

  Dim p As Object
  Dim s As String
  Dim j As Integer

  Set p = ObjectCollection("PORTPINS")

  For j = 1 To MAX_PINS_FOR_ALTERNATE_FUNCTIONS
    s = p.PinName(sAllAlternatePinName & "_" & j)
    If Not (s = "") Then ' if the alternate function exist
      Call ReleaseAlternatePin(r, sAllAlternatePinName & "_" & j)
    Else
' ste 03.07.02.
'      Exit For
    End If
  Next j

End Sub
Sub ReleaseAlternatePin(r As ProjectData, sAlternatePinName As String)
  On Error Resume Next
  Dim p As Object
  Dim sPin As String
  Dim sDirectionReg As String
  Dim sOpenDrainReg As String
  Dim sEmergencyStopReg As String
  Dim sOutputReg As String
  Dim iMode As Integer

  Set p = ObjectCollection("PORTPINS")

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

  ' get the current configured function of the alternate pin
  iMode = r.v("MuxFor_" & sAlternatePinName)
 
  ' release the alternate pin only if he was selected before
  Select Case (iMode)
      Case 0  ' the alternate pin was not used
              ' nothing do do

      Case 1  ' the alternate pin was selected as input
          r.v("MuxFor_" & sAlternatePinName) = 0  ' set Mux to "not used"

      Case 2  ' the alternate pin was selected as output
          r.v("MuxFor_" & sAlternatePinName) = 0  ' set Mux to "not used"
          sDirectionReg = GetDirectionRegName(p.Port(sPin), p.Pin(sPin))
          If (r.Regs.BitFind(sDirectionReg) = True) Then
            Call ResetBit(r, sDirectionReg, DIRECTION_BIT_POS)
          End If

          sOpenDrainReg = GetOpenDrainRegName(p.Port(sPin), p.Pin(sPin))
          If (r.Regs.BitFind(sOpenDrainReg) = True) Then
            Call ResetBit(r, sOpenDrainReg, OPEN_DRAIN_BIT_POS)
          End If

          sEmergencyStopReg = GetEmergencyStopRegName(p.Port(sPin), p.Pin(sPin))
          If (r.Regs.BitFind(sEmergencyStopReg) = True) Then
            r.v(sEmergencyStopReg) = 0
          End If

          sOutputReg = GetOutputRegName(p.Port(sPin), p.Pin(sPin))
          If (r.Regs.BitFind(sOutputReg) = True) Then
            r.v(sOutputReg) = 0
          End If

      Case 3  ' the alternate pin was selected as input/output
          'tbd

      Case Else
  End Select
 
  If (iMode > 0) Then
  ' if the same is todo for all configurations
  End If

End Sub


'*****************************************************************************
'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 = "P" & vPort & "_IOCR" & ((iPin \ 4) * 4) & "_PC" & iPin     ' e.g. P2_IOCR4_P4
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 = "P" & vPort & "_IOCR" & ((iPin \ 4) * 4) & "_PC" & iPin     ' e.g. P2_IOCR4_P4
End Function
'*****************************************************************************
'Description:
'        This function return the name of the Emergency Stop register.
'-----------------------------------------------------------------------------
'Parameters:
'        vPort           name of the port
'        iPin            name of the pin
'-----------------------------------------------------------------------------
'Returns:
'        The name of the register
'*****************************************************************************
Public Function GetEmergencyStopRegName(vPort As Variant, iPin As Integer) As String
  On Error Resume Next

  GetEmergencyStopRegName = ""
  GetEmergencyStopRegName = "P" & vPort & "_ESR_EN" & iPin    ' e.g. P2_ESR_EN5
End Function


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

  GetInputRegName = ""
  GetInputRegName = "P" & vPort & "_IN_P" & iPin    ' e.g. P2_IN_P0
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 & "_OUT_P" & iPin    ' e.g. P2_OUT_P0
End Function
