/**
*   @file    Lin_Cfg.h
*   @version 1.0.0
*
*   @brief   AUTOSAR Lin - High level header of LIN driver.
*   @details This file contains declarations of the functions defined by AutoSAR.
*
*   @addtogroup LIN
*   @{
*/
/*==================================================================================================
*   Project              : AUTOSAR 4.3 MCAL
*   Platform             : PA
*   Peripheral           : ESCI
*   Dependencies         : 
*
*   Autosar Version      : 4.3.1
*   Autosar Revision     : ASR_REL_4_3_REV_0001
*   Autosar Conf.Variant :
*   SW Version           : 1.0.0
*   Build Version        : MPC5777C_MCAL_1_0_0_RTM_ASR_REL_4_3_REV_0001_20190315
*
*   (c) Copyright 2006-2016 Freescale Semiconductor, Inc. 
*       Copyright 2017-2019 NXP
*   All Rights Reserved.
==================================================================================================*/
/*==================================================================================================
==================================================================================================*/

#ifndef LIN_CFG_H
#define LIN_CFG_H

#ifdef __cplusplus
extern "C"
{
#endif

/**
* @page misra_violations MISRA-C:2012 violations
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.1, External identifiers shall be distinct.
* The used compilers use more than 31 chars for identifiers.
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.2, Identifiers declared in the same scope and name space shall be distinct.
* The used compilers use more than 31 chars for identifiers.
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.4, Macro identifiers shall be distinct.
* The used compilers use more than 31 chars for identifiers.
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.5, Identifiers shall be distinct from macro names.
* The used compilers use more than 31 chars for identifiers.
*
* @section Lin_Cfg_h_REF_1
* Violates MISRA 2012 Advisory Rule 20.1, #include directives should only be preceded by preprocessor directives or comments.
* <MA>_MemMap.h is included after each section define in order to set the current memory section as defined by AUTOSAR.
*
* @section Lin_Cfg_h_REF_2
* Violates MISRA 2012 Required Directive 4.10, Precautions shall be taken in order to prevent the contents of a header
* file being included more than once.
* This violation is not fixed since the inclusion of <MA>_MemMap.h is as per AUTOSAR requirement [SWS_MemMap_00003].
*
* @section Lin_Cfg_h_REF_3
* Violates MISRA 2012 Advisory Rule 2.5, A project should not contain unused macro declarations.
* Macro unused in driver but it needed for testing.
*/

/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
[!NOCODE!]
[!// check for duplicated channel id, or hw channel id
    [!VAR "tmp_ch_id" = "-1"!]
    [!VAR "tmp_hw_ch" = "-1"!]
    [!VAR "tmp_channel_index" = "0"!]
    [!VAR "max_channel_nr" = "count(LinChannel/*)"!]  
    [!LOOP "LinChannel/*"!]
        [!VAR "i" = "0"!]
        [!VAR "tmp_channel_index" = "$tmp_channel_index + 1"!]
        [!VAR "tmp_ch_id" = "LinChannelId"!]
        [!VAR "tmp_hw_ch" = "substring-after(LinHwChannel,'_')"!]
        [!FOR "i" = "$tmp_channel_index + 1" TO "$max_channel_nr"!] 
            [!IF "$tmp_ch_id = ../*[num:i($i)]/LinChannelId"!]
                [!ERROR!] Duplicate LinChannelId.[!ENDERROR!]
            [!ENDIF!]
            [!IF "$tmp_hw_ch = substring-after(../*[num:i($i)]/LinHwChannel,'_')"!]
                [!ERROR!] Duplicate LinHwChannel IDs.[!ENDERROR!]
            [!ENDIF!]
        [!ENDFOR!]
    [!ENDLOOP!]
[!// end check for duplicated channel id, or hw channel id
[!CODE!][!// 
#include "EcuM_Cbk.h"
[!ENDCODE!][!//
[!ENDNOCODE!][!//

#include "Mcal.h"

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
/**
* @file           Lin_Cfg.h
*/
[!NOCODE!][!//
[!INCLUDE "Lin_VersionCheck_Inc.m"!][!//
[!ENDNOCODE!][!//

/* Inclusion of incompatible header files shall be avoided */

#define LIN_VENDOR_ID_CFG                    43
/**
* @violates @ref Lin_Cfg_h_REF_3 Macro unused in driver but it needed for testing.
*/
#define LIN_MODULE_ID_CFG                    82
#define LIN_AR_RELEASE_MAJOR_VERSION_CFG     4
#define LIN_AR_RELEASE_MINOR_VERSION_CFG     3
#define LIN_AR_RELEASE_REVISION_VERSION_CFG  1
#define LIN_SW_MAJOR_VERSION_CFG             1
#define LIN_SW_MINOR_VERSION_CFG             0
#define LIN_SW_PATCH_VERSION_CFG             0
/*==================================================================================================
                                      FILE VERSION CHECKS
==================================================================================================*/
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  /* Check if the source file and EcuM_Cbk header file are of the same version */
  #if ((LIN_AR_RELEASE_MAJOR_VERSION_CFG != ECUM_CBK_AR_RELEASE_MAJOR_VERSION) || \
       (LIN_AR_RELEASE_MINOR_VERSION_CFG != ECUM_CBK_AR_RELEASE_MINOR_VERSION))
    #error "AutoSar Version Numbers of Lin_Cfg.h and EcuM_Cbk.h are different"
  #endif
  /* Check if current file and Mcal.h header file are of the same Autosar version */
    #if ((LIN_AR_RELEASE_MAJOR_VERSION_CFG != MCAL_AR_RELEASE_MAJOR_VERSION) || \
         (LIN_AR_RELEASE_MINOR_VERSION_CFG != MCAL_AR_RELEASE_MINOR_VERSION))
        #error "AutoSar Version Numbers of Lin_Cfg.h and Mcal.h are different"
    #endif
#endif


/*==================================================================================================
*                                          CONSTANTS
==================================================================================================*/
/**
* @brief          Precompile Support On.
* @details        VARIANT-PRE-COMPILE: Only parameters with "Pre-compile time"
*                 configuration are allowed in this variant.
*
* @api
*/
#define LIN_PRECOMPILE_SUPPORT [!IF "(IMPLEMENTATION_CONFIG_VARIANT = 'VariantPreCompile' or IMPLEMENTATION_CONFIG_VARIANT='VariantLinkTime') and (variant:size() <= 1)"!] (STD_ON) [!ELSE!] (STD_OFF) [!ENDIF!]

[!MACRO "LinExportDriverConfiguration"!]
[!NOCODE!][!//
[!VAR "configName" = "as:name(LinGlobalConfig)"!][!//
[!CODE!][!//
#define LIN_CONF_PB \
[!ENDCODE!][!//
[!IF "var:defined('postBuildVariant')"!][!//
    [!VAR "variantIndex"="0"!][!//
    [!VAR "variantNumber"="variant:size()"!][!//
    [!LOOP "variant:all()"!][!VAR "variantIndex"="$variantIndex + 1"!][!//
    [!//
        [!CODE!][!WS4!]extern CONST(Lin_ConfigType, LIN_CONST) [!"$configName"!]_[!"."!];[!IF "$variantIndex < $variantNumber"!]\[!ENDIF!][!CR!][!ENDCODE!]
    [!ENDLOOP!][!//
[!ELSE!][!//
    [!CODE!][!WS4!]extern CONST(Lin_ConfigType, LIN_CONST) [!"$configName"!];[!CR!][!ENDCODE!]
[!ENDIF!][!//
[!ENDNOCODE!][!//
[!ENDMACRO!]

[!IF "((IMPLEMENTATION_CONFIG_VARIANT != 'VariantPostBuild') and (variant:size()>1)) or (IMPLEMENTATION_CONFIG_VARIANT = 'VariantPostBuild')"!]
    [!CALL "LinExportDriverConfiguration"!]
[!ENDIF!]

/**
* @brief          Unified Interrupt.
* @details        This define was enable when the platform has only one 
*                 interrupt for each LINFlex channel
*
* @api
*/
[!FOR "index" = "0" TO "num:i(ecu:get('Lin.LinGlobalConfig.LinChannel'))-1"!][!//
[!CODE!][!//
/**
* @violates @ref Lin_Cfg_h_REF_3 This macro guarded by #if defined(LinHWCh_[!"$index"!]).
*/
#define LINHWCH[!"$index"!]_UNIFIED_INTERRUPTS [!IF "ecu:has(concat('LinHWCh',string(num:i($index)),'UnifiedInterrupts'))"!][!IF "ecu:get(concat('LinHWCh',string(num:i($index)),'UnifiedInterrupts')) = 'TRUE'"!](STD_ON)[!ELSE!](STD_OFF)[!ENDIF!][!ELSE!](STD_OFF)[!ENDIF!]
[!ENDCODE!][!//
[!ENDFOR!][!//

/**
* @brief   No of Channels configured.
*
*
* @api
*/
#define LIN_HW_MAX_MODULES [!"num:i(count(LinGlobalConfig/LinChannel/*))"!]U

/**
* @brief   Total number of available hardware lin channels.
*
* 
* @api
*/
#define LIN_HW_MAX_AVAILABLE_MODULES [!"num:i(ecu:get('Lin.LinGlobalConfig.LinChannel'))"!]U

/**
* @brief   Max data length of the LIN SDU buffer to be returned.
*
* @api
*/
#define LIN_MAX_DATA_LENGTH  8U

/**
* @brief   Number of loops before returning LIN_E_TIMEOUT.
*
* @api
*/
#define  LIN_TIMEOUT_LOOPS ((uint32)[!"LinGeneral/LinTimeoutDuration"!]U)

/**
* @brief   Switches the Development Error Detection and Notification ON or OFF.
*
* @api
*/
#define LIN_DEV_ERROR_DETECT      [!IF "LinGeneral/LinDevErrorDetect"!](STD_ON) /* Enable Development Error Detection */[!ELSE!](STD_OFF) /* Disable Development Error Detection */[!ENDIF!]

/**
* @brief          Support for version info API.
* @details        Switches the Lin_GetVersionInfo() API ON or OFF.
*
* @api
*/
#define LIN_VERSION_INFO_API      [!IF "LinGeneral/LinVersionInfoApi"!](STD_ON) /* Enable API Lin_GetVersionInfo */[!ELSE!](STD_OFF) /* Disable API Lin_GetVersionInfo */[!ENDIF!]

[!NOCODE!]
[!CODE!]
/**
* @brief          Link Lin channels symbolic names with Lin channel IDs.
* @details        Link Lin channels symbolic names with Lin channel IDs.
*
* @api
*/
[!ENDCODE!]
[!FOR "MaxHwChannels" = "0" TO "num:i(ecu:get('Lin.LinGlobalConfig.LinChannel'))"!]
    [!VAR "HWChannelFound" = "0"!]
        [!LOOP "LinGlobalConfig/LinChannel/*"!]
            [!VAR "HardwareChannel" = "substring-after(LinHwChannel,'_')"!]
            [!IF "$MaxHwChannels = $HardwareChannel"!]
                [!IF "$HWChannelFound = 0"!]
                    [!CODE!]
#define LinHWCh_[!"$HardwareChannel"!]    [!"$HardwareChannel"!]U
                    [!ENDCODE!][
                    [!VAR "HWChannelFound" = "1"!]
                [!ENDIF!]
            [!ENDIF!]
        [!ENDLOOP!]
    
[!ENDFOR!]
[!ENDNOCODE!]

/**
* @brief   Switches the Production Error Detection and Notification OFF
*
* @api
*/
[!NOCODE!]
[!VAR "LinDemErrorEnable" = "'false'"!]
[!IF "AutosarExt/LinDisableDemReportErrorStatus ='false'"!][!//
    [!IF "node:exists(LinDemEventParameterRefs)"!]
        [!VAR "LinDemErrorEnable" = "'true'"!]
    [!ENDIF!]
[!ENDIF!]
[!ENDNOCODE!]
#define LIN_DISABLE_DEM_REPORT_ERROR_STATUS   [!IF "$LinDemErrorEnable"!](STD_OFF) /* Enable Production Error Detection */[!ELSE!](STD_ON) /* Disable Production Error Detection */[!ENDIF!]

/**
* @brief          Enable Non-Autosar API for Dual-Clock support.
* @details        Enable/diasble API Lin_SetClockMode() to set the 
*                 clock to be used by the LIN driver (Normal clock: default mode;
*                 Alternate clock: when the driver is in Low-Power mode).
*                 This can be set to STD_ON only if it is activated from xdm file
*                 (Lin.AutosarExt.LinEnableDualClockMode = STD_ON).
*
* @api
*/
[!NOCODE!][!//
[!VAR "DualClockEnabled"= "'false'"!]
    [!LOOP "LinGlobalConfig/LinChannel/*"!][!//
        [!IF "(node:exists(LinClockRef_Alternate))"!][!//
            [!VAR "DualClockEnabled" = "'true'"!]
        [!ENDIF!][!//
    [!ENDLOOP!][!//
[!ENDNOCODE!][!//

#define LIN_DUAL_CLOCK_MODE       [!IF "$DualClockEnabled"!](STD_ON) /* Enable API Lin_SetClockMode */[!ELSE!](STD_OFF) /* Disable API Lin_SetClockMode*/[!ENDIF!]
/**
* @brief          Disable Frame Timeout Feature.
* @details        When AutosarExt/LinDisableFrameTimeout == STD_ON,
*                 the LIN Master will accept the frame that is longer than TFrame_Maximum.
*
* @api
*/
#define LIN_DISABLE_FRAME_TIMEOUT [!IF "AutosarExt/LinDisableFrameTimeout"!] (STD_ON) /* The LIN Master will accept the frame that is longer than TFrame_Maximum. */ [!ELSE!] (STD_OFF) /* The master will not accept the frame that is longer than TFrame_Maximum. */[!ENDIF!]

/*==================================================================================================
*                                      DEFINES AND MACROS
==================================================================================================*/

/*==================================================================================================
*                                             ENUMS
==================================================================================================*/

/*==================================================================================================
*                                STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/
/**
* @brief          LIN channel configuration type structure.
* @details        This is the type of the external data structure containing
*                 the overall initialization data for one LIN Channel.
*                 A pointer to such a structure is provided to the LIN channel
*                 initialization routine for configuration of the LIN hardware
*                 channel.
*
* @api
*/
typedef struct
{
    VAR(uint8, LIN_VAR) u8LinChannelID;              /*!< Lin Channel ID */
    VAR(uint8, LIN_VAR) u8LinHwChannel;              /*!< LIN Hardware Channel.*/
    VAR(uint8, LIN_VAR) u8LinChannelWakeupSupport; /*!< Is wake-up supported by the LIN channel ?.*/
    VAR(EcuM_WakeupSourceType, LIN_VAR) LinChannelEcuMWakeupSource; /*!< [LIN098] This parameter contains a reference to the Wakeup Source for this controller as defined in the ECU State Manager.*/
} Lin_StaticConfig_ChannelConfigType;
/**
* @brief          LIN buffer type structure.
* @details        This is the type of the external data structure containing
*                 the Frame ID, data length, Checksum, Timeout value for
*                 LIN Frame.
*
* 
* @api
*/
typedef struct
{
  VAR(uint8, LIN_VAR) u8Lin_HEADER[4];    /*!< Lin message header information.*/
  VAR(uint8, LIN_VAR) au8Lin_DATA[LIN_MAX_DATA_LENGTH];    /*!< Data length from 0 to 7 bytes.*/
  VAR(uint8, LIN_VAR) u8Lin_TYPE;    /*!< Master response or slave response.*/
} Lin_BuffType;

/**
* @brief          LIN channel configuration type structure.
* @details        This is the type of the external data structure containing
*                 the overall initialization data for one LIN Channel.
*                 A pointer to such a structure is provided to the LIN channel
*                 initialization routine for configuration of the LIN hardware
*                 channel.
*
* @api
*/
typedef struct
{
    P2CONST(Lin_StaticConfig_ChannelConfigType, LIN_VAR, LIN_APPL_CONST) pChannelConfigPC; /* Pointer to precompile configuration structure */
#if (STD_ON == LIN_DUAL_CLOCK_MODE)
    VAR(uint32, LIN_VAR) u32Lin_BaudRate_RegValue_Alternate;    /*!< LIN baudrate value with alternate clock.*/
    VAR(uint32, LIN_VAR) u32Lin_BaudRate_RegValue;    /*!< LIN baudrate value.*/    
#else
    VAR(uint32, LIN_VAR) u32Lin_BaudRate_RegValue;    /*!< LIN baudrate value.*/
#endif /* STD_ON == LIN_DUAL_CLOCK_MODE */
} Lin_ChannelConfigType;

/**
* @brief          LIN driver configuration type structure.
* @details        This is the type of the pointer to the external data
*                 LIN Channels.
*                 A pointer of such a structure is provided to the LIN driver
*                 initialization routine for configuration of the LIN hardware
*                 channel.
*
* @api
*
* @implements Lin_ConfigType_struct
*/
typedef struct
{
    /**
    * @brief     Hardware channel.
    * @details   Constant pointer of the constant external data
    *            structure containing the overall initialization data
    *            for all the LIN Channels.
    */
    CONSTP2CONST(Lin_ChannelConfigType, LIN_APPL_CONST, LIN_CONST) pLin_Channel[LIN_HW_MAX_MODULES];
} Lin_ConfigType;


/*==================================================================================================
*                                GLOBAL VARIABLE DECLARATIONS
==================================================================================================*/

/*==================================================================================================
*                                    FUNCTION PROTOTYPES
==================================================================================================*/

/*==================================================================================================
*                                  EXTERNAL CONSTANTS
==================================================================================================*/

#define LIN_START_SEC_CONFIG_DATA_UNSPECIFIED
/**
* @violates @ref Lin_Cfg_h_REF_1 <MA>_MemMap.h is included after each section define in order to
                 set the current memory section as defined by AUTOSAR.
* @violates @ref Lin_Cfg_h_REF_2 This violation is not fixed since the inclusion of <MA>_MemMap.h
                 is as per AUTOSAR requirement [SWS_MemMap_00003].
*/
#include "Lin_MemMap.h"

[!NOCODE!][!//
[!LOOP "LinGlobalConfig/LinChannel/*"!][!//
    [!VAR "nodeName" = "name(.)"!][!//
    [!CODE!][!//
extern CONST(Lin_StaticConfig_ChannelConfigType,LIN_CONST) Lin_[!"node:name(.)"!]_PC;
    [!ENDCODE!]
[!ENDLOOP!][!//
[!ENDNOCODE!][!//

#if (STD_OFF == LIN_DISABLE_DEM_REPORT_ERROR_STATUS)
extern CONST(Mcal_DemErrorType,LIN_CONST) Lin_E_TimeoutCfg;
#endif /* STD_OFF == LIN_DISABLE_DEM_REPORT_ERROR_STATUS */

#define LIN_STOP_SEC_CONFIG_DATA_UNSPECIFIED
/**
* @violates @ref Lin_Cfg_h_REF_1 <MA>_MemMap.h is included after each section define in order to
                 set the current memory section as defined by AUTOSAR.
* @violates @ref Lin_Cfg_h_REF_2 This violation is not fixed since the inclusion of <MA>_MemMap.h
                 is as per AUTOSAR requirement [SWS_MemMap_00003].
*/
#include "Lin_MemMap.h"

#endif /* LIN_CFG_H */

#ifdef __cplusplus
}
#endif
