/* 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) 2023. 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: V1.0.0 demo
******************************************************************************/
/*!
 * @file 06_FLASH_Eep_Test.c
 *
 * @brief This file is flash eeprom oprate guide.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "gpio.h"
#include "ac7803x_debugout.h"
#include "eep_drv.h"

/* ============================================  Define  ============================================ */
#define USER_EEP_SIZE            (1024U)        /* User-defined Memory Size                                                */
#define USER_EEP_RAMBASEADDR     (0x2000E000U)  /* Base Address of SRAM in Use (Not Recommended for Use Except for EEPROM  */
#define USER_EEP_FLASHBASEADDR   (0x08040000U)  /* Base Address of FLASH in Use (Not Recommended for Use Except for EEPROM */
#define USER_EEP_PAGESIZE        (0x400U)       /* FLASH Page Size, Fixed Chip Property                                    */
#define USER_EEP_MINPROBYTE      (0x04U)        /* Minimum Programming Unit of FLASH, Fixed Chip Property                  */


/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
const Eep_ConfigType eep_config = {
    .EepSize          = USER_EEP_SIZE,           /* User-defined Memory Size                                               */
    .EepRamBaseAddr   = USER_EEP_RAMBASEADDR,    /* Base Address of SRAM in Use (Not Recommended for Use Except for EEPROM */
    .EepFlashBaseAddr = USER_EEP_FLASHBASEADDR,  /* Base Address of FLASH in Use (Not Recommended for Use Except for EEPROM*/
    .FlashPageSize    = USER_EEP_PAGESIZE,       /* FLASH Page Size, Fixed Chip Property                                   */
    .FlashMinProByte  = USER_EEP_MINPROBYTE,     /* Minimum Programming Unit of FLASH, Fixed Chip Property                 */
    .IcType           = EEP_AC78036,             /* Chip Model                                                             */
    .CallbackType     = NULL,                    /* Callback Function, Set by User, Invoked When Logical Block is Changed  */
};

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


/* ======================================  Functions define  ======================================== */
/*!
 * @brief main
 *
 * @param[in]  none
 *
 * @return none
 */
void EepTest(void)
{
    static uint8_t offset =0;                               /* Offset */
    uint8_t readWriteBuff = 0;                              /* Temp ` */
    uint16_t i = 0;
    Eep_StatusType res = EEP_SUCCESS;                       /* Operation Return Value */

    res = Eep_Hal_Init(&eep_config);                        /* Initialize EEPROM, Initialize Only Algorithm Required Variables, Without Erasing Space */
    if(EEP_SUCCESS != res)                                  /* Determine if Initialization is Successful                                              */
    {
        printf("LINE[%d] : EEP Init fail, ERROR_CODE[%02X] ...\r\n", __LINE__, res);
    }
    
    res = Eep_Hal_Read(0x00U, &readWriteBuff, 1u);          /* Read First Time Usage Flag      */
    if(EEP_SUCCESS != res)                                  /* Determine if Read is Successful */
    {
        printf("LINE[%d] : Read frist erase flag fail, ERROR_CODE[%02X] ...\r\n", __LINE__, res);
    }
    
    if(0xAA == readWriteBuff)
    {
                                                            /* Not First Time Usage, No Operation Needed */
    }
    else
    {
        /* <Precautions>
           This step is not necessary, it's just to mitigate the potential impact of residual data in Flash on 
           the emulated EEPROM algorithm. Users can design according to their actual project requirements. 
        */
        printf("Frist use, Erase EEP...\r\n");
        res = Eep_Hal_Erase();                              /* Erase all Flash space used for emulating EEPROM */
        if(EEP_SUCCESS != res)                              /* Determine if Erase is Successful                */
        {
            printf("LINE[%d] : Erase EEP fail , ERROR_CODE[%02X]...\r\n", __LINE__, res);
        }
        
        readWriteBuff = 0xAA;                               /* First-time Usage Flag                                             */
        res = Eep_Hal_Write(0x00U, &readWriteBuff, 1u);     /* After erasing the space used for emulating EEPROM, write the flag */
        if(EEP_SUCCESS != res)
        {
            printf("LINE[%d] : Write frist erase flag fail, ERROR_CODE[%02X]...\r\n", __LINE__, res);
        }
    }
    printf("K3 Write , K4 Read...\r\n");

    while(1)
    {
        /***************************************************** Write Data Task  ***************************************************************/
        if(Get_Key3Value())                                     /* Press K3 to Write Data, Ensure Writing Address Does Not Exceed EEPROM Size */
        {
            printf("Write Task start...\r\n");              
            for(i=1; i< 1024; i++)
            {
                readWriteBuff = i + offset;
                res = Eep_Hal_Write(i, &readWriteBuff, 1u);     /* The address 0 is used to store the first-time usage flag, so writing starts from address 1. */
                if(EEP_SUCCESS != res)
                {
                    printf("LINE[%d] :Write data[%d] fail, ERROR_CODE[%02X] ...\r\n", __LINE__, i, res);
                }
            }
            offset ++;                                          /* After each execution of the write task, the offset increases by 1 */
            printf("Write Task end! \r\n");
        }
        
        
        /***************************************************** Read Data Task *************************************************************/
        if(Get_Key4Value())                                     /* Press K4 to Read EEPROM Data                  */
        {
            printf("Read Task start...\r\n");
            res = Eep_Hal_Read(0, &readWriteBuff, 1u);          /* Read the Flag Stored at Address 0 and Print It */
            if(EEP_SUCCESS != res)
            {
                 printf("LINE[%d] : Read data[%d] fail, ERROR_CODE[%02X]...\r\n", __LINE__, i, res);
            }
            else 
            {
                 printf(" FristUseFlag is %02X ", readWriteBuff);
            }
            
            for(i=1; i< 1024; i++)
            {
                 readWriteBuff = i;
                 res = Eep_Hal_Read(i, &readWriteBuff, 1u);     /* Read Data and Print */
                 if(EEP_SUCCESS != res)
                 {
                     printf("LINE[%d] : Read data[%d] fail, ERROR_CODE[%02X] ...\r\n", __LINE__, i, res);
                 }
                 else 
                 {
                    printf(" Addr[%04X] , Data[%02X]\r\n ", i, readWriteBuff);
                 }
            }
            printf("\r\nRead Task end!\r\n");
        }

        RGB_Toggle();                                     
    }

}

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