/** ##########################################################################
**     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

**  -20230602-  --V1.0--  --mingyea---  --修改--

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

/*---------------------------------------------------------------------------
- I N C L U D E   F I L E S
----------------------------------------------------------------------------*/
#include "eeprom.h"
#include "common_cfg.h"
#include "common_memory.h"
#include "error.h"
#include "gpio.h"
#include "wdog.h"
#include "nvm.h"
#include "eeprom_state_machine.h"

#ifdef NVM_EEPROM_EXT_EN

/*---------------------------------------------------------------------------
- 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
----------------------------------------------------------------------------*/
#ifdef EEPROM_EXT_DEBUG_EN
eeprom_input_test_s g_eeprom_input_test[EEPROM_AREA_TOTAL_FOR_RAM]  = {0u};  //MEMORY_MAP_RAM_FLAG_NOINIT
#endif

MEMORY_MAP_RAM_FLAG_NOINIT eeprom_ext_s g_eeprom_ext[EEPROM_AREA_TOTAL_FOR_RAM] @RAM_EEPROM_OP_RAM_ADDR;
eeprom_ext_test_s g_eeprom_ext_test[EEPROM_AREA_TOTAL_FOR_RAM] ;

//MEMORY_MAP_FLAG_NO_OPTIME MEMORY_MAP_RAM_EEPROM u8 g_ram_for_eeprom_data[EEPROM_RAM_MAX_U8] = {0u};

#if 0  //debug的時候，開啟該宏
#else
u8 *g_ram_for_eeprom[EEPROM_AREA_TOTAL_FOR_RAM] =
{
	(u8*)(u32)EEPROM_AREA0_RAM_ADDR_START,
	#if EEPROM_AREA_TOTAL>1u
	(u8*)(u32)EEPROM_AREA1_RAM_ADDR_START
	#endif
};
#endif

u32 g_logic_page_read_data[EEPROM_AREA_TOTAL_FOR_RAM] MEMORY_MAP_RAM_WDOG @RAM_EEPROM_DATA_RAM_ADDR ; //逻辑页的数据--注意，该地址也是未初始化的，见ICF
u32 g_logic_page_write_data[EEPROM_AREA_TOTAL_FOR_RAM] MEMORY_MAP_RAM_WDOG @(RAM_EEPROM_DATA_RAM_ADDR+(4u*EEPROM_AREA_TOTAL_FOR_RAM)) ; //逻辑页的数据
u8 g_logic_page_read_u8data[EEPROM_AREA_TOTAL_FOR_RAM][4]  MEMORY_MAP_RAM_WDOG @(RAM_EEPROM_DATA_RAM_ADDR+(8u*EEPROM_AREA_TOTAL_FOR_RAM));


/*---------------------------------------------------------------------------
- C O N S T A N T S
----------------------------------------------------------------------------*/
const eeprom_user_info_s g_eeprom_user_info[EEPROM_AREA_TOTAL_FOR_RAM] =
{
	{
		EEPROM_AREA0_EFLASH_PAGE_NUM,
		EFLASH_AREA0_MASS_PAGE_SIZE,
		EEPROM_AREA0_ADDR_TOTAL_U32,
		EEPROM_AREA0_ADDR_TOTAL_U8,
		EEPROM_AREA0_RAM_ADDR_START,
		EEPROM_AREA0_RAM_ADDR_END,
		EFLASH_AREA0_PAGE0_BASE_ADDRESS,
		EFLASH_AREA0_PAGE0_END_ADDRESS,
		EFLASH_AREA0_PAGE1_BASE_ADDRESS,
		EFLASH_AREA0_PAGE1_END_ADDRESS,
	},
	#if EEPROM_AREA_TOTAL >1u
	{
		EEPROM_AREA1_EFLASH_PAGE_NUM,
		EFLASH_AREA1_MASS_PAGE_SIZE,
		EEPROM_AREA1_ADDR_TOTAL_U32,
		EEPROM_AREA1_ADDR_TOTAL_U8,
		EEPROM_AREA1_RAM_ADDR_START,
		EEPROM_AREA1_RAM_ADDR_END,
		EFLASH_AREA1_PAGE0_BASE_ADDRESS,
		EFLASH_AREA1_PAGE0_END_ADDRESS,
		EFLASH_AREA1_PAGE1_BASE_ADDRESS,
		EFLASH_AREA1_PAGE1_END_ADDRESS,
	},
	#endif
};


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

/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	: area_id: Var片区
|Output parameters	:
|Return value		:
|Description		: 返回当前有效页
----------------------------------------------------------------------------*/
u16 eeprom_get_cur_page_id(u8 area_id)
{
	assert_param(area_id<EEPROM_AREA_TOTAL);
	return g_eeprom_ext[area_id].current_page;
}

