Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Port.inc"

Option Explicit

'*****************************************************************************
' Module:
'        ADC
'-----------------------------------------------------------------------------
' Description:
'        This file contains the 'Update' functions for the module.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0  Mahesh         18.06.07    First version
'
'*****************************************************************************

Global ADC0_Channel(16) As Integer
Global ADC0_Source(3) As Integer
Global ADC0_ResultReg(8) As Integer

Global ADC1_Channel(16) As Integer
Global ADC1_Source(3) As Integer
Global ADC1_ResultReg(8) As Integer

'*****************************************************************************
' 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
'        "ADC_NAME_Functionname" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(vIndex As Variant, func As FunctionClass, r As ProjectData) As Boolean
On Error Resume Next

        RefreshFunctionDescriptions = False

        Dim sDescription  As String
        Dim sReturnValue  As String
        Dim sVarPre       As String
        Dim sRegPre       As String
        Dim sFctName      As String
        Dim iChParam      As Integer
        Dim i             As Integer

        sVarPre = "ADC" & vIndex
        sRegPre = sVarPre & "_"

        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_Init
        '------------------------------------------------------------------------------------------------
        sFctName = "Init"
        sDescription = GetGeneralInitDescription("ADC") & vbCrLf & vbCrLf & _
        "Following SFR fields will be initialized:" & vbCrLf & _
        "GLOBCTR  - Global Control" & vbCrLf & _
        "RSPR0    - Priority and Arbitration Register" & vbCrLf & _
        "ASENR    - Arbitration slot enable register" & vbCrLf & _
        "CHCTRx   - Channel Control Register x" & vbCrLf & _
        "RCRx     - Result Control Register x" & vbCrLf & _
        "KSCFG    - Module configuration Register" & vbCrLf & _
        "INPCR    - Input class Registers" & vbCrLf & _
        "CHINPRx  - Channel Interrupt register" & vbCrLf & _
        "EVINPRx  - Event Interrupt register" & vbCrLf & _
        "SYNCTR   - Synchronisation control register" & vbCrLf & _
        "LCBRx    - Limit check boundary register" & vbCrLf & _
        "PISEL    - Port input selection" & vbCrLf & _
        "QMR0     - Sequential 0 mode register" & vbCrLf & _
        "CRMR1    - Parallel mode register" & vbCrLf & _
        "QMR2     - Sequential 2 mode register"

        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
        
        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_StartSeq0ReqChNum
        '------------------------------------------------------------------------------------------------
        sFctName = "StartSeq0ReqChNum"
        
        sDescription = "This function starts the conversion of the requested channel." & vbCrLf & _
        "NOTE -" & vbCrLf & _
        "Before passing ubEnIntr argument as 1,make sure that Seq 0 " & _
        "source interrupt is enabled." & vbCrLf & _
        "External Trigger -> 0,Indicates software trigger (Conversion starts once this function is executed)"
                                        
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
        
        sDescription = "External Trigger : defines external trigger."
                                   
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubExtTrg", "ubyte", sDescription)) Then Exit Function
        
        sDescription = "Enable Source Interrupt : defines source interrupt"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "ubEnIntr", "ubyte", sDescription)) Then Exit Function
        sDescription = "Refill : defines the refill"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para3", "ubRFill", "ubyte", sDescription)) Then Exit Function
        sDescription = "Channel number : Name of the Request Channel Number (0 - " & (15 - vIndex * 8) & ")- see macros defined in the header file"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para4", "ubChannelNum", "ubyte", sDescription)) Then Exit Function

        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "StartSeq2ReqChNum"
        
        sDescription = "This function starts the conversion of the requested channel." & vbCrLf & _
        "NOTE -" & vbCrLf & _
        "Before passing ubEnIntr argument as 1,make sure that Seq 2 " & _
        "source interrupt is enabled." & vbCrLf & _
        "External Trigger -> 0,Indicates software trigger (Conversion starts once this function is executed)"
                                        
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
        sDescription = "External Trigger : defines external trigger."
                                  
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubExtTrg", "ubyte", sDescription)) Then Exit Function
        
        sDescription = "Enable Source Interrupt : defines source interrupt"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "ubEnIntr", "ubyte", sDescription)) Then Exit Function
        sDescription = "Refill : defines the refill"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para3", "ubRFill", "ubyte", sDescription)) Then Exit Function
        sDescription = "Channel number : Name of the Request Channel Number (0 - " & (15 - vIndex * 8) & ") - see macros defined in the header file"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para4", "ubChannelNum", "ubyte", sDescription)) Then Exit Function

        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "ClearPendingBits"
        sDescription = "This function clears all the pending requests for the corresponding source." & vbCrLf & _
                                   "ubSourceNum -> " & sRegPre & "SOURCE_0,indicates seq 0 source" & vbCrLf & _
                                   "ubSourceNum -> " & sRegPre & "SOURCE_1,indicates Parallel source" & vbCrLf & _
                                   "ubSourceNum -> " & sRegPre & "SOURCE_2,indicates seq 2 source"
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
        sDescription = "SourceNumber : Source Number for which pending bits have to be cleared."
    
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubSourceNum", "ubyte", sDescription)) Then Exit Function

        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_SRNx
        '------------------------------------------------------------------------------------------------
        For i = 0 To 3
            sFctName = "SRN" & i
            sDescription = "This is the interrupt service routine for the Service " & _
                           "Request Node " & i & " of the ADC" & vIndex & " module."

            sReturnValue = "None"
            If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 2)) Then Exit Function
        Next i
        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "SetLoadEvent"
        sDescription = "This macro sets software load event for parallel source"
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 0)) Then Exit Function
        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "StartParReqChNum"
        sDescription = "This function starts conversion request of analog channel." & _
                 "The possible values for the request channels are: " & vbCrLf & _
                 "Bit 0 = 1   ->  analog channel 0 is requested for conversion " & vbCrLf & _
                 "Bit 4 = 1   ->  analog channel 4 is requested for conversion " & vbCrLf & _
                 "Bit 8 = 1   ->  analog channel 8 is requested for conversion " & vbCrLf & _
                 "Bit 12 = 1   ->  analog channel 12 is requested for conversion "
        sReturnValue = "None"
        
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
        sDescription = "ChannelNumber : defines the channel number for parallel source to start conversion"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "uwChannelNum", "uword", sDescription)) Then Exit Function

        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "Busy"
        sDescription = "This macro checks the conversion state of the current " & _
                   "ADC channel by examination of the busy flag (BUSY)." & _
                               "It returns '1' while a conversion is running."

        sReturnValue = "Returns a 1 if A/D converter is active, else 0"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "uword", sDescription, sReturnValue, 0)) Then Exit Function
             
        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "GetResultData"
        sDescription = "This macro returns the left aligned, 10-bit result data of the last conversion from requested result register" & vbCrLf & _
                       "Ex- RegNum = RESULT_REG_0,for Result register 0" & vbCrLf & _
                       "    RegNum = RESULT_REG_2,for Result register 2" & vbCrLf & _
                       "  - For 8 bit resolution user has to right shift the result" & vbCrLf & _
                       "    by 2 bits as shown below" & vbCrLf & _
                       "    uwResult = ADC0_uwGetResultData(RESULT_REG_0);" & vbCrLf & _
                       "    uwResult >>= 2;" & vbCrLf & _
                       "NOTE -" & vbCrLf & _
                       "If this macro is called before " & sRegPre & "uwGetResultChNum macro for the same result register " & _
                       "then channel information will be lost.So always call " & sRegPre & "uwGetResultChNum macro before " & _
                       "this macro if channel information is required."
        sReturnValue = "Returns result data from requested result register"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "uword", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "RegNum: defines the result register number"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegNum", "", sDescription)) Then Exit Function
        
        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "GetResultChNum"
        sDescription = "This macro returns channel number from requested result register" & vbCrLf & _
                       "Ex- RegNum = RESULT_REG_0,for Result register 0" & vbCrLf & _
                       "    RegNum = RESULT_REG_2,for Result register 2" & vbCrLf & _
                       "NOTE -" & vbCrLf & _
                       "If only channel information is required then make sure that you call " & sRegPre & "vClearValidBit macro after reading " & _
                       "the channel number."
        sReturnValue = "Returns channel number from requested result register"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "ubyte", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "RegNum: defines the result register number"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegNum", "", sDescription)) Then Exit Function
        
        '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "Function0"
        sDescription = "This macro clears valid bit flag for the requested result register" & vbCrLf & _
                       "Ex- RegNum = RESULT_REG_0,for Result register 0" & vbCrLf & _
                       "    RegNum = RESULT_REG_2,for Result register 2"
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 0)) Then Exit Function
        sDescription = "RegNum: defines the result register number"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegNum", "", sDescription)) Then Exit Function
       
	    '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "Seq2QueueFlush"
        sDescription = "This macro Flushes the Sequential source 2 Queue"
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 0)) Then Exit Function
       ' sDescription = "RegNum: defines the result register number"
       ' If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegNum", "", sDescription)) Then Exit Function
       
	    '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "Seq2QueueEmpty"
        sDescription = "This macro checks whether Sequential source 2 Queue depth is empty or not"
        sReturnValue = "Returns '1' if sequential 2 queue is empty, else '0'"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "uword", sDescription, sReturnValue, 0)) Then Exit Function
       ' sDescription = "RegNum: defines the result register number"
       ' If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "RegNum", "", sDescription)) Then Exit Function
       
         '------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "Function1"
        sDescription = "This function assigns attributes of channel0 or channel1 to the requested channel"
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
        sDescription = "AliasChNum: defines the Alias channel number (Ch0 and Ch1)"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubAliasChNum", "ubyte", sDescription)) Then Exit Function
        sDescription = "ReqChNum: defines the channel number to be aliased"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para2", "ubReqChNum", "ubyte", sDescription)) Then Exit Function
        
		'------------------------------------------------------------------------------------------------
        ' Description for ADC_NAME_...
        '------------------------------------------------------------------------------------------------
        sFctName = "Function2"
        sDescription = "This function sets TREV bit for the requested sequential source in the register QMRx." & vbCrLf & _
                       "It is applicable only when sequential source conversion request API is called with 'ubExtTrg' parameter set to '1'." & vbCrLf & _
					   "This API helps in triggering (software) all the sequential conversion requests which are waiting for external hardware trigger to occur."  & vbCrLf & _
					   "Ex- ubSrc = " &  sRegPre & "SOURCE_0,for sequential source 0" & vbCrLf & _
                       "    ubSrc = " &  sRegPre & "SOURCE_2,for sequential source 2"
        sReturnValue = "None"
        If (Not func.AddFunction(sVarPre & "_NAME_" & sFctName, "void", sDescription, sReturnValue, 1)) Then Exit Function
        sDescription = "ubSrc: defines the sequential source number"
        If (Not func.AddParameter(sVarPre & "_NAME_" & sFctName & "_Para1", "ubSrc", "ubyte", sDescription)) Then Exit Function
       

		If (Not func.Sort) Then Exit Function
        RefreshFunctionDescriptions = True
