add eeprom.c
authorkyz <kongyuzhong@google.com>
Thu, 18 Jun 2026 08:46:14 +0000 (16:46 +0800)
committerkyz <kongyuzhong@google.com>
Thu, 18 Jun 2026 09:09:15 +0000 (17:09 +0800)
12 files changed:
.gitignore
Aging_app/eeprom/Inc/ee_config.h [new file with mode: 0644]
Aging_app/eeprom/Inc/ee_lowlevel.h [new file with mode: 0644]
Aging_app/eeprom/Inc/ee_middlelevel.h [new file with mode: 0644]
Aging_app/eeprom/Inc/eep_emulation.h [new file with mode: 0644]
Aging_app/eeprom/Src/ee_lowlevel.c [new file with mode: 0644]
Aging_app/eeprom/Src/ee_middlelevel.c [new file with mode: 0644]
Aging_app/eeprom/Src/eep_emulation.c [new file with mode: 0644]
Aging_app/freertos/IARProject/freertos.ewp [new file with mode: 0644]
Aging_app/freertos/inc/eeprom.h [new file with mode: 0644]
Aging_app/freertos/src/eeprom.c [new file with mode: 0644]
Aging_app/freertos/src/main.c

index 84f4b9855c493137aa92975d8dc3bd50bac30d4a..94ac9250d2bfcc39ec98bfd0cda476bc93c5cc97 100644 (file)
@@ -2,8 +2,7 @@
 *.dep
 *.swp
 *.ewd
-*.ewp
 *.ewt
 Debug/
 *.log
