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

** #########################################################################*/
#ifndef EEPROM_CFG_H__
#define EEPROM_CFG_H__

/*---------------------------------------------------------------------------
- I N C L U D E   F I L E S
----------------------------------------------------------------------------*/
#include "common_types.h"
#include "boot_app_cfg.h"

/*---------------------------------------------------------------------------
- D E F I N E S / M A C R O S
----------------------------------------------------------------------------*/
#define NVM_EEPROM_EXT_EN  //开启本模块

#define RAM_EEPROM_OP_RAM_ADDR RAM_NO_INIT_ADDR
#define RAM_EEPROM_DATA_RAM_ADDR RAM_WDOG_ADDR
//#define assert_param DEVICE_ASSERT

#define EEPROM_FLASH_CAN_REWRITE_TO_BIT_ZERO   //芯片支持flash再次把bit=1写成bit=0--复旦微支持，杰发不支持

#define FLASH_ADDRS_ALIGN            4
#define FLASH_ADDRS_ALIGN_RES_MASK   0x03u //0xFFFFFFFCu 

//以下是公共配置
#define EFLASH_PAGE_SIEZ        (u16)0x0800    //真正的扇区大小，这里叫做小页的大小
#define EEPROM_VIRT_ADDRESS_OFFSET 1u  //地址的偏移，因为0000是标志，要加1，当做0001来用  -- 逻辑地址

#define EEPROM_EXT_TASK_TIME_UNIT  1u
#define EEPROM_EXT_TIMEOUT  (5000/EEPROM_EXT_TASK_TIME_UNIT)

#define EEPROM_EXT_UN_WRITE_INIT                  ((u32)0xFFFFFFFF)     /* 沒有該數據時，讀取回來的默認值*/
#define EEPROM_READ_U32_NUM  4u //每次讀取幾個U32----实际含义是每次读取U8的个数
#define EEPROM_FLAG_NOT_FIRST_RESET 0x57263762u   //RAM区分是否初次上电--不能亂改要和boot程序保持一致，测试完都改回 0x57263762u
#define EEPROM_FLAG_FIRST_RESET_CLEAR 0x00000000u   //清除--RAM区分是否初次上电
#define EFLASH_READ_NOT_VALID_ADDR_CUR 0x0u  //0xffffffffu

#define EEPROM_OPTIMIZE_SEARCH_FAST  //优化查找的速度--直接定位到当前地址
#ifdef EEPROM_OPTIMIZE_SEARCH_FAST
#define EEPROM_EXT_DATA_MAX_OFFSET 10u //多偏移几个来查找，防止找漏了
#endif

////以下是按不同片区划分，而不同
#define EEPROM_AREA_ID_0   0u
#define EEPROM_AREA_ID_1   1u
#define EEPROM_AREA_ID_COM EEPROM_AREA_ID_0   //片区1 只在app用到的储存
#define EEPROM_AREA_ID_APP EEPROM_AREA_ID_1   //片区2 在app和boot都用到的储存
#define EEPROM_AREA_TOTAL 2u   //片区的划分，根据用途不同
#define EEPROM_AREA_TOTAL_FOR_RAM 2u   //片区的划分，根据用途不同


///////和应用相关---片区1
#define EEPROM_AREA0_EFLASH_PAGE_NUM  1u   //每个大页，内部有多少个小页

#define EFLASH_AREA0_MASS_PAGE_SIZE  (u16)((u16)EFLASH_PAGE_SIEZ*EEPROM_AREA0_EFLASH_PAGE_NUM)  /* Page size = 8KByte  大的页，一共有两大页，用于切换使用*/

/* EEPROM start address in Flash */
#define EFLASH_AREA0_START_ADDRESS    ((u32)0x0001C000) /* EEPROM emulation start address: after 64KByte of used Flash memory */

/* Pages 0 and 1 base and end addresses */
#define EFLASH_AREA0_PAGE0_BASE_ADDRESS      ((u32)(EFLASH_AREA0_START_ADDRESS + 0x0000u))
#define EFLASH_AREA0_PAGE0_END_ADDRESS       ((u32)(EFLASH_AREA0_START_ADDRESS + (EFLASH_AREA0_MASS_PAGE_SIZE - 1u)))