End Function
'*****************************************************************************
'Description:
'        For each parameter that should be used as a constant (on page "Parameters")
'        a line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added parameter a project variable with the name
'        "DUMMY_PARA_Parametername" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        vIndex           parameter of the module which was added with AddModule
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshParameterDescriptions(vIndex As Variant, para As ParameterClass, r As ProjectData) As Boolean
  On Error Resume Next

  RefreshParameterDescriptions = False

  Dim sDescription  As String
  Dim sVarPre       As String
  Dim sRegPre       As String
  Dim sParaName     As String
  Dim i             As Integer
  Dim vChannelCount As Variant
  sVarPre = "ADC" & vIndex
  sRegPre = sVarPre & "_"

  If (vIndex = 0) Then
        vChannelCount = 16
        Else
        vChannelCount = 8
        End If

  For i = 0 To (vChannelCount - 1)
  
      If ((r.Exist(sVarPre & "_PARA_ANA_" & i))) Then
          
          '------------------------------------------------------------------------------------------------
          ' Description for DUMMY_PARA_xx
          '------------------------------------------------------------------------------------------------
          sDescription = "This parameter identifies " & sVarPre & " analog channel " & i
          sParaName = "ANA_" & i
          If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
      Else
         Exit Function
      End If
  
  Next i
  For i = 0 To 2
      If ((r.Exist(sVarPre & "_PARA_SOURCE_" & i))) Then
          '------------------------------------------------------------------------------------------------
          ' Description for DUMMY_PARA_xx
          '------------------------------------------------------------------------------------------------
          sDescription = "This parameter identifies " & sVarPre & " -Source " & i
          sParaName = "SOURCE_" & i
          If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
      Else
          Exit Function
      End If
  Next i

  If (Not para.Sort) Then Exit Function
  RefreshParameterDescriptions = True