-settings/
\ No newline at end of file
+settings/
diff --git a/Aging_app/eeprom/Inc/ee_config.h b/Aging_app/eeprom/Inc/ee_config.h
new file mode 100644 (file)
index 0000000..90722f3
--- /dev/null
@@ -0,0 +1,22 @@
+/***************************************************************************//**
+ * @file     ee_config.h
+ * @brief    EEPROM emulation config header file.
+ * @version  V1.2
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021 Zhixin Semiconductor Ltd. All rights reserved..
+ * 
+ ******************************************************************************/
+#ifndef EE_CONFIG_H
+#define EE_CONFIG_H
+
+#define Z20K11X_SDK     1U
+#define Z20K14X_SDK     2U
+
+//#define Z20K1XX_SDK       Z20K11X_SDK
+#define Z20K1XX_SDK       Z20K14X_SDK
+
+
+#endif
\ No newline at end of file
diff --git a/Aging_app/eeprom/Inc/ee_lowlevel.h b/Aging_app/eeprom/Inc/ee_lowlevel.h
new file mode 100644 (file)
index 0000000..b88f4e8
--- /dev/null
@@ -0,0 +1,34 @@
+/***************************************************************************//**
+ * @file     ee_lowlevel.h
+ * @brief    EEPROM emulation low level header file.
+ * @version  V1.3
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.
+ * 
+ ******************************************************************************/
+#ifndef EE_LOWLEVEL_H
+#define EE_LOWLEVEL_H
+
+
+
+uint32_t EE_FlashReadHalfWord(uint32_t addr, uint16_t * halfWord);
+uint32_t EE_FlashReadWord(uint32_t addr, uint32_t * word);
+uint32_t EE_FlashRead(uint32_t addr, uint8_t * buffer, uint32_t size,void (*CallBack)(void));
+uint32_t EE_FlashReadByWord(uint32_t addr, uint32_t *buffer, uint32_t size,
+                            void (*CallBack)(void));
+
+uint32_t EE_FlashBlankCheck(uint32_t addr, uint32_t size, 
+                            FlagStatus_t *blankFlag, void (*CallBack)(void));
+uint32_t EE_DataCheck(uint32_t addr, const uint8_t *buf, uint32_t len,
+                      FlagStatus_t *flag, void (*CallBack)(void));
+
+uint32_t EE_FlashWriteWithPadding(uint32_t srcAddr, uint32_t len, 
+                                  uint32_t srcInFlash, uint32_t addr, 
+                                  void (*CallBack)(void));
+
+uint32_t EE_SectorErase(uint32_t addr, uint32_t size, void (*CallBack)(void));
+FlagStatus_t EE_SramCheckByWord(uint32_t *buf, uint32_t size, uint32_t value);
+#endif
\ No newline at end of file
diff --git a/Aging_app/eeprom/Inc/ee_middlelevel.h b/Aging_app/eeprom/Inc/ee_middlelevel.h
new file mode 100644 (file)
index 0000000..d6df9fe
--- /dev/null
@@ -0,0 +1,70 @@
+/***************************************************************************//**
+ * @file     ee_middlelevel.h
+ * @brief    EEPROM emulation middle level header file.
+ * @version  V1.3
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.
+ * 
+ ******************************************************************************/
+#ifndef EE_MIDDLELEVEL_H
+#define EE_MIDDLELEVEL_H
+
+#include "eep_emulation.h"
+
+#define EE_CACHE_ITEM_SIZE     4U
+
+#define EE_RECORD_ADDR_DELETED   0xFFFFFFFEU
+#define EE_RECORD_ADDR_EMPTY     0xFFFFFFFFU
+
+
+#define EE_ERASED_WORD         0xFFFFFFFFU
+
+#define EE_VALID_PATTERN_SIZE_IN_BYTE        EE_FL_PHRASE_SIZE
+#define EE_INVALID_PATTERN_SIZE_IN_BYTE      EE_FL_PHRASE_SIZE
+
+#define EE_VALID_PATTERN_SIZE_IN_WORD       (EE_VALID_PATTERN_SIZE_IN_BYTE >> 2U)
+#define EE_INVALID_PATTERN_SIZE_IN_WORD     (EE_INVALID_PATTERN_SIZE_IN_BYTE >> 2U)
+
+extern const uint32_t validPattern[EE_VALID_PATTERN_SIZE_IN_WORD];
+extern const uint32_t invalidPattern[EE_INVALID_PATTERN_SIZE_IN_WORD];
+
+void HalfwordToBytes(uint16_t src, uint8_t *dest);
+void WordToBytes(uint32_t src, uint8_t *dest);
+void EE_UpdateCacheTable(EE_cache_t* cTable, uint16_t recordID, 
+                                   uint32_t newValue);
+uint32_t EE_SearchInCache(EE_cache_t* cTable, uint16_t recordID, 
+                          uint32_t* recAddr);
+uint32_t EE_ReadRecordAtAddr(uint32_t recordAddr, uint16_t dataSize, 
+                             uint8_t *buf, uint16_t *readSize, 
+                             void (*CallBack)(void));
+uint32_t EE_SearchInSectors(EE_Config_t* eeConfig, uint16_t recordID, 
+                            uint32_t* recordAddr, void (*CallBack) (void));
+uint32_t EE_ValidateRecord(uint32_t recordAddr, void (*CallBack)(void));
+uint32_t EE_InvalidateRecord(uint32_t recordAddr, void (*CallBack)(void));
+uint32_t EE_WriteRecordInSector(EE_SectorInfo_t *secInfo, uint16_t recordID, 
+                                uint16_t size, const uint8_t *src, uint32_t srcInFlash,
+                                void (*CallBack)(void));
+uint16_t EE_AlignToPhraseSize(uint16_t size);
+uint32_t EE_ReadSectorHeader(uint32_t addr, EE_SectorHeader_t * head, 
+                             void (*CallBack)(void));
+uint32_t EE_ReadRecordHeader(uint32_t addr, EE_RecordHeader_t * head,
+                             void (*CallBack)(void));
+uint32_t EE_ValidateSector(const EE_SectorConfig_t *pSector, 
+                           void (*CallBack)(void));
+uint32_t EE_InvalidateSector(const EE_SectorConfig_t *pSector, 
+                             void (*CallBack)(void));
+uint32_t EE_EmptySector(const EE_SectorConfig_t* pSector, void (*CallBack)(void));
+uint32_t EE_FormatSector(const EE_SectorConfig_t* pSector, uint32_t id, 
+                         void (*CallBack) (void));
+uint32_t EE_ScanSector(EE_Config_t* eeConfig, ControlState_t swapEn, 
+                       void (*CallBack)(void));
+
+uint32_t EE_CheckSectorSpaceForRecord(EE_SectorInfo_t* secInfo, 
+                                      uint16_t dataSize);
+uint32_t EE_SectorSwap(EE_Config_t* eeConfig, void (*CallBack)(void));
+
+
+#endif
\ No newline at end of file
diff --git a/Aging_app/eeprom/Inc/eep_emulation.h b/Aging_app/eeprom/Inc/eep_emulation.h
new file mode 100644 (file)
index 0000000..fd6af7c
--- /dev/null
@@ -0,0 +1,283 @@
+/***************************************************************************//**
+ * @file     eep_emulation.h
+ * @brief    EEPROM emulation header file.
+ * @version  V1.3
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved..
+ * 
+ ******************************************************************************/
+#ifndef EEP_EMULATION_H
+#define EEP_EMULATION_H
+
+#include "ee_config.h"
+
+#if (defined(DEV_Z20K148M)) || (defined(DEV_Z20K146M))||(defined(DEV_Z20K144M))
+#include "common_drv.h"
+#else
+#include "Z20K11xM_drv.h"
+#endif
+
+
+
+/* Return code definition */
+#define EE_OK                                       0x00000000U
+#define EE_ERROR_BUSY                               0x00000001U
+#define EE_ERROR_DATA_NOT_FOUND                     0x00000002U
+#define EE_ERROR_NOT_IN_CACHE                       0x00000004U
+#define EE_ERROR_ECC                                0x00000008U
+#define EE_ERROR_INPUT_PARA                         0x00000010U
+#define EE_ERROR_NO_ENOUGH_SPACE                    0x00000020U
+#define EE_ERROR_FLASH_ERR                          0x00000040U
+#define EE_ERROR_CACHE_ITEM                         0x00000080U
+#define EE_ERROR_SWAP                               0x00000100U
+#define EE_ERROR_NOT_ENOUGH_SECTORS                 0x00000200U
+#define EE_ERROR_HEADER_INVALID                     0x00000400U
+#define EE_ERROR_WRITTEN_LOC_NOT_EMPTY              0x00000800U
+#define EE_ERROR_REC_ID_OUT_OF_RANGE                0x00001000U
+#define EE_ERROR_READ_REC_DATA_ECC                  0x00002000U
+#define EE_ERROR_READ_ECC                           0x00004000U
+#define EE_ERROR_WRITTEN_VALUE                      0x00008000U
+
+#define FLASH_MODULE      Z20K1XX
+
+#if (FLASH_MODULE == Z20K1XX)
+#define EE_FL_SECTOR_SIZE   8192U
+#define EE_FL_PHRASE_SIZE   16U
+#define EE_ECC_SIZE            EE_FL_PHRASE_SIZE
+#endif
+
+
+#define EE_SECTOR_ID_OFFSET         0U
+#define EE_SECTOR_ADDR_OFFSET       4U
+#define EE_SECTOR_SIZE_OFFSET       8U
+#define EE_SECTOR_CHECKSUM_OFFSET   12U
+#define EE_SECTOR_VALID_OFFSET      EE_FL_PHRASE_SIZE
+#define EE_SECTOR_INVALID_OFFSET    (EE_SECTOR_VALID_OFFSET + EE_FL_PHRASE_SIZE)
+#define EE_SECTOR_RECORD_OFFSET     (EE_FL_PHRASE_SIZE * 3U)
+#define EE_SECTOR_INFO_LEN           EE_FL_PHRASE_SIZE
+#define EE_SECTOR_VALID_LEN          EE_FL_PHRASE_SIZE
+#define EE_SECTOR_INVALID_LEN        EE_FL_PHRASE_SIZE
+#define EE_SECTOR_HEADER_LEN         (EE_SECTOR_INFO_LEN + EE_SECTOR_VALID_LEN + EE_SECTOR_INVALID_LEN)
+
+
+#define EE_RECORD_ID_OFFSET          0U
+#define EE_RECORD_SIZE_OFFSET        2U
+#define EE_RECORD_ADDR_OFFSET        4U
+#define EE_RECORD_CHECKSUM_OFFSET    8U
+#define EE_RECORD_VALID_OFFSET       EE_FL_PHRASE_SIZE
+#define EE_RECORD_INVALID_OFFSET     (EE_RECORD_VALID_OFFSET + EE_FL_PHRASE_SIZE)
+#define EE_RECORD_ID_LEN             2U
+#define EE_RECORD_SIZE_LEN           2U
+#define EE_RECORD_ADDR_LEN           4U
+#define EE_RECORD_CHECKSUM_LEN       4U
+#define EE_RECORD_INFO_LEN           EE_FL_PHRASE_SIZE
+#define EE_RECORD_VALID_LEN          EE_FL_PHRASE_SIZE
+#define EE_RECORD_INVALID_LEN        EE_FL_PHRASE_SIZE
+#define EE_RECORD_HEADER_LEN         (EE_RECORD_INFO_LEN + EE_RECORD_VALID_LEN + EE_RECORD_INVALID_LEN)
+
+
+/* Macros for CallBack period controlling */
+#define EE_CALLBACK_READ_BYTES_NUM               0x0020U
+#define EE_CALLBACK_SEARCH_BYTES_NUM             0x0020U
+#define EE_CALLBACK_VERIFY_BYTES_NUM             0x0020U
+
+
+
+#define MIN_SECTOR_NUM      2U
+
+#define EE_RETRY_TIMES      3U
+
+/* Define record status type */
+typedef enum
+{
+    EE_REC_STAT_VALID = 0U,
+    EE_REC_STAT_DELETED = 1U,
+    EE_REC_STAT_INCONSISTENT = 2U,
+    EE_REC_STAT_HEADER_INVALID = 3U,
+    EE_REC_STAT_HEADER_BLANK = 4U, 
+}EE_RecordStatus_t;
+
+/* Define sector status type */
+typedef enum
+{
+    EE_SEC_STAT_VALID = 0U,
+    EE_SEC_STAT_INVALIDATED = 1U,
+    EE_SEC_STAT_INCONSISTENT = 2U,
+    EE_SEC_STAT_HEADER_INVALID = 3U,
+    EE_SEC_STAT_HEADER_BLANK = 4U, 
+}EE_SectorStatus_t;
+
+
+/* Define Sector Config type */
+typedef struct
+{
+    uint32_t startAddr;        /* the sector start address */
+    uint32_t size;             /* the size of sector */
+}EE_SectorConfig_t;
+
+/* Define Sector run-time information type */
+typedef struct
+{
+    uint32_t index;            /* the sector index */ 
+    uint32_t id;               /* the ID of sector */
+    EE_SectorStatus_t status;  /* status of this sector */    
+    uint32_t recordAddr;       /* the address of next record header to be
+                                 written */
+    uint32_t dataAddr;         /* the address of current record data */      
+}EE_SectorInfo_t;
+    
+/* Define Sector header type */
+typedef struct
+{
+    uint32_t id;               /* the ID of active sector */
+    uint32_t startAddr;        /* the sector start address */
+    uint32_t size;             /* the size of sector */
+    EE_SectorStatus_t status;  /* status of this sector */    
+}EE_SectorHeader_t;
+
+/* record header type */
+typedef struct
+{    
+    EE_RecordStatus_t  recordStatus; /* record status */ 
+    uint32_t  dataStartAddr;         /* the record start address */    
+    uint16_t  dataSize;              /* the record size */
+    uint16_t  recordID;              /* the unique record ID */
+}EE_RecordHeader_t;
+
+/* Define Cache Table type */
+typedef struct
+{
+    uint32_t* cachePtr;               /* a pointer to cache table */
+    uint32_t num;                    /* the number of items
+                                        (each item is 4-byte) */
+}EE_cache_t;
+
+/* Define Sector Config type */
+typedef struct
+{
+    uint32_t sectorNum;             /* the number of sectors being used emulation */    
+    const EE_SectorConfig_t  **sectors;   /* the sector configuration array pointer */
+    ControlState_t cacheEn;         /* the flag to enable/disable the cache table */
+    EE_cache_t *cTable;             /* the cache table structure */  
+    EE_SectorInfo_t actSecInfo;     /* the information of active sector */ 
+    volatile FlagStatus_t busyFlag; /* the busy flag of the sectors */
+    uint16_t maxRecordId;           /* the maximum record ID supported. The 
+                                       record ID is from 0 - maxRecordId */
+}EE_Config_t;
+
+/***************************************************************************!
+ * @brief: Init the EEPROM Emulation driver
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  swapEn: It indicates if it swaps sectors when it finds invalid 
+ *                     record header during sector scanning.
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return result
+ *         EE_OK
+ *         EE_ERROR_BUSY
+ *         EE_ERROR_NOT_ENOUGH_SECTORS
+ *         EE_ERROR_FLASH_ERR
+ *         EE_ERROR_ECC
+ *         EE_ERROR_REC_HEADER_INVALID
+ *         EE_ERROR_INPUT_PARA
+ *         EE_ERROR_SWAP
+ *         
+ *
+ ****************************************************************************/
+uint32_t EE_Init(EE_Config_t* eeConfig, ControlState_t swapEn, 
+                 void (*CallBack) (void));
+
+/***************************************************************************//**
+ * @brief  This API is to read the specific data record. The data record size
+ *         is determined by the data size stored in emulated EEPROM.
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  recordID: the ID of record to be read
+ * @param[in]  dataSize: the size of data to be read from the record. It can not
+ *                       exceeds the size of record.
+ * @param[in]  buf: it points to the buffer that stores the data read out from
+ *                  record.
+ * @param[out]  acturalReadSize: the size of data that are actually read out 
+ *                               will be stored to the memory pointed by
+ *                               acturalReadSize after calling this function
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @note       User must ensure that the data size to be read does not exceed 
+ *             the buffer size. 
+ *             
+ *
+ * @return     EE_OK
+ *             EE_ERROR_BUSY -- EE is writing or erasing
+ *             EE_ERROR_DATA_NOT_FOUND
+ *             EE_ERROR_REC_ID_OUT_OF_RANGE
+ *             EE_ERROR_ECC
+ *             EE_ERROR_REC_HEADER_INVALID
+ *
+ ******************************************************************************/  
+uint32_t EE_ReadRecord(EE_Config_t* eeConfig, uint16_t recordID, 
+                       uint16_t dataSize, uint8_t *buf, 
+                       uint16_t *acturalReadSize, void (*CallBack) (void));
+
+/***************************************************************************
+ * @brief; This API is to write data records to the EEPROM emulated Flash
+ *          and re-write data record if this program operation fails.
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  recordID: the ID of record to be written
+ * @param[in]  dataSize: the size of data to be written. 
+ * @param[in]  buf: it points to the buffer that stores the data to be written
+ * @param[in]  srcInFlash: it indicates if the location of data(buf) to be 
+ *                         written is in FLASH.
+ *                         - 1: in flash
+ *                         - 0: not in flash
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return EE_OK
+ *         EE_ERROR_BUSY -- EE is writing or erasing
+ *         EE_ERROR_NO_ENOUGH_SPACE
+ *         EE_ERROR_SWAP
+ *         EE_ERROR_INPUT_PARA
+ *         EE_ERROR_WRITTEN_LOC_NOT_EMPTY
+ *         EE_ERROR_READ_REC_DATA_ECC
+ *         EE_ERROR_WRITTEN_VALUE
+ *         EE_ERROR_ECC
+ *
+ ****************************************************************************/
+uint32_t EE_WriteRecord(EE_Config_t* eeConfig, uint16_t recordID, 
+                        uint16_t dataSize,const uint8_t *buf, 
+                        uint32_t srcInFlash,void (*CallBack) (void));
+
+/***************************************************************************//**
+ * @brief  This API is to delete the specific data record. 
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  recordID: the ID of record to be read
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return     EE_OK
+ *             EE_ERROR_BUSY -- EE is writing or erasing
+ *             EE_ERROR_DATA_NOT_FOUND
+ *             EE_ERROR_REC_ID_OUT_OF_RANGE
+ *             EE_ERROR_FLASH_ERR
+ *
+ ******************************************************************************/   
+uint32_t EE_DeleteRecord(EE_Config_t* eeConfig, uint16_t recordID, 
+                         void (*CallBack) (void));
+
+#endif
\ No newline at end of file
diff --git a/Aging_app/eeprom/Src/ee_lowlevel.c b/Aging_app/eeprom/Src/ee_lowlevel.c
new file mode 100644 (file)
index 0000000..b509661
--- /dev/null
@@ -0,0 +1,630 @@
+/***************************************************************************//**
+ * @file     ee_lowlevel.c
+ * @brief    This file provides low level functions used by eeprom emulation .
+ * @version  V1.3
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.
+ *
+ ******************************************************************************/
+#include "eep_emulation.h"
+
+
+
+
+#if (defined(DEV_Z20K148M)) || (defined(DEV_Z20K146M))||(defined(DEV_Z20K144M))
+#include "flash_drv.h"
+#include "scm_drv.h"
+#define SCM        ((scm_reg_t *) SCM_BASE_ADDR)        /*!< scm Register */   
+#else
+#include "Z20K11xM_flash.h"
+#endif
+
+
+
+#include "string.h"
+#include "ee_lowlevel.h"
+
+#define FLASHC      ((flash_reg_t *) FLASHC_BASE_ADDR)     /*!< flash controller Register */
+
+
+/***************************************************************************!
+ * @brief: This API is used to read data from flash. 
+ *
+ * @param[in] addr: the start address of data to be read out. 
+ * @param[in] buffer: points to a buffer where the read-out data to be stored
+ * @param[in] size: the size to be read out in byte.
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return EE_OK
+ *         EE_ERROR_ECC: ECC error when read flash
+ *
+ ****************************************************************************/
+uint32_t EE_FlashRead(uint32_t addr, uint8_t * buffer, uint32_t size,
+                      void (*CallBack)(void))
+{
+    uint32_t returnCode = EE_OK;
+    uint32_t counter;
+    uint32_t alignedCnt, unalignedCnt;
+    uint32_t buf32 = (uint32_t)buffer;
+    uint32_t offset = 0U;
+    
+    /* Clear the double-bit ECC flag before reading */
+    FLASH_IntClear(FLASH_INT_DFDIF);
+
+    if((addr % 4U == 0U) && ((uint32_t)buffer % 4U == 0U))
+    {
+        alignedCnt = size >> 2U;
+        unalignedCnt = size % 4U;
+        
+        for(counter = 0U; counter < alignedCnt; counter++)
+        {
+            /* Callback service */
+            if ((counter % EE_CALLBACK_READ_BYTES_NUM) == 0U)
+            {
+                if (CallBack != NULL)
+                {
+                    CallBack();
+                }
+            }
+            
+            *(uint32_t *)(buf32 + offset) = *(uint32_t *)(addr + offset);
+            offset += 4U;
+        }
+        
+        for(counter = 0U; counter < unalignedCnt; counter++)
+        {
+            buffer[offset] = *(uint8_t *)(addr + offset);
+            offset++;
+        }
+        
+        if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+        {
+            FLASH_IntClear(FLASH_INT_DFDIF);
+            returnCode = EE_ERROR_ECC;
+        }        
+    }
+    else
+    {
+        /* Read data */
+        for (counter = 0U; counter < size; counter++)
+        {
+            /* Callback service */
+            if ((counter % EE_CALLBACK_READ_BYTES_NUM) == 0U)
+            {
+                if (CallBack != NULL)
+                {
+                    CallBack();
+                }
+            }
+        
+            buffer[counter] = *(uint8_t *)(addr + counter);
+        }
+    
+        if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+        {
+            FLASH_IntClear(FLASH_INT_DFDIF);
+            returnCode = EE_ERROR_ECC;
+        }
+    }
+
+    return returnCode;
+}
+
+/***************************************************************************!
+ * @brief: This API is used to read data from flash by word. 
+ *
+ * @param[in] addr: the start address of data to be read out. It must aligned 
+ *                  to 32 bit.
+ * @param[in] buffer: points to a buffer where the read-out data to be stored
+ * @param[in] size: the size to be read out in word.
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return EE_OK
+ *         EE_ERROR_ECC: ECC error when read flash
+ *
+ ****************************************************************************/
+uint32_t EE_FlashReadByWord(uint32_t addr, uint32_t *buffer, uint32_t size,
+                            void (*CallBack)(void))
+{
+    uint32_t returnCode = EE_OK;
+    uint32_t counter;
+    
+    /* Clear the double-bit ECC flag before reading */
+    FLASH_IntClear(FLASH_INT_DFDIF);
+
+    /* Read data */
+    for (counter = 0U; counter < size; counter++)
+    {
+        /* Callback service */
+        if ((counter % EE_CALLBACK_READ_BYTES_NUM) == 0U)
+        {
+            if (CallBack != NULL)
+            {
+                CallBack();
+            }
+        }
+        
+        buffer[counter] = *(uint32_t *)(addr + counter*4U);
+    }
+    
+    if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+    {
+        FLASH_IntClear(FLASH_INT_DFDIF);
+        returnCode = EE_ERROR_ECC;
+    }
+
+    return returnCode;
+}
+
+/***************************************************************************!
+ * @brief: This API is used to read a word from flash.. 
+ *
+ * @param[in] addr: the address of phrase to be read out. It is required to be
+ *                  phase aligned.
+ * @param[in] word: points to a buffer where the read-out data to be stored
+
+ *
+ * @return EE_OK
+ *         EE_ERROR_ECC: ECC error when read flash
+ *
+ ****************************************************************************/
+uint32_t EE_FlashReadWord(uint32_t addr, uint32_t * word)
+{
+    uint32_t returnCode = EE_OK;
+    uint32_t tmp;
+    
+    *word = 0U;
+    
+    /* Clear the double-bit ECC flag before reading */
+    FLASH_IntClear(FLASH_INT_DFDIF);
+    
+    if(addr % 4U == 0U)
+    {
+        *word = *(uint32_t *)addr;
+    }
+    else
+    {
+        tmp = *(uint8_t *)(addr + 3U);
+        *word |= (tmp << 24U);
+        tmp = *(uint8_t *)(addr + 2U);
+        *word |= (tmp << 16U);
+        tmp = *(uint8_t *)(addr + 1U);
+        *word |= (tmp << 8U);
+        *word |= *(uint8_t *)(addr);
+    }
+ 
+    if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+    {
+        FLASH_IntClear(FLASH_INT_DFDIF);
+        returnCode = EE_ERROR_ECC;
+    }
+
+    return returnCode;
+}
+
+/***************************************************************************!
+ * @brief: This API is used to read a halfword from flash. 
+ *
+ * @param[in] addr: the address of phrase to be read out. It is required to be
+ *                  phase aligned.
+ * @param[in] halfWord: points to a buffer where the read-out data to be stored
+
+ *
+ * @return EE_OK
+ *         EE_ERROR_ECC: ECC error when read flash
+ *
+ ****************************************************************************/
+uint32_t EE_FlashReadHalfWord(uint32_t addr, uint16_t * halfWord)
+{
+    uint32_t returnCode = EE_OK;
+    uint16_t tmp;
+    
+    *halfWord = 0U;
+    
+    /* Clear the double-bit ECC flag before reading */
+    FLASH_IntClear(FLASH_INT_DFDIF);
+    
+    if(addr % 2U == 0U)
+    {
+        *halfWord = *(uint16_t *)addr;
+    }
+    else
+    {
+        tmp = *(uint8_t *)(addr + 1U);
+        *halfWord |= (tmp << 8U);
+        *halfWord |= *(uint8_t *)(addr);
+    }
+ 
+    if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+    {
+        FLASH_IntClear(FLASH_INT_DFDIF);
+        returnCode = EE_ERROR_ECC;
+    }
+
+    return returnCode;
+}
+
+/***************************************************************************!
+ * @brief: This API is used to check if flash data is 0xFF. 
+ *
+ * @param[in] addr: the address of phrase to be read out. It is required to be
+ *                  phase aligned.
+ * @param[in] size: the size to be checked in byte.
+ * @param[out] blankFlag: the check result is stored in blankFlag by this function.
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return EE_OK
+ *         EE_ERROR_ECC: ECC error when read flash
+ *
+ ****************************************************************************/
+uint32_t EE_FlashBlankCheck(uint32_t addr, uint32_t size, 
+                            FlagStatus_t *blankFlag, void (*CallBack)(void))
+{
+    uint32_t returnCode = EE_OK;
+    uint32_t counter;
+    uint32_t unalignedLen;
+    uint32_t lenToCheck, restLen, addrToCheck;
+    ResultStatus_t tmp;
+    FLASH_CmdConfig_t cmdConfig = {
+                                   FLASH_CMD_ACT_WAIT,
+                                   CallBack
+                                  };
+    
+    *blankFlag = SET;
+    
+    unalignedLen = (EE_FL_PHRASE_SIZE - addr%EE_FL_PHRASE_SIZE)%EE_FL_PHRASE_SIZE;
+    
+    /* Clear the double-bit ECC flag before reading */
+    FLASH_IntClear(FLASH_INT_DFDIF);
+
+    lenToCheck = (unalignedLen > size)? size:unalignedLen;
+    for (counter = 0U; counter < lenToCheck; counter++)
+    {
+        /* Callback service */
+        if ((counter % EE_CALLBACK_READ_BYTES_NUM) == 0U)
+        {
+            if (CallBack != NULL)
+            {
+                CallBack();
+            }
+        }
+        
+        if(0xFFU != *(uint8_t *)(addr + counter))
+        {
+            *blankFlag = RESET;
+        }
+    
+        if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+        {
+            FLASH_IntClear(FLASH_INT_DFDIF);
+            returnCode = EE_ERROR_ECC;
+            return returnCode;
+        }
+    }
+    
+    if(CallBack != NULL)
+    {
+        CallBack();
+    }
+    
+    restLen = size - lenToCheck;
+    addrToCheck = addr + lenToCheck;
+    if(restLen != 0U)
+    {
+        for(counter = 0U; counter < restLen/EE_FL_PHRASE_SIZE; counter++)
+        {
+            tmp = FLASH_VerifyPhrase(addrToCheck + counter*EE_FL_PHRASE_SIZE,
+                                     &cmdConfig);
+            
+            if(tmp != SUCC)
+            {
+                *blankFlag = RESET;
+            }
+        }
+        
+        if(restLen%EE_FL_PHRASE_SIZE != 0U)
+        {
+            restLen -= counter*EE_FL_PHRASE_SIZE;
+            addrToCheck += counter*EE_FL_PHRASE_SIZE;
+            
+            for (counter = 0U; counter < restLen; counter++)
+            {
+                 /* Callback service */
+                if ((counter % EE_CALLBACK_READ_BYTES_NUM) == 0U)
+                {
+                    if (CallBack != NULL)
+                    {
+                        CallBack();
+                    }
+                }
+        
+                if(0xFFU != *(uint8_t *)(addrToCheck + counter))
+                {
+                    *blankFlag = RESET;
+                }
+    
+                if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+                {
+                    FLASH_IntClear(FLASH_INT_DFDIF);
+                    returnCode = EE_ERROR_ECC;
+                }
+            }
+        }        
+    }   
+
+    return returnCode;
+}
+
+/***************************************************************************!
+ * @brief: This API is used to check if flash data is equal to expected value 
+ *
+ * @param[in] addr: the address of data to be check
+ * @param[in] src: the buffer which storeds the expected value.
+ * @param[in] len: length of data to be checked.
+ * @param[in] flag: if data are equal to expected value, flag is SET, otherwise
+ *                  flash is RESET.
+ * @param[in] CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return EE_OK
+ *         EE_ERROR_ECC: ECC error when read flash
+ *
+ ****************************************************************************/
+uint32_t EE_DataCheck(uint32_t addr, const uint8_t *buf, uint32_t len,
+                      FlagStatus_t *flag, void (*CallBack)(void))
+{
+    uint32_t returnCode = EE_OK;
+    uint32_t counter;
+    
+    *flag = SET;
+    
+    /* Clear the double-bit ECC flag before reading */
+    FLASH_IntClear(FLASH_INT_DFDIF);
+
+    for (counter = 0U; counter < len; counter++)
+    {
+        /* Callback service */
+        if ((counter % EE_CALLBACK_READ_BYTES_NUM) == 0U)
+        {
+            if (CallBack != NULL)
+            {
+                CallBack();
+            }
+        }
+        
+        if(buf[counter] != *(uint8_t *)(addr + counter))
+        {
+            *flag = RESET;
+        }
+    
+        if(FLASHC->FLASH_FSTAT.DFDIF  == 1U)
+        {
+            FLASH_IntClear(FLASH_INT_DFDIF);
+            returnCode = EE_ERROR_ECC;
+            break;
+        }
+    }
+
+    return returnCode;
+}
+        
+/***************************************************************************!
+ * @brief: This API erases an EE sector 
+ *
+ * @param[in] addr: the address EE sector to be verified.
+ * @param[in] size: the size of EE sector
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return EE_OK
+ *         EE_ERROR_INPUT_PARA: input parameter are invalid
+ *         EE_ERROR_FLASH_ERR
+ *
+ ****************************************************************************/
+uint32_t EE_SectorErase(uint32_t addr, uint32_t size, void (*CallBack)(void))
+{
+    uint32_t returnCode = EE_OK;
+    uint32_t counter;
+    ResultStatus_t tmp;
+    FLASH_CmdConfig_t cmdConfig =
+    {
+        FLASH_CMD_ACT_WAIT,
+        CallBack
+    };
+    
+    if((addr % EE_FL_SECTOR_SIZE != 0U) || (size % EE_FL_SECTOR_SIZE != 0U))
+    {
+        returnCode = EE_ERROR_INPUT_PARA;
+        return returnCode;
+    }
+
+    for(counter = 0U; counter < size/EE_FL_SECTOR_SIZE; counter++)
+    {           
+        tmp = FLASH_EraseSector(addr + counter*EE_FL_SECTOR_SIZE, &cmdConfig);
+        
+#if (defined(DEV_Z20K148M)) || (defined(DEV_Z20K146M))||(defined(DEV_Z20K144M))
+        /* if cache is enable */
+        if(0U == SCM->SCM_MISCCTL1.CACHE_DIS)
+        {
+            SCM_CacheClr();
+        }
+#endif
+        
+        if(tmp != SUCC)
+        {
+            returnCode = EE_ERROR_FLASH_ERR;
+        }
+    }        
+       
+    return returnCode;
+}
+
+/***************************************************************************!
+ * @brief: This API is used to write data into flash. If the size of data is not
+ *         multiple of phrase size. It will be padded with 0xFF.
+ *         
+ *
+ * @param[in] srcAddr: the address of source data to be written into flash
+ * @param[in] len: the size of data to be written.
+ * @param[in] srcInFlash: it indicates if the location of data to be written
+ *                         is in FLASH.
+ *                         - 1: in flash
+ *                         - 0: not in flash
+ * @param[in] addr: the start address to be written to. It is required to be 
+ *                  phrase-aligned.
+ * @param[in] CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return SUCC
+ *         ERR
+ *
+ ****************************************************************************/
+uint32_t EE_FlashWriteWithPadding(uint32_t srcAddr, uint32_t len, 
+                                  uint32_t srcInFlash, uint32_t addr, 
+                                  void (*CallBack)(void))
+{
+    uint32_t returnCode = EE_OK;
+    uint32_t alignedLen;
+    uint32_t remaind;
+    uint32_t writeAddr = addr;
+    uint32_t readAddr = srcAddr;
+    uint8_t buf[EE_FL_PHRASE_SIZE];
+    FLASH_CmdConfig_t config = 
+    {
+        FLASH_CMD_ACT_WAIT,
+        CallBack
+    };
+    
+    if(addr%EE_FL_PHRASE_SIZE != 0U)
+    {
+        returnCode = EE_ERROR_INPUT_PARA;
+    }
+    else
+    {        
+        alignedLen = (len / EE_FL_PHRASE_SIZE) * EE_FL_PHRASE_SIZE;
+        remaind = len % EE_FL_PHRASE_SIZE;
+    
+        while(alignedLen != 0U)
+        {
+            if(srcInFlash == 1U)
+            {
+                returnCode = EE_FlashRead(readAddr, buf, EE_FL_PHRASE_SIZE,
+                                          CallBack);
+                if(returnCode != EE_OK)
+                {
+                    return EE_ERROR_READ_ECC;
+                }
+                
+                if(ERR == FLASH_ProgramPhrase(writeAddr, buf, &config))
+                {
+                    returnCode = EE_ERROR_FLASH_ERR;
+                }
+            }
+            else
+            {
+                if(ERR == FLASH_ProgramPhrase(writeAddr, (uint8_t *)readAddr, &config))
+                {
+                    returnCode = EE_ERROR_FLASH_ERR;
+                }
+            }            
+            
+            alignedLen -= EE_FL_PHRASE_SIZE;
+            readAddr += EE_FL_PHRASE_SIZE;
+            writeAddr += EE_FL_PHRASE_SIZE;
+        }
+        
+#if (defined(DEV_Z20K148M)) || (defined(DEV_Z20K146M))||(defined(DEV_Z20K144M))
+        /* if cache is enable */
+        if(0U == SCM->SCM_MISCCTL1.CACHE_DIS)
+        {
+            SCM_CacheClr();
+        }
+#endif
+        if(returnCode == EE_OK)
+        {
+            if(remaind != 0U)
+            {
+                if(srcInFlash == 1U)
+                {
+                    returnCode = EE_FlashRead(readAddr, buf, remaind, CallBack);
+                    if(returnCode != EE_OK)
+                    {
+                        return EE_ERROR_READ_ECC;
+                    }
+                }
+                else
+                {
+                    for(uint32_t i = 0U; i < remaind; i++)
+                    {
+                        buf[i] = *(uint8_t*)(readAddr + i);
+                    }
+                }
+            
+                for(uint32_t i = remaind; i < EE_FL_PHRASE_SIZE; i++)
+                {
+                    buf[i] = 0xFFU;
+                }
+        
+                if(ERR == FLASH_ProgramPhrase(writeAddr, (const uint8_t *)buf, &config))
+                {
+                    returnCode = EE_ERROR_FLASH_ERR;
+                }
+            }
+        }
+    }
+    
+#if (defined(DEV_Z20K148M)) || (defined(DEV_Z20K146M))||(defined(DEV_Z20K144M))
+        /* if cache is enable */
+        if(0U == SCM->SCM_MISCCTL1.CACHE_DIS)
+        {
+            SCM_CacheClr();
+        }
+#endif
+    
+    return returnCode;
+}
+
+/***************************************************************************!
+ * @brief: This API is used to check if sram data is equal to a value. 
+ *
+ * @param[in] buf: points to the data to be checked
+ * @param[in] size: the size to be checked in word.
+ * @param[in] value: value to compare with
+ *
+ * @return RESET - not equal
+ *         SET - equal
+ *
+ ****************************************************************************/
+FlagStatus_t EE_SramCheckByWord(uint32_t *buf, uint32_t size, uint32_t value)
+{
+    uint32_t i;
+    
+    for(i = 0U; i< size; i++)
+    {
+        if(buf[i] != value)
+        {
+            return RESET;
+        }
+    }
+    
+    return SET;
+}
+
diff --git a/Aging_app/eeprom/Src/ee_middlelevel.c b/Aging_app/eeprom/Src/ee_middlelevel.c
new file mode 100644 (file)
index 0000000..ecae61d
--- /dev/null
@@ -0,0 +1,1182 @@
+/***************************************************************************//**
+ * @file     ee_middlelevel.c
+ * @brief    This file provides middle level functions used by eeprom emulation.
+ * @version  V1.3
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.
+ *
+ ******************************************************************************/
+#include <stdint.h>
+#include <string.h>
+#include "eep_emulation.h"
+#include "ee_middlelevel.h"
+#include "ee_lowlevel.h"
+
+const uint32_t validPattern[EE_VALID_PATTERN_SIZE_IN_WORD] = 
+{
+    0x7869de01U,
+    0xcdf0ab23U,
+    0xFFFFFFFFU,
+    0xFFFFFFFFU,
+};
+
+const uint32_t invalidPattern[EE_INVALID_PATTERN_SIZE_IN_WORD] = 
+{
+    0xcdf0ab23U,
+    0x7869de01U,
+    0xFFFFFFFFU,
+    0xFFFFFFFFU,
+};
+
+void HalfwordToBytes(uint16_t src, uint8_t *dest)
+{
+    dest[0] = (uint8_t)(src & 0xFFU);
+    dest[1] = (uint8_t)(src >> 8U);
+}
+
+void WordToBytes(uint32_t src, uint8_t *dest)
+{
+    dest[0] = (uint8_t)(src & 0xFFU);
+    dest[1] = (uint8_t)((src >> 8U) & 0xFFU);
+    dest[2] = (uint8_t)((src >> 16U) & 0xFFU);
+    dest[3] = (uint8_t)((src >> 24U) & 0xFFU);
+}
+
+/***************************************************************************!
+ * @brief: This API is to update the contents of the cache table while the
+ *          data address is changed by writing a new value or sector swapping.
+ *
+ * @param[in]  cTable: a pointer that points to the cache table struct
+ * @param[in]  recordID: record ID that to be updated
+ * @param[in]  newValue: the value to be written to cache
+ *
+ * @return none
+ *
+ ****************************************************************************/
+void EE_UpdateCacheTable(EE_cache_t* cTable, uint16_t recordID, 
+                                   uint32_t newValue)
+{
+    /* Check if it is in the cache table */
+    if (recordID < (cTable->num))
+    {
+        /* Update the cache table item with new value */
+        cTable->cachePtr[recordID] = newValue;
+    }
+}
+
+/***************************************************************************!
+ *
+ * @brief: This API is to search the required data record ID in the cache table.
+ *
+ * @param[in]  cTable: a pointer that points to the cache table struct
+ * @param[in]  recordID: record ID that to be searched
+ * @param[out] recAddr: record address
+ *
+ * @return EE_OK
+ *         EE_ERROR_NOT_IN_CACHE
+ *         EE_ERROR_DATA_NOT_FOUND
+ *
+ ****************************************************************************/
+uint32_t EE_SearchInCache(EE_cache_t* cTable, uint16_t recordID, uint32_t* recAddr)
+{
+    uint32_t returnCode = EE_OK;
+
+    /* check if the data ID is in the cache table ID range */
+    if (recordID >= (cTable->num))
+    {
+        /* record ID is out of cache range */
+        returnCode = EE_ERROR_NOT_IN_CACHE;
+    }
+    else
+    {
+        /* Fetch the required data record address */
+        *recAddr = cTable->cachePtr[recordID];
+        if (*recAddr >= EE_RECORD_ADDR_DELETED)   
+        {
+            /* The record was deleted or not found */
+            returnCode = EE_ERROR_DATA_NOT_FOUND;
+        }
+    }
+
+    return returnCode;
+}
+
+/***************************************************************************!
+ *
+ * @brief This function will read a valid/invalid flag.
+ *
+ * @param[in] addr: address of the flag
+ * @param[in] flagPattern: the flag will be compared with flagPattern. If they
+ *                         are equal, flag is SET.
+ * @param[out] flag: the flag value will be stored in it by this function.
+ * @param[in] CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return
+ *
+ ****************************************************************************/
+static uint32_t EE_ReadFlag(uint32_t addr, const uint32_t * flagPattern, 
+                           FlagStatus_t *flag, void (*CallBack)(void))
+{
+    uint32_t returnCode;
+    uint32_t buf[EE_VALID_PATTERN_SIZE_IN_WORD];
+    uint8_t i;
+    
+    *flag =  RESET;
+ 
+    returnCode = EE_FlashReadByWord(addr, buf,EE_VALID_PATTERN_SIZE_IN_WORD,CallBack);
+    if (EE_OK != returnCode)
+    {
+        return returnCode;
+    }
+
+    *flag =  SET;
+    for(i = 0U; i<EE_VALID_PATTERN_SIZE_IN_WORD; i++)
+    {
+        if(buf[i] != flagPattern[i])
+        {
+            *flag = RESET;
+            break;
+        }
+    } 
+    
+    if(*flag == RESET)
+    {
+        for(i = 0U; i<EE_VALID_PATTERN_SIZE_IN_WORD; i++)
+        {
+            if(buf[i] != 0xFFFFFFFFU)
+            {
+                returnCode = EE_ERROR_HEADER_INVALID;
+                break;
+            }
+        }        
+    }
+   
+    return returnCode;
+}
+
+/***************************************************************************!
+ *
+ * @brief This function will read Sector header from specified address.
+ *        It stores data into the Sector header structure.
+ *
+ * @param[in] addr: address of the Sector header
+ * @param[out] head: points to a Sector header structure. The Sector header 
+ *                   infomation will be stored into it by this API.
+ * @param[in] CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return status
+ *
+ ****************************************************************************/
+uint32_t EE_ReadSectorHeader(uint32_t addr, EE_SectorHeader_t * head, 
+                             void (*CallBack)(void))
+{
+    uint32_t returnCode;
+    uint32_t checkSum, wordSize;
+    FlagStatus_t validFlag, invalidFlag, infoBlankFlag;
+    uint32_t buf[EE_RECORD_INFO_LEN>>2U];
+    
+    wordSize = EE_RECORD_INFO_LEN>>2U;    
+    head->status = EE_SEC_STAT_HEADER_INVALID;
+    
+    /* read header info */
+    returnCode = EE_FlashReadByWord(addr, buf, wordSize,CallBack);
+    if(returnCode != EE_OK)
+    {
+        return returnCode;
+    }
+    
+    infoBlankFlag = EE_SramCheckByWord(buf, wordSize, 0xFFFFFFFFU);
+  
+    if(infoBlankFlag == RESET)
+    {
+        head->id = buf[0U];
+        head->startAddr = buf[1U];
+        head->size = buf[2U];
+        checkSum = buf[3U];
+    
+        if(head->id + head->startAddr + head->size != checkSum)
+        {
+            return EE_ERROR_HEADER_INVALID;
+        }
+    }
+
+    returnCode = EE_ReadFlag(addr + EE_SECTOR_VALID_OFFSET, validPattern, 
+                            &validFlag, CallBack );
+    if(returnCode != EE_OK)
+    {
+        return returnCode;
+    }
+    returnCode = EE_ReadFlag(addr + EE_SECTOR_INVALID_OFFSET, invalidPattern, 
+                            &invalidFlag, CallBack );
+    if(returnCode != EE_OK)
+    {
+        return returnCode;
+    }
+
+    if((validFlag == RESET) && ( invalidFlag == RESET))
+    {
+        if(infoBlankFlag == RESET)
+        {
+            head->status = EE_SEC_STAT_INCONSISTENT;
+        }
+        else
+        {
+            head->status = EE_SEC_STAT_HEADER_BLANK;
+        }
+    }
+    else if((validFlag == SET) && (invalidFlag == RESET))
+    {
+        head->status = EE_SEC_STAT_VALID;
+    }
+    else if((validFlag == SET) && ( invalidFlag == SET))
+    {
+        head->status = EE_SEC_STAT_INVALIDATED;
+    }
+    else
+    {
+        head->status = EE_SEC_STAT_HEADER_INVALID;
+    }
+    
+    return returnCode;
+}
+
+/***************************************************************************!
+ *
+ * @brief This function will read record header from specified address.
+ *        It stores data into the record header structure.
+ *
+ * @param[in] addr: address of the record header
+ * @param[out] head: points to a record header structure. The record header 
+ *                   infomation will be stored into it by this API.
+ * @param[in] CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return  - EE_OK
+ *          - EE_ERROR_ECC
+ *          - EE_ERROR_HEADER_INVALID
+ *
+ ****************************************************************************/
+uint32_t EE_ReadRecordHeader(uint32_t addr, EE_RecordHeader_t * head,
+                             void (*CallBack)(void))
+{
+    uint32_t returnCode;
+    uint32_t checkSum, wordSize;
+    FlagStatus_t validFlag, invalidFlag, infoBlankFlag;
+    uint32_t buf[EE_RECORD_INFO_LEN>>2U];
+    uint32_t bufAddr = (uint32_t)buf;
+    
+    wordSize = EE_RECORD_INFO_LEN>>2U;
+    head->recordStatus = EE_REC_STAT_HEADER_INVALID;
+ 
+    /* read header info */
+    returnCode = EE_FlashReadByWord(addr, buf, wordSize,CallBack);
+    if(returnCode != EE_OK)
+    {
+        return returnCode;
+    }
+    
+    infoBlankFlag = EE_SramCheckByWord(buf, wordSize, 0xFFFFFFFFU);
+  
+    if(infoBlankFlag == RESET)
+    {
+        head->recordID = *(uint16_t *)(bufAddr + EE_RECORD_ID_OFFSET);
+        head->dataSize = *(uint16_t *)(bufAddr + EE_RECORD_SIZE_OFFSET);
+        head->dataStartAddr = *(uint32_t *)(bufAddr + EE_RECORD_ADDR_OFFSET);
+        checkSum = *(uint32_t *)(bufAddr + EE_RECORD_CHECKSUM_OFFSET);
+    
+        if((uint32_t)(head->recordID) + (uint32_t)(head->dataSize)
+           + head->dataStartAddr != checkSum)
+        {
+            return EE_ERROR_HEADER_INVALID;
+        }
+    }
+    
+    if(EE_OK != EE_ReadFlag(addr + EE_RECORD_VALID_OFFSET, validPattern, 
+                           &validFlag, CallBack ))
+    {
+        return EE_ERROR_HEADER_INVALID;
+    }
+    
+    if(EE_OK != EE_ReadFlag(addr + EE_RECORD_INVALID_OFFSET, invalidPattern, 
+                           &invalidFlag, CallBack ))
+    {
+        return EE_ERROR_HEADER_INVALID;
+    }
+
+    if((validFlag == RESET) && ( invalidFlag == RESET))
+    {
+        if(infoBlankFlag == RESET)
+        {
+            head->recordStatus = EE_REC_STAT_INCONSISTENT;
+        }
+        else
+        {
+            head->recordStatus = EE_REC_STAT_HEADER_BLANK;
+        }
+    }
+    else if((validFlag == SET) && (invalidFlag == RESET))
+    {
+        head->recordStatus = EE_REC_STAT_VALID;
+    }
+    else if((validFlag == SET) && ( invalidFlag == SET))
+    {
+        head->recordStatus = EE_REC_STAT_DELETED;
+    }
+    else
+    {
+        head->recordStatus = EE_REC_STAT_HEADER_INVALID;
+    }
+    
+    return returnCode;
+}
+
+/***************************************************************************!
+ *
+ * @brief This API reads a record from the given record header address
+ *
+ * @param[in] recordAddr: the record header address
+ * @param[in] dataSize: the size of data to be read out
+ * @param[in] buf: points to a buffer where data that are read out are stored
+ * @param[out] readSize: the size of data that are actually read out 
+ *                      will be stored to the memory pointed by
+ *                      readSize after calling this function
+ * @param[in] CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return  - EE_OK
+ *          - EE_ERROR_ECC
+ *          - EE_ERROR_REC_HEADER_INVALID
+ *         
+ *
+ ****************************************************************************/
+uint32_t EE_ReadRecordAtAddr(uint32_t recordAddr, uint16_t dataSize, 
+                             uint8_t *buf, uint16_t *readSize, 
+                             void (*CallBack)(void))
+{
+    EE_RecordHeader_t recHeader;     /* Local data record head structure */
+    uint32_t returnCode;
+
+    /* Read record head */
+    returnCode = EE_ReadRecordHeader(recordAddr, &recHeader, CallBack);
+    if(returnCode != EE_OK)
+    {
+        *readSize = 0;
+    }
+    else
+    {
+        *readSize = recHeader.dataSize;
+
+        if (*readSize > dataSize)
+        {
+            *readSize = dataSize;
+        }
+
+        /* Read the data */
+        if (EE_ERROR_ECC == EE_FlashRead(recHeader.dataStartAddr, buf, 
+                                         *readSize, CallBack))
+        {
+            *readSize = 0;
+            returnCode = EE_ERROR_ECC;
+        }
+    }
+    
+    return returnCode;
+}
+/***************************************************************************!
+ *
+ * @brief: This API will search the latest record ID in EEPROM flash
+ *         with status that is valid or deleted.It will go through the entire 
+ *         active sector for the latest data record.
+ *
+ * @param[in]  pSector: points to sector config struct
+ * @param[in]  recordID: the ID of record to be read
+ * @param[out] recAddr: record address
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return  void
+ *
+ ****************************************************************************/
+static void EE_SearchRecord(EE_Config_t* eeConfig, uint16_t recordID, 
+                     uint32_t* recordAddr, void (*CallBack)(void))
+{
+    const EE_SectorConfig_t* pSectorCfg;
+    EE_RecordHeader_t recHeader;    /* Local data record head structure */
+    uint32_t searchAddr;
+    uint32_t count = 0U;
+
+    pSectorCfg = eeConfig->sectors[eeConfig->actSecInfo.index];    
+
+    /* set to invalid address */
+    *recordAddr = EE_RECORD_ADDR_EMPTY;
+    
+    searchAddr = eeConfig->actSecInfo.recordAddr;
+
+    while (searchAddr > (pSectorCfg->startAddr + EE_SECTOR_RECORD_OFFSET))
+    {
+        /* Point to header address of previous record */
+        searchAddr -= EE_RECORD_HEADER_LEN;
+
+        /* CallBack service routine */
+        count++;
+        if ((count % EE_CALLBACK_SEARCH_BYTES_NUM) == 0U)
+        {
+            if (CallBack != NULL)
+            {
+               CallBack();
+            }
+        }
+
+        /* Read record head */
+        if(EE_OK != EE_ReadRecordHeader(searchAddr, &recHeader, CallBack))
+        {
+            /* do nothing */
+        }
+        else
+        {
+            if (recordID == recHeader.recordID)
+            {
+                if (recHeader.recordStatus == EE_REC_STAT_DELETED)
+                {
+                    *recordAddr = EE_RECORD_ADDR_DELETED;    /* Mark as deleted */
+                    break;  /* Stop searching */
+                }
+                else if(recHeader.recordStatus == EE_REC_STAT_VALID)
+                {
+                    /* Found the record */
+                    *recordAddr = searchAddr;       
+                    break;  /* Stop searching */
+                }
+                else
+                {
+                    /* EE_REC_STAT_HEADER_INVALID, EE_REC_STAT_INCONSISTENT,
+                       EE_REC_STAT_HEADER_BLANK */
+                }
+            }
+        }
+    }
+}
+
+/***************************************************************************
+ * @brief: This API is to search the required record ID in the active sector.
+ *         It returns the data if the record is found and valid.
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  recordID: the ID of record to be read
+ * @param[out] recAddr: record address
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return  EE_OK
+ *          EE_ERROR_DATA_NOT_FOUND
+ *
+ ****************************************************************************/
+uint32_t EE_SearchInSectors(EE_Config_t* eeConfig, uint16_t recordID, 
+                            uint32_t* recordAddr, void (*CallBack) (void))
+{
+    uint32_t searchResult = EE_ERROR_DATA_NOT_FOUND;
+
+    EE_SearchRecord(eeConfig, recordID, recordAddr, CallBack);
+
+    if(*recordAddr != EE_RECORD_ADDR_EMPTY)         /* Found the record */
+    {
+        if(*recordAddr != EE_RECORD_ADDR_DELETED)   /* The record is valid */
+        {
+            searchResult = EE_OK;
+        }
+    }
+
+    return searchResult;
+}
+
+/***************************************************************************
+* @brief        Adjusts size so it's integer multiple of EE_FL_PHRASE_SIZE
+* 
+* @param[in]    size: actual size
+* 
+* @return       uint16_t
+*               the size is integer multiple of EE_FL_PHRASE_SIZE
+* 
+****************************************************************************/
+uint16_t EE_AlignToPhraseSize(uint16_t size)
+{
+    /* Check whether the size is integer multiple */
+    if(0U == (size % EE_FL_PHRASE_SIZE))
+    {
+        /* size is integer multiple of EE_FL_PHRASE_SIZE */
+    }
+    else
+    {
+        size = ((size / EE_FL_PHRASE_SIZE) + 1U )* EE_FL_PHRASE_SIZE;
+    }
+    
+    return size;
+}
+
+/***************************************************************************
+ * @brief      Validate a sector
+ * 
+ * 
+ * @param[in]  pSector: points to sector config struct
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ * @return    EE_OK
+ *            EE_ERROR_FLASH_ERR
+ *
+ ****************************************************************************/
+uint32_t EE_ValidateSector(const EE_SectorConfig_t *pSector, 
+                           void (*CallBack)(void))
+{
+    uint32_t returnCode;
+    uint32_t patternInFlash;
+    
+#ifdef EE_PATTERN_IN_SRAM
+    patternInFlash = 0U;
+#else
+    patternInFlash = 1U;
+#endif
+    
+    returnCode = EE_FlashWriteWithPadding((uint32_t)validPattern, EE_VALID_PATTERN_SIZE_IN_BYTE,
+                                          patternInFlash,
+                                          pSector->startAddr + EE_SECTOR_VALID_OFFSET,
+                                           CallBack);
+
+    return returnCode;
+}
+
+/***************************************************************************
+ * @brief      Invalidate a sector
+ * 
+ * 
+ * @param[in]  pSector: points to sector config struct
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ * @return    EE_OK
+ *            EE_ERROR_FLASH_ERR
+ *
+ ****************************************************************************/
+uint32_t EE_InvalidateSector(const EE_SectorConfig_t *pSector, 
+                             void (*CallBack)(void))
+{
+    uint32_t returnCode;
+    uint32_t patternInFlash;
+    
+#ifdef EE_PATTERN_IN_SRAM
+    patternInFlash = 0U;
+#else
+    patternInFlash = 1U;
+#endif
+    
+    returnCode =  EE_FlashWriteWithPadding((uint32_t)invalidPattern, EE_INVALID_PATTERN_SIZE_IN_BYTE,
+                                           patternInFlash,
+                                           pSector->startAddr + EE_SECTOR_INVALID_OFFSET,
+                                           CallBack);
+
+    return returnCode;
+}
+
+/***************************************************************************
+ * @brief      Validate a record
+ * 
+ * 
+ * @param[in]  recordAddr: the header address of record to be invalidated
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ * @return    EE_OK
+ *            EE_ERROR_FLASH_ERR
+ *
+ ****************************************************************************/
+uint32_t EE_ValidateRecord(uint32_t recordAddr, void (*CallBack)(void))
+{
+    uint32_t returnCode;
+    uint32_t patternInFlash;
+    
+#ifdef EE_PATTERN_IN_SRAM
+    patternInFlash = 0U;
+#else
+    patternInFlash = 1U;
+#endif
+    
+    returnCode = EE_FlashWriteWithPadding((uint32_t)validPattern, 
+                                          EE_VALID_PATTERN_SIZE_IN_BYTE,
+                                          patternInFlash,
+                                          recordAddr + EE_RECORD_VALID_OFFSET,
+                                          CallBack);
+    
+    return returnCode;
+}
+
+/***************************************************************************
+ * @brief      Invalidate a record
+ * 
+ * 
+ * @param[in]  recordAddr: the header address of record to be invalidated
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ * @return    EE_OK
+ *            EE_ERROR_FLASH_ERR
+ *
+ ****************************************************************************/
+uint32_t EE_InvalidateRecord(uint32_t recordAddr, void (*CallBack)(void))
+{
+    uint32_t returnCode;
+    uint32_t patternInFlash;
+    
+#ifdef EE_PATTERN_IN_SRAM
+    patternInFlash = 0U;
+#else
+    patternInFlash = 1U;
+#endif
+    
+    returnCode = EE_FlashWriteWithPadding((uint32_t)invalidPattern,
+                                          EE_INVALID_PATTERN_SIZE_IN_BYTE,
+                                          patternInFlash,
+                                          recordAddr + EE_RECORD_INVALID_OFFSET,
+                                          CallBack);
+    
+    return returnCode;
+}
+
+/***************************************************************************
+ * @brief      Write and validate a record in a specific sector
+ * 
+ * 
+ * @param[in]  secInfo: points to sector info struct
+ * @param[in]  recordID: the ID of record to be written
+ * @param[in]  size: the size of data to be written. 
+ * @param[in]  src: it points to the buffer that stores the data to be written
+ * @param[in]  srcInFlash: it indicates if the location of data to be written
+ *                         is in FLASH.
+ *                         - 1: in flash
+ *                         - 0: not in flash
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ * @return    EE_OK
+ *            EE_ERROR_NO_ENOUGH_SPACE
+ *            EE_ERROR_FLASH_ERR
+ *
+ ****************************************************************************/
+uint32_t EE_WriteRecordInSector(EE_SectorInfo_t *secInfo, uint16_t recordID, 
+                                uint16_t size, const uint8_t *src, uint32_t srcInFlash,
+                                void (*CallBack)(void))
+{
+    uint32_t alignedSize, space;
+    uint32_t returnCode;
+    uint32_t dataAddr, hdrAddr;
+    uint8_t recordInfo[EE_FL_PHRASE_SIZE];
+    uint32_t checkSum, i ;
+    FlagStatus_t blankFlag = RESET;
+    FlagStatus_t checkFlag = RESET;
+    
+    space = secInfo->dataAddr - secInfo->recordAddr;
+    
+    if(size == 0U)
+    {
+        return EE_ERROR_INPUT_PARA;
+    }
+        
+    alignedSize = EE_AlignToPhraseSize(size);
+    
+    if((alignedSize + (2U * EE_RECORD_HEADER_LEN )) > space )
+    {
+        returnCode = EE_ERROR_NO_ENOUGH_SPACE;
+    }
+    else
+    {
+        dataAddr = secInfo->dataAddr - alignedSize;
+        hdrAddr = secInfo->recordAddr;
+        
+        /* check if the flash location for record header is empty */
+        returnCode = EE_FlashBlankCheck(hdrAddr, EE_RECORD_HEADER_LEN, 
+                                        &blankFlag,CallBack);
+        if((returnCode != EE_OK) || (blankFlag == RESET))
+        {
+            return EE_ERROR_WRITTEN_LOC_NOT_EMPTY;
+        }        
+    
+        /* init recordInfo */
+        for(i = 0U; i<EE_FL_PHRASE_SIZE;i++)
+        {
+            recordInfo[i] = 0xFFU;
+        }        
+        
+        checkSum = (uint32_t)recordID + (uint32_t)size + dataAddr;
+        
+        HalfwordToBytes(recordID, &(recordInfo[EE_RECORD_ID_OFFSET]));
+        HalfwordToBytes(size, &(recordInfo[EE_RECORD_SIZE_OFFSET]));
+        WordToBytes(dataAddr, &(recordInfo[EE_RECORD_ADDR_OFFSET]));
+        WordToBytes(checkSum, &(recordInfo[EE_RECORD_CHECKSUM_OFFSET]));
+        
+        /* write record info */
+        returnCode = EE_FlashWriteWithPadding((uint32_t)recordInfo, EE_RECORD_INFO_LEN,
+                                              0, hdrAddr, CallBack);
+        if(returnCode != EE_OK)
+        {
+            return returnCode;
+        }
+        
+        blankFlag = RESET;
+        /* check if the flash location for record data is empty */
+        returnCode = EE_FlashBlankCheck(dataAddr, alignedSize, &blankFlag,
+                                        CallBack);
+        if((returnCode != EE_OK) || (blankFlag == RESET))
+        {
+            return EE_ERROR_WRITTEN_LOC_NOT_EMPTY;
+        }
+        
+        secInfo->recordAddr += EE_RECORD_HEADER_LEN;
+        secInfo->dataAddr = dataAddr;
+        /* write data into flash */
+        returnCode = EE_FlashWriteWithPadding((uint32_t)src, size, srcInFlash, dataAddr, 
+                                              CallBack);
+        if(EE_OK == returnCode)
+        {
+            returnCode = EE_DataCheck(dataAddr, src, size, &checkFlag, CallBack);
+            
+            if((returnCode == EE_OK) && (checkFlag == SET))
+            {
+                /* validate record */
+                returnCode = EE_ValidateRecord(hdrAddr, CallBack);
+            }
+            else
+            {
+                returnCode |= EE_ERROR_WRITTEN_VALUE;
+            }            
+        }
+        else
+        {
+            returnCode = EE_ERROR_READ_REC_DATA_ECC;
+        }
+    }
+    
+    return returnCode;
+}
+
+/***************************************************************************
+ * @brief      erase EE sector
+ * 
+ * 
+ * @param[in]  pSector: points to sector config struct
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ *
+ ****************************************************************************/
+uint32_t EE_EmptySector(const EE_SectorConfig_t* pSector, void (*CallBack)(void))
+{
+    uint32_t returnCode;
+
+    /* erase the EE sector */
+    returnCode = EE_SectorErase(pSector->startAddr,pSector->size,CallBack);
+   
+    return returnCode;    
+}
+
+/***************************************************************************
+ * @brief      Write sector information into sector header
+ * 
+ * 
+ * @param[in]  pSector: points to sector config struct
+ * @param[in]  id: the active sector ID to be written into sector header
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ *
+ ****************************************************************************/
+uint32_t EE_FormatSector(const EE_SectorConfig_t* pSector, uint32_t id, 
+                         void (*CallBack) (void))
+{
+    uint8_t sectorInfo[EE_FL_PHRASE_SIZE];
+    uint32_t checkSum;
+    uint32_t returnCode;
+        
+    checkSum = id + pSector->startAddr + pSector->size;
+        
+    WordToBytes(id, &(sectorInfo[EE_SECTOR_ID_OFFSET]));
+    WordToBytes(pSector->startAddr, &(sectorInfo[EE_SECTOR_ADDR_OFFSET]));
+    WordToBytes(pSector->size, &(sectorInfo[EE_SECTOR_SIZE_OFFSET]));
+    WordToBytes(checkSum, &(sectorInfo[EE_SECTOR_CHECKSUM_OFFSET]));
+        
+    /* write record info */
+    returnCode = EE_FlashWriteWithPadding((uint32_t)sectorInfo, 
+                                          EE_SECTOR_INFO_LEN, 0,
+                                          pSector->startAddr, CallBack);
+    
+    return returnCode;    
+}
+
+/***************************************************************************
+ * @brief      Check if there is enough space in the sector to store new
+ *             record
+ * 
+ * 
+ * @param[in]  secInfo: points to sector information struct
+ * @param[in]  dataSize: data size of the record
+ *
+ * @return 
+ *           - 0: no enough space
+ *           - 1: space is enough 
+ ****************************************************************************/
+uint32_t EE_CheckSectorSpaceForRecord(EE_SectorInfo_t* secInfo, 
+                                      uint16_t dataSize)
+{
+    uint32_t alignedSize, space,res;
+    
+    space = secInfo->dataAddr - secInfo->recordAddr;
+        
+    alignedSize = EE_AlignToPhraseSize(dataSize);
+
+    /* One EE_RECORD_HEADER_LEN must be left blank to have a clear separation
+       between header space and data space */
+    if((alignedSize + (2U * EE_RECORD_HEADER_LEN)) > space )
+    {
+        /* no enough space */
+        res = 0;
+    }
+    else
+    {
+        /* space is enough for new record */
+        res = 1;
+    }
+    
+    return res;
+}
+
+/***************************************************************************
+ * @brief      scan the whole active sector, update active sector information, 
+ *             update the cache if it is enabled
+ * 
+ * 
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ *
+ ****************************************************************************/
+uint32_t EE_ScanSector(EE_Config_t* eeConfig, ControlState_t swapEn, 
+                       void (*CallBack)(void))
+{    
+    const EE_SectorConfig_t *pSector = eeConfig->sectors[eeConfig->actSecInfo.index];
+    uint32_t recordAddr = pSector->startAddr + EE_SECTOR_HEADER_LEN;
+    uint32_t dataAddr = pSector->startAddr + pSector->size;
+    uint32_t returnCode = EE_OK;
+    EE_RecordHeader_t recHead;
+    FlagStatus_t swapFlag = RESET, scanEndFlag = RESET, lastHeaderInvalid = RESET;
+    
+    while((scanEndFlag == RESET) && (dataAddr > (recordAddr + EE_RECORD_HEADER_LEN*2U)))
+    {        
+        returnCode = EE_ReadRecordHeader(recordAddr, &recHead, CallBack);
+        if((returnCode != EE_OK) && (swapEn == ENABLE))
+        {
+            swapFlag = SET;
+        }
+        
+        switch(recHead.recordStatus)
+        {
+            case EE_REC_STAT_VALID:
+                /* Check if the cache table is enabled */
+                if(eeConfig->cacheEn == ENABLE)
+                {
+                    EE_UpdateCacheTable(eeConfig->cTable, recHead.recordID, 
+                                        recordAddr);
+                }
+            
+                recordAddr += EE_RECORD_HEADER_LEN;
+                dataAddr = recHead.dataStartAddr;
+                lastHeaderInvalid = RESET;
+                break;
+                
+            case EE_REC_STAT_DELETED:
+            case EE_REC_STAT_INCONSISTENT:
+                recordAddr += EE_RECORD_HEADER_LEN;
+                dataAddr = recHead.dataStartAddr;
+                lastHeaderInvalid = RESET;
+                break;
+                
+            case EE_REC_STAT_HEADER_INVALID:
+                recordAddr += EE_RECORD_HEADER_LEN;
+                lastHeaderInvalid = SET;
+                /* the data start address info is invalid, so do not change the
+                   dataAddr */
+                break;
+                
+            case EE_REC_STAT_HEADER_BLANK:
+                scanEndFlag = SET;
+                break;
+                
+            default:
+                /* impossible */
+                break;
+        }
+    }
+    
+    eeConfig->actSecInfo.recordAddr = recordAddr;
+    eeConfig->actSecInfo.dataAddr = dataAddr;
+    
+    /* if last record header is invalid, swap sector, no matter the swapEn is
+       enabled or not */
+    if(lastHeaderInvalid == SET)
+    {
+        swapFlag = SET;
+    }
+    
+    if((swapFlag == SET) || (dataAddr <= (recordAddr + EE_RECORD_HEADER_LEN*2U)))
+    {
+        returnCode = EE_SectorSwap(eeConfig, CallBack);
+        if(returnCode != EE_OK)
+        {
+            returnCode = EE_ERROR_SWAP;
+        }
+    }
+    
+    return returnCode;
+}
+
+/***************************************************************************
+ * @brief      swap sectors
+ * 
+ * 
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ *
+ ****************************************************************************/
+uint32_t EE_SectorSwap(EE_Config_t* eeConfig, void (*CallBack)(void))
+{
+    uint32_t nextSector, actSector, id;
+    uint32_t returnCode;
+    EE_SectorHeader_t secHead;
+    EE_RecordHeader_t recHead;
+    uint16_t nextRecordId;
+    uint32_t recordAddr;
+    const EE_SectorConfig_t *srcSector, *destSector;
+    EE_SectorInfo_t destSecInfo;
+    EE_SectorInfo_t *srcSecInfo;
+    
+    actSector = eeConfig->actSecInfo.index;
+    srcSecInfo = &(eeConfig->actSecInfo);
+    
+    /* check the active sector status */
+    if(EE_OK != EE_ReadSectorHeader(eeConfig->sectors[actSector]->startAddr, &secHead, CallBack))
+    {
+        /* do nothing */
+    }
+    
+    if(EE_SEC_STAT_VALID != secHead.status)
+    {
+        /* the status of active sector is not valid */
+        returnCode = EE_ERROR_INPUT_PARA;
+        return returnCode;
+    }
+    
+    srcSector = eeConfig->sectors[actSector];
+    nextSector = (actSector + 1U) % eeConfig->sectorNum;
+    destSector = eeConfig->sectors[nextSector];
+    /* empty the next sector */
+    returnCode = EE_EmptySector(destSector,CallBack);
+    if(returnCode != EE_OK)
+    {
+        return returnCode;
+    }
+    
+    destSecInfo.status = EE_SEC_STAT_HEADER_BLANK;
+    
+    /* format next sector */
+    id = srcSecInfo->id + 1U;
+    returnCode =  EE_FormatSector(destSector, id, CallBack);
+    if(returnCode != EE_OK)
+    {
+        return returnCode;
+    }
+    /* update destination sector config parameters */
+    destSecInfo.id = id;
+    destSecInfo.recordAddr = destSector->startAddr + EE_RECORD_HEADER_LEN;
+    destSecInfo.dataAddr = destSector->startAddr + destSector->size;
+    destSecInfo.status = EE_SEC_STAT_INCONSISTENT;
+    destSecInfo.index = nextSector;
+    
+    /* swap records */ 
+    nextRecordId = 0U;
+    if(eeConfig->cacheEn == ENABLE)
+    {
+        /* Check for each item in the cache table */
+        for (nextRecordId = 0U; nextRecordId < eeConfig->cTable->num; nextRecordId++)
+        {
+            /* Get record address */
+            recordAddr = eeConfig->cTable->cachePtr[nextRecordId];
+
+            /* If the record is in the source sector */
+            if ((recordAddr > srcSector->startAddr) &&
+                (recordAddr < (srcSector->startAddr + srcSector->size)))
+            {
+                /* Read the record */
+                if(EE_OK != EE_ReadRecordHeader(recordAddr, &recHead, CallBack))
+                {
+                    /* the recordStatus must be EE_REC_STAT_HEADER_INVALID, it
+                       will be handled below, so do nothing here */
+                }
+                
+                if(recHead.recordStatus == EE_REC_STAT_VALID)
+                {
+                    returnCode = EE_WriteRecordInSector(&destSecInfo, 
+                                                        recHead.recordID, 
+                                                        recHead.dataSize,
+                                                        (uint8_t *)recHead.dataStartAddr,
+                                                        1U, CallBack);
+
+                    if(returnCode == EE_OK)
+                    {
+                        /* Check if the cache table is enabled */
+                        if(eeConfig->cacheEn == ENABLE)
+                        {
+                            EE_UpdateCacheTable(eeConfig->cTable, recHead.recordID, 
+                                                destSecInfo.recordAddr - EE_RECORD_HEADER_LEN);
+                        }
+                    }
+                    else if( returnCode == EE_ERROR_READ_REC_DATA_ECC)
+                    {
+                        /* read source record data ECC,  the status of this
+                           written record is inconsistent, skip this record */
+                        if(eeConfig->cacheEn == ENABLE)
+                        {
+                            /* delete it in the cache */
+                            EE_UpdateCacheTable(eeConfig->cTable, recHead.recordID, 
+                                                EE_RECORD_ADDR_EMPTY);
+                        }                        
+                    }
+                    else
+                    {
+                        /* fail to write a record, stop and return */
+                        return returnCode;
+                    }
+                }
+                else
+                {
+                    /* it is in cache, but the status of record is not valid 
+                       the cache item error */
+                    returnCode = EE_ERROR_CACHE_ITEM;
+                    return returnCode;
+                }
+            }
+        }
+    }
+    
+    /* Continue copy records aren't in the cache table */
+    while((nextRecordId <= eeConfig->maxRecordId) && (returnCode == EE_OK))
+    {
+        EE_SearchRecord(eeConfig, nextRecordId, &recordAddr, CallBack);
+        if((recordAddr != EE_RECORD_ADDR_EMPTY) && 
+           (recordAddr != EE_RECORD_ADDR_DELETED))
+        {
+            /* Read the record header*/
+            returnCode = EE_ReadRecordHeader(recordAddr, &recHead, CallBack);
+            if(returnCode != EE_OK)
+            {
+                /* record searched but flash error when read header */
+                return returnCode;
+            }
+                
+            if(recHead.recordStatus == EE_REC_STAT_VALID)
+            {
+                returnCode = EE_WriteRecordInSector(&destSecInfo, 
+                                                    recHead.recordID, 
+                                                    recHead.dataSize,
+                                                    (uint8_t *)recHead.dataStartAddr,
+                                                    1U, CallBack);
+
+                if(returnCode == EE_OK)
+                {
+                    /* success,go on to next record */
+                }
+                else if( returnCode == EE_ERROR_READ_REC_DATA_ECC)
+                {
+                    /* read source record data ECC, the status of this
+                       written record is inconsistent, skip this record */                    
+                }
+                else
+                {
+                    /* fail to write a record, stop and return */
+                    return returnCode;
+                }                
+            }
+            else
+            {
+                /* the read-out record status when searching and reading header 
+                   is different, it is probably flash error when read flash */
+                returnCode = EE_ERROR_FLASH_ERR;
+            }
+        }
+        
+        nextRecordId++;
+    }
+    
+    if(returnCode == EE_OK)
+    {
+        /* make the sector to be valid */
+        returnCode = EE_ValidateSector(destSector, CallBack);
+
+        if(returnCode == EE_OK)
+        {            
+            if(EE_OK != EE_ReadSectorHeader(eeConfig->sectors[nextSector]->startAddr, 
+                                            &secHead, CallBack))
+            {
+                /* the secHead.status must not be EE_SEC_STAT_VALID, so it is 
+                   handled below, do nothing here */
+            }
+            if((secHead.status != EE_SEC_STAT_VALID) || 
+               (destSecInfo.id != secHead.id))
+            {
+                returnCode = EE_ERROR_SWAP;
+            }
+            else
+            {                
+                eeConfig->actSecInfo.index = nextSector;
+                eeConfig->actSecInfo.id = destSecInfo.id;
+                eeConfig->actSecInfo.status = EE_SEC_STAT_VALID;
+                eeConfig->actSecInfo.dataAddr = destSecInfo.dataAddr;
+                eeConfig->actSecInfo.recordAddr = destSecInfo.recordAddr;
+                
+                /* invalidated the source sector */
+                returnCode = EE_InvalidateSector(srcSector, CallBack);
+            }
+        }
+    }
+    
+    return returnCode;
+}
diff --git a/Aging_app/eeprom/Src/eep_emulation.c b/Aging_app/eeprom/Src/eep_emulation.c
new file mode 100644 (file)
index 0000000..4ab390b
--- /dev/null
@@ -0,0 +1,430 @@
+/***************************************************************************//**
+ * @file     eep_emulation.c
+ * @brief    EEPROM emulation source file.
+ * @version  V1.3
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.
+ * 
+ ******************************************************************************/
+#include "eep_emulation.h"
+#include "ee_middlelevel.h"
+
+/***************************************************************************!
+ * @brief: Init the EEPROM Emulation driver
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  swapEn: It indicates if it swaps sectors when it finds invalid 
+ *                     record header during sector scanning.
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return result
+ *         EE_OK
+ *         EE_ERROR_BUSY
+ *         EE_ERROR_NOT_ENOUGH_SECTORS
+ *         EE_ERROR_FLASH_ERR
+ *         EE_ERROR_ECC
+ *         EE_ERROR_REC_HEADER_INVALID
+ *         EE_ERROR_INPUT_PARA
+ *         EE_ERROR_SWAP
+ *         
+ *
+ ****************************************************************************/
+uint32_t EE_Init(EE_Config_t* eeConfig, ControlState_t swapEn, 
+                 void (*CallBack) (void))
+{
+    const EE_SectorConfig_t* pSector;
+    EE_SectorHeader_t secHeader;
+    uint32_t returnCode;
+    uint32_t temp, cnt;
+    uint16_t i;    
+    
+    if (eeConfig->sectorNum < MIN_SECTOR_NUM)
+    {
+        /* There isn't enough number of sectors for emulation */
+        returnCode = EE_ERROR_NOT_ENOUGH_SECTORS;
+    }
+    else if (eeConfig->busyFlag == SET)
+    {
+        returnCode = EE_ERROR_BUSY;
+    }
+    else
+    {
+        /* Set the busy flag */
+        eeConfig->busyFlag = SET;
+        
+        /* Clear Cache table */
+        if (eeConfig->cacheEn == ENABLE)
+        {
+            /* Get number of Cache items */
+            temp = eeConfig->cTable->num;  
+            for (i = 0U; i < temp; i++)
+            {
+                 EE_UpdateCacheTable(eeConfig->cTable, i, EE_RECORD_ADDR_EMPTY);
+            }
+        }
+        
+        eeConfig->actSecInfo.id = 0U;
+        eeConfig->actSecInfo.index = 0U;
+        eeConfig->actSecInfo.status = EE_SEC_STAT_HEADER_INVALID;
+        
+        /* scan sector headers to find valid sector */
+        temp = eeConfig->sectorNum;
+        for (i = 0U; i < temp; i++)
+        {
+            pSector = eeConfig->sectors[i];
+            
+            cnt = 0;
+            do
+            {
+                returnCode = EE_ReadSectorHeader(pSector->startAddr, &secHeader,
+                                                 CallBack);
+                cnt++;
+                if(cnt >= EE_RETRY_TIMES)
+                {
+                    break;
+                }
+            }while(returnCode != EE_OK);
+            
+            if(returnCode != EE_OK)
+            {
+                /* read sector header error */
+            }
+            else
+            {
+                if(secHeader.status == EE_SEC_STAT_VALID)
+                {
+                    if((secHeader.id > eeConfig->actSecInfo.id) && 
+                       (secHeader.startAddr == pSector->startAddr) &&
+                       (secHeader.size == pSector->size))
+                    {
+                        eeConfig->actSecInfo.id = secHeader.id;
+                        eeConfig->actSecInfo.status = EE_SEC_STAT_VALID;
+                        eeConfig->actSecInfo.index = i;
+                        eeConfig->actSecInfo.recordAddr = pSector->startAddr 
+                                                         + EE_SECTOR_HEADER_LEN;
+                        eeConfig->actSecInfo.dataAddr = pSector->startAddr 
+                                                        + pSector->size;
+                    }
+                }
+            }
+        }        
+        
+        if(eeConfig->actSecInfo.id == 0U)
+        {
+            /* no active sector is found, erase the first sector */
+            pSector = eeConfig->sectors[0];
+            /* empty sector */
+            returnCode = EE_EmptySector(pSector,CallBack);
+
+            if(returnCode == EE_OK)
+            {
+                /* format the sector */
+                returnCode =  EE_FormatSector(pSector, 1, CallBack);
+                if(returnCode == EE_OK)
+                {
+                    /* validate the sector */
+                    returnCode =  EE_ValidateSector(pSector, CallBack);
+                        
+                    eeConfig->actSecInfo.id = 1U;
+                    eeConfig->actSecInfo.index = 0;
+                    eeConfig->actSecInfo.status = EE_SEC_STAT_VALID;
+                    eeConfig->actSecInfo.recordAddr = pSector->startAddr 
+                                                     + EE_SECTOR_HEADER_LEN;
+                    eeConfig->actSecInfo.dataAddr = pSector->startAddr 
+                                                    + pSector->size;
+                }
+            }
+            else
+            {
+                returnCode = EE_ERROR_FLASH_ERR;
+            }
+        }
+        else
+        {            
+            returnCode = EE_ScanSector(eeConfig, swapEn, CallBack);            
+        }        
+        
+        /* Unlock the busy lock */
+        eeConfig->busyFlag = RESET;
+    }
+
+    return returnCode;
+}
+/***************************************************************************//**
+ * @brief  This API is to read the specific data record. The data record size
+ *         is determined by the data size stored in emulated EEPROM.
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  recordID: the ID of record to be read
+ * @param[in]  dataSize: the size of data to be read from the record. It can not
+ *                       exceeds the size of record.
+ * @param[in]  buf: it points to the buffer that stores the data read out from
+ *                  record.
+ * @param[out]  acturalReadSize: the size of data that are actually read out 
+ *                               will be stored to the memory pointed by
+ *                               acturalReadSize after calling this function
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @note       User must ensure that the data size to be read does not exceed 
+ *             the buffer size. 
+ *             
+ *
+ * @return     EE_OK
+ *             EE_ERROR_BUSY -- EE is writing or erasing
+ *             EE_ERROR_DATA_NOT_FOUND
+ *             EE_ERROR_REC_ID_OUT_OF_RANGE
+ *             EE_ERROR_ECC
+ *             EE_ERROR_REC_HEADER_INVALID
+ *
+ ******************************************************************************/  
+uint32_t EE_ReadRecord(EE_Config_t* eeConfig, uint16_t recordID, 
+                       uint16_t dataSize, uint8_t *buf, 
+                       uint16_t *acturalReadSize, void (*CallBack) (void))
+{
+    /* record header address */
+    uint32_t recordAddr = EE_RECORD_ADDR_EMPTY;  
+    uint32_t returnCode = EE_OK;
+    
+    if(recordID > eeConfig->maxRecordId)
+    {
+        returnCode = EE_ERROR_REC_ID_OUT_OF_RANGE;
+        return returnCode;
+    }
+
+    /* Check the write lock */
+    if (eeConfig->busyFlag == SET)
+    {
+        /* EE is busy */
+        returnCode = EE_ERROR_BUSY;
+    }
+    else
+    {
+        *acturalReadSize = 0U;
+        
+         /* Search in the cache table */
+        if(eeConfig->cacheEn == ENABLE)
+        {
+            returnCode = EE_SearchInCache(eeConfig->cTable, recordID, &recordAddr);            
+        }
+
+        /* Search in flash sectors if cache is disable or the record is not in cache */
+        if((eeConfig->cacheEn == DISABLE) || ((returnCode & EE_ERROR_NOT_IN_CACHE) != 0U))
+        {
+            returnCode = EE_SearchInSectors(eeConfig, recordID, &recordAddr,
+                                            CallBack);
+        }
+
+        /* Read record if found */
+        if(returnCode == EE_OK)
+        {
+            returnCode = EE_ReadRecordAtAddr(recordAddr, dataSize, buf, 
+                                             acturalReadSize, CallBack);
+            if(returnCode != EE_OK)
+            {
+                if(eeConfig->cacheEn == ENABLE)
+                {
+                    EE_UpdateCacheTable(eeConfig->cTable, recordID, 
+                                        EE_RECORD_ADDR_EMPTY);
+                }
+            }
+        }
+        else
+        {
+            returnCode |= EE_ERROR_DATA_NOT_FOUND;
+        }
+    }
+
+    return returnCode;
+}
+
+/***************************************************************************
+ * @brief; This API is to write data records to the EEPROM emulated Flash
+ *          and re-write data record if this program operation fails.
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  recordID: the ID of record to be written
+ * @param[in]  dataSize: the size of data to be written. 
+ * @param[in]  buf: it points to the buffer that stores the data to be written
+ * @param[in]  srcInFlash: it indicates if the location of data(buf) to be 
+ *                         written is in FLASH.
+ *                         - 1: in flash
+ *                         - 0: not in flash
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return EE_OK
+ *         EE_ERROR_BUSY -- EE is writing or erasing
+ *         EE_ERROR_NO_ENOUGH_SPACE
+ *         EE_ERROR_SWAP
+ *         EE_ERROR_INPUT_PARA
+ *         EE_ERROR_WRITTEN_LOC_NOT_EMPTY
+ *         EE_ERROR_READ_REC_DATA_ECC
+ *         EE_ERROR_WRITTEN_VALUE
+ *         EE_ERROR_ECC
+ *
+ ****************************************************************************/
+uint32_t EE_WriteRecord(EE_Config_t* eeConfig, uint16_t recordID, 
+                        uint16_t dataSize,const uint8_t *buf, 
+                        uint32_t srcInFlash,void (*CallBack) (void))
+{
+    EE_SectorInfo_t *secInfo;
+    uint32_t returnCode = EE_OK;
+    uint32_t spaceFlag;
+    
+    if(recordID > eeConfig->maxRecordId)
+    {
+        returnCode = EE_ERROR_REC_ID_OUT_OF_RANGE;
+        return returnCode;
+    }
+        
+    if(dataSize == 0U)
+    {
+        returnCode = EE_ERROR_INPUT_PARA;
+        return returnCode;
+    }
+    
+    if (eeConfig->busyFlag == SET)
+    {
+        /* EE is busy */
+        returnCode = EE_ERROR_BUSY;
+    }
+    else
+    {
+        /* Set the busy flag */
+        eeConfig->busyFlag = SET; 
+
+        secInfo = &(eeConfig->actSecInfo);
+        spaceFlag = EE_CheckSectorSpaceForRecord(secInfo,dataSize);                                     
+
+        /* One EE_RECORD_HEADER_LEN must be left blank to have a clear separation
+        between header space and data space */
+        if(spaceFlag == 0U )
+        {
+            /* The record won't fit into current sector. swap sectors */
+            returnCode = EE_SectorSwap(eeConfig, CallBack);
+            
+            if(returnCode == EE_OK)
+            {
+                secInfo = &(eeConfig->actSecInfo);
+                spaceFlag = EE_CheckSectorSpaceForRecord(secInfo,dataSize);
+                if(spaceFlag == 0U)
+                {
+                    returnCode |= EE_ERROR_NO_ENOUGH_SPACE;
+                }
+                else
+                {
+                    /* enough space for new record */
+                }
+            }
+            else
+            {
+                returnCode |= EE_ERROR_SWAP;
+            }
+        }
+        
+        if(returnCode == EE_OK)
+        {
+            returnCode |= EE_WriteRecordInSector(secInfo, recordID, dataSize, 
+                                                buf, srcInFlash, CallBack);
+            if(returnCode == EE_OK)
+            {
+                /* Check if the cache table is enabled */
+                if(eeConfig->cacheEn == ENABLE)
+                {
+                    EE_UpdateCacheTable(eeConfig->cTable, recordID, 
+                                        secInfo->recordAddr - EE_RECORD_HEADER_LEN);
+                }
+            }
+        }
+        
+        /* clear the busy flag */
+        eeConfig->busyFlag = RESET;        
+    }
+
+    return returnCode;
+}
+
+/***************************************************************************//**
+ * @brief  This API is to delete the specific data record. 
+ *
+ * @param[in]  eeConfig: EE configuration struct
+ * @param[in]  recordID: the ID of record to be read
+ * @param[in]  CallBack: it points to a function to service the time critical 
+ *                       events. Any code reachable from this function
+ *                       must not be placed in the same Flash block as the 
+ *                       EEPROM emulation
+ *
+ * @return     EE_OK
+ *             EE_ERROR_BUSY -- EE is writing or erasing
+ *             EE_ERROR_DATA_NOT_FOUND
+ *             EE_ERROR_REC_ID_OUT_OF_RANGE
+ *             EE_ERROR_FLASH_ERR
+ *
+ ******************************************************************************/  
+uint32_t EE_DeleteRecord(EE_Config_t* eeConfig, uint16_t recordID, 
+                         void (*CallBack) (void))
+{
+    uint32_t recordAddr = EE_RECORD_ADDR_EMPTY;  /* record header address */
+    uint32_t returnCode = EE_OK;   /* Return code */
+    
+    if(recordID > eeConfig->maxRecordId)
+    {
+        returnCode = EE_ERROR_REC_ID_OUT_OF_RANGE;
+        return returnCode;
+    }
+
+    /* Check the write lock */
+    if (eeConfig->busyFlag == SET)
+    {
+        /* EE is busy */
+        returnCode = EE_ERROR_BUSY;
+    }
+    else
+    {      
+         /* Search in the cache table */
+        if(eeConfig->cacheEn == ENABLE)
+        {
+            returnCode = EE_SearchInCache(eeConfig->cTable, recordID, &recordAddr);            
+        }
+
+        /* Search in flash sectors if cache is disable or the record is not in cache */
+        if((eeConfig->cacheEn == DISABLE) || (returnCode == EE_ERROR_NOT_IN_CACHE))
+        {
+            returnCode = EE_SearchInSectors(eeConfig, recordID, &recordAddr,
+                                            CallBack);
+        }
+
+        /* Read record if found */
+        if(returnCode == EE_OK)
+        {
+            returnCode = EE_InvalidateRecord(recordAddr,CallBack);
+            if(returnCode == EE_OK)
+            {
+                if(eeConfig->cacheEn == ENABLE)
+                {
+                    EE_UpdateCacheTable(eeConfig->cTable, recordID, 
+                                        EE_RECORD_ADDR_DELETED);
+                }
+            }
+        }
+        else
+        {
+            returnCode |= EE_ERROR_DATA_NOT_FOUND;
+        }
+    }
+
+    return returnCode;
+}
+
+
diff --git a/Aging_app/freertos/IARProject/freertos.ewp b/Aging_app/freertos/IARProject/freertos.ewp
new file mode 100644 (file)
index 0000000..a3b588e
--- /dev/null
@@ -0,0 +1,1337 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project>
+    <fileVersion>3</fileVersion>
+    <configuration>
+        <name>Debug</name>
+        <toolchain>
+            <name>ARM</name>
+        </toolchain>
+        <debug>1</debug>
+        <settings>
+            <name>General</name>
+            <archiveVersion>3</archiveVersion>
+            <data>
+                <version>31</version>
+                <wantNonLocal>1</wantNonLocal>
+                <debug>1</debug>
+                <option>
+                    <name>ExePath</name>
+                    <state>Debug\Exe</state>
+                </option>
+                <option>
+                    <name>ObjPath</name>
+                    <state>Debug\Obj</state>
+                </option>
+                <option>
+                    <name>ListPath</name>
+                    <state>Debug\List</state>
+                </option>
+                <option>
+                    <name>GEndianMode</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>Input description</name>
+                    <state>Full formatting, without multibyte support.</state>
+                </option>
+                <option>
+                    <name>Output description</name>
+                    <state>Full formatting, without multibyte support.</state>
+                </option>
+                <option>
+                    <name>GOutputBinary</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OGCoreOrChip</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>GRuntimeLibSelect</name>
+                    <version>0</version>
+                    <state>2</state>
+                </option>
+                <option>
+                    <name>GRuntimeLibSelectSlave</name>
+                    <version>0</version>
+                    <state>2</state>
+                </option>
+                <option>
+                    <name>RTDescription</name>
+                    <state>Use the full configuration of the C/C++ runtime library. Full locale interface, C locale, file descriptor support, multibytes in printf and scanf, and hex floats in strtod.</state>
+                </option>
+                <option>
+                    <name>OGProductVersion</name>
+                    <state>8.32.4.20866</state>
+                </option>
+                <option>
+                    <name>OGLastSavedByProductVersion</name>
+                    <state>8.50.6.28950</state>
+                </option>
+                <option>
+                    <name>GeneralEnableMisra</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>GeneralMisraVerbose</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OGChipSelectEditMenu</name>
+                    <state>Z20K118M    ZhiXin Z20K118M</state>
+                </option>
+                <option>
+                    <name>GenLowLevelInterface</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>GEndianModeBE</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>OGBufferedTerminalOutput</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>GenStdoutInterface</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>GeneralMisraRules98</name>
+                    <version>0</version>
+                    <state>1000111110110101101110011100111111101110011011000101110111101101100111111111111100110011111001110111001111111111111111111111111</state>
+                </option>
+                <option>
+                    <name>GeneralMisraVer</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>GeneralMisraRules04</name>
+                    <version>0</version>
+                    <state>111101110010111111111000110111111111111111111111111110010111101111010101111111111111111111111111101111111011111001111011111011111111111111111</state>
+                </option>
+                <option>
+                    <name>RTConfigPath2</name>
+                    <state>$TOOLKIT_DIR$\inc\c\DLib_Config_Full.h</state>
+                </option>
+                <option>
+                    <name>GBECoreSlave</name>
+                    <version>28</version>
+                    <state>35</state>
+                </option>
+                <option>
+                    <name>OGUseCmsis</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OGUseCmsisDspLib</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>GRuntimeLibThreads</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CoreVariant</name>
+                    <version>28</version>
+                    <state>35</state>
+                </option>
+                <option>
+                    <name>GFPUDeviceSlave</name>
+                    <state>Z20K118M    ZhiXin Z20K118M</state>
+                </option>
+                <option>
+                    <name>FPU2</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>NrRegs</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>NEON</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>GFPUCoreSlave2</name>
+                    <version>28</version>
+                    <state>35</state>
+                </option>
+                <option>
+                    <name>OGCMSISPackSelectDevice</name>
+                </option>
+                <option>
+                    <name>OgLibHeap</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OGLibAdditionalLocale</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OGPrintfVariant</name>
+                    <version>0</version>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>OGPrintfMultibyteSupport</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OGScanfVariant</name>
+                    <version>0</version>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>OGScanfMultibyteSupport</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>GenLocaleTags</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>GenLocaleDisplayOnly</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>DSPExtension</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>TrustZone</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>TrustZoneModes</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+            </data>
+        </settings>
+        <settings>
+            <name>ICCARM</name>
+            <archiveVersion>2</archiveVersion>
+            <data>
+                <version>36</version>
+                <wantNonLocal>1</wantNonLocal>
+                <debug>1</debug>
+                <option>
+                    <name>CCDefines</name>
+                    <state>DEV_Z20K118M</state>
+                </option>
+                <option>
+                    <name>CCPreprocFile</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCPreprocComments</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCPreprocLine</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CCListCFile</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCListCMnemonics</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCListCMessages</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCListAssFile</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCListAssSource</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCEnableRemarks</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCDiagSuppress</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>CCDiagRemark</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>CCDiagWarning</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>CCDiagError</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>CCObjPrefix</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CCAllowList</name>
+                    <version>1</version>
+                    <state>00000000</state>
+                </option>
+                <option>
+                    <name>CCDebugInfo</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IEndianMode</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IProcessor</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IExtraOptionsCheck</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IExtraOptions</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>CCLangConformance</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCSignedPlainChar</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CCRequirePrototypes</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCDiagWarnAreErr</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCCompilerRuntimeInfo</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IFpuProcessor</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>OutputFile</name>
+                    <state>$FILE_BNAME$.o</state>
+                </option>
+                <option>
+                    <name>CCLibConfigHeader</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>PreInclude</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>CompilerMisraOverride</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCIncludePath2</name>
+                    <state>$PROJ_DIR$\..\..\..\StdDriver\Inc</state>
+                    <state>$PROJ_DIR$\..\..\..\Platform</state>
+                    <state>$PROJ_DIR$\..\..\..\Platform\Core</state>
+                    <state>$PROJ_DIR$\..\..\..\Platform\Devices</state>
+                    <state>$PROJ_DIR$\..\..\..\Platform\Devices\Z20K118M\Inc</state>
+                    <state>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include</state>
+                    <state>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\portable\IAR\ARM_CM0</state>
+                    <state>$PROJ_DIR$\..\inc</state>
+                    <state>$PROJ_DIR$\..\..\eeprom\Inc</state>
+                </option>
+                <option>
+                    <name>CCStdIncCheck</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCCodeSection</name>
+                    <state>.text</state>
+                </option>
+                <option>
+                    <name>IProcessorMode2</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CCOptLevel</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCOptStrategy</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCOptLevelSlave</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CompilerMisraRules98</name>
+                    <version>0</version>
+                    <state>1000111110110101101110011100111111101110011011000101110111101101100111111111111100110011111001110111001111111111111111111111111</state>
+                </option>
+                <option>
+                    <name>CompilerMisraRules04</name>
+                    <version>0</version>
+                    <state>111101110010111111111000110111111111111111111111111110010111101111010101111111111111111111111111101111111011111001111011111011111111111111111</state>
+                </option>
+                <option>
+                    <name>CCPosIndRopi</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCPosIndRwpi</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCPosIndNoDynInit</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IccLang</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IccCDialect</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IccAllowVLA</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IccStaticDestr</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IccCppInlineSemantics</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IccCmsis</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IccFloatSemantics</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCOptimizationNoSizeConstraints</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCNoLiteralPool</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCOptStrategySlave</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCGuardCalls</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CCEncSource</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCEncOutput</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CCEncOutputBom</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CCEncInput</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IccExceptions2</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IccRTTI2</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OICompilerExtraOption</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CCStackProtection</name>
+                    <state>0</state>
+                </option>
+            </data>
+        </settings>
+        <settings>
+            <name>AARM</name>
+            <archiveVersion>2</archiveVersion>
+            <data>
+                <version>10</version>
+                <wantNonLocal>1</wantNonLocal>
+                <debug>1</debug>
+                <option>
+                    <name>AObjPrefix</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>AEndian</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>ACaseSensitivity</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>MacroChars</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AWarnEnable</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AWarnWhat</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AWarnOne</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>AWarnRange1</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>AWarnRange2</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>ADebug</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>AltRegisterNames</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>ADefines</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>AList</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AListHeader</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>AListing</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>Includes</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>MacDefs</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>MacExps</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>MacExec</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>OnlyAssed</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>MultiLine</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>PageLengthCheck</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>PageLength</name>
+                    <state>80</state>
+                </option>
+                <option>
+                    <name>TabSpacing</name>
+                    <state>8</state>
+                </option>
+                <option>
+                    <name>AXRef</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AXRefDefines</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AXRefInternal</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AXRefDual</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AProcessor</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>AFpuProcessor</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>AOutputFile</name>
+                    <state>$FILE_BNAME$.o</state>
+                </option>
+                <option>
+                    <name>ALimitErrorsCheck</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>ALimitErrorsEdit</name>
+                    <state>100</state>
+                </option>
+                <option>
+                    <name>AIgnoreStdInclude</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AUserIncludes</name>
+                    <state>$PROJ_DIR$\..\inc</state>
+                </option>
+                <option>
+                    <name>AExtraOptionsCheckV2</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>AExtraOptionsV2</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>AsmNoLiteralPool</name>
+                    <state>0</state>
+                </option>
+            </data>
+        </settings>
+        <settings>
+            <name>OBJCOPY</name>
+            <archiveVersion>0</archiveVersion>
+            <data>
+                <version>1</version>
+                <wantNonLocal>1</wantNonLocal>
+                <debug>1</debug>
+                <option>
+                    <name>OOCOutputFormat</name>
+                    <version>3</version>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>OCOutputOverride</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>OOCOutputFile</name>
+                    <state>freertos.hex</state>
+                </option>
+                <option>
+                    <name>OOCCommandLineProducer</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>OOCObjCopyEnable</name>
+                    <state>1</state>
+                </option>
+            </data>
+        </settings>
+        <settings>
+            <name>CUSTOM</name>
+            <archiveVersion>3</archiveVersion>
+            <data>
+                <extensions></extensions>
+                <cmdline></cmdline>
+                <hasPrio>0</hasPrio>
+            </data>
+        </settings>
+        <settings>
+            <name>BICOMP</name>
+            <archiveVersion>0</archiveVersion>
+            <data />
+        </settings>
+        <settings>
+            <name>BUILDACTION</name>
+            <archiveVersion>1</archiveVersion>
+            <data>
+                <prebuild></prebuild>
+                <postbuild></postbuild>
+            </data>
+        </settings>
+        <settings>
+            <name>ILINK</name>
+            <archiveVersion>0</archiveVersion>
+            <data>
+                <version>23</version>
+                <wantNonLocal>1</wantNonLocal>
+                <debug>1</debug>
+                <option>
+                    <name>IlinkLibIOConfig</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>XLinkMisraHandler</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkInputFileSlave</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkOutputFile</name>
+                    <state>hello_world.out</state>
+                </option>
+                <option>
+                    <name>IlinkDebugInfoEnable</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkKeepSymbols</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkRawBinaryFile</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkRawBinarySymbol</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkRawBinarySegment</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkRawBinaryAlign</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkDefines</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkConfigDefines</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkMapFile</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkLogFile</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkLogInitialization</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkLogModule</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkLogSection</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkLogVeneer</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkIcfOverride</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkIcfFile</name>
+                    <state>$PROJ_DIR$\..\..\..\Platform\Devices\Z20K118M\MakeRule\IAR\Z20K118M_flash.icf</state>
+                </option>
+                <option>
+                    <name>IlinkIcfFileSlave</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkEnableRemarks</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkSuppressDiags</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkTreatAsRem</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkTreatAsWarn</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkTreatAsErr</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkWarningsAreErrors</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkUseExtraOptions</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkExtraOptions</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkLowLevelInterfaceSlave</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkAutoLibEnable</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkAdditionalLibs</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkOverrideProgramEntryLabel</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkProgramEntryLabelSelect</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkProgramEntryLabel</name>
+                    <state>Reset_Handler</state>
+                </option>
+                <option>
+                    <name>DoFill</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>FillerByte</name>
+                    <state>0xFF</state>
+                </option>
+                <option>
+                    <name>FillerStart</name>
+                    <state>0x0</state>
+                </option>
+                <option>
+                    <name>FillerEnd</name>
+                    <state>0x0</state>
+                </option>
+                <option>
+                    <name>CrcSize</name>
+                    <version>0</version>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CrcAlign</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CrcPoly</name>
+                    <state>0x11021</state>
+                </option>
+                <option>
+                    <name>CrcCompl</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CrcBitOrder</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>CrcInitialValue</name>
+                    <state>0x0</state>
+                </option>
+                <option>
+                    <name>DoCrc</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkBE8Slave</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkBufferedTerminalOutput</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkStdoutInterfaceSlave</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CrcFullSize</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkIElfToolPostProcess</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkLogAutoLibSelect</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkLogRedirSymbols</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkLogUnusedFragments</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkCrcReverseByteOrder</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkCrcUseAsInput</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkOptInline</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkOptExceptionsAllow</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkOptExceptionsForce</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkCmsis</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkOptMergeDuplSections</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkOptUseVfe</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkOptForceVfe</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkStackAnalysisEnable</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkStackControlFile</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkStackCallGraphFile</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>CrcAlgorithm</name>
+                    <version>1</version>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>CrcUnitSize</name>
+                    <version>0</version>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkThreadsSlave</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkLogCallGraph</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkIcfFile_AltDefault</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkEncInput</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkEncOutput</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IlinkEncOutputBom</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkHeapSelect</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkLocaleSelect</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkTrustzoneImportLibraryOut</name>
+                    <state>uart_test_import_lib.o</state>
+                </option>
+                <option>
+                    <name>OILinkExtraOption</name>
+                    <state>1</state>
+                </option>
+                <option>
+                    <name>IlinkRawBinaryFile2</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkRawBinarySymbol2</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkRawBinarySegment2</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IlinkRawBinaryAlign2</name>
+                    <state></state>
+                </option>
+            </data>
+        </settings>
+        <settings>
+            <name>IARCHIVE</name>
+            <archiveVersion>0</archiveVersion>
+            <data>
+                <version>0</version>
+                <wantNonLocal>1</wantNonLocal>
+                <debug>1</debug>
+                <option>
+                    <name>IarchiveInputs</name>
+                    <state></state>
+                </option>
+                <option>
+                    <name>IarchiveOverride</name>
+                    <state>0</state>
+                </option>
+                <option>
+                    <name>IarchiveOutput</name>
+                    <state>###Unitialized###</state>
+                </option>
+            </data>
+        </settings>
+        <settings>
+            <name>BILINK</name>
+            <archiveVersion>0</archiveVersion>
+            <data />
+        </settings>
+    </configuration>
+    <group>
+        <name>Core</name>
+        <group>
+            <name>Src</name>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\Platform\Devices\Z20K118M\Startup\IAR\Z20K118M_startup.s</name>
+            </file>
+        </group>
+    </group>
+    <group>
+        <name>Drv</name>
+        <group>
+            <name>Inc</name>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_adc.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_clock.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20k11xM_dma.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_drv.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_gpio.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_stim.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_sysctrl.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_tdg.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_uart.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Inc\Z20K11xM_wdog.h</name>
+            </file>
+        </group>
+        <group>
+            <name>Src</name>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_adc.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_clock.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_dma.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_drv.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_gpio.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_stim.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_sysctrl.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_tdg.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_uart.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_wdog.c</name>
+            </file>
+        </group>
+        <file>
+            <name>$PROJ_DIR$\..\..\..\StdDriver\Src\Z20K11xM_flash.c</name>
+        </file>
+    </group>
+    <group>
+        <name>eeprom</name>
+        <group>
+            <name>inc</name>
+            <file>
+                <name>$PROJ_DIR$\..\..\eeprom\Inc\ee_config.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\eeprom\Inc\ee_lowlevel.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\eeprom\Inc\ee_middlelevel.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\eeprom\Inc\eep_emulation.h</name>
+            </file>
+        </group>
+        <group>
+            <name>src</name>
+            <file>
+                <name>$PROJ_DIR$\..\..\eeprom\Src\ee_lowlevel.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\eeprom\Src\ee_middlelevel.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\..\..\eeprom\Src\eep_emulation.c</name>
+            </file>
+        </group>
+    </group>
+    <group>
+        <name>Inc</name>
+        <file>
+            <name>$PROJ_DIR$\..\inc\adc_collect.h</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\inc\dma_transfer.h</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\inc\FreeRTOSConfig.h</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\inc\main_common.h</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\inc\system_init.h</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\inc\task_lists.h</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\inc\uart_transfer.h</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\inc\wifi_transfer.h</name>
+        </file>
+    </group>
+    <group>
+        <name>Rtos</name>
+        <group>
+            <name>Freertos</name>
+            <group>
+                <name>Src</name>
+                <group>
+                    <name>Inc</name>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\atomic.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\croutine.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\deprecated_definitions.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\event_groups.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\FreeRTOS.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\list.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\message_buffer.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\mpu_prototypes.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\mpu_wrappers.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\portable.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\projdefs.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\queue.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\semphr.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\stack_macros.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\StackMacros.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\stream_buffer.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\task.h</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\include\timers.h</name>
+                    </file>
+                </group>
+                <group>
+                    <name>port</name>
+                    <group>
+                        <name>mem</name>
+                        <file>
+                            <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\portable\MemMang\heap_4.c</name>
+                        </file>
+                    </group>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\portable\IAR\ARM_CM0\port.c</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\portable\IAR\ARM_CM0\portasm.s</name>
+                    </file>
+                    <file>
+                        <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\portable\IAR\ARM_CM0\portmacro.h</name>
+                    </file>
+                </group>
+                <file>
+                    <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\croutine.c</name>
+                </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\event_groups.c</name>
+                </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\list.c</name>
+                </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\queue.c</name>
+                </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\stream_buffer.c</name>
+                </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\tasks.c</name>
+                </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\..\RTOS\FreeRTOS\Source\timers.c</name>
+                </file>
+            </group>
+        </group>
+    </group>
+    <group>
+        <name>Src</name>
+        <file>
+            <name>$PROJ_DIR$\..\src\adc_collect.c</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\src\dma_transfer.c</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\src\eeprom.c</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\src\main.c</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\src\system_init.c</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\src\task_lists.c</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\src\uart_transfer.c</name>
+        </file>
+        <file>
+            <name>$PROJ_DIR$\..\src\wifi_transfer.c</name>
+        </file>
+    </group>
+</project>
diff --git a/Aging_app/freertos/inc/eeprom.h b/Aging_app/freertos/inc/eeprom.h
new file mode 100644 (file)
index 0000000..f5bc201
--- /dev/null
@@ -0,0 +1,58 @@
+/***************************************************************************//**
+ * @file     ee_basic_demo.h
+ * @brief    This file is a header file for EE basic demo.
+ * @version  V1.0.0
+ * @date     June-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021 Zhixin Semiconductor Ltd. All rights reserved.
+ * 
+ ******************************************************************************/
+#ifndef EEPROM_H
+#define EEPROM_H
+
+
+#include "eep_emulation.h"
+
+
+
+#define EE_BASE_ADDR            0x01000000U
+
+#define EE_SECTOR_NUM           3U
+#define EE_SECTOR_SIZE          EE_FL_SECTOR_SIZE 
+#define EE_SECTOR_0_ADDR        (EE_BASE_ADDR)
+#define EE_SECTOR_1_ADDR        (EE_SECTOR_0_ADDR + EE_SECTOR_SIZE)
+#define EE_SECTOR_2_ADDR        (EE_SECTOR_1_ADDR + EE_SECTOR_SIZE)
+
+#define EE_MAX_RECORD_ID          0x20U
+#define EE_CACHE_RECORD_NUM       (EE_MAX_RECORD_ID + 1U)
+
+#define EE_TEST_RECORD_ID        0x01U
+#define BUFFER_SIZE              0x30U
+
+
+extern EE_Config_t eeConf;
+
+typedef struct{
+    char serial[16];
+}device_info_t;
+
+typedef struct{
+    char ssid[32];
+    char password[64];
+    char srv_ip[16];
+    char srv_port[16];
+}network_cfg_t;
+/* 记录ID定义 */
+#define EE_RECORD_ID_DEVICE_INFO    0U  /* 设备信息记录ID */
+#define EE_RECORD_ID_NETWORK_CFG    1U  /* 网络配置记录ID */
+int device_info_read(device_info_t *info);
+int device_info_write(device_info_t *info);
+int network_cfg_read(network_cfg_t *cfg);
+int network_cfg_write(network_cfg_t *cfg);
+int eeprom_init( void );
+
+extern device_info_t g_devinfo;
+extern network_cfg_t g_netcfg;
+#endif
diff --git a/Aging_app/freertos/src/eeprom.c b/Aging_app/freertos/src/eeprom.c
new file mode 100644 (file)
index 0000000..f15ae5e
--- /dev/null
@@ -0,0 +1,217 @@
+/***************************************************************************//**
+ * @file     ee_basic_demo.c
+ * @brief    This file provide a demo of using EE.
+ * @version  V1.3
+ * @date     May-2021
+ * @author   Zhixin Semiconductor
+ *
+ * @note
+ * Copyright (C) 2021-2023 Zhixin Semiconductor Ltd. All rights reserved.
+ * 
+ ******************************************************************************/
+#include "eeprom.h"
+
+
+#include "Z20K11xM_wdog.h"
+#include "Z20K11xM_clock.h"
+
+/* cache configuration */
+static uint32_t cacheTable[EE_CACHE_RECORD_NUM];
+static EE_cache_t cacheConf =
+{
+    cacheTable,               /* cache start address */
+    EE_CACHE_RECORD_NUM       /* cache size */
+};
+
+/* sector 0 configuration */
+static const EE_SectorConfig_t sectorConf0 =
+{
+    EE_SECTOR_0_ADDR,          /* the start address */
+    EE_SECTOR_SIZE,            /* the size */
+};
+
+/* sector 1 configuration */
+static const EE_SectorConfig_t sectorConf1 =
+{
+    EE_SECTOR_1_ADDR,          /* the start address */
+    EE_SECTOR_SIZE,            /* the size */
+};
+
+/* sector 2 configuration */
+static const EE_SectorConfig_t sectorConf2 =
+{
+    EE_SECTOR_2_ADDR,          /* the start address */
+    EE_SECTOR_SIZE,            /* the size */
+};
+
+/* sector configuration array */
+static const EE_SectorConfig_t* sectorConfig[EE_SECTOR_NUM] =
+{
+    &sectorConf0,
+    &sectorConf1, 
+    &sectorConf2, 
+};
+
+/* EEPROM configuration */
+EE_Config_t eeConf =
+{
+    .sectorNum = EE_SECTOR_NUM,  /* the number of sectors being used emulation */
+    .sectors = sectorConfig,     /* the configuration of sectors */
+    .cacheEn = ENABLE,           /* enable cache */
+    .cTable = &cacheConf,        /* the cache config */
+    .busyFlag = RESET,
+    .maxRecordId = EE_MAX_RECORD_ID,         /* the max record ID */
+};
+
+void CallBack(void);
+static void ErrorTrap(void);
+
+void CallBack(void)
+{
+    //WDOG_Refresh();
+}
+
+static void ErrorTrap(void)
+{
+    while(true)
+    {
+        CallBack();
+    }
+}
+
+static uint32_t ret;
+static uint8_t buffer[BUFFER_SIZE];
+
+
+int eeprom_init( void )
+{
+    uint32_t i;
+    uint32_t ret;
+ 
+    /* Initialize eeprom, it will try 3 times if failed */
+    i = 0U;
+    ret = ~EE_OK;
+    while ((i < 3U) && (ret != EE_OK)){
+        ret = EE_Init(&eeConf, ENABLE, &CallBack);
+        i++;
+    }
+    if(ret == EE_OK)
+        return 0;
+    else
+        return -1;
+}
+
+int eeprom_test(void)
+{
+    device_info_t info;
+    memset( &info, 0, sizeof(device_info_t));
+
+    if( eeprom_init() == 0 ){
+        if( device_info_read(&info) == 0 )
+            info.serial[0] +=1;
+        else
+            strcpy( info.serial, "ABCDEF" );
+        if( device_info_write(&info) ==  0 ){
+            device_info_read(&info);
+        }
+    }
+
+}
+
+int device_info_read(device_info_t *info)
+{
+    uint32_t result;
+    uint16_t readSize;
+
+    if (info == NULL) {
+        return -1;
+    }
+
+    result = EE_ReadRecord(&eeConf, EE_RECORD_ID_DEVICE_INFO, 
+                          sizeof(device_info_t), (uint8_t*)info, 
+                          &readSize, CallBack);
+
+    if ((result != EE_OK) || (readSize != sizeof(device_info_t))) {
+        return -1;
+    }
+
+    return 0;
+}
+
+int device_info_write(device_info_t *info)
+{
+    uint32_t result;
+
+    if (info == NULL) {
+        return -1;
+    }
+
+    result = EE_WriteRecord(&eeConf, EE_RECORD_ID_DEVICE_INFO, 
+                           sizeof(device_info_t), (uint8_t*)info, 
+                           0, CallBack);
+
+    if (result != EE_OK) {
+        result = EE_Init(&eeConf, ENABLE, CallBack);
+        if (result != EE_OK) {
+            return -1;
+        }
+
+        result = EE_WriteRecord(&eeConf, EE_RECORD_ID_DEVICE_INFO, 
+                               sizeof(device_info_t), (uint8_t*)info, 
+                               0, CallBack);
+        if (result != EE_OK) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+int network_cfg_read(network_cfg_t *cfg)
+{
+    uint32_t result;
+    uint16_t readSize;
+
+    if (cfg == NULL) {
+        return -1;
+    }
+
+    result = EE_ReadRecord(&eeConf, EE_RECORD_ID_NETWORK_CFG, 
+                          sizeof(network_cfg_t), (uint8_t*)cfg, 
+                          &readSize, CallBack);
+
+    if ((result != EE_OK) || (readSize != sizeof(network_cfg_t))) {
+        return -1;
+    }
+
+    return 0;
+}
+
+int network_cfg_write(network_cfg_t *cfg)
+{
+    uint32_t result;
+
+    if (cfg == NULL) {
+        return -1;
+    }
+
+    result = EE_WriteRecord(&eeConf, EE_RECORD_ID_NETWORK_CFG, 
+                           sizeof(network_cfg_t), (uint8_t*)cfg, 
+                           0, CallBack);
+
+    if (result != EE_OK) {
+        result = EE_Init(&eeConf, ENABLE, CallBack);
+        if (result != EE_OK) {
+            return -1;
+        }
+
+        result = EE_WriteRecord(&eeConf, EE_RECORD_ID_NETWORK_CFG, 
+                               sizeof(network_cfg_t), (uint8_t*)cfg, 
+                               0, CallBack);
+        if (result != EE_OK) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
\ No newline at end of file
index 9432621273a4887f955c36c9ff7864795189dfc1..48b6b7086d0331f0dc7cb84b863f6dcbdfb4cb28 100644 (file)
@@ -21,6 +21,7 @@
 #include "adc_collect.h"
 #include "task_lists.h"
 
+#include "eeprom.h"
 
 /* The number of items the queue can hold. */
 #define RECV_QUEUE_LENGTH                    (5U)
@@ -70,8 +71,14 @@ void Control_ElectricRelay(bool bTurnOn);  // 
 
 int16_t Temp_Now;  // µ±Ç°Î¶È
 
+device_info_t g_devinfo;
+network_cfg_t g_netcfg;
+
 void main()
 {
+       device_info_t dev_info;
+       network_cfg_t net_cfg;
+
        /* System init */
     System_Init();
 
@@ -96,7 +103,28 @@ void main()
                
     memset(&UserDaemon, 0x0, sizeof(UserDaemon));
     UserDaemon.xUserSemaphore = xSemaphoreCreateCounting(10, 0);
-      
+
+       //ĬÈÏÖµ
+       strcpy( g_devinfo.serial, __DEVICES_ID__);
+       strcpy( g_netcfg.ssid, "zjkj1688");
+       strcpy( g_netcfg.password, "61888888");
+       strcpy( g_netcfg.srv_ip, "47.122.26.54");
+       strcpy( g_netcfg.srv_port, "9190");
+
+       if( eeprom_init() == 0 ){
+               //´Óeeprom¶ÁÈ¡É豸ÐÅÏ¢
+               if( device_info_read(&dev_info) == 0){
+                       strcpy(g_devinfo.serial, dev_info.serial);
+               }
+               //´Óeeprom¶ÁÈ¡ÍøÂçÅäÖÃ
+               if( network_cfg_read(&net_cfg) == 0 && net_cfg.ssid && net_cfg.password && net_cfg.srv_ip && net_cfg.srv_port){
+                       strcpy(g_netcfg.ssid, net_cfg.ssid);
+                       strcpy(g_netcfg.password, net_cfg.password);
+                       strcpy(g_netcfg.srv_ip, net_cfg.srv_ip);
+                       strcpy(g_netcfg.srv_port, net_cfg.srv_port);
+               }
+       }
+
     if(xRecvQueue != NULL )
     {
         Tasks_Create();