/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @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 "Board.h"
#include "stdio.h"
#include "inttypes.h"
#include "flash_eeprom.h"
#include "user_fls_config.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup FLASH_Examples
  @{
*/

/** @addtogroup FMC_Flash_EEPROM
  @{
  */

/** @defgroup FMC_Flash_EEPROM_Macros Macros
  @{
  */

/**@} end of group FMC_Flash_EEPROM_Macros */

/** @defgroup FMC_Flash_EEPROM_Variables Variables
  @{
*/

/* read and write buffer */
static uint32_t g_writeBuffer[BUFFER_SIZE] = {0};
static uint32_t g_readBuffer[BUFFER_SIZE] = {0};

/**@} end of group FMC_Flash_EEPROM_Variables */

/** @defgroup FMC_Flash_EEPROM_Functions Functions
  @{
*/

static void Print_Buffer(const uint32_t *PBuffer, uint32_t BufferLength);
static BOOL BufferCompare(const uint32_t *Buff1, const uint32_t *Buff2, uint32_t Size);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    uint32_t i, VirtAddress;

    Board_ComInit(COM1);

    /* Init LED2 */
    Board_LedInit(LED2);

    /* fill g_writeBuffer */
    for (i = 0U; i < BUFFER_SIZE; i++)
    {
        g_writeBuffer[i] = i + 0x100U;
    }

    /* Print write data */
    printf("\r\nwrite data:\r\n");
    Print_Buffer(g_writeBuffer, BUFFER_SIZE);

    /* Flash EEPROM Init */
    Flash_EE_Init();

    /* Write the variable data and corresponding virtual address to Flash EEPROM*/
    /* Virtual address value must be between 0 ~ NUMBER_OF_VARIABLES */
    for(i = 0U; i < BUFFER_SIZE; i++)
    {
        VirtAddress = i;
        Flash_EE_WriteData(VirtAddress, g_writeBuffer[i]);
    }

    /* Read the variable data corresponding to the virtual address */
    for(i = 0; i < BUFFER_SIZE; i++)
    {
        VirtAddress = i;
        Flash_EE_ReadData(VirtAddress, &g_readBuffer[i]);
    }

    /* Print read data */
    printf("\r\nread data:\r\n");
    Print_Buffer(g_readBuffer, BUFFER_SIZE);

    /* Compare the values of two buffers for equality */
    if(BufferCompare(g_writeBuffer, g_readBuffer, BUFFER_SIZE) == TRUE)
    {
        Board_LedOn(LED2);
        printf("\r\nFlash EEPROM test successful!\r\n");
    }
    else
    {
        Board_LedOff(LED2);
        printf("\r\nFlash EEPROM test fail!\r\n");
    }

    while (1)
    {
    }
}

/*!
 * @brief       Print Buffer Data
 *
 * @param       pBuffer: buffer
 *
 * @param       length : length of the Buffer
 *
 * @retval      None
 */
static void Print_Buffer(const uint32_t *PBuffer, uint32_t BufferLength)
{
    uint32_t i = 0U;

    for (i = 0U; i  < BufferLength; i++)
    {
        printf("0x%08x ", (unsigned int)PBuffer[i]);

        if (((i + 1U) % 8U) == 0U)
        {
            printf("\r\n");
        }
        else
        {
            /* do nothing */
        }
    }
}

/*!
 * @brief       Compares two buffers
 *
 * @param       buf1:    First buffer to be compared
 *
 * @param       buf1:    Second buffer to be compared
 *
 * @param       size:    Buffer size
 *
 * @retval      Return TRUE if buf1 = buf2. If not then return FALSE
 */
static BOOL BufferCompare(const uint32_t *Buff1, const uint32_t *Buff2, uint32_t Size)
{
    uint32_t i = 0;
    BOOL returnVal = TRUE;

    for (i = 0; i < Size; i++)
    {
        if (Buff1[i] != Buff2[i])
        {
            returnVal = FALSE;
			break;
        }
        else
        {
            /* do nothing */
        }
    }

    return returnVal;
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == 0U)
    {
        /* do nothing */
    }

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#pragma message("Not supported compiler type")
#endif

/**@} end of group FMC_Flash_EEPROM_Functions */
/**@} end of group FMC_Flash_EEPROM */
/**@} end of group FLASH_Examples */
/**@} end of group Examples */
