/*!
 * @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.
 */

/* Includes */
#include "g32a10xx_int.h"
#include "user_tmr_config.h"
#include "user_tmr_config.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_6Steps
  @{
*/

/** @defgroup TMR_6Steps_INT_Variables INT_Variables
  @{
*/

/* The count of microseconds */
volatile uint32_t g_cntUs = 0;
/* The count of milliseconds */
volatile uint32_t g_cntMs = 0;
/* public variables */
static uint8_t g_pwmStep = 0;

/**@} end of group TMR_6Steps_INT_Variables */

/** @defgroup TMR_6Steps_INT_Functions INT_Functions
  @{
*/



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

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

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

/*!
 * @brief        This function handles SysTick Handler
 *
 * @param        None
 *
 * @retval       None
 *
 * @note
 */
void SysTick_Handler(void)
{
    if ((uint32_t)0u != g_cntMs)
    {
        g_cntMs = g_cntMs - (uint32_t)1u;
    }
    else
    {
        /* nothing */
    }

    if ((uint32_t)0u != g_cntUs)
    {
        g_cntUs = g_cntUs - (uint32_t)1u;
    }
    else
    {
        /* nothing */
    }
}

/*!
 * @brief       TMR1 Trigger and Commutation IRQ Handler
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_BRK_UP_TRG_COM_IRQHandler(void)
{
    /* The interrupt flag bit must be cleared first. */
    /* Clear the TMR1 COM pending bit */
    Tmr_ClearIntFlag(TMR1, (uint16_t)TMR_INT_CCU);

    g_pwmStep++;

    switch (g_pwmStep)
    {
        case 1:
            /* configuration: Channel1 and Channel2 PWM1 Mode */
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_1, TMR_OC_MODE_PWM1);
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_2, TMR_OC_MODE_PWM1);

            /* configuration: Channel1 */
            Tmr_EnableCCxChannel(TMR1, TMR_CHANNEL_1);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_1);

            /* configuration: Channel2 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_2);
            Tmr_EnableCCxNChannel(TMR1, TMR_CHANNEL_2);

            /* configuration: Channel3 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_3);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_3);
            break;

        case 2:
            /* configuration: Channel2 and Channel3 PWM1 Mode */
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_2, TMR_OC_MODE_PWM1);
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_3, TMR_OC_MODE_PWM1);

            /* configuration: Channel1 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_1);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_1);

            /* configuration: Channel2 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_2);
            Tmr_EnableCCxNChannel(TMR1, TMR_CHANNEL_2);

            /* configuration: Channel3 */
            Tmr_EnableCCxChannel(TMR1, TMR_CHANNEL_3);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_3);
            break;

        case 3:
            /* configuration: Channel3 and Channel1 PWM1 Mode */
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_3, TMR_OC_MODE_PWM1);
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_1, TMR_OC_MODE_PWM1);

            /* configuration: Channel1 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_1);
            Tmr_EnableCCxNChannel(TMR1, TMR_CHANNEL_1);

            /* configuration: Channel2 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_2);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_2);

            /* configuration: Channel3 */
            Tmr_EnableCCxChannel(TMR1, TMR_CHANNEL_3);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_3);
            break;

        case 4:
            /* configuration: Channel1 and Channel2 PWM1 Mode */
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_1, TMR_OC_MODE_PWM1);
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_2, TMR_OC_MODE_PWM1);

            /* configuration: Channel1 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_1);
            Tmr_EnableCCxNChannel(TMR1, TMR_CHANNEL_1);

            /* configuration: Channel2 */
            Tmr_EnableCCxChannel(TMR1, TMR_CHANNEL_2);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_2);

            /* configuration: Channel3 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_3);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_3);
            break;

        case 5:
            /* configuration: Channel2 and Channel3 PWM1 Mode */
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_2, TMR_OC_MODE_PWM1);
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_3, TMR_OC_MODE_PWM1);

            /* configuration: Channel1 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_1);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_1);

            /* configuration: Channel2 */
            Tmr_EnableCCxChannel(TMR1, TMR_CHANNEL_2);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_2);

            /* configuration: Channel3 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_3);
            Tmr_EnableCCxNChannel(TMR1, TMR_CHANNEL_3);
            break;

        case 6:
            /* configuration: Channel3 and Channel1 PWM1 Mode */
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_3, TMR_OC_MODE_PWM1);
            Tmr_SelectOCxMode(TMR1, TMR_CHANNEL_1, TMR_OC_MODE_PWM1);

            /* configuration: Channel1 */
            Tmr_EnableCCxChannel(TMR1, TMR_CHANNEL_1);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_1);

            /* configuration: Channel2 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_2);
            Tmr_DisableCCxNChannel(TMR1, TMR_CHANNEL_2);

            /* configuration: Channel3 */
            Tmr_DisableCCxChannel(TMR1, TMR_CHANNEL_3);
            Tmr_EnableCCxNChannel(TMR1, TMR_CHANNEL_3);

            g_pwmStep = 0;
            break;

        default:
            g_pwmStep = 0;
            break;
    }
}
/**@} end of group TMR_6Steps_INT_Functions */
/**@} end of group TMR_6Steps */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
