void Init_Task(uint32_t task_init_data)
{
		int tester=0;
	//uint_8 sys=0;
	bool bInitOpen=FALSE;
  bool	bInitStill=FALSE;
	bool bInitVLPS=FALSE;
	MQX_TICK_STRUCT ttt;
	 _mqx_uint       mqx_ret;
	  trace_init();
	 show_version_information();
		//////////////zga add
	//Set LPTMR to timeout about 5 seconds
		Lptmr_Init(1000, LPOCLK);	
		ADC_Init();
		Calibrate_ADC();
		ADC_Init();
		DMA1_Init();
	//////////////zga add
		// clear flag  
		APP_TRACE("start 1\n\r");
	 _task_create_at(0, SHELL_TASK, 0, shell_task_stack, SHELL_TASK_STACK_SIZE);
	 _task_create_at(0, MMA8415_TASK, 0, mma8451_task_stack, MMA8451_TASK_STACK_SIZE);
	
		Lptmr_Start();
	

	
for(;;)
	{
		 mqx_ret = _lwsem_wait(&g_lptmr_int_sem);

	
	//	_time_delay_ticks(10);
		tester++;
//_time_delay_ticks(10);
		//APP_TRACE("tester is: %d\r\n",tester);
		_time_get_elapsed_ticks(&ttt);
          APP_TRACE("high ttt %d, low ttt%d\r\n", ttt.TICKS[1],ttt.TICKS[0]);
		if(Measured)
		{	Measured=0;
			APP_TRACE ("light: %d ,%d \r\n", (uint16_t) MeasuredValues[1],tester);
		}
		if((GetTouchON()==TRUE))
			{
				SetSysStatus(ACTIVE_OPEN);
			}
			// for test 
			SetSysStatus(ACTIVE_OPEN);
		switch (sysStatus)
	{
		case ACTIVE_OPEN:
					bInitStill=FALSE;
					bInitVLPS=FALSE;
					APP_TRACE ("ACTIVE_OPEN\r\n");
					if(bInitOpen==FALSE)
					{
						bInitOpen=TRUE;
						putmma8451running();
						SysTick_PDD_EnableDevice(SysTick_BASE_PTR, PDD_ENABLE);
					}
					
					
			break;

    case ACTIVE_STILL:
					bInitOpen=FALSE;
					bInitVLPS=FALSE;
				APP_TRACE ("ACTIVE_still\r\n");
					if(bInitStill==FALSE)
					{
						bInitStill=TRUE;
						putmma8451detect();
					}
					enter_vlps();
		case 	VLPSMODE:
					bInitOpen=FALSE;
					bInitStill=FALSE;
				APP_TRACE ("vlpsmode\r\n");
					if(bInitVLPS==FALSE)
					{
						bInitVLPS=TRUE;
						putmma8451standby();
					}
					enter_vlps();
    default:
            break;
	}


	} 
	
}
int main (void)
{  
	uint32_t execution_cycle;	//actual execution cycle
	char ch;

#ifdef CMSIS  // If we are conforming to CMSIS, we need to call start here
    start();
#endif

	printf("\n\rRunning the LQRUG_bme_ex2 project.\n\r");
	

	if (RCM_SRS0 & RCM_SRS0_WAKEUP_MASK)
	{
	  	printf("Wakeup initialization flow\n\r");
		
		systick_init();
		
		cnt_start_value = SYST_CVR;
			
		Init_BME_GPIO();
		
		ADC_BME_Trigger();
		
		//Set LPTMR to timeout about 1 second
		Lptmr_BME_Init(1000, LPOCLK);	
		
		ADC_BME_Init();
		Calibrate_BME_ADC();
		ADC_BME_Init();
		ADC_Start(ADC0_CHANB);
		
		// Enable the ADC interrupt in NVIC
#ifdef CMSIS
		enable_irq(ADC0_IRQn) ;   // ready for this interrupt.  
		enable_irq(LPTimer_IRQn);
#else
		enable_irq(ADC0_irq_no) ;   // ready for this interrupt.  
		enable_irq(LPTMR0_irq_no);
#endif
	  
		cnt_end_value = SYST_CVR;
		
		execution_cycle = cnt_start_value - cnt_end_value - overhead;
		
		systick_disable();
		
#ifdef DEBUG_PRINT
		printf("Systick start value: 0x%x\n\r", cnt_start_value);
		printf("Systick end value: 0x%x\n\r", cnt_end_value);
		printf("Actual execution cycle for initialization phase in normal C code: 0x%x\n\r", execution_cycle);
#endif	
	}
	else
	{
	  	printf("Normal initialization flow\n\r");	//make sure the two printf has the same characters to output
		
	  	systick_init();
	
		cnt_start_value = SYST_CVR;
			
		Init_GPIO();
		
		ADC_Trigger();
		
		//Set LPTMR to timeout about 1 second
		Lptmr_Init(1000, LPOCLK);	
		
		ADC_Init();
		Calibrate_ADC();
		ADC_Init();
		
		ADC_Start(ADC0_CHANB);
		
		// Enable the ADC interrupt in NVIC
#ifdef CMSIS
		enable_irq(ADC0_IRQn) ;   // ready for this interrupt.  
		enable_irq(LPTimer_IRQn);
#else
		enable_irq(ADC0_irq_no) ;   // ready for this interrupt.  
		enable_irq(LPTMR0_irq_no);
#endif

		cnt_end_value = SYST_CVR;
		
		execution_cycle = cnt_start_value - cnt_end_value - overhead;
		
		systick_disable();
		
#ifdef DEBUG_PRINT
		printf("Systick start value: 0x%x\n\r", cnt_start_value);
		printf("Systick end value: 0x%x\n\r", cnt_end_value);
		printf("Actual execution cycle for initialization phase in normal C code: 0x%x\n\r", execution_cycle);
#endif	
	}
	
	Lptmr_Start();
	
#ifndef FREEDOM
	printf("ADC conversion for potentiometer started, press any key to stop ADC conversion\n\r");
#else
	printf("No potentiometer or LED on FREEDOM board, press any key to stop ADC conversion\n\r");
#endif
	
	while(!char_present()) 
	{
#ifndef FREEDOM
		if (cycle_flags == ADC0A_DONE) 
		{
			printf("\r  R0A=%8d",result0A); 
			cycle_flags &= ~ADC0A_DONE ;
		}	
#endif
	} 
	
	in_char();	//Read out any available characters 
	
	ADC_Stop();
	
	printf("ADC conversion stopped, press 'l' to enter VLLS1 mode\n\r");
	
#ifndef FREEDOM
	printf("Press SW3 or SW4(Reset button) on TWR-KL25Z48M to exit VLLS1 mode\n\r");	
#else
	printf("Press SW1(Reset button) on FREEDOM board to exit VLLS1 mode\n\r");
#endif	
	
	while(1)
	{
	  ch = in_char();
	  //out_char(ch);
	  if(ch != 'l')
	  	printf("Incorrect character input, Press 'l' to enter VLLS1 mode\n\r");	
	  else
		break;
	}
		
	llwu_configure(0x0080/*PTC3*/, LLWU_PIN_FALLING, 0x0);
	
	/* Configure SW3 - init for GPIO PTC3/LLWU_P7/UART1_RX/FTM0_CH2/CLKOUT*/
	PORTC_PCR3 = ( PORT_PCR_MUX(1) |
				   PORT_PCR_PE_MASK |
				   PORT_PCR_PFE_MASK |
				   PORT_PCR_PS_MASK);
	  
	enter_vlls1();		  
	
}