/**
	******************************************************************************
	* @file
	* @author
	* @version
	* @date
	* @brief
	* @function List:
	******************************************************************************
	* @attention
	*
	*
	* <h2><center>&copy; COPYRIGHT 2021 </center></h2>
	******************************************************************************
	* @History:
	* @Author:
	* @Data:
	* @Version:
*/
#ifndef _DEM_CFG_H_
#define _DEM_CFG_H_

#include "Dcm_Cfg.h"
#include "Dem_Types.h"
#include "PduR_Dcm.h"
#include "calibration.h"
#include "nvm.h"
typedef u16 dem_addr_t;  //TEMP_MODIFY

#define TOTAL_DTC_NUM  (NUM_OF_DTC)    /*配置 NUM_OF_DTC */
#define DTC_STATUS_USED_MASK  (0x49U)  /*配置 DTC MASK */
#define DTC_SNAP_SHOT_DATA_ENABLE (STD_OFF)  /*配置是否支持快照 0: 不使用snap shot data  */
#define DTC_EXTENDED_DATA_ENABLE (STD_OFF)  /*配置是否支持擴展數據:0: 不使用EXTENDEDt data  */

#define DEM_DTC_STORAGE_TO_EEPROM_EN  (STD_ON)
#define TOTAL_MAX_STORED_DTC_NUM (NUM_OF_DTC)  /*配置最大的 存儲 DTC 的個數 */

//#define DTC_TASK_TICK_PER_SEC  (TASK_TICKS_PER_SEC*0.5)  /*配置 CAN TASK PERIOD:100/s   CAN 任務 每秒100次  */
//#define DTC_TIME_s(x) ((u16)((x)*DTC_TASK_TICK_PER_SEC))

#define EEPROM_INTIED_FLAG_ADDR             (EEPROM_LOGIC_BASE_ADDR_FOR_DTC)   //NVM_START_ADDR_DTC  --  注意 
#define DEM_DTC_STORAGE_EEPROM_START_ADDR   (dem_addr_t)(EEPROM_INTIED_FLAG_ADDR+2u)  /*配置 DTC 存儲首地址 */

#if (TOTAL_DTC_NUM <253)
typedef uint8_t dtc_handle_t;
#else
typedef uint16_t dtc_handle_t;  
#endif

#define DTC_NO_EXIST     ((dtc_handle_t)(-1))	//其實就是 ff表示 不存在 
#define DTC_IS_IN_GROUP  ((dtc_handle_t)(-2))	//其實就是 fe表示在組內

#define DTC_INIT_FLAG_STORY_TO_EXT_EEPROM
#define DTC_RECORD_STORY_TO_EXT_EEPROM

#define DTC_ALL_GROUPS 			0xFFFFFFu
#define DTC_NETWORK_COMM_GROUPS	0xC00000u
#define DTC_BODY_GROUPS 		0x800000u
#define DTC_CHASSIS_GROUPS 		0x400000u
#define DTC_POWERTRAIN_GROUPS 	0X100000u
#define DTC_EMISSION_GROUPS 	0x000000u



typedef uint16 IntEEPROM1_TAddress; /* Type of address to the EEPROM */


#define EEPROM_UNINITED		     0u
#define EEPROM_INITED	         1u
#define EEPROM_DTC_CLEAR         2u

#define DTC_SET_TIME_FOR_OUTPUT      (80u)   /* 80*10MS = 800mS */
#define DTC_TEST_FAILED              (0u)
#define DTC_TEST_PASSED              (1u)

#define TEST_RESULT_STATE_BIT_LENGTH  (2U)
#define TOTAL_TEST_RESULT_NUM        ((TOTAL_DTC_NUM)/(8/(TEST_RESULT_STATE_BIT_LENGTH)) + 1)

#define TOTAL_DTC_FLAG_NUM                (TOTAL_DTC_NUM / 8 + 1)
//#define DTC_MONITORING_CYCLE_NUM          (TOTAL_STORED_DTC_NUM / 8 + 1)
#define LAST_DTC_INDEX					  (TOTAL_DTC_NUM-1)
#define NO_DTC_STORED_INDEX               (0xffu)



