/**
*   @file    Lin_NonASR.c
*   @version 1.0.0
*
*   @brief   AUTOSAR Lin - Lin AutosarExt high level interface implementation.
*   @details Lin AutosarExt high level interface implementation.
*
*   @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.
==================================================================================================*/
/*==================================================================================================
==================================================================================================*/

#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_ASRExt_c_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_ASRExt_c_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_ASRExt_c_REF_3
* Violates MISRA 2012 Advisory Rule 11.4, A conversion should not be performed between a pointer to object and an integer type.
* The cast is used to access memory mapped registers.
*
* @section Lin_ASRExt_c_REF_4
* Violates MISRA 2012 Required Rule 11.6, A cast shall not be performed between pointer to void and an arithmetic type.
* This macro computes the address of any register by using the hardware offset of the controller.
* The address calculated as an unsigned int is passed to a macro for initializing the pointer with that address.
*
* @section Lin_ASRExt_c_REF_5
* Violates MISRA 2012 Required Rule 10.8, The value of a composite expression shall not be cast to a different essential type
* category or a wider essential type Pointer type conversions.
*
* @section Lin_ASRExt_c_REF_6
* Violates MISRA 2012 Advisory Rule 8.7, Function and objects should not be defined with external linkage if they are referenced in only one translation uint.
*/

/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
/**
* @file           Lin_NonASR.c
*/
#include "Lin.h"
#include "Lin_ASRExt.h"
#include "Lin_IPW.h"
#include "Lin_ESCI.h"

#ifdef LIN_USE_DMA
#include "CDD_Mcl.h"
#endif  /* LIN_USE_DMA */

#include "StdRegMacros.h"
#include "Reg_eSys.h"
#include "Reg_eSys_ESCI.h"

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
/**
* @file           Lin_NonASR.c
*/
#define LIN_VENDOR_ID_ASREXT_C                      43
#define LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C       4
#define LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C       3
#define LIN_AR_RELEASE_REVISION_VERSION_ASREXT_C    1
#define LIN_SW_MAJOR_VERSION_ASREXT_C               1
#define LIN_SW_MINOR_VERSION_ASREXT_C               0
#define LIN_SW_PATCH_VERSION_ASREXT_C               0
/*==================================================================================================
*                                     FILE VERSION CHECKS
==================================================================================================*/
/* Check if current file and Lin header file are of the same vendor */
#if (LIN_VENDOR_ID_ASREXT_C != LIN_VENDOR_ID)
    #error "Lin_ASRExt.c and Lin.h have different vendor ids"
#endif
/* Check if current file and Lin header file are of the same Autosar version */
#if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MAJOR_VERSION) || \
     (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MINOR_VERSION) || \
     (LIN_AR_RELEASE_REVISION_VERSION_ASREXT_C != LIN_AR_RELEASE_REVISION_VERSION) \
    )
    #error "AutoSar Version Numbers of Lin_ASRExt.c and Lin.h are different"
#endif
/* Check if current file and Lin header file are of the same Software version */
#if ((LIN_SW_MAJOR_VERSION_ASREXT_C != LIN_SW_MAJOR_VERSION) || \
     (LIN_SW_MINOR_VERSION_ASREXT_C != LIN_SW_MINOR_VERSION) || \
     (LIN_SW_PATCH_VERSION_ASREXT_C != LIN_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Lin_ASRExt.c and Lin.h are different"
#endif
/* Check if current file and Lin_Ipw header file are of the same vendor */
#if (LIN_VENDOR_ID_ASREXT_C != LIN_IPW_VENDOR_ID)
    #error "Lin_ASRExt.c and Lin_ASRExt.h have different vendor ids"
#endif
/* Check if current file and Lin_Ipw header file are of the same version */
#if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MAJOR_VERSION_IPW) || \
     (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MINOR_VERSION_IPW) || \
     (LIN_AR_RELEASE_REVISION_VERSION_ASREXT_C != LIN_AR_RELEASE_REVISION_VERSION_IPW) \
    )
    #error "AutoSar Version Numbers of Lin_ASRExt.c and Lin_Ipw.h are different"
