#ifndef USER_DRIVERS_INC_FLASH_H_
#define USER_DRIVERS_INC_FLASH_H_

#include "CH57x_common.h"

/* CodeFlash划分:
 *  0x0000000-0x0001000 : Jump , 4K
 *  0x0001000-0x001E000 : APP , 116K
 *  0x001E000-0x0036000 : IAP , 96K
 *  0x0036000-0x0037000 : 保留，4k
 *  0x0037000-0x003A000 : User-Flash , 12K
 *  0x003A000-0x003BFFF : image-Flag , 8K
 * */

/*CH572D FlashROM : 240KB 0x00000000-0x0003BFFF
 *  本项目中使用Flash 0x037000-0x00039FFF ~ 作为用户信息存储(12K)
 */



#define EEPROM_INFO_START_ADDR              0x037000

#define EEPROM_INFO_SAVE_ADDR1              EEPROM_INFO_START_ADDR      //端口信息
#define EEPROM_INFO_SAVE_ADDR2              0x038000                    //条件组
#define EEPROM_INFO_SAVE_ADDR3              0x039000                    //延时参数设置
#define EEPROM_INFO_END_ADDR                0x039FFF

#define EEPROM_INFO_Size                    4096  //0x001000        //flash-ROM,擦除最小单位4K

#define IMAGE_A_FLAG           0x01
#define IMAGE_B_FLAG           0x02
#define IMAGE_IAP_FLAG         0x03

#define EEPROM_DataInfo_Len                 0x04            //目前数据长度
#define EEPROM_SaveInfo_Flag                0x5A            //保存标志位
#define EEPROM_SaveGpInfo_Flag              0x1A            //条件组保存标志位
#define EEPROM_RARDelayTim_Flag             0x3A            //延迟事件的延迟参数保存标志位



#define DOOR_RDR_DELAYTIME          30000       //门磁延时关时间
#define BATH_RDR_DELAYTIME          3000000     //卫浴灯延时关时间


/* 存放在DataFlash里的OTA信息 */
typedef struct
{
    unsigned char ImageFlag; //记录的当前的image标志
    unsigned char Revd[3];    //第一个字节存放OTA跳转到APP的次数，最多三次
}OTADataFlashInfo_t;


typedef struct
{
    uint8_t VC_ConNToSGruop;     //无卡取电无人至有人条件组数
    uint8_t VC_ConNToSSubset;    //无卡取电无人至有人子条件总数

    uint8_t  VC_ConSToNGruop;       //条件组数(总数)
    uint8_t  VC_ConSToNSubset;      //无卡取电有人至无人子条件总数
    uint8_t PortGroupInfo[VIRTUAL_GP_NUM_MAX][15];
}GroupInfo_t;


typedef struct
{
    uint32_t Door_RARON_Time;       //门磁开触发廊灯开事件延迟
    uint32_t Door_RARFF_TimeOut;     //廊灯关事件延迟
    uint32_t BATH_RARON_Time;       //卫浴雷达开触发卫浴灯开事件延迟
    uint32_t BATH_RARFF_TimeOut;     //卫浴雷达开触发卫浴灯关事件延迟

}RAR_Info;




/*保持参数默认值*/
extern uint8_t PortInfo[VIRTUAL_PORT_MAX][11];
extern GroupInfo_t g_portGp;
extern RAR_Info g_rar_info;

uint8_t EEPROM_CheckSum(uint8_t *data,uint16_t len);

void EEPROM_Init(void);
uint8_t EEPROM_Read_PortInfo(void);
void EEPROM_Default_PortInfo(void);
uint8_t EEPROM_Write_PortInfo(void);


uint8_t EEPROM_Read_PortGroupData(GroupInfo_t *info);
void EEPROM_Default_PortGroupData(void);
uint8_t EEPROM_Write_PortGroupData(GroupInfo_t *info);

void EEPROM_Default_RARDelayTim(RAR_Info *RarInfo);
uint8_t EEPROM_Write_RARDelayTim(RAR_Info *RarInfo);
uint8_t EEPROM_Read_RARDelayTim(RAR_Info *RarInfo);

#endif


