/* 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) 2022. 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.
 */

/*!
 * @file ac780x_eflash.c
 *
 * @brief This file provides EFLASH integration functions.
 *
 */

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

/* ============================================  Define  ============================================ */
#define EFLASH_UNLOCK_KEY1                  (0xac7802UL)    /* eflash controler ulock key 1    */
#define EFLASH_UNLOCK_KEY2                  (0x01234567UL)  /* eflash controler ulock key 2    */
#define PFLASH_WRITE_PROTECT_NUM            (4UL)           /* P-flash write protection option byte address total count */
#define PFLASH_READ_PROTECT_NUM             (1UL)           /* Read protection option byte address total count */
#define DFLASH_WRITE_PROTECT_NUM            (2UL)           /* D-Flash write protection option byte address total count */

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Unlock the eflash controller
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS: operation successful
 *        - EFLASH_STATUS_TIMEOUT: operation timeout
 */
EFLASH_StatusType EFLASH_UnlockCtrl(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint8_t ctrlLockBit = 0U;
    uint32_t timeoutCount = 0U;

    ctrlLockBit = EFLASH_GetCtrlLockBitReg();
    while (1U == ctrlLockBit)
    {
        EFLASH_SetKeyReg(EFLASH_UNLOCK_KEY1);
        EFLASH_SetKeyReg(EFLASH_UNLOCK_KEY2);

        if (100U < timeoutCount)
        {
            ret = EFLASH_STATUS_TIMEOUT;
            break;
        }
        ctrlLockBit = EFLASH_GetCtrlLockBitReg();
        timeoutCount++;
    }

    return ret;
}

/*!
 * @brief Lock the eflash controllor
 *
 * @param[in] none
 * @return none
 */
void EFLASH_LockCtrl(void)
{
    EFLASH_LockCtrlReg();
}

/*!
 * @brief Read a specified length of data from the eflash specified address
 *
 * @param[in] addr: eflash address
 * @param[in] buffer: Point to the data bufffer for saving the read data
 * @param[in] len: read length(the unit is word)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS: Operation was successful
 *        - EFLASH_STATUS_ERROR:   Operation was failure
 */
