/* ======================================================================
 *   Copyright (c) 2022 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     PortExample.c
 *
 *  \brief    This file contains the DIO test example
 */

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

/* Include Files */
#include "assert.h"
/*LDRA_NOANALYSIS*/
#include "Std_Types.h"
/*LDRA_ANALYSIS*/
#include "Det.h"
#include "Dio.h"
#include "Mcu.h"
#include "EcuM_Cbk.h"
#include "Mcu.h"
#include "Mcu_Cfg.h"
#include "Port.h"


#include "app_utils.h"
#include "sys_vim.h"
#include "hw_ctrl_core.h"


uint8 MainFunc_Execution;

void StartCyclecount()
{

}

#ifdef __cplusplus
extern "C" {
#endif

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();

}

#ifdef __cplusplus
}
#endif

#define DIOEXAMPLE_ARRAYSIZE(x)  (sizeof ((x)) / sizeof (x[0]))


/** @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.
 */
#if (STD_ON == PORT_CFG_VERSION_INFO_API)
Std_VersionInfoType       VersionInfo;
#endif
sint32 TestState = TRUE;
Dio_LevelType gPinLevel[64];
int32_t main(void)
{
    uint16 uart_tx_pin, uart_rx_pin;
    uint32_least i;
    uint32 sys_clk_freq_vclk_const;
    Std_ReturnType      retVal = E_OK;
    AppUtils_defaultInit();

    Mcu_Init(&McuModuleConfiguration);
    Port_Init(PortConfigSet_0_pt);

    uart_tx_pin = 13;
    uart_rx_pin = 14;

    /* Set up the pinmux for UART tx */
    Port_SetPinMode(uart_tx_pin, PORT_PIN_MODE_LIN0);

    /* Set up the pinmux for UART rx */
    Port_SetPinMode(uart_rx_pin, PORT_PIN_MODE_LIN0);
    Port_SetPinDirection(uart_rx_pin, PORT_PIN_IN);

    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


 
    AppUtils_printf("CLANG compiled : portApp: Sample Application - STARTS !!!\n\r");

#if (STD_ON == PORT_CFG_VERSION_INFO_API)
    Port_GetVersionInfo(&VersionInfo);
    AppUtils_printf("Port Driver version info:%d.%d.%d\n\r",
                    VersionInfo.sw_major_version,
                    VersionInfo.sw_minor_version,
                    VersionInfo.sw_patch_version);
    AppUtils_printf("Port Driver Module/Driver:%d.%d \n\r",
                    VersionInfo.moduleID,
                    VersionInfo.vendorID);
#endif



    Port_SetPinDirection(PortConf_PortPin_PortPin_0, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_1, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_2, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_3, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_4, PORT_PIN_OUT);
	
	Port_SetPinDirection(PortConf_PortPin_PortPin_0, PORT_PIN_IN);
    Port_SetPinDirection(PortConf_PortPin_PortPin_1, PORT_PIN_IN);
    Port_SetPinDirection(PortConf_PortPin_PortPin_2, PORT_PIN_IN);
    Port_SetPinDirection(PortConf_PortPin_PortPin_3, PORT_PIN_IN);
    Port_SetPinDirection(PortConf_PortPin_PortPin_4, PORT_PIN_IN);
	
	Port_SetPinDirection(PortConf_PortPin_PortPin_0, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_1, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_2, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_3, PORT_PIN_OUT);
    Port_SetPinDirection(PortConf_PortPin_PortPin_4, PORT_PIN_OUT);
	
	
	gPinLevel[0] = Dio_ReadChannel(64);
    AppUtils_printf("Port Value for GPIOEF channel 2 :%x \n\r",gPinLevel[0]);
	
	if (gPinLevel[0] == STD_HIGH)
	{
		Dio_FlipChannel(64);
		AppUtils_printf("Dio_FlipChannel - GPIOEF\n\r");
		gPinLevel[0] = Dio_ReadChannel(64);
		AppUtils_printf("Port Value:%x \n\r",gPinLevel[0]);
	}
	else
	{
		retVal = E_NOT_OK;
	}
	if (gPinLevel[0] == STD_LOW && retVal ==E_OK)
	{
		Dio_FlipChannel(64);
		AppUtils_printf("Dio_FlipChannel - GPIOEF\n\r");
		gPinLevel[0] = Dio_ReadChannel(64);
		AppUtils_printf("Port Value:%x \n\r",gPinLevel[0]);
	}
	else
	{
		retVal = E_NOT_OK;
	}
	
	gPinLevel[1] = Dio_ReadChannel(70);
	AppUtils_printf("Port Value for GPIOEF :%x \n\r",gPinLevel[1]);
	
	if (gPinLevel[1] ==STD_HIGH && retVal ==E_OK)
	{	

		Dio_FlipChannel(70);
		AppUtils_printf("Dio_FlipChannel - GPIOEF \n\r");
		gPinLevel[1] = Dio_ReadChannel(70);
		AppUtils_printf("Port Value:%x \n\r",gPinLevel[1]);
		
	}
	else
	{
		retVal = E_NOT_OK;
	}

	if (gPinLevel[1] ==STD_LOW && retVal ==E_OK)
	{
		Dio_FlipChannel(70);
		AppUtils_printf("Dio_FlipChannel -GPIOEF\n\r");

		gPinLevel[1] = Dio_ReadChannel(70);
		AppUtils_printf("Port Value:%x \n\r",gPinLevel[1]);
	}
	else
	{
		retVal = E_NOT_OK;
	}
	
    Dio_WriteChannel(120, STD_LOW);
	AppUtils_printf("Dio_WriteChannel -GPIOGH\n\r");
	
    gPinLevel[2] = Dio_ReadChannel(120);
	AppUtils_printf("Port Value:%x \n\r",gPinLevel[2]);
	
	if (gPinLevel[2] ==STD_LOW && retVal ==E_OK)
	{
		retVal = E_OK;
	}
    else
	{
		retVal = E_NOT_OK;
	}

    Dio_WriteChannel(120, STD_HIGH);
	AppUtils_printf("Dio_WriteChannel -GPIOGH\n\r");

    gPinLevel[2] = Dio_ReadChannel(120); 
    AppUtils_printf("Port Value:%x \n\r",gPinLevel[2]);
	
	if (gPinLevel[2] ==STD_HIGH && retVal ==E_OK)
	{
		retVal = E_OK;
	}
    else
	{
		retVal = E_NOT_OK;
	}
	
	if(retVal == E_OK)
	{
		AppUtils_printf("PORT Test Passed!!!\n\r");
	}
	else
	{
		AppUtils_printf("PORT Test Failed!!!\n\r");
	}
	
    return TestState;
}

#ifdef __cplusplus
extern "C" {
#endif

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

}

void   SchM_Exit_Dio_DIO_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();

}


void Dem_ReportErrorStatus(Dem_EventIdType     eventId,
                           Dem_EventStatusType eventStatus)
{
    if (DEM_EVENT_STATUS_FAILED == eventStatus)
    {
        assert(FALSE);
    }

    return;
}

#ifdef __cplusplus
}
#endif
