/** ##########################################################################
**     Filename  :
**     Project   :
**     Module    :
**     Processor :
**     Version   :
**     Compiler  :
**     Date/Time :
**     Abstract  :
**     Contents  :
**     Note      :
**
**     (c) Copyright dmdz Co.,Ltd
**  --------------------------------------------------------------------------
**               R E V I S I O N   H I S T O R Y
**  --------------------------------------------------------------------------
**  Date       Ver   Author             Description

**  -20191106-  --V01--  --LYJ---  --初版--
**  -20230602-  --V1.0--  --mingyea---  --修改--

** #########################################################################*/

/*---------------------------------------------------------------------------
- I N C L U D E   F I L E S
----------------------------------------------------------------------------*/
#include "error.h"
#include "calibration.h"
#include "cpu.h"
#include "nvm.h"

//#include "nvm_drive.h"
//#include "EEPROM.h"
/*---------------------------------------------------------------------------
- D E F I N E S / M A C R O S
----------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
- T Y P E   D E F I N I T I O N S
----------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
- S T A T I C   V A R I A B L E S
----------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
* G L O B A L   V A R I A B L E S
----------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
- C O N S T A N T S
----------------------------------------------------------------------------*/
#ifdef __ICCARM__
//110078207 0110078207/001
MEMORY_MAP_FLAG_NO_OPTIME const u8 g_sysdid_f180[SYSDID_F180_CK_LEN] @SYSDID_F180_ADDR =
#else
MEMORY_MAP_FLAG_NO_OPTIME const u8 g_sysdid_f180[SYSDID_F180_CK_LEN] __attribute__((at(SYSDID_F180_ADDR))) =
#endif
{
    0x18,0x03,0x01,0x01,0x00,0x00,0x18,0x01,0x14,0x00
};


#ifdef __ICCARM__
//110078207 0110078207/001
MEMORY_MAP_FLAG_NO_OPTIME const u8 g_sysdid_f181[SYSDID_F181_CK_LEN] @SYSDID_F181_ADDR =
#else
MEMORY_MAP_FLAG_NO_OPTIME const u8 g_sysdid_f181[SYSDID_F181_CK_LEN] __attribute__((at(SYSDID_F181_ADDR))) =
#endif
{
    0x01,0x30,0x30,0x30,0x30,0x30,0x31,0x31,0x30,0x30,0x37,0x38,0x32,0x30,0x38,0x00,
};

MEMORY_MAP_FLAG_NO_OPTIME const u8 g_sysdid_f194[SYSDID_F194_CK_LEN] @SYSDID_F194_SELF_ADDR =
{
   0x4Du,0x54u,0x00u,0x10u,0xADu,0x1Eu,0x00u,0x00u,0x01u,0x7Du  //boot区的F194的校验码必须有，等于前面各字节之和
};
#ifdef __ICCARM__
MEMORY_MAP_FLAG_NO_OPTIME const  u8 g_sysdid_f1f2[SYSDID_F1F2_CK_LEN] @SYSDID_F1F2_ADDR =
#else
MEMORY_MAP_FLAG_NO_OPTIME const u8 g_sysdid_f1f2[SYSDID_F1F2_CK_LEN] __attribute__((at(SYSDID_F1F2_ADDR))) =
#endif
{
    //BOOTSW0100-00
    //'B','O','O','T','S','W',0x30,0x32,0x30,0x30,'-',0x30,0x30,0x35,0x00,
    0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,0xffu,
};


