/*!
 * @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 "stdio.h"
#include "inttypes.h"
#include "Board.h"
#include "user_sms_config.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup SMS_Examples
  @{
  */

/** @addtogroup Sms_Interrupt
  @{
  */

/** @defgroup Sms_Interrupt_Macros Macros
  @{
  */

/* printf function configs to USART1 */
#define DEBUG_USART                    USART1

/**@} end of group Sms_Interrupt_Macros */

/** @defgroup Sms_Interrupt_Functions Functions
  @{
  */
void SERM_IRQHandler(void);

static void Sms_BitsFaultIsr(void)
{
    uint32_t regVal = 0U;
    uint8_t circleCnt = 0U;
    uint32_t errorAddr = 0U;
    
    /* Clear the state flgs */
    Sms_ReadStsFlag();

    regVal = Sms_ReadIntSts();

    if ((uint32_t)0U != regVal)
    {
        while(circleCnt < SMS_FAULTS_TOTAL_COUNT)
        {
            if (0U != (regVal & ((uint32_t)1U << Sms_GetErrorBitPosition(circleCnt))))
            {
                errorAddr = Sms_GetErrorAddr();
                /* The fault id:
                 *    0: RD_ERR_SIN_ID
                 *    1: RD_ERR_DBL_ID
                 *    2: RMW_ERR_SIN_ID
                 *    3: RMW_ERR_DBL_ID
                 */
                printf("Sms error notification,FaultID:%d, FaultAddr:0x%x\r\n", 
                        circleCnt, (unsigned int)errorAddr);
            }
            else
            {
                /* do nothing */
            }

            circleCnt += 1U;
        }

        /* Clear the error flag */
        Sms_WriteIntRem(0U);
    }
    else
    {
        /* do nothing */
    }
}

int main(void)
{
    /* COM1 init*/
    Board_ComInit(COM1);

    Sms_Init(&g_smsConfig);
    
    /* Disable all the interrupts mask, and enable all the error interrupt */
    Sms_DisableIntMask(SMS_INTRMn_RD_ERR_DET_MASK);
    Sms_DisableIntMask(SMS_INTRMn_RD_ERR_SIN_MASK);
    Sms_DisableIntMask(SMS_INTRMn_RD_ERR_DBL_MASK);
    Sms_DisableIntMask(SMS_INTRMn_RMW_ERR_DET_MASK);
    Sms_DisableIntMask(SMS_INTRMn_RMW_ERR_SIN_MASK);
    Sms_DisableIntMask(SMS_INTRMn_RMW_ERR_DBL_MASK);

    NVIC_SetPriority(SMS_IRQn, 0x0f);
    NVIC_EnableIRQ(SMS_IRQn);

    Sms_InjectFault(SMS_INJECT_SINGLE_BIT_ERROR);
    Sms_InjectFault(SMS_INJECT_DOUBLE_BIT_ERROR);
    Sms_InjectFault(SMS_INJECT_TRIPLE_BIT_ERROR);

    while(1)
    {

    }
}


/*!
* @brief          This function handles SMS exception
*
* @param[in]      None
* @param[out]     None
* @param[in,out]  None
*/
void SERM_IRQHandler(void)
{
    Sms_BitsFaultIsr();
}

#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) == (uint8_t)RESET)
    {
        /* 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 Sms_Interrupt_Functions */
/**@} end of group Sms_Interrupt */
/**@} end of group SMS_Examples */
/**@} end of group Examples */
