/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2026 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "H_STM32F4_I2C_1.h"
#include "H_STM32F4_SPI_1_2.h"
#include "XPT2046_TP_Access.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
   while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  //---------------------------------
  // I2C1応答タイムアウト
  //---------------------------------
  if(GLB_structParam_Handle_I2C1.int16_vTimerI2C > 0){
    GLB_structParam_Handle_I2C1.int16_vTimerI2C--;
  }


  //----------------------------------------------------------------
  // SPI1 Slave Receive End Timer
  //----------------------------------------------------------------
  if(GLB_structParam_Handle_SPI1.int16_vSPI_ReceiveEnd > 0){
    GLB_structParam_Handle_SPI1.int16_vSPI_ReceiveEnd--;
  }
  //----------------------------------------------------------------

  //----------------------------------------------------------------
  // SPI1 Timeout
  //----------------------------------------------------------------
  if(GLB_structParam_Handle_SPI1.uint16_vTimerSPI > 0){
    GLB_structParam_Handle_SPI1.uint16_vTimerSPI--;
  }
  //----------------------------------------------------------------


  //----------------------------------------------------------------
  // SPI2 Slave Receive End Timer
  //----------------------------------------------------------------
  if(GLB_structParam_Handle_SPI2.int16_vSPI_ReceiveEnd > 0){
    GLB_structParam_Handle_SPI2.int16_vSPI_ReceiveEnd--;
  }
  //----------------------------------------------------------------

  //----------------------------------------------------------------
  // SPI2 Timeout
  //----------------------------------------------------------------
  if(GLB_structParam_Handle_SPI2.uint16_vTimerSPI > 0){
    GLB_structParam_Handle_SPI2.uint16_vTimerSPI--;
  }
  //----------------------------------------------------------------


  //---------------------------------------------
  // タッチパネル監視タイマ
  //---------------------------------------------
  if(GLB_structParam_Position_TP.int16_vTimer_TP_Interval > 0){
    GLB_structParam_Position_TP.int16_vTimer_TP_Interval--;
  }
  //---------------------------------------------

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f4xx.s).                    */
/******************************************************************************/

/* USER CODE BEGIN 1 */

// I2C event interrupt request
void I2C1_EV_IRQHandler(void)
{
  HAL_I2C_EV_IRQHandler(&Handle_I2C1);
}


// I2C1 error interrupt request.
void I2C1_ER_IRQHandler(void)
{
  HAL_I2C_ER_IRQHandler(&Handle_I2C1);
}



// I2C Tx Transfer completed callback.
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *pHandle_I2C)
{
  if(pHandle_I2C->Instance == I2C1){
    GLB_structParam_Handle_I2C1.uint16_vI2C_TxIdx
        = GLB_structParam_Handle_I2C1.uint16_vI2C_TxNumber;
  }
}


// I2C Rx Transfer completed callback.
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *pHandle_I2C)
{
  if(pHandle_I2C->Instance == I2C1){
    GLB_structParam_Handle_I2C1.uint16_vI2C_RxIdx
        = GLB_structParam_Handle_I2C1.uint16_vI2C_RxNumber;
  }
}



// I2C error callbacks.
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *pHandle_I2C)
{
}



//--------------------------------------------------------------
// This function handles DMA Transfer Complete interrupt request.
//--------------------------------------------------------------
// SPI1 TX送信
void DMA2_Stream3_IRQHandler(void)
{
  HAL_DMA_IRQHandler(Handle_SPI1.hdmatx);
}


// SPI1 RX受信
void DMA2_Stream2_IRQHandler(void)
{
  HAL_DMA_IRQHandler(Handle_SPI1.hdmarx);
}


//--------------------------------------------------------------
// This function handles DMA Transfer Complete interrupt request.
//--------------------------------------------------------------
// SPI2 TX送信
void DMA1_Stream4_IRQHandler(void)
{
	HAL_DMA_IRQHandler(Handle_SPI2.hdmatx);
}


// SPI2 RX受信
void DMA1_Stream3_IRQHandler(void)
{
	HAL_DMA_IRQHandler(Handle_SPI2.hdmarx);
}


/**
  * @brief  TxRx Transfer completed callback.
  * @param  hspi: SPI handle.
  * @note   This example shows
  *         a simple way to report end of DMA TxRx transfer,
  *         and you can add your own implementation.
  * @retval None
  */
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
  if(hspi->Instance == SPI1)
  {
    //----------------------------------------------------------
    // 送信数セット : 送信終了
    //----------------------------------------------------------
    GLB_structParam_Handle_SPI1.int16_vSPI_TxIdx
            = GLB_structParam_Handle_SPI1.int16_vSPI_TxNumber;

    //----------------------------------------------------------
    // 受信数セット : 受信終了
    //----------------------------------------------------------
    GLB_structParam_Handle_SPI1.int16_vSPI_RxIdx
            = GLB_structParam_Handle_SPI1.int16_vSPI_RxNumber;
  }
  else if(hspi->Instance == SPI2)
  {
    //----------------------------------------------------------
    // 送信数セット : 送信終了
    //----------------------------------------------------------
    GLB_structParam_Handle_SPI2.int16_vSPI_TxIdx
            = GLB_structParam_Handle_SPI2.int16_vSPI_TxNumber;

    //----------------------------------------------------------
    // 受信数セット : 受信終了
    //----------------------------------------------------------
    GLB_structParam_Handle_SPI2.int16_vSPI_RxIdx
            = GLB_structParam_Handle_SPI2.int16_vSPI_RxNumber;
  }
}



/**
  * @brief  SPI error callbacks.
  * @param  hspi: SPI handle
  * @note   This example shows
  *         a simple way to report transfer error,
  *         and you can add your own implementation.
  * @retval None
  */
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
{
  // Transfer error in reception/transmission process
  if(hspi->Instance == SPI1)
  {

  }
  else if(hspi->Instance == SPI2)
  {

  }
}

/* USER CODE END 1 */
