/*
******************************************************************************************************
 @Module        	
 @Filename     	RF_Id.c
 @Project        	SEM-3000
-----------------------------------------------------------------------------------------------------
 @Controller     	Si1001

 @Compiler      	Keil 8.12

 @ Frequency   	Using Internal clock 24.5MHz

 @Codegenerator 	0.1

 @Description   

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

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


#define _RF_Id_C_

/*
********************************************************************************************************
									 @Project Includes
********************************************************************************************************
*/

#include "PROJECT_INCLUDES.H"



/*
*********************************************************************************************************
 									@Macros
*********************************************************************************************************
*/




/*
***************************************************************************************************************************
									@Global Variables
***************************************************************************************************************************
*/	

volatile Rf_Command_Receive_Data   Rf_ID ;




/*
*********************************************************************************************************
*                                                            System_Parameter_Init
*
* Description:   System_Parameter_Init 
*
* Arguments  :  NONE
*
* Returns      :   NONE

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



void System_Parameter_Init(RF_ID_MEMORY_INITIALIZE Initialize_Source)
{
       U8 k;
	   
	if(Initialize_Source==SYSTEM_RESET_INITIALIZE){  // Reset 되여  초기화시  디폴트  상태로 
	
			memset(&event1.State, 0, sizeof(event1) );
							  				   
			Flash_Memory_To_Buff(&event1.State, System_Parameter_BaseAddr, sizeof(event1) );  // 플레시 메모리에서 읽어오기 
			
			if( event1.State !=ID_ONCE_INITIALIZE_WRITE && event1.State !=SYSTEM_SET_WRIT )  // 처음 초기화 하건  페어링 하지 않 앗거나 
			{					
										
					memset( &event1.Channel_ID[0], DEFAULT_ID, sizeof(event1.Channel_ID) );					
											
					Default_Set_Time_And_Day();  // 시간 디폴트  설정 
#if 0
					for(k=0; k<Max_CHANNEL_ID ; k++)
					{
						event1.One_Day_Peak_Power[k]=350; // 1일 최대  순시 전력 
						event1.Week_Peak_Power[k]=350*24*7; // 1주  최대  순시 전력 
						event1.Week_7_Peak_Power[k]=350*24*7*7; // 7주  최대  순시 전력 
						event1.Month_Peak_Power[k]=350*24*30; // 1달  최대  순시 전력 
					}
				//	if(nation_code==KOREA)
				//		{
					event1.Base_Cost[TIER_1] =   Cost.Base_Cost[TIER_1]	=	410;
					event1.Base_Cost[TIER_2] =   Cost.Base_Cost[TIER_2]	=	910;
					event1.Base_Cost[TIER_3] =   Cost.Base_Cost[TIER_3]	=	1600;
					event1.Base_Cost[TIER_4] =  Cost.Base_Cost[TIER_4]	=	3850;
					event1.Base_Cost[TIER_5] =  Cost.Base_Cost[TIER_5]	=	7300;
					event1.Base_Cost[TIER_6] =  Cost.Base_Cost[TIER_6]	=	12940;

					event1.Treshold[TIER_1] =  Cost.Treshold[TIER_1]		=	100;
					event1.Treshold[TIER_2] =  Cost.Treshold[TIER_2]		=	200;
					event1.Treshold[TIER_3] =  Cost.Treshold[TIER_3]		=	300;
					event1.Treshold[TIER_4] =  Cost.Treshold[TIER_4]		=	400;
					event1.Treshold[TIER_5] =  Cost.Treshold[TIER_5]		=	500;
					event1.Treshold[TIER_6] =  Cost.Treshold[TIER_6]		=	0xffff;

					event1.Billing_Rate[TIER_1] =  Cost.Billing_Rate[TIER_1]	=	60.7;
					event1.Billing_Rate[TIER_2] =  Cost.Billing_Rate[TIER_2]	=	125.9;
					event1.Billing_Rate[TIER_3] =  Cost.Billing_Rate[TIER_3]	=	187.9;
					event1.Billing_Rate[TIER_4] =  Cost.Billing_Rate[TIER_4]	=	280.6;
					event1.Billing_Rate[TIER_5] =  Cost.Billing_Rate[TIER_5]	=	417.7;
					event1.Billing_Rate[TIER_6] =  Cost.Billing_Rate[TIER_6]	=	709.5;
				//		}
						
				
					event1.Average_Cost=130.0; // 평균 단가 
#endif			
					event1.State =ID_ONCE_INITIALIZE_WRITE;

					event1.wifi_id_size = 0;
					event1.find_crypto = 0;
					event1.wifi_pwd_size = 0;
					event1.device_info_size = 0;
					memset(&event1.wifi_id[0], 0, 30);
					memset(&event1.wifi_pwd[0], 0, 30);
					memset(&event1.device_info[0], 0, 90);
					memset(&event1.my_ip[0], 0, 20);

#ifdef SET_CRT_VOLTAGE
					for(k=0; k<Max_CHANNEL_ID ; k++)
					{
						event1.device_type[k] = 0;
						event1.AC_voltage[k] = 0;
					}
#endif

					///event1.Limit_Pwr_Consumption=2; // default 2
					///event1.fLimit_Pwr_Consumption=0;

					FLASH_Page_Erase(System_Parameter_BaseAddr, SCRATCHPAD_FLASH_MEMORY); 
					Buff_To_Flash_Memory(System_Parameter_BaseAddr, &event1.State, sizeof(event1) );
				
			}	
			
	}
	else { 
					//설정 에 의해 초기화 한다 
					
					event1.State =SYSTEM_SET_WRIT;
					
					FLASH_Page_Erase(System_Parameter_BaseAddr, SCRATCHPAD_FLASH_MEMORY); 
					
					Buff_To_Flash_Memory(System_Parameter_BaseAddr, &event1.State, sizeof(event1) );
	}  	
	///printf("YYYY=%d\n", event1.years); // cmlee   이 루틴 내에서 변경된다.   
	//Default_Set_Time_And_Day();  // 이 라인은 나중에 플래쉬 메모리가 확인되면 삭제한다. cmlee inserted    time

	// 플래쉬의 값으로 결정 , oxff 이면 플래쉬의 내용이 없다고 보고 공장 디폴트로 초기화 ==> 
	// 플래쉬의 저장된 값으로 변수들 초기화 
	// 현재는 임시로 초기화해서 진행 
       // cmlee initialize peak time setting
       #if 0
	for(k=0; k<Max_peaktime;k++)
		{
		Peak.hour[k]=event1.alarm_hour[k];
		Peak.minute[k]=event1.alarm_minute[k];
		Peak.which_AMPM[k]=event1.alarm_which_AMPM[k];
		Peak.which_Peak[k]=event1.alarm_which_peak[k];
		}
	#endif 
	#if 0
	for(k=0; k<Max_peaktime;k++)
		{
		Peak.hour[k]=0;
		Peak.minute[k]=0;
		Peak.which_AMPM[k]=0;
		Peak.which_Peak[k]=0;
		}
	#endif 

}