const did_attribute_t did_attribute_table[DID_ATTRIBUTE_TABLE_LENGTH] =
{
	{0xF180u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F180_ADDR, SYSDID_F180_CK_LEN},
	{0xF181u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F181_ADDR, SYSDID_F181_CK_LEN},
	{0xF1F2u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F1F2_ADDR, SYSDID_F1F2_CK_LEN},
	{0xF182u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F182_ADDR, SYSDID_F182_CK_LEN},
	{0xF187u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F187_ADDR, SYSDID_F187_CK_LEN},
	{0xF188u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F188_ADDR, SYSDID_F188_CK_LEN},
	{0xF18Au, DID_TYPE_CONST,   READ_ONLY, SYSDID_F18A_ADDR, SYSDID_F18A_CK_LEN},
	{0xF18Bu, DID_TYPE_CONST,   READ_ONLY, SYSDID_F18B_ADDR, SYSDID_F18B_CK_LEN},
	{0xF18Cu, DID_TYPE_VOLATILE,   READ_AND_WRITE, SYSDID_ECU_SN_ADDR, SYSDID_ECU_SN_CK_LEN},
	{0xF191u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F191_ADDR, SYSDID_F191_CK_LEN},
	{0xF192u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F192_ADDR, SYSDID_F192_CK_LEN},
	{0xF193u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F193_ADDR, SYSDID_F193_CK_LEN},
	{0xF194u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F194_ADDR, SYSDID_F194_CK_LEN},
	{0xF195u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F195_ADDR, SYSDID_F195_CK_LEN},    //(uint32_t)&g_sysdid_f195[0]
	{0xF1F1u, DID_TYPE_CONST,   READ_ONLY, SYSDID_F1F1_ADDR, SYSDID_F1F1_CK_LEN},
	{0xF19eu, DID_TYPE_CONST,   READ_ONLY, SYSDID_F19E_ADDR, SYSDID_F19E_CK_LEN},
	{0xF1A0u, DID_TYPE_VOLATILE,   READ_ONLY, SYSDID_VEHICLE_CONFIG_ADDR, SYSDID_VEHICLE_CONFIG_CK_LEN},
	{0x01A0u, DID_TYPE_VOLATILE,   READ_AND_WRITE, SYSDID_MANUFAC_LIFECYCLE_COUNT_ADDR, SYSDID_MANUFAC_LIFECYCLE_COUNT_CK_LEN},
	{0x01B0u, DID_TYPE_VOLATILE,   READ_ONLY, SYSDID_SECURITY_KEY_ADDR, SYSDID_SECURITY_KEY_CK_LEN},
	{0xF190u, DID_TYPE_VOLATILE,   READ_AND_WRITE, SYSDID_F190_VIN_ADDR, SYSDID_F190_VIN_CK_LEN},
	{0xF198u, DID_TYPE_VOLATILE,   READ_AND_WRITE, SYSDID_F198_REPAIR_SN_ADDR, SYSDID_F198_REPAIR_SN_CK_LEN},
	{0xF199u, DID_TYPE_VOLATILE,   READ_AND_WRITE, SYSDID_F199_DATE_ADDR, SYSDID_F199_DATE_CK_LEN},
	{0xF100u, DID_TYPE_VOLATILE,   READ_AND_WRITE, SYSDID_ATTEMP_ADDR, SYSDID_ATTEMP_LEN+SYSDID_ATTEMP_MAX_LEN}
};


/*---------------------------------------------------------------------------
- F U N C T I O N   P R O T O T Y P E
----------------------------------------------------------------------------*/

void cal_did_test(void);




/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
----------------------------------------------------------------------------*/
static uint8_t get_did_headler(did_t did)

{
	uint8_t i;
	u8 l_result =0xffu;
	for(i = 0; i < DID_ATTRIBUTE_TABLE_LENGTH; i++)
	{
		if(did == did_attribute_table[i].id)
		{
			l_result = i;
			break;
		}
	}
	return l_result;
}