/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	: area_id: Var片区
|Output parameters	:
|Return value		:
|Description		: 返回，快速定位地址
----------------------------------------------------------------------------*/
u32 eeprom_get_cur_end_addr(u8 area_id)
{
	assert_param(area_id<EEPROM_AREA_TOTAL);
	return g_eeprom_ext[area_id].current_end_addr;
}



#ifdef EEPROM_EXT_DEBUG_IO_EN
//测试相关

void EEPROM_EXT_TEST_ON(u8 id)
{
	if(id == 0u)
	{
		TEST1_ON();
	}
	else
	{
		TEST2_ON();
	}
}

void EEPROM_EXT_TEST_OFF(u8 id)
{
	if(id == 0u)
	{
		TEST1_OFF();
	}
	else
	{
		TEST2_OFF();
	}
}
#endif

/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	: 
|Output parameters	:
|Return value		:
|Description		: 任务
----------------------------------------------------------------------------*/
void eeprom_test_task(void)
{
	u16 i,j;
	u8 l_temp;
	u8 l_buf[4];
	u32 l_data;
	#ifdef EEPROM_EXT_DEBUG_EN
	if(g_eeprom_input_test[EEPROM_AREA_ID_0].flag==0x2)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_0].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_0].count++;
		for(i = 0; i < 1; i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x56+i + g_eeprom_input_test[EEPROM_AREA_ID_0].count;
			//eeprom_write_u8_ext(0+i,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_0].test_count[58]++;
		#endif
	}

	if(g_eeprom_input_test[EEPROM_AREA_ID_0].flag==0x3)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_0].count++;
		#if 0
		for( j=0;j<(EEPROM_AREA0_ADDR_TOTAL_U32/4);j++)
		{
			l_data=0u;
			for(i = 0; i < 4; i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
			{
				wdog_task();
				l_temp = 0x56+i + g_eeprom_input_test[EEPROM_AREA_ID_0].count+4*j;
				//eeprom_write_u8_ext(j*4+i,&l_temp,1);
				nvm_write_bytes(NVM_TYPE_EEPROM,j*4+i,&l_temp,1);
				l_data |= (u32)l_temp<<(8*i);
			}
			#ifdef EEPROM_EXT_STATE_MACHILE_ENABLE
			#else
			//写入ram的值，
			//eeprom_write_one_element_seq(EEPROM_AREA_ID_0,j,l_data);
			#endif
		}
		#else
			l_buf[0]=0xffu;
			l_buf[1]=0xffu;
			l_buf[2]=0xffu;
			l_buf[3]=0xffu;
			nvm_write_bytes(NVM_TYPE_EEPROM,0,&l_buf[0],4);
		#endif
		//擦除
		g_eeprom_input_test[EEPROM_AREA_ID_0].flag=1u;
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_0].test_count[59]++;
		#endif
	}

	if(g_eeprom_input_test[EEPROM_AREA_ID_0].flag==0x5)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_0].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_0].count++;
		for(i = 0; i < EEPROM_AREA0_ADDR_TOTAL_U8; i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x99+i + g_eeprom_input_test[EEPROM_AREA_ID_0].count;
			//eeprom_write_u8_ext(0+i,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_0].test_count[60]++;
		#endif
		eeprom_state_machine_area_resatrt(EEPROM_AREA_ID_0);
	}

	if(g_eeprom_input_test[EEPROM_AREA_ID_0].flag==0x6)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_0].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_0].count++;
		for(i = 0; i < (EEPROM_AREA0_ADDR_TOTAL_U8/4); i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x99+i + g_eeprom_input_test[EEPROM_AREA_ID_0].count;
			//eeprom_write_u8_ext(0+i,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_0].test_count[61]++;
		#endif
		eeprom_state_machine_area_resatrt(EEPROM_AREA_ID_0);
		//g_eeprom_ext.page_count=0u;
	}


	if(g_eeprom_input_test[EEPROM_AREA_ID_1].flag==0x2)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_1].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_1].count++;
		for(i = 0; i < 1; i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x56+i + g_eeprom_input_test[EEPROM_AREA_ID_1].count;
			//eeprom_write_u8_ext(0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_1].test_count[62]++;
		#endif
	}

	if(g_eeprom_input_test[EEPROM_AREA_ID_1].flag==0x3)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_1].count++;
		#if 0
		for( j=0;j<(EEPROM_AREA1_ADDR_TOTAL_U32/4);j++)
		{
			l_data=0u;
			for(i = 0; i < 4; i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
			{
				wdog_task();
				l_temp = 0x56+i + g_eeprom_input_test[EEPROM_AREA_ID_1].count+4*j;
				//eeprom_write_u8_ext(j*4+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
				nvm_write_bytes(NVM_TYPE_EEPROM,j*4+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
				//eeprom_write_u8_ext1(j*4+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
				l_data |= (u32)l_temp<<(8*i);
			}
			#ifdef EEPROM_EXT_STATE_MACHILE_ENABLE
			#else
			//写入ram的值，
			//eeprom_write_one_element_seq(EEPROM_AREA_ID_1,j,l_data);
			#endif
		}
		#else
		l_buf[0]=0xffu;
		l_buf[1]=0xffu;
		l_buf[2]=0xffu;
		l_buf[3]=0xffu;
		nvm_write_bytes(NVM_TYPE_EEPROM,0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_buf[0],4);
		#endif
		//擦除
		g_eeprom_input_test[EEPROM_AREA_ID_1].flag=1u;
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_1].test_count[63]++;
		#endif
	}

	if(g_eeprom_input_test[EEPROM_AREA_ID_1].flag==0x5)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_1].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_1].count++;
		for(i = 0; i < EEPROM_AREA1_ADDR_TOTAL_U8; i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x99+i + g_eeprom_input_test[EEPROM_AREA_ID_1].count;
			//eeprom_write_u8_ext(0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_1].test_count[64]++;
		#endif
		eeprom_state_machine_area_resatrt(EEPROM_AREA_ID_1);
	}

	if(g_eeprom_input_test[EEPROM_AREA_ID_1].flag==0x6)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_1].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_1].count++;
		for(i = 0; i < (EEPROM_AREA1_ADDR_TOTAL_U8/4); i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x99+i + g_eeprom_input_test[EEPROM_AREA_ID_1].count;
			//eeprom_write_u8_ext(0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_1].test_count[65]++;
		#endif
		eeprom_state_machine_area_resatrt(EEPROM_AREA_ID_1);
		//g_eeprom_ext.page_count=0u;
	}


	if(g_eeprom_input_test[EEPROM_AREA_ID_1].flag==0x7)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_1].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_1].count++;
		for(i = 0; i < 1; i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x99+i + g_eeprom_input_test[EEPROM_AREA_ID_1].count;
			//eeprom_write_u8_ext(0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
			//eeprom_write_u8_ext1(0+i+EEPROM_AREA1_RAM_ADDR_START-EEPROM_AREA0_RAM_ADDR_START,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_1].test_count[66]++;
		#endif
		//eeprom_state_machine_area_resatrt(EEPROM_AREA_ID_1);
		//g_eeprom_ext.page_count=0u;
	}

	#endif

}



