/* 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 tests executing FLASH operations in SRAM, 
 * simulating the scenario in IAP (In-Application Programming) upgrades where the boot program cannot include a flash driver.
 *
 */

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

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


/* ============================================  Define  ============================================ */
#define flashDriverStartAddr     (0x20002000U)
#define DFLASHPAGE1ADDR          (0x08040000U)
/* ===========================================  Typedef  ============================================ */
typedef struct{
    EFLASH_StatusType   (*eFlash_Unlock) (void);
    EFLASH_StatusType   (*eFlash_ErasePage) (uint32_t addr);
    EFLASH_StatusType   (*eFlash_Program) (uint32_t addr, uint32_t *data, uint32_t size);
    void                (*eFlash_Read) (uint32_t addr, uint32_t size, uint32_t *data);
    void                (*eFlash_Lock) (void);
}FlashOptInfo;

/* ==========================================  Variables  =========================================== */
FlashOptInfo *g_pFlashoptInfo = (void*)0;

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

/*!
 * @brief   HardFault_Handler
 *
 * @param   none
 * @return  none
 */
void HardFault_Handler()
{
    printf("\r\nInto HardFault...\r\n");
    while(1);
}
/* ======================================  Functions define  ======================================== */
/*!
 * @brief   main function
 *
 * @param   none
 * @return  int
 */
int main(void)
{
    static uint32_t writeAndReadData = 0U;
    uint32_t *tmp = 0;
    uint8_t i = 0;
    InitDebug();
    InitDelay();
    GPIO_LedInit();
    GPIO_KeyInit();
    
    printf("\r\nFlash RunInSram  Test...\r\n");
    printf("\r\nPress K3 to test Flash writing operation in SRAM, and press K4 to test Flash reading operation in SRAM\r\n");

    memset((uint8_t *)flashDriverStartAddr, 0, sizeof(data));
    memcpy((uint8_t *)flashDriverStartAddr, data, sizeof(data));
    tmp = (uint32_t *)flashDriverStartAddr;
    for(i=0; i<sizeof(FlashOptInfo)/4; i++)
    {
        /* Set offset, This step is essential and cannot be omitted. */
        tmp[i] += (uint32_t)flashDriverStartAddr;
    }
    
    g_pFlashoptInfo = (FlashOptInfo *)flashDriverStartAddr;

    while (1)
    {
        /* Press K3 to perform a read protection disable/enable test. */
        if (Get_Key3Value())
        {
            g_pFlashoptInfo->eFlash_Unlock();                   /* Unlock Flash */
            g_pFlashoptInfo->eFlash_ErasePage(DFLASHPAGE1ADDR); /* Erase Flash */
            g_pFlashoptInfo->eFlash_Program(DFLASHPAGE1ADDR, &writeAndReadData, 1); /* Write Flash */
            g_pFlashoptInfo->eFlash_Lock();                     /* Lock Flash */
            printf("\r\nWrite done...\r\n");
            writeAndReadData += 1;                              /* Update data */
        }
        
        /* Press K4 to initiate a write test and verify if the write operation is affected by the read protection */
        if (Get_Key4Value())
        {
            g_pFlashoptInfo->eFlash_Read(DFLASHPAGE1ADDR, 1, &writeAndReadData);  /* Read */
            printf("\r\nRead addr[0x%08X] is [%d]\r\n",DFLASHPAGE1ADDR,  writeAndReadData);
            writeAndReadData += 1;

        }
        RGB_Toggle();                                            /* RGB blink*/
    }
}

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

