/*!
 * @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 "g32a10xx_fls.h"
#include "g32a10xx_iwdt.h"
#include "g32a10xx_misc.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup FLASH_Examples
  @{
*/

/** @addtogroup FMC_Flash_Ecc
  @{
  */

/** @defgroup FMC_Flash_Ecc_INT_Macros INT_Macros 
  @{
*/
    
/* printf function configs to USART1*/
#define DEBUG_USART  USART1


/**@} end of group FMC_Flash_Ecc_INT_Macros */

/** @defgroup FMC_Flash_Ecc_Functions Functions
  @{
  */

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    uint32_t TestErasePageAddr ;
    uint32_t data[2] ;
    uint32_t Addr;
    volatile uint32_t DATA;
    
    /* Init COM */
    Board_ComInit(COM1);
    
    /* Init LED2 */
    Board_LedInit(LED2);

    /* Unlock Flash for Clear or Write*/
    Fmc_Unlock();
    
    /* Set address*/
    TestErasePageAddr = 0x08040000;
    
    /* Set data*/
    data[0] = 0x55555555;
    data[1] = 0xAAAAAAAAU;
    
    /* Clear the ECC double-bit error flag*/
    Fmc_ClearStatusFlag(FMC_FLAG_DBFI);
    
    for(Addr = TestErasePageAddr; Addr < (TestErasePageAddr + (uint32_t)0x200U); Addr += 4U)
    {
        DATA = *(uint32_t*)Addr;
        (void)DATA;
    }
    
        /* Generate ECC errors by resetting using IWDT*/
        if(Fmc_ReadStatusFlag(FMC_FLAG_DBFI) == 0U)
        {
            Iwdt_Enable();
            while(1)
            {
                /* Reset Page data*/
                Fmc_ErasePage(TestErasePageAddr);
                Addr = TestErasePageAddr;
                while(Addr < (TestErasePageAddr + (uint32_t)0x200U))
                {
                    /* Write data in address*/
                    Fmc_ProgramWord(Addr, (uint32_t)FMC_LEN_64BIT, data);
                    Addr += (uint32_t)8U;
                }
            }
        }
       else
        {
            /* Reset Page data*/
            Fmc_ErasePage(TestErasePageAddr);
            
            Fmc_EnableInterrupt((uint32_t)FMC_INT_DBFI);
            Nvic_EnableIrqRequest(FLASH_IRQn, 2);
            
            for(Addr = TestErasePageAddr; Addr < (TestErasePageAddr + (uint32_t)0x200U); Addr += 4U)
            {
                DATA = *(uint32_t*)Addr;
            }
            
            if(Fmc_ReadStatusFlag(FMC_FLAG_DBFI) == 1U)
            {
                Board_LedOff(LED2);
                printf("The 2bits ECC error has not been cleared.\r\n");
            }
            else
            {
                Board_LedOn(LED2);
                printf("The 2bits ECC test successful!\r\n");
            }
        }

    /* Lock Flash*/
    Fmc_Lock();

    for (;;)
    {
    }
}

#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_Ecc_Functions */
/**@} end of group FMC_Flash_Ecc */
/**@} end of group FLASH_Examples */
/**@} end of group Examples */
