/* ======================================================================
 *   Copyright (c) 2021 Texas Instruments Incorporated
 *
 *   All rights reserved. Property of Texas Instruments Incorporated.
 *   Restricted rights to use, duplicate or disclose this code are
 *   granted through contract.
 *
 *   The program may not be used without the written permission
 *   of Texas Instruments Incorporated or against the terms and conditions
 *   stipulated in the agreement under which this program has been
 *   supplied.
 * ==================================================================== */

/**
 *  \file     McuApp.c
 *
 *  \brief    This file contains the Mcu test example
 */

/**********************************************************************************************************************
 *  INCLUDES
 *********************************************************************************************************************/

/* Include Files */
#include "assert.h"
/*LDRA_NOANALYSIS*/
#include "string.h"
#include "Std_Types.h"
#include "Det.h"
#include "Dem.h"
/*LDRA_ANALYSIS*/
#include "Mcu.h"
#include "EcuM_Cbk.h"
#include "Mcu.h"
#include "Mcu_Cfg.h"
#include "Port.h"
#include "Port_Cfg.h"
#include "app_utils.h"
#include "trace.h"
#include "sys_vim.h"
#include "hw_ctrl_core.h"

uint8 MainFunc_Execution;

#ifdef __cplusplus
extern "C" {
#endif

/** @fn void main(void)
 *   @brief Application main function
 *   @note This function is empty by default.
 *
 *   This function is called after startup.
 *   The user can use this function to implement the application.
 */

Std_VersionInfoType      VersionInfo;
sint32 TestState = TRUE;

void Uart_Delay(uint32 Delay)
{
    for ( Delay=0 ;Delay<5000; Delay++)
    {
        
        ;
    }
}

void  SchM_Enter_Mcu_MCU_EXCLUSIVE_AREA_0()
{
    AppUtils_SchM_Enter_EXCLUSIVE_AREA_0();
}

void   SchM_Exit_Mcu_MCU_EXCLUSIVE_AREA_0()
{
     AppUtils_SchM_Exit_EXCLUSIVE_AREA_0();
}

void  SchM_Enter_Port_PORT_EXCLUSIVE_AREA_0()
{
    AppUtils_SchM_Enter_EXCLUSIVE_AREA_0();

}

void   SchM_Exit_Port_PORT_EXCLUSIVE_AREA_0()
{

  AppUtils_SchM_Exit_EXCLUSIVE_AREA_0();

}

int32_t main(void)
{
    uint8 referenceArray[20];
    uint16 mss_uart_tx_pin, mss_uart_rx_pin;
    uint32 sys_clk_freq_vclk_const;

    AppUtils_defaultInit();
    Mcu_Init(&McuModuleConfiguration);
    Port_Init(PortConfigSet_0_pt);
    /* Default is AWR29XX*/
    mss_uart_tx_pin = 13;
    mss_uart_rx_pin = 14;
    
    /* Set up the pinmux for UART tx */
    Port_SetPinMode(mss_uart_tx_pin, PORT_PIN_MODE_LIN0);

    /* Set up the pinmux for UART rx */
    Port_SetPinMode(mss_uart_rx_pin, PORT_PIN_MODE_LIN0);
    Port_SetPinDirection(mss_uart_rx_pin, PORT_PIN_IN);
    /* Initialize interrupt */
    vimInit();

    #if (STD_OFF == MCU_NO_PLL)
    if(McuModuleConfiguration_0.Mcu_PllSourceId == MCU_CLKSRC_DPLL)
    {
        sys_clk_freq_vclk_const = (McuModuleConfiguration_0.Mcu_PllConfig[MCU_CLKSRC_DPLL].Mcu_PllClk1.MCU_PLL_HSDIV2 / (2e6)) * 30;
    }
    else
    {
        sys_clk_freq_vclk_const = (McuModuleConfiguration_0.Mcu_PllConfig[MCU_CLKSRC_APLL].Mcu_PllClk1.MCU_PLL_HSDIV0 / (2e6)) * 30;
    }
    Enable_Uart();
    #else
    Enable_Uart();
    #endif
    /* Delay for UART to settle up */
    Uart_Delay(5000);
    AppUtils_printf("\n\rMcuApp: Sample Application - STARTS !!!\n\r");
#if (STD_ON == MCU_GET_VERSION_INFO_API)
    Mcu_GetVersionInfo(&VersionInfo);
    AppUtils_printf("Mcu Driver version info:%d.%d.%d\n\r",
                    VersionInfo.sw_major_version,
                    VersionInfo.sw_minor_version,
                    VersionInfo.sw_patch_version);
    AppUtils_printf("Mcu Driver Module/Driver:%d.%d \n\r",
                    VersionInfo.moduleID,
                    VersionInfo.vendorID);
#endif

    my_memset((uint8*)0x70000000, 0, 20);
    my_memset(referenceArray, 0xBE, 20);

#if (STD_ON == MCU_INIT_RAM_API)
    Mcu_InitRamSection(0);
#endif

    if (memcmp((uint8*)0x70000000, &referenceArray[0], 20) != 0)
    {
        GT_0trace(McalAppTrace, GT_INFO,
               "RAM section is not initialized properly");
		AppUtils_printf("MCU Test Failed!!!\n\r");
        TestState = FALSE;
    }
    else
    {
		GT_0trace(McalAppTrace, GT_INFO,
               "RAM section is initialized properly\n\r");
        /* check the wakeup cause */
        if(Mcu_GetResetReason() == MCU_POWER_ON_RESET)
        {
            GT_0trace(McalAppTrace, GT_INFO,
                   "Wake up from Power ON\n\r");
			AppUtils_printf("MCU Test Passed!!!\n\r");
        }
        else
        {
            GT_0trace(McalAppTrace, GT_INFO,
                   " Wake up from Soft Reset\n\r");
			AppUtils_printf("MCU Test Passed!!!\n\r");
        }
    }
	
    return TestState;
}

void EcuM_CheckWakeup(EcuM_WakeupSourceType wakeupSource)
{
    MainFunc_Execution = 1;
}


#ifdef __cplusplus
}
#endif

