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

Option Explicit
'*****************************************************************************
' @Doc
' @Module        GLOBAL.INC
'-----------------------------------------------------------------------------
' @Controllers
'
' @Description   This module contains global functions that can
'                be used in all other script files.
'
'-----------------------------------------------------------------------------
' @History       Date           Author        Version
'                03.05.07       Jagadeesha     XC22xx v0.01
'*****************************************************************************

' this type is for the calculation of a time
' it is used in the function ConvertTime()
Type UnitType
  dValue As Double   ' value of the time / frequency
  sUnit As String    ' unit of the time / frequency
  dDivider As Double    ' divider factor referring to usec / MHz
End Type

Global U0C0Proto As String
Global U0C1Proto As String
Global U1C0Proto As String
Global U1C1Proto As String
Global U2C0Proto As String
Global U2C1Proto As String

'Marking Option : global variables
Type tModule
    ModuleName As String
    Left As Long
    Top As Long
    Width As Long
    Height As Long
End Type

Global ModuleDetails() As tModule

'Start (P11 - Jai)
Type CType
    CntrlrType As String
End Type

Global CTypeDetails() As CType
'End (P11 - Jai)

Type tPortPin
     PortName As String
     PinNum As Integer
     sAltName() As String
     sDescription() As String
     bExclusive() As Variant
     iFunction() As Variant
End Type

Type tPort
     PinDetails() As tPortPin
End Type
Global PortDetails() As tPort


Type tPortDetails
     PortName As String
     PortDescription As String
     StartPin As Integer
     LastPin As Integer
     InputChar As Integer
     EdgeChar As Integer
     DriveStrength As Integer
End Type

Global Ports() As tPortDetails

Global bChangedSystemClock As Boolean ' Added on 05.09.07 to update peripheral clock when system frequency is changed in PLL page

'Constants for functions UpdateAllPeripherals and UpdateOnePeripherals
Global Const INITIALIZATION = 0          'initialization for a new project
Global Const UPDATE_CLOCK = 1            'clock unpdate
Global Const SET_MODULE_TO_DEFAULT = 3   'set module to the default
'*****************************************************************************
'Description:
'        This function returns the max fcpu [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        max fcpu [MHz]
'*****************************************************************************
Function GetMaxFcpu(r As ProjectData) As Integer
    Dim sMC_Name As String

    sMC_Name = DefGetValue("Name")
    
     GetMaxFcpu = 80
     If (r.v("MAIN_CPU_TYPE") = sMC_Name & "-96F80") Then
        GetMaxFcpu = 80
     ElseIf (r.v("MAIN_CPU_TYPE") = sMC_Name & "-96F66") Then
        GetMaxFcpu = 66
     End If
End Function
'*****************************************************************************
'Description:
'         This function returns the system clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   system clock [MHz]
'*****************************************************************************
Function GetSystemClock(r As ProjectData) As Variant
  On Error GoTo GetSystemClockError

    Dim sMC_Name As String

    sMC_Name = DefGetValue("Name")

    GetSystemClock = GetMaxFcpu(r)
    If (r.v("MAIN_SYSCLKVAL") = 2) Then
         'GetSystemClock = r.v("MAIN_CLK_CPU")
         If (r.v("MAIN_ACTUALSYSCLKVAL") > 0) Then
            GetSystemClock = r.v("MAIN_ACTUALSYSCLKVAL")
         Else
            GetSystemClock = r.v("MAIN_CLK_CPU")
         End If
    End If
    If ((r.v("MAIN_CPU_TYPE") = sMC_Name & "-96F80") And (r.v("MAIN_SYSCLKVAL") = 1)) Then
         GetSystemClock = Round(GetSystemClock / 1.2121)
'    ElseIf (r.v("MAIN_SYSCLKVAL") = 2) Then
'         GetSystemClock = Round(GetSystemClock / 2)
    End If
  Exit Function

GetSystemClockError:
  errorbox "Error in function GetSystemClock", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'         This function returns the peripheral clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   peripheral clock [MHz]
'*****************************************************************************
Function GetPeripheralClock(r As ProjectData) As Variant
  On Error GoTo GetPeripheralClockError

  Dim sMC_Name As String

  sMC_Name = DefGetValue("Name")
    
    GetPeripheralClock = GetMaxFcpu(r)
    If (r.v("MAIN_SYSCLKVAL") = 2) Then
        'GetPeripheralClock = r.v("MAIN_CLK_CPU")
         If (r.v("MAIN_ACTUALSYSCLKVAL") > 0) Then
            GetPeripheralClock = r.v("MAIN_ACTUALSYSCLKVAL")
         Else
            GetPeripheralClock = r.v("MAIN_CLK_CPU")
         End If
    End If
    If ((r.v("MAIN_CPU_TYPE") = sMC_Name & "-96F80") And (r.v("MAIN_SYSCLKVAL") = 1)) Then
         GetPeripheralClock = Round(GetPeripheralClock / 1.2121)
'    ElseIf (r.v("MAIN_SYSCLKVAL") = 2) Then
'         GetPeripheralClock = Round(GetPeripheralClock / 2)
    End If
  Exit Function

GetPeripheralClockError:
  errorbox "Error in function GetPeripheralClock", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'         This function returns the maximum system clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   system clock [MHz]
'*****************************************************************************
Function GetMaxSystemClock(r As ProjectData) As Double
  On Error Resume Next
  'GetMaxSystemClock = r.d("MAIN_CLK_CPU")
   If (InStr(r.v("MAIN_CPU_TYPE"), "96F80")) Then
       GetMaxSystemClock = Format(80, "0.00")
   ElseIf (InStr(r.v("MAIN_CPU_TYPE"), "96F66")) Then
       GetMaxSystemClock = Format(66, "0.00")
   End If
End Function

'*****************************************************************************
'Description:
'         This function returns the maximum peripheral clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   peripheral clock [MHz]
'*****************************************************************************
Function GetMaxPeripheralClock(r As ProjectData) As Double
  On Error Resume Next

  GetMaxPeripheralClock = r.d("MAIN_CLK_PD")
End Function
'*****************************************************************************
'Description:
'        This function makes all activities to enable an interrupt.
'        It sets the service request enable bit and selectes the service
'        request routine.
'        Example:
'        Call EnableSRN(r, "SSC0", "SSC0_TIC", "Tx")
'
'        For this example the register SSC0_TIC and the variable
'        SSC0_USE_Tx must exist.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModulName        name of the Module (e.g. SSC0)
'        RegName          name of the register (e.g. SSC0_RIC)
'        ISRName          name of the service request routine (e.g Rx)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub EnableSRN(r As ProjectData, ModuleName As String, RegName As String, ISRName As String)
On Error Resume Next
 If (RegName = "EOPIC") Then
 r.v(RegName & "_EOPIE") = 1
 r.v(ModuleName & "_USE_" & ISRName) = 1
 Else
 r.v(RegName & "_IE") = 1
 r.v(ModuleName & "_USE_" & ISRName) = 1
 End If

End Sub

'*****************************************************************************
'Description:
'        This function makes all activities to disable an interrupt.
'        It resets the service request enable bit, set the priority to zero
'        and deselectes the service request routine.
'        Example:
'        Call DisableSRN(r, "SSC0", "SSC0_TIC", "Tx")
'
'        For this example the register SSC0_TIC and the variable
'        SSC0_USE_Tx must exist.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModuleName       the name of the module (e.g. SSC0)
'        RegName          the name of the register (e.g. SSC0_RIC)
'        ISRName          the name of the service request routine (e.g Rx)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableSRN(r As ProjectData, ModuleName As String, RegName As String, ISRName As String)
  On Error Resume Next
  Dim iChannel As Integer
  Dim iLevel As Integer
  Dim iGroup As Integer
  Dim iGPX As Integer

  iLevel = r.v(RegName & "_ILVL")
  iGroup = r.v(RegName & "_GLVL")
  iGPX = r.v(RegName & "_GPX")
  
  
   If (iLevel = 12) Then
               If iGPX = 0 Then
                   If r.v("BNK0_GPR" & iGroup) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & iGroup) > 0 Then
                   r.v("INT_USE_FAST" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1)) = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GLVL") = 0
                   Else
                    r.v("BNK0_GPR" & iGroup) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & iGroup) = 0
               Else
                   If r.v("BNK2_GPR" & iGroup) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) > 0 Then
                   r.v("INT_USE_FAST" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1)) = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_GLVL") = 0
                   Else
                    r.v("BNK2_GPR" & iGroup) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) = 0
              End If
           End If
           
           
      If (iLevel = 13) Then
               If iGPX = 0 Then
                   If r.v("BNK0_GPR" & (iGroup + 4)) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & iGroup) > 0 Then
                   r.v("INT_USE_FAST" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1)) = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GLVL") = 0
                   Else
                    r.v("BNK0_GPR" & (iGroup + 4)) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & iGroup) = 0
               Else
                   If r.v("BNK2_GPR" & (iGroup + 4)) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) > 0 Then
                   r.v("INT_USE_FAST" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1)) = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_GLVL") = 0
                   Else
                    r.v("BNK2_GPR" & (iGroup + 4)) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) = 0
              End If
           End If
           
       If (iLevel = 14) Then
               If iGPX = 0 Then
                   If r.v("BNK1_GPR" & iGroup) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & iGroup) > 0 Then
                   r.v("INT_USE_FAST" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1)) = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GLVL") = 0
                   
                   Else
                    r.v("BNK1_GPR" & iGroup) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & iGroup) = 0
               Else
                   If r.v("BNK3_GPR" & iGroup) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) > 0 Then
                   r.v("INT_USE_FAST" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1)) = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) - 1) & "CSP_GLVL") = 0
                   Else
                    r.v("BNK3_GPR" & iGroup) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) = 0
              End If
           End If
           
           If (iLevel = 15) Then
               If iGPX = 0 Then
                   If r.v("BNK1_GPR" & (iGroup + 4)) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & iGroup) > 0 Then
                   r.v("INT_USE_FAST" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1)) = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & (r.v("INT_LEV" & iLevel & "_GRP" & iGroup) - 1) & "CSP_GLVL") = 0
                   Else
                    r.v("BNK1_GPR" & (iGroup + 4)) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & iGroup) = 0
               Else
                   If r.v("BNK3_GPR" & (iGroup + 4)) = 0 And r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) > 0 Then
                   r.v("INT_USE_FAST" & r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4) - 1)) = 0
                   r.v("FINT" & r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4) - 1) & "CSP_EN") = 0 'Added by jag on 08.12.07
                   r.v("FINT" & r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4) - 1) & "CSP_GPX") = 0
                   r.v("FINT" & r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4) - 1) & "CSP_ILVL") = 0
                   r.v("FINT" & r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4) - 1) & "CSP_GLVL") = 0
                   Else
                    r.v("BNK3_GPR" & (iGroup + 4)) = 0
                   End If
                   r.v("INT_LEV" & iLevel & "_GRP" & (iGroup + 4)) = 0
              End If
           End If

    
  
  
  

  
  If (RegName = "EOPIC") Then
   r.v(RegName & "_EOPIE") = 0
  Else
  r.v(RegName & "_IE") = 0
   End If
  r.v(RegName & "_GLVL") = 0
  r.v(RegName & "_ILVL") = 0
  
  r.v(ModuleName & "_USE_" & ISRName) = 0
  
  If (iLevel = 14 Or iLevel = 12 Or iLevel = 10 Or iLevel = 8) Then
      iChannel = iGroup
      Call SetPECChannelToDefault(r, iChannel)
  ElseIf (iLevel = 15 Or iLevel = 13 Or iLevel = 11 Or iLevel = 9) Then
      iChannel = iGroup + 4
      Call SetPECChannelToDefault(r, iChannel)
  End If
