/**
*   @file    Mcl_Axbs.c
*   @version 1.0.0
*
*   @brief   AUTOSAR Mcl - Axbs low level driver.
*   @details contains the low level driver for the Axbs module.
*
*   @addtogroup MCL_MODULE
*   @{
*/
/*==================================================================================================
*   Project              : AUTOSAR 4.3 MCAL
*   Platform             : PA
*   Peripheral           : eDMA
*   Dependencies         : none
*
*   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.
* Identifiers (internal and external) shall not rely on the significance of more than 31 characters. 
* 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 namespace shall be distinct.
* This violation is due to the requirement that requests to have a file version check.
*
* @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 Mcl_Axbs_c_REF_1
* 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 Mcl_Axbs_c_REF_2
* 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 Mcl_Axbs_c_REF_3
* Violates MISRA 2012 Advisory Rule 8.7, Objects shall be defined at block scope if they are only accessed from within a single function.
* These objects are used in various parts of the code.
*
* @section Mcl_Axbs_c_REF_4
* Violates MISRA 2012 Advisory Rule 8.9, An object should be defined at block scope if identifier only appears in a single function.
* These objects are used in various parts of the code.
*
* @section Mcl_Axbs_c_REF_5
* 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 Mcl_Axbs_c_REF_6
* 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 Mcl_Axbs_c_REF_7
* Violates MISRA 2012 Required Rule 10.8, The value of a composite expression shall no be cast to a different essential type category or a wider essential type.
* This violation is due to the complex expression (macro) is given as parameter is not evaluated at preprocessing step.
*
**/
/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
#include "CDD_Mcl_Cfg.h"

#ifdef MCL_ENABLE_CROSSBAR
#if (MCL_ENABLE_CROSSBAR == STD_ON)

#include "Mcl_Axbs.h"
#include "CDD_Mcl.h"
#if (MCL_DEV_ERROR_DETECT == STD_ON)
#include "Det.h"
#endif

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
/**
* @file           Mcl_Axbs.c
*/
#define MCL_AXBS_VENDOR_ID_C                    43
#define MCL_AXBS_AR_RELEASE_MAJOR_VERSION_C     4
#define MCL_AXBS_AR_RELEASE_MINOR_VERSION_C     3
#define MCL_AXBS_AR_RELEASE_REVISION_VERSION_C  1
#define MCL_AXBS_SW_MAJOR_VERSION_C             1
#define MCL_AXBS_SW_MINOR_VERSION_C             0
#define MCL_AXBS_SW_PATCH_VERSION_C             0


/*==================================================================================================
*                                      FILE VERSION CHECKS
==================================================================================================*/

#if (MCL_AXBS_VENDOR_ID_C != MCL_AXBS_VENDOR_ID)
    #error "Mcl_Axbs.c and Mcl_Axbs.h have different vendor IDs"
#endif
/* Check if source file and Mcl_Axbs header file are of the same Autosar version */
#if ((MCL_AXBS_AR_RELEASE_MAJOR_VERSION_C != MCL_AXBS_AR_RELEASE_MAJOR_VERSION) || \
     (MCL_AXBS_AR_RELEASE_MINOR_VERSION_C != MCL_AXBS_AR_RELEASE_MINOR_VERSION) || \
     (MCL_AXBS_AR_RELEASE_REVISION_VERSION_C != MCL_AXBS_AR_RELEASE_REVISION_VERSION))
    #error "AutoSar Version Numbers of Mcl_Axbs.h and Mcl_Axbs.c are different"
#endif
/* Check if source file and Mcl_Axbs header file are of the same Software version */
#if ((MCL_AXBS_SW_MAJOR_VERSION_C != MCL_AXBS_SW_MAJOR_VERSION) || \
     (MCL_AXBS_SW_MINOR_VERSION_C != MCL_AXBS_SW_MINOR_VERSION) || \
     (MCL_AXBS_SW_PATCH_VERSION_C != MCL_AXBS_SW_PATCH_VERSION))
    #error "Software Version Numbers of Mcl_Axbs.h and Mcl_Axbs.c are different"
