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

Option Explicit
'*****************************************************************************
' Module:
'        UART
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'UART'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil          25-10-04    initial version
'        0.2  Sunil          18-11-04    code added to select BRG, when transmitter or
'                                        receiver option is selected.
'        0.3  Sunil          29-12-04    default baudrate is initialized to maximum value.
'                                        (default value of BG is 0x00).
'                                        module prefix added.
'
'*****************************************************************************

'*****************************************************************************
'Description:
'        This function is called from DAvE just before the Peripherals
'        window is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************


Function OnLoad(ps As Peripherals) As Boolean
    On Error GoTo OnLoadError

    OnLoad = False

    If (Not OnLoadPageUART(ps)) Then Exit Function
    If (Not OnLoadPageBRG(ps)) Then Exit Function
    If (Not OnLoadPageInterrupts(ps)) Then Exit Function
    If (Not OnLoadPageFunctions(ps)) Then Exit Function
    If (Not OnLoadPageParameters(ps)) Then Exit Function
    If (Not OnLoadPageNotes(ps)) Then Exit Function
    
    ' call function to initialize the menu
    If (Not InitMenu(ps, "UART\Dialog")) Then Exit Function

    OnLoad = True
    Exit Function

OnLoadError:
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function is called from DAvE just before a page
'        is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnChangingPage(ps As Peripherals, Page As Integer, Pagename As String) As Boolean
    On Error GoTo OnChangingPageError

    OnChangingPage = False
    
    If (Not OnChangingPageUART(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageBRG(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageInterrupts(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageFunctions(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageParameters(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageNotes(ps, Pagename, "UART")) Then Exit Function

    OnChangingPage = True
    Exit Function

OnChangingPageError:
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function is called from DAvE in two cases:
'
'        1.
'            The contents of the peripherals window must be refreshed.
'            In this case the 'Controlname' parameter is a empty string
'            and the values array 'v()' contains nothing.
'
'        2.
'            A control on the peripherals has been changed.
'            In this case the 'Controlname' parameter contains the name
'            of the changed control and the values array 'v()' contains the
'            changed value(s).
'
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the changed control (for case 2)
'        v()             array with the changed values (for case 2)
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, Page As Integer, Pagename As String) As Boolean
    On Error Resume Next

    OnShow = False

    If (Not OnShowPageUART(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageBRG(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageInterrupts(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageFunctions(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageParameters(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageNotes(ps, Controlname, Pagename, "UART", v)) Then Exit Function

    OnShow = True
End Function

'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a button.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the button
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnClick(ps As Peripherals, Controlname As String) As Boolean
    On Error GoTo OnClickError

    OnClick = False
    ' no buttons implemented
    OnClick = True
    Exit Function

OnClickError:
    errorbox "Can not process the click", Err.Description
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a menu entry.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Toolname        name of the entry
'        ToolId          id of the entry
'        Tooltext        additional text of the entry
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnMenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    OnMenuClick = False

    If (Not MenuClick(ps, Toolname, ToolId, Tooltext, "UART\Dialog")) Then Exit Function

    OnMenuClick = True
    Exit Function

OnMenuClickError:
    Exit Function
End Function
    
'-----------------------------------------------------------------------------
' local functions
'-----------------------------------------------------------------------------

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageUART(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageUART = False

  If (Not ps.AddPage("UART")) Then Exit Function

  OnLoadPageUART = True
End Function

Public Function OnChangingPageUART(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next
  Dim c As Collection
  OnChangingPageUART = False

  If (Not ps.ExtenderCollection("UART").Loaded) And (Pagename = "UART") Then
    ps.ExtenderCollection("UART").Loaded = True
    
    Set c = ps.ControlCollection
    '-------------------------------
    ' frame: fraTransmitter
    '-------------------------------
    If (Not ps.AddFrame("fraTransmitter", 80, 110, 2960, 1300, "Transmitter")) Then Exit Function
        If (Not ps.AddOption("OptNoPinSel", 200, 250, 1750, 380, "No pins are selected")) Then Exit Function
        If (Not ps.AddOption("OptSelP1.1", 200, 550, 1860, 380, "Use pin P1.1 (TXD_0)")) Then Exit Function
        If (Not ps.AddOption("OptSelP0.2", 200, 850, 1860, 380, "Use pin P0.2 (TXD_1)")) Then Exit Function

    '-------------------------------
    ' frame: fraReceiver
    '-------------------------------
    If (Not ps.AddFrame("fraReceiver", 3200, 110, 2400, 1300, "Receiver")) Then Exit Function
        If (Not ps.AddOption("optSelNone", 200, 250, 1750, 380, "No pins are selected")) Then Exit Function
        If (Not ps.AddOption("optSelP1.0", 200, 550, 1870, 380, "Use pin P1.0 (RXD_0)")) Then Exit Function
        If (Not ps.AddOption("optSelP0.1", 200, 850, 1870, 380, "Use pin P0.1 (RXD_1)")) Then Exit Function
        
    '-------------------------------
    ' frame: fraReceiverEnable
    '-------------------------------
    If (Not ps.AddFrame("fraReceiverEnable", 5760, 110, 2840, 1300, "Receiver Enable")) Then Exit Function
        If (Not ps.AddCheck("chkEnableReceiver", 200, 300, 1890, 380, "Enable receiver (REN)")) Then Exit Function
    
    '-------------------------------
    ' frame: fraMode
    '-------------------------------
    If (Not ps.AddFrame("fraMode", 80, 1500, 5500, 2400, "Mode Control (SM0, SM1)")) Then Exit Function
        If (Not ps.AddOption("OptMode0", 200, 300, 4200, 380, "Mode 0: 8-bit shift register, fixed baud rate (fPCLK/2)")) Then Exit Function
        If (Not ps.AddOption("OptMode1", 200, 699, 5000, 380, "Mode 1: 8-bit shift UART, variable baud rate (see BRG or Timer1)")) Then Exit Function
        If (Not ps.AddOption("OptMode2", 200, 1098, 4980, 380, "Mode 2: 9-bit shift UART, fixed baud rate (fPCLK/32 or fPCLK/64)")) Then Exit Function
        If (Not ps.AddOption("OptMode3", 200, 1498, 5000, 380, "Mode 3: 9-bit shift UART, variable baud rate (see BRG or Timer1)")) Then Exit Function
        
    '-------------------------------
    ' frame: fraBaudSelect
    '-------------------------------
    If (Not ps.AddFrame("fraBaudSelect", 5760, 1500, 2840, 1000, "Baudrate Source")) Then Exit Function
        If (Not ps.AddOption("OptBRG", 200, 250, 650, 380, "BRG")) Then Exit Function
        If (Not ps.AddOption("OptTimer1", 200, 580, 850, 380, "Timer 1")) Then Exit Function
    

    '-------------------------------
    ' frame: fraInterrupts
    '-------------------------------
    If (Not ps.AddFrame("fraInterrupts", 5760, 2600, 2840, 1300, "Interrupts")) Then Exit Function
        If (Not ps.AddCheck("chkEnableInt", 200, 250, 1750, 380, "Enable interrupt (ES)")) Then Exit Function
        If (Not ps.AddCheck("chkValidStopBit", 200, 600, 2490, 660, "Receive interrupt will only be activated if a valid stop bit was received (SM2)")) Then Exit Function

    '-------------------------------
    ' frame: fraOptions
    '-------------------------------
    If (Not ps.AddFrame("fraOptions", 80, 4000, 2550, 1300, "Options")) Then Exit Function
        If (Not ps.AddCheck("chkDoubleBaudrate", 200, 260, 2200, 495, "Double baud rate (SMOD) (Note: Only in Mode 2)")) Then Exit Function
        
    '-------------------------------
    ' frame: fraRXDO
    '-------------------------------
    If (Not ps.AddFrame("fraRXDO", 2800, 4000, 2800, 1300, "RXDO Pin Selection")) Then Exit Function
        If (Not ps.AddOption("OptRXDONoPinSel", 200, 250, 1750, 380, "No pins are selected")) Then Exit Function
        If (Not ps.AddOption("OptRXDOSelP1.5", 200, 550, 2000, 380, "Use pin P1.5 (RXDO_0)")) Then Exit Function
        If (Not ps.AddOption("OptRXDOSelP0.0", 200, 850, 2000, 380, "Use pin P0.0 (RXDO_1)")) Then Exit Function
    
    '-------------------------------
    ' frame: fraMultiprocessor
    '-------------------------------
    If (Not ps.AddFrame("fraMultiprocessor", 5760, 4000, 2840, 1300, "Multiprocessor")) Then Exit Function
        If (Not ps.AddCheck("chkMultiprocCom", 200, 330, 1890, 380, "Enable multiprocessor communication (SM2)")) Then Exit Function

    
  End If
  OnChangingPageUART = True
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageUART(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError

  OnShowPageUART = False
     
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bPinFree As Boolean
  Dim bDisabled As Boolean

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

  If (Pagename = "UART") Then
    '-------------------------------------------------------------------------
    ' page: UART (action)
    '-------------------------------------------------------------------------
    
    Select Case Controlname
        '------------------------------------------------------------------------------------
        ' Frame Transmitter
        '------------------------------------------------------------------------------------
        Case "OptNoPinSel"
            If r.v("UART_TxPin_P1.1_Used") = 1 Then
                Call ReleaseAlternateOutputPin(r, "TXD_0")
                r.v("UART_TxPin_P1.1_Used") = 0
            End If
            If r.v("UART_TxPin_P0.2_Used") = 1 Then
                Call ReleaseAlternateOutputPin(r, "TXD_1")
                r.v("UART_TxPin_P0.2_Used") = 0
            End If
            Call CheckResetUart(r)
            If r.v("UART_RXDO_Pin_P0.0_Used") = 1 Then
                Call ReleaseAlternateOutputPin(r, "RXDO_1")
                r.v("UART_RXDO_Pin_P0.0_Used") = 0
            End If
            If r.v("UART_RXDO_Pin_P1.5_Used") = 1 Then
                r.v("UART_RXDO_Pin_P1.5_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_0")
            End If
          
        Case "OptSelP1.1"
            r.v("UART_TxPin_P1.1_Used") = 1
            If r.v("UART_TxPin_P0.2_Used") = 1 Then
                r.v("UART_TxPin_P0.2_Used") = 0
                Call ReleaseAlternateOutputPin(r, "TXD_1")
            End If
            If (r.v(BRG_PREFIX & "BCON_R") = 0) And (GetUartMode(r) <> 0) And (GetUartMode(r) <> 2) Then r.v(BRG_PREFIX & "BCON_R") = 1
            Call RequestAlternateOutputPin(r, "TXD_0", ALT_FUN2)
        
        Case "OptSelP0.2"
            r.v("UART_TxPin_P0.2_Used") = 1
            If r.v("UART_TxPin_P1.1_Used") = 1 Then
                r.v("UART_TxPin_P1.1_Used") = 0
                Call ReleaseAlternateOutputPin(r, "TXD_0")
            End If
            If (r.v(BRG_PREFIX & "BCON_R") = 0) And (GetUartMode(r) <> 0) And (GetUartMode(r) <> 2) Then r.v(BRG_PREFIX & "BCON_R") = 1
            Call RequestAlternateOutputPin(r, "TXD_1", ALT_FUN2)

        '------------------------------------------------------------------------------------
        ' Frame "Receiver"
        '------------------------------------------------------------------------------------
        Case "chkEnableReceiver"
            r.l(UART_PREFIX & "SCON_REN") = v(1)
            If (v(1) = 0) Then
                Call CheckResetUart(r)
            Else
                If (r.v(BRG_PREFIX & "BCON_R") = 0) And (GetUartMode(r) <> 0) And (GetUartMode(r) <> 2) Then r.v(BRG_PREFIX & "BCON_R") = 1
            End If

        Case "optSelNone"
            If r.v("UART_RxPin_P1.0_Used") = 1 Then
                r.v("UART_RxPin_P1.0_Used") = 0
                Call ReleaseAlternateInputPin(r, "RXD_0")
            End If
            If r.v("UART_RxPin_P0.1_Used") = 1 Then
                r.v("UART_RxPin_P0.1_Used") = 0
                Call ReleaseAlternateInputPin(r, "RXD_1")
            End If
            r.v(UART_PREFIX & "SCON_REN") = 0
            r.v(UART_PREFIX & "SCON_SM2") = 0
            r.v(SCU_PREFIX & "MODPISEL_URRIS") = 0
            Call CheckResetUart(r)
            
        Case "optSelP1.0"
            r.v("UART_RxPin_P1.0_Used") = 1
            r.v(SCU_PREFIX & "MODPISEL_URRIS") = 0
            Call RequestAlternateInputPin(r, "RXD_0")
            If r.v("UART_RxPin_P0.1_Used") = 1 Then
                r.v("UART_RxPin_P0.1_Used") = 0
                Call ReleaseAlternateInputPin(r, "RXD_1")
            End If
            
        Case "optSelP0.1"
            r.v("UART_RxPin_P0.1_Used") = 1
            r.v(SCU_PREFIX & "MODPISEL_URRIS") = 1
            Call RequestAlternateInputPin(r, "RXD_1")
            If r.v("UART_RxPin_P1.0_Used") = 1 Then
                r.v("UART_RxPin_P1.0_Used") = 0
                Call ReleaseAlternateInputPin(r, "RXD_0")
            End If

        '------------------------------------------------------------------------------------
        ' Frame "Mode Control"
        '------------------------------------------------------------------------------------
        Case "OptMode0"
            Call SetUartMode(r, 0)
            r.v(UART_PREFIX & "SCON_SM2") = 0   ' clear bit, because its meaning is mode dependent
            r.l(UART_PREFIX & "PCON_SMOD") = 0
            r.v(BRG_PREFIX & "FDCON_BGS") = 0
            r.l(BRG_PREFIX & "BCON_R") = 0
                        
        Case "OptMode1"
            Call SetUartMode(r, 1)
            Call ResetBRG(r)
            If (r.v(BRG_PREFIX & "BCON_R") = 0) And (GetUartMode(r) <> 2) Then r.v(BRG_PREFIX & "BCON_R") = 1
            r.v(UART_PREFIX & "SCON_SM2") = 0   ' clear bit, because its meaning is mode dependent
            r.l(UART_PREFIX & "PCON_SMOD") = 0
            r.v("BRG_Interrupt_Select") = 0
            
            If r.v("UART_RXDO_Pin_P0.0_Used") = 1 Then
                r.v("UART_RXDO_Pin_P0.0_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_1")
            End If
            If r.v("UART_RXDO_Pin_P1.5_Used") = 1 Then
                r.v("UART_RXDO_Pin_P1.5_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_0")
            End If
            
        Case "OptMode2"
            Call SetUartMode(r, 2)
            r.l(BRG_PREFIX & "BCON_R") = 0
            r.l(BRG_PREFIX & "BCON_BRPRE") = &H0
            r.v(UART_PREFIX & "SCON_SM2") = 0   ' clear bit, because its meaning is mode dependent
            r.v(BRG_PREFIX & "FDCON_BGS") = 0
                        
            If r.v("UART_RXDO_Pin_P0.0_Used") = 1 Then
                r.v("UART_RXDO_Pin_P0.0_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_1")
            End If
            If r.v("UART_RXDO_Pin_P1.5_Used") = 1 Then
                r.v("UART_RXDO_Pin_P1.5_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_0")
            End If

        Case "OptMode3"
            Call SetUartMode(r, 3)
            Call ResetBRG(r)
            If (r.v(BRG_PREFIX & "BCON_R") = 0) And (GetUartMode(r) <> 2) Then r.v(BRG_PREFIX & "BCON_R") = 1
            r.v(UART_PREFIX & "SCON_SM2") = 0   ' clear bit, because its meaning is mode dependent
            r.l(UART_PREFIX & "PCON_SMOD") = 0
            r.v("BRG_Interrupt_Select") = 0
            
            If r.v("UART_RXDO_Pin_P0.0_Used") = 1 Then
                r.v("UART_RXDO_Pin_P0.0_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_1")
            End If
            If r.v("UART_RXDO_Pin_P1.5_Used") = 1 Then
                r.v("UART_RXDO_Pin_P1.5_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_0")
            End If


        '------------------------------------------------------------------------------------
        ' Frame "Baudrate Source"
        '------------------------------------------------------------------------------------
        Case "OptBRG"
            r.v(BRG_PREFIX & "FDCON_BGS") = 0
            Call ResetBRG(r)
            r.v(BRG_PREFIX & "BCON_R") = 1
        
        Case "OptTimer1"
            r.v(BRG_PREFIX & "FDCON_BGS") = 1
            If r.v(BRG_PREFIX & "FDCON_FDM") <> 1 Then
                Call ResetBRG(r)
            End If
        
        '------------------------------------------------------------------------------------
        ' Frame "Options"
        '------------------------------------------------------------------------------------
        Case "chkDoubleBaudrate"
            r.v(UART_PREFIX & "PCON_SMOD") = v(1)
            
            
        '------------------------------------------------------------------------------------
        ' Frame "RXDO output pin selection"
        '------------------------------------------------------------------------------------
        Case "OptRXDONoPinSel"
            If r.v("UART_RXDO_Pin_P1.5_Used") = 1 Then
                Call ReleaseAlternateOutputPin(r, "RXDO_0")
                r.v("UART_RXDO_Pin_P1.5_Used") = 0
            End If
            If r.v("UART_RXDO_Pin_P0.0_Used") = 1 Then
                Call ReleaseAlternateOutputPin(r, "RXDO_1")
                r.v("UART_RXDO_Pin_P0.0_Used") = 0
            End If
            Call CheckResetUart(r)
        
        Case "OptRXDOSelP1.5"
            r.v("UART_RXDO_Pin_P1.5_Used") = 1
            If r.v("UART_RXDO_Pin_P0.0_Used") = 1 Then
                r.v("UART_RXDO_Pin_P0.0_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_1")
            End If
            If (r.v(BRG_PREFIX & "BCON_R") = 0) And (GetUartMode(r) <> 0) And (GetUartMode(r) <> 2) Then r.v(BRG_PREFIX & "BCON_R") = 1
            Call RequestAlternateOutputPin(r, "RXDO_0", ALT_FUN2)
        
        Case "OptRXDOSelP0.0"
            r.v("UART_RXDO_Pin_P0.0_Used") = 1
            If r.v("UART_RXDO_Pin_P1.5_Used") = 1 Then
                r.v("UART_RXDO_Pin_P1.5_Used") = 0
                Call ReleaseAlternateOutputPin(r, "RXDO_0")
            End If
            If (r.v(BRG_PREFIX & "BCON_R") = 0) And (GetUartMode(r) <> 0) And (GetUartMode(r) <> 2) Then r.v(BRG_PREFIX & "BCON_R") = 1
            Call RequestAlternateOutputPin(r, "RXDO_1", ALT_FUN3)
            
        '------------------------------------------------------------------------------------
        ' Frame "Multiprocessor"
        '------------------------------------------------------------------------------------
        Case "chkMultiprocCom"
            r.v(UART_PREFIX & "SCON_SM2") = v(1)

        '------------------------------------------------------------------------------------
        ' Frame "Interrupts"
        '------------------------------------------------------------------------------------
        Case "chkEnableInt"
            r.v(CORE_PREFIX & "IEN0_ES") = v(1)
            r.v("UART_USE_Isr") = v(1)

        Case "chkValidStopBit"
            r.v(UART_PREFIX & "SCON_SM2") = v(1)



    End Select

    If (Controlname <> "") Then
        Call AdjustFunctions(ps) ' deselect function which are not applicable anymore
    End If

    '-------------------------------------------------------------------------
    ' page: UART (show)
    '-------------------------------------------------------------------------
    
    '------------------------------------------------------------------------------------
    ' Frame "Transmitter"
    '------------------------------------------------------------------------------------
    With c("OptNoPinSel")
        .Value = Not (r.v("UART_TxPin_P0.2_Used") Or r.v("UART_TxPin_P1.1_Used"))
    End With
    
    With c("OptSelP1.1")
        bDisabled = Not IsAlternatePinFree(r, "TXD_0")
        .Enabled = Not bDisabled
        .Value = -CInt(r.v("UART_TxPin_P1.1_Used") = 1)
    End With

    With e("OptSelP1.1")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "TXD_0")
        End If
    End With

    With c("OptSelP0.2")
        bDisabled = Not IsAlternatePinFree(r, "TXD_1")
        .Enabled = Not bDisabled
        .Value = -CInt(r.v("UART_TxPin_P0.2_Used") = 1)
    End With

    With e("OptSelP0.2")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "TXD_1")
        End If
    End With


    '------------------------------------------------------------------------------------
    ' Frame "Receiver"
    '------------------------------------------------------------------------------------

    With c("chkEnableReceiver")
        bDisabled = (r.v("UART_RxPin_P1.0_Used") = 0 And r.v("UART_RxPin_P0.1_Used") = 0)
        .Enabled = Not bDisabled
        .Value = -CInt(r.v(UART_PREFIX & "SCON_REN") = 1)
    End With
 

    With e("chkEnableReceiver")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            
            If (r.v("UART_RxPin_P1.0_Used") = 0 And r.v("UART_RxPin_P0.1_Used") = 0) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("No pins are selected for reception")
            End If
        End If
    End With

    With c("optSelNone")
        .Value = (r.v("UART_RxPin_P1.0_Used") = 0 And r.v("UART_RxPin_P0.1_Used") = 0)
    End With
    
    With c("optSelP1.0")
        bDisabled = Not IsAlternatePinFree(r, "RXD_0")
        .Enabled = Not bDisabled
        .Value = -CInt(r.v("UART_RxPin_P1.0_Used") = 1)
    End With

    With e("optSelP1.0")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "RXD_0")
        End If
    End With

    With c("optSelP0.1")
        bDisabled = Not IsAlternatePinFree(r, "RXD_1")
        .Enabled = Not bDisabled
        .Value = -CInt(r.v("UART_RxPin_P0.1_Used") = 1)
    End With

    With e("optSelP0.1")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "RXD_1")
        End If
    End With
    

    '------------------------------------------------------------------------------------
    ' Frame "Mode Control"
    '------------------------------------------------------------------------------------
    With c("OptMode0")
         bDisabled = CheckDisabledUart(r)
         .Enabled = Not bDisabled
        .Value = (GetUartMode(r) = 0)
    End With

    With e("OptMode0")
                If (bDisabled) Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
        End If
    End With
    
    '------------------------------------------------------------------------------------
    With c("OptMode1")
         bDisabled = CheckDisabledUart(r)
         .Enabled = Not bDisabled
        .Value = (GetUartMode(r) = 1)
    End With

    With e("OptMode1")
                If (bDisabled) Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
        End If
    End With

    '------------------------------------------------------------------------------------
    With c("OptMode2")
         bDisabled = CheckDisabledUart(r)
         .Enabled = Not bDisabled
        .Value = (GetUartMode(r) = 2)
    End With

    With e("OptMode2")
                If (bDisabled) Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
        End If
    End With

    '------------------------------------------------------------------------------------
    With c("OptMode3")
         bDisabled = CheckDisabledUart(r)
         .Enabled = Not bDisabled
        .Value = (GetUartMode(r) = 3)
    End With

    With e("OptMode3")
                If (bDisabled) Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
        End If
    End With
    
    '------------------------------------------------------------------------------------
    ' Frame "Baudrate Source"
    '------------------------------------------------------------------------------------
    With c("OptBRG")
        bDisabled = (GetUartMode(r) = 0) Or (GetUartMode(r) = 2)
        .Enabled = Not bDisabled
        .Value = (r.v(BRG_PREFIX & "FDCON_BGS") = 0)
    End With
    
    With e("OptBRG")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 1 or 3 is not selected")
        End If
    End With
    
    With c("OptTimer1")
        bDisabled = (GetUartMode(r) = 0) Or (GetUartMode(r) = 2)
        .Enabled = Not bDisabled
        .Value = (r.v(BRG_PREFIX & "FDCON_BGS") = 1)
    End With
    
    With e("OptTimer1")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 1 or 3 is not selected")
        End If
    End With

    '------------------------------------------------------------------------------------
    ' Frame "Multiprocessor"
    '------------------------------------------------------------------------------------
    With c("chkMultiprocCom")
        bDisabled = (GetUartMode(r) < 2)
        .Enabled = Not bDisabled
        .Value = -CInt(IsMultiComMode(r))
    End With

    With e("chkMultiprocCom")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 2 or 3 is not selected")
        End If
    End With

    '------------------------------------------------------------------------------------
    ' Frame "Interrupts"
    '------------------------------------------------------------------------------------
    With c("chkEnableInt")
        bDisabled = CheckDisabledUart(r)
        .Enabled = Not bDisabled
        .Value = -CInt(r.v(CORE_PREFIX & "IEN0_ES") = 1)
    End With

    With e("chkEnableInt")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
        End If
    End With

    '------------------------------------------------------------------------------------
    With c("chkValidStopBit")
        bDisabled = (r.v(UART_PREFIX & "SCON_REN") = 0) Or (GetUartMode(r) <> 1)
        .Enabled = Not bDisabled
        .Value = -CInt((r.v(UART_PREFIX & "SCON_SM2") = 1) And (GetUartMode(r) = 1))
    End With

    With e("chkValidStopBit")
                If (bDisabled) Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (GetUartMode(r) <> 1) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 1 is not selected")
            End If
            If (r.v(UART_PREFIX & "SCON_REN") = 0) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the receiver is not enabled")
            End If
        End If
    End With

    '------------------------------------------------------------------------------------
    ' Frame "Options"
    '------------------------------------------------------------------------------------
    With c("chkDoubleBaudrate")
        bDisabled = CheckDisabledUart(r) Or ((GetUartMode(r) <> 2) And (r.v(BRG_PREFIX & "FDCON_BGS") = 0))
        .Enabled = Not bDisabled
        .Value = -CInt(r.v(UART_PREFIX & "PCON_SMOD") = 1)
    End With

    With e("chkDoubleBaudrate")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (CheckDisabledUart(r)) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
            End If
            If GetUartMode(r) = 0 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 2 is not selected")
            End If
            
            If (GetUartMode(r) = 1) Or (GetUartMode(r) = 3) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Timer1 is not selected as a baudrate generator")
            End If
        End If
    End With
    
    '------------------------------------------------------------------------------------
    ' Frame "RXDO Pin Selection"
    '------------------------------------------------------------------------------------
    With c("OptRXDONoPinSel")
        bDisabled = CheckDisabledUart(r) Or (GetUartMode(r) <> 0)
        .Enabled = Not bDisabled
        .Value = Not (r.v("UART_RXDO_Pin_P0.0_Used") Or r.v("UART_RXDO_Pin_P1.5_Used"))
    End With
    
    With e("OptRXDONoPinSel")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 0 is not selected")
        End If
    End With
    
    With c("OptRXDOSelP1.5")
        bPinFree = IsAlternatePinFree(r, "RXDO_0")
        bDisabled = CheckDisabledUart(r) Or (GetUartMode(r) <> 0 Or (Not bPinFree))
        .Enabled = Not bDisabled
        .Value = r.v("UART_RXDO_Pin_P1.5_Used")
    End With
    
    With e("OptRXDOSelP1.5")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If GetUartMode(r) <> 0 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 0 is not selected")
            ElseIf (Not bPinFree) Then
                .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "RXDO_0")
            End If
        End If
    End With
        
    With c("OptRXDOSelP0.0")
        bPinFree = IsAlternatePinFree(r, "RXDO_1")
        bDisabled = CheckDisabledUart(r) Or (GetUartMode(r) <> 0 Or (Not bPinFree))
        .Enabled = Not bDisabled
        .Value = r.v("UART_RXDO_Pin_P0.0_Used")
    End With
    
    With e("OptRXDOSelP0.0")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If GetUartMode(r) <> 0 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 0 is not selected")
            ElseIf (Not bPinFree) Then
                .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "RXDO_1")
            End If
        End If
    End With
    
  End If         ' if Pagename

  OnShowPageUART = True
  Exit Function

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

'*****************************************************************************
'Description:
'        This subroutine is called when the TxD or RxD Pin is deselected.
'        The result might be that UART operation is no longer possible.
'        Then the variables and SFRs will be reset to DAvE's default state.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub CheckResetUart(r As ProjectData)
  On Error Resume Next

    If r.v("UART_TxPin_P0.2_Used") = 0 And r.v("UART_TxPin_P1.1_Used") = 0 And r.v(UART_PREFIX & "SCON_REN") = 0 Then
        r.l(UART_PREFIX & "SCON_SM2") = 0
        Call SetUartMode(r, DEFAULT_UART_MODE)
        r.l(UART_PREFIX & "PCON_SMOD") = 0
        r.v(CORE_PREFIX & "IEN0_ES") = 0
        r.l(UART_PREFIX & "FDCON_BGS") = 0
        r.v(BRG_PREFIX & "BCON_R") = 0
        r.l(BRG_PREFIX & "BCON_BRPRE") = &H0
        r.v("UART_USE_Isr") = 0
        r.v("BRG_Interrupt_Select") = 0
    End If
    
    If (((r.v("UART_TxPin_P1.1_Used") = 0) And (r.v("UART_TxPin_P0.2_Used") = 0)) And ((r.v("UART_RxPin_P1.0_Used") = 0) And (r.v("UART_RxPin_P0.1_Used") = 0))) Then
       Call ResetBRG(r)
    End If
    
End Sub

'*****************************************************************************
'Description:
'        This function checks whether UART has to be disabled or not based
'        on the transmitter and receiver settings.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data object
'-----------------------------------------------------------------------------
'Returns: True  - If the UART has to be disbaled
'         False - If not
'*****************************************************************************
Function CheckDisabledUart(r As ProjectData) As Boolean
  On Error Resume Next
    
    CheckDisabledUart = False
    
    If r.v("UART_TxPin_P0.2_Used") = 0 And r.v("UART_TxPin_P1.1_Used") = 0 And r.v(UART_PREFIX & "SCON_REN") = 0 Then
        CheckDisabledUart = True
    End If
    
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageFunctions(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageFunctions = False

  If (Not ps.AddPage("Functions")) Then Exit Function

  OnLoadPageFunctions = True
End Function

Public Function OnChangingPageFunctions(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageFunctions = False

  Dim pos As Long

  If (Not ps.ExtenderCollection("Functions").Loaded) And (Pagename = "Functions") Then
    ps.ExtenderCollection("Functions").Loaded = True

    If (Not ps.AddFrame("fraUART_USE_Init", 100, 100, 4150, 700, "Initialization Function")) Then Exit Function
    If (Not ps.AddCheck("chkUART_USE_Init", 200, 270, 1150, 380, "UART_vInit")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_Init", 2150, 300, 1900, 315)) Then Exit Function

    If (Not ps.AddFrame("fraUART_CFile", 4400, 100, 4150, 700, "Source File")) Then Exit Function
    If (Not ps.AddText("txtUART_CFile", 1500, 300, 2550, 315)) Then Exit Function
    If (Not ps.AddLabel("lblUART_CFile", 0, 200, 300, 1575, 380, "File name")) Then Exit Function

    ' Functions and Macros
    '------------------------------------------------------------------------------------------------
    If (Not ps.AddFrame("fraLibMisc", 100, 900, 4150, 4370, "Miscellaneous Functions")) Then Exit Function
    pos = 0
    If (Not ps.AddCheck("chkUART_USE_Isr", 200, 270 + pos * 300, 2100, 380, "UART_viIsr")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_Isr", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_RxEnable", 200, 270 + pos * 300, 2100, 380, "UART_vRxEnable")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_RxEnable", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_RxDisable", 200, 270 + pos * 300, 2100, 380, "UART_vRxDisable")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_RxDisable", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_RxReady", 200, 270 + pos * 300, 2100, 380, "UART_bRxReady")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_RxReady", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_TxReady", 200, 270 + pos * 300, 2100, 380, "UART_bTxReady")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_TxReady", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    '------------------------------------------------------------------------------------------------
    If (Not ps.AddFrame("fraLib8Bit", 4400, 900, 4150, 1075, "8-Bit Data Units")) Then Exit Function
    pos = 0

    If (Not ps.AddCheck("chkUART_USE_GetData8", 200, 270 + pos * 300, 2100, 380, "UART_ubGetData8")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_GetData8", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_SendData8", 200, 270 + pos * 300, 2100, 380, "UART_vSendData8")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_SendData8", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    '------------------------------------------------------------------------------------------------
    If (Not ps.AddFrame("fraLib9Bit", 4400, 2075, 4150, 1075, "9-Bit Data Units")) Then Exit Function
    pos = 0

    If (Not ps.AddCheck("chkUART_USE_GetData9", 200, 270 + pos * 300, 2100, 380, "UART_uwGetData9")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_GetData9", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_SendData9", 200, 270 + pos * 300, 2100, 380, "UART_vSendData9")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_SendData9", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    '------------------------------------------------------------------------------------------------
    If (Not ps.AddFrame("fraLibMulti", 4400, 3250, 4150, 2025, "Multiprocessor Communication")) Then Exit Function
    pos = 0
    If (Not ps.AddCheck("chkUART_USE_SendAddr", 200, 270 + pos * 300, 2100, 380, "UART_vSendAddr")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_SendAddr", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_OwnAddress", 200, 270 + pos * 300, 2100, 380, "UART_bOwnAddress")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_OwnAddress", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_WakeUp", 200, 270 + pos * 300, 2100, 380, "UART_vWakeUp")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_WakeUp", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkUART_USE_GotoSleep", 200, 270 + pos * 300, 2100, 380, "UART_vGotoSleep")) Then Exit Function
    If (Not ps.AddText("txtUART_NAME_GotoSleep", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

  End If

  OnChangingPageFunctions = True
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageFunctions(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError
  
  OnShowPageFunctions = False
  
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim func As FunctionClass

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set func = New FunctionClass
  
  If (Pagename = "Functions") Then
    '-------------------------------------------------------------------------
    ' page: Functions (action)
    '-------------------------------------------------------------------------
    Select Case Controlname
        Case "chkUART_USE_Init"
             r.v("UART_USE_Init") = v(1)
    
        Case "txtUART_NAME_Init"
             If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, c("chkUART_USE_Init").Caption)) Then
                r.v("UART_NAME_Init") = v(1)
             End If
    
        Case "txtUART_CFile"
             If (IsInputFormatOK(v(1), FORMAT_FILES, c("lblUART_CFile").Caption)) Then
                r.v("UART_CFile") = v(1)
             End If
    End Select     ' case Controlname
    
    If (Not UpdateFunction(ps, Controlname, v, "Isr")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "RxEnable")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "RxDisable")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "RxReady")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "TxReady")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "GetData8")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "SendData8")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "GetData9")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "SendData9")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "SendAddr")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "OwnAddress")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "WakeUp")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "GotoSleep")) Then Exit Function
    
    '-------------------------------------------------------------------------
    ' page: Functions (show)
    '-------------------------------------------------------------------------
    Call RefreshFunctionDescriptions(func, r)

    '-------------------------------------------------------------------------
    ' control: chkUART_USE_Init (show)
    '-------------------------------------------------------------------------
    c("chkUART_USE_Init").Value = -CInt(r.v("UART_USE_Init") = 1)
    e("chkUART_USE_Init").EnabledTooltipText = FormatFunctionToolTip(r, func, "UART_NAME_Init")

    '-------------------------------------------------------------------------
    ' control: txtUART_NAME_Init (show)
    '-------------------------------------------------------------------------
    With c("txtUART_NAME_Init")
        bDisabled = r.v("UART_USE_Init") = 0
        .Enabled = Not bDisabled
        .Text = FormatTextOutput(r.v("UART_NAME_Init"), "S")
    End With

    With e("txtUART_NAME_Init")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_NAME)
        End If
    End With
