/***********************************************************************************
 * @Copyright (c)	:(C)2013, Qindao ieslab Co., Ltd
 * @FileName   	      :new_gsm_EEPROM_processor.c
 * @Author       	:Intelligent gas team
 * @Version      	:V1.0
 * @Date         	:2013-11-29
 * @Description	:the function of the entity of system processor
 ************************************************************************************/
#ifndef __EEPROM_APP_H
#define __EEPROM_APP_H

/*--------------------------------Including-------------------------------------------*/
#include "UWater_eeprom_driver.h"
#include "eeprom_app.h" 
/*------------------------------ Variable Define -------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif


//按照喀什规约分配EEPROM存储地址表 具体参见《NB气表EEPROM存储分配地址表》 

//#define EEP_ADDR_SYS_BASE             0x0000
////#define EEP_ADDR_SOE_BASE             0x0800
//#define EEP_ADDR_MONTH_FROZEN_BASE    0x1000    //10年月冻结10*12*EACH_DATA_SIZE即1200，0x0480
//#define EEP_ADDR_DAY_FROZEN_BASE      0x1800    //365*EACH_DATA_SIZE即3650，0x0E42   0x1800+0x0E42=0x2642
//#define EEP_ADDR_HOUR_FROZEN_BASE     0x3000    //62天的小时冻结，62*24* EACH_DATA_SIZE即14880，0x3A20    0x3000+0x3A20= 0x6A20 
//#define EEP_ADDR_SYS_BACKUP_BASE      0x7000  

//#define EEP_ADDR_ACCUMUL_BASE      	  0x7800  
//#define EEP_ADDR_ACCUMUL_BACKUP_BASE  0x7900  
//#define EE_S_FAIL				0
//#define EE_S_SUCCESS			1
//#define EE_S_OPERATING			2
//#define EE_S_ADD_ERR			4

#define EE_S_NO_ERR							0x00
#define EE_S_MAIN_BACKUP_BOTH_ERR			0x01  //主数据区备用数据区CRC错误
#define EE_S_MAIN_ERR						0x02  //主数据区备用数据区CRC错误
#define EE_S_TIMEOUT_ERR					0x04  //EEPROM超时

#define EE_WR_TIMEOUT			52	//单位为100ms，超时时间为100ms乘以EE_WR_TIMEOUT。
#define EE_Write_Retry_Max_Time	3
#define WRITEOPERTAION			0x55
#define READOPERTAION			0xAA
#define Bit_2Bits				0x01 //2位小数用1个字节可以表示
#define Bit_3Bits				0x02 //3位小数用2个字节可以表示
#define Demical_Bit				Bit_3Bits	//小数位位数，可设置为2或3
#define NODATA					0xFF
#define EEADDERR				0x00
#define EESYSDATAADDOK			0x01
#define EEOTHERDATAADDOK		0x02
typedef enum
{
	cst_EE_S_S0=0,
	cst_EE_S_S1,
	cst_EE_S_S2,
	cst_EE_S_S3,
	cst_EE_S_S4,
	cst_EE_S_S5,
	cst_EE_S_S6,
	cst_EE_S_S7,
	cst_EE_S_S8,
	cst_EE_S_S9,
	cst_EE_S_S10,
	cst_EE_S_S11,
	cst_EE_S_S12,
	cst_EE_S_S13,
	cst_EE_S_S14,
	cst_EE_S_S15,
	cst_EE_S_S16,
	cst_EE_S_S17,
	cst_EE_S_S18,
	cst_EE_S_S19,
	cst_EE_S_S20,
	cst_EE_S_S21
}cst_EE_S_STEP_NAME;
typedef enum
{
	cst_EE_A_S0=0,
	cst_EE_A_S1,
	cst_EE_A_S2,
	cst_EE_A_S3,
	cst_EE_A_S4,
	cst_EE_A_S5,
	cst_EE_A_S6,
	cst_EE_A_S7,
	cst_EE_A_S8,
	cst_EE_A_S9,
	cst_EE_A_S10,
	cst_EE_A_S11,
	cst_EE_A_S12,
	cst_EE_A_S13,
	cst_EE_A_S14,
	cst_EE_A_S15,
	cst_EE_A_S16,
	cst_EE_A_S17,
	cst_EE_A_S18,
	cst_EE_A_S19,
	cst_EE_A_S20,
	cst_EE_A_S21
}cst_EE_A_STEP_NAME;//EEPROM操作累积量小数位的状态机状态。
typedef enum
{
	cst_EE_TOTAL_S0,
	cst_EE_TOTAL_S1,
	cst_EE_TOTAL_S2,
	cst_EE_TOTAL_S3,
	cst_EE_TOTAL_S4,
}cst_EE_TOTAL_STEP_NAME;//EEPROM初始化状态机状态。


typedef enum
{
	IMPORTANT_DATA_OP_S0 = 0,
	IMPORTANT_DATA_OP_S1,
	IMPORTANT_DATA_OP_S2,
	IMPORTANT_DATA_OP_S3,
	IMPORTANT_DATA_OP_S4,
	IMPORTANT_DATA_OP_S5,
	IMPORTANT_DATA_OP_S6,
	IMPORTANT_DATA_OP_S7,
	IMPORTANT_DATA_OP_S8,
	IMPORTANT_DATA_OP_S9,
	IMPORTANT_DATA_OP_S20,
	IMPORTANT_DATA_OP_S21
}IMPORTANT_DATA_OP;

enum
{	
	FIRST_POWER_ON_SAVE_S0 = 0,
	FIRST_POWER_ON_SAVE_S1,
	FIRST_POWER_ON_SAVE_S2,
	FIRST_POWER_ON_SAVE_S3
};

typedef enum
{
	EE_INIT_STATE_S0=0,
	EE_INIT_STATE_S1,
	EE_INIT_STATE_S2,
	EE_INIT_STATE_S3,
	EE_INIT_STATE_S4,
	EE_INIT_STATE_S5,
	EE_INIT_STATE_S20
}TYPE_EE_INIT_STATE;


typedef struct
{
	u8 data[EEPageSize-2];
	u16 CRCvalue;
}EE_DATA_STRUCT;
typedef union
{	
	EE_DATA_STRUCT EE_Sysdata_Struct;
	u8 buf[EEPageSize];
}EE_DATA_UNION;

void ee_sys_data_init (void);    
u8 ee_write_sys_data_all(void);
u8 ee_read_sys_data_all(void);
void ee_read_soe_data(void);
void ee_read_hour_frozen_data(void);
void ee_read_day_frozen_data(void);
void ee_read_month_frozen_data(void);
void ee_write_soe_data(void);
void ee_write_hour_frozen_data(void);
void ee_write_day_frozen_data(void);
void ee_write_month_frozen_data(void);
void ee_s_clear_err_msg(void);
u8 ee_s_get_err_msg(void);

void ee_wr_sysdata_machine(void);
u8 ee_sys_check_complete(void);
u8 ee_sys_if_idle(void);
u8 ee_sys_if_sleep(void);
void ee_write_accumul_Decimals(u16* accumul_Decimals_Pointer);
void ee_read_accumul_Decimals(u16* accumul_Decimals_Pointer);
void ee_wr_accumul_machine(void);
u8 ee_accumul_check_complete(void);
u8 ee_accumul_if_idle(void);
u8 ee_accumul_if_sleep(void);
void EeSysMachine(void);
u16 ee_addr_compute(u8 *data_addr);

u8 EeReadImportantDataIfIdle(void);
u8 EeWriteImportantDataIfIdle(void);
u8 EeEraseImportantDataIfIdle(void);//擦除状态机是否空闲
void StartEeEraseImportantData(void);
void StartEeReadImportantData(void);
void StartEeWriteImportantData(void);
void set_important_data_rd_state (void);
u8  get_important_data_rd_state (void);
void EeReadImportantDataMachine(void);
void LoadEepromImportantData(void);
void RAMBackupRefresh(void);
void set_reset_souce (u16 reset_souce);
u16 get_reset_souce (void);

#ifdef __cplusplus
}
#endif		//#ifdef __cplusplus

#endif



