/* 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
******************************************************************************/
/*!
 *Routine Description:
 *
 *   This function is used to test the usage of read protection. 
 *   Pressing K3, if read protection is enabled, will disable it. 
 *   Disabling read protection will result in erasing the entire chip. 
 *   If read protection is not enabled, pressing K3 will enable it. 
 *   Please note that enabling read protection requires the JTAG/SWD device to be disconnected
 *
 */

/*!
 * @file main.c
 *
 * @brief This file is main.c.
 *
 */

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

/* ============================================  Define  ============================================ */
#define     PFLASH_PAGE_254     (0x0803F800U)
#define     DFLASH_PAGE_1       (0x08040400U)
#define     FLASH_PAGE_SIZE     (0x400U)
/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */


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


/* ======================================  Functions define  ======================================== */
void WriteFlashTest(void);          /* Write Test */


/*!
 * @brief   main function
 *
 * @param   none
 * @return  int
 */
int main(void)
{
    InitDebug();
    InitDelay();
    GPIO_LedInit();
    GPIO_KeyInit();
    
    printf("Flash Read Protect Test...\r\n");
    if(EFLASH_GetReadProtectReg())
    {
        printf("\r\n[ReadProtect] The chip is in read protection mode. Pressing the K3 button disable read protection will result in a full chip erase...\r\n");
    }
    else 
    {
        printf("\r\n[UnReadProtect] Pressing the K3 button can enable read protection, which takes effect after a reset...\r\n");
    }
    printf("\r\nPress K4 for write test.\r\n");
    
    while (1)
    {
        /* Press K3 to perform a read protection disable/enable test. */
        if (Get_Key3Value())
        {
            if(EFLASH_GetReadProtectReg())
            {
                /* Disabling read protection will erase the entire chip. Please use with caution. */
                printf("\r\n After 2 seconds, the read protection will be disabled,leading to a complete chip erase...\r\n");
                mdelay(2000U);
                EFLASH_UnlockCtrl(); 
                EFLASH_DisableReadProtection();
                EFLASH_LockCtrl(); 
            }
            else 
            {
                /* Enabling read protection needs to be performed in a non-debug state, 
                   Please disconnect the JTAG/SWD device. */
                EFLASH_UnlockCtrl(); 
                EFLASH_EnableReadProtection();
                EFLASH_LockCtrl(); 
                printf("\r\n Enabling read protection takes effect after a reset... \r\n");
            }
        }
        
        /* Press K4 to initiate a write test and verify if the write operation is affected by the read protection */
        if (Get_Key4Value())
        {
            WriteFlashTest();                                    /* Write Test */
        }
        RGB_Toggle();                                            /* RGB blink */
    }
}


/*!
 * @brief   WriteFlashTest
 *          To conduct a write test only.
 * @param   none
 * @return  none
 */
void WriteFlashTest()
{
    EFLASH_StatusType rev = EFLASH_STATUS_ERROR;
    uint32_t writeAndReadData = 0U;
    EFLASH_Read(PFLASH_PAGE_254, &writeAndReadData, 1);   /* Read data from Pflash */
    printf("\r\nRead Pflash addr[0x%08X] is [%08X]\r\n", PFLASH_PAGE_254, writeAndReadData);
    EFLASH_Read(DFLASH_PAGE_1, &writeAndReadData, 1);     /* Read data from Dflash  */
    printf("\r\nRead Dflash addr[0x%08X] is [%08X]\r\n", DFLASH_PAGE_1, writeAndReadData);
    
    EFLASH_UnlockCtrl();                                 /* Unlock Flash */
    rev = EFLASH_PageErase(PFLASH_PAGE_254);             /* Erase 254 pages of Pflash. */
    if(rev != EFLASH_STATUS_SUCCESS)
    {
        printf("\r\n[%d]ERROR: Erase Dflash addr[0x%08X ~ 0x%08X] fail, error code is [%d]...\r\n", \
                __LINE__, PFLASH_PAGE_254, PFLASH_PAGE_254+FLASH_PAGE_SIZE, rev);
    }
    else
    {
        writeAndReadData += 1U;
        rev = EFLASH_PageProgram(PFLASH_PAGE_254, &writeAndReadData,  1);   /* Program Pflash */
        if(rev != EFLASH_STATUS_SUCCESS)
        {
            printf("\r\n[%d]ERROR: Program Dflash addr[0x%08X ~ 0x%08X] fail, error code is [%d]...\r\n", \
                __LINE__, PFLASH_PAGE_254, PFLASH_PAGE_254+FLASH_PAGE_SIZE, rev);
        }
        else 
        {
            printf("\r\nPflash Write done...\r\n");
        }
    }
            
    rev = EFLASH_PageErase(DFLASH_PAGE_1);                                  /* Erase 1 pages of Dflash. */
    if(rev != EFLASH_STATUS_SUCCESS)
    {
        printf("\r\n[%d]ERROR: Erase Dflash addr[0x%08X ~ 0x%08X] fail, error code is [%d]...\r\n", \
            __LINE__, DFLASH_PAGE_1, DFLASH_PAGE_1+FLASH_PAGE_SIZE, rev);
    }
    else
    {
        writeAndReadData += 1U;
        rev = EFLASH_PageProgram(DFLASH_PAGE_1, &writeAndReadData,  1);     /* Program Dflash */
        if(rev != EFLASH_STATUS_SUCCESS)
        {
            printf("\r\n[%d]ERROR: Program Dflash addr[0x%08X ~ 0x%08X] fail, error code is [%d]...\r\n", \
                __LINE__, DFLASH_PAGE_1, DFLASH_PAGE_1+FLASH_PAGE_SIZE, rev);
        }
        else 
        {
            printf("\r\nDflash Write done...\r\n");
        }
    }
            
    EFLASH_Read(PFLASH_PAGE_254, &writeAndReadData, 1);  /* Read data from Pflash */
    printf("\r\nAfter read Pflash addr[0x%08X] is [%08X]\r\n", PFLASH_PAGE_254, writeAndReadData);
    EFLASH_Read(DFLASH_PAGE_1, &writeAndReadData, 1);    /* Read data from Dflash */
    printf("\r\nAfter read Dflash addr[0x%08X] is [%08X]\r\n", DFLASH_PAGE_1, writeAndReadData);

    EFLASH_LockCtrl();                                   /* Lock Flash */
}

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


