Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\Port.cls"
Option Explicit
'*****************************************************************************
' Module:
'        IO
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        IO dialog script and in the IO codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  Sunil          08-10-04     initial version
'        0.2  Sunil          18-01-05     Functions GetPUDSELDefaultValue and
'                                         GetPUDENDefaultValue are added. Which
'                                         are used in the codegeneration part.
'
'
'*****************************************************************************

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

'*****************************************************************************
'Description:
'        For each function which should be used for this module a line
'        "If (Not func.AddFunction .." must be insert in this function.
'        For each parameter that should be used for this function a
'        line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added function a project variable with the name
'        "IO_NAME_Functionname" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next

  Dim sDescription  As String
  Dim sReturnValue  As String
  Dim sOutPortList  As String
  Dim sInPortList   As String
  Dim cPorts        As DAvECollection
  Dim Port          As Port

  RefreshFunctionDescriptions = False

  Set cPorts = ObjectCollection("PORTS")


  ' for each function of this module the property AddFunction must be called
  '
  ' AddFunction(sFuncName As String, sFuncType As String, sFuncDescription As String,
  '             sReturnvalue As String, lSort As Long) As Boolean
  '
  ' sFuncName          | String | function name as key (use the name of the variable
  '                               for the function name e.g. SSC_NAME_Init)
  ' sFuncType          | String | type of the function
  ' sFuncDescription   | String | description of the function
  ' sReturnvalue       | String | description of the return value
  ' lSort              | Long   | ' sort of the function (0 => macro, 1 => function, 2 => ISR)

    ' for each parameter of a function the property AddParameter must be called
    '
    'AddParameter(sParName As String, sPar As String, sParType As String, sParDescription As String) As Boolean
    '
    ' sParName         | String | parameter name (key)
    ' sPar             | String | parameter name
    ' sParType         | String | type of the parameter
    ' sParDescription  | String | description of the parameter


  ' generate name lists of the available ports
  sOutPortList = ""
  sInPortList = ""
  For Each Port In cPorts
    If (Port.OutputDir) Then
        If (sOutPortList <> "") Then
            sOutPortList = sOutPortList & ", "
        End If
        sOutPortList = sOutPortList & "P" & Port.PortNumber
    End If
    If (Port.InputDir) Then
        If (sInPortList <> "") Then
            sInPortList = sInPortList & ", "
        End If
        sInPortList = sInPortList & "P" & Port.PortNumber
    End If
  Next Port

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription("IO")
                  

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

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_ReadPin
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro returns the status of the chosen portpin." & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "'PinName' identifies the pin of a bit-addressable port. " & _
                 "The default names can be changed in the port configuration dialog. "

  sReturnValue = "Status of the chosen portpin"
  If (Not func.AddFunction("IO_NAME_ReadPin", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Pin to be read"
  If (Not func.AddParameter("IO_NAME_ReadPin_Par1", "PinName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_ReadPort
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro returns the value of the chosen port." & vbCrLf & _
                 "The following definitions for PortName are available:" & vbCrLf & sInPortList

  sReturnValue = "Value of the port"
  If (Not func.AddFunction("IO_NAME_ReadPort", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Port to be read"
  If (Not func.AddParameter("IO_NAME_ReadPort_Par1", "PortName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_ResetPin
  '------------------------------------------------------------------------------------------------
  sDescription = "The chosen portpin is set to '0'. " & vbCrLf & _
                 "Note: " & vbCrLf & _
                 "'PinName' identifies the pin of a bit-addressable port. " & _
                 "The default names can be changed in the port configuration dialog. "

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

  sDescription = "Pin to be set to '0'"
  If (Not func.AddParameter("IO_NAME_ResetPin_Par1", "PinName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_SetInput
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro changes the direction of I/O port pins to input. " & _
                 "Parameter 'PortName' selects the port " & _
                 "and parameter 'Mask' selects the lines of this port. " & vbCrLf & _
                                 "Example: " & r.v("IO_NAME_SetInput") & "(P1, 0x11) sets P1.0 and P1.4 to input."

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

  sDescription = "Port to be configured"
  If (Not func.AddParameter("IO_NAME_SetInput_Par1", "PortName", "", sDescription)) Then Exit Function

  sDescription = "Mask for port pins to be configured"
  If (Not func.AddParameter("IO_NAME_SetInput_Par2", "Mask", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_SetOutput
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro changes the direction of I/O port pins to output. " & _
                 "Parameter 'PortName' selects the port " & _
                 "and parameter 'Mask' selects the lines of this port. " & vbCrLf & _
                                 "Example: " & r.v("IO_NAME_SetOutput") & "(P1, 0x11) sets P1.0 and P1.4 to output."

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

  sDescription = "Port to be configured"
  If (Not func.AddParameter("IO_NAME_SetOutput_Par1", "PortName", "", sDescription)) Then Exit Function

  sDescription = "Mask for port pins to be configured"
  If (Not func.AddParameter("IO_NAME_SetOutput_Par2", "Mask", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_SetPin
  '------------------------------------------------------------------------------------------------
  sDescription = "The chosen portpin is set to '1'." & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "'PinName' identifies the pin of a bit-addressable port. " & _
                 "The default names can be changed in the port configuration dialog. "

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

  sDescription = "Pin to be set to '1'"
  If (Not func.AddParameter("IO_NAME_SetPin_Par1", "PinName", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_TogglePin
  '------------------------------------------------------------------------------------------------
  sDescription = "The chosen portpin will be toggled. " & vbCrLf & _
                 "Note:" & vbCrLf & _
                 "'PinName' identifies the pin of a bit-addressable port. " & _
                 "The default names can be changed in the port configuration dialog. "

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

  sDescription = "Pin to be toggled"
  If (Not func.AddParameter("IO_NAME_TogglePin_Par1", "PinName", "", sDescription)) Then Exit Function
 
  '------------------------------------------------------------------------------------------------
  ' Description for IO_NAME_WritePort
  '------------------------------------------------------------------------------------------------
  sDescription = "The forwarded data is written to the chosen port." & vbCrLf & _
                 "The following definitions for PortName are available:" & vbCrLf & sOutPortList

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

  sDescription = "Port to be altered"
  If (Not func.AddParameter("IO_NAME_WritePort_Par1", "PortName", "", sDescription)) Then Exit Function

  sDescription = "Value to be written to the chosen port"
  If (Not func.AddParameter("IO_NAME_WritePort_Par2", "Data", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function

'*****************************************************************************
'Description:
'        This functions returns the default value of PUDSEL register for the
'        ports.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        PortNumber       Number of the port
'-----------------------------------------------------------------------------
'Returns:
'          Default value of the PUDSEL register.
'*****************************************************************************

Public Function GetPUDSELDefaultValue(r As ProjectData, Port As String) As Integer
  On Error Resume Next
  
  Select Case Port
  ' Note the default values of Port0 and Port1 is not same as given in the ITS.
  ' In DAvE the undefined bits get the default value 0. Check out the values in Register Explorer.
    Case "P0"
        GetPUDSELDefaultValue = &H3F
    
    Case "P1"
        GetPUDSELDefaultValue = &HE3
    
    Case "P2"
        GetPUDSELDefaultValue = &HFF
    
    Case "P3"
        GetPUDSELDefaultValue = &HBF
    
  End Select

End Function

'*****************************************************************************
'Description:
'        This functions returns the default value of PUDEN register for the
'        ports.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        PortNumber       Number of the port
'-----------------------------------------------------------------------------
'Returns:
'          Default value of the PUDEN register.
'*****************************************************************************

Public Function GetPUDENDefaultValue(r As ProjectData, Port As String) As Integer
  On Error Resume Next
  
  Select Case Port
   ' Note the default values of Port0 and Port1 is not as given in the ITS.
   ' In DAvE the undefined bits get the default value 0. Check out the values in Register Explorer.
    Case "P0"
        GetPUDENDefaultValue = &H4
    
    Case "P1"
        GetPUDENDefaultValue = &HE3
    
    Case "P2"
        GetPUDENDefaultValue = &H0
        
    Case "P3"
        GetPUDENDefaultValue = &H40
    
  End Select


End Function

'*****************************************************************************
'Description:
'        This functions returns the actual default value of PUDSEL register for the
'        ports.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        PortNumber       Number of the port
'-----------------------------------------------------------------------------
'Returns:
'          Default value of the PUDSEL register.
'*****************************************************************************

Public Function GetActualPUDSELDefaultValue(r As ProjectData, Port As String) As Integer
  On Error Resume Next
  
  Select Case Port
  ' Note the default values of Port0 and Port1 is not same as given in the ITS.
  ' In DAvE the undefined bits get the default value 0. Check out the values in Register Explorer.
    Case "P0"
        GetActualPUDSELDefaultValue = &HFF
    
    Case "P1"
        GetActualPUDSELDefaultValue = &HFF
    
    Case "P2"
        GetActualPUDSELDefaultValue = &HFF
    
    Case "P3"
        GetActualPUDSELDefaultValue = &HBF
    
  End Select

End Function

'*****************************************************************************
'Description:
'        This functions returns the actual default value of PUDEN register for the
'        ports.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        PortNumber       Number of the port
'-----------------------------------------------------------------------------
'Returns:
'          Default value of the PUDEN register.
'*****************************************************************************

Public Function GetActualPUDENDefaultValue(r As ProjectData, Port As String) As Integer
  On Error Resume Next
  
  Select Case Port
   ' Note the default values of Port0 and Port1 is not as given in the ITS.
   ' In DAvE the undefined bits get the default value 0. Check out the values in Register Explorer.
    Case "P0"
        GetActualPUDENDefaultValue = &HC4
    
    Case "P1"
        GetActualPUDENDefaultValue = &HFF
    
    Case "P2"
        GetActualPUDENDefaultValue = &H0
        
    Case "P3"
        GetActualPUDENDefaultValue = &H40
    
  End Select

End Function




'below function is added by sankar
'*****************************************************************************
'Description:
'        This functions updates the reset value to Portx PUDSEL and PUDEN registers
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        iPort            Number of the port
'        iPin             Number of the pin
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************

Public Sub ResetPullDevice(r As ProjectData, iPort As Variant, iPin As Integer)
  On Error Resume Next

  Dim iPx_PUDEN  As Integer
  Dim iPx_PUDSEL As Integer
  Dim sPullupPulldownEnableReg As String
  Dim sPullupPulldownSelectReg As String

  sPullupPulldownEnableReg = "P" & iPort & "_PUDEN_P" & iPin
  sPullupPulldownSelectReg = "P" & iPort & "_PUDSEL_P" & iPin
  iPx_PUDEN  = GetActualPUDENDefaultValue(r, ("P" & iPort))
  iPx_PUDSEL = GetActualPUDSELDefaultValue(r, ("P" & iPort))

  r.v(sPullupPulldownEnableReg) = ((iPx_PUDEN  And (2 ^ iPin)) \ (2 ^ iPin))
  r.v(sPullupPulldownSelectReg) = ((iPx_PUDSEL And (2 ^ iPin)) \ (2 ^ iPin))

End Sub
