/** @file     Rte_WdgM_Test.c
  * 
  * @brief    RTE Sample SWC implementation skeleton file
  * 
  * @note     Generated by ETAS GmbH  RTA-RTE v5.8.2  (R4.0 backend version: v7.8.5 (Build 46241))
  */

#include "Rte_Wdg_SWC.h"

uint8 TestWdgM_TestNumber=0;	/* test number */

static boolean lTestWdgM_StopPetWdg=FALSE;
static boolean lTestWdgM_Started_Status=FALSE;
static uint32 TestWdgM_Counter=0;

static uint8 Wait4reset_Counter = 50U;
boolean isTestCaseTrigger = 0;
volatile uint8 WdgM_Mode_Current = (uint8)RTE_MODE_WdgM_Mode_SUPERVISION_OK;
volatile uint8 WdgM_Mode_New;


/* for LED blinking */
#include "Dio.h"
#define BLINK_PERIOD_TICKS	10
#define BLINK_PERIOD_FAILED_TICKS	2
/* --------------------------------------------------------------------------- */
/* RTE Event: /WdgM_Test/WdgM_Test/WdgM_Test_IB/TimingEvent */

FUNC(void, WdgM_Test_CODE) RE_Wdg_SWC(void)
{
	/*START--for LED blinking */
	static uint32 BswSE_Counter=0;
	static uint32 BswSE_BlinkPeriod=BLINK_PERIOD_TICKS;
	/*END--for LED blinking */

	static uint8 last_TestNumber=0;
	uint8 lTestNewNumber;

	if(lTestWdgM_StopPetWdg==FALSE)	/* if lTestWdgM_StopPetWdg=TRUE, stop petting watchdog */
		Rte_Call_RPort_Alive_WdgMSE_ASWTask_CheckpointReached(0);	/* Report to WdgM Alive supervision for CheckpointId 0 */

	lTestNewNumber=Rte_IRead_RE_Wdg_SWC_RPort_TestRequested_uint8_Impl();

	/* increment counter */
	TestWdgM_Counter++;

	/* trigger on change */
	if(last_TestNumber!=lTestNewNumber)
	{
		last_TestNumber=lTestNewNumber;
		if((lTestNewNumber>0)&&(lTestNewNumber<=2))
		{
			TestWdgM_TestNumber=last_TestNumber;
		}
	}

	if((lTestWdgM_Started_Status==FALSE)&&(TestWdgM_TestNumber!=0))		/* no test started */
	{
		TestWdgM_Counter=0;		/* reset test counter */
		lTestWdgM_Started_Status=TRUE;	/* start testing */
	}
	else	/* lTestWdgM_Started_Status==FALSE */
	{
		if(TestWdgM_TestNumber==1)
		{
			if((TestWdgM_Counter>=10)&&(TestWdgM_Counter<20))	/* after 1s and for 1s */
			{
				lTestWdgM_StopPetWdg=TRUE;	/* flag to start creating default */
				Rte_Write_PPort_WDG_TestResult_WDG_TestResult(WdgM_Mode_Current);
			}
			else if (TestWdgM_Counter>=20)
			{
				lTestWdgM_StopPetWdg=FALSE;	/* stop creating default */
				lTestWdgM_Started_Status=FALSE;	/* test is done */
				TestWdgM_TestNumber=0;
			}
		}
		else if(TestWdgM_TestNumber==2)
		{
			if(TestWdgM_Counter>=10)	/* after 1s */
			{
				lTestWdgM_StopPetWdg=TRUE;	/* start creating default */
				/* no test end, watchdog should reset the board */
				Rte_Write_PPort_WDG_TestResult_WDG_TestResult(WdgM_Mode_Current);
			}
		}
	}

	WdgM_Mode_New = Rte_Mode_RPort_globalMode_currentMode();
	if(WdgM_Mode_New != WdgM_Mode_Current){
		WdgM_Mode_Current = WdgM_Mode_New;
		Rte_Write_PPort_WDG_TestResult_WDG_TestResult(WdgM_Mode_Current);
	}

	if(TestWdgM_TestNumber !=0){
		isTestCaseTrigger = 1;
	}

	if(isTestCaseTrigger == 1){
		Wait4reset_Counter--;
	}

	if(Wait4reset_Counter == 0){
		Wait4reset_Counter = 50;
		Rte_Write_PPort_WDG_TestResult_WDG_TestResult(100);
		isTestCaseTrigger = 0;
	}

	/*START--for LED blinking */
	if(RTE_MODE_WdgM_Mode_SUPERVISION_FAILED==Rte_Mode_RPort_globalMode_currentMode())
		BswSE_BlinkPeriod=BLINK_PERIOD_FAILED_TICKS;
	else if(RTE_MODE_WdgM_Mode_SUPERVISION_OK==Rte_Mode_RPort_globalMode_currentMode())
		BswSE_BlinkPeriod=BLINK_PERIOD_TICKS;

	/* Blink LED On/Off. Period=500ms or 100ms */
	BswSE_Counter++;
	if(BswSE_Counter>BswSE_BlinkPeriod/2)
		Dio_WriteChannel(DioConf_DioChannel_TRB_LED, 0);
	else
		Dio_WriteChannel(DioConf_DioChannel_TRB_LED, 1);
	if(BswSE_Counter>=BswSE_BlinkPeriod)
		BswSE_Counter=0;

	/*END--for LED blinking */

	/* Report to WdgM Alive supervision for CheckpointId 1 */
	Rte_Call_RPort_Alive_WdgMSE_ASWTask_CheckpointReached(1);
}

