#ifndef _NON_VOLATILE_MEMORYS_H_
#define _NON_VOLATILE_MEMORYS_H_




    ////////////////////////////以下地址及定义不要修改       ////////////////////////////////////////////////////////////




/*//BOOT存储地址              //长度16个字节 
 #define DFLASH_BOOTAddress 	  0x100000   
#define DFLASH_BOOT_LEN	  8   //字 */   


                




    ////////////////////////////以上地址及定义不要修改       //////////////////////////////////////////////////////////// 









/******************************************************************************
非易失性数据存储器配置
******************************************************************************/
#define NVM_USE_EEPROM 1 //0：断开EEPROM,强制使用数据默认值 \
                                                //1：使用EEPROM保存非易失性数据
/******************************************************************************
初始化标识

数据组名称： NVM_INIT_DATA
信号成员： -- NVM_INIT_FLAG
******************************************************************************/
//TYW
typedef union {
    struct
    {
        uint8_t Flag[4];
    } Sig;
    uint8_t Data[4];
} NVMInitUnion;

EXTERN NVMInitUnion NVMInit;

static const NVMInitUnion NVMDefaultInit =
    {
        /* Flag[] =*/"tyw",
};

#define NVM_INIT_FLAG NVMInit.Sig.Flag

/******************************************************************************
里程总计
数据组名称： NVM_ODO_DATA
信号成员：    NVM_ODO_VALUE
******************************************************************************/
typedef union {
    struct
    {                        
        uint32_t Value;
        uint8_t ClrCnt;
        uint8_t Rsvd;
        uint16_t Cleared;
    } Sig;
    uint8_t Data[8];
} NVMODOUnion;

EXTERN NVMODOUnion NVMODO;

static const NVMODOUnion NVMDefaultODO =
{
        /* Value = */ 0,
        /* ClrCnt = */ 0,
        /* Rsvd = */ 0,
        /* Cleared = */ 0,
};

#define NVM_ODO_VALUE NVMODO.Sig.Value
#define NVM_ODO_CLEAR_COUNT NVMODO.Sig.ClrCnt
#define NVM_ODO_CLEARED NVMODO.Sig.Cleared



/******************************************************************************
故障码存储         
******************************************************************************/
#define cnDTCslen 20 //18

typedef struct                         
{
    uint8_t  ECUPowerVoltage;
    uint16_t VehicleSpeed;
    uint8_t  OccurrenceCounter;
    uint32_t FirstMalfunction;
    uint32_t LastMalfunction;
    uint16_t EngineSpeed;
    uint8_t  PowerRange;
    uint8_t  TimeOfFailure_S;
    uint8_t  TimeOfFailure_Min;   
    uint8_t  TimeOfFailure_Hr;   
    uint8_t  TimeOfFailure_Month;   
    uint8_t  TimeOfFailure_Day;   
    uint8_t  TimeOfFailure_Year;   
} SnapshotUnion;

typedef struct
{
    uint8_t  DTCStatus;
    uint8_t  DTCAgeing;
    uint8_t  DTCCount;
    uint8_t  DTCPendingCount;
    SnapshotUnion Snapshot;
} DTCUnion;

typedef struct
{
    DTCUnion DTC[27u];
} DTCToDFlashInfoUnion;

EXTERN DTCToDFlashInfoUnion DTCToDFlashInfo;
static const DTCUnion NVMDTC_DEFAULT = {0x50,0,0,0,{0,0,0,0,0,0,0,0,0,0,0,0,0}};
/******************************************************************************/
typedef union {
    struct
    {
        uint32_t TripA_NUM : 32;
    } Sig;
    uint8_t Data[4];
} NVMTRIPUnion;
/*
static const NVMTRIPUnion NVMDefaultTrip = 
{
    0
};
   */
EXTERN NVMTRIPUnion NVMTRIP;

#define NVM_Trip_VALUE NVMTRIP.Sig.TripA_NUM

/*** 结构声明 ***/
typedef union 
{
    struct
    {
        uint32_t ServiseSet : 32;
        uint32_t Service : 32;
        INT8U Service_Reset : 1;//首次保养里程标志
        uint32_t Service_Frist : 32; 
        
    }Sig;
    uint8_t Data[13];	    //  uint8_t Data[9];
}NVMSERVISEUnion;