#define EFLASH_AREA0_PAGE1_BASE_ADDRESS      ((u32)(EFLASH_AREA0_START_ADDRESS + EFLASH_AREA0_MASS_PAGE_SIZE))
#define EFLASH_AREA0_PAGE1_END_ADDRESS       ((u32)(EFLASH_AREA0_START_ADDRESS + (2u * EFLASH_AREA0_MASS_PAGE_SIZE - 1u)))

/* Variables' number */
#define EEPROM_AREA0_ADDR_TOTAL_U32 (((u16)EFLASH_AREA0_MASS_PAGE_SIZE/2u/4u)-1u)   //一个大扇区，转换成多少个字，
#define EEPROM_AREA0_ADDR_TOTAL_U8  (EEPROM_AREA0_ADDR_TOTAL_U32*4u)

#define EEPROM_AREA0_RAM_ADDR_START 0x20006000u   //相映射的 RAM地址，要和icf上定义的相一致
#define EEPROM_AREA0_RAM_ADDR_END (EEPROM_AREA0_RAM_ADDR_START+EEPROM_AREA0_ADDR_TOTAL_U8-1u) //0x002FFFu 最後一頁缺少4字節,要減少 47FB
#define EEPROM_AREA0_RAM_ADDR_START_OFFSET 0u  //


///////和应用相关---片区2
#define EEPROM_AREA1_EFLASH_PAGE_NUM  3u   //每个大页，内部有多少个小页

#define EFLASH_AREA1_MASS_PAGE_SIZE  (u16)((u16)EFLASH_PAGE_SIEZ*EEPROM_AREA1_EFLASH_PAGE_NUM)  /* Page size = 8KByte  大的页，一共有两大页，用于切换使用*/

/* EEPROM start address in Flash */
#define EFLASH_AREA1_START_ADDRESS    ((u32)EFLASH_AREA0_PAGE1_END_ADDRESS+1u) /* EEPROM emulation start address: after 64KByte of used Flash memory */

/* Pages 0 and 1 base and end addresses */
#define EFLASH_AREA1_PAGE0_BASE_ADDRESS      ((u32)(EFLASH_AREA1_START_ADDRESS + 0x0000u))
#define EFLASH_AREA1_PAGE0_END_ADDRESS       ((u32)(EFLASH_AREA1_START_ADDRESS + (EFLASH_AREA1_MASS_PAGE_SIZE - 1u)))

#define EFLASH_AREA1_PAGE1_BASE_ADDRESS      ((u32)(EFLASH_AREA1_START_ADDRESS + EFLASH_AREA1_MASS_PAGE_SIZE))
#define EFLASH_AREA1_PAGE1_END_ADDRESS       ((u32)(EFLASH_AREA1_START_ADDRESS + (2u * EFLASH_AREA1_MASS_PAGE_SIZE - 1u)))

/* Variables' number */
#define EEPROM_AREA1_ADDR_TOTAL_U32 (((u16)EFLASH_AREA1_MASS_PAGE_SIZE/2u/4u)-1u)   //一个大扇区，转换成多少个字，
#define EEPROM_AREA1_ADDR_TOTAL_U8  (EEPROM_AREA1_ADDR_TOTAL_U32*4u)

#define EEPROM_AREA1_RAM_ADDR_START 0x20006400u //(EEPROM_AREA0_RAM_ADDR_END+1u)   //
#define EEPROM_AREA1_RAM_ADDR_END (EEPROM_AREA1_RAM_ADDR_START+EEPROM_AREA1_ADDR_TOTAL_U8-1u) //0x002FFFu 最後一頁缺少4字節,要減少 47FB
#define EEPROM_AREA1_RAM_ADDR_START_OFFSET 0u  //


#if EEPROM_AREA_TOTAL > 1u
#define EEPROM_AERA_END_ADDR (EFLASH_AREA1_PAGE1_END_ADDRESS+1u-4u)
#else
#define EEPROM_AERA_END_ADDR (EFLASH_AREA0_PAGE1_END_ADDRESS+1u-4u)
#endif

//总的
#define EEPROM_FLASH_ADDR_START EFLASH_AREA0_START_ADDRESS   	//总的eeprom的flash的起始地址
#define EEPROM_FLASH_ADDR_END 0x1FFFFu //总的eeprom的flash的结束地址--暂时没用到，先用 EEPROM_AERA_END_ADDR


