/*
 *  (c) Copyright 2006-2016 Freescale Semiconductor, Inc.
 *  Copyright 2017, 2019-2022 NXP
 *
 *  NXP Confidential. This software is owned or controlled by NXP and may only
 *  be used strictly in accordance with the applicable license terms. By
 *  expressly accepting such terms or by downloading, installing, activating
 *  and/or otherwise using the software, you are agreeing that you have read,
 *  and that you agree to comply with and are bound by, such license terms. If
 *  you do not agree to be bound by the applicable license terms, then you may
 *  not retain, install, activate or otherwise use the software.
 *
 *  This file contains sample code only. It is not part of the production code deliverables.
 */

#ifdef __cplusplus
extern "C"{
#endif
/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
#include "typedefs.h"

#include "sample_app_ethswt_data_definition.h"
#include "sample_app_ethswt_initialization.h"
#include "sample_app_ethswt_task_2_send_frames.h"
#include "sample_app_ethswt_task_1_get_counters.h"
#include "Mcal.h"
#include "oal_util_autosar.h"

#ifdef PFE_CFG_FCI_ENABLE
#include "sample_app_fci.h"
#endif /* PFE_CFG_FCI_ENABLE */

#include "mpu_configuration.h"

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

/*==================================================================================================
*                                      GLOBAL VARIABLES
==================================================================================================*/
#ifdef NXP_LOG_ENABLED

    #define ETH_43_PFE_START_SEC_VAR_INIT_UNSPECIFIED
    #include "Eth_43_PFE_MemMap.h"

    /* This array contains debug messages, one message on each line */
    /* In trace32, use this command to display them: data.dump debug_buff /ascii /nohex */
    VAR_ALIGN(char debug_buff[NXP_LOG_ASR_CFG_LINE_COUNT][NXP_LOG_ASR_CFG_LINE_SIZE] = {{0}}, NXP_LOG_ASR_CFG_LINE_SIZE)

    #define ETH_43_PFE_STOP_SEC_VAR_INIT_UNSPECIFIED
    #include "Eth_43_PFE_MemMap.h"

    #define ETH_43_PFE_START_SEC_VAR_INIT_32
    #include "Eth_43_PFE_MemMap.h"

    uint32 debug_line = 0U;

    #define ETH_43_PFE_STOP_SEC_VAR_INIT_32
    #include "Eth_43_PFE_MemMap.h"

#endif

#define ETH_43_PFE_START_SEC_VAR_INIT_32
#include "Eth_43_PFE_MemMap.h"

uint32 pfeRxCtr[ETH_43_PFE_NUM_CONTROLLER_CFG] = {0U};
uint32 pfeTxCtr[ETH_43_PFE_NUM_CONTROLLER_CFG] = {0U};
uint32 pfeTxConfCtr[ETH_43_PFE_NUM_CONTROLLER_CFG] = {0U};
uint32 pfeTxErrorCtr[ETH_43_PFE_NUM_CONTROLLER_CFG] = {0U};

#define ETH_43_PFE_STOP_SEC_VAR_INIT_32
#include "Eth_43_PFE_MemMap.h"

#define ETH_43_PFE_START_SEC_VAR_CLEARED_UNSPECIFIED
#include "Eth_43_PFE_MemMap.h"

/* Structures for EMAC statistics */
#if STD_ON == ETH_43_GET_COUNTER_API
    Eth_CounterType pfeCounterValues;
#endif /* STD_ON == ETH_43_GET_COUNTER_API */
#if STD_ON == ETH_43_GET_RXSTATS_API
    Eth_RxStatsType pfeRxStats;
#endif /* STD_ON == ETH_43_GET_RXSTATS_API */
#if STD_ON == ETH_43_GET_TXSTATS_API
    Eth_TxStatsType pfeTxStats;
#endif /* STD_ON == ETH_43_GET_TXSTATS_API */
#if STD_ON == ETH_43_GET_TXERROR_COUNTER_API
    Eth_TxErrorCounterValuesType pfeTxErrorCounterValues;
#endif /* STD_ON == ETH_43_GET_TXERROR_COUNTER_API */

#define ETH_43_PFE_STOP_SEC_VAR_CLEARED_UNSPECIFIED
#include "Eth_43_PFE_MemMap.h"

#define ETH_43_PFE_START_SEC_VAR_INIT_8_NO_CACHEABLE
#include "Eth_43_PFE_MemMap.h"

/* Trigger for reading controller statistics. */
/* Set it with the debugger to the index of required controller. */
volatile uint8 getStatisticsFromCtrl = ETH_43_PFE_NUM_CONTROLLER_CFG;

#define ETH_43_PFE_STOP_SEC_VAR_INIT_8_NO_CACHEABLE
#include "Eth_43_PFE_MemMap.h"

#define ETH_43_PFE_START_SEC_CODE
#include "Eth_43_PFE_MemMap.h"

/*==================================================================================================
                                     FUNCTION PROTOTYPES
==================================================================================================*/
#if defined(CONFIG_MINIHIF)
    extern void configure_minihif_hw(void);
#endif

void GetStatistics(void);

/*==================================================================================================
                                       GLOBAL FUNCTIONS
==================================================================================================*/
int main(void)
{
    Std_ReturnType ret = E_OK;

    /* Extend the MPU memory region to 4mb */
#if (STD_ON == ETH_43_PFE_ENABLE_USER_MODE_SUPPORT)
    OsIf_Trusted_Call(Mpu_Configuration);
#else
    Mpu_Configuration();
#endif /* STD_ON == ETH_43_PFE_ENABLE_USER_MODE_SUPPORT */
    /* Initialize MCAL drivers */
    ret = SampleAppInitTask();
    /* Trap the application when there was an error during init */
    while (E_OK != ret)
    {
        ;
    }

#if defined(CONFIG_MINIHIF)
    configure_minihif_hw();
#endif /*TEST_MINIHIF*/

#if (defined(PFE_CFG_FCI_ENABLE) && (defined(FCI_L2BR_TEST) || defined(FCI_RTABLE_TEST)))
    /* FCI test */
    SampleAppFciTestTask();
#endif

    while (E_OK == ret)
    {
#ifdef USE_SJA1105P_DRIVER
        if (switchMainFunction == 1U)
        {
            EthSwt_43_SJA1105P_MainFunction();
            switchMainFunction = 0U;
        }
#endif
        if (pfeMainFunction == 1U)
        {
            Eth_43_PFE_MainFunction();
            pfeMainFunction = 0U;
        }
        if (sendFrames == 1U)
        {
            ret = SampleAppTask2();
            sendFrames = 0U;
        }
        if (getStatisticsFromCtrl < ETH_43_PFE_NUM_CONTROLLER_CFG)
        {
            GetStatistics();
            getStatisticsFromCtrl = ETH_43_PFE_NUM_CONTROLLER_CFG;
        }
#ifdef USE_SJA1105P_DRIVER
        if (getCounters == 1U)
        {
            ret = SampleAppTask1();
            getCounters = 0U;
        }
#endif
        /* switch context when a task returns or within the timer ISR */
    }

    return 0;
}

/**
* @brief      GetStatistics - Get EMAC statistics
* @details    This function is called periodically from the main loop when getStatisticsFromCtrl is set.
*             The flag is used also as an index of the controller which statistics are requested.
*
* @return     none
* @retval     none
*
* @pre None
* @post None
*/
void GetStatistics(void)
{
#if STD_ON == ETH_43_GET_COUNTER_API
    (void)Eth_43_PFE_GetCounterValues(getStatisticsFromCtrl, &pfeCounterValues);
#endif /* STD_ON == ETH_43_GET_COUNTER_API */
#if STD_ON == ETH_43_GET_RXSTATS_API
    (void)Eth_43_PFE_GetRxStats(getStatisticsFromCtrl, &pfeRxStats);
#endif /* STD_ON == ETH_43_GET_RXSTATS_API */
#if STD_ON == ETH_43_GET_TXSTATS_API
    (void)Eth_43_PFE_GetTxStats(getStatisticsFromCtrl, &pfeTxStats);
#endif /* STD_ON == ETH_43_GET_TXSTATS_API */
#if STD_ON == ETH_43_GET_TXERROR_COUNTER_API
    (void)Eth_43_PFE_GetTxErrorCounterValues(getStatisticsFromCtrl, &pfeTxErrorCounterValues);
#endif /* STD_ON == ETH_43_GET_TXERROR_COUNTER_API */
}

#define ETH_43_PFE_STOP_SEC_CODE
#include "Eth_43_PFE_MemMap.h"

#ifdef __cplusplus
}
#endif