#endif
/* Check if current file and Lin_Ipw header file are of the same version */
#if ((LIN_SW_MAJOR_VERSION_ASREXT_C    != LIN_SW_MAJOR_VERSION_IPW) || \
     (LIN_SW_MINOR_VERSION_ASREXT_C    != LIN_SW_MINOR_VERSION_IPW) || \
     (LIN_SW_PATCH_VERSION_ASREXT_C    != LIN_SW_PATCH_VERSION_IPW) \
    )
    #error "Software Version Numbers of Lin_ASRExt.c and Lin_Ipw.h are different"
#endif
/* Check if current file and Lin_Ipw header file are of the same version */
#if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MAJOR_VERSION_IPW) || \
     (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MINOR_VERSION_IPW) || \
     (LIN_AR_RELEASE_REVISION_VERSION_ASREXT_C != LIN_AR_RELEASE_REVISION_VERSION_IPW) \
    )
    #error "AutoSar Version Numbers of Lin_ASRExt.c and Lin_Ipw.h are different"
#endif
/* Check if current file and Lin_ASRExt header file are of the same vendor */
#if (LIN_VENDOR_ID_ASREXT_C != LIN_VENDOR_ID_ASREXT)
    #error "Lin_ASRExt.c and Lin_ASRExt.h have different vendor ids"
#endif
/* Check if current file and Lin_ASRExt header file are of the same Autosar version */
#if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MAJOR_VERSION_ASREXT) || \
     (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MINOR_VERSION_ASREXT) || \
     (LIN_AR_RELEASE_REVISION_VERSION_ASREXT_C != LIN_AR_RELEASE_REVISION_VERSION_ASREXT) \
    )
    #error "AutoSar Version Numbers of Lin_ASRExt.c and Lin_ASRExt.h are different"
#endif
/* Check if current file and Lin_ASRExt header file are of the same Software version */
#if ((LIN_SW_MAJOR_VERSION_ASREXT_C != LIN_SW_MAJOR_VERSION_ASREXT) || \
     (LIN_SW_MINOR_VERSION_ASREXT_C != LIN_SW_MINOR_VERSION_ASREXT) || \
     (LIN_SW_PATCH_VERSION_ASREXT_C != LIN_SW_PATCH_VERSION_ASREXT) \
    )
    #error "Software Version Numbers of Lin_ASRExt.c and Lin_ASRExt.h are different"
#endif
/* Check if current file and Lin_ESCI header file are of the same vendor */
#if (LIN_VENDOR_ID_ASREXT_C != LIN_VENDOR_ID_ESCI)
    #error "Lin_ASRExt.c and Lin_ESCI.h have different vendor ids"
#endif
/* Check if current file and Lin_ESCI header file are of the same Autosar version */
#if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MAJOR_VERSION_ESCI) || \
     (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MINOR_VERSION_ESCI) || \
     (LIN_AR_RELEASE_REVISION_VERSION_ASREXT_C != LIN_AR_RELEASE_REVISION_VERSION_ESCI) \
    )
    #error "AutoSar Version Numbers of Lin_ASRExt.c and Lin_ESCI.h are different"
#endif
/* Check if current file and Lin_ESCI header file are of the same Software version */
#if ((LIN_SW_MAJOR_VERSION_ASREXT_C != LIN_SW_MAJOR_VERSION_ESCI) || \
     (LIN_SW_MINOR_VERSION_ASREXT_C != LIN_SW_MINOR_VERSION_ESCI) || \
     (LIN_SW_PATCH_VERSION_ASREXT_C != LIN_SW_PATCH_VERSION_ESCI) \
    )
    #error "Software Version Numbers of Lin_ASRExt.c and Lin_ESCI.h are different"