If (Not CheckFunction(ps, func, True, "Init")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "Isr")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "RxEnable")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "RxDisable")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "RxReady")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "TxReady")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "GetData8")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "SendData8")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "GetData9")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "SendData9")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "SendAddr")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "OwnAddress")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "WakeUp")) Then Exit Function
    If (Not CheckFunction(ps, func, True, "GotoSleep")) Then Exit Function
  
    '-------------------------------------------------------------------------
    ' control: lblUART_CFile (show)
    '-------------------------------------------------------------------------
    With c("lblUART_CFile")
        bDisabled = r.v("UART_USE_Init") = 0
        .Enabled = Not bDisabled
    End With

    With e("lblUART_CFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_CFILE_NAME)
        End If
    End With

    '-------------------------------------------------------------------------
    ' control: txtUART_CFile (show)
    '-------------------------------------------------------------------------
    With c("txtUART_CFile")
      bDisabled = (r.v("UART_USE_Init") = 0)
      .Enabled = Not (bDisabled)
      .Text = FormatTextOutput(r.v("UART_CFile"), "S")
    End With

    With e("txtUART_CFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_CFILE_NAME)
        End If
    End With

  End If         ' if Pagename

  OnShowPageFunctions = True
  Exit Function

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

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the Project Data Object
'        Controlname     control name
'        v()             value array
'        sName           function keyword
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function UpdateFunction(ps As Peripherals, Controlname As String, v() As Variant, sName As String) As Boolean
  On Error GoTo ExitLabel
  UpdateFunction = False

  Dim r As ProjectData
  Dim c As Collection

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

  '---------------------------------------------------------------------
  ' control: chkUART_USE_.. (action)
  '---------------------------------------------------------------------
  If (Controlname = "chkUART_USE_" & sName) Then
    r.v("UART_USE_" & sName) = v(1)
  End If

  '---------------------------------------------------------------------
  ' control: txtUART_NAME_.. (action)
  '---------------------------------------------------------------------
  If (Controlname = "txtUART_NAME_" & sName) Then
    If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, c("chkUART_USE_" & sName).Caption)) Then
        r.v("UART_NAME_" & sName) = v(1)
    Else
        Exit Function
    End If
  End If

  UpdateFunction = True
