/*!
 * @file        g32a10xx_int.c
 *
 * @brief       Main Interrupt Service Routines
 *
 * @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.
 */

#include "g32a10xx_int.h"
#include "user_tmr_config.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_PWMInput
  @{
*/

/** @defgroup TMR_PWMInput_INT_Functions INT_Functions
  @{
  */


/*!
 * @brief   This function handles Hard Fault exception
 *
 * @param   None
 *
 * @retval  None
 *
 */
void HardFault_Handler(void)
{
}	

/*!
 * @brief   This function handles Memory Manage exception
 *
 * @param   None
 *
 * @retval  None
 *
 */
void MemManage_Handler(void)
{
}

/*!
 * @brief   This function handles Bus Fault exception
 *
 * @param   None
 *
 * @retval  None
 *
 */
void BusFault_Handler(void)
{
}

/*!
 * @brief   This function handles Usage Fault exception
 *
 * @param   None
 *
 * @retval  None
 *
 */
void UsageFault_Handler(void)
{
}

/*!
 * @brief   This function handles SVCall exception
 *
 * @param   None
 *
 * @retval  None
 *
 */
void SVC_Handler(void)
{
}

/*!
 * @brief   This function handles Debug Monitor exception
 *
 * @param   None
 *
 * @retval  None
 *
 */
void DebugMon_Handler(void)
{
}

/*!
 * @brief   This function handles PendSV_Handler exception
 *
 * @param   None
 *
 * @retval  None
 *
 */
void PendSV_Handler(void)
{
}

/*!
 * @brief   This function handles SysTick Handler
 *
 * @param   None
 *
 * @retval  None
 *
 */
void SysTick_Handler(void)
{
    tick = tick + (uint32_t)1;
}

/*!
 * @brief   This function handles TMR1 Update Handler
 *
 * @param   None
 *
 * @retval  None
 *
 */
void TMR1_BRK_UP_TRG_COM_IRQHandler(void)
{
    if (Tmr_ReadIntFlag(TMR1, TMR_INT_FLAG_UPDATE) == (uint16_t)SET)
    {
        /* The interrupt flag bit must be cleared first. */
        Tmr_ClearIntFlag(TMR1, (uint16_t)TMR_INT_UPDATE);
        tick = tick + (uint32_t)1;
    }
    else
    {
        /* nothing */
    }
}

/*!
 * @brief   This function handles TMR3 Handler
 *
 * @param   None
 *
 * @retval  None
 *
 */
void TMR3_IRQHandler(void)
{
    uint32_t loc_ic1;
    uint32_t loc_ic2;
    uint32_t loc_hclk;
    uint32_t loc_ovf;

    if (Tmr_ReadIntFlag(TMR3, TMR_INT_FLAG_UPDATE) == (uint16_t)SET)
    {
        Tmr_ClearIntFlag(TMR3, (uint16_t)TMR_INT_UPDATE);
        g_overflowCNT = g_overflowCNT + (uint32_t)1u;
    }
    else
    {
        /* nothing */
    }

    if (Tmr_ReadIntFlag(TMR3, TMR_INT_FLAG_CH1) == (uint16_t)SET)
    {
        Tmr_ClearIntFlag(TMR3, (uint16_t)TMR_INT_CH1);
        g_ic1Value = Tmr_ReadCaputer1(TMR3);
        
        loc_ovf = g_overflowCNT;
        if (loc_ovf > (uint32_t)0u)
        {
            g_ic1Value += (loc_ovf - (uint32_t)1u) * (uint32_t)65536u;
        }
        else
        {
            /* nothing */
        }
    }
    else
    {
        /* nothing */
    }

    if (Tmr_ReadIntFlag(TMR3, TMR_INT_FLAG_CH2) == (uint16_t)SET)
    {
        Tmr_ClearIntFlag(TMR3, (uint16_t)TMR_INT_CH2);
        g_ic2Value = Tmr_ReadCaputer2(TMR3);
        
        loc_ovf = g_overflowCNT;
        if (loc_ovf > (uint32_t)0u)
        {
            g_ic2Value += (loc_ovf - (uint32_t)1u) * (uint32_t)65536u;
        }
        else
        {
            /* nothing */
        }

        loc_ic1  = g_ic1Value;
        loc_ic2  = g_ic2Value;
        loc_hclk = g_hclk;

        if (loc_ic2 != (uint32_t)0u)
        {
            g_dutyCycle = ((loc_ic1 * (uint32_t)100u) + (loc_ic2 / (uint32_t)2u)) / loc_ic2;
            g_frequency = (loc_hclk + (loc_ic2 / (uint32_t)2u)) / loc_ic2;
            
            g_highLev = loc_ic1 / (loc_hclk / (uint32_t)1000000UL);
            g_lowLev  = (loc_ic2 - loc_ic1) / (loc_hclk / (uint32_t)1000000UL);
            g_peroid  = loc_ic2 / (loc_hclk / (uint32_t)1000000UL);
        }
        else
        {
            g_dutyCycle = 0u;
            g_frequency = 0u;
            g_highLev   = 0u;
            g_lowLev    = 0u;
            g_peroid    = 0u;
        }
        g_irqFlag = g_irqFlag + 1u;
        g_overflowCNT = 0;
    }
    else
    {
        /* nothing */
    }
}

/**@} end of group TMR_PWMInput_INT_Functions */
/**@} end of group TMR_PWMInput */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
