/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V3.1.0  AC7840x  Demo
 ******************************************************************************/

  /*!
 *例程说明：
 *例程5， eep.lib 使用次数计数
 */
/* ===========================================  Includes  =========================================== */

#include "debugout_ac7840x.h"
#include "clock_config.h"
#include "eep_drv.h"
#include "gpio.h"

/* ============================================  Define  ============================================ */
#define EEP_SIZE            4096U
/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
uint32_t g_UsageCount = 0;


// 如果使用了CSE 模块， 不可以将EEP使用的Sram基地址 设置为0x14000000U
const Eep_Hal_ConfigType eep_config = {
    .EepSize          = EEP_SIZE,           /*!< EEP空间大小 (byte) */
    .EepRamBaseAddr   = 0x2000D000U,        /*!< EEP使用的Sram基地址 */ 
    .EepFlashBaseAddr = 0x01000000U,        /*!< EEP使用的FLASH基地址 */
    .FlashPageSize    = 0x800U,             /*!< FLASH页大小(byte) */
    .FlashMinProByte  = 8U,                 /*!< FLASH最小编程字节(byte) */
    .IcType           = EEP_HAL_AC78406,    /*!< 芯片型号 */
    .CallbackType     = NULL,               /*!< 回调函数 */
};
/*
    注意：
    1. 用户需要保证EEP使用的Sram空间不可被其他程序使用；
    2. 如果使用了CSE模块，禁止使用0x1400_0000U做为EEP的SramR基地址；
    3. 如果使用eepRamBaseAddr为0x1400_0000U, 请注意FlexRAM需要做SRAM使用， 注意FLASH_PART的EPART位；
    4. EEP的每一项操作，初始化， 读，写和擦除都需要判断返回值以确定是否操作成功；

*/
/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */
 

/*!
 * @brief main
 *
 * @param[in]  none
 *
 * @return none
 */
void EepTest(void)
{
    static uint8_t offset =0;                               /*!< 偏移值 , 每操作一次写入， 偏移值+1 */
    uint8_t readWriteBuff = 0;                              /*!< 写入数据       */
    uint16_t i = 0;                                         /*!< 计数值         */
    Eep_Hal_StatusType res = EEP_HAL_SUCCESS;               /*!< EEP操作返回值  */

    InitDebug();                                            /*!< 串口初始化     */
    GPIO_LedInit();                                         /*!< LED初始化      */
    res = Eep_Hal_Init(&eep_config);                        /*!< 初始化EEP， 每次复位都需要执行一次EEP_Init, EEP_Init只初始化算法所需变量，不会擦除空间 */
    if(EEP_HAL_SUCCESS != res)                              /*!< 判断初始化是否成功 */
    {
        printf("LINE[%d] : ERROR_CODE[%d] EEP Init fail...\r\n", __LINE__, res);
    }
    
    res = Eep_Hal_Erase();                                  /*!< 此次测试为不掉电测试, 为避免无效数据影响，每次复位（重新测试） 都把用于模拟eeprom 的FLASH数据全部清除 */
    if(EEP_HAL_SUCCESS != res)
    {
        printf("LINE[%d] : ERROR_CODE[%d] Erase EEP fail...\r\n", __LINE__, res); 
    }
    
    printf("Eeprom UsageCount Test Star...\r\n");
    while(1)
    {

       LED3_TOGGLE;
       for(i=0; i<EEP_SIZE; i++)
       {
            readWriteBuff = i;
            readWriteBuff += offset;
            res = Eep_Hal_Write(i, &readWriteBuff, 1u);     /*!< 向0 ~ (EEP_SIZE-1) 的逻辑地址，写入数据， 用于测试写入  */
            if(EEP_HAL_SUCCESS != res)
            {
                printf("LINE[%d] : ERROR_CODE[%d] Write data[%d] fail...\r\n", __LINE__, res, i);
                LED3_ON;
                while(1);
            }
       }
       
       for(i=0; i<EEP_SIZE; i++)
       {
            res = Eep_Hal_Read(i, &readWriteBuff, 1u);      /*!< 读取0 ~ （EEP_SIZE-1) 逻辑地址数据， 与写入数据对比 */
            if(EEP_HAL_SUCCESS != res)
            {
                printf("LINE[%d] : ERROR_CODE[%d] Read data[%d] fail...\r\n", __LINE__, res, i);
                LED3_ON;
                while(1);
            }
                
            if(readWriteBuff != ((i + offset)%256))         /*!< 校验是否写入成功 */
            {
                printf("LINE[%d] : ERROR: Check fail,   Read Addr[0x%02X] Data[0x%02X]，Write Addr[0x%02X] Data[0x%02X]...\r\n ", __LINE__, i, readWriteBuff, i, (i+offset));
                LED3_ON;
                while(1);
            }
       }
       
       offset++;                                            /*!< 偏置自加 */
       g_UsageCount++;                                      /*!< 操作次数自加 */
       
       printf("\r\n*****************<    Usage Count %d    >*****************\r\n", g_UsageCount);
                                 
    }
}

/* =============================================  EOF  ============================================== */