ExitLabel:
End Function

'*****************************************************************************
'Description:           Any functions which do not suit the current settings
'                       will be deselected.
'-----------------------------------------------------------------------------
'Parameters:            none
'*****************************************************************************
Sub AdjustFunctions(ps As Peripherals)
  On Error Resume Next
  Dim func As FunctionClass

  Set func = New FunctionClass
  If (Not CheckFunction(ps, func, False, "Init")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "Isr")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "RxEnable")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "RxDisable")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "RxReady")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "TxReady")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "GetData8")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "SendData8")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "GetData9")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "SendData9")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "SendAddr")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "OwnAddress")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "WakeUp")) Then Exit Sub
  If (Not CheckFunction(ps, func, False, "GotoSleep")) Then Exit Sub
End Sub

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        func            reference to the function object
'        sName           function keyword
'        bShow           True  = update the function page
'                        False = adjust selections (deselect if not applicable)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function CheckFunction(ps As Peripherals, func As FunctionClass, bShow As Boolean, sName As String) As Boolean
  On Error GoTo ExitLabel
  CheckFunction = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection

  Dim bDisabled          As Boolean
  Dim bConditionInit     As Boolean
  Dim bConditionIsr      As Boolean
  Dim bConditionPins     As Boolean
  Dim bConditionRx       As Boolean
  Dim bConditionTx       As Boolean
  Dim bConditionMode13   As Boolean
  Dim bConditionBRGen    As Boolean
  Dim bConditionIntFlg   As Boolean
  Dim bCondition8Bit     As Boolean
  Dim bCondition9Bit     As Boolean
  Dim bConditionMulti    As Boolean
  Dim bNormalModeSel     As Boolean

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

  '-------------------------------------------------------------------------
  ' control: chkUART_USE_.. (show)
  '-------------------------------------------------------------------------
  bDisabled = False
  bConditionInit = False
  bConditionIsr = False
  bConditionPins = False
  bConditionRx = False
  bConditionTx = False
  bConditionMode13 = False
  bConditionIntFlg = False
  bCondition8Bit = False
  bCondition9Bit = False
  bConditionMulti = False

  ' Select the possible disabling reasons:
  Select Case sName
    Case "Init"
        bConditionPins = True
    
    Case "Isr"
        bConditionIsr = True

    Case "RxEnable", "RxDisable"
        bConditionInit = True
        bConditionRx = True

    Case "RxReady"
        bConditionInit = True
        bConditionRx = True
        bConditionIntFlg = True

    Case "TxReady"
        bConditionInit = True
        bConditionTx = True
        bConditionIntFlg = True
        
    Case "GetData8"
        bConditionInit = True
        bConditionRx = True
        bCondition8Bit = True

    Case "SendData8"
        bConditionInit = True
        bConditionTx = True
        bCondition8Bit = True

    Case "GetData9"
        bConditionInit = True
        bConditionRx = True
        bCondition9Bit = True

    Case "SendData9"
        bConditionInit = True
        bConditionTx = True
        bCondition9Bit = True

    Case "SendAddr"
        bConditionInit = True
        bConditionTx = True
        bConditionMulti = True

    Case "OwnAddress", "WakeUp", "GotoSleep"
        bConditionInit = True
        bConditionRx = True
        bConditionMulti = True
  End Select

  ' Check the selected disabling reasons:
  If (bConditionInit) Then bConditionInit = (r.v("UART_USE_Init") = 0)
  If (bConditionPins) Then bConditionPins = (r.v("UART_TxPin_P0.2_Used") = 0 And r.v("UART_TxPin_P1.1_Used") = 0 And r.v(UART_PREFIX & "SCON_REN") = 0)
  If (bConditionRx) Then bConditionRx = (r.v(UART_PREFIX & "SCON_REN") = 0)
  If (bConditionTx) Then bConditionTx = (r.v("UART_TxPin_P0.2_Used") = 0 And r.v("UART_TxPin_P1.1_Used") = 0)
  If (bConditionMode13) Then bConditionMode13 = (GetUartMode(r) <> 1) And (GetUartMode(r) <> 3)
  If (bConditionIntFlg) Then bConditionIntFlg = (r.v("UART_USE_Isr") = 1)
  If (bCondition8Bit) Then bCondition8Bit = DataSize9Bit(r)
  If (bCondition9Bit) Then bCondition9Bit = Not DataSize9Bit(r)
  If (bConditionMulti) Then bConditionMulti = Not IsMultiComMode(r)
  
  If r.v(BRG_PREFIX & "FDCON_FDM") = 1 Then bNormalModeSel = True
  
  ' reasons for deselection:
  bDisabled = bConditionPins Or bConditionRx Or bConditionTx Or bConditionMode13 Or bConditionIntFlg Or _
              bCondition8Bit Or bCondition9Bit Or bConditionMulti

  If sName = "Init" Then
    If bDisabled And bNormalModeSel Then bDisabled = False
  Else
      bConditionPins = (r.v("UART_TxPin_P0.2_Used") = 0 And r.v("UART_TxPin_P1.1_Used") = 0 And r.v(UART_PREFIX & "SCON_REN") = 0)
      If bConditionInit And bNormalModeSel And bConditionPins Then
        bConditionInit = False
      End If
  End If
  

  If Not bShow Then

    '------------ Deselect Function if not applicable ----------------------
    If (bDisabled) Then
        r.v("UART_USE_" & sName) = 0
    End If

  Else

    '------------ Update the displayed function page -----------------------

      With c("chkUART_USE_" & sName)

        ' reasons for disabling:
        bDisabled = bDisabled Or bConditionInit Or bConditionIsr
        .Enabled = Not bDisabled
        .Value = -CInt((r.v("UART_USE_Init") = 1) And (r.v("UART_USE_" & sName) = 1))

      End With


      With e("chkUART_USE_" & sName)
        If (bDisabled) Then
            ' tooltip if control is disabled
            If (bConditionIsr) Then
                    .DisabledTooltipText = TOOLTIP_DISABLED_ISR & vbCrLf & FormatFunctionToolTip(r, func, "UART_NAME_" & sName)
            Else
                .DisabledTooltipText = GetDisabledFunctionTooltipEntry(func, "UART_NAME_" & sName)
                If (bConditionInit) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
                If (bConditionPins) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
                If (bConditionRx) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the receiver is not enabled")
                If (bConditionTx) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter")
                If (bConditionMode13) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 1 or 3 is not selected")
                If (bConditionIntFlg) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART interrupt is enabled. So the interrupt flags can not be used for this function.")
                If (bCondition8Bit) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the data unit size of the selected mode is not 8 bit")
                If (bCondition9Bit) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the data unit size of the selected mode is not 9 bit")
                If (bConditionMulti) Then .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("multiprocessor communication is not selected")
            End If
        Else
            ' tooltip if control is enabled
            .EnabledTooltipText = FormatFunctionToolTip(r, func, "UART_NAME_" & sName)
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: txtUART_NAME_... (show)
      '-------------------------------------------------------------------------
      With c("txtUART_NAME_" & sName)
        bDisabled = c("chkUART_USE_" & sName).Value = 0
        .Enabled = Not bDisabled
        .Text = FormatTextOutput(r.s("UART_NAME_" & sName), "S")
      End With

      With e("txtUART_NAME_" & sName)
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_NAME)
        End If
      End With

  End If

  CheckFunction = True