#define TOTAL_DTC_MONITORING_CYCLE        2u
#define DTC_MONITORING_CYCLE2             0u
#define DTC_MONITORING_CYCLE4             1u

#define TOTAL_DTC_SNAP_SHOT_ID_NUM        4u  /* FE-5 4 did */
#define TOTAL_DTC_EXTENDED_DATA_NUM       1u
#define MAX_DTC_AGING_COUNTER_VALUE       40u

#define MIN_DTC_FAULT_DT_CNT_VALUE        16u		/* -128 + 16 */
#define MAX_DTC_FAULT_DT_CNT_VALUE        223u		/* 127 - 32 */
#define DTC_FAULT_DT_CNT_UP_STEP          32u
#define DTC_FAULT_DT_CNT_DOWN_STEP        16u

#define DTC_MONITORING_CYCLE0_MASK        0x01u
#define DTC_MONITORING_CYCLE1_MASK        0x02u
#define DTC_MONITORING_CYCLE2_MASK        0x04u
#define DTC_MONITORING_CYCLE3_MASK        0x08u
#define DTC_MONITORING_CYCLE4_MASK        0x10u

/* Define DTC index  */


#define DTC_POWER_LOW_ID 0u  /*0*/  /*低於9v  500ms*/
#define DTC_POWER_HIGH_ID 1u /*1*/  /*超過16v  500ms*/

#define DTC_PREV_BUTTON_STUCK_ID 2u  //向上搜台
#define DTC_PHONE_BUTTON_STUCK_ID 3u  //电话
#define DTC_VOICE_BUTTON_STUCK_ID 4u
#define DTC_VEDIO_BUTTON_STUCK_ID 5u
#define DTC_NEXT_BUTTON_STUCK_ID 6u
#define DTC_MUTE_BUTTON_STUCK_ID 7u
#define DTC_CUSTOM_BUTTON_STUCK_ID 8u
#define DTC_MENU2_CTRL_BUTTON_STUCK_ID 9u
#define DTC_LOST_COM_WITH_GW_ID 10u
#define DTC_SPDP_BUTTON_STUCK_ID 11u
#define DTC_SPDD_BUTTON_STUCK_ID 12u
#define DTC_DSPD_BUTTON_STUCK_ID 13u
#define DTC_DSPP_BUTTON_STUCK_ID 14u
#define DTC_LEFT_MENU_STUCK_ID 15u
#define DTC_LEFT_PICK_STUCK_ID 16u
#define DTC_RIGHT_PICK_STUCK_ID 17u
#define DTC_HEAT_OVERCURRENT_INDEX 18u
#define DTC_SWH_STRIP_SHORT_INDEX 19u
#define DTC_SWH_STRIP_OPEN_INDEX 20u
#define DTC_SWH_NTC_SHORT_INDEX 21u
#define DTC_SWH_NTC_OPEN_INDEX 22u

#define  DTC_GROUP_BODY_INDEX_MIN (0U)
#define  DTC_GROUP_BODY_INDEX_MAX (DTC_TAILGATE_PE_SW_STUCK_ID)

#define  DTC_GROUP_NETWORK_INDEX_MIN (DTC_GROUP_BODY_INDEX_MAX+1)
#define  DTC_GROUP_NETWORK_INDEX_MAX (DTC_LOST_COMMUNICATION_IMMO_ID)

#define DTC_VOLTAGE_IS_TOO_LOW  (DTC_POWER_LOW_ID)
#define DTC_VOLTAGE_IS_TOO_HIGH (DTC_POWER_HIGH_ID)







typedef struct{
	dtc_status_t dtc_status;  
#if (DTC_SNAP_SHOT_DATA_ENABLE) /* not used for brilliance-auto */
	dtc_snap_shot_data_t DTCSnapShotData;
#endif
#if (DTC_EXTENDED_DATA_ENABLE)	
	dtc_extended_data_t DTCextended_data;
#endif
} dtc_stored_data_t;