////RAM总区间范围
#define EEPROM_RAM_ADDR_START 0x20006000u   	//相映射的 RAM地址，要和icf上定义的相一致
#define EEPROM_RAM_ADDR_END (0x20006FFF-4u)//(0x20006FFF-4u*EEPROM_AREA_TOTAL_FOR_RAM) 	//见icf---注意，这里减多少，和EEPROM_AREA1_RAM_ADDR_START有关

#define EEPROM_RAM_MAX_U8 ((EFLASH_AREA0_MASS_PAGE_SIZE+EFLASH_AREA1_MASS_PAGE_SIZE)/4u)


#define EEPROM_AREAx_EFLASH_PAGE_NUM(x)  g_eeprom_user_info[x].sector_num
#define EFLASH_AREAx_MASS_PAGE_SIZE(x)  g_eeprom_user_info[x].mass_page_size_byte
#define EEPROM_AREAx_ADDR_TOTAL_U32(x)  g_eeprom_user_info[x].mass_page_addr_total_u32
#define EEPROM_AREAx_ADDR_TOTAL_U8(x)  g_eeprom_user_info[x].mass_page_addr_total_u8
#define EEPROM_AREAx_RAM_ADDR_START(x)  g_eeprom_user_info[x].ram_start_addr
#define EEPROM_AREAx_RAM_ADDR_END(x)  g_eeprom_user_info[x].ram_end_addr
#define EFLASH_AREAx_PAGE0_BASE_ADDRESS(x)  g_eeprom_user_info[x].eflash_page0_start_addr
#define EFLASH_AREAx_PAGE0_END_ADDRESS(x)  g_eeprom_user_info[x].eflash_page0_end_addr
#define EFLASH_AREAx_PAGE1_BASE_ADDRESS(x)  g_eeprom_user_info[x].eflash_page1_start_addr
#define EFLASH_AREAx_PAGE1_END_ADDRESS(x)  g_eeprom_user_info[x].eflash_page1_end_addr




#ifdef NVM_EEPROM_EXT_EN
#define EEPROM_EXT_STATE_MACHILE_ENABLE //用状态机方式，实现
#define EEPROM_EXT_NOT_TRANS_MORE_WHEN_FULL //写满时，不要重复的擦写
#endif
#define EEPROM_EXT_JUDGE_PAGE_ADDR  //为了安全，在最底层，再加一次判断地址范围

//extern MEMORY_MAP_FLAG_NO_OPTIME MEMORY_MAP_RAM_EEPROM u8 g_ram_for_eeprom_data[EEPROM_RAM_MAX_U8];


#ifdef NVM_EEPROM_EXT_EN
//#define EEPROM_EXT_DEBUG_EN  //变量跟踪
//#define EEPROM_EXT_DEBUG_IO_EN  //io测试时间
//#define EEPROM_INPUT_TEST   //修改RAM值，用来测试
#endif


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



/*---------------------------------------------------------------------------
- G L O B A L   V A R I A B L E S
- only configuration table allowed here,variables are not allowed!
----------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
- C O N S T A N T S
----------------------------------------------------------------------------*/

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

#ifdef EEPROM_INPUT_TEST

//给外部接口--任务
void eeprom_ext_input_test(void);

#endif

#if 0
#define EEPROM_EXT_TEST_ON() TEST1_ON()
#define EEPROM_EXT_TEST_OFF() TEST1_OFF()

#define EEPROM_EXT_TEST1_ON() TEST1_ON()
#define EEPROM_EXT_TEST1_OFF() TEST1_OFF()

#define EEPROM_EXT_ERASE_TEST_ON() //TEST1_ON()
#define EEPROM_EXT_ERASE_TEST_OFF() //TEST1_OFF()
#else

#ifdef EEPROM_EXT_DEBUG_IO_EN

void EEPROM_EXT_TEST_ON(u8 id);
void EEPROM_EXT_TEST_OFF(u8 id);

#define EEPROM_EXT_TEST1_ON EEPROM_EXT_TEST_ON
#define EEPROM_EXT_TEST1_OFF EEPROM_EXT_TEST_OFF

#define EEPROM_EXT_ERASE_TEST_ON EEPROM_EXT_TEST_ON
#define EEPROM_EXT_ERASE_TEST_OFF EEPROM_EXT_TEST_OFF

#endif

void eeprom_test_task(void);

#endif


#endif /* __EEPROM_H */

/*******************************END OF FILE************************************/
