/*!
 * @file        apm32f00x_int.c
 *
 * @brief       Main Interrupt Service Routines
 *
 * @version     V1.0.0
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  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 SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "apm32f00x_int.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR1_6stepswithTMR2
  @{
*/

/** @defgroup TMR_6Steps_INT_Variables INT_Variables
  @{
*/

uint8_t PWMStep = 0;

/**@} end of group TMR_6Steps_INT_Variables */

/** @defgroup TMR1_6stepswithTMR2_INT_Functions INT_Functions
  @{
*/

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

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

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

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

/*!
 * @brief       This function handles SysTick exception
 *
 * @param       None
 *
 * @retval      None
 */
void SysTick_Handler(void)
{
    APM_DelayIsr();
}

/*!
 * @brief       This function handles TMR1_UT_IRQn exception
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_UT_IRQHandler(void)
{
    /* Clear the TMR1 TRG pending bit */
    TMR1_ClearIntFlag(TMR1, TMR1_INT_TRIGGER);

    PWMStep++;

    switch (PWMStep)
    {
        case 1:
            /* TMR2_CH2:OFF, TMR2_CH3:ON */
            TMR2_DisableCompareCapture(TMR2_CHANNEL_2);
            TMR2_EnableCompareCapture(TMR2_CHANNEL_3);

            /* TMR1_CH1:ON, TMR1_CH2:OFF */
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_1);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_2);

            /* TMR1_CH3:OFF, TMR1_CH4:OFF */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_3);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_4);
            break;

        case 2:
            /* TMR2_CH2:OFF, TMR2_CH3:OFF */
            TMR2_DisableCompareCapture(TMR2_CHANNEL_2);
            TMR2_DisableCompareCapture(TMR2_CHANNEL_3);

            /* TMR1_CH1:ON, TMR1_CH2:ON */
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_1);
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_2);

            /* TMR1_CH3:OFF, TMR1_CH4:OFF */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_3);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_4);
            break;

        case 3:
            /* TMR1_CH1:OFF, TMR1_CH2:ON */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_1);
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_2);

            /* TMR1_CH3:ON, TMR1_CH4:OFF */
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_3);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_4);

            /* TMR2_CH2:OFF, TMR2_CH3:OFF */
            TMR2_DisableCompareCapture(TMR2_CHANNEL_2);
            TMR2_DisableCompareCapture(TMR2_CHANNEL_3);
            break;

        case 4:
            /* TMR1_CH1:OFF, TMR1_CH2:OFF */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_1);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_2);

            /* TMR1_CH3:ON, TMR1_CH4:ON */
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_3);
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_4);

            /* TMR2_CH2:OFF, TMR2_CH3:OFF */
            TMR2_DisableCompareCapture(TMR2_CHANNEL_2);
            TMR2_DisableCompareCapture(TMR2_CHANNEL_3);
            break;

        case 5:
            /* TMR1_CH1:OFF, TMR1_CH2:OFF */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_1);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_2);

            /* TMR1_CH3:OFF, TMR1_CH4:ON */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_3);
            TMR1_EnableCompareCapture(TMR1, TMR1_CHANNEL_4);

            /* TMR2_CH2:ON, TMR2_CH3:OFF */
            TMR2_EnableCompareCapture(TMR2_CHANNEL_2);
            TMR2_DisableCompareCapture(TMR2_CHANNEL_3);
            break;

        case 6:
            /* TMR1_CH1:OFF, TMR1_CH2:OFF */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_1);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_2);

            /* TMR1_CH3:OFF, TMR1_CH4:OFF */
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_3);
            TMR1_DisableCompareCapture(TMR1, TMR1_CHANNEL_4);

            /* TMR2_CH2:ON, TMR2_CH3:ON */
            TMR2_EnableCompareCapture(TMR2_CHANNEL_2);
            TMR2_EnableCompareCapture(TMR2_CHANNEL_3);

            PWMStep = 0;
            break;

        default:
            PWMStep = 0;
            break;
    }
}

/**@} end of group TMR1_6stepswithTMR2_INT_Functions */
/**@} end of group TMR1_6stepswithTMR2 */
/**@} end of group Examples */