EFLASH_StatusType EFLASH_Read(uint32_t addr, uint32_t *buffer, uint32_t len)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint32_t i;

    DEVICE_ASSERT(NULL != dataBuffer);
    if (((addr < PFLASH_END_ADDRESS) && (addr >= PFLASH_BASE_ADDRESS)) || \
            ((addr < DFLASH_END_ADDRESS) && (addr >= DFLASH_BASE_ADDRESS)))
    {
        for (i = 0; i < len; i++)
        {
            buffer[i] = *(uint32_t*)addr;
            addr += 4U;
        }
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Perform start command sequence on eflash.
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS: Operation was successful
 *        - EFLASH_STATUS_ERROR:   Operation was failure
 */
static EFLASH_StatusType FLASH_StartCmdSequence(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    /* trigger command execute */
    EFLASH_TrigCtrlCmdReg();
    /* Wait till busy bit is cleared */
    while (EFLASH_GetCmdBusyStatus());

    /* Check command execute error status */
    if (EFLASH_GetStatusReg())
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Erase specified P-Flash or D-Flash user page
 *
 * @param[in] addr: P-Flash or D-Flash page addrees to be erased (should be 4-aligned)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_PageErase(uint32_t addr)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    /* check eflash address */
    if (((addr < PFLASH_END_ADDRESS) && (addr >= PFLASH_BASE_ADDRESS)) || \
            ((addr < DFLASH_END_ADDRESS) && (addr >= DFLASH_BASE_ADDRESS)))
    {
        /* Wait for last operation to be completed */
        if (EFLASH_GetCmdBusyStatus())
        {
            ret = EFLASH_STATUS_BUSY;
        }
        else
        {
            EFLASH_CLEAR_STAT_ERROR_BITS();
            /* Configure address */
            EFLASH_SetStartAddressReg(addr);
            /* Configure command */
            EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_PAGERASE);
            /* Trigger to start */
            ret = FLASH_StartCmdSequence();
        }
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Mass erase P-Flash user area
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_MassErase(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    /* Wait for last operation to be completed */
    if (EFLASH_GetCmdBusyStatus())
    {
        ret = EFLASH_STATUS_BUSY;
    }
    else
    {
        EFLASH_CLEAR_STAT_ERROR_BITS();
        /* Configure command */
        EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_MASSRASE);
        /* Select operation in P-Flash */
        EFLASH_SetSelectReg(0U);
        /* Trigger to start */
        ret = FLASH_StartCmdSequence();
    }

    return ret;
}

/*!
 * @brief P-Flash or D-Flash program
 *
 * @param[in] addr: eflash addrees to be programed (should be 4-aligned)
 * @param[in] buffer: Point to the data to be programed
 * @param[in] len: Data length to be programed (the unit is word)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_PageProgram(uint32_t addr, uint32_t *buffer, uint32_t len)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint32_t dataCount = 0x0UL;

    DEVICE_ASSERT(NULL != dataBuffer);
    DEVICE_ASSERT(0 != dataLength);
    /* check eflash address */
    if (((addr < PFLASH_END_ADDRESS) && (addr >= PFLASH_BASE_ADDRESS)) || \
            ((addr < DFLASH_END_ADDRESS) && (addr >= DFLASH_BASE_ADDRESS)))
    {
        /* Wait for last operation to be completed */
        if (EFLASH_GetCmdBusyStatus())
        {
            ret = EFLASH_STATUS_BUSY;
        }
        else
        {
            EFLASH_CLEAR_STAT_ERROR_BITS();
            /* Configure address */
            EFLASH_SetStartAddressReg(addr);
            /* Configure length */
            EFLASH_SetLengthReg(len);
            /* Configure command */
            EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_PAGEPROGRAM);
            /* Trigger to start */
            EFLASH_TrigCtrlCmdReg();
            /* program data */
            if (!EFLASH_GetStatusReg())
            {
                for (dataCount = 0UL; dataCount < len; )
                {
                    if (EFLASH_GetDataBufferReg())
                    {
                        EFLASH_SetDataReg(buffer[dataCount]);
                        dataCount++;
                    }
                }
                /* Check whether the command is finished */
                while (EFLASH_GetCmdBusyStatus());
            }
            else
            {
                ret = EFLASH_STATUS_ERROR;
            }
        }
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Verify whether the specified area is erased. not support D-Flash verify
 *
 * @param[in] addr: eflash addrees to be verified (should be 4-aligned)
 * @param[in] len: page erase verify length(the unit is word)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_PageEraseVerify(uint32_t addr, uint32_t len)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    /* Check the P-Flash address */
    if ((addr < PFLASH_END_ADDRESS) && (addr >= PFLASH_BASE_ADDRESS))
    {
        /* Wait for last operation to be completed */
        if (EFLASH_GetCmdBusyStatus())
        {
            ret = EFLASH_STATUS_BUSY;
        }
        else
        {
            EFLASH_CLEAR_STAT_ERROR_BITS();
            /* Configure address */
            EFLASH_SetStartAddressReg(addr);
            /* Configure command */
            EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_PAGERASEVERIFY);
            /* Configure length */
            EFLASH_SetLengthReg(len);
            /* Trigger to start */
            ret = FLASH_StartCmdSequence();
        }
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Verify whether all the P-Flash is erased
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_MassEraseVerify(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    /* Wait for last operation to be completed */
    if (EFLASH_GetCmdBusyStatus())
    {
        ret = EFLASH_STATUS_BUSY;
    }
    else
    {
        EFLASH_CLEAR_STAT_ERROR_BITS();
        /* Configure command */
        EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_MASSRASEVERIFY);
        EFLASH_SetSelectReg(0U);
        /* Trigger to start */
        ret = FLASH_StartCmdSequence();
    }

    return ret;
}