EXTERN NVMSERVISEUnion  NVMSERVISE;

/*** 信号默认值 ***/
static const NVMSERVISEUnion NVMDefaultServise =
{
    50000, 50000, 0, 100000,	     //间隔保养里程，保养大计，保养标志位,首次保养里程
};


#define NVM_SERVISE_SET NVMSERVISE.Sig.ServiseSet   //记录保养设置设置值（首保设置与间隔设置）
#define NVM_USER_SETTINGS_SERVICE NVMSERVISE.Sig.Service   //记录保养大计
#define NVM_SERVICE_RESET NVMSERVISE.Sig.Service_Reset       //	  保养标志位
#define NVM_SERVICE_FIRST NVMSERVISE.Sig.Service_Frist	       //记录首次保养设置值


/***********************************总里程清0************************************/

typedef union 
{
    struct
    {
        uint32_t ResetTotalODO : 32;
        uint8_t ResetODOTime : 8; 
    }Sig;
    uint8_t Data[5];
}NVMClearODOUnion;

EXTERN NVMClearODOUnion  NVMClearODO;

static const NVMClearODOUnion NVMClearODOInit = {0,0};

#define NVM_SUM_ODO_Reset     NVMClearODO.Sig.ResetTotalODO
#define NVM_Time_ODO_Reset    NVMClearODO.Sig.ResetODOTime 

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



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

static const INT8U NVMDefault_Zero[21] =
    {
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
};





//BOOT跳转标志     
#define Precondition_Check_Fail   0
#define Precondition_Check_Sucess   1



#define Jump_Flag_APP   0x66
#define Jump_Flag_BOOT   0x55
   typedef union 
{
    struct
    {
        INT8U   Header;                     //0xA5
        INT8U   Need_Respond;         //是否有需要回复的信息   
        INT8U   Session;           //会话
        INT8U   Lock_Level ;       //解锁安全等级    
        INT8U   BOOT0_Valid;     
        INT8U   BOOT1_Valid;     
        INT8U   APP_Valid; 
        INT8U   Jump_Flag;      //跳转标志
        INT8U   Precondition_Check_Result ;
        INT8U   :8;
        INT8U   :8;
        INT8U   :8;
        INT8U   :8;
        INT8U   :8;
        INT8U   :8;

        INT8U  CheckSUM;              
    }BIT;
    INT8U Data[16];
}_NVM_Boot_Flag;

/**********************Dflash*******************************/



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


typedef union 
{
    struct
    {
        uint8_t PPK_Erase : 1;
        uint8_t  : 7;
    } Sig;
    uint8_t Data[1];
}NVM_PPK_Default_Erase;

EXTERN NVM_PPK_Default_Erase NVM_PPK_Erase;

#define NVM_PPK_Erase_Flag NVM_PPK_Erase.Sig.PPK_Erase

/******************************************************************************
存储数据属性表
******************************************************************************/
/*** 结构声明 ***/
typedef struct
{
    uint8_t *pData;
    const uint8_t *pDefaultValue;
    uint8_t Len;
    uint8_t DataType;
    uint16_t Addr;
} NVMDataAttributeTableStruct;


/******************************************************************************
数据更新控制结构
******************************************************************************/
EXTERN uint8_t NVMEEPROMFault;
#define NVM_EEPROM_FAULT NVMEEPROMFault


#define NVM_CONFIG_ADDR_0_0 0X0620
#define NVM_CONFIG_ADDR_1_0 0X0030





/*** 存储数据列表 ***/
#define NVM_INIT_DATA      0
#define NVM_ODO_DATA       1
#define NVM_Servise_DATA   2
#define NVM_DTC            3
#define NVM_ResetODO       23


//#define NVM_TRIP_DATA 2
//#define NVM_CONFIG_0 3
//#define NVM_CONFIG_1 4

//#define NVM_DTC_DATA 5

//#define NVM_F19D_DATA 6
#define NVM_F190_DATA 5
#define NVM_PPK_FLAG 6
//#define NVM_F199_DATA 8
//#define NVM_F19A_DATA 9

