
/*
******************************************************************************************************
 @Module        	MAIN
 @Filename     	main.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

******************************************************************************************************
*/
 
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include "PROJECT_INCLUDES.H"

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

OPERATION_MODE  Operation_Mode;   

#define SW_VERSION_NO 103;   
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

#define ENABLE_CROSSBAR()	Set_Bit(XBR2 , XBARE); 		

U8 LowBattery_ModeChk;
void data_initialization(void);

#define WIFI_PMIC_PORT_DIGITAL_MODE 				P1MDIN	  |=WIFI_RSTN_EN;
#define WIFI_PMIC_PORT_PUSH_PULL_OUT_MODE 		P1MDOUT  |=WIFI_RSTN_EN;

#define WIFI_PMIC_PORT_DIGITAL_MODE 				P1MDIN	  |=WIFI_PMIC_EN;
#define WIFI_PMIC_PORT_PUSH_PULL_OUT_MODE 		P1MDOUT  |=WIFI_PMIC_EN;
void Default_GPIO_Initialization(void)
{        				 	  
	XBR0	= 0x01; 		
	
	ENABLE_CROSSBAR();
#if 1
    WIFI_PMIC_PORT_DIGITAL_MODE;
    WIFI_PMIC_PORT_PUSH_PULL_OUT_MODE;

    WIFI_PMIC_PORT_DIGITAL_MODE;
    WIFI_PMIC_PORT_PUSH_PULL_OUT_MODE;

    WIFI_RSTN = 1;
    WIFI_PMIC = 1;
#endif
									 
}
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function initializes the system clock to use the internal Precision oscillator at its maximum frequency.
//
//-----------------------------------------------------------------------------

#define Internal_Oscillator_Enable()   	Set_Bit(OSCICN , IOSCEN); 		
#define VDD_Monitor_Enabled()  		{ Set_Bit(RSTSRC , PORSF);  Set_Bit(RSTSRC , MCDRSF); }	
#define  System_Clock_Select(Divid,  Clock_Source)  { CLKSEL=0;  CLKSEL=Divid|Clock_Source; }

void Oscillator_Init (void)
{
	Internal_Oscillator_Enable();						 
	
	VDD_Monitor_Enabled();

	System_Clock_Select(System_clock_Divided_1, Internal_Osc_24_5MHZ);
//	System_Clock_Select(System_clock_Divided_1, Internal_Osc_20MHZ);
										
}

 
void System_Initialization(void)
{	
   	Watchdog_Timer_Disabled();


    
	Default_GPIO_Initialization();	
	
	Oscillator_Init(); 	

	PWM_PCA0_Init();

	UART0_Init();


	
  	RTC_Init ();   
	 	
	Timer_Scheduler_Initializ(); 
		
	I2C_Init();
	
	//Buzzer_Initialization();

	//Global_Interrupt_Disable();
	//Key_Initialization();   // change ordering   

	Global_Interrupt_Disable()  ;
  	RTC_Timer_Run();

	Battery_Check_Init();

	//LPM_Init();
	RF_Wakeup();


	RF_Communication_Initializ();
		
	SPI_Init();

	System_Parameter_Init(SYSTEM_RESET_INITIALIZE);

	Energy_Monitor_Init(); 
    	sci_communication_init();
	data_initialization(); // cmlee 데이터 초기화    flash에서 메모리로 copy 

	EA=0;

	//serial_memory_init();
	//Read_PowerInfomation();  // EEPROM 초기화 
    Ct_Detector_Init();

	ADC0_Init();		
	Global_Interrupt_Enable();

	Led_Init(); 
	
}



/*
*********************************************************************************************************
*                                                                    StateMachine_Process
*
* Description:   시스템 운영에 필요한 State Machine Loop를 실행 한며 main()에서  무한 루프로 반복 호출 한다. 
*
* Arguments  :  NONE
*
* Returns    :     NONE

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

void StateMachine_Process(void)
{
       U16 Pairing_ID;	
	   
	
	///One_Hour_Event_State_Machine();

	sci_packet_analysis();
	
	///ADC(); 

	RTC_Processing_Year_Day();
	

 	Pairing_ID = Make_Pairing_ID();			// Pairing ID를  Random으로 지속적으로 생성 을 위해 

} 

OPERATION_MODE Normal_Operation(void)
{
	
	if(Get_PowerDown_Mode())  return POWERDOWN_MODE;
	

	return	NORMAL_OPERATION;
	
}


//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
//
extern U32 Grap_Unit1;
void main (void)
{
	unsigned char test;
    int i;
//	unsigned long test1;
//	U32 test2, address,x;

   	System_Initialization();

	ppPhyInitRadio();
    
	event1.SW_Version= SW_VERSION_NO;
	 event1.Pass_code=31777;
	
	 
	Operation_Mode = NORMAL_OPERATION; 
	
	///test=event1.Fare_Method;
	///if(test==0xff)event1.Fare_Method=0;
	
	if(!(event1.Temperature_C_F==0x00 || event1.Temperature_C_F==0x01) )event1.Temperature_C_F=0;
	// 	test=event1.Temperature_C_F;
	//if(event1.Cost_Unit_nation==0xff)event1.Cost_Unit_nation=KOREA;
	///if(event1.alarm_moment_peak==0xffffffff)event1.alarm_moment_peak=1500;
	///if(event1.alarm_month_target==0xffffffff)event1.alarm_month_target=300;
//	if(event1.Cost_Unit_nation==0xff)event1.Cost_Unit_nation=0;

	//if(event1.fLimit_Pwr_Consumption)
	//event1.Home_Building_Indu = event1.Limit_Pwr_Consumption;
	//else
	//event1.Home_Building_Indu = 2;  // 프래쉬에 저장되면 변경       // home building 
	
	///event1.Pairing_select = 1;   // have to change variable  0: NonPairing      1: Pairing
	//LCDMap.LCD_BitCtrl.SEG42_SETING_TIME_TIER=1;  // TIER 아이콘 ON
	//System_Parameter_Init(SYSTEM_SETING);
	
	//serial_memory_init();
	//Read_PowerInfomation();  // EEPROM 초기화 
	  /////////////////////////////////////////////////////////
     //	LCDMap.LCD_BitCtrl.SEG17_DIGIT1_11A=1;
	  
	// flash memory store to check time ==> OK
	#if 1
	SECOND_INIT=event1.seconds;  
   	MINUTE_INIT=event1.minutes;
   	HOUR_INIT=event1.hours;
	RTC_SetTimer(TIME_INIT);
	RTC_Timer_Run();		

	#endif 
    WiFi_Port_init(1);    
    WiFi_Port_init(0);
    WiFi_Port_init(1);   
    
	while (1) 
   	{ 	
		SendRF_Energy_Power_CT_Sensor();	
		StateMachine_Process();	
		//Power_Sleep_Mode();
		//printf("TEst222 =ld% ", Search_Week_Peak_Power[Display_Channel-1]);   /// TEST
   	}	
}                                    
/* cmlee ==============================================================
This routine is copy to flash to memory when startup
=====================================================================*/

void data_initialization(void)
{
LowBattery_ModeChk = 0;

}