#endif
/* Check if current file and Reg_eSys_ESCI header file are of the same vendor */
#if (LIN_VENDOR_ID_ASREXT_C != LIN_VENDOR_ID_REGESCI)
    #error "Lin_ASRExt.c and Reg_eSys_ESCI.h have different vendor ids"
#endif
/* Check if current file and Reg_eSys_ESCI header file are of the same Autosar version */
#if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MAJOR_VERSION_REGESCI) || \
     (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C    != LIN_AR_RELEASE_MINOR_VERSION_REGESCI) || \
     (LIN_AR_RELEASE_REVISION_VERSION_ASREXT_C != LIN_AR_RELEASE_REVISION_VERSION_REGESCI) \
    )
    #error "AutoSar Version Numbers of Lin_ASRExt.c and Reg_eSys_ESCI.h are different"
#endif
/* Check if current file and Reg_eSys_ESCI header file are of the same Software version */
#if ((LIN_SW_MAJOR_VERSION_ASREXT_C != LIN_SW_MAJOR_VERSION_REGESCI) || \
     (LIN_SW_MINOR_VERSION_ASREXT_C != LIN_SW_MINOR_VERSION_REGESCI) || \
     (LIN_SW_PATCH_VERSION_ASREXT_C != LIN_SW_PATCH_VERSION_REGESCI) \
    )
    #error "Software Version Numbers of Lin_ASRExt.c and Reg_eSys_ESCI.h are different"
#endif

#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  /* Check if the source file and StdRegMacros header file are of the same version */
  #if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C != STDREGMACROS_AR_RELEASE_MAJOR_VERSION) || \
       (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C != STDREGMACROS_AR_RELEASE_MINOR_VERSION) \
      )
      #error "AutoSar Version Numbers of Lin_ASRExt.c and StdRegMacros.h are different"
  #endif
  /* Check if the source file and Reg_eSys header file are of the same version */
  #if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C != REG_ESYS_AR_RELEASE_MAJOR_VERSION) || \
       (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C != REG_ESYS_AR_RELEASE_MINOR_VERSION) \
      )
      #error "AutoSar Version Numbers of Lin_ASRExt.c and Reg_eSys.h are different"
  #endif
#endif
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    #ifdef LIN_USE_DMA
        /* Check if the source file and CDD_Mcl header file are of the same version */
      #if ((LIN_AR_RELEASE_MAJOR_VERSION_ASREXT_C != MCL_AR_RELEASE_MAJOR_VERSION) || \
           (LIN_AR_RELEASE_MINOR_VERSION_ASREXT_C != MCL_AR_RELEASE_MINOR_VERSION))
          #error "AutoSar Version Numbers of Lin_ASRExt.c and CDD_Mcl.h are different"
      #endif
    #endif  /* LIN_USE_DMA */
#endif

/*==================================================================================================
*                          LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
==================================================================================================*/


/*==================================================================================================
*                                       LOCAL MACROS
==================================================================================================*/


/*==================================================================================================
*                                      LOCAL CONSTANTS
==================================================================================================*/


/*==================================================================================================
*                                      LOCAL VARIABLES
==================================================================================================*/

/*==================================================================================================
*                                      GLOBAL CONSTANTS
==================================================================================================*/


/*==================================================================================================
*                                      GLOBAL VARIABLES
==================================================================================================*/

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


/*==================================================================================================
*                                       LOCAL FUNCTIONS
==================================================================================================*/
#define LIN_START_SEC_CODE
/**
* @violates @ref Lin_ASRExt_c_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_ASRExt_c_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"

/*==================================================================================================
*                                       GLOBAL FUNCTIONS
==================================================================================================*/

/*================================================================================================*/
#if LIN_DUAL_CLOCK_MODE == STD_ON