/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
----------------------------------------------------------------------------*/
u8 cal_read_did(did_t did, uint8_t *p_buf, uint8_t length)
{
	uint8_t headler;
	uint8_t opt_length;
	u8 l_result = TRUE;

	CAL_CPU_SR_ALLOC();

	do
	{
		headler = get_did_headler(did) ;
		if( (headler == 0xffu)  || (did_attribute_table[headler].rw == WRITE_ONLY) )
		{
			l_result =  FALSE;
			break;
		}

		if(length < did_attribute_table[headler].length)
		{
			opt_length = length;
		}
		else
		{
			opt_length = did_attribute_table[headler].length;
		}

		CAL_ENTER_CRITICAL();
		#if 0
		for(i = 0; i < opt_length; i++)
		{
			p_buf[i]  = did_attribute_table[headler].addr[i];
		}
		#endif
		if(did_attribute_table[headler].type == DID_TYPE_CONST)
		{
			(void)nvm_read_bytes(NVM_TYPE_FLASH,did_attribute_table[headler].phy_addr,p_buf,(u16)opt_length);
		}
		else
		{
			(void)nvm_read_bytes(NVM_TYPE_EEPROM,did_attribute_table[headler].phy_addr,p_buf,(u16)opt_length);
		}
		CAL_EXIT_CRITICAL();
	}while(0);

	return l_result;
}

/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
----------------------------------------------------------------------------*/
u8 cal_write_did(did_t did, const uint8_t *p_buf, uint8_t length)
{
	uint8_t l_result = FALSE;
	uint8_t headler;
	uint8_t opt_length;
	CAL_CPU_SR_ALLOC();	/*PRQA S 0318,3205*/

	do
	{
		headler = get_did_headler(did) ;
		if( (headler == 0xffu) || (did_attribute_table[headler].rw == READ_ONLY))
		{
			l_result =  FALSE;
			break;
		}

		if(length < did_attribute_table[headler].length)
		{
			opt_length = length;
		}
		else
		{
			opt_length = did_attribute_table[headler].length;
		}
		CAL_ENTER_CRITICAL();
		#if 0
		for(i = 0; i < opt_length; i++)
		{
			did_attribute_table[headler].addr[i] = p_buf[i];
		}
		#endif
		if(did_attribute_table[headler].type == DID_TYPE_CONST)
		{
			l_result = TRUE;
			//(void)nvm_write_bytes(NVM_TYPE_FLASH,did_attribute_table[headler].phy_addr,p_buf,opt_length);
		}
		else
		{
			l_result = nvm_write_bytes(NVM_TYPE_EEPROM,did_attribute_table[headler].phy_addr,(u8*)&p_buf[0],(u16)opt_length);   /*PRQA S 0311*/
			if((u8)ERR_OK == l_result)
			{
				l_result = TRUE;
			}
		}
		CAL_EXIT_CRITICAL();
	}while(0);

	return l_result;
}

/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
----------------------------------------------------------------------------*/
void cal_did_init(void)
{
	uint8_t  i;
	//u8 j;
	//u16_swap_t l_buf[8];
	//u8 l_data[64];

	CAL_CPU_SR_ALLOC();	/*PRQA S 0318,3205*/

	for(i = 0; i < DID_ATTRIBUTE_TABLE_LENGTH; i++)
	{

		#ifdef FUN_WDT_ENABLE
		wdog_task();
		#endif

		if(did_attribute_table[ i ].type == DID_TYPE_CONST)
		{

			continue;  /*PRQA S 2005*/
		}
		CAL_ENTER_CRITICAL();

		CAL_EXIT_CRITICAL();


	}

	//cal_did_test();
}


//static u8 g_cal_refresh_flag = 0u;  /*PRQA S 3207*/