#endif


#if (MCL_AXBS_VENDOR_ID_C != MCL_VENDOR_ID_CFG)
    #error "Mcl_Axbs.c and CDD_Mcl_Cfg.h have different vendor IDs"
#endif
/* Check if source file and CDD_Mcl_Cfg header file are of the same Autosar version */
#if ((MCL_AXBS_AR_RELEASE_MAJOR_VERSION_C != MCL_AR_RELEASE_MAJOR_VERSION_CFG) || \
     (MCL_AXBS_AR_RELEASE_MINOR_VERSION_C != MCL_AR_RELEASE_MINOR_VERSION_CFG) || \
     (MCL_AXBS_AR_RELEASE_REVISION_VERSION_C != MCL_AR_RELEASE_REVISION_VERSION_CFG))
    #error "AutoSar Version Numbers of CDD_Mcl_Cfg.h and Mcl_Axbs.c are different"
#endif
/* Check if source file and CDD_Mcl_Cfg header file are of the same Software version */
#if ((MCL_AXBS_SW_MAJOR_VERSION_C != MCL_SW_MAJOR_VERSION_CFG) || \
     (MCL_AXBS_SW_MINOR_VERSION_C != MCL_SW_MINOR_VERSION_CFG) || \
     (MCL_AXBS_SW_PATCH_VERSION_C != MCL_SW_PATCH_VERSION_CFG))
    #error "Software Version Numbers of CDD_Mcl_Cfg.h and Mcl_Axbs.c are different"
#endif


/*==================================================================================================
                          LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
==================================================================================================*/
#define MCL_START_SEC_CONST_32
/* @violates @ref Mcl_Axbs_c_REF_1 MISRA 2012 Required 4.10*/
#include "Mcl_MemMap.h"
/**
* @brief base address array for Axbs
*/
/* @violates @ref Mcl_Axbs_c_REF_3 MISRA 2012 Advisory 8.7*/
/* @violates @ref Mcl_Axbs_c_REF_4 MISRA 2012 Advisory 8.9*/
CONST(uint32, MCL_CONST) AXBS_BASE_ADDR32[MCL_CROSSBAR_NUM_INSTANCES] = {
    XBAR_0_BASEADDR
#ifdef XBAR_1_BASEADDR
    #if (MCL_CROSSBAR_NUM_INSTANCES > 1U)
    ,XBAR_1_BASEADDR
    #endif
#endif
};

#define MCL_STOP_SEC_CONST_32
/* @violates @ref Mcl_Axbs_c_REF_1 MISRA 2012 Required 4.10*/
/* @violates @ref Mcl_Axbs_c_REF_2 MISRA 2012 Advisory 20.1*/
#include "Mcl_MemMap.h"

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


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


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


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


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

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


/*==================================================================================================
*                                       LOCAL FUNCTIONS
==================================================================================================*/
#define MCL_START_SEC_CODE
/* @violates @ref Mcl_Axbs_c_REF_1 MISRA 2012 Required 4.10*/
/* @violates @ref Mcl_Axbs_c_REF_2 MISRA 2012 Advisory 20.1*/
#include "Mcl_MemMap.h"


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

