Attribute VB_Name = "Module1"
'#Uses "Format.inc"
'#Uses "Const.inc"

Option Explicit
'*****************************************************************************
' Module:
'        GLOBAL.INC
'-----------------------------------------------------------------------------
' Description:
'        This module contains global functions that can
'        be used in all other script files.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil          06-10-04    initial version
'        0.2  Sheshadri      08-10-04    added GetSystemClock, GetCoreClock
'        0.3  Sunil          25-10-04    added BaudrateType datatype,
'                                        function ConvertBaudrate
'        0.4  Sunil          02-12-04    Bug in ConvertBaudrate is fixed.
'                                        (dDivider = dDivider * 10001 chaged to dDivider = dDivider * 1000)
'        0.5  Sunil          29-12-04    Module Prefix is added to register names.
'        0.6  Sunil          04-02-05    Function "GetValueNDIV" added
'
'
'*****************************************************************************

'*****************************************************************************
' Constants
'*****************************************************************************
' User defined data type for baudrate
Type BaudrateType
  dValue As Double   ' Baudrate value
  sUnit As String    ' unit (kBaud / MBaud)
  dDivider As Double ' divider factor referring to kBaud / MBaud
End Type

Global Const RATIO_FSYS_T01 = 2 ' RATIO_FSYS_T01 = fSYS / T01CounterRate
Global Const TXT_US = Chr(181) & "s" ' micro seconds

'*****************************************************************************
'Description:
'       The function returns the current system frequency fSYS in MHz
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        system frequency in MHz
'*****************************************************************************
Function GetSystemClock(r As ProjectData) As Variant
    On Error GoTo GetSystemClockError

    Select Case (r.v("PLL_CTRL"))
        Case 1
            GetSystemClock = r.v("MAIN_OSC")
        Case 2
            GetSystemClock = 150 / 2
        Case 3
            GetSystemClock = r.v("MAIN_OSC") / 2
        Case 4
            GetSystemClock = (r.v("MAIN_OSC") * GetValueNDIV(r)) / 2
    End Select
    
GetSystemClockError:
    Exit Function
End Function

'*****************************************************************************
'Description:
'         This function returns the Core/peripheral clock [MHz].
'-----------------------------------------------------------------------------
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        Core/peripheral frequency in MHz
'*****************************************************************************
Function GetCoreClock(r As ProjectData) As Variant
    On Error GoTo GetCoreClockError
      
      GetCoreClock = GetSystemClock(r) / ((2 ^ r.d(SCU_PREFIX & "CMCON_CLKREL")) * 3)
      
      Exit Function
    
GetCoreClockError:
    ErrorBox "Error"
End Function

'*****************************************************************************
'Description:
'       This function checks if the passed register name is defined in the
'       register file. If ok, the same name is returned.
'       If not ok, an error message is displayed an a #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'        sSfrName   Register Name
'-----------------------------------------------------------------------------
'Returns:
'        okay:      Register Name
'        error:     "#error..."
'*****************************************************************************
Function SfrName(r As ProjectData, sSfrName As String) As String
    On Error Resume Next

    SfrName = ""
    If (Not r.Regs.SfrFind(sSfrName)) Then GoTo RegError
    SfrName = sSfrName
    Exit Function

RegError:
    SfrName = RegErrMsg(sSfrName)
End Function

'*****************************************************************************
'Description:
'       This function checks if the passed register name is defined in the
'       register file. If ok, its address is returned in string format.
'       If not ok, an error message is displayed an a #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'        sSfrName   Register Name
'-----------------------------------------------------------------------------
'Returns:
'        okay:      Register Address
'        error:     "#error..."
'*****************************************************************************
Function SfrAddress(r As ProjectData, sSfrName As String) As String
    On Error Resume Next

    SfrAddress = ""
    If (Not r.Regs.SfrFind(sSfrName)) Then GoTo RegError
    SfrAddress = r.Regs.SfrAdress
    Exit Function

RegError:
    SfrAddress = RegErrMsg(sSfrName)
End Function

'*****************************************************************************
'Description:
'       This function checks if the passed SFR bit is defined in the
'       register file and if the register is bit-addressable.
'       If ok, the bit name is returned.
'       If not ok, an error message is displayed an a #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'        sBitVarName    SFR bit as project variable name, e.g. "ADCON0_BSY"
'-----------------------------------------------------------------------------
'Returns:
'                       SFR bit as C register name, e.g. "BSY"
'*****************************************************************************
Function BitName(r As ProjectData, sBitVarName As String) As String
    On Error Resume Next

    Dim sSfrName As String
    Dim lAddress As Long

    BitName = ""
    If (Not r.Regs.BitFind(sBitVarName)) Then GoTo RegError
    sSfrName = r.Regs.BitSfrName
    r.Regs.SfrIndex = r.Regs.BitSfrIndex
    lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))

    If (lAddress > &HFF) Or ((lAddress Mod 8) <> 0) Then GoTo RegError

    BitName = Mid(r.Regs.BitName, Len(sSfrName) + 2)
    Exit Function