End Sub
'*****************************************************************************
'Description:
'        This function checks if the requested interrupt service routine
'        is selected.
'        Example:
'        IsISRSelected(r, "SSC0", "SSC0_TIC", "Tx")
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModuleName       the name of the module (e.g. SSC0)
'        RegName          the name of the service request control register (e.g SSC0_TIC)
'        ISRName          the name of the service request routine (e.g Tx)
'-----------------------------------------------------------------------------
'Returns:
'       Fales             if not seleected
'       True              else
'*****************************************************************************
Function IsISRSelected(r As ProjectData, ModuleName As String, RegName As String, ISRName As String) As Boolean
On Error Resume Next
  IsISRSelected = False
  If (r.v(RegName & "_GLVL") > 0 Or r.v(RegName & "_ILVL") > 0) Then
  If (RegName = "EOPIC") Then
    IsISRSelected = (r.v(ModuleName & "_USE_Init") = 1 And _
                     r.v(RegName & "_EOPIE") = 1 And _
                     r.v(ModuleName & "_USE_" & ISRName) = 1)
  Else
    IsISRSelected = (r.v(ModuleName & "_USE_Init") = 1 And _
                     r.v(RegName & "_IE") = 1 And _
                     r.v(ModuleName & "_USE_" & ISRName) = 1)
 End If
 End If
End Function

'*****************************************************************************
'Description:
'        This function checks if the function is selected.
'        Example:
'        IsIFctSelected(r, "SSC0", "ReadCAPREL")
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModuleName       the name of the module (e.g. SSC0)
'        FctName          the name of the function (e.g ReadCAPREL)
'-----------------------------------------------------------------------------
'Returns:
'       Fales             if not seleected
'       True              else
'*****************************************************************************
Function IsFctSelected(r As ProjectData, ModuleName As String, FctName As String) As Boolean
On Error Resume Next
  IsFctSelected = False
  IsFctSelected = (r.v(ModuleName & "_USE_Init") = 1 And r.v(ModuleName & "_USE_" & FctName) = 1)
End Function

'*****************************************************************************
'Description:
'        This functions generates the statments for the interrupt service
'        request node.
'        Example:
'        Call GenerateIR(f, r, "SSCTIC", "SSCTIE", "transmit")
'
'-----------------------------------------------------------------------------
'Parameters:
'        f                file handle
'        r                reference to the ProjectData object
'        RegName          the name of the service request control register
'                         (e.g. SSCTIC)
'        ISRName          the name of the interrupt node (e.g transmit )
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub GenerateIR(f As Variant, r As ProjectData, RegName As String, IntName As String)
    On Error Resume Next
       
    Dim v           As Variant
    Dim CHKREG      As String
    Dim iHelp       As Integer
    Dim iLev        As Integer
    Dim iNotPLEV    As Integer
    Dim d           As String
    
    If (RegName = "EOPIC") Then
      CHKREG = "EOPIC_EOPIE"
      Else
     CHKREG = RegName & "_IE"
     End If
      
''      If (r.v(RegName & "_IE") = 1) Then
      If (r.v(CHKREG) = 1) Then
      d = IntName & " service request node configuration:"
      Print #f, FormatComments(2, 1, "", "", d)
    
      If (r.v(RegName & "_ILVL") = 0) Then
        Print #f, FormatCommentsForInit(2, IntName & " interrupt is enabled but NO INTERRUPT WILL BE GENERATED because priority level is 0")
      Else
        Print #f, FormatCommentsForInit(1, IntName & " interrupt priority level (ILVL) = " & Format(r.v(RegName & "_ILVL")))
        Print #f, FormatCommentsForInit(1, IntName & " interrupt group level (GLVL) = " & Format(r.v(RegName & "_GLVL")))
        Print #f, FormatCommentsForInit(1, IntName & " group priority extension (GPX) = " & Format(r.v(RegName & "_GPX")))
      End If
      
      v = 0
      v = v + r.sfr(RegName)
      Print #f, ""
      Print #f, FormatCodeStatemente(2, RegName, "=", FormatTextOutput(v, "H4"), "")
      Print #f, ""

      If (r.v(RegName & "_ILVL") = 8 Or _
          r.v(RegName & "_ILVL") = 10 Or _
          r.v(RegName & "_ILVL") = 12 Or _
          r.v(RegName & "_ILVL") = 14) Then
          
          iHelp = r.v(RegName & "_GLVL")
          iLev = 8
      
      ElseIf (r.v(RegName & "_ILVL") = 9 Or _
          r.v(RegName & "_ILVL") = 11 Or _
          r.v(RegName & "_ILVL") = 13 Or _
          r.v(RegName & "_ILVL") = 15) Then
          
          iHelp = r.v(RegName & "_GLVL") + 4
          iLev = 9
      
      End If

      'PLEV muss mit xIC_ILVL invertiert vergliechen werden
      If (r.v("PECC" & iHelp & "_PLEV") = 0) Then
          iNotPLEV = 3
      ElseIf (r.v("PECC" & iHelp & "_PLEV") = 1) Then
          iNotPLEV = 2
      ElseIf (r.v("PECC" & iHelp & "_PLEV") = 2) Then
          iNotPLEV = 1
      ElseIf (r.v("PECC" & iHelp & "_PLEV") = 3) Then
          iNotPLEV = 0
      End If

      If (r.v(RegName & "_ILVL") > 7 And r.v(RegName & "_GPX") = 0 And _
          ((r.v(RegName & "_ILVL") - iLev) / 2 = iNotPLEV)) Then
        
        '------------------------------------------------------------------------------------------------
        ' Use PEC channel " & iHelp & " for " & r.v("PEC_CHANNEL_" & iHelp) & ":"
        '------------------------------------------------------------------------------------------------
        Print #f, "  ///  Use PEC channel " & iHelp & " for " & r.v("PEC_CHANNEL_" & iHelp) & ":"

        '--------------------------------------------- PECCx_COUNT
        If (r.v("PECC" & iHelp & "_COUNT") = 255) Then
          d = "continuous transfer"
        Else
          If (r.v("PECC" & iHelp & "_COUNT") = 0) Then
            d = "normal interrupt"
          Else
            d = "decrement counter"
          End If
        End If
        Print #f, FormatCommentsForInit(1, d)

        '--------------------------------------------- PECCx_INC
        Select Case (r.v("PECC" & iHelp & "_INC"))
                Case (0)
                      d = "pointers are not modified"
                Case (1)
                      d = "increment destination pointer"
                Case (2)
                      d = "increment source pointer"
                Case (3)
                      d = "increment destination and source pointers"
        End Select
        Print #f, FormatCommentsForInit(1, d)

        '--------------------------------------------- PECCx_BWT
        If (r.v("PECC" & iHelp & "_BWT") = 0) Then
          d = "transfer a word"
        Else
          d = "transfer a byte"
        End If
        Print #f, FormatCommentsForInit(1, d)
        
        '--------------------------------------------- PECCx_EOPINT
        If (r.v("PECC" & iHelp & "_EOPINT") = 0) Then
          d = "service End of PEC interrrupt by a EOP interrupt node is disabled"
        Else
          d = "service End of PEC interrrupt by a EOP interrupt node is enabled"
        End If
        Print #f, FormatCommentsForInit(1, d)

        If (r.v("PECC" & iHelp & "_EOPINT") = 1) Then
            If (r.v("PECISNC_C" & iHelp & "IE") = 0) Then
              d = "End of PEC intrrupt request is disabled"
            Else
              d = "End of PEC intrrupt request is enabled"
            End If
            Print #f, FormatCommentsForInit(1, d)
        End If
        
        '--------------------------------------------- PECCx_CL
        If (r.v("PECC" & iHelp & "_CL") = 0) Then
          d = "channel link mode is disabled"
        Else
          d = "channel link mode is enabled"
        End If
        Print #f, FormatCommentsForInit(1, d)
        Print #f, ""

        v = 0
        v = v + r.sfr("PECC" & iHelp)
        Print #f, FormatCodeStatemente(2, "PECC" & iHelp, "=", FormatTextOutput(v, "H4"), _
          "load PECC" & iHelp & " control register")
        Print #f, ""

        '--------------------------------------------- SRCPx_SRCPx
        If (r.v("PECC" & iHelp & "_COUNT") <> 0) Then
          If (r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "0") Then
            'd = "DO NOT FORGET TO SET THE SOURCE-POINTER FOR PECC CHANNEL " & iHelp & " BECAUSE IT IS SELECTED FOR USER DEFINED"
            d = "SOURCE-POINTER for PECC channel " & iHelp & " is set because it is selected for user defined"
            Print #f, FormatCommentsForInit(2, d)
            Print #f, ""                                            'PEC SEG Configuraions added/Raja
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
                Print #f, "  SRCP" & iHelp & "  =  (uword)("; FormatTextOutput(r.v("SRCPTR_UD" & iHelp), "H4"); ");              //set source pointer offset"
                If (r.v("PEC_SEG_SRC" & iHelp) <> 0) Then
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0x00FF) | ((uword)"; FormatTextOutput(r.v("PEC_SEG_SRC" & iHelp), "H2"); " << 8);  //set source pointer segment"
                Else
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0x00FF);  //set source pointer segment"
                End If
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
                Print #f, "  SRCP" & iHelp & "  =  _sof_("; FormatTextOutput(r.v("SRCPTR_UD" & iHelp), "H4") & ");               //set source pointer offset"
                If (r.v("PEC_SEG_SRC" & iHelp) <> 0) Then
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0x00FF) | ((uword)"; FormatTextOutput(r.v("PEC_SEG_SRC" & iHelp), "H2"); " << 8);  //set source pointer segment"
                Else
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0x00FF);  //set source pointer segment"
                End If
            End If
          Else '(r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp)<>"0")
          
           
            If IsPECUSICSOURCESelected(r, iHelp) = True Then
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0x00FF) | 0x2000 ; //set source pointer segment"
            End If
            
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
           
              Print #f, "  SRCP" & iHelp & "  =  (uword)(&" & r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) & ");  //set source pointer"
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
              Print #f, "  SRCP" & iHelp & "  =  _sof_(&" & r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) & ");  //set source pointer"
            End If
          End If
        End If

        '--------------------------------------------- DSTPx_DSTPx
        If (r.v("PECC" & iHelp & "_COUNT") <> 0) Then
          Print #f, ""
          If (r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "0") Then
            'd = "DO NOT FORGET TO SET THE DESTINATION-POINTER FOR PECC CHANNEL " & iHelp & " BECAUSE IT IS SELECTED FOR USER DEFINED"
            d = "DESTINATION-POINTER for PECC channel " & iHelp & " is set because it is selected for user defined"
            Print #f, FormatCommentsForInit(2, d)
            Print #f, ""
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
                Print #f, "  DSTP" & iHelp & "  =  (uword)("; FormatTextOutput(r.v("DSTPTR_UD" & iHelp), "H4"); ");              //set destination pointer offset"
                If (r.v("PEC_SEG_DST" & iHelp) <> 0) Then
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0xFF00) | ("; FormatTextOutput(r.v("PEC_SEG_DST" & iHelp), "H2"); ");      //set destination pointer segment"
                Else
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0xFF00);      //set destination pointer segment"
                End If
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
                Print #f, "  DSTP" & iHelp & "  =  _sof_("; FormatTextOutput(r.v("DSTPTR_UD" & iHelp), "H4") & ");               //set destination pointer offset"
                If (r.v("PEC_SEG_DST" & iHelp) <> 0) Then
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0xFF00) | ("; FormatTextOutput(r.v("PEC_SEG_DST" & iHelp), "H2"); ");       //set destination pointer segment"
                Else
                Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0xFF00);      //set destination pointer segment"
                End If
            End If
          Else
            If IsPECUSICDESTSelected(r, iHelp) = True Then
                    Print #f, "  PECSEG" & iHelp & "  =  (PECSEG" & iHelp & " & 0xFF00) | 0x0020 ; //set destination pointer segment"
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
              Print #f, "  DSTP" & iHelp & "  =  (uword)(&" & r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) & ");  //set destination pointer"
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
              Print #f, "  DSTP" & iHelp & "  =  _sof_(&" & r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) & ");  //set destination pointer"
            End If
          End If
        End If

        Print #f, ""
      End If
    End If
    