typedef struct{
	u8 write_pointer;   /* DTC_INDEX Stored to RAM */
	u8 stored_dtc_index[TOTAL_MAX_STORED_DTC_NUM];  /* DTC_INDEX Stored to RAM ----是把SupportedDTC中的需要儲存到eeprom的buf id 轉到該數組中 所以<=11*/
	/* Don't save cMonitoringCycle in EEPROM, it just used for  the increasing of monitory cycle counter . */
	dtc_stored_data_t dtc_stored_dt[TOTAL_MAX_STORED_DTC_NUM];
} dtc_info_struct_t;

#define SYS_PARM_STRGE_START_ADDR (0)

//#define DTC_SNAPSHOT_RECORD_LENGTH  (sizeof(dtc_snap_shot_data_t) + 8)  /* snap_shot_data LENGTH + DID 4*2  BYTE LENGTH) */
#define DTC_SNAPSHOT_RECORD_LENGTH  (sizeof(dtc_snap_shot_data_t) + 2 * TOTAL_DTC_SNAP_SHOT_ID_NUM)  /* snap_shot_data LENGTH + DID 4*2  BYTE LENGTH) */
#define DTC_EXTDATA_RECORD_LENGTH  (sizeof(dtc_extended_data_t) + 1 * TOTAL_DTC_EXTENDED_DATA_NUM)  /* snap_shot_data LENGTH + DID 4*2  BYTE LENGTH) */


#define _SnapShort_size_0     (fld_sizeof(dtc_snap_shot_data_t, main_ecu_supply_voltage))
#define _SnapShort_size_1     (fld_sizeof(dtc_snap_shot_data_t, vehicle_speed))
#define _SnapShort_size_2     (fld_sizeof(dtc_snap_shot_data_t, odometer))
#define _SnapShort_size_3     (fld_sizeof(dtc_snap_shot_data_t, tbox_datatime))


#define _SnapShort_ADDR_0     (offsetof1(dtc_snap_shot_data_t, main_ecu_supply_voltage) + SYS_PARM_STRGE_START_ADDR)
#define _SnapShort_ADDR_1     (offsetof1(dtc_snap_shot_data_t, vehicle_speed) + SYS_PARM_STRGE_START_ADDR)
#define _SnapShort_ADDR_2     (offsetof1(dtc_snap_shot_data_t, odometer) + SYS_PARM_STRGE_START_ADDR)
#define _SnapShort_ADDR_3     (offsetof1(dtc_snap_shot_data_t, tbox_datatime) + SYS_PARM_STRGE_START_ADDR)

#define _ExtData_ADDR_1     (sizeof(dtc_status_t) +sizeof(dtc_snap_shot_data_t) + SYS_PARM_STRGE_START_ADDR)
#define _ExtData_ADDR_2     (_ExtData_ADDR_1 + 1)


//extern const uint32 SupportedDTC[NUM_OF_DTC];

#if (DTC_SNAP_SHOT_DATA_ENABLE)
extern const u8 c_dtc_snap_shot_data_id[TOTAL_DTC_SNAP_SHOT_ID_NUM][2];
extern const u8 c_dtc_snap_shot_data_size[TOTAL_DTC_SNAP_SHOT_ID_NUM];
#endif

#if (DTC_EXTENDED_DATA_ENABLE)
extern const u8 c_dtc_extended_data_id[TOTAL_DTC_EXTENDED_DATA_NUM];
#endif


extern const u32 SupportedDTC[TOTAL_DTC_NUM] ;


#define DEM_TASK_TIME_UNIT 5   //10Ms跑一次任务
#define DEM_TASK_WAIT_EEPROM_INIT (3000/DEM_TASK_TIME_UNIT)   //等待eeprom初始化的超时
#define DEM_CFG_ENABLE_MACHINE_STATE_FOR_INIT  //使能--初始化，要等到eeprom成功讀取之後


#endif