RegError:
    BitName = RegErrMsg(sBitVarName)
End Function

'*****************************************************************************
'Description:
'       This function returns a bit mask with all passed bits set to '1'.
'       If not all of the bits are defined in the register file, an error message
'       will be displayed.
'-----------------------------------------------------------------------------
'Parameters:
'       sSfrName    SFR name, e.g. "ADCON0"
'       sBit0..7    variable number of SFR bits of SFR sSfrName, e.g. "BSY", "ADM"
'-----------------------------------------------------------------------------
'Returns:
'       bit mask    e.g. &H18 for "ADCON0", "BSY", "ADM"
'*****************************************************************************
Function BitMask(r As ProjectData, sSfrName As String, Optional sBit0 As String, Optional sBit1 As String = "", _
    Optional sBit2 As String = "", Optional sBit3 As String = "", Optional sBit4 As String = "", _
    Optional sBit5 As String = "", Optional sBit6 As String = "", Optional sBit7 As String = "") As Long
    On Error Resume Next

    Dim sBitName    As String
    Dim s           As String
    Dim iBit        As Long

    BitMask = 0
    For iBit = 0 To 7
        If (iBit = 0) Then sBitName = sBit0
        If (iBit = 1) Then sBitName = sBit1
        If (iBit = 2) Then sBitName = sBit2
        If (iBit = 3) Then sBitName = sBit3
        If (iBit = 4) Then sBitName = sBit4
        If (iBit = 5) Then sBitName = sBit5
        If (iBit = 6) Then sBitName = sBit6
        If (iBit = 7) Then sBitName = sBit7

        If (sBitName <> "") Then
            s = sSfrName & "_" & sBitName
            If (Not r.Regs.BitFind(s)) Then GoTo RegError
            BitMask = BitMask + (2 ^ r.Regs.BitPos)
        End If
    Next iBit
    Exit Function

RegError:
    RegErrMsg (s)
End Function

'*****************************************************************************
'Description:
'       This function generates a C-Statement that sets or clears a SFR bit.
'       The best addressing method is always used:
'           bit-addressable SFRs        e.g.    BSY = 1;
'           non-bit-addressable SFRs    e.g.    ADCON0 |= 0x10;
'
'       If the SFR bit is not defined in the register file, an error message
'       is displayed and an #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'       sBitVarName           SFR bit as project variable name, e.g. "ADCON0_BSY"
'       iBitVal               value to be set: 0...1
'       bTerminated           True:  statement with ';' and appended comment
'                                    (used for functions)
'                             False: statement without ';' and without comment
'                                    (used for macros)
'-----------------------------------------------------------------------------
'Returns:
'       C Statement           e.g. "ADCON0 &= ~(ubyte)0x10;  //   clear BSY"
'*****************************************************************************
Function SfrBitStatement(r As ProjectData, sBitVarName As String, iBitVal As Integer, Optional bTerminated As Boolean = True) As String
    On Error Resume Next

    Dim sBitName    As String
    Dim sSfrName    As String
    Dim sTmp        As String
    Dim sAppendix   As String
    Dim lAddress    As Long

    SfrBitStatement = ""
    If (Not r.Regs.BitFind(sBitVarName)) Then GoTo RegError
    sSfrName = r.Regs.BitSfrName
    r.Regs.SfrIndex = r.Regs.BitSfrIndex
    lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))
    sBitName = Mid(r.Regs.BitName, Len(sSfrName) + 2)

    If (bTerminated) Then
        sAppendix = ";"
    Else
        sAppendix = ""
    End If

    If (lAddress <= &HFF) And ((lAddress Mod 8) = 0) Then
        ' SFR is bit-addressable

        sTmp = sBitName & " = " & iBitVal & sAppendix
    Else
        ' SFR is not bit-addressable

        If (iBitVal = 1) Then
            sTmp = sSfrName & " |= " & FormatTextOutput(2 ^ r.Regs.BitPos, "H2") & sAppendix
        Else
            sTmp = sSfrName & " &= ~(ubyte)" & FormatTextOutput(2 ^ r.Regs.BitPos, "H2") & sAppendix
        End If

        If (bTerminated) Then
            If (iBitVal = 1) Then
                sTmp = FillRight(19, sTmp) & " //   set   " & sBitName
            Else
                sTmp = FillRight(19, sTmp) & " //   clear " & sBitName
            End If
        End If
    End If

    SfrBitStatement = sTmp

    Exit Function