ExitLabel:
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageParameters(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageParameters = False

  If (Not ps.AddPage("Parameters")) Then Exit Function

  OnLoadPageParameters = True
End Function


Public Function OnChangingPageParameters(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  Dim iPos As Integer
  Dim sName As String

  OnChangingPageParameters = False

  If (Not ps.ExtenderCollection("Parameters").Loaded) And (Pagename = "Parameters") Then
    ps.ExtenderCollection("Parameters").Loaded = True

    If (Not ps.AddFrame("fraUART_HFile", 100, 100, 8450, 700, "Header File")) Then Exit Function
    If (Not ps.AddText("txtUART_HFile", 1825, 250, 5000, 315)) Then Exit Function
    If (Not ps.AddLabel("lblUART_HFile", 0, 200, 300, 1575, 380, "File name")) Then Exit Function

  End If

  OnChangingPageParameters = True
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageParameters(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError

  OnShowPageParameters = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim sName As String

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


  If (Pagename = "Parameters") Then
    '-------------------------------------------------------------------------
    ' page: Parameters (action)
    '-------------------------------------------------------------------------
    Select Case Controlname
        Case "txtUART_HFile"
            If (IsInputFormatOK(v(1), FORMAT_FILES, c("lblUART_HFile").Caption)) Then
                r.v("UART_HFile") = v(1)
            Else
                Exit Function
            End If

        Case Else
            'no editable parameters implemented

    End Select

    '-------------------------------------------------------------------------
    ' page: Parameters (show)
    '-------------------------------------------------------------------------
    With c("lblUART_HFile")
        bDisabled = (r.v("UART_USE_Init") = 0)
        .Enabled = Not bDisabled
    End With
    With e("lblUART_HFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_HFILE_NAME)
        End If
    End With

    With c("txtUART_HFile")
        bDisabled = (r.v("UART_USE_Init") = 0)
        .Enabled = Not bDisabled
        .Text = FormatTextOutput(r.v("UART_HFile"), "S")
    End With
    With e("txtUART_HFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_HFILE_NAME)
        End If
    End With

    'no editable parameters implemented

  End If         ' if Pagename

  OnShowPageParameters = True
  Exit Function

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

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Private Function OnLoadPageBRG(ps As Peripherals) As Boolean
On Error Resume Next
    OnLoadPageBRG = False
    
    If (Not ps.AddPage("BRG")) Then Exit Function
    
    OnLoadPageBRG = True
End Function

Public Function OnChangingPageBRG(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next
  
  OnChangingPageBRG = False
  
  If (Not ps.ExtenderCollection("BRG").Loaded) And (Pagename = "BRG") Then
    ps.ExtenderCollection("BRG").Loaded = True
    
    
    '-------------------------------
    ' frame: fraBRGCon
    '-------------------------------
    If (Not ps.AddFrame("fraBRGCon", 100, 100, 3500, 1000, "BRG Run Control")) Then Exit Function
        If (Not ps.AddCheck("chkBRGCon", 200, 260, 1950, 380, "Enable BRG (BCON.R)")) Then Exit Function
    
    '-------------------------------
    ' frame: fraFracDivide
    '-------------------------------
    If (Not ps.AddFrame("fraFracDivide", 3700, 100, 3400, 1000, "Fractional Divider Control")) Then Exit Function
        If (Not ps.AddCheck("chkFracDivide", 200, 260, 3150, 380, "Enable Fractional Divider (FDCON.FDEN)")) Then Exit Function
        
    '-------------------------------
    ' frame: fraBRG
    '-------------------------------
    If (Not ps.AddFrame("frmBRG", 100, 1300, 7000, 2800, "BRG Settings")) Then Exit Function
        If (Not ps.AddLabel("lblPreScaler", 0, 200, 300, 1575, 240, "Prescaler (BRPRE)")) Then Exit Function
        If (Not ps.AddCombo("cboPreScaler", 2000, 250, 1815, 315)) Then Exit Function
        With ps.ControlCollection("cboPreScaler")
            .AddItem "fDIV = fPCLK"
            .AddItem "fDIV = fPCLK/2"
            .AddItem "fDIV = fPCLK/4"
            .AddItem "fDIV = fPCLK/8"
            .AddItem "fDIV = fPCLK/16"
            .AddItem "fDIV = fPCLK/32"
        End With
        
        If (Not ps.AddLabel("lblFracDivide", 0, 4000, 250, 1815, 1500, "STEP Value")) Then Exit Function
        If (Not ps.AddText("txtFracDivide", 5500, 250, 1000, 315)) Then Exit Function
        
        
        If (Not ps.AddLabel("lblReqBaud", 0, 200, 800, 1575, 385, "Required Baudrate [kBaud]")) Then Exit Function
        If (Not ps.AddText("txtReqBaud", 2000, 800, 1000, 315)) Then Exit Function
        
        If (Not ps.AddLabel("lblMinBaudLabel", 0, 200, 1550, 1300, 385, "Min. Baudrate [Baud]")) Then Exit Function
        If (Not ps.AddLabel("lblMinBaud", 1, 2000, 1550, 1000, 315)) Then Exit Function
        
        If (Not ps.AddLabel("lblMaxBaudLabel", 0, 200, 2150, 1300, 385, "Max. Baudrate [MBaud]")) Then Exit Function
        If (Not ps.AddLabel("lblMaxBaud", 1, 2000, 2150, 1000, 315)) Then Exit Function
        
        If (Not ps.AddLabel("lblActualBaudLabel", 0, 4000, 800, 1575, 385, "Actual Baudrate [kBaud]")) Then Exit Function
        If (Not ps.AddLabel("lblActualBaud", 1, 5500, 800, 1000, 315)) Then Exit Function
                
        If (Not ps.AddLabel("lblDeviationLabel", 0, 4000, 1550, 1500, 400, "Percentage of deviation [%]")) Then Exit Function
        If (Not ps.AddLabel("lblDeviation", 1, 5500, 1550, 1000, 315)) Then Exit Function
        
        If (Not ps.AddLabel("lblReloadValue", 0, 4000, 2150, 1200, 400, "Reload Value (BG) ")) Then Exit Function
        If (Not ps.AddText("txtReloadValue", 5500, 2150, 1000, 315)) Then Exit Function
        
    '---------------------------------------------------------------------------------------
    ' Following controls are for Normal Divider mode
    '---------------------------------------------------------------------------------------
    
    '-------------------------------
    ' frame: fraNormalDivider
    '-------------------------------
    If (Not ps.AddFrame("fraNormalDivider", 100, 100, 3500, 1000, "Normal Divider Mode")) Then Exit Function
        If (Not ps.AddCheck("chkNormalDivider", 200, 260, 2800, 380, "Select Normal Divider Mode (FDM)")) Then Exit Function

    '-------------------------------
    ' frame: fraStartTimer
    '-------------------------------
    If (Not ps.AddFrame("fraStartTimer", 3700, 100, 3400, 1000, "Timer Run Control")) Then Exit Function
        If (Not ps.AddCheck("chkStartTimer", 200, 260, 3150, 380, "Start timer (FDEN)")) Then Exit Function


    '-------------------------------
    ' frame: fraTmrSettings
    '-------------------------------
    If (Not ps.AddFrame("frmTmrSettings", 100, 1300, 7000, 2800, "Timer Settings")) Then Exit Function
        If (Not ps.AddLabel("lblTmrPreScaler", 0, 200, 300, 1575, 240, "Prescaler (BRPRE)")) Then Exit Function
        If (Not ps.AddCombo("cboTmrPreScaler", 2000, 250, 1815, 315)) Then Exit Function
        With ps.ControlCollection("cboTmrPreScaler")
            .AddItem "fDIV = fPCLK"
            .AddItem "fDIV = fPCLK/2"
            .AddItem "fDIV = fPCLK/4"
            .AddItem "fDIV = fPCLK/8"
            .AddItem "fDIV = fPCLK/16"
            .AddItem "fDIV = fPCLK/32"
        End With


        If (Not ps.AddLabel("lblTmrReloadValue", 0, 200, 925, 1575, 385, "Reload Value (STEP)")) Then Exit Function
        If (Not ps.AddText("txtTmrReloadValue", 2000, 900, 1000, 315)) Then Exit Function

        If (Not ps.AddLabel("lblTmrOverflow", 0, 200, 1550, 1500, 385, "Timer Overflow [" & Chr(181) & "s]")) Then Exit Function
        If (Not ps.AddLabel("lblTmrOverflowtxt", 1, 2000, 1550, 1275, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmTmrInterrupt
    '-------------------------------
    If (Not ps.AddFrame("frmTmrInterrupt", 100, 4200, 7000, 900, "Interrupt Control")) Then Exit Function
        If (Not ps.AddCheck("chkEnableNDOV", 125, 300, 3300, 400, "Enable timer overflow interrupt (IEN0.ET2)")) Then Exit Function


  End If
  
  OnChangingPageBRG = True
  
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageBRG(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError

  OnShowPageBRG = False
  
  Dim SharedInt As Integer
  Dim bDisabled As Boolean
  Dim c As Collection
  Dim e As Collection
  Dim r As ProjectData
  Dim Baudrate    As BaudrateType
  Dim MaxBaudRate As BaudrateType
  
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set r = ps.ProjectData
  
  If Pagename = "BRG" Then
  '-------------------------------------------------------------------------
  'Page BRG (Control)
  '-------------------------------------------------------------------------
    Select Case Controlname
        Case "chkBRGCon"
            If v(1) = 0 Then
                r.l(BRG_PREFIX & "BCON_BRPRE") = &H0
                r.l(BRG_PREFIX & "FDSTEP_STEP") = &H0
                r.v(BRG_PREFIX & "FDCON_FDEN") = 0
                r.v("Uart_Baudrate") = GetUartMaxBaudrate(r)
                Call UpdateUartBaudrate(r)
            End If
            r.v(BRG_PREFIX & "BCON_R") = v(1)
        
        Case "chkNormalDivider"
            r.v(BRG_PREFIX & "FDCON_FDM") = v(1)
            If v(1) = 0 Then
                Call ResetBRG(r)
                r.v("BRG_Interrupt_Select") = 0
            End If
        
        Case "chkStartTimer"
            r.v(BRG_PREFIX & "FDCON_FDEN") = v(1)
            
'        Case "chkEnableNDOV"
'        If v(1) = 1 Then
'            If r.v(CORE_PREFIX & "IEN0_ET2") = 1 Then
'                If Ask(MSG_SELECT_BRG_INTERRUPT_1) Then
'                    r.v("BRG_Interrupt_Select") = v(1)
'                End If
'            Else
'                If Ask(MSG_SELECT_BRG_INTERRUPT_2) Then
'                    r.v("BRG_Interrupt_Select") = v(1)
'                End If
'            End If
'        Else
'          r.v("BRG_Interrupt_Select") = 0
'        End If
        Case "chkEnableNDOV"
            If v(1) = 1 Then
              'MsgBox MSG_SELECT_XINTR5_INTERRUPTS
              If (r.v("INTR_MSGEN") = 1) Then
                 SharedInt = MsgBox(MSG_SELECT_XINTR5_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                 If (SharedInt = 6) Then
                    r.v("INTR_MSGEN") = 1
                 ElseIf (SharedInt = 7) Then
                    r.v("INTR_MSGEN") = 0
                    SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                 End If
              End If
              r.v("BRG_Interrupt_Select") = v(1)
            Else
              r.v("BRG_Interrupt_Select") = 0
            End If
        Case "chkFracDivide"
            r.v(BRG_PREFIX & "FDCON_FDEN") = v(1)
            If Not v(1) Then
                r.l(BRG_PREFIX & "FDSTEP_STEP") = &H0
            End If
             Call UpdateUartBaudrate(r)
        Case "cboPreScaler"
            Select Case v(1)
                Case "fDIV = fPCLK"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H0
                    
                Case "fDIV = fPCLK/2"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H1
                    
                Case "fDIV = fPCLK/4"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H2
                    
                Case "fDIV = fPCLK/8"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H3
                    
                Case "fDIV = fPCLK/16"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H4
                    
                Case "fDIV = fPCLK/32"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H5
                    
            End Select
            r.v("Uart_Baudrate") = Format(GetUartMaxBaudrate(r), "0.0000")
            Call UpdateUartBaudrate(r)
        
        Case "cboTmrPreScaler"
            Select Case v(1)
                Case "fDIV = fPCLK"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H0
                    
                Case "fDIV = fPCLK/2"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H1
                    
                Case "fDIV = fPCLK/4"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H2
                    
                Case "fDIV = fPCLK/8"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H3
                    
                Case "fDIV = fPCLK/16"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H4
                    
                Case "fDIV = fPCLK/32"
                    r.l(BRG_PREFIX & "BCON_BRPRE") = &H5
                    
            End Select
        
        Case "txtFracDivide"
            If (IsInputFormatOK(v(1), "H2", c("lblFracDivide").Caption, 0, 255)) Then
               r.sfr(BRG_PREFIX & "FDSTEP") = v(1)
               'r.d("Uart_Baudrate") = Format(GetActualUartBaudrate(r), "0.0000")
             Else
                 Exit Function
             End If
        
        Case "txtReqBaud"
            Baudrate = ConvertBaudrate(r.v("Uart_Baudrate"))
            MaxBaudRate = ConvertBaudrate(GetUartMaxBaudrate(r))
            
            If (IsInputFormatOK(v(1), "F", c("lblReqBaud").Caption)) Then
                If (IsInputValueOK(Format(v(1) * Baudrate.dDivider, "0.0000"), _
                   Format(GetUartMinBaudrate(r), "0.0000"), _
                   Format(Format(GetUartMaxBaudrate(r) / MaxBaudRate.dDivider, "0.0000") _
                    * MaxBaudRate.dDivider, "0.0000"))) Then
                    r.v("Uart_Baudrate") = v(1) * Baudrate.dDivider ' convert to baud
                    Call UpdateUartBaudrate(r)
                Else
                    Exit Function
                End If
            Else
                Exit Function
            End If
            
        Case "txtReloadValue"
            If (IsInputFormatOK(v(1), "H2", c("lblReloadValue").Caption, 0, 255)) Then
               r.sfr(BRG_PREFIX & "BG") = v(1)
               r.d("Uart_Baudrate") = Format(GetActualUartBaudrate(r), "0.0000")
             Else
                 Exit Function
             End If
             
       Case "txtTmrReloadValue"
            If (IsInputFormatOK(v(1), "H2", "STEP", 0, 255)) Then
               r.l(BRG_PREFIX & "FDSTEP_STEP") = v(1)
             Else
                 Exit Function
             End If
        
        
    End Select
    
  '-------------------------------------------------------------------------
  'Page BRG (Show)
  '-------------------------------------------------------------------------
  
  If r.v(BRG_PREFIX & "FDCON_BGS") = 1 Or Not (GetUartMode(r) = 1 Or GetUartMode(r) = 3) Then
    '-------------------------------------------------------------------
    ' Frame : "Normal Divider Mode"
    '-------------------------------------------------------------------
    With c("chkNormalDivider")
        bDisabled = CheckDisabledUart(r)
        .Enabled = Not bDisabled
        .Value = r.l(BRG_PREFIX & "FDCON_FDM")
    End With
    
    With c("chkStartTimer")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
        .Value = r.l(BRG_PREFIX & "FDCON_FDEN")
    End With
    
    With e("chkStartTimer")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Normal divider mode is not selected")
        End If
    End With
    
    With c("lblTmrPreScaler")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
    End With
    
    With c("cboTmrPreScaler")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
        .ListIndex = r.l(BRG_PREFIX & "BCON_BRPRE")
    End With
    
    With e("cboTmrPreScaler")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Normal divider mode is not selected")
        End If
    End With
    
    With c("txtTmrReloadValue")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
            .Text = CALCULATION_NOT_POSSIBLE
        Else
            .Text = FormatTextOutput(r.l(BRG_PREFIX & "FDSTEP_STEP"), "H2")
        End If
    End With
    
    With e("txtTmrReloadValue")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Normal divider mode is not selected")
        End If
    End With
    
    With c("lblTmrOverflowtxt")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
            .Caption = CALCULATION_NOT_POSSIBLE
        Else
            .Caption = GetBRGTmrOverflow(r)
        End If
    End With
    
    With c("lblTmrReloadValue")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
    End With
        
    With c("lblTmrOverflow")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
    End With
    
    With c("chkEnableNDOV")
        bDisabled = (r.v(BRG_PREFIX & "FDCON_FDM") = 0)
        .Enabled = Not bDisabled
        .Value = r.v("BRG_Interrupt_Select")
    End With
    
    With e("chkEnableNDOV")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Normal divider mode is not selected")
        End If
    End With
    
    End If
    
    bDisabled = Not ((r.v(BRG_PREFIX & "FDCON_BGS") = 1) Or Not (GetUartMode(r) = 1 Or GetUartMode(r) = 3))
    
    With c("fraNormalDivider")
        .Visible = Not bDisabled
    End With
    
    With c("fraStartTimer")
        .Visible = Not bDisabled
    End With
    
    With c("frmTmrSettings")
        .Visible = Not bDisabled
    End With
    
    With c("frmTmrInterrupt")
        .Visible = Not bDisabled
    End With
    
  If r.v(BRG_PREFIX & "FDCON_BGS") = 0 And Not (GetUartMode(r) = 0 Or GetUartMode(r) = 2) Then
    '-------------------------------------------------------------------
    ' Frame : "BRG run control"
    '-------------------------------------------------------------------
    With c("chkBRGCon")
        bDisabled = (CheckDisabledUart(r) Or (GetUartMode(r) = 0) Or (GetUartMode(r) = 2))
        .Enabled = Not bDisabled
        .Value = r.l(BRG_PREFIX & "BCON_R")
    End With
    
    With e("chkBRGCon")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If GetUartMode(r) = 2 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 2 is selected")
            ElseIf CheckDisabledUart(r) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the UART is not used as transmitter or receiver")
            Else
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Mode 0 is selected")
            End If
        End If
    End With
    
    
    
    '-------------------------------------------------------------------
    ' Frame : "Fractional Divider"
    '-------------------------------------------------------------------
    With c("chkFracDivide")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        .Value = r.l(BRG_PREFIX & "FDCON_FDEN")
    End With
    
    With e("chkFracDivide")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Baudrate generator is not enabled")
        End If
    End With
    
    With c("txtFracDivide")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0) Or (r.v(BRG_PREFIX & "FDCON_FDEN") = 0)
        .Enabled = False
        If (bDisabled) Then
            .Text = CALCULATION_NOT_POSSIBLE
        Else
            .Text = FormatTextOutput(r.l(BRG_PREFIX & "FDSTEP_STEP"), "H2")
        End If
    End With
    
    With e("txtFracDivide")
        If (bDisabled) Then
           .DisabledTooltipText = FormatDisabledToolTip("This value will be set by DAvE if the fractional divider is enabled.")
        End If
    End With
    With c("lblFracDivide")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0) Or (r.v(BRG_PREFIX & "FDCON_FDEN") = 0)
        .Enabled = Not bDisabled
    End With
    
    '-------------------------------------------------------------------
    ' Frame : "BRG settings"
    '-------------------------------------------------------------------
    With c("lblPreScaler")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
    End With
    
    With c("cboPreScaler")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        .ListIndex = r.l(BRG_PREFIX & "BCON_BRPRE")
    End With
    
    With e("cboPreScaler")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Baudrate generator is not enabled")
        End If
    End With
    
    
    '-------------------------------------------------------------------
    ' txtReqBaud (show)
    '-------------------------------------------------------------------
    With c("txtReqBaud")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
            .Text = CALCULATION_NOT_POSSIBLE
            c("lblReqBaud").Caption = "Required baud rate [baud]"
        Else
            Baudrate = ConvertBaudrate(r.d("Uart_Baudrate"))
            .Text = Format(Baudrate.dValue, "0.0000")
            c("lblReqBaud").Caption = "Required baud rate [" & Baudrate.sUnit & "]"
            
        End If
        
    End With
        
    With e("txtReqBaud")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Baudrate generator is not enabled")
        End If
    End With
    
    '-------------------------------------------------------------------
    ' lblReqBaud (show)
    '-------------------------------------------------------------------
    With c("lblReqBaud")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
    End With
    
    '-------------------------------------------------------------------
    ' lblActualBaudLabel (show)
    '-------------------------------------------------------------------
    With c("lblActualBaudLabel")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
    End With
    
    With c("lblActualBaud")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
            .Caption = CALCULATION_NOT_POSSIBLE
            c("lblActualBaudLabel").Caption = "Actual baud rate [baud]"
        Else
            Baudrate = ConvertBaudrate(GetActualUartBaudrate(r))
            .Caption = Format(Baudrate.dValue, "0.0000")
            c("lblActualBaudLabel").Caption = "Actual baud rate [" & Baudrate.sUnit & "]"
        End If
    End With
    
    With c("lblMinBaud")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
            .Caption = CALCULATION_NOT_POSSIBLE
            c("lblMinBaudLabel").Caption = "Min. baud rate [baud]"
        Else
            Baudrate = ConvertBaudrate(GetUartMinBaudrate(r))
            .Caption = Format(Baudrate.dValue, "0.0000")
            c("lblMinBaudLabel").Caption = "Min. baud rate [" & Baudrate.sUnit & "]"
        End If
      
    End With
    
    With c("lblMinBaudLabel")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
    End With
    
    
    With c("lblMaxBaud")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
            .Caption = CALCULATION_NOT_POSSIBLE
            c("lblMaxBaudLabel").Caption = "Max. baud rate [baud]"
        Else
            Baudrate = ConvertBaudrate(GetUartMaxBaudrate(r))
            .Caption = Format(Baudrate.dValue, "0.0000")
            c("lblMaxBaudLabel").Caption = "Max. baud rate [" & Baudrate.sUnit & "]"
        End If
        
    End With
    
    With c("lblMaxBaudLabel")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
    End With
            
    With c("txtReloadValue")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
            .Text = CALCULATION_NOT_POSSIBLE
        Else
             UpdateUartBaudrate (r) 'ashish
            .Text = FormatTextOutput(r.l(BRG_PREFIX & "BG_BR_VALUE"), "H2")
        End If
    End With
    
    With e("txtReloadValue")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("Baudrate generator is not enabled")
        End If
    End With
    
    With c("lblReloadValue")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
    End With
       
    With c("lblDeviation")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
        
        If (bDisabled) Then
          .Caption = CALCULATION_NOT_POSSIBLE
        Else
            .Caption = Format((Format(GetActualUartBaudrate(r), "0.0000") - r.d("Uart_Baudrate")) / _
                             r.d("Uart_Baudrate") * 100, "0.0000")
        End If
      
    End With
    
    With c("lblDeviationLabel")
        bDisabled = (r.v(BRG_PREFIX & "BCON_R") = 0)
        .Enabled = Not bDisabled
    End With
    
  End If
    
  End If
    
  OnShowPageBRG = True
  Exit Function
     
OnShowError:
  errorbox "ERROR: Show BRG", Err.Description
End Function

'*****************************************************************************
'Description:
'             This function resets the BRG module.
'
'
'-----------------------------------------------------------------------------
'Parameters:
'           r    Project Data
'
'-----------------------------------------------------------------------------
'Returns:
'           Nothing
'
'*****************************************************************************
  
Private Sub ResetBRG(r As ProjectData)
On Error Resume Next
    
   r.v(BRG_PREFIX & "BCON_R") = 0
   r.sfr(BRG_PREFIX & "BG") = 0
   r.v(BRG_PREFIX & "FDCON_FDM") = 0
   r.v(BRG_PREFIX & "FDCON_FDEN") = 0
   r.sfr(BRG_PREFIX & "FDSTEP") = 0
   r.l(BRG_PREFIX & "BCON_BRPRE") = 0
   r.v("Uart_Baudrate") = GetUartMaxBaudrate(r)
End Sub
