Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\classes\FunctionClass.cls
Option Explicit
'*****************************************************************************
' Module:
'        MAIN
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        MAIN dialog script and in the MAIN codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        0.1  als            25-10-04    final version
'        0.2 radhish         31-12-04    Range of system freguency updated
'
'*****************************************************************************


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

' use this constant for Keil Startup
Global Const MSG_KEIL_STARTUP = "Disabling of startup generation would affect by not generatig start up file." & vbCrLf & _
                                "So changes in External OSC Configurations will not be affected and inturn " & vbCrLf & _
                                "this might affect functioning of Modules." & vbCrLf & _
                                "Do you want to continue?"

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

RefreshFunctionDescriptions = False

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sIndex As String
  Dim sPrefix As String

  sIndex = "MAIN"

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

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


  '------------------------------------------------------------------------------------------------
  ' Description for MAIN_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = "This function initializes the microcontroller. " & _
                 "It is assumed that the SFRs are in their reset state."
  sReturnValue = "None"
  If (Not func.AddFunction("MAIN_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for SHINT_Init
  '------------------------------------------------------------------------------------------------
  sDescription = "This function initializes the shared interrupts. "
  sReturnValue = "None"
  If (Not func.AddFunction("SHINT_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function
  '------------------------------------------------------------------------------------------------
  ' Description for Main_viXINTR5Isr
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the shared interrupt node XINTR5. " & _
                 "XINTR5 ISR Node is shared by Timer 2, UART Fractional Divider(BRG Interrupt),MultiCAN Node 0 and LIN. " & _
                 "Depending on the selected module interrupt it is called." & vbCrLf & _
                 "T2 - Depending on the selected operating mode it is called when TF2 is set by " & _
                 "an overflow or underflow of the timer 2 register or when EXF2 " & _
                 "is set by a negative transition on T2EX." & vbCrLf & _
                 "UART - It is called after the BRG timer overflows and sets the NDOV bit." & vbCrLf & _
                 "CAN -  It is called for the  Service Request Node 0 of the MultiCAN module." & vbCrLf & _
                  TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If ((r.v("T2_USE_IsrTmr") = 1) And (r.v("BRG_Interrupt_Select") = 1)) Then
     If (Not func.AddFunction("T2_NAME_IsrTmr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf (r.v("T2_USE_IsrTmr") = 1) Then
      If (Not func.AddFunction("T2_NAME_IsrTmr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf (r.v("BRG_Interrupt_Select") = 1) Then
      If (Not func.AddFunction("BRG_NAME_Isr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf ((r.v("CAN_USE_SRN0") = 1)) Then
      If (Not func.AddFunction("CAN_NAME_SRN0", "void", sDescription, sReturnValue, 2)) Then Exit Function
  End If
  '------------------------------------------------------------------------------------------------
  ' Description for Main_vXINTR6Isr
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the shared interrupt node XINTR6. " & _
                "XINTR6 ISR Node is shared by ADC[1:0] and MultiCAN Nodes 1 & 2. " & _
                "Depending on the selected module interrupt it is called. " & vbCrLf & _
                "ADC -  It is activated by different interrupt sources (Channel Interrupts or Event Interrupts)." & _
                "The user obtains the conversion result by calling the function " & _
                "ADC_uwGetResultDataX(0,1,2,3)" & "." & vbCrLf & _
                "CAN - It is called for the  Service Request Node 1 and 2 of the MultiCAN module." & vbCrLf & _
                 TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If (r.v("ADC_USE_Isr") = 1) Then
     If (Not func.AddFunction("ADC_NAME_Isr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf ((r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1)) Then
     If (Not func.AddFunction("CAN_NAME_SRN1", "void", sDescription, sReturnValue, 2)) Then Exit Function
  End If
  '------------------------------------------------------------------------------------------------
  ' Description for Main_vXINTR8Isr
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the shared interrupt node XINTR8. " & _
                "XINTR8 ISR Node is shared by External Interrupt 2,Timer 21,CORDIC,UART1,UART1 Fractional Divider(BRG Interrupt), and MDU[1:0]. " & _
                "Depending on the selected module interrupt it is called. " & vbCrLf & _
                "EXT2 - It is called for the External interrupt 2" & ". " & vbCrLf & _
                "T21 - Depending on the selected operating mode it is called when TF2 is set by " & _
                "an overflow or underflow of the timer 21 register or when EXF2 " & _
                "is set by a negative transition on T2EX." & vbCrLf & _
                "UART1 - It is called after the BRG timer overflows and sets the NDOV bit." & vbCrLf & _
                "It is called after each transmission (flag TI_1 set) or reception (flag RI_1 set) of a data unit." & vbCrLf & _
                 TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If ((r.v("T21_USE_IsrTmr") = 1) And (r.v("IR_USE_IsrExt2") = 1)) Then
     If (Not func.AddFunction("T21_NAME_IsrTmr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf (r.v("T21_USE_IsrTmr") = 1) Then
     If (Not func.AddFunction("T21_NAME_IsrTmr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf r.v("IR_USE_IsrExt2") = 1 Then
      If (Not func.AddFunction("IR_NAME_IsrExt2", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf (r.v("UART1_USE_Isr") = 1) Then
      If (Not func.AddFunction("UART1_NAME_Isr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf (r.v("BRG1_Interrupt_Select") = 1) Then
      If (Not func.AddFunction("BRG1_NAME_Isr", "void", sDescription, sReturnValue, 2)) Then Exit Function
  End If
  '------------------------------------------------------------------------------------------------
  ' Description for Main_vXINTR9Isr
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the shared interrupt node XINTR9. " & _
                 "XINTR9 ISR Node is shared by External Interrupts 3,4 5,6 and MultiCAN Node3 . " & _
                 "Depending on the selected module interrupt it is called. " & vbCrLf & _
                 "EXT3- It is called for the External interrupts 3,4,5 and 6" & ". " & vbCrLf & _
                 "CAN -  It is called for the  Service Request Node 3 of the MultiCAN module." & vbCrLf & _
                  TOOLTIP_ISR_USERCODE
  sReturnValue = "None"
  If (r.v("IR_USE_IsrExt3") = 1) Then
     If (Not func.AddFunction("IR_NAME_IsrExt" & 3, "void", sDescription, sReturnValue, 2)) Then Exit Function
  ElseIf (r.v("CAN_USE_SRN3") = 1) Then
     If (Not func.AddFunction("CAN_NAME_SRN3", "void", sDescription, sReturnValue, 2)) Then Exit Function
  End If
  '------------------------------------------------------------------------------------------------
  ' Description for Main_vXINTR10Isr
  '------------------------------------------------------------------------------------------------
    sDescription = "This is the service routine for the shared interrupt node XINTR10. " & _
                   "XINTR10 ISR Node is shared by CCU6 Node Pointer 0 and MultiCAN Node 4 . " & _
                   "Depending on the selected module interrupt it is called. " & vbCrLf & _
                   "CCU6 - If the content of the corresponding " & _
                   "compare timer (configurable) equals the content of the " & _
                   "capture/compare register or if a capture event " & _
                   "occurs at the associated port pin, the interrupt request " & _
                   "flag is set and an interrupt is triggered " & _
                   "(only if enabled)." & vbCrLf & _
                   "CAN - It is called for the  Service Request Node 4 of the MultiCAN module." & vbCrLf & _
                    TOOLTIP_ISR_USERCODE
   sReturnValue = "None"
   If (r.v("CCU6_USE_NodeI0") = 1) Then
      If (Not func.AddFunction("CCU6_NAME_NodeI0", "void", sDescription, sReturnValue, 1)) Then Exit Function
   ElseIf (r.v("CAN_USE_SRN4") = 1) Then
      If (Not func.AddFunction("CAN_NAME_SRN4", "void", sDescription, sReturnValue, 2)) Then Exit Function
   End If
 '------------------------------------------------------------------------------------------------
 ' Description for Main_vXINTR11Isr
 '------------------------------------------------------------------------------------------------
    sDescription = "This is the service routine for the shared interrupt node XINTR11. " & _
                   "XINTR11 ISR Node is shared by CCU6 Node Pointer 1 and MultiCAN Node 5 . " & _
                   "Depending on the selected module interrupt it is called. " & vbCrLf & _
                   "CCU6 - If the content of the corresponding " & _
                   "compare timer (configurable) equals the content of the " & _
                   "capture/compare register or if a capture event " & _
                   "occurs at the associated port pin, the interrupt request " & _
                   "flag is set and an interrupt is triggered " & _
                   "(only if enabled)." & vbCrLf & _
                   "CAN - It is called for the  Service Request Node 5 of the MultiCAN module." & vbCrLf & _
                    TOOLTIP_ISR_USERCODE
    sReturnValue = "None"
    If (r.v("CCU6_USE_NodeI1") = 1) Then
      If (Not func.AddFunction("CCU6_NAME_NodeI1", "void", sDescription, sReturnValue, 1)) Then Exit Function
    ElseIf (r.v("CAN_USE_SRN5") = 1) Then
      If (Not func.AddFunction("CAN_NAME_SRN5", "void", sDescription, sReturnValue, 2)) Then Exit Function
    End If
 '------------------------------------------------------------------------------------------------
 ' Description for Main_vXINTR12Isr
 '------------------------------------------------------------------------------------------------
    sDescription = "This is the service routine for the shared interrupt node XINTR12. " & _
                   "XINTR12 ISR Node is shared by CCU6 Node Pointer 2 and MultiCAN Node 6 . " & _
                   "Depending on the selected module interrupt it is called. " & vbCrLf & _
                   "CCU6 - If the content of the corresponding " & _
                   "compare timer (configurable) equals the content of the " & _
                   "capture/compare register or if a capture event " & _
                   "occurs at the associated port pin, the interrupt request " & _
                   "flag is set and an interrupt is triggered " & _
                   "(only if enabled)." & vbCrLf & _
                   "CAN - It is called for the  Service Request Node 6 of the MultiCAN module." & vbCrLf & _
                    TOOLTIP_ISR_USERCODE
    sReturnValue = "None"
    If (r.v("CCU6_USE_NodeI2") = 1) Then
      If (Not func.AddFunction("CCU6_NAME_NodeI2", "void", sDescription, sReturnValue, 1)) Then Exit Function
    ElseIf (r.v("CAN_USE_SRN6") = 1) Then
      If (Not func.AddFunction("CAN_NAME_SRN6", "void", sDescription, sReturnValue, 2)) Then Exit Function
    End If
 '------------------------------------------------------------------------------------------------
 ' Description for Main_vXINTR13Isr
 '------------------------------------------------------------------------------------------------
    sDescription = "This is the service routine for the shared interrupt node XINTR13. " & _
                   "XINTR13 ISR Node is shared by CCU6 Node Pointer 3 and MultiCAN Node 7 . " & _
                   "Depending on the selected module interrupt it is called. " & vbCrLf & _
                   "CCU6 - If the content of the corresponding " & _
                   "compare timer (configurable) equals the content of the " & _
                   "capture/compare register or if a capture event " & _
                   "occurs at the associated port pin, the interrupt request " & _
                   "flag is set and an interrupt is triggered " & _
                   "(only if enabled)." & vbCrLf & _
                   "CAN - It is called for the  Service Request Node 7 of the MultiCAN module." & vbCrLf & _
                    TOOLTIP_ISR_USERCODE
    sReturnValue = "None"
    If (r.v("CCU6_USE_NodeI3") = 1) Then
       If (Not func.AddFunction("CCU6_NAME_NodeI3", "void", sDescription, sReturnValue, 1)) Then Exit Function
    ElseIf (r.v("CAN_USE_SRN7") = 1) Then
      If (Not func.AddFunction("CAN_NAME_SRN7", "void", sDescription, sReturnValue, 2)) Then Exit Function
    End If
  '------------------------------------------------------------------------------------------------
  ' Description for MAIN_NAME_Main
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the main function."
  sReturnValue = "None"
  If (Not func.AddFunction("MAIN_NAME_Main", "void", sDescription, sReturnValue, 1)) Then Exit Function

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

  RefreshFunctionDescriptions = True
End Function


'*****************************************************************************
'Description:
'        This function returns the fsys clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fsys clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_FSYS(r As ProjectData) As Variant
    Dim dF As Double
    
    dF = r.v("MAIN_FSYS")

    If (dF < r.v("MAIN_FSYS_MIN")) Then
        Get_FSYS = TO_LOW
    ElseIf (dF > r.v("MAIN_FSYS_MAX")) Then
        Get_FSYS = TO_HIGH
    Else
        Get_FSYS = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the fextOSC clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fextOSC clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_fextOSC(r As ProjectData) As Variant
    Dim dF As Double
    
    dF = r.v("MAIN_EXTOSC")

    If (dF < r.v("MAIN_EXTOSC_MIN")) Then
        Get_fextOSC = TO_LOW
    ElseIf (dF > r.v("MAIN_EXTOSC_MAX")) Then
        Get_fextOSC = TO_HIGH
    Else
        Get_fextOSC = Format(dF, "0.00")
    End If

End Function


'*****************************************************************************
'Description:
'        Peripheral Frequency
'        This function returns the fPCLK clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fPCLK clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_fPCLK(r As ProjectData) As Variant
    Dim dF As Double
    dF = r.v("MAIN_FSYS") / (GetValueCLKREL(r))
    If (dF <= MAIN_PCLK_MIN) Then
        Get_fPCLK = TO_LOW
    ElseIf (dF >= MAIN_PCLK_MAX) Then
        Get_fPCLK = TO_HIGH
    Else
       Get_fPCLK = Format(dF, "00.00")
    End If

End Function
'*****************************************************************************
'Description:
'        CLKOUT Frequency
'        This function returns the CLKOUT frequency [MHz].
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
Function Get_fsysCLKOUT(r As ProjectData) As Variant
    Dim dF As Double
    
    dF = r.v("MAIN_FSYS") / GetValueCOREL(r)
    
    If (dF <= MAIN_PCLK_MIN) Then
        Get_fsysCLKOUT = TO_LOW
    ElseIf (dF >= MAIN_PCLK_MAX) Then
        Get_fsysCLKOUT = TO_HIGH
    ElseIf (r.v(SCU_PREFIX & "COCON_COUTS") = 1) And (r.v(SCU_PREFIX & "COCON_TLEN") = 1) Then
       Get_fsysCLKOUT = Format((dF / 2), "00.00")
    Else
    Get_fsysCLKOUT = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'        The "UpdateProjectData" functions of all peripheral modules are
'        called with parameter "lngUpdateDescriptor" being passed down.
'
'        Restrictions:
'        - The first module in collection "MODULENAMES" is the main module
'          (which has no "UpdateProjectData" function implemented)
'        - All other modules in collection "MODULENAMES" contain a file
'          "Ini.dave" with a "UpdateProjectData" function
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data
'        lngUpdateDescriptor    identification of the update context (input)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function UpdatePeripherals(r As ProjectData, lngUpdateDescriptor As Long) As Boolean
  On Error GoTo UpdatePeripheralsError

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

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

  For lngIndex = 2 To cp.Count  ' skip the first module "main"
    Set sc = New Script
    sc.Load (DefGetPath("Scripts") & cp(lngIndex).ModuleDirectory & "\Ini.dave")
    If (Not sc.Functions.UpdateProjectData(r, cp(lngIndex).ParameterValue, lngUpdateDescriptor)) Then Exit Function
  Next lngIndex

  UpdatePeripherals = True
  Exit Function

UpdatePeripheralsError:
  Exit Function
End Function