RegError:
    SfrBitStatement = RegErrMsg(sBitVarName)
End Function

'*****************************************************************************
'Description:
'       This function generates a C-Statement that tests a SFR bit.
'       The best addressing method is always used:
'           bit-addressable SFRs        e.g.    BSY
'           non-bit-addressable SFRs    e.g.    (ADCON0 & 0x10) != 0
'
'       If the SFR bit is not defined in the register file, an error message
'       is displayed and an #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'       sBitVarName           SFR bit as project variable name, e.g. "ADCON0_BSY"
'-----------------------------------------------------------------------------
'Returns:
'       C Statement           e.g. "((ADCON0 & 0x10) != 0)"
'*****************************************************************************
Function SfrBitTest(r As ProjectData, sBitVarName As String) As String
    On Error Resume Next

    Dim sBitName    As String
    Dim sSfrName    As String
    Dim lAddress    As Long

    SfrBitTest = ""
    If (Not r.Regs.BitFind(sBitVarName)) Then GoTo RegError
    sSfrName = r.Regs.BitSfrName
    r.Regs.SfrIndex = r.Regs.BitSfrIndex
    lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))

    If (lAddress <= &HFF) And ((lAddress Mod 8) = 0) Then
        ' SFR is bit-addressable

        sBitName = Mid(r.Regs.BitName, Len(sSfrName) + 2)
        SfrBitTest = sBitName
    Else
        ' SFR is not bit-addressable

        SfrBitTest = "((" & sSfrName & " & " & FormatTextOutput(2 ^ r.Regs.BitPos, "H2") & ") != 0)"
    End If
    Exit Function

RegError:
    SfrBitTest = RegErrMsg(sBitVarName)
End Function

'*****************************************************************************
'Description:
'       This function returns the bit field position for the passed bit field.
'       If the bit field is not defined in the register file, an error message
'       will be displayed.
'-----------------------------------------------------------------------------
'Parameters:
'       sBFldVarName   bit field as project variable name, e.g. "ADCON1_ADSTC"
'-----------------------------------------------------------------------------
'Returns:
'       bit field position   e.g. 2 for "ADCON1_ADSTC"
'*****************************************************************************
Function BFldPos(r As ProjectData, sBFldVarName As String) As Long
    On Error Resume Next

    BFldPos = 0
    If (Not r.Regs.BitFind(sBFldVarName)) Then GoTo RegError
    BFldPos = r.Regs.BitPos - r.Regs.BitWidth + 1
    Exit Function

RegError:
    RegErrMsg (sBFldVarName)
End Function

'*****************************************************************************
'Description:
'       todo: workaround for missing information in the register file
'       This functions checks if the SFR is located in the mapped SFR area.
'-----------------------------------------------------------------------------
'Parameters:
'       sSfrName    SFR name, e.g. "ADCON0"
'-----------------------------------------------------------------------------
'Returns:
'       True    SFR is located in the mapped SFR area (accessible only when RMAP=1)
'       False   SFR is not located in the mapped SFR area
'*****************************************************************************
Function IsMappedSfr(sSfrName As String) As Boolean 'todo workaround for missing information in the register file
    On Error Resume Next

    IsMappedSfr = False
    Select Case sSfrName
        ' WDT registers
        Case WDT_PREFIX & "WDTCON"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTREL"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTWINB"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTL"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTH"
                IsMappedSfr = True
        ' FLASH registers
        Case FLASH_PREFIX & "FSER0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EESER0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCON2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCSP1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EESER1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCSP0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECSP"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCON0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCS0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECS"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCON1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FPTCR"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCS1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECPP0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FPTPS0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FSER1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECPP1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FPTPS1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCSP2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FTKAS"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCS2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCPP0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FSER2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCPP1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FTCON"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FTRL"
                IsMappedSfr = True
        Case "FLASH_PAGE"
                IsMappedSfr = True
                
        ' OCDS registers
        Case OCDS_PREFIX & "MMCR2"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMCR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMSR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMBPCR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMICR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMDR_R"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMDR_T"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPSR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_0L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_0H"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_1L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_1H"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_2L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_2H"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_3L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_3H"
                IsMappedSfr = True
    End Select
    
End Function

