/*!
    \file    gd32f4xx_it.c
    \brief   interrupt service routines

    \version 2022-03-08, V2.0.0, demo for GD32F4xx
*/

/*
    Copyright (c) 2022, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f4xx_it.h"
#include "systick.h"
#include "bsp_os.h"
#include "Usart.h"
#include "device_interrupt.h"
#include "full_bridge.h"
#include "Timer.h"
extern BSP_OS_SEM sem_enet_isr_recv;
/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void NMI_Handler(void)
{
  term_printf("****************** NMI_Handler ********************/\r\n\r\n");
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void HardFault_Handler(void)
{
  term_printf("****************** HardFault_Handler ********************/\r\n\r\n");

    
    beep_enable();

    /* if Hard Fault exception occurs, go to infinite loop */
    while(1){
    }
}

/*!
    \brief      this function handles MemManage exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MemManage_Handler(void)
{
  term_printf("****************** MemManage_Handler ********************/\r\n\r\n");
    /* if Memory Manage exception occurs, go to infinite loop */
    while(1){
    }
}

/*!
    \brief      this function handles BusFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void BusFault_Handler(void)
{
  term_printf("****************** BusFault_Handler ********************/\r\n\r\n");
    /* if Bus Fault exception occurs, go to infinite loop */
    while(1){
    }
}

/*!
    \brief      this function handles UsageFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UsageFault_Handler(void)
{
  term_printf("****************** UsageFault_Handler ********************/\r\n\r\n");
    /* if Usage Fault exception occurs, go to infinite loop */
    while(1){
    }
}

/*!
    \brief      this function handles SVC exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SVC_Handler(void)
{
}

/*!
    \brief      this function handles DebugMon exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DebugMon_Handler(void)
{
}

/*!
    \brief      this function handles PendSV exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
//void PendSV_Handler(void)
//{
//}

/*!
    \brief      this function handles SysTick exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
//void SysTick_Handler(void)
//{
//    delay_decrement();
//}



/*!
    \brief      ENET 中断标志位清除
    \param[in]  none
    \param[out] none
    \retval     none
    \note       参考官方例程
                LH  @2022.05.19
*/
void ENET_IRQHandler(void)
{
    //清除中断标志位
    enet_interrupt_flag_clear(ENET_DMA_INT_FLAG_RS_CLR);
    enet_interrupt_flag_clear(ENET_DMA_INT_FLAG_NI_CLR);
    BSP_OS_SemPost(&sem_enet_isr_recv);
}




/*!
    \brief      USART1 中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.19
*/
void USART1_IRQHandler(void)
{
  u_int8_t c = 0;

  c = usart_data_receive(USART1);//读相应串口数据 
  
  IRQHandler_485_process(c);
  
  usart_interrupt_flag_clear(USART1 , USART_INT_FLAG_RBNE);
}
    

// /*!
//     \brief      USART2 中断处理函数
//     \param[in]  none
//     \param[out] none
//     \retval     none
//     \note       LH  @2022.05.20
// */
// void USART2_IRQHandler(void)
// {
//   u_int8_t c = 0;

//   c = usart_data_receive(USART2);//读相应串口数据 
  
//   IRQHandler_422_process(c);
  
//   usart_interrupt_flag_clear(USART2 , USART_INT_FLAG_RBNE);
// }




/*!
    \brief      EXTI5_9 中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.26
*/
void EXTI5_9_IRQHandler(void)
{

  if(RESET != exti_interrupt_flag_get(EXTI_7))
  {
    ptz_SW_IRQHandler(EXTI_7);
    exti_interrupt_flag_clear(EXTI_7);
  }
  if(RESET != exti_interrupt_flag_get(EXTI_8))
  {
    ptz_SW_IRQHandler(EXTI_8);
    exti_interrupt_flag_clear(EXTI_8);
  }
  if(RESET != exti_interrupt_flag_get(EXTI_9))
  {
    ptz_SW_IRQHandler(EXTI_9);
    exti_interrupt_flag_clear(EXTI_9);
  }
}

/*!
    \brief      EXTI10_15 中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.26
*/
void EXTI10_15_IRQHandler(void)
{
  if(RESET != exti_interrupt_flag_get(EXTI_10))
  {
    exti_interrupt_flag_clear(EXTI_10);
    ptz_H_HALL_IRQHandler(EXTI_10);
    
  }
  if(RESET != exti_interrupt_flag_get(EXTI_11))
  {
    exti_interrupt_flag_clear(EXTI_11);
    ptz_H_HALL_IRQHandler(EXTI_11);
    
  }
  if(RESET != exti_interrupt_flag_get(EXTI_12))
  {
    exti_interrupt_flag_clear(EXTI_12);
    ptz_H_HALL_IRQHandler(EXTI_12);
    
  }
  

  if(RESET != exti_interrupt_flag_get(EXTI_13))
  {
//     hall_irq_process();
    exti_interrupt_flag_clear(EXTI_13);
    ptz_V_HALL_IRQHandler(EXTI_13);
    
  }
  if(RESET != exti_interrupt_flag_get(EXTI_14))
  {
    exti_interrupt_flag_clear(EXTI_14);
    ptz_V_HALL_IRQHandler(EXTI_14);
     
  }
  if(RESET != exti_interrupt_flag_get(EXTI_15))
  {
    exti_interrupt_flag_clear(EXTI_15);
    ptz_V_HALL_IRQHandler(EXTI_15);
    
  }
}




/*!
    \brief      EXTI0 中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.26
*/
void EXTI0_IRQHandler(void)
{
  if(RESET != exti_interrupt_flag_get(EXTI_0))
  {
    ptz_SW_IRQHandler(EXTI_0);
    exti_interrupt_flag_clear(EXTI_0);
  }
  
}



/*!
    \brief      EXTI1 中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.26
*/
void EXTI1_IRQHandler(void)
{
  if(RESET != exti_interrupt_flag_get(EXTI_1))
  {
    ptz_SW_IRQHandler(EXTI_1);
    exti_interrupt_flag_clear(EXTI_1);
  }
}



/*!
    \brief      EXTI2 中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.26
*/
void EXTI2_IRQHandler(void)
{
  if(RESET != exti_interrupt_flag_get(EXTI_2))
  {
    ptz_SW_IRQHandler(EXTI_2);
    exti_interrupt_flag_clear(EXTI_2);
  }
}





#ifdef Full_bridge

/*!
    \brief      定时器2 水平电机中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.26
*/
//void TIMER2_IRQHandler(void)
//{
//  if(RESET != timer_interrupt_flag_get(TIMER2,TIMER_INT_FLAG_UP))
//  {
//    h_bldc_six_step();
//    timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_UP);
//  }
//}




/*!
    \brief      定时器3 垂直电机中断处理函数
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.05.26
*/
//void TIMER3_IRQHandler(void)
//{
//  if(RESET != timer_interrupt_flag_get(TIMER3,TIMER_INT_FLAG_UP))
//  {
//    v_bldc_six_step();
//    timer_interrupt_flag_clear(TIMER3,TIMER_INT_FLAG_UP);
//  }
//}


void TIMER4_IRQHandler(void)
{
  if(RESET != timer_interrupt_flag_get(TIMER4,TIMER_INT_FLAG_UP))
  {
    
    v_bldc_six_step();
    h_bldc_six_step();
    timer_interrupt_flag_clear(TIMER4,TIMER_INT_FLAG_UP);
  }
}

#endif


