/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f1xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  */
/* USER CODE END Header */

#include "main.h"
#include "stm32f1xx_it.h"
#include "ethernetif.h"
#include "uart_trans.h"
#include "config.h"

extern SPI_HandleTypeDef hspi1;
extern TIM_HandleTypeDef htim4;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern DMA_HandleTypeDef hdma_usart2_rx;
extern DMA_HandleTypeDef hdma_usart2_tx;
extern DMA_HandleTypeDef hdma_usart3_rx;
extern DMA_HandleTypeDef hdma_usart3_tx;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;
extern volatile uint8_t g_uart1_rx_probe_byte;

void NMI_Handler(void)
{
  Debug_TrapWithRttHint("NMI_Handler");
}

void HardFault_Handler(void)
{
  Debug_TrapWithRttHint("HardFault_Handler");
}

void MemManage_Handler(void)
{
  Debug_TrapWithRttHint("MemManage_Handler");
}

void BusFault_Handler(void)
{
  Debug_TrapWithRttHint("BusFault_Handler");
}

void UsageFault_Handler(void)
{
  Debug_TrapWithRttHint("UsageFault_Handler");
}

void DebugMon_Handler(void)
{
}

void SysTick_Handler(void)
{
  HAL_IncTick();
}

void DMA1_Channel2_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_usart3_tx);
}

void DMA1_Channel3_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_usart3_rx);
}

void DMA1_Channel4_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_usart1_tx);
}

void DMA1_Channel5_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_usart1_rx);
}

void DMA1_Channel6_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_usart2_rx);
}

void DMA1_Channel7_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_usart2_tx);
}

void TIM4_IRQHandler(void)
{
  HAL_TIM_IRQHandler(&htim4);
}

void SPI1_IRQHandler(void)
{
  HAL_SPI_IRQHandler(&hspi1);
}

void USART1_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart1);
}

void USART2_IRQHandler(void)
{
  if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE))
  {
    __HAL_UART_CLEAR_IDLEFLAG(&huart2);
    uart_trans_idle_handler(UART_CHANNEL_U0);
  }
  HAL_UART_IRQHandler(&huart2);
}

void USART3_IRQHandler(void)
{
  if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE))
  {
    __HAL_UART_CLEAR_IDLEFLAG(&huart3);
    uart_trans_idle_handler(UART_CHANNEL_U1);
  }
  HAL_UART_IRQHandler(&huart3);
}

void EXTI0_IRQHandler(void)
{
  if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_0))
  {
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0);
    ethernetif_set_irq_pending();
  }
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart == &huart2)
  {
    uart_trans_tx_cplt_handler(UART_CHANNEL_U0);
  }
  else if (huart == &huart3)
  {
    uart_trans_tx_cplt_handler(UART_CHANNEL_U1);
  }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart == &huart1)
  {
    config_uart_rx_byte(g_uart1_rx_probe_byte);
    (void)HAL_UART_Receive_IT(&huart1, (uint8_t *)&g_uart1_rx_probe_byte, 1u);
  }
  else if (huart == &huart2)
  {
    uart_trans_rx_cplt_handler(UART_CHANNEL_U0);
  }
  else if (huart == &huart3)
  {
    uart_trans_rx_cplt_handler(UART_CHANNEL_U1);
  }
}

void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart == &huart2)
  {
    uart_trans_rx_half_cplt_handler(UART_CHANNEL_U0);
  }
  else if (huart == &huart3)
  {
    uart_trans_rx_half_cplt_handler(UART_CHANNEL_U1);
  }
}
