/* 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 01_FLASH_Basic.c
 *
 * @brief This file is flash basic oprate guide.
 *
 */

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

/* ============================================  Define  ============================================ */
#define     TEST_ADDRESS_PFLASH          0x0803F000         /* PFLASH Page 253 start address */
#define     TEST_ADDRESS_DFLASH          0x08040000         /* Dflash page 0 start address */
#define     FLASH_PAGE_SIZE              0x400              /* Page size is 0x400 */
#define     PROGRAM_DATA_LENGTH          FLASH_PAGE_SIZE/4  /* Program data length */
#define     READ_DATA_LENGTH             100/4              /* The length of read data */
/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
uint32_t g_writeBuff[PROGRAM_DATA_LENGTH] = {0};            /* Test Write Buffer  */

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


/* ======================================  Functions define  ======================================== */
/*!
 * @brief Write Pflash Job
 *
 * @param[in] none
 * @return EFLASH STATUS: eflash operation status
 */
EFLASH_StatusType Job_Pflash_Write(void)
{
    EFLASH_StatusType rev = EFLASH_STATUS_ERROR;
    
    EFLASH_UnlockCtrl();                                 /* Unlock Flash */
    printf("\r\nStar Erase Pflash[%08X ~ %08X]...\r\n ", TEST_ADDRESS_PFLASH, TEST_ADDRESS_PFLASH+FLASH_PAGE_SIZE);
    rev = EFLASH_PageErase(TEST_ADDRESS_PFLASH);         /* Erase Flash by page */
    if(rev != EFLASH_STATUS_SUCCESS)
    {
        printf("\r\n[%d]ERROR: Erase Pflash[%08X ~ %08X] fail...\r\n", \
                __LINE__, TEST_ADDRESS_PFLASH, TEST_ADDRESS_PFLASH+FLASH_PAGE_SIZE);
    }
    else
    {
        /* Program Pflash */
        rev = EFLASH_PageProgram(TEST_ADDRESS_PFLASH, g_writeBuff,  PROGRAM_DATA_LENGTH); 
        if(rev != EFLASH_STATUS_SUCCESS)
        {
            printf("\r\n[%d]ERROR: Program Pflash[%08X ~ %08X] fail...\r\n", \
                    __LINE__, TEST_ADDRESS_PFLASH, TEST_ADDRESS_PFLASH+FLASH_PAGE_SIZE);
        }
        else 
        {
            printf("\r\nPflash Write done...\r\n");
        }
    }
    EFLASH_LockCtrl();                               /* Lock Flash */
    return rev;
}


/*!
 * @brief Read Pflash Job.
 *
 * @param[in] none
 * @return EFLASH STATUS: eflash operation status
 */
void Job_Pflash_Read(void)
{
    uint32_t readBuff[READ_DATA_LENGTH] = {0};      /* Read buff */
    uint8_t *readPoint = (uint8_t *)readBuff;       /* uint32 to uint8 */
    uint8_t i = 0;
    
    EFLASH_Read(TEST_ADDRESS_PFLASH, readBuff, READ_DATA_LENGTH);   /* read data to readbuff  */
    printf("\r\nRead addr[%08X ~ %08X]: \r\n", TEST_ADDRESS_DFLASH, (TEST_ADDRESS_DFLASH+READ_DATA_LENGTH*4));
    for(i=0; i<(READ_DATA_LENGTH*4); i++)
    {
        printf("%02X ", readPoint[i]);
    }
    printf("\r\n");
}

/*!
 * @brief Write Dflash Job
 *
 * @param[in] none
 * @return EFLASH STATUS: eflash operation status
 */
EFLASH_StatusType Job_Dflash_Write(void)
{
    EFLASH_StatusType rev = EFLASH_STATUS_ERROR;
    
    EFLASH_UnlockCtrl();                            /* Unlock Flash */
    printf("\r\nStar Erase Dflash[%08X ~ %08X]...\r\n ", TEST_ADDRESS_DFLASH, TEST_ADDRESS_DFLASH+FLASH_PAGE_SIZE);
    rev = EFLASH_PageErase(TEST_ADDRESS_DFLASH);    /* Erase Flash by page */
    if(rev != EFLASH_STATUS_SUCCESS)
    {
        printf("\r\n[%d]ERROR: Erase Dflash[%08X ~ %08X] fail...\r\n", \
                __LINE__, TEST_ADDRESS_DFLASH, TEST_ADDRESS_DFLASH+FLASH_PAGE_SIZE);
    }
    else
    {
        /* Program Pflash */
        rev = EFLASH_PageProgram(TEST_ADDRESS_DFLASH,  g_writeBuff,  PROGRAM_DATA_LENGTH); 
        if(rev != EFLASH_STATUS_SUCCESS)
        {
            printf("\r\n[%d]ERROR: Program Dflash[%08X ~ %08X] fail...\r\n", \
                    __LINE__, TEST_ADDRESS_DFLASH, TEST_ADDRESS_DFLASH+FLASH_PAGE_SIZE);
        }
        else 
        {
            printf("\r\nDflash Write done...\r\n");
        }
    }
    EFLASH_LockCtrl();                               /* Lock Flash */
    return rev;
}


/*!
 * @brief Read Dflash Job
 *
 * @param[in] none
 * @return none
 */
void Job_Dflash_Read(void)
{
    uint32_t readBuff[READ_DATA_LENGTH] = {0};      /* Read buff */
    uint8_t *readPoint = (uint8_t *)readBuff;       /* uint32 to uint8 */
    uint8_t i = 0;
    
    EFLASH_Read(TEST_ADDRESS_DFLASH, readBuff, READ_DATA_LENGTH);   /* read data to Read buff  */
    printf("\r\nRead addr[%08X ~ %08X]: \r\n", TEST_ADDRESS_DFLASH, (TEST_ADDRESS_DFLASH+READ_DATA_LENGTH*4));
    for(i=0; i<(READ_DATA_LENGTH*4); i++)
    {
        printf("%02X ", readPoint[i]);
    }
    printf("\r\n");
}

/*!
 * @brief Update write buffer.
 *
 * @param[in] none
 * @return none
 */
void Job_Update_Writebuff(void)
{
    uint16_t i =0 ;
    uint8_t *updatePoint = (uint8_t *)g_writeBuff;
    for(i=0; i<FLASH_PAGE_SIZE; i++)
    {
        updatePoint[i] += 1;
    }
    printf("\r\nUpdate Write buffer done...\r\n");
}

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