/*==================================================================================================
*   Project              : RTD AUTOSAR 4.4
*   Platform             : CORTEXM
*   Peripheral           : S32K3XX
*   Dependencies         : none
*
*   Autosar Version      : 4.4.0
*   Autosar Revision     : ASR_REL_4_4_REV_0000
*   Autosar Conf.Variant :
*   SW Version           : 1.0.0
*   Build Version        : S32K3_RTD_1_0_0_D2110_ASR_REL_4_4_REV_0000_20211007
*
*   (c) Copyright 2020 - 2021 NXP Semiconductors
*   All Rights Reserved.
*
*   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.
==================================================================================================*/

/**
 *   @file main.c
 *
 *   @addtogroup main_module main module documentation
 *   @{
 */

/* Including necessary configuration files. */
#include "Mcal.h"
#include "CAN.h"
#include "SchM_Can.h"
#include "Mcu.h"
#include "Mcl.h"
#include "Port.h"
#include "Dio.h"
#include "Uart.h"
#include "Platform.h"
#include "Lpuart_Uart_Ip_Irq.h"
#include "Flexio_Uart_Ip_Irq.h"
#include "Gpt.h"
#include "Wdg_43_Instance0.h"

#ifdef WDG_INSTANCE1
#if (WDG_INSTANCE1 == STD_ON)
#include "Wdg_43_Instance1.h"
#endif
#endif

/* User includes */
#include "AppTaskMain.h"
#include "hal_adapter.h"
#include "AppTaskBle.h"
#include "AppTaskCan.h"
#include "AppTaskUart0.h"



/*!
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
*/
int main(void)
{
	volatile int exit_code = 0;

	//Init MCU Module
#if (MCU_PRECOMPILE_SUPPORT == STD_ON)
    Mcu_Init(NULL_PTR);
#elif (MCU_PRECOMPILE_SUPPORT == STD_OFF)
    Mcu_Init(&Mcu_Config_VS_0);
#endif /* (MCU_PRECOMPILE_SUPPORT == STD_ON) */

    //Init Clock Module
    Mcu_InitClock(McuClockSettingConfig_0);
     while ( MCU_PLL_LOCKED != Mcu_GetPllStatus() )   /* Wait until PLL is locked */
    {
        /* Busy wait until the System PLL is locked */
    }
    Mcu_DistributePllClock();


    //Init MCL Module
    Mcl_Init(NULL_PTR);
    Mcu_SetMode(McuModeSettingConf_Run);
    OsIf_Init(NULL_PTR);

    //Init Platform Module
    Platform_Init(NULL_PTR);

    //Init Port Module
#if (PORT_PRECOMPILE_SUPPORT == STD_ON)
    Port_Init(NULL_PTR);
#elif (PORT_PRECOMPILE_SUPPORT == STD_OFF)
    Port_Init(&Port_Config_VS_0);
#endif

    //Init Uart Module
#if (UART_PRECOMPILE_SUPPORT == STD_ON)
    Uart_Init(NULL_PTR);
#elif (UART_PRECOMPILE_SUPPORT == STD_OFF)
    Uart_Init(&Uart_xConfig_VS_0);
#endif


    //Init CAN Module
#if (CAN_PRECOMPILE_SUPPORT == STD_ON)
    Can_Init(NULL_PTR);
#elif (CAN_PRECOMPILE_SUPPORT == STD_OFF)
    Can_Init(&Can_Config_VS_0);
#endif
    Can_SetControllerMode(CanController_0, CAN_CS_STARTED);


    //Init EEP Module
//    /* Partition only if it was not partitioned before for EERAM with code 0x4 */
//    Eep_DepartParitition(T_EEEPROM_SIZE);
        /* Initialize Eep driver */
    #if defined (EEP_PRECOMPILE_SUPPORT)
        Eep_Init(NULL_PTR);
    #else
        Eep_Init(&Eep_Config_VS_0);
    #endif

//    Boot_CheckDownlaodAPPStatus();


    //Init GPT Driver and WatchDog
	Gpt_Init(&Gpt_Config_VS_0);
	/* Enable the Gpt notification to periodically service the Wdg */
	Gpt_EnableNotification(GptConf_GptChannelConfiguration_GptChannelConfiguration_0);
	WdgInit();
	IP_LPUART0->CTRL |= LPUART_CTRL_ILT(1);
	IP_LPUART2->CTRL |= LPUART_CTRL_ILT(1);
    //Dio_WriteChannel(DioConf_DioChannel_PTE9_GPIO_OUT_MCU_BT_POW_EN,STD_ON);
    //Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_BT_MOD,STD_ON);

    xTaskCreate( MainTask, ( const char * const ) "MainTask", 128, (void*)0, main_TASK_PRIORITY+3 , NULL );
	UartInit();
//    xTaskCreate( BleTask, ( const char * const ) "BleTask", 256 + 64, (void*)0, main_TASK_PRIORITY , NULL );
    xTaskCreate( CanTask, ( const char * const ) "CanTask" , 512, (void*)0, main_TASK_PRIORITY + 2, NULL );
    xTaskCreate( UartTask, ( const char * const ) "UartTask" , 512, (void*)0, main_TASK_PRIORITY + 1, NULL );
    vTaskStartScheduler();
    for( ;; )
    {
        if(exit_code != 0)
        {
            break;
        }
    }

    return exit_code;
}

/** @} */