/**
* @brief        This function initializes the AXBS module. 
* @details     
*
* @param[in]    pAxbsConfig - pointer to Axbs configuration structure
*/
FUNC(void, MCL_CODE) Axbs_Init( P2CONST(Mcl_CrossbarConfigType, AUTOMATIC, MCL_APPL_CONST) pAxbsConfig)
{
    /* @brief Counter value used in loops */
    VAR(uint8, AUTOMATIC) u8Counter;
    /* @brief Pointer to DMA channel specific configuration */
    P2CONST(Mcl_AxbsSlavePortConfigType, AUTOMATIC, MCL_APPL_CONST) pSlavePortConfig = NULL_PTR;
#ifdef MCL_AXBS_ENABLE_INIT_MGPCR
    #if (STD_ON == MCL_AXBS_ENABLE_INIT_MGPCR)    
        P2CONST(Mcl_AxbsMasterPortConfigType, AUTOMATIC, MCL_APPL_CONST) pMasterPortConfig = NULL_PTR;
    #endif
#endif
    /* Configuring channels Enable, Trigger and Source */
    for (u8Counter = 0x0U; u8Counter < pAxbsConfig->MclCrossbarIpNumPorts; u8Counter++)
    {
        pSlavePortConfig = &((*(pAxbsConfig->pMclCrossbarSlaveHwIpConfig))[u8Counter]);
        /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
        /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
        /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
        if((REG_READ32(AXBS_CRS_ADDR(pSlavePortConfig->Axbs_PortNumber, pSlavePortConfig->Axbs_InstanceNumber)) & AXBS_LK_BIT_MASK32) == 0UL)
        {
#ifdef MCL_CROSSBAR_PRS_READONLY
    #if (STD_OFF == MCL_CROSSBAR_PRS_READONLY)
            /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
            /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
            /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
            REG_RMW32(AXBS_PRS_ADDR(pSlavePortConfig->Axbs_PortNumber,pSlavePortConfig->Axbs_InstanceNumber), AXBS_PRS_MASK32, pSlavePortConfig->Axbs_PortPriorityConfig);
    #endif
#endif
            /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
            /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
            /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
            REG_WRITE32(AXBS_CRS_ADDR(pSlavePortConfig->Axbs_PortNumber, pSlavePortConfig->Axbs_InstanceNumber),pSlavePortConfig->Axbs_PortControlConfig);
        }
#if (MCL_DEV_ERROR_DETECT == STD_ON)   
        else
        {   
            (void)Det_ReportError((uint16)MCL_MODULE_ID, 0U, (uint8)MCL_INIT_ID_U8, (uint8)MCL_E_CRS_AXBS_LOCKED_U8);
        }
#endif        
    }
    
#ifdef MCL_AXBS_ENABLE_INIT_MGPCR
    #if (STD_ON == MCL_AXBS_ENABLE_INIT_MGPCR)
        /* Configuring for Master ports */
        for (u8Counter = 0x0U; u8Counter < pAxbsConfig->MclCrossbarMasterNumPorts; u8Counter++)
        {
            pMasterPortConfig = &((*(pAxbsConfig->pMclCrossbarMasterHwIpConfig))[u8Counter]);
            REG_RMW32(AXBS_MGPCR_ADDR(pMasterPortConfig->Axbs_PortNumber, pMasterPortConfig->Axbs_InstanceNumber), AXBS_MGPCR_MASK32, pMasterPortConfig->Axbs_MasterGeneralConfig);
        }
    #endif
#endif
}