'*****************************************************************************
'Description:
'       This function generates the declaration of a SFR.
'       The used syntax depends on the currently selected compiler.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       SFR name, e.g. "ADCON0"
'       sAddr       SFR address, e.g. "0xD8"
'       sComment    comment, e.g. "// ADC control register"
'-----------------------------------------------------------------------------
'Returns:
'       SFR declaration     e.g. "sfr ADCON0 = 0xD8;    // ADC control register"
'*****************************************************************************
Function SfrDeclaration(r As ProjectData, sName As String, sAddr As String, sComment As String) As String
    On Error Resume Next

    If (r.v("MAIN_COMPILER") = "Keil") Then
        SfrDeclaration = "sfr " & FillRight(10, sName) & " = " & sAddr & ";    " & sComment
     ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        SfrDeclaration = "sfr at " & sAddr & " " & FillRight(10, sName) & "  ; " & sComment
     ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
        SfrDeclaration = "__sfr __no_init volatile unsigned char  " & FillRight(14,sName) & " @ (" & sAddr & ")" & ";" & sComment
    Else
        SfrDeclaration = "_sfrbyte " & FillRight(10, sName) & " _at(" & sAddr & ");    " & sComment
    End If
End Function

'*****************************************************************************
'Description:
'       This function generates the declaration of a SFR bit
'       (only valid for bit-addressable SFRs)
'       The used syntax depends on the currently selected compiler.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       SFR bit name, e.g. "BSY"
'       sAddr       SFR bit address, e.g. "0xDB"
'       sComment    comment, e.g. "// busy flag"
'-----------------------------------------------------------------------------
'Returns:
'       SFR bit declaration     e.g. "sbit BSY = 0xDB;    // busy flag"
'*****************************************************************************
Function SfrBitDeclaration(r As ProjectData, sName As String, sAddr As String, sComment As String) As String
    On Error Resume Next

    If (r.v("MAIN_COMPILER") = "Keil") Then
        SfrBitDeclaration = "sbit " & FillRight(10, Identifier(r, sName)) & " = " & sAddr & ";    " & sComment
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        SfrBitDeclaration = "sbit at  " & sAddr & "  " & FillRight(10, Identifier(r, sName)) & "  ; " & sComment
    ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
        SfrBitDeclaration = "__bit __no_init volatile bool  " & FillRight(10, Identifier(r, sName)) & "@" & "  (" & sAddr & ")" & ";" & sComment
    Else
        SfrBitDeclaration = "_sfrbit " & FillRight(10, Identifier(r, sName)) & " _at(" & sAddr & ");    " & sComment
    End If
End Function

'*****************************************************************************
'Description:
'       This function converts a SFR name or SFR bit name into a valid
'       identifier for the C-compilers. The first occurence of character '/'
'       is replaced by the character '_'.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       input name, e.g. "C/T2"
'-----------------------------------------------------------------------------
'Returns:
'       identifier, e.g. "C_T2"
'*****************************************************************************
Function Identifier(r As ProjectData, sName As String) As String
    On Error Resume Next

    Dim iPos As Integer
    Dim iLen As Integer

    Identifier = sName
    iPos = InStr(sName, "/")
    If (iPos > 0) Then
        iLen = Len(sName)
        Identifier = Left(sName, iPos - 1) & "_" & Right(sName, iLen - iPos)
    End If
End Function

'*****************************************************************************
'Description:
'       This function generates the declaration of an interrupt service routine.
'       The used syntax depends on the currently selected compiler.
'-----------------------------------------------------------------------------
'Parameters:
'       sFuncName           function name, e.g. "XXX_viDummy"
'       sIntNumberName      interrupt number, e.g. "5"
'-----------------------------------------------------------------------------
'Returns:
'       ISR declaration     e.g. "void XXX_viDummy 5"
'*****************************************************************************
Function IsrDeclaration(r As ProjectData, sFuncName As String, sIntNumberName As String) As String
    On Error Resume Next
    
    Select Case (r.v("MAIN_COMPILER"))
        Case "Keil"
            IsrDeclaration = "void " & sFuncName & "(void) interrupt " & sIntNumberName
        Case "SDCC"
            IsrDeclaration = "void " & sFuncName & "(void) interrupt " & sIntNumberName
        Case "IAR"
            IsrDeclaration = "#pragma vector = " & sIntNumberName & vbCrLf & _
                             "__interrupt void " & sFuncName & "(void)"
        Case Else
            IsrDeclaration = "ERROR"
    End Select
End Function

