/*!
 * @file        bsp_flash.c
 *
 * @brief       Include IAP flash operation
 *
 * @version     V1.0.0
 *
 * @date        2026-02-26
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "bsp_flash.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup IAP_Examples
  @{
*/

/** @addtogroup IAP_BootLoader
  @{
  */

/** @defgroup IAP_BootLoader_Functions Functions
  @{
*/

/*!
 * @brief       Unlocks Flash for write access
 *
 * @param       None
 *
 * @retval      None
 *
 */
void Flash_IapInit(void)
{
    /* Unlock Flash for Clear or Write*/
    Fmc_Unlock();
    /* Clear all FMC flags */
    Fmc_ClearStatusFlag(FMC_FLAG_BUSY);
    Fmc_ClearStatusFlag(FMC_FLAG_PE);
    Fmc_ClearStatusFlag(FMC_FLAG_WPE);
    Fmc_ClearStatusFlag(FMC_FLAG_OC);
}

/*!
 * @brief       Erase of all user flash area
 *
 * @param       Application : APP1 or APP2
 *
 * @retval      SUCCESS: user flash area successfully erased
 *              ERROR: error occurred
 */
uint32_t Flash_IapErase(App_DefType Application)
{
    uint32_t address = 0U;
    uint32_t addressEnd = 0U;
    uint32_t retVal = (uint32_t)SUCCESS;

    if (Application == APP1)
    {
        address = USER_APP1_START_ADDRESS;
    }
    else
    {
        address = USER_APP2_START_ADDRESS;
    }

    if (Application == APP1)
    {
        addressEnd = USER_APP1_END_ADDRESS;
    }
    else
    {
        addressEnd = USER_APP2_END_ADDRESS;
    }

    while (address <= (uint32_t) addressEnd)
    {
        if (Fmc_ErasePage(address) != FMC_STATE_COMPLETE)
        {
            /* Error occurred while page erase */
            retVal = (uint32_t)ERROR;
            break;
        }
        else
        {
            address =address + FLASH_PAGE_SIZE;
        }
    }

    return retVal;
}

/*!
 * @brief       Write the appoint data buffer in flash
 *
 * @param       AddressPtr: start address for writing data buffer
 *
 * @param       DataPtr: pointer on data buffer
 *
 * @param       Length: length of data buffer (unit is 8-bit)
 *
 * @param       Application : APP1 or APP2
 *
 * @retval      SUCCESS: data successfully written to Flash memory
 *              ERROR: Error occurred while writing data in Flash memory
 *
 * @note        buffer data are 32-bit aligned
 */
uint32_t Flash_IapWrite(__IO uint32_t* AddressPtr, const uint32_t* DataPtr, uint16_t Length, App_DefType Application)
{
    uint32_t i = 0U;
    uint32_t addressEnd = 0U;
    uint32_t retVal = (uint32_t)SUCCESS;
    uint16_t lengthTemp = Length;
    uint8_t breakCheck = 0U;

    if (Application == APP1)
    {
        addressEnd = USER_APP1_END_ADDRESS - 8U;
    }
    else
    {
        addressEnd = USER_APP2_END_ADDRESS - 8U;
    }

    /* Write 8 bytes one time */
    lengthTemp = (lengthTemp + 7U) / 8U;

    for (i = 0; i < lengthTemp; i++)
    {
        if (*AddressPtr > addressEnd)
        {
            retVal = (uint32_t)ERROR;
            breakCheck = 1U;
        }
        else
        {
            /* The operation will be done by word and Check the written value*/
            if ((Fmc_ProgramWord(*AddressPtr, (uint32_t)FMC_LEN_64BIT, (uint32_t*)((uint32_t)DataPtr + (i * 8U))) != FMC_STATE_COMPLETE) \
                || (*(uint32_t*)*AddressPtr != *(uint32_t*)((uint32_t)DataPtr + (i * 8U))) 
                || (*(uint32_t*)(*AddressPtr + 4U) != *(uint32_t*)((uint32_t)DataPtr + (i * 8U) + 4U)))
            {
                retVal = (uint32_t)ERROR;
                breakCheck = 1U;
            }
            else
            {
                *AddressPtr += 8U;
            }
        }

        if(1U == breakCheck)
        {
            break;
        }
        else
        {
            /* do nothing */
        }
    }

    return retVal;
}

/**@} end of group IAP_BootLoader_Functions */
/**@} end of group IAP_BootLoader */
/**@} end of group IAP_Examples */
/**@} end of group Examples */