#if (MCL_DEINIT_API == STD_ON)
/**
* @brief        This function de-initializes the AXBS module. 
* @details     
*
* @param[in]    pAxbsConfig - pointer to Axbs configuration structure
*/
FUNC(void, MCL_CODE) Axbs_DeInit( P2CONST(Mcl_CrossbarConfigType, AUTOMATIC, MCL_APPL_CONST) pAxbsConfig)
{
    /* @brief Counter value used in loops */
    VAR(uint8, AUTOMATIC) u8Counter;
    /* @brief Pointer to DMA channel specific configuration */
    P2CONST(Mcl_AxbsSlavePortConfigType, AUTOMATIC, MCL_APPL_CONST) pSlavePortConfig = NULL_PTR;
#ifdef MCL_AXBS_ENABLE_INIT_MGPCR
    #if (STD_ON == MCL_AXBS_ENABLE_INIT_MGPCR)    
        P2CONST(Mcl_AxbsMasterPortConfigType, AUTOMATIC, MCL_APPL_CONST) pMasterPortConfig = NULL_PTR;
    #endif
#endif
    /* Configuring channels Enable, Trigger and Source */
    for (u8Counter = 0x0U; u8Counter < pAxbsConfig->MclCrossbarIpNumPorts; u8Counter++)
    {
        pSlavePortConfig = &((*(pAxbsConfig->pMclCrossbarSlaveHwIpConfig))[u8Counter]);
        /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
        /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
        /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
        if((REG_READ32(AXBS_CRS_ADDR(pSlavePortConfig->Axbs_PortNumber, pSlavePortConfig->Axbs_InstanceNumber)) & AXBS_LK_BIT_MASK32) == 0UL)
        {
#ifdef MCL_CROSSBAR_PRS_READONLY
    #if (STD_OFF == MCL_CROSSBAR_PRS_READONLY)       
            if(AXBS_INSTANCE0 == pSlavePortConfig->Axbs_InstanceNumber)
            {
                /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
                /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
                /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
                REG_RMW32(AXBS_PRS_ADDR(pSlavePortConfig->Axbs_PortNumber, pSlavePortConfig->Axbs_InstanceNumber), AXBS_PRS_MASK32, MCL_CROSSBAR0_PRS_RESET_VALUE_U32);
            }
        #ifdef MCL_CROSSBAR_NUM_INSTANCES
            #if (MCL_CROSSBAR_NUM_INSTANCES == 2U)
            else
            {
                /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
                /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
                /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
                REG_RMW32(AXBS_PRS_ADDR(pSlavePortConfig->Axbs_PortNumber, pSlavePortConfig->Axbs_InstanceNumber), AXBS_PRS_MASK32, MCL_CROSSBAR1_PRS_RESET_VALUE_U32);
            }
            #endif
        #endif
    #endif
#endif
            if(AXBS_INSTANCE0 == pSlavePortConfig->Axbs_InstanceNumber)
            {
                /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
                /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
                /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
                REG_WRITE32(AXBS_CRS_ADDR(pSlavePortConfig->Axbs_PortNumber, pSlavePortConfig->Axbs_InstanceNumber), MCL_CROSSBAR0_CRS_RESET_VALUE_U32);
            }
#ifdef MCL_CROSSBAR_NUM_INSTANCES
    #if (MCL_CROSSBAR_NUM_INSTANCES == 2U)
            else
            {
                /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
                /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
                /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
                REG_WRITE32(AXBS_CRS_ADDR(pSlavePortConfig->Axbs_PortNumber, pSlavePortConfig->Axbs_InstanceNumber), MCL_CROSSBAR1_CRS_RESET_VALUE_U32);
            }
    #endif
#endif 
        }
#if (MCL_DEV_ERROR_DETECT == STD_ON)
        else
        {
            (void)Det_ReportError((uint16)MCL_MODULE_ID, 0U, (uint8)MCL_DEINIT_ID_U8, (uint8)MCL_E_CRS_AXBS_LOCKED_U8);
        }
#endif
#ifdef MCL_AXBS_ENABLE_INIT_MGPCR
    #if (STD_ON == MCL_AXBS_ENABLE_INIT_MGPCR)  
        pMasterPortConfig = &((*(pAxbsConfig->pMclCrossbarMasterHwIpConfig))[u8Counter]);
        /* @violates @ref Mcl_Axbs_c_REF_5 MISRA 2012 Advisory 11.4*/
        /* @violates @ref Mcl_Axbs_c_REF_6 MISRA 2012 Required 11.6*/
        /* @violates @ref Mcl_Axbs_c_REF_7 MISRA 2012 Required 10.8*/
        REG_RMW32(AXBS_MGPCR_ADDR(pMasterPortConfig->Axbs_PortNumber, pMasterPortConfig->Axbs_InstanceNumber), AXBS_MGPCR_MASK32, (uint32)0x00000000);
    #endif
#endif
    }
}
#endif
#define MCL_STOP_SEC_CODE
/* @violates @ref Mcl_Axbs_c_REF_1 MISRA 2012 Required 4.10*/
/* @violates @ref Mcl_Axbs_c_REF_2 MISRA 2012 Advisory 20.1*/
#include "Mcl_MemMap.h"

#endif     /* #ifdef MCL_ENABLE_CROSSBAR */
#endif     /* #if (MCL_ENABLE_CROSSBAR == STD_ON) */

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