/*!
 * @brief Option page erase
 *
 * @param[in] addr: option addrees to erase (should be 4-aligned)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
static EFLASH_StatusType EFLASH_OptionPageErase(uint32_t addr)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    if ((addr < OPTION_END) && (addr >= OPTION_BASE))
    {
        /* Wait for last operation to be completed */
        if (EFLASH_GetCmdBusyStatus())
        {
            ret = EFLASH_STATUS_BUSY;
        }
        else
        {
            EFLASH_CLEAR_STAT_ERROR_BITS();
            /* Configure address */
            EFLASH_SetStartAddressReg(addr);
            /* Configure command */
            EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_PROTECTERASE);
            /* enable option byte operation */
            EFLASH_EnableOptionReg(1U);
            /* Trigger to start */
            ret = FLASH_StartCmdSequence();
        }
        /* disable option byte operation */
        EFLASH_EnableOptionReg(0U);
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Program data to specified option page address
 *
 * @param[in] addr: option addrees (should be 4-aligned)
 * @param[in] buffer: Point to the data bufffer to be programed
 * @param[in] len: Data length to be programmed (the unit is word)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
static EFLASH_StatusType EFLASH_OptionPageProgram(uint32_t addr, uint32_t *buffer, uint32_t len)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint32_t dataCount = 0x0UL;

    DEVICE_ASSERT(NULL != dataBuffer);
    if ((addr < OPTION_END) || (addr >= OPTION_BASE))
    {
        /* Wait for last operation to be completed */
        if (EFLASH_GetCmdBusyStatus())
        {
            ret = EFLASH_STATUS_BUSY;
        }
        else
        {
            EFLASH_CLEAR_STAT_ERROR_BITS();
            /* Configure address */
            EFLASH_SetStartAddressReg(addr);
            /* Configure length */
            EFLASH_SetLengthReg(len);
            /* Configure command */
            EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_PROTECTROGRAM);
            /* enable option byte operation */
            EFLASH_EnableOptionReg(1U);
            /* Trigger to start */
            EFLASH_TrigCtrlCmdReg();

            /* program data */
            for (dataCount = 0UL; dataCount < len;)
            {
                if (EFLASH_GetDataBufferReg())
                {
                    EFLASH_SetDataReg(buffer[dataCount]);
                    EFLASH_SetDataMaskReg(0xFFU);
                    dataCount++;
                }
            }
            /* Check whether the command is finished */
            while (EFLASH_GetCmdBusyStatus());
            if (EFLASH_GetStatusReg())
            {
                ret = EFLASH_STATUS_ERROR;
            }
        }
        /* disable option byte operation */
        EFLASH_EnableOptionReg(0U);
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Set P-Flash write protection
 *
 * @param[in] protectStatus: write protect status value, this parameter pointer
 *            to the start address of the defined space of two words
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_SetWriteProtection(uint32_t *protectStatus)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint32_t optBuffer[PFLASH_WRITE_PROTECT_NUM] = {0U};

    DEVICE_ASSERT(NULL != protectStatus);;

    EFLASH_CLEAR_STAT_ERROR_BITS();
    ret = EFLASH_OptionPageErase(PFLASH_WRITE_PROTECT_BASE);
    if (EFLASH_STATUS_SUCCESS == ret)
    {
        optBuffer[0] = protectStatus[0] & 0xFFFFUL;
        optBuffer[1] = (protectStatus[0] >> 16U) & 0xFFFFU;
        optBuffer[2] = protectStatus[1] & 0xFFFFU;
        optBuffer[3] = (protectStatus[1] >> 16U) & 0xFFFFU;
        ret = EFLASH_OptionPageProgram(PFLASH_WRITE_PROTECT_BASE, optBuffer, PFLASH_WRITE_PROTECT_NUM);
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Enable eflash read protection
 *
 * This API is used to set(enable) eflash read protection. Set(enable) read
 * protection will take effect after reset and the all EFLASH main memory(user
 * pages include D-flash) will return with 0x0 when read by JTAG/SWD. EFLASH main memory
 * can not be erased or programed by code/JTAG/SWD when in read protection status.
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_EnableReadProtection(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint32_t optBuffer[PFLASH_READ_PROTECT_NUM] = {0U};

    EFLASH_CLEAR_STAT_ERROR_BITS();
    ret = EFLASH_OptionPageErase(OPTION_BASE);
    if (EFLASH_STATUS_SUCCESS != ret)
    {
        ret = EFLASH_STATUS_ERROR;
    }
    else
    {
        optBuffer[0] = 0x5AA55AA5UL;
        ret = EFLASH_OptionPageProgram(OPTION_BASE, optBuffer, PFLASH_READ_PROTECT_NUM);
    }

    return ret;
}

/*!
 * @brief Disable EFLASH read protection
 *
 * This API is used to clear(disable) EFLASH read protection. Clear(disable) read
 * protection will take effect after reset and the EFLASH main memory(include D-Flash)
 * will be erased to 0xFFFFFFFF automatically.
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DisableReadProtection(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint32_t optBuffer[PFLASH_READ_PROTECT_NUM] = {0UL};

    EFLASH_CLEAR_STAT_ERROR_BITS();
    ret = EFLASH_OptionPageErase(OPTION_BASE);
    if (EFLASH_STATUS_SUCCESS != ret)
    {
        ret = EFLASH_STATUS_ERROR;
    }
    else
    {
        optBuffer[0] = 0xFFFFFFACUL;
        ret = EFLASH_OptionPageProgram(OPTION_BASE, optBuffer, PFLASH_READ_PROTECT_NUM);
    }

    return ret;
}

/*!
 * @brief Block erase specified D-Flash user area
 *
 * @param[in] addr: D-flash addrees to be erased (should be 4-aligned)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFBlockErase(uint32_t addr)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    /* check D-flash address */
    if ((addr < DFLASH_END_ADDRESS) || (addr >= DFLASH_BASE_ADDRESS))
    {
        /* Wait for last operation to be completed */
        if (EFLASH_GetCmdBusyStatus())
        {
            ret = EFLASH_STATUS_BUSY;
        }
        else
        {
            EFLASH_CLEAR_STAT_ERROR_BITS();
            /* Configure address */
            EFLASH_SetStartAddressReg(addr);
            /* Configure command */
            EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_BLOCKERASE);
            /* Trigger to start */
            ret = FLASH_StartCmdSequence();
        }
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

/*!
 * @brief Mass erase D-Flash user area
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFMassErase(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    /* Wait for last operation to be completed */
    if (EFLASH_GetCmdBusyStatus())
    {
        ret = EFLASH_STATUS_BUSY;
    }
    else
    {
        EFLASH_CLEAR_STAT_ERROR_BITS();
        /* Configure command */
        EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_MASSRASE);
        /* Select operation in D-Flash */
        EFLASH_SetSelectReg(1U);
        /* Configure address */
        EFLASH_SetStartAddressReg(0x08020000UL);
        /* Trigger to start */
        ret = FLASH_StartCmdSequence();
    }

    return ret;
}