/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
----------------------------------------------------------------------------*/
void cal_did_read_refresh(void)
{
	#if 0
	u8 j;
	u16_swap_t l_buf[16]={0};

	if(g_cal_refresh_flag ==0u)
	{
		g_cal_refresh_flag = 1;

		#ifdef FUN_WDT_ENABLE
		wdog_task();
		#endif

		#if 1
		/*F180*/
		for(j=0;j<8;j++)
		{
			#ifdef CAL_USE_NVM
			(void)eeprom_get_u16((eeprom_addr_t)(EEPROM_DATA_BASE_ADDR+EEPROM_F180_ADDR+2*j), &l_buf[j].w);
			#endif
			volatile_did.boot_soft_id[2*j] = l_buf[j].w >> 8;
			volatile_did.boot_soft_id[2*j+1] = l_buf[j].w;
		}
		#endif

		#if 1
		/*F187*/
		for(j=0;j<5;j++)
		{
			l_buf[j].w = (u16)const_did.geely_spare_part_number[2*j] << 8 | const_did.geely_spare_part_number[2*j+1] ;
			#ifdef CAL_USE_NVM
			(void)eeprom_set_u16((eeprom_addr_t)(EEPROM_DATA_BASE_ADDR+EEPROM_F187_ADDR+2*j), l_buf[j].w);
			#endif
		}
		#endif

		#if 1
		/*F189*/
		for(j=0;j<8;j++)
		{
			l_buf[j].w = (u16)const_did.ecu_software_version_number[2*j] << 8 | const_did.ecu_software_version_number[2*j+1] ;
			#ifdef CAL_USE_NVM
			(void)eeprom_set_u16((eeprom_addr_t)(EEPROM_DATA_BASE_ADDR+EEPROM_F189_ADDR+2*j), l_buf[j].w);
			#endif
		}
		#endif

		#if 1

		/*F193*/
		for(j=0;j<8;j++)
		{
			l_buf[j].w = (u16)const_did.system_supplier_ecu_hardware_version_number[2*j] << 8 | const_did.system_supplier_ecu_hardware_version_number[2*j+1] ;
			#ifdef CAL_USE_NVM
			(void)eeprom_set_u16((eeprom_addr_t)(EEPROM_DATA_BASE_ADDR+EEPROM_F193_ADDR+2*j), l_buf[j].w);
			#endif
		}
		#endif


	}
	#endif
}



/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		: 判斷數組的值是否都為 value;
----------------------------------------------------------------------------*/
u8 buf_judge_value_is_same(uc8 *pdata,u8 len ,u8 value)
{

	u8 l_result = TRUE;

	u8 i;

	for(i=0;i<len;i++)
	{
		if( pdata[i] != value )
		{
			l_result = FALSE;
			break;
		}
	}

	return l_result;

}
#if 0
//u8 *g_cal_paddr;
volatile u8 g_cal_w_buf[50];
volatile u8 g_cal_r_buf[50];
void cal_did_test(void)
{
	//g_cal_paddr = (u8*)&g_sysdid_f195[0];
	cal_read_did(0xF180u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF100u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF181u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF1F2u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF182u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF187u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF188u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF18au,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF18bu,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF18cu,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF191u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF192u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF193u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF194u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF195u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF1f1u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF19eu,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF190u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF190u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF198u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF199u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF195u,(u8*)&g_cal_w_buf[0],50);
	cal_write_did(0xF190u,(u8*)&g_cal_w_buf[0],50);
	cal_read_did(0xF190u,(u8*)&g_cal_r_buf[0],50);
	cal_read_did(0xF1A0u,(u8*)&g_cal_r_buf[0],50);
	g_cal_w_buf[0]= 0x56;
	cal_write_did(0x01A0u,(u8*)&g_cal_w_buf[0],50);
	cal_read_did(0x01A0u,(u8*)&g_cal_r_buf[0],50);
	g_cal_w_buf[0]= 0x77;
	cal_write_did(0x01b0u,(u8*)&g_cal_w_buf[0],50);
	cal_read_did(0x01b0u,(u8*)&g_cal_r_buf[0],50);
	g_cal_w_buf[0]= 0x99;
	cal_write_did(0xF198u,(u8*)&g_cal_w_buf[0],50);
	cal_read_did(0xF198u,(u8*)&g_cal_r_buf[0],50);
	g_cal_w_buf[0]= 0x88;
	cal_write_did(0xF199u,(u8*)&g_cal_w_buf[0],50);
	cal_read_did(0xF199u,(u8*)&g_cal_r_buf[0],50);
	//(void)nvm_read_bytes(NVM_TYPE_FLASH,(u32)&g_cal_paddr,&g_cal_r_buf[0],SYSDID_F195_CK_LEN);
	//(void)nvm_read_bytes(NVM_TYPE_FLASH,(u32)&g_sysdid_f195[0],&g_cal_w_buf[0],SYSDID_F195_CK_LEN);
}
#endif