#ifdef EEPROM_INPUT_TEST

/*---------------------------------------------------------------------------
|Prototype			:
|Called by			:
|Preconditions		:
|Input parameters	: 
|Output parameters	:
|Return value		:
|Description		: 给外部接口--任务
----------------------------------------------------------------------------*/
void eeprom_ext_input_test(void)
{
	u16 i,j;
	u8 l_temp;
	u8 l_buf[4];
	u32 l_data;

	//if(g_eeprom_input_test[EEPROM_AREA_ID_0].flag==0x6)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_0].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_0].count++;
		for(i = 0; i < (EEPROM_AREA0_ADDR_TOTAL_U8/4); i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x99+i + g_eeprom_input_test[EEPROM_AREA_ID_0].count;
			//eeprom_write_u8_ext(0+i,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_0].test_count[61]++;
		#endif
		//eeprom_state_machine_area_resatrt(EEPROM_AREA_ID_0);
		//g_eeprom_ext.page_count=0u;
	}

	//if(g_eeprom_input_test[EEPROM_AREA_ID_1].flag==0x6)
	{
		g_eeprom_input_test[EEPROM_AREA_ID_1].flag=1u;
		//写入ram的值，
		g_eeprom_input_test[EEPROM_AREA_ID_1].count++;
		for(i = 0; i < (EEPROM_AREA1_ADDR_TOTAL_U8/4); i++)  //EEPROM_AREA0_ADDR_TOTAL_U8
		{
			wdog_task();
			l_temp = 0x99+i + g_eeprom_input_test[EEPROM_AREA_ID_1].count;
			//eeprom_write_u8_ext(0+i,&l_temp,1);
			nvm_write_bytes(NVM_TYPE_EEPROM,0+i,&l_temp,1);
		}
		#ifdef EEPROM_EXT_DEBUG_EN
		g_eeprom_ext_test[EEPROM_AREA_ID_1].test_count[61]++;
		#endif
		//eeprom_state_machine_area_resatrt(EEPROM_AREA_ID_1);
		//g_eeprom_ext.page_count=0u;
	}

}
#endif


#endif