End Sub

'*****************************************************************************
'Description:
'        This function generates the declaration for the interrupt service
'        routine in the generated code.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ISRName          the name of the service request routine (e.g SSC0_viTx)
'        VectorName       the name of the service request routine vector (e.g TX_INT)
'        Lvl              Interrupt priority level(e.g 1 - 15)
'-----------------------------------------------------------------------------
'Returns:
'        Service request declaration
'*****************************************************************************
 Function GenerateIsrDeclaration(r As ProjectData, ISRName As String, VectorName As Variant, Optional Lvl As Integer, Optional Grp As Integer) As String
    On Error Resume Next
    
    
    Dim i, j As Integer
    
    
    If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
        If (InStr(VectorName, "TRAP")) Then
            If (InStr(r.v("MAIN_COMPILER"), "Tasking Viper")) Then
                GenerateIsrDeclaration = "_interrupt(" & VectorName & ") void " & ISRName & "(void)"
            Else
                GenerateIsrDeclaration = "interrupt(" & VectorName & ") void " & ISRName & "(void)"
            End If
            
        Else
          If (Lvl < 12) Then
            GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & _
            "  void " & ISRName & "(void)"
          End If
        End If
    End If
    If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
        If (InStr(VectorName, "TRAP")) Then
            GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
        Else
            If (Lvl < 12) Then
               GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
            End If
        End If
    End If
            
        
       'Section code for fast interrupts and bank selection
          
    If (Lvl >= 12) Then
       
       
       ' For Bank Selection Part
       '
       '--------------------------------- Level 12 Part Starts -------------------------------------------------------------------------
       
         If ((Lvl = 12) And (Grp = 0)) Then
            If (r.v("BNK0_GPR0") = 2) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR0") = 3) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 12) And (Grp = 1)) Then
            If (r.v("BNK0_GPR1") = 2) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR1") = 3) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 12) And (Grp = 2)) Then
            If (r.v("BNK0_GPR2") = 2) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR2") = 3) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 12) And (Grp = 3)) Then
            If (r.v("BNK0_GPR3") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR3") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 12) And (Grp = 4)) Then
            If (r.v("BNK2_GPR0") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR0") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 12) And (Grp = 5)) Then
            If (r.v("BNK2_GPR1") = 2) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR1") = 3) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 12) And (Grp = 6)) Then
            If (r.v("BNK2_GPR2") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR2") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 12) And (Grp = 7)) Then
            If (r.v("BNK2_GPR3") = 2) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR3") = 3) Then
               GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        '--------------------------------- Level 12 Part Over -------------------------------------------------------------------------
        
        
        
        '--------------------------------- Level 13 Part Starts -------------------------------------------------------------------------
       
         If ((Lvl = 13) And (Grp = 0)) Then
            If (r.v("BNK0_GPR4") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR4") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 13) And (Grp = 1)) Then
            If (r.v("BNK0_GPR5") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR5") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 13) And (Grp = 2)) Then
            If (r.v("BNK0_GPR6") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR6") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 13) And (Grp = 3)) Then
            If (r.v("BNK0_GPR7") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK0_GPR7") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 13) And (Grp = 4)) Then
            If (r.v("BNK2_GPR4") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR4") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 13) And (Grp = 5)) Then
            If (r.v("BNK2_GPR5") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR5") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 13) And (Grp = 6)) Then
            If (r.v("BNK2_GPR6") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR6") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 13) And (Grp = 7)) Then
            If (r.v("BNK2_GPR7") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK2_GPR7") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        '--------------------------------- Level 13 Part Over -------------------------------------------------------------------------
        
        
        
        
         '--------------------------------- Level 14 Part Starts -------------------------------------------------------------------------
       
         If ((Lvl = 14) And (Grp = 0)) Then
            If (r.v("BNK1_GPR0") = 2) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR0") = 3) Then
                 GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 14) And (Grp = 1)) Then
            If (r.v("BNK1_GPR1") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR1") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 14) And (Grp = 2)) Then
            If (r.v("BNK1_GPR2") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR2") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 14) And (Grp = 3)) Then
            If (r.v("BNK1_GPR3") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR3") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 14) And (Grp = 4)) Then
            If (r.v("BNK3_GPR0") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR0") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 14) And (Grp = 5)) Then
            If (r.v("BNK3_GPR1") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR1") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 14) And (Grp = 6)) Then
            If (r.v("BNK3_GPR2") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR2") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 14) And (Grp = 7)) Then
            If (r.v("BNK3_GPR3") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR3") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        '--------------------------------- Level 14 Part Over -------------------------------------------------------------------------
        
        
        
        
         '--------------------------------- Level 15 Part Starts -------------------------------------------------------------------------
       
         If ((Lvl = 15) And (Grp = 0)) Then
            If (r.v("BNK1_GPR4") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR4") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 15) And (Grp = 1)) Then
            If (r.v("BNK1_GPR5") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR5") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 15) And (Grp = 2)) Then
            If (r.v("BNK1_GPR6") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR6") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 15) And (Grp = 3)) Then
            If (r.v("BNK1_GPR7") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK1_GPR7") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 15) And (Grp = 4)) Then
            If (r.v("BNK3_GPR4") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR4") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 15) And (Grp = 5)) Then
            If (r.v("BNK3_GPR5") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR5") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        
        If ((Lvl = 15) And (Grp = 6)) Then
            If (r.v("BNK3_GPR6") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR6") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        
        If ((Lvl = 15) And (Grp = 7)) Then
            If (r.v("BNK3_GPR7") = 2) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 1, 1)
            ElseIf (r.v("BNK3_GPR7") = 3) Then
                GenerateIsrDeclaration = GenerateIsrDeclOnBnkSel(r, ISRName, VectorName, 2, 2)
            End If
         End If
        '--------------------------------- Level 15 Part Over -------------------------------------------------------------------------
                                      
        '-----------------------------------------For Fast Interrupt Part---------------------------------------------------------------
               
        If (r.v("FINT0CSP_EN") = 1) And (((r.v("FINT0CSP_ILVL") + 12) = Lvl) And ((r.v("FINT0CSP_GLVL") + (r.v("FINT0CSP_GPX") * 4)) = Grp)) Then
           If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
              GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName & "FAST = CACHED"
           ElseIf (InStr(r.v("MAIN_COMPILER"), "Tasking Classic")) Then
              GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & " _cached" & _
                                            " void " & ISRName & "(void)"
           ElseIf (InStr(r.v("MAIN_COMPILER"), "Tasking Viper")) Then
'              GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & " __frame(MDL, MDH)" & _
'                                            " void " & ISRName & "(void)"
               GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & _
                                        " void " & ISRName & "(void)"
           End If
           
        ElseIf (r.v("FINT1CSP_EN") = 1) And (((r.v("FINT1CSP_ILVL") + 12) = Lvl) And ((r.v("FINT1CSP_GLVL") + (r.v("FINT1CSP_GPX") * 4)) = Grp)) Then
           If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
              GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName & "FAST = CACHED"
           ElseIf (InStr(r.v("MAIN_COMPILER"), "Tasking Classic")) Then
              GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & " _cached" & _
                                            " void " & ISRName & "(void)"
           ElseIf (InStr(r.v("MAIN_COMPILER"), "Tasking Viper")) Then
'              GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & " __frame(MDL, MDH)" & _
'                                            " void " & ISRName & "(void)"
               GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & _
                                        " void " & ISRName & "(void)"
           End If
        End If
        
  
        '-----------------------------------------Fast Interrupt Part Ends here---------------------------------------------------------------
      
        '-----------------------------------------If Lvl is >= 12 but it is not configured for fast or bank selection-------------------------
      
         If (GenerateIsrDeclaration = "") Then

            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
               If (InStr(VectorName, "TRAP")) Then
                  GenerateIsrDeclaration = "interrupt(" & VectorName & ") void " & ISRName & "(void)"
               Else
                  GenerateIsrDeclaration = "_interrupt(" & VectorName & ")" & _
                  "  void " & ISRName & "(void)"
               End If
            ElseIf (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
               If (InStr(VectorName, "TRAP")) Then
                  GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
               Else
                  GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
               End If
            End If

         End If
       '-------------------------------------------------------------- Ends here---------------------------------------------------------------
      End If
   
End Function

'*****************************************************************************
'Description:
'        This function generates the declaration for the interrupt service
'        routine in the generated code depending on bank selection.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ISRName          the name of the service request routine (e.g SSC0_viTx)
'        VectorName       the name of the service request routine vector (e.g TX_INT)
'        BnkNr            Bank selection number
'        FstNr            Fast interrupt number
'-----------------------------------------------------------------------------
'Returns:
'        Service request declaration depending upon bank selection
'*****************************************************************************
 Function GenerateIsrDeclOnBnkSel(r As ProjectData, ISRName As String, VectorName As Variant, Optional BnkNr As Integer, Optional FstNr As Integer) As String
    On Error Resume Next
   
    Dim sISRName
        
    If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
           sISRName = "void " & ISRName & "(void) interrupt " & VectorName & " using _FAST_ABANK" & FstNr & "_"
    ElseIf (InStr(r.v("MAIN_COMPILER"), "Tasking Classic")) Then
           sISRName = "_interrupt(" & VectorName & ")" & " _localbank(-" & BnkNr & ")" & _
                                            " void " & ISRName & "(void)"
    ElseIf (InStr(r.v("MAIN_COMPILER"), "Tasking Viper")) Then
           sISRName = "_interrupt(" & VectorName & ")" & " __registerbank(-" & BnkNr & ")" & _
                                            " void " & ISRName & "(void)"
    End If
    
   GenerateIsrDeclOnBnkSel = sISRName
   
End Function

'*****************************************************************************
'Description:
'        This function generates the extern declaration for the interrupt
'        service routine in the generated code if it is configured for
'        fast interrupt
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ISRName          the name of the service request routine (e.g SSC0_viTx)
'        VectorName       the name of the service request routine vector (e.g TX_INT)
'        Lvl              priority level of an interrupt
'        Grp              priority group of an interrupt
'-----------------------------------------------------------------------------
'Returns:
'        Service request declaration depending upon bank selection
'*****************************************************************************
 Function GenerateExternDeclrn(r As ProjectData, ISRName As String, VectorName As Variant, Lvl As Integer, Grp As Integer) As String
    On Error Resume Next
   
    Dim sISRName As String
   
    If (r.v("FINT0CSP_EN") = 1) And (((r.v("FINT0CSP_ILVL") + 12) = Lvl) And ((r.v("FINT0CSP_GLVL") + (r.v("FINT0CSP_GPX") * 4)) = Grp)) Then
       If (InStr(r.v("MAIN_COMPILER"), "Tasking Classic")) Then
          sISRName = "extern _interrupt(" & VectorName & ")" & " _cached" & _
                     " void " & ISRName & "(void);"
       ElseIf (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
         sISRName = "extern " & "void " & ISRName & "(void);"
       End If
    End If
      
    If (r.v("FINT1CSP_EN") = 1) And (((r.v("FINT1CSP_ILVL") + 12) = Lvl) And ((r.v("FINT1CSP_GLVL") + (r.v("FINT1CSP_GPX") * 4)) = Grp)) Then
       If (InStr(r.v("MAIN_COMPILER"), "Tasking Classic")) Then
          sISRName = "extern _interrupt(" & VectorName & ")" & " _cached" & _
                     " void " & ISRName & "(void);"
       ElseIf (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
         sISRName = "extern " & "void " & ISRName & "(void);"
       End If
    End If
    
   GenerateExternDeclrn = sISRName
   
End Function

'*****************************************************************************
'Description:
'        This function generates the fast interrupt control register
'        assignments for the interrupt service routine in the generated
'        code if it is configured for
'        fast interrupt
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ISRName          the name of the service request routine (e.g SSC0_viTx)
'        VectorName       the name of the service request routine vector (e.g TX_INT)
'        Lvl              priority level of an interrupt
'        Grp              priority group of an interrupt
'-----------------------------------------------------------------------------
'Returns:
'        Service request declaration depending upon bank selection
'*****************************************************************************
 Function GenerateFastIntRegAssgn(r As ProjectData, ISRName As String, Lvl As Integer, Grp As Integer) As String
    On Error Resume Next
    
    Dim sISRName As String
    
    Dim v As Variant
     If (r.v("FINT0CSP_EN") = 1) And (((r.v("FINT0CSP_ILVL") + 12) = Lvl) And ((r.v("FINT0CSP_GLVL") + (r.v("FINT0CSP_GPX") * 4)) = Grp)) Then
       v = r.sfr("FINT0CSP")
       If (InStr(r.v("MAIN_COMPILER"), "Tasking Classic")) Then
          sISRName = "  FINT0CSP = " & FormatTextOutput(v, "H4") & " | " & "(((unsigned long)&(" & ISRName & "))>>16);" & "    //Fast Interrupt Control Reg. 0" & _
                       vbCrLf & "  FINT0ADDR =" & " (unsigned int)&(" & ISRName & ");" & "                     //Fast Interrupt Address Reg. 0"
       ElseIf (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
         sISRName = "  FINT0CSP = SEG(" & ISRName & ") | " & FormatTextOutput(v, "H4") & ";" & "     //Fast Interrupt Control Reg. 0" & _
                     vbCrLf & "  FINT0ADDR = SOF(" & ISRName & ");" & "             //Fast Interrupt Address Reg. 0"
       End If
    End If
      
    If (r.v("FINT1CSP_EN") = 1) And (((r.v("FINT1CSP_ILVL") + 12) = Lvl) And ((r.v("FINT1CSP_GLVL") + (r.v("FINT1CSP_GPX") * 4)) = Grp)) Then
       v = r.sfr("FINT1CSP")
       If (InStr(r.v("MAIN_COMPILER"), "Tasking Classic")) Then
          sISRName = "  FINT1CSP = " & FormatTextOutput(v, "H4") & " | " & "(((unsigned long)&(" & ISRName & "))>>16);" & "    //Fast Interrupt Control Reg. 1" & _
                       vbCrLf & "  FINT1ADDR =" & " (unsigned int)&(" & ISRName & ");" & "                     //Fast Interrupt Address Reg. 1"
       ElseIf (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
         sISRName = "  FINT1CSP = SEG(" & ISRName & ") | " & FormatTextOutput(v, "H4") & ";" & "     //Fast Interrupt Control Reg. 1" & _
                     vbCrLf & "  FINT1ADDR = SOF(" & ISRName & ");" & "             //Fast Interrupt Address Reg. 1"
       End If
    End If
    GenerateFastIntRegAssgn = sISRName

End Function

'*****************************************************************************
'Description:
'        This function converts a time in usec to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the time in usec
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the time
'        .sUnit           the new unit of the time
'        .bDivider        the new divider of the new time, referring to usec
'*****************************************************************************
Function ConvertTime(dValue As Double) As UnitType
  On Error GoTo ConvertTimeError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "usec"
  dDivider = 1

  If (dHelp < 1) Then
    dHelp = dHelp * 1000
    sHelp = "ns"
    dDivider = dDivider / 1000
  End If
 
  If (dHelp >= 1000) Then
    dHelp = dHelp / 1000
    sHelp = "ms"
    dDivider = 1000
    If (dHelp >= 1000) Then
      dHelp = dHelp / 1000
      sHelp = "s"
      dDivider = dDivider * 1000
      If (dHelp >= 60) Then
        dHelp = dHelp / 60
        sHelp = "min"
        dDivider = dDivider * 60
        If (dHelp >= 60) Then
          dHelp = dHelp / 60
          sHelp = "h"
          dDivider = dDivider * 60
          If (dHelp >= 24) Then
            dHelp = dHelp / 24
            sHelp = "days"
            dDivider = dDivider * 24
            If (dHelp >= 30) Then
              dHelp = dHelp / 30
              sHelp = "months"
              dDivider = dDivider * 30
              If (dHelp >= 12) Then
                dHelp = dHelp / 12
                sHelp = "years"
                dDivider = dDivider * 12
              End If
            End If
          End If
        End If
      End If
    End If
  End If

  ConvertTime.dValue = dHelp
  ConvertTime.sUnit = sHelp
  ConvertTime.dDivider = dDivider
     

  Exit Function

ConvertTimeError:
  ConvertTime.dValue = dValue
  ConvertTime.sUnit = "usec"
  ConvertTime.dDivider = 1
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function converts a frequency in MHz to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the time in MHz
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the frequency
'        .sUnit           the new unit of the frequency
'        .bDivider        the new divider of the new time, referring to MHz
'*****************************************************************************
Function ConvertFrequency(dValue As Double) As UnitType
  On Error GoTo ConvertFrequencyError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "MHz"
  dDivider = 1

  If (dHelp < 1) Then
    dHelp = dHelp * 1000
    sHelp = "kHz"
    dDivider = dDivider / 1000
  
    If (dHelp < 1) Then
      dHelp = dHelp * 1000
      sHelp = "Hz"
      dDivider = dDivider / 1000
    End If
  
  End If
 

  ConvertFrequency.dValue = dHelp
  ConvertFrequency.sUnit = sHelp
  ConvertFrequency.dDivider = dDivider
     

  Exit Function

ConvertFrequencyError:
  ConvertFrequency.dValue = dValue
  ConvertFrequency.sUnit = "MHz"
  ConvertFrequency.dDivider = 1
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function converts a baud rate in [baud] to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the baudrate in [baud]
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the baud rate
'        .sUnit           the new unit of the baud rate
'        .bDivider        the new divider of the new baud rate, referring to [baud]
'*****************************************************************************
Function ConvertBaudrate(dValue As Double) As UnitType
  On Error GoTo ConvertBaudrateError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "baud"
  dDivider = 1

  If (dHelp >= 1000) Then
    dHelp = (dHelp / 1000)
    sHelp = "kbaud"
    dDivider = dDivider * 1000
    
    If (dHelp >= 1000) Then
      dHelp = (dHelp / 1000)
      sHelp = "Mbaud"
      dDivider = dDivider * 1000
    End If
  
  End If

  ConvertBaudrate.dValue = dHelp
  ConvertBaudrate.sUnit = sHelp
  ConvertBaudrate.dDivider = dDivider

  Exit Function

ConvertBaudrateError:
  ConvertBaudrate.dValue = dValue
  ConvertBaudrate.sUnit = "baud"
  ConvertBaudrate.dDivider = 1
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function makes all update activities for the page Functions.
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the functions)
'        ps              reference to the Peripherals object
'        func            reference to the FunctionClass object
'        Controlname     control name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateFunction(sIndex As String, ps As Peripherals, func As FunctionClass, Controlname As String, v() As Variant) As Boolean
  On Error GoTo UpdateFunctionError
  UpdateFunction = False

  Dim r As ProjectData
  Dim sName As String
  Dim d As String
  Dim l As Long

  Set r = ps.ProjectData

  ' for each function in this FunctionClass object
  For l = 1 To func.FunctionCount
    ' get the function name which was inserted in 'Include.inc' with 'AddFunction()'
    d = func.FunctionName(l)
    ' get only the third part of the function name (e.g "Init")
    sName = Right(d, Len(d) - InStr(d, "NAME") - Len("NAME"))
    '---------------------------------------------------------------------
    ' control: chk_.. (action)
    '---------------------------------------------------------------------
    If (Controlname = "chk_" & sName) Then
      ' action on clicking the selected control
      r.v(sIndex & "_USE_" & sName) = v(1)
      Exit For
    End If

    '---------------------------------------------------------------------
    ' control: txt_.. (action)
    '---------------------------------------------------------------------
    If (Controlname = "txt_" & sName) Then
      ' action on clicking the selected control
      If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, , , )) Then
        r.s(d) = v(1)
      Else
        Exit Function
      End If
      Exit For
    End If
  Next l


  '---------------------------------------------------------------------
  ' control: txt_CFile (action)
  '---------------------------------------------------------------------
  If (Controlname = "txt_CFile") Then
    ' action on clicking the selected control
    If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, , , )) Then
      r.v(sIndex & "_CFile") = v(1)
    Else
      Exit Function
    End If
  End If


  UpdateFunction = True
  Exit Function

UpdateFunctionError:
  errorbox "Can not show page", Err.Description
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function makes all show activities for the page Functions
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the functions)
'        ps              reference to the Peripherals object
'        func            reference to the FunctionClass object
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ShowFunction(sIndex As String, ps As Peripherals, func As FunctionClass, v() As Variant) As Boolean
  On Error GoTo ShowFunctionError
  ShowFunction = False
    
  Dim r         As ProjectData
  Dim c         As Collection
  Dim e         As Collection
  Dim bDisabled As Boolean
  Dim sName     As String
  Dim d         As String
  Dim l         As Long

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  ' for each function in this 'func' object
  For l = 1 To func.FunctionCount
    ' get the function name which was insert in 'Include.inc' with 'AddFunction()'
    d = func.FunctionName(l)

    ' get only the third part of the function name (e.g "Init")
    sName = Right(d, Len(d) - InStr(d, "NAME") - Len("NAME"))

    '-------------------------------------------------------------------------
    ' control: chk_.. (show)
    '-------------------------------------------------------------------------
    ' action on showing the control
    With c("chk_" & sName)
      ' disable condition
      bDisabled = func.Disabled(d)
      .Enabled = Not (bDisabled)
      ' show condition
      If (sName = "Init") Then
        .Value = -CInt(r.v(sIndex & "_USE_" & sName) = 1)
      Else
        If (sName = "vSyncStartTmr") Then
          .Value = -CInt(func.Value(d))
          
        ElseIf (func.FunctionSort(d) = 2) Then ' if ISR
          .Value = -CInt(func.Value(d))
        Else
          .Value = -CInt(r.v(sIndex & "_USE_" & sName) = 1 And r.v(sIndex & "_USE_Init") = 1 And func.Value(d))
        End If
      End If
    End With
    
    With e("chk_" & sName)
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = func.DisabledTooltip(d)
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: txt_... (show)
    '-------------------------------------------------------------------------
    ' action on showing the control
    With c("txt_" & sName)
      ' disable condition

      If (sName = "vSyncStartTmr") Then
           bDisabled = 1 '(r.v("CCU60_USE_" & sName) = 0 Or r.v(sIndex & "_USE_Init") = 0 Or Not (func.Value(d)))
           
      ElseIf (func.FunctionSort(d) = 2) Then ' if ISR
          bDisabled = (r.v(sIndex & "_USE_" & sName) = 0 Or r.v(sIndex & "_USE_Init") = 0 Or Not (func.Value(d)))
      Else
          bDisabled = (r.v(sIndex & "_USE_" & sName) = 0 Or r.v(sIndex & "_USE_Init") = 0)
      End If
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.s(d), "S")
    End With

    With e("txt_" & sName)
      If (bDisabled) Then
        ' tooltip if control is disabled
        If (sName = "vSyncStartTmr") Then
            .DisabledTooltipText = FormatFunctionToolTip(r, func, d)
        Else
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_NAME) & vbCrLf & vbCrLf & FormatFunctionToolTip(r, func, d)
        End If
      Else
        ' tooltip if control is enabled
        .EnabledTooltipText = FormatFunctionToolTip(r, func, d)
      End If
    End With
  Next l

  '-------------------------------------------------------------------------
  ' control: lbl_CFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("lbl_CFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
  End With

  With e("lbl_CFile")
    If (bDisabled) Then
      ' tooltip if control is disabled
      .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_CFILE_NAME)
    Else
      ' tooltip if control is enabled
    End If
  End With

  '-------------------------------------------------------------------------
  ' control: txt_CFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("txt_CFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
    ' show condition
    .Text = FormatTextOutput(r.v(sIndex & "_CFile"), "S")
  End With

  With e("txt_CFile")
    If (bDisabled) Then
      ' tooltip if control is disabled
      .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_CFILE_NAME)
    Else
      ' tooltip if control is enabled
    End If
  End With

  ShowFunction = True
  Exit Function

ShowFunctionError:
  errorbox "Can not show page", Err.Description
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function makes all update activities for the page Parameters.
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the parameters)
'        ps              reference to the Peripherals object
'        para            reference to the ParameterClass object
'        Controlname     control name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateParameter(sIndex As String, ps As Peripherals, para As ParameterClass, Controlname As String, v() As Variant) As Boolean
  On Error GoTo UpdateParameterError
  UpdateParameter = False

  Dim r As ProjectData
  Dim sName As String
  Dim d As String
  Dim l As Long

  Set r = ps.ProjectData

  ' for each parameter in this ParameterClass object
  For l = 1 To para.ParameterCount
    ' get the parameter name which was insert in 'Include.inc' with 'AddParameter()'
    d = para.ParameterName(l)
    ' get only the third part of the parameter name (e.g "T2")
    sName = Right(d, Len(d) - InStr(d, "PARA") - Len("PARA"))

    '---------------------------------------------------------------------
    ' control: txt_.. (action)
    '---------------------------------------------------------------------
    If (Controlname = "txt_" & sName) Then
      ' action on clicking the selected control
      If (IsInputFormatOK(v(1), FORMAT_PARAMETERS, , , )) Then
        r.s(d) = v(1)
      Else
        Exit Function
      End If
      Exit For
    End If
  Next l

  '---------------------------------------------------------------------
  ' control: txt_HFile (action)
  '---------------------------------------------------------------------
  If (Controlname = "txt_HFile") Then
    ' action on clicking the selected control
    If (IsInputFormatOK(v(1), FORMAT_PARAMETERS, , , )) Then
      r.v(sIndex & "_HFile") = v(1)
    Else
      Exit Function
    End If
  End If

  UpdateParameter = True
  Exit Function

UpdateParameterError:
  errorbox "Can not show page", Err.Description
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This parameter makes all show activities for the page Parameters
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the parameters)
'        ps              reference to the Peripherals object
'        para            reference to the ParameterClass object
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ShowParameter(sIndex As String, ps As Peripherals, para As ParameterClass, v() As Variant) As Boolean
  On Error GoTo ShowParameterError
  ShowParameter = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim sName As String
  Dim d As String
  Dim l As Long
  Dim bPARAchk As Boolean

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  ' for each parameter in this 'para' object
  For l = 1 To para.ParameterCount
    ' get the parameter name which was insert in 'Include.inc' with 'AddParameter()'
    d = para.ParameterName(l)
    
    ' get only the third part of the parameter name (e.g "T2")
    sName = Right(d, Len(d) - InStr(d, "PARA") - Len("PARA"))
    
    bPARAchk = True
    If ((Not ModuleCheck("CAN")) And sName = "CAN") Or _
       ((Not ModuleCheck("CCU6")) And sName = "CC6") Or _
       ((Not ModuleCheck("ADC")) And sName = "ADC") Then
    bPARAchk = False
    End If
    '-------------------------------------------------------------------------
    ' control: txt_... (show)
    '-------------------------------------------------------------------------
    ' action on showing the control
    If bPARAchk Then
    With c("txt_" & sName)
      ' disable condition
      bDisabled = (r.v(sIndex & "_USE_Init") = 0 Or para.Disabled(d))
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.s(d), "S")
    End With

    With e("txt_" & sName)
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_PARAMETER
        If (para.Disabled(d) = True) Then
          .DisabledTooltipText = .DisabledTooltipText & para.DisabledTooltip(d)
        End If
        If (r.v(sIndex & "_USE_Init") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT) & vbCrLf & vbCrLf & _
            FormatParameterToolTip(r, para, d)
        End If
      Else
        ' tooltip if control is enabled
        .EnabledTooltipText = FormatParameterToolTip(r, para, d)
      End If
    End With
    End If  'MO ModuleCheck
  Next l

  '-------------------------------------------------------------------------
  ' control: lbl_HFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("lbl_HFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
  End With

  With e("lbl_HFile")
    If (bDisabled) Then
      ' tooltip if control is disabled
      .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_HFILE_NAME)
    Else
      ' tooltip if control is enabled
    End If
  End With

  '-------------------------------------------------------------------------
  ' control: txt_HFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("txt_HFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
    ' show condition
    .Text = FormatTextOutput(r.v(sIndex & "_HFile"), "S")
  End With

  With e("txt_HFile")
    If (bDisabled) Then
      ' tooltip if control is disabled
      .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_HFILE_NAME)
    Else
      ' tooltip if control is enabled
    End If
  End With


  ShowParameter = True
  Exit Function

ShowParameterError:
  errorbox "Can not show page", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'        This function checks the paramter v if he is lower as the parameter
'        MinValue or greater as the parameter MaxValue. If so then a massage
'        box is opened.
'-----------------------------------------------------------------------------
'Parameters:
'        v               value which should be checked
'        MinValue        minimum value for the parameter v
'        MaxValue        maximum value for the parameter v
'-----------------------------------------------------------------------------
'Returns:
'        True            the value v is in range
'        False           else
'*****************************************************************************
Function IsInputValueOK(v As Variant, MinValue As Double, MaxValue As Double) As Boolean
  On Error Resume Next

  IsInputValueOK = False

  If (v < MinValue) Then  ' if the value is too low
    msgbox ("The input value must be greater.", vbCritical ,"DAvE")
    Exit Function
  Else
    If (v > MaxValue) Then   ' if the value is too high
      msgbox ("The input value must be lower.", vbCritical ,"DAvE")
      Exit Function
    Else
      IsInputValueOK = True  ' the value is OK
    End If
  End If

End Function


Function SetDefaultPropertiesForAllControlsOnPage(szPagename As String, c As Object, e As Object, bDisabled As Boolean, szDisabledTooltip As String, szEnabledTooltip As String)
  On Error Resume Next

  Dim o As Object
  Dim s As String

  ' for each control
  For Each o In c
    ' on this page
    s = ""
    s = o.Container.Container.Tag
    If (s = szPagename) Then
      o.Enabled = Not bDisabled
      ' determine tooltip
      If (bDisabled) Then
        ' tooltip if control is disabled
        e(o.Tag).DisabledTooltipText = szDisabledTooltip
      Else
        ' tooltip if control is enabled
        e(o.Tag).EnabledTooltipText = szEnabledTooltip
      End If
    End If
  Next
End Function

Function SetDefaultPropertiesForAllControlsOnFrame(szFramename As String, c As Object, e As Object, bDisabled As Boolean, szDisabledTooltip As String, szEnabledTooltip As String)
  On Error Resume Next

  Dim o As Object
  Dim s As String

  ' for each control
  For Each o In c
    ' on this frame
    s = ""
    s = o.Container.Caption
    If (s = szFramename) Then
      o.Enabled = Not bDisabled
      ' determine tooltip
      If (bDisabled) Then
        ' tooltip if control is disabled
        e(o.Tag).DisabledTooltipText = szDisabledTooltip
      Else
        ' tooltip if control is enabled
        e(o.Tag).EnabledTooltipText = szEnabledTooltip
      End If
    End If
  Next
End Function

'*****************************************************************************
'Description:
'         This function sets the PEC channel in to the default.
'         If the channel is linked to or from the other channel and
'         the channel has no interrupt, then the pair channel is set to
'         the default too.
'-----------------------------------------------------------------------------
'Parameters:
'        r               ProjectData
'        iChannel        PEC channel
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub SetPECChannelToDefault(r As ProjectData, iChannel As Integer)
    On Error Resume Next

    Dim i As Integer
    Dim cp As DAvECollection
    Dim ir As InterruptClass
    Dim iFirstLevel As Integer
    Dim iGroup As Integer
    Dim asISR(0 To 3) As String

    Set cp = ObjectCollection("IRSOURCES")
  
    If (iChannel < 4) Then
        iFirstLevel = 14
        iGroup = iChannel
    ElseIf (iChannel > 3) Then
        iFirstLevel = 15
        iGroup = iChannel - 4
    End If

    For i = 0 To 3
        asISR(i) = "none"
    Next i
    
    For Each ir In cp
        If (r.v(ir.Enabled) = 1) Then
            If (r.v(ir.GroupName) = iGroup And r.v(ir.GPXName) = 0) Then
                If (r.v(ir.LevelName) = iFirstLevel) Then
                    asISR(0) = ir.IntName
                End If
                If (r.v(ir.LevelName) = iFirstLevel - 2) Then
                    asISR(1) = ir.IntName
                End If
                If (r.v(ir.LevelName) = iFirstLevel - 4) Then
                    asISR(2) = ir.IntName
                End If
                If (r.v(ir.LevelName) = iFirstLevel - 6) Then
                    asISR(3) = ir.IntName
                End If
            End If
          
        End If
    Next ir
    
    For i = 0 To 3
        If (asISR(i) = "none" And r.v("PECC" & iChannel & "_PLEV") = i) Then
            
            'set PEC to default
            r.v("PECC" & iChannel & "_COUNT") = 0
            r.v("PEC_SRCP" & iChannel & "_SRCP" & iChannel) = 0
            r.v("PEC_DSTP" & iChannel & "_DSTP" & iChannel) = 0
            r.v("PECC" & iChannel & "_INC") = 0
            r.v("PECC" & iChannel & "_EOPINT") = 0
            r.v("PECISNC_C" & iChannel & "IE") = 0
            r.v("PEC_CHANNEL_" & iChannel) = ""
            r.v("SRCPTR_UD" & iChannel) = 0
            r.v("DSTPTR_UD" & iChannel) = 0

        End If
        r.v("PEC_CHANNEL_" & iChannel & "_" & i) = asISR(i)
    Next i

    

End Sub

'*****************************************************************************
'Description:
'        The "UpdateProjectData" functions of all peripheral modules are
'        called with parameter "lngUpdateDescriptor" being passed down.
'
'        Restrictions:
'        - The first module in collection "MODULENAMES" is the main module
'          (which has no "UpdateProjectData" function implemented)
'        - All other modules in collection "MODULENAMES" contain a file
'          "Ini.dave" with a "UpdateProjectData" function
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data
'
'        lngUpdateDescriptor    identification of the update context (input)
'                               - INITIALIZATION for inizialization of the module
'                               - UPDATE_CLOCK for clock update
'                               - SET_MODULE_TO_DEFAULT for setting all modules to
'                                 the default
'
'        bAsk (Optional)        if Ask must be started (true or false)
'
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateAllPeripherals(r As ProjectData, lngUpdateDescriptor As Long, Optional bAsk As Variant) As Boolean
  On Error GoTo UpdateAllPeripheralsError

  Dim lngIndex    As Long
  Dim cp          As DAvECollection
  Dim sc          As Script
  Dim sModuleName As String
  Dim sAsk       As String

  UpdateAllPeripherals = False
  Set cp = ObjectCollection("MODULENAMES")


  If (lngUpdateDescriptor = UPDATE_CLOCK) Then
      sAsk = "Any of your settings which are dependent on the system clock" & vbCrLf & _
              "will be adjusted without further notification." & vbCrLf & _
              "Do you really want to continue ?"

  ElseIf (lngUpdateDescriptor = SET_MODULE_TO_DEFAULT) Then
      sAsk = "All modules are set to default. Do you really want to continue ?"
  ElseIf (lngUpdateDescriptor = INITIALIZATION) Then
      sAsk = "All module are neu initialization. Do you really want to continue ?"
  Else
      sAsk = ""
  End If

  If IsMissing(bAsk) Then
     'if no ask
      For lngIndex = 2 To cp.Count  ' skip the first module "main"
          sModuleName = cp(lngIndex).ModuleName
          If (Not UpdateOnePeripherals(r, sModuleName, lngUpdateDescriptor)) Then Exit Function
      Next lngIndex
  
  Else
      If (Ask(sAsk)) Then
          For lngIndex = 2 To cp.Count  ' skip the first module "main"
              sModuleName = cp(lngIndex).ModuleName
              If (Not UpdateOnePeripherals(r, sModuleName, lngUpdateDescriptor)) Then Exit Function
          Next lngIndex
      Else
          Exit Function
      End If
  
  End If
  

  UpdateAllPeripherals = True
  Exit Function

UpdateAllPeripheralsError:
  errorbox "Initialize error", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'        The "UpdateProjectData" functions of all peripheral modules are
'        called with parameter "lngUpdateDescriptor" being passed down.
'
'        Restrictions:
'        - The first module in collection "MODULENAMES" is the main module
'          (which has no "UpdateProjectData" function implemented)
'        - All other modules in collection "MODULENAMES" contain a file
'          "Ini.dave" with a "UpdateProjectData" function
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data
'        sModuleName            name of the Module s. Install.inc (e.g. ASC0, CAN1, CC1)
'        lngUpdateDescriptor    identification of the update context (input)
'                               - INITIALIZATION for inizialization of the module
'                               - UPDATE_CLOCK for clock update
'                               - SET_MODULE_TO_DEFAULT for set the module to
'                                 the default
'
'        bAsk (Optional)        if Ask must be started (true or false)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateOnePeripherals(r As ProjectData, sModuleName As String, lngUpdateDescriptor As Long, Optional bAsk As Variant) As Boolean
  On Error GoTo UpdateOnePeripheralsError

  Dim lngIndex  As Long
  Dim cp        As DAvECollection
  Dim sc        As Script
  Dim sAsk      As String

  UpdateOnePeripherals = False
  Set cp = ObjectCollection("MODULENAMES")

  If (lngUpdateDescriptor = UPDATE_CLOCK) Then
      sAsk = "Any of your settings in " & sModuleName & " which are dependent on the system clock" & vbCrLf & _
              "will be adjusted without further notification." & vbCrLf & _
              "Do you really want to continue ?"
  ElseIf (lngUpdateDescriptor = SET_MODULE_TO_DEFAULT) Then
      sAsk = "The module " & sModuleName & " will be set to the default. Do you really want to continue ?"
  ElseIf (lngUpdateDescriptor = INITIALIZATION) Then
      sAsk = "The module " & sModuleName & " will be neu initialization. Do you really want to continue ?"
  Else
      sAsk = ""
  End If

  If IsMissing(bAsk) Then
      'if no ask
      Set sc = New Script
      sc.Load (DefGetPath("Scripts") & cp(sModuleName).ModuleDirectory & "\Ini.dave")
      If (Not sc.Functions.UpdateProjectData(r, cp(sModuleName).ParameterValue, lngUpdateDescriptor)) Then Exit Function
  Else
      If (Ask(sAsk)) Then
          Set sc = New Script
          sc.Load (DefGetPath("Scripts") & cp(sModuleName).ModuleDirectory & "\Ini.dave")
          If (Not sc.Functions.UpdateProjectData(r, cp(sModuleName).ParameterValue, lngUpdateDescriptor)) Then Exit Function
      Else
          Exit Function
      End If
  
  End If

  UpdateOnePeripherals = True
  Exit Function

UpdateOnePeripheralsError:
  errorbox "Initialize error", Err.Description
  Exit Function
End Function


'*****************************************************************************
' @bfunc   Public | GetModuleDetails |
'
'-----------------------------------------------------------------------------
'Description:
'             This functoin read the module details from CFG file.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        none
'*****************************************************************************

Public Sub GetModuleDetails()
Dim szFile As String
Dim i As Integer, iModCount As Integer, iState As Integer
Dim objExcelApp As Object
Dim Controller As String
Dim f
Dim szLine As String

    szFile = DefGetFile("CfgFile")
    f = FreeFile
    iModCount = 0
    Open szFile For Input As f
    iState = 0
    
    While (Not EOF(f))
        If iState = 3 Then
            iState = 1
        Else
            Line Input #f, szLine
            szLine = Trim(LCase(szLine))
        End If
        
        
        If szLine <> "" And Left(szLine, 1) <> "#" Then
        
        Select Case iState
            
            Case 0
                If LCase(szLine) = "<begin_module_details>" Then
                    iState = 1
                Else
                    errorbox "error Case 0"
                    Exit Sub
                End If
            
            Case 1
                If szLine = "<begin_module>" Then
                    iState = 2
                    
                    If Not EOF(f) Then
                        Line Input #f, szLine
                    Else
                        errorbox "Incomplete File"
                        End
                    End If
                    
                        If ((InStr(szLine, "ModuleName") > 0)) Then
                        szLine = Right(szLine, Len(szLine) - Len("ModuleName ="))
                     
                        ReDim Preserve ModuleDetails(iModCount + 1)
            
                        With ModuleDetails(iModCount)
                            .ModuleName = Trim(szLine)
                            
                        If Not EOF(f) Then
                            Line Input #f, szLine
                            szLine = Right(szLine, Len(szLine) - Len("Left ="))
                            .Left = Trim(szLine)
                        Else
                            errorbox "Incomplete left data in module - " & .ModuleName
                            End
                        End If
                        
                        If Not EOF(f) Then
                            Line Input #f, szLine
                            szLine = Right(szLine, Len(szLine) - Len("Top ="))
                            .Top = Trim(szLine)
                        Else
                            errorbox "Incomplete top data in module - " & .ModuleName
                            End
                        End If
                        
                        If Not EOF(f) Then
                            Line Input #f, szLine
                            szLine = Right(szLine, Len(szLine) - Len("Width ="))
                            .Width = Trim(szLine)
                        Else
                            errorbox "Incomplete width data in module - " & .ModuleName
                            End
                        End If
                        
                        If Not EOF(f) Then
                            Line Input #f, szLine
                            szLine = Right(szLine, Len(szLine) - Len("Height ="))
                            .Height = Trim(szLine)
                        Else
                            errorbox "Incomplete height data in module - " & .ModuleName
                            End
                        End If
                        
                        End With
                         
                         iModCount = iModCount + 1
                         
                         Line Input #f, szLine
                         
                         If Trim(LCase(szLine)) <> "<end_module>" Then
                            errorbox "Missing <end_module> in Module : " & ModuleDetails(iModCount).ModuleName
                            End
                         End If
                         
                        End If
                Else
                    errorbox "Error Case 1"
                    End
                End If
            
            Case 2
                If szLine = "<begin_module>" Then
                    iState = 3
                ElseIf szLine = "<end_module_details>" Then
                    iState = 4
                    Close f
                    Exit Sub
                Else
                    errorbox "Error case 2"
                    End
                End If
                
            
        End Select
        End If
        
    Wend

    Close f
    
    If iState <> 4 Then
        errorbox "Incomplete File. Missing <end_module_details>"
        End
    End If

    If Not CheckModValidity Then
        End
    End If
  
End Sub

'Start (P11 - Jai)
'*****************************************************************************
' @bfunc   Public | GetModuleDetails |
'
'-----------------------------------------------------------------------------
'Description:
'             This functoin read the module details from CFG file.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        none
'*****************************************************************************

Public Sub GetControllerDetails()
Dim szFile As String
Dim iState As Integer
Dim iCntrCount As Integer
Dim f
Dim szLine As String

    szFile = DefGetFile("CfgFile")
    f = FreeFile
    iCntrCount = 0
    
    Open szFile For Input As f
    
            While Not EOF(f)
                Line Input #f, szLine
                szLine = LCase(Trim(szLine))
                
                If (szLine = "<begin_controller_details>") Then
                    While (szLine <> "<end_controller_details>")
                        Line Input #f, szLine
                        szLine = LCase(Trim(szLine))
                        If (szLine <> "<end_controller_details>") And (szLine <> "") Then
                            ReDim Preserve CTypeDetails(iCntrCount + 1)

                            With CTypeDetails(iCntrCount)
                                .CntrlrType = UCase(Trim(szLine))
                            End With

                            iCntrCount = iCntrCount + 1
                        End If
                    Wend
                End If
            Wend

    Close f

End Sub
'End (P11 - Jai)

'*****************************************************************************
' @bfunc   Public | InstallManualLinks | Boolean |
'
'-----------------------------------------------------------------------------
'Description:
'             This functoin check the validity of the modules.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        Boolean
'*****************************************************************************
Public Function CheckModValidity() As Boolean
    Dim szModuleNames(26) As String
    Dim i As Integer, j As Integer
    Dim iModCount As Integer
    Dim bFlag As Boolean
    
    szModuleNames(0) = "IO"
    szModuleNames(1) = "USIC0"
    szModuleNames(2) = "USIC1"
    szModuleNames(3) = "USIC2"
    szModuleNames(4) = "U0C0"
    szModuleNames(5) = "U0C1"
    szModuleNames(6) = "U1C0"
    szModuleNames(7) = "U1C1"
    szModuleNames(8) = "U2C0"
    szModuleNames(9) = "U2C1"
    szModuleNames(10) = "GPT1"
    szModuleNames(11) = "GPT2"
    szModuleNames(12) = "CC2"
    szModuleNames(13) = "CCU60"
    szModuleNames(14) = "CCU61"
    szModuleNames(15) = "ADC Clock"
    szModuleNames(16) = "ADC0"
    szModuleNames(17) = "ADC1"
    szModuleNames(18) = "RTC"
    szModuleNames(19) = "CAN"
    szModuleNames(20) = "INT"

    iModCount = UBound(ModuleDetails)
    
    For i = 0 To iModCount - 1
        bFlag = False
        For j = 0 To 25
            If UCase(ModuleDetails(i).ModuleName) = szModuleNames(j) Then
                bFlag = True
                Exit For
            End If
        Next j
        If Not bFlag Then
            CheckModValidity = False
            MsgBox "Unknown module " & ModuleDetails(i).ModuleName, vbInformation
            Exit Function
        End If
    Next i
    
    CheckModValidity = True
    
End Function

'*****************************************************************************
' @bfunc   Public | GetModuleIndex | Integer |
'
'-----------------------------------------------------------------------------
'Description:
'             This functoin returns the index of a module.
'-----------------------------------------------------------------------------
'Parameters:
'        szModule   As Module Name
'-----------------------------------------------------------------------------
'Returns:
'        Integer
'*****************************************************************************

Public Function GetModuleIndex(szModule As String) As Integer

Dim i As Integer, iModCount As Integer

    iModCount = UBound(ModuleDetails)
     
    For i = 0 To iModCount - 1
        If UCase(ModuleDetails(i).ModuleName) = UCase(szModule) Then
            GetModuleIndex = i
            Exit For
        End If
    Next i

End Function

'*****************************************************************************
' @bfunc   Public | ModuleCheck | Boolean |
'-----------------------------------------------------------------------------
'Description:
'             This functoin checks the validity of a module.
'-----------------------------------------------------------------------------
'Parameters:
'       szModule   As Module Name
'-----------------------------------------------------------------------------
'Returns:
'        Boolean
'*****************************************************************************

Public Function ModuleCheck(szModule As String) As Boolean
On Error Resume Next
Dim i As Integer, iModCount As Integer
ModuleCheck = False

    iModCount = UBound(ModuleDetails)
    
    For i = 0 To iModCount - 1
        If (UCase(ModuleDetails(i).ModuleName) = UCase(szModule)) Then
        ModuleCheck = True
        Exit For
        End If
    Next i
'ModuleCheck = True
End Function

'*****************************************************************************
' @bfunc   Public | GetPortPinDetails |
'
'-----------------------------------------------------------------------------
'Description:
'             This function read the port pin details from CFG file.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        none
'*****************************************************************************

Public Sub GetPortpinDetails()
  
  Dim i As Integer
  Dim f As Variant
  Dim szFile As String, szLine As String
  
  Dim PortsAttributes As Variant
  Dim PinAttributes As Variant
  Dim PortName As String
  Dim PinNum As Variant
  Dim iPinCount As Integer
  Dim iPortCount As Integer
  
  Dim iAltCount As Integer
  
  iPortCount = 0
  
   

    szFile = DefGetFile("CfgFile")
    f = FreeFile
    Open szFile For Input As f
    Line Input #f, szLine
    szLine = LCase(Trim(szLine))

    While (InStr(szLine, LCase("begin_pin_details")) = 0)
        Line Input #f, szLine
        szLine = LCase(Trim(szLine))
    Wend

    If szLine = "<begin_pin_details>" Then
              
          While (szLine <> "<end_pin_details>")
                Line Input #f, szLine
                szLine = Trim(szLine)
              
                
          If (szLine = "<begin_port>") Then
          
             Line Input #f, szLine
             szLine = Trim(szLine)
             If ((InStr(szLine, "PortName") > 0)) Then
                  ReDim Preserve Ports(iPortCount + 1)
                  PortsAttributes = SplitLine(szLine, ",")
                  With Ports(iPortCount)
                        .PortName = Trim(Right(PortsAttributes(0), Len(PortsAttributes(0)) - Len("PortName =")))
                        .PortDescription = Trim(PortsAttributes(1))
                        .StartPin = Trim(PortsAttributes(2))
                        .LastPin = Trim(PortsAttributes(3))
                        .InputChar = Trim(PortsAttributes(4))
                        .EdgeChar = Trim(PortsAttributes(5))
                        .DriveStrength = Trim(PortsAttributes(6))
                   End With

             End If
             
             iPinCount = 0
             
             While (szLine <> "<end_port>")
                Line Input #f, szLine
                szLine = Trim(szLine)
                 
                If szLine = "<begin_pin>" Then
                   
                   ReDim Preserve PortDetails(iPortCount + 1)
                   ReDim Preserve PortDetails(iPortCount).PinDetails(iPinCount + 1)
                  
                   iAltCount = 0
                   
                   
                   While (szLine <> "<end_pin>")
                      
                      Line Input #f, szLine
                      szLine = Trim(szLine)
                             
                      If szLine <> "<end_pin>" Then

                         If ((InStr(szLine, "PinName") > 0)) Then
                             PortName = Right(szLine, Len(szLine) - Len("PinName ="))
                             PinNum = Mid(PortName, (InStr(PortName, ".") + 1), 2)
                             PortName = Mid(PortName, 1, (InStr(PortName, ".") - 1))

                             PortName = Trim(Replace(PortName, "P", ""))
                             PortName = Trim(Replace(PortName, ".", ""))

                             i = PinNum

                             With PortDetails(iPortCount).PinDetails(iPinCount)
                                  .PortName = PortName
                                  .PinNum = PinNum
                             End With
                            
                             'MsgBox "Hello " & PortDetails(iPortCount).PinDetails(iPinCount).PortName
                             'MsgBox "World " & PortDetails(iPortCount).PinDetails(iPinCount).PinNum
                         Else
                             PinAttributes = SplitLine(szLine, ",")
                                                                   
                             ReDim Preserve PortDetails(iPortCount).PinDetails(iPinCount).sAltName(iAltCount + 1)
                             ReDim Preserve PortDetails(iPortCount).PinDetails(iPinCount).sDescription(iAltCount + 1)
                             ReDim Preserve PortDetails(iPortCount).PinDetails(iPinCount).bExclusive(iAltCount + 1)
                             ReDim Preserve PortDetails(iPortCount).PinDetails(iPinCount).iFunction(iAltCount + 1)
                                  
                             With PortDetails(iPortCount).PinDetails(iPinCount)
                                  .sAltName(iAltCount) = PinAttributes(0)
                                  .sDescription(iAltCount) = PinAttributes(1)
                                  .bExclusive(iAltCount) = PinAttributes(2)
                             End With

                             If (UBound(PinAttributes) = 3) Then
                                 With PortDetails(iPortCount).PinDetails(iPinCount)
                                      .iFunction(iAltCount) = PinAttributes(3)
                                 End With
                             End If
                            
                             iAltCount = iAltCount + 1
                        
                         End If
                         
                      End If
                   Wend
                   
                   iPinCount = iPinCount + 1
                   
                End If
               
              Wend

              iPortCount = iPortCount + 1
            End If
             
            Wend
      End If
       
    Close f
End Sub
Public Function SplitLine(Expression As String, Optional Delimiter As String) As Variant

    Dim i As Long
    Dim lNextPos As Long
    Dim sText As String
    Dim lCount As Long
    Dim varTemp() As String
    For i = 1 To Len(Expression)

        lNextPos = InStr(i + 1, Expression, Delimiter)
        If lNextPos = 0 Then
            lNextPos = Len(Expression) + 1
        End If
        sText = Mid$(Expression, i, lNextPos - i)
        ReDim Preserve varTemp(lCount)
        varTemp(lCount) = sText
        lCount = lCount + 1
        i = lNextPos + Len(Delimiter) - 1

    Next
    SplitLine = varTemp

End Function
'*****************************************************************************
'Description:
'        This function checks the controller type and returns 1 if it is
'        144 pin controller else 2 for 100 pin controller
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               Project Data
'-----------------------------------------------------------------------------
'Returns:
'        1            Controller is of 144 pin type
'        2            Controller is of 100 pin type
'*****************************************************************************
Function IsControllerTypeOK(r As ProjectData) As Integer
  On Error Resume Next
  
  IsControllerTypeOK = 0
    
  If (InStr(r.v("MAIN_CPU_TYPE"), "XC2287") Or InStr(r.v("MAIN_CPU_TYPE"), "XC2387") Or InStr(r.v("MAIN_CPU_TYPE"), "XE167")) Then
      IsControllerTypeOK = 1
  ElseIf (InStr(r.v("MAIN_CPU_TYPE"), "XC2267") Or InStr(r.v("MAIN_CPU_TYPE"), "XC2365") Or InStr(r.v("MAIN_CPU_TYPE"), "XE164")) Then
      IsControllerTypeOK = 2
  End If
  
End Function
'*****************************************************************************
'Description:
'        This function checks if USIC reg is selected as PEC Source
'

'-----------------------------------------------------------------------------
'Returns:
'       Fales             if not seleected
'       True              else
'*****************************************************************************
Function IsPECUSICSOURCESelected(r As ProjectData, iHelp As Integer) As Boolean
On Error Resume Next
  IsPECUSICSOURCESelected = False
  
  If (r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "U0C0_RBUF" Or r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "U0C1_RBUF" Or _
      r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "U1C0_RBUF" Or r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "U1C1_RBUF" Or _
      r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "U2C0_RBUF" Or r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "U2C1_RBUF") Then
    
    IsPECUSICSOURCESelected = True
   
  Else
    IsPECUSICSOURCESelected = False
    
  End If
  
End Function

'*****************************************************************************
'Description:
'        This function checks if USIC reg is selected as PEC destination
'
'-----------------------------------------------------------------------------

'Returns:
'       Fales             if not seleected
'       True              else
'*****************************************************************************
Function IsPECUSICDESTSelected(r As ProjectData, iHelp As Integer) As Boolean
On Error Resume Next
  IsPECUSICDESTSelected = False
  
  If (r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "U0C0_TBUF00" Or r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "U0C1_TBUF00" Or _
      r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "U1C0_TBUF00" Or r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "U1C1_TBUF00" Or _
      r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "U2C0_TBUF00" Or r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "U2C1_TBUF00") Then
    
    IsPECUSICDESTSelected = True
  Else
    IsPECUSICDESTSelected = False
    
  End If
  
End Function