#ifdef CALIBRATION_DISP_ADDR_DEBUG

typedef struct
{
	u16 addr;
	u8 len;
}cal_disp_s;

MEMORY_MAP_FLAG_NO_OPTIME const cal_disp_s g_cal_eeprom_addr[] =
{
	{SYSDID_ATTEMP_ADDR,SYSDID_ATTEMP_LEN},
	{SYSDID_ATTEMP_MAX_ADDR,SYSDID_ATTEMP_MAX_LEN},
	{SYSDID_ATTEMP_CRC_ADDR,SYSDID_ATTEMP_CK_LEN},
	{SYSDID_ECU_SN_ADDR,SYSDID_ECU_SN_CK_LEN},
	{SYSDID_F190_VIN_ADDR,SYSDID_F190_VIN_CK_LEN},
	{SYSDID_F198_REPAIR_SN_ADDR,SYSDID_F198_REPAIR_SN_CK_LEN},
	{SYSDID_F199_DATE_ADDR,SYSDID_F199_DATE_CK_LEN},
	{SYSDID_MANUFAC_LIFECYCLE_COUNT_ADDR,SYSDID_MANUFAC_LIFECYCLE_COUNT_CK_LEN},
	{SYSDID_SECURITY_KEY_ADDR,SYSDID_SECURITY_KEY_CK_LEN},
	{SYSDID_SA_LOCKTIMER_TIME_ADDR,SYSDID_SA_LOCKTIMER_TIME_LEN},
	{SYSDID_SA_LOCKTIMER_FLAG_ADDR,SYSDID_SA_LOCKTIMER_FLAG_LEN},
	{SYSDID_SA_LV1_ATTEMPT_ADDR,SYSDID_SA_LV1_ATTEMPT_LEN},
	{SYSDID_SA_LC_CRC_ADDR,SYSDID_SA_LOCKTIMER_CK_LEN},
	{SYSDID_SU_LOCKTIMER_TIME_ADDR,SYSDID_SU_LOCKTIMER_TIME_LEN},
	{SYSDID_SU_LOCKTIMER_FLAG_ADDR,SYSDID_SU_LOCKTIMER_FLAG_LEN},
	{SYSDID_SU_LV1_ATTEMPT_ADDR,SYSDID_SU_LV1_ATTEMPT_LEN},
	{SYSDID_SU_LC_CRC_ADDR,SYSDID_SU_LOCKTIMER_CK_LEN},
	{SYSDID_VEHICLE_CONFIG_ADDR,SYSDID_VEHICLE_CONFIG_CK_LEN},
	{SYSDID_SW_VERSION_FOR_SG_ADDR,SYSDID_SW_VERSION_FOR_SG_CK_LEN},
	{SYSDID_RES1_ADDR,SYSDID_RES1_CK_LEN},
	{SYSDID_RES2_ADDR,SYSDID_RES2_CK_LEN},
	{SYSDID_RES3_ADDR,SYSDID_RES3_CK_LEN},
	{SYSDID_RES4_ADDR,SYSDID_RES4_CK_LEN},
	{SYSDID_RES5_ADDR,SYSDID_RES5_CK_LEN},
	{SYSDID_RES6_ADDR,SYSDID_RES6_CK_LEN},
	{SYSDID_SA_TOTAL_TIMEOUT_ADDR,SYSDID_SA_TOTAL_TIMEOUT_CK_LEN},
	{SYSDID_SU_TOTAL_TIMEOUT_ADDR,SYSDID_SU_TOTAL_TIMEOUT_CK_LEN},
};

#endif


/*PRQA S 1503*/
