/*
******************************************************************************************************
 @Module        	
 @Filename     	power.c
 @Project        	SEM-3000
-----------------------------------------------------------------------------------------------------
 @Controller     	Si1010

 @Compiler      	Keil 8.12

 @ Frequency   	Using Internal clock 24.5MHz

 @Codegenerator 	0.1

 @Description   

------------------------------------------------------------------------------------------------------
 @Date          		2013-07-05 11:00:00

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

#define _Power_Mag_

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "PROJECT_INCLUDES.H"

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// LPM_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function will initialize the low power functionality
//
//-----------------------------------------------------------------------------

void LPM_Init (void)
{
	PMU0CF = CLEAR;
											/*
										Bit                   Name                            Description Write Read
										7 SLEEP          Sleep Mode Select           Writing 1 places the device in Sleep Mode.   
										6 SUSPEND     Suspend Mode Select       Writing 1 places the device in Suspend Mode.
										5 CLEAR         Wake-up Flag Clear         Writing 1 clears all wakeup flags.
										4 RSTWK        Reset Pin Wake-up Flag        N/A Set to 1 if a falling edge	has been detected on RST.
										3 RTCFWK 	SmaRTClock Oscillator Fail Wake-up Source Enable and Flag
															0: Disable wake-up on SmaRTClock Osc. Fail.
															1: Enable wake-up on  SmaRTClock Osc. Fail. Set to 1 if the SmaRTClock Oscillator has failed.
										2 RTCAWK SmaRTClock Alarm Wake-up Source Enable and Flag
															0: Disable wake-up on	SmaRTClock Alarm.
															1: Enable wake-up on SmaRTClock Alarm.		Set to 1 if a SmaRTClock	Alarm has occurred.
										1 PMATWK Port Match Wake-up Source Enable and Flag
															0: Disable wake-up on	Port Match Event.
															1: Enable wake-up on Port Match Event.	Set to 1 if a Port Match	Event has occurred.
										0 CPT0WK Comparator0 Wake-up	Source Enable and Flag
															0: Disable wake-up on	Comparator0 rising edge.
															1: Enable wake-up on	Comparator0 rising edge.
															Set to 1 if Comparator0	rising edge has occurred.	 */
	PMU0CF_Local = 0;
		
  	Sleep_Mode_Chk_Timer= timerSecHandleAssign();
		
   	timerSecTimerSet(Sleep_Mode_Chk_Timer, 10); 	// Set Time out priode

   	LPM_Enable_Wakeup(RTC);
	 
	DC0CN = 0x04;				//0x06 -> 0x04  	3.0 V -> 2.4V -> 2.1V
   	FLSCL |= 0x40;                      // Set the BYPASS bit for operation > 10MHz

}


//-----------------------------------------------------------------------------
// LPM_Enable_Wakeup ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function will enable wakeup sources
//
//-----------------------------------------------------------------------------

void LPM_Enable_Wakeup (U8 wakeup)
{
//  PMU0CF_Local |= (wakeup & (RTC | PORT_MATCH | COMPARATOR));
  PMU0CF_Local |= wakeup ;
}


//-----------------------------------------------------------------------------
// LPM_Disable_Wakeup ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function will disable wakeup sources
//
//-----------------------------------------------------------------------------

void LPM_Disable_Wakeup (U8 wakeup)
{
  PMU0CF_Local &= ~(wakeup & (RTC | PORT_MATCH | COMPARATOR));
}


//-----------------------------------------------------------------------------
// LPM
//-----------------------------------------------------------------------------
//
// Return Value : none
// Parameters   : 1) U8 mode -- can be SLEEP or SUSPEND
//
// This function places the device in a low power mode
//
//-----------------------------------------------------------------------------
void LPM(U8 mode)
{
//	U8 CLKSEL_save;
//	U8 EA_save;
//	U8 PMU0CF_snapshot;
//	U8 RTC0CN_snapshot;
	mode=0;
#if 0
       NOP();
       NOP();
       NOP();
	EA_save = EA;
	Global_Interrupt_Disable();

	 
	 PWM0_Counter_Stop();  // Sound Stop


	CLKSEL_save = CLKSEL;
	CLKSEL = 0x74;

	// Enable the Flash read one-shot timer   
	FLSCL &= ~BYPASS;                   // Clear the one-shot bypass bit
	FLWR  =   NON_ZERO;                 // Write a "dummy" value to FLWR

	// Verify that the system clock setting has been applied
	while(!(CLKSEL & 0x80));            // Wait until CLKRDY -> 1

	//----------------------------------
	// ~~~ Device in Low Power Mode ~~~
	//
	   PMU0CF = (mode |RTC) ; // PMU0CF_Local);
	//       PMU0CF = (mode | PMU0CF_Local);
	//
	// ~~~   Device is now Awake    ~~~
	//----------------------------------



	// Restore the System Clock
	CLKSEL = CLKSEL_save;

	// Disable (Bypass) the Flash Read one-shot timer if the system clock
	// frequency is higher than 10 MHz
	if(SYSCLK > 10000000)
	{
	  FLSCL |= BYPASS;                 // Set the one-shot bypass bit                     
	}

	// Capture the wake-up source and clear all wake-up source flags   
	PMU0CF_snapshot = PMU0CF;
	PMU0CF = CLEAR;

	// Capture RTC events that occured while PMU0CF was being cleared
	RTC0CN_snapshot = RTC_Read(RTC0CN); 

	// If an RTC Alarm occured while PMU0CF was being cleared, clear 
	// the PCU0CF flag again   
	if(RTC0CN_snapshot & (ALRM) && ((PMU0CF_snapshot & RTCAWK) == 0))
	{
	  PMU0CF_snapshot |= PMU0CF;
	  PMU0CF = CLEAR;
	}

	// Restore Interrupt State
	EA = EA_save;
   
    #endif
}               

/*
*********************************************************************************************************
*                                   RF_Wakeup
*
* Description:        Sleep Mode에서 살아 난 후 다시 RF 초기화 
*
* Colled:               RF_Send_Foot_Switch() 함수 
*
* Arguments  :	NONE
*
* Returns      :    	NONE
*
*********************************************************************************************************
*/

void RF_Wakeup(void)
{
	idata U8 status;
	
	status = ppPhyInit();
   	status = ppPhyInitRadio();		
	    
}

#define SLEEP_MODE_TIME  1  // 1초 동안  슬립모드 진입  
void Power_Sleep_Mode(void){
	#if 0
	if(timeSecTimeExpired(Sleep_Mode_Chk_Timer) == TIMER_EXPIRED ){
		timerSecTimerSet(Sleep_Mode_Chk_Timer, 10); 	// Set Time out priode
	//	printf("slep mode Start " );
	    	RTC_SleepTicks(1);		
	//	printf("slep mode End " );
	//}  
	#else
	//ppPhyShutDown();
	RTC_SleepTicks(SLEEP_MODE_TIME);
	//RF_Wakeup();
      #endif
}
 