/*!
 * @brief Verify whether the mass erase operation is performed in D-Flash successfully
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFMassEraseVerify(void)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;

    /* Wait for last operation to be completed */
    if (EFLASH_GetCmdBusyStatus())
    {
        ret = EFLASH_STATUS_BUSY;
    }
    else
    {
        EFLASH_CLEAR_STAT_ERROR_BITS();
        /* Configure command */
        EFLASH_SetCommandReg((uint32_t)EFLASH_CMD_MASSRASEVERIFY);
        /* Select operation in D-Flash */
        EFLASH_SetSelectReg(1U);
        /* Trigger to start */
        ret = FLASH_StartCmdSequence();
    }

    return ret;
}

/*!
 * @brief Set D-Flash write protection
 *
 * @param[in] protectStatus: write protect status value, this parameter pointer
 *            to the start address of the defined space of 1 word
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFSetWriteProtection(uint32_t *protectStatus)
{
    EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
    uint32_t optBuffer[DFLASH_WRITE_PROTECT_NUM] = {0U};

    EFLASH_CLEAR_STAT_ERROR_BITS();
    ret = EFLASH_OptionPageErase(DFLASH_WRITE_PROTECT_BASE);
    if (EFLASH_STATUS_SUCCESS == ret)
    {
        optBuffer[0] = (*protectStatus) & 0xFFFFU;
        optBuffer[1] = ((*protectStatus) >> 16U) & 0xFFFFU;
        ret = EFLASH_OptionPageProgram(DFLASH_WRITE_PROTECT_BASE, optBuffer, DFLASH_WRITE_PROTECT_NUM);
    }
    else
    {
        ret = EFLASH_STATUS_ERROR;
    }

    return ret;
}

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