/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  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 SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

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

/** @addtogroup Examples
  @{
*/

/** @addtogroup FMC_Program
  @{
*/

/** @defgroup FMC_Program_Macros Macros
  @{
*/

#define FMC_ADDR          (0x7800)
#define FMC_PAGE_SIZE     (1024)

/**@} end of group FMC_Program_Macros */

/** @defgroup FMC_Program_Functions Functions
  @{
*/

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    uint32_t i;
    uint32_t addr = 0;
    uint32_t *p = NULL;
    BOOL state = TRUE;

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_DelayInit(APM_DELAY_MS);

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);


    /* Unlock flash controler  */
    FMC_Unlock();
    /* Erase page  */
    FMC_ErasePage(FMC_ADDR);

    /* Program  */
    addr = FMC_ADDR;
    for(i = 0; i < FMC_PAGE_SIZE / 4; i++)
    {
        FMC_ProgramWord(addr, i);

        addr += 4;
    }
    /* Lock flash controler  */
    FMC_Lock();

    /* Read and verify data  */
    p = (uint32_t *)FMC_ADDR;
    for(i = 0; i < FMC_PAGE_SIZE / 4; i++)
    {
        if(p[i] != i)
        {
            state = FALSE;
            break;
        }
    }

    while(1)
    {
        if(state == TRUE)
        {
            APM_MINI_LEDOn(LED3);
        }
        else
        {
            APM_MINI_LEDToggle(LED2);
            APM_MINI_LEDToggle(LED3);
        }
        APM_Delay(500);
    }
}

/**@} end of group FMC_Program_Functions */
/**@} end of group FMC_Program */
/**@} end of group Examples */