'*****************************************************************************
'Description:
'       Calculation of an interrupt number (used for ISR declarations)
'-----------------------------------------------------------------------------
'Parameters:
'       iIntVectorAddress   interrupt vector address, e.g. &H0B
'-----------------------------------------------------------------------------
'Returns:
'       interrupt number    e.g. 1 for address &H0B, 2 for address &H13 etc.
'*****************************************************************************
Function IntNumber(r As ProjectData, iIntVectorAddress As Integer) As Variant
'    IntNumber = (iIntVectorAddress - 3) \ 8
  If (r.v("MAIN_COMPILER") = "Keil") Or (r.v("MAIN_COMPILER") = "SDCC") Then
    IntNumber = (iIntVectorAddress - 3) \ 8
  ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
    IntNumber = FormatTextOutput(iIntVectorAddress, "H4")
  End If

End Function

'*****************************************************************************
'Description:
'       Display of an error message in case of a failed SFR/bit name check.
'       A #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       name of the SFR or SFR-bit, e.g. "SYSSSCON"
'-----------------------------------------------------------------------------
'Returns:
'       error statement     e.g. "#error ... SYSSSCON ..."
'*****************************************************************************
Private Function RegErrMsg(sName As String) As String
    On Error Resume Next
    Dim s As String

    s = "Internal Error: Code generation for SFR/Bit '" & sName & "' not possible"
    Call ErrorBox(s)
    RegErrMsg = vbCrLf & "#error " & s & vbCrLf
End Function

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

  dTempValue = dValue
  sUnit = "baud"
  dDivider = 1

  If (dTempValue >= 1000) Then
    dTempValue = (dTempValue / 1000)
    sUnit = "kbaud"
    dDivider = dDivider * 1000

   ' This code is intentionally commented out.
   ' If (dTempValue >= 1000) Then
   '   dTempValue = (dTempValue / 1000)
   '   sUnit = "Mbaud"
   '   dDivider = dDivider * 1000
   ' End If
  
  End If

  ConvertBaudrate.dValue = dTempValue
  ConvertBaudrate.sUnit = sUnit
  ConvertBaudrate.dDivider = dDivider

  Exit Function

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

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

  IsInputValueOK = False

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

End Function

'*****************************************************************************
'Description:
'        This function generates the declaration for the interrupt service
'        routine in the generated code.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ISRName          the name of the service request routine (e.g SSC0_viTx)
'        VectorName       the name of the service request routine vector (e.g TX_INT)
'-----------------------------------------------------------------------------
'Returns:
'        Service request declaration
'*****************************************************************************
Function GenerateIsrDeclaration(r, ISRName As String, VectorName As Variant) As String
    On Error Resume Next
    
    If (InStr(r.v("MAIN_COMPILER"), "SDCC")) Then
        GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
    End If

    If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
        GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
    End If

    If (InStr(r.v("MAIN_COMPILER"), "IAR")) Then
        GenerateIsrDeclaration = "#pragma vector = " & VectorName & vbCrLf & _
                                 "__interrupt void " & ISRName & "(void)"

    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the NDIV value selected.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'-----------------------------------------------------------------------------
'Returns:
'        NDIV value
'*****************************************************************************
Function GetValueNDIV(r As ProjectData) As Integer
    On Error Resume Next
    
    GetValueNDIV = 2
    
    If r.v(SCU_PREFIX & "PLL_CON_NDIV") < 8 Then
        GetValueNDIV = r.v(SCU_PREFIX & "PLL_CON_NDIV") + 14
    Else
        Select Case r.v(SCU_PREFIX & "PLL_CON_NDIV")
            Case 8
                GetValueNDIV = 24
            Case 9
                GetValueNDIV = 28
            Case 10
                GetValueNDIV = 30
            Case 11
                GetValueNDIV = 32
            Case 12
                GetValueNDIV = 40
            Case 13
                GetValueNDIV = 42
            Case 14
                GetValueNDIV = 45
            Case 15
                GetValueNDIV = 50
        End Select
    End If
    

End Function
Function GetValueCOREL(r As ProjectData) As Integer
    On Error Resume Next
    GetValueCOREL = 2
    Select Case r.v(SCU_PREFIX & "COCON_COREL")
        Case 0
            GetValueCOREL = 2
        Case 1
            GetValueCOREL = 3
        Case 2
            GetValueCOREL = 4
        Case 3
            GetValueCOREL = 5
        Case 4
            GetValueCOREL = 6
        Case 5
            GetValueCOREL = 8
        Case 6
            GetValueCOREL = 9
        Case 7
            GetValueCOREL = 10
        Case 8
            GetValueCOREL = 12
        Case 9
            GetValueCOREL = 16
        Case 10
            GetValueCOREL = 18
        Case 11
            GetValueCOREL = 20
        Case 12
            GetValueCOREL = 24
        Case 13
            GetValueCOREL = 32
        Case 14
            GetValueCOREL = 36
        Case 15
            GetValueCOREL = 40
    End Select
End Function