/**
* @brief          Lin_SetClockMode.
* @details        Switch the clock mode to the alternate clock mode on
*                 all the Lin channels.
*
* @param[in]      ClockMode   New clock mode.
*
* @return                     The result of the switching clock operation.
* @retval E_OK                The switching operation was OK.
* @retval E_NOT_OK            The switching operation has failed caused by the
*                             wrong driver state.
*
* @api            Switch the clock mode to the alternate clock mode on all
*                 the Lin channels.
*
* @pre            LIN_DUAL_CLOCK_MODE must be defined and its value must be STD_ON.
*
* @note           Switch the clock mode to the alternate clock mode on
*                 all the Lin channels.
*/
/**
* @violates @ref Lin_ASRExt_c_REF_5 These functions represent the IPV of the driver. External linkage is needed to "export" the IPV.
*
* @violates @ref Lin_ASRExt_c_REF_6 This function call from other layer.
*
* @implements     Lin_SetClockMode_Activity
*/
FUNC(Std_ReturnType, LIN_CODE) Lin_SetClockMode(VAR(Lin_ClockModesType, AUTOMATIC) ClockMode)
{
    VAR(Std_ReturnType, AUTOMATIC) u8TempReturn = (uint8)E_OK;
    VAR(uint8,AUTOMATIC) u8chLoop;
    VAR(uint8,AUTOMATIC) u8Lin_ESCI;
    VAR(Lin_StatusType,AUTOMATIC) LinStatus = LIN_NOT_OK;
    static VAR(uint8,AUTOMATIC) au8linSdu[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    static P2VAR(uint8, AUTOMATIC, LIN_APPL_VAR) pu8linSduPtr = au8linSdu;

    for(u8chLoop=(uint8)0U; u8chLoop < LIN_HW_MAX_MODULES; u8chLoop++)
    {
        /* Get the hardware Lin u8Channel from logical u8Channel */
        u8Lin_ESCI = Lin_pChannelConfigPtr[u8chLoop]->pChannelConfigPC->u8LinHwChannel;
        LinStatus = Lin_GetStatus(u8chLoop, &pu8linSduPtr);

        switch (LinStatus)
        {
            case LIN_NOT_OK :
            /* If the LIN driver or some channels are not initialized it's not possible to
                change the baudrate */
                u8TempReturn = (uint8)E_NOT_OK;
                break;
            default :
                    if (LIN_NORMAL == ClockMode)
                        {
                            /* set baud rate value for normal running mode */
                            /**
                            * @violates @ref Lin_ASRExt_c_REF_3 The cast is used to access memory mapped registers.
                            *
                            * @violates @ref Lin_ASRExt_c_REF_4 This macro computes the address of any register by using the hardware offset of the controller.
                            *
                            * @violates @ref Lin_ASRExt_c_REF_5 The value of a composite expression shall not be cast to a different essential type category
                            *                or a wider essential type Pointer type conversions.
                            */
                            REG_WRITE16(ESCI_BRR_ADDR32(u8Lin_ESCI), (uint16)((Lin_pcConfigPtr->pLin_Channel[u8chLoop]->u32Lin_BaudRate_RegValue) & (ESCI_BRR_SBR_MASK_U16)));
                        }
                    else
                        {
                            /* set baud rate value for low power mode */
                            /**
                            * @violates @ref Lin_ASRExt_c_REF_3 The cast is used to access memory mapped registers.
                            *
                            * @violates @ref Lin_ASRExt_c_REF_4 This macro computes the address of any register by using the hardware offset of the controller.
                            *
                            * @violates @ref Lin_ASRExt_c_REF_5 The value of a composite expression shall not be cast to a different essential type category
                            *                or a wider essential type Pointer type conversions.
                            */
                            REG_WRITE16(ESCI_BRR_ADDR32(u8Lin_ESCI), (uint16)((Lin_pcConfigPtr->pLin_Channel[u8chLoop]->u32Lin_BaudRate_RegValue_Alternate) & (ESCI_BRR_SBR_MASK_U16)));
                        }
                break;
        }
    }
    return u8TempReturn;
}

#endif /* DUAL CLOCK MODE */

#define LIN_STOP_SEC_CODE
/**
* @violates @ref Lin_ASRExt_c_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_ASRExt_c_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"

#ifdef __cplusplus
}
#endif
/** @} */