End Function
'*****************************************************************************
'Description:
'          This function returns the module clock [MHz] of the
'          ADC module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'      GetADCModuleClock [MHz]
'
'*****************************************************************************
Function GetADCModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  GetADCModuleClock = GetPeripheralClock(r)
   
End Function
'*****************************************************************************
'Description:
'          This function returns the ADC Analog Clock value [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'      GetADCAnalogClock [MHz]
'
'*****************************************************************************
Function GetADCAnalogClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  GetADCAnalogClock = GetPeripheralClock(r) / (r.v("ADC" & vIndex & "_GLOBCTR_DIVA")+1)
  
End Function
'*****************************************************************************
'Description:
'          This function returns the ADC Digital Clock value [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'      GetADCdigitalClock [MHz]
'
'*****************************************************************************
Function GetADCdigitalClock(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  GetADCdigitalClock = GetPeripheralClock(r) / (r.v("ADC" & vIndex & "_GLOBCTR_DIVD") +1)
   
End Function
'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipModuleDisabled(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  Dim sVarPre As String
  
  sVarPre = "ADC" & vIndex
  
  GetTooltipModuleDisabled = ""
  GetTooltipModuleDisabled = GetTooltipModuleDisabled & FormatDisabledToolTip("the " & sVarPre & " module is disabled")
End Function

'*****************************************************************************
'Description:
'        This function intialize the Function Page variables
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************

Sub DisableAllFunctions(r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim sVarPre As String
  Dim i       As Integer
  
  sVarPre = "ADC" & vIndex
  
  r.v(sVarPre & "_USE_StartSeq0ReqChNum") = 0
  r.v(sVarPre & "_USE_StartSeq2ReqChNum") = 0
  r.v(sVarPre & "_USE_ClearPendingBits") = 0
  For i = 0 To 3
      r.v(sVarPre & "_USE_SRN" & i) = 0
  Next i
  r.v(sVarPre & "_USE_SetLoadEvent") = 0
  r.v(sVarPre & "_USE_StartParReqChNum") = 0
  r.v(sVarPre & "_USE_Busy") = 0
  r.v(sVarPre & "_USE_GetResultData") = 0
End Sub

'*****************************************************************************
'Description:
'          This function returns the ADC Conversion Time[uSec].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'      GetAdcConversionTime [uSec]
'
'*****************************************************************************
Function GetAdcConversionTime(r As ProjectData, vIndex As Variant) As Double
 On Error Resume Next
 
  GetAdcConversionTime = 17 / GetADCAnalogClock(r, vIndex)
  
End Function

'*****************************************************************************
'Description:
'          This function Checks the SRN status and returns the True or False.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'-----------------------------------------------------------------------------
'Returns:
'      CheckSRNx [True/False]
'
'*****************************************************************************
Function CheckSRNx(r As ProjectData, vIndex As Variant, SRN As Integer) As Boolean
 On Error Resume Next
 Dim i As Integer
 Dim InterruptPtrRegNum As Integer
 Dim sVarPre As String
 Dim sRegPre As String
 
 sVarPre = "ADC" & vIndex
 sRegPre = sVarPre & "_"
 
 CheckSRNx = False
 
 For i = 0 To (15 - vIndex * 8)
   If ((i < 4)) Then
      InterruptPtrRegNum = 0 'First four channels have common sfr register
   ElseIf ((i < 8) And (i > 3)) Then
      InterruptPtrRegNum = 4 'Second four channels have common sfr register
   ElseIf ((i < 12) And (i > 7)) Then
      InterruptPtrRegNum = 8 'Third four channels have common sfr register
   ElseIf ((i < 16) And (i > 11)) Then
      InterruptPtrRegNum = 12 'Fourth four channels have common sfr register
   Else
  'Do nothing
   End If
   
   If ((Not (r.v(sRegPre & "CHCTR" & i & "_LCC") = 0)) And _
      (r.v(sRegPre & "ENABLE_CHANNEL" & i) = 1) And _
       r.v(sRegPre & "CHINPR" & InterruptPtrRegNum & "_CHINP" & i) = SRN) Then
       If (vIndex = 0) Then
          ADC0_Channel(i) = 1
       Else
          ADC1_Channel(i) = 1
       End If
       CheckSRNx = True
   Else
      If (vIndex = 0) Then
         ADC0_Channel(i) = 0
      Else
         ADC1_Channel(i) = 0
      End If
   End If
  
 Next i

 If ((r.v(sRegPre & "ASENR_ASEN0") = 1) And (r.v(sRegPre & "SEQ0_ENABLE_INTERRUPT") = 1) And (r.v(sRegPre & "EVINPR0_" & "EVINP0") = SRN)) Then
     CheckSRNx = True
     If (vIndex = 0) Then
        ADC0_Source(0) = 1
     Else
        ADC1_Source(0) = 1
     End If
        'Exit Function
 Else
     If (vIndex = 0) Then
         ADC0_Source(0) = 0
     Else
         ADC1_Source(0) = 0
     End If
 End If
 If ((r.v(sRegPre & "ASENR_ASEN1") = 1) And (r.v(sRegPre & "CRMR1_ENSI") = 1) And (r.v(sRegPre & "EVINPR0_" & "EVINP1") = SRN)) Then
     CheckSRNx = True
     If (vIndex = 0) Then
         ADC0_Source(1) = 1
     Else
         ADC1_Source(1) = 1
     End If
        'Exit Function
  Else
     If (vIndex = 0) Then
        ADC0_Source(1) = 0
     Else
        ADC1_Source(1) = 0
     End If
  End If
  If ((r.v(sRegPre & "ASENR_ASEN2") = 1) And (r.v(sRegPre & "SEQ2_ENABLE_INTERRUPT") = 1) And (r.v(sRegPre & "EVINPR0_" & "EVINP2") = SRN)) Then
      CheckSRNx = True
      If (vIndex = 0) Then
         ADC0_Source(2) = 1
      Else
         ADC1_Source(2) = 1
      End If
        'Exit Function
  Else
      If (vIndex = 0) Then
         ADC0_Source(2) = 0
      Else
         ADC1_Source(2) = 0
      End If
  End If
  
  For i = 0 To 3
  If ((r.v(sRegPre & "RCR" & i & "_IEN") = 1) And (r.v(sRegPre & "EVINPR8_" & "EVINP" & (i + 8)) = SRN)) Then
      CheckSRNx = True
      If (vIndex = 0) Then
          ADC0_ResultReg(i) = 1
      Else
          ADC1_ResultReg(i) = 1
      End If
        'Exit Function
  Else
      If (vIndex = 0) Then
         ADC0_ResultReg(i) = 0
      Else
         ADC1_ResultReg(i) = 0
      End If
  End If
 Next i

 For i = 4 To 7
  If ((r.v(sRegPre & "RCR" & i & "_IEN") = 1) And (r.v(sRegPre & "EVINPR12_" & "EVINP" & (i + 8)) = SRN)) Then
      CheckSRNx = True
      If (vIndex = 0) Then
          ADC0_ResultReg(i) = 1
      Else
          ADC1_ResultReg(i) = 1
      End If
        'Exit Function
  Else
      If (vIndex = 0) Then
         ADC0_ResultReg(i) = 0
      Else
         ADC1_ResultReg(i) = 0
      End If
  End If
 Next i
End Function

'*****************************************************************************
'Description:
'          This function Clears all unused IC registers.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'        RegName          Node pointer registers
'                         Ex:- CHINPR_CHINP0
'-----------------------------------------------------------------------------
'Returns:
'      None
'
'*****************************************************************************
Sub ClearUnusedICRegisters(r As ProjectData, vIndex As Variant, RegName As String)

 On Error Resume Next

 Dim sRegPre As String

 sRegPre = "ADC" & vIndex & "_"

 If (r.v(sRegPre & RegName) = 0) Then
     r.v("ADC_" & (0 + vIndex * 4) & "IC_IE") = 1
     r.v(sRegPre & "USE_SRN0") = 1
     If (Not CheckSRNx(r, vIndex, 1)) Then
         r.v("ADC_" & (1 + vIndex * 4) & "IC_IE") = 0
         r.v(sRegPre & "USE_SRN1") = 0
     End If
     If Not CheckSRNx(r, vIndex, 2) Then
        r.v("ADC_" & (2 + vIndex * 4) & "IC_IE") = 0
        r.v(sRegPre & "USE_SRN2") = 0
     End If
     If Not CheckSRNx(r, vIndex, 3) Then
        r.v("ADC_" & (3 + vIndex * 4) & "IC_IE") = 0
        r.v(sRegPre & "USE_SRN3") = 0
     End If
 ElseIf (r.v(sRegPre & RegName) = 1) Then
        r.v("ADC_" & (1 + vIndex * 4) & "IC_IE") = 1
        r.v(sRegPre & "USE_SRN1") = 1
        If (Not CheckSRNx(r, vIndex, 0)) Then
            r.v("ADC_" & (0 + vIndex * 4) & "IC_IE") = 0
            r.v(sRegPre & "USE_SRN0") = 0
        End If
        If Not CheckSRNx(r, vIndex, 2) Then
           r.v("ADC_" & (2 + vIndex * 4) & "IC_IE") = 0
           r.v(sRegPre & "USE_SRN2") = 0
        End If
        If Not CheckSRNx(r, vIndex, 3) Then
               r.v("ADC_" & (3 + vIndex * 4) & "IC_IE") = 0
               r.v(sRegPre & "USE_SRN3") = 0
        End If
 ElseIf (r.v(sRegPre & RegName) = 2) Then
        r.v("ADC_" & (2 + vIndex * 4) & "IC_IE") = 1
        r.v(sRegPre & "USE_SRN2") = 1
        If (Not CheckSRNx(r, vIndex, 0)) Then
            r.v("ADC_" & (0 + vIndex * 4) & "IC_IE") = 0
            r.v(sRegPre & "USE_SRN0") = 0
        End If
        If Not CheckSRNx(r, vIndex, 1) Then
           r.v("ADC_" & (1 + vIndex * 4) & "IC_IE") = 0
           r.v(sRegPre & "USE_SRN1") = 0
        End If
        If Not CheckSRNx(r, vIndex, 3) Then
           r.v("ADC_" & (3 + vIndex * 4) & "IC_IE") = 0
           r.v(sRegPre & "USE_SRN3") = 0
        End If
 ElseIf (r.v(sRegPre & RegName) = 3) Then
        r.v("ADC_" & (3 + vIndex * 4) & "IC_IE") = 1
        r.v(sRegPre & "USE_SRN3") = 1
        If (Not CheckSRNx(r, vIndex, 0)) Then
            r.v("ADC_" & (0 + vIndex * 4) & "IC_IE") = 0
            r.v(sRegPre & "USE_SRN0") = 0
        End If
        If Not CheckSRNx(r, vIndex, 1) Then
           r.v("ADC_" & (1 + vIndex * 4) & "IC_IE") = 0
           r.v(sRegPre & "USE_SRN1") = 0
        End If
        If Not CheckSRNx(r, vIndex, 2) Then
           r.v("ADC_" & (2 + vIndex * 4) & "IC_IE") = 0
           r.v(sRegPre & "USE_SRN2") = 0
        End If
 End If
End Sub

'*****************************************************************************
'Description:
'          This function Checks the status of Pin 6.1.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'        RegName          Node pointer registers
'                         Ex:- CHINPR_CHINP0
'-----------------------------------------------------------------------------
'Returns:
'               CheckStatusOfPin6_1[True/False]
'*****************************************************************************
Function CheckStatusOfPin6_1(r As ProjectData, vIndex As Variant) As Boolean
 On Error Resume Next
 
 Dim sRegPre As String
 CheckStatusOfPin6_1 = False
 sRegPre = "ADC" & vIndex & "_"
 
 If ((r.v("ADC0_ASENR_ASEN0") = 1) And (r.v("ADC0_PISEL_REQTR0") = 2)) Then
      CheckStatusOfPin6_1 = True
      Exit Function
 ElseIf ((r.v("ADC0_ASENR_ASEN1") = 1) And (r.v("ADC0_PISEL_REQTR1") = 2)) Then
        CheckStatusOfPin6_1 = True
        Exit Function
 ElseIf ((r.v("ADC0_ASENR_ASEN2") = 1) And (r.v("ADC0_PISEL_REQTR2") = 2)) Then
        CheckStatusOfPin6_1 = True
        Exit Function
 ElseIf ((r.v("ADC1_ASENR_ASEN0") = 1) And (r.v("ADC1_PISEL_REQTR0") = 2)) Then
        CheckStatusOfPin6_1 = True
        Exit Function
 ElseIf ((r.v("ADC1_ASENR_ASEN1") = 1) And (r.v("ADC1_PISEL_REQTR1") = 2)) Then
        CheckStatusOfPin6_1 = True
        Exit Function
 ElseIf ((r.v("ADC1_ASENR_ASEN2") = 1) And (r.v("ADC1_PISEL_REQTR2") = 2)) Then
        CheckStatusOfPin6_1 = True
        Exit Function
 Else
   'Do nothing
 End If
End Function

'*****************************************************************************
'Description:
'          This function Checks the status of Pin 6.3.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           ParameterValue
'        RegName          Node pointer registers
'                         Ex:- CHINPR_CHINP0
'-----------------------------------------------------------------------------
'Returns:
'               CheckStatusOfPin6_3[True/False]
'*****************************************************************************
Function CheckStatusOfPin6_3(r As ProjectData, vIndex As Variant) As Boolean
 On Error Resume Next

 Dim sRegPre As String
 CheckStatusOfPin6_3 = False
 sRegPre = "ADC" & vIndex & "_"
 
 If ((r.v("ADC0_ASENR_ASEN0") = 1) And (r.v("ADC0_PISEL_REQTR0") = 3)) Then
     CheckStatusOfPin6_3 = True
     Exit Function
 ElseIf ((r.v("ADC0_ASENR_ASEN1") = 1) And (r.v("ADC0_PISEL_REQTR1") = 3)) Then
     CheckStatusOfPin6_3 = True
     Exit Function
 ElseIf ((r.v("ADC0_ASENR_ASEN2") = 1) And (r.v("ADC0_PISEL_REQTR2") = 3)) Then
     CheckStatusOfPin6_3 = True
     Exit Function
 ElseIf ((r.v("ADC1_ASENR_ASEN0") = 1) And (r.v("ADC1_PISEL_REQTR0") = 3)) Then
     CheckStatusOfPin6_3 = True
     Exit Function
 ElseIf ((r.v("ADC1_ASENR_ASEN1") = 1) And (r.v("ADC1_PISEL_REQTR1") = 3)) Then
     CheckStatusOfPin6_3 = True
     Exit Function
 ElseIf ((r.v("ADC1_ASENR_ASEN2") = 1) And (r.v("ADC1_PISEL_REQTR2") = 3)) Then
     CheckStatusOfPin6_3 = True
     Exit Function
 Else
   'Do nothing
 End If
End Function

'*****************************************************************************
'Description:
'          This function Clears all ADC IC registers.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'               None
'*****************************************************************************
Sub ClearAllICRegisters(r As ProjectData)
    On Error Resume Next
    
    Dim sRegPre As String
    sRegPre = "ADC_"
    r.sfr(sRegPre & "0IC") = 0
    r.sfr(sRegPre & "1IC") = 0
    r.sfr(sRegPre & "2IC") = 0
    r.sfr(sRegPre & "3IC") = 0
    r.sfr(sRegPre & "4IC") = 0
    r.sfr(sRegPre & "5IC") = 0
    r.sfr(sRegPre & "6IC") = 0
    r.sfr(sRegPre & "7IC") = 0
End Sub

'*****************************************************************************
'Description:
'          This function Clears all ADC0 IC registers.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'               None
'*****************************************************************************
Sub ClearAllADC0ICRegisters(r As ProjectData)
    On Error Resume Next
    Dim sRegPre As String
    sRegPre = "ADC_"
    r.sfr(sRegPre & "0IC") = 0
    r.sfr(sRegPre & "1IC") = 0
    r.sfr(sRegPre & "2IC") = 0
    r.sfr(sRegPre & "3IC") = 0
End Sub
'*****************************************************************************
'Description:
'          This function Clears all ADC1 IC registers.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'               None
'*****************************************************************************
Sub ClearAllADC1ICRegisters(r As ProjectData)
    On Error Resume Next
    
    Dim sRegPre As String
    sRegPre = "ADC_"
    r.sfr(sRegPre & "4IC") = 0
    r.sfr(sRegPre & "5IC") = 0
    r.sfr(sRegPre & "6IC") = 0
    r.sfr(sRegPre & "7IC") = 0
End Sub

'*****************************************************************************
'Description:
'          This function Clears all ADC1 IC registers.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'               None
'*****************************************************************************

Sub FIFORegisterDefaultConfiguration(r As ProjectData,vIndex As Variant,FifoStructureSelected As Integer)
	On Error Resume Next
    
    Dim sRegPre As String
	Dim i As Integer
    sRegPre = "ADC" & vIndex & "_"
	
    For i = 0 to 7
		r.v(sRegPre & "UserDefined" & i) = 0
		r.v(sRegPre & "RCR" & i & "_FEN") = 0
	Next i
	If(FifoStructureSelected = 0) Then
		r.v(sRegPre & "RCR3" & "_FEN") = 1
		r.v(sRegPre & "UserDefined3") = 1
		r.v(sRegPre & "RCR2" & "_FEN") = 1	
	   	r.v(sRegPre & "UserDefined2") = 2
		r.v(sRegPre & "RCR1" & "_FEN") = 1
		r.v(sRegPre & "UserDefined1") = 2
		r.v(sRegPre & "RCR0" & "_FEN") = 1
		r.v(sRegPre & "UserDefined0") = 3
	ElseIf(FifoStructureSelected = 1) Then
		r.v(sRegPre & "RCR7" & "_FEN") = 1
		r.v(sRegPre & "UserDefined7") = 1	
	   	r.v(sRegPre & "RCR6" & "_FEN") = 1
		r.v(sRegPre & "UserDefined6") = 2
		r.v(sRegPre & "RCR5" & "_FEN") = 1
		r.v(sRegPre & "UserDefined5") = 2
		r.v(sRegPre & "RCR4" & "_FEN") = 1
		r.v(sRegPre & "UserDefined4") = 3
	ElseIf(FifoStructureSelected = 2) Then
	    r.v(sRegPre & "RCR7" & "_FEN") = 1
		r.v(sRegPre & "UserDefined7") = 1
		r.v(sRegPre & "RCR6" & "_FEN") = 1	
	   	r.v(sRegPre & "UserDefined6") = 2
		r.v(sRegPre & "RCR5" & "_FEN") = 1
		r.v(sRegPre & "UserDefined5") = 2
		r.v(sRegPre & "RCR4" & "_FEN") = 1
		r.v(sRegPre & "UserDefined4") = 2
		r.v(sRegPre & "RCR3" & "_FEN") = 1
		r.v(sRegPre & "UserDefined3") = 2
		r.v(sRegPre & "RCR2" & "_FEN") = 1
		r.v(sRegPre & "UserDefined2") = 2
		r.v(sRegPre & "RCR1" & "_FEN") = 1
		r.v(sRegPre & "UserDefined1") = 2
		r.v(sRegPre & "RCR0" & "_FEN") = 1
		r.v(sRegPre & "UserDefined0") = 3
	End If

End Sub