/*** 存储数据属性表 ***/
static const NVMDataAttributeTableStruct NVMDataAttributeTable[] =
{
  /*===================================================================================================================================================================
           数据名称         |        数据存储位置        |                    默认值                     |              数据长度              |                 存储类型                   |存储地址
  -------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  {/* NVM_INIT_DATA*/        NVMInit.Data, 	                       	NVMDefaultInit.Data,  	                        sizeof(NVMInit), 										 EEPROM_HIREL_DATA_TYPE, 						0x0000},//0行
  {/* NVM_ODO_DATA*/         NVMODO.Data, 	            	          NVMDefaultODO.Data, 													  sizeof(NVMODO), 										 EEPROM_HIREL_DATA_TYPE, 						0x0001},//1行
  {/*NVMSERVISE*/            NVMSERVISE.Data,                       NVMDefaultServise.Data, 												sizeof(NVMSERVISE),  								 EEPROM_NORMAL_DATA_TYPE,						0x00D0},//9//13
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[0]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[0]),      EEPROM_NORMAL_DATA_TYPE,           0x00E0},//9//13	
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[1]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[1]),      EEPROM_NORMAL_DATA_TYPE,           0x00FA},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[2]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[1]),      EEPROM_NORMAL_DATA_TYPE,           0x0114},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[3]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[2]),      EEPROM_NORMAL_DATA_TYPE,           0x012E},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[4]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[3]),      EEPROM_NORMAL_DATA_TYPE,           0x0148},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[5]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[4]),      EEPROM_NORMAL_DATA_TYPE,           0x0162},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[6]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[5]),      EEPROM_NORMAL_DATA_TYPE,           0x017C},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[7]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[6]),      EEPROM_NORMAL_DATA_TYPE,           0x0196},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[8]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[7]),      EEPROM_NORMAL_DATA_TYPE,           0x01B0},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[9]),   (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[8]),      EEPROM_NORMAL_DATA_TYPE,           0x01CA},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[10]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[9]),      EEPROM_NORMAL_DATA_TYPE,           0x01E4},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[11]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[10]),     EEPROM_NORMAL_DATA_TYPE,           0x01FE},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[12]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[11]),     EEPROM_NORMAL_DATA_TYPE,           0x0218},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[13]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[12]),     EEPROM_NORMAL_DATA_TYPE,           0x0232},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[14]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[13]),     EEPROM_NORMAL_DATA_TYPE,           0x024C},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[15]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[14]),     EEPROM_NORMAL_DATA_TYPE,           0x0266},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[16]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[15]),     EEPROM_NORMAL_DATA_TYPE,           0x0280},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[17]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[16]),     EEPROM_NORMAL_DATA_TYPE,           0x029A},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[19]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x02B4},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[20]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x02CE},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[21]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x02E8},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[22]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x0302},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[23]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x031C},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[24]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x0336},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[25]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x0350},
  {/*DTC*/                   (uint8_t*)&(DTCToDFlashInfo.DTC[26]),  (uint8_t*)&(NVMDTC_DEFAULT.DTCStatus),          sizeof(DTCToDFlashInfo.DTC[18]),     EEPROM_NORMAL_DATA_TYPE,           0x036A},
  {/*Clear ODO*/             NVMClearODO.Data,                      NVMClearODOInit.Data,                           sizeof(NVMClearODO),                 EEPROM_NORMAL_DATA_TYPE,           0x0384},  

  /*=================================================================================================================================================================*/
};

/*** 存储数据总数 ***/
#define NVM_TOTAL_DATA_NUM (sizeof(NVMDataAttributeTable) / sizeof(NVMDataAttributeTableStruct))

typedef struct
{
    uint8_t Enable;
    uint8_t Req[NVM_TOTAL_DATA_NUM];
    uint8_t Current;
} NVMUpdateCtrlStruct;

extern NVMUpdateCtrlStruct NVMUpdateCtrl;
/******************************************************************************
函数声明
******************************************************************************/
void NVM_Init(void);
void NVM_Data_Update(uint8_t DataName);
void NVM_Data_Update_Service(void);
void Delay_Ms(INT8U Time);
#endif