
#include "Stubs.h"
#include "Std_Types.h"
#include "Mcal.h"
#include "EcuM.h"
#include "IfxStm_reg.h"
#include "Rte_Const.h"
#include "Rte_Main.h"
#include "Gpt.h"
#include "Dio.h"
#include "Irq.h"



/******************************************************************************/

/* DEFINITION OF LOCAL MACROS */

#if (RTE_PERIODIC_COUNTER_TICK_INTERVAL_US<1000UL)
#error	"The TICK_PERIOD_MS assignement need to be reviewed if the SYSTEM tick period goes below 1ms"
#else
#define TICK_PERIOD_MS		(RTE_PERIODIC_COUNTER_TICK_INTERVAL_US/100UL)
#endif

/******************************************************************************/


FUNC(void, OS_CALLOUT_CODE) StartupHook(void)
{
}

/* debugging counter for SystemTick Gpt Notification */
volatile uint32 ATOM_notification_cnt;
void Gpt_Cbk_Notification(void)
{
	Dio_LevelType Level;
	
	ATOM_notification_cnt++;

	IncrementCounter(Rte_TickCounter);
	
	/* wait 50 periods before toggling the LED */
	if(ATOM_notification_cnt>=50)
	{
	Level = (Dio_LevelType)(ATOM_notification_cnt%2);
	/* DIO toggle to test the system tick period (sys tick period = DIO period / 2 */
	Dio_WriteChannel(DioConf_DioChannel_DioChannel_LED, Level);
	}
}

void IC_McalIrq_Init ( void )
{
	IrqAscLin_Init();
	IrqCcu6_Init();
	IrqGpt_Init();    /* Assign priorities for GPT interrupts */
	IrqGtm_Init();    /* Assign priorities for GTM interrupts */
	IrqCan_Init();    /* Assign priorities for CAN interrupts */
	IrqHsm_Init();
	IrqGpsrGroup_Init();
	IrqSpi_Init();
	IrqAdc_Init();
	IrqDsadc_Init();
	IrqMsc_Init();
	IrqFlexray_Init();
	IrqEthernet_Init();
	IrqDma_Init();    /* Assign priorities for DMA interrupts */
	IrqStm_Init();    /* Assign priorities for STM interrupts */
	IrqScu_Init();
	IrqPmu_Init();    /* Assign priorities for FLS interrupts */
	IrqSent_Init();
	IrqI2c_Init();
}

void IC_RteTimerStart ( void )
{
	IC_McalIrq_Init();

	/* GTM register OCS sets the fiels SUS ad 2:
	 * 	Soft suspend (GTM Halt Mode). In Soft suspend registers could be read or written
	 *
	 * 	suspend the GTM counters during the debug phase
	 *
	 */
    GTM_OCS.U = 0x12000000;

	Gpt_EnableNotification(GptConf_GptChannel_GptChannelConfiguration_0);
	Gpt_StartTimer(GptConf_GptChannel_GptChannelConfiguration_0,TICK_PERIOD_MS);

	(void)Rte_Start();
}


FUNC(void, OS_CALLOUT_CODE) ErrorHook(StatusType Error)
{
	while(1);
}


/**************************************************************/
/* Called during ShutdownOS 								  */
/**************************************************************/
FUNC(void, OS_CALLOUT_CODE)ShutdownHook(StatusType Error)
{
	EcuM_Shutdown();
}


/**************************************************************/
/* implementation of the blank check missing in MCAL		  */
/**************************************************************/

Std_ReturnType Fls_BlankCheck(Fls_AddressType TargetAddress,
                                         Fls_LengthType Length)
{
	Std_ReturnType RetVal;
    static uint8 compare_block[MAX_BLANK_CHECK_SIZE];
    uint32 blockIndex;
    for(blockIndex=0; blockIndex < Length; blockIndex++)
    {
    	compare_block[blockIndex] = (uint8)FEE_ERASE_PATTERN;
    }

    RetVal = Fls_Compare((Fls_AddressType)TargetAddress, &compare_block[0] , Length );
	return RetVal;

}
