/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 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 "PLSR.h"
#include "tim.h"
#include "usart.h"
#include "modbus.h"
#include "string.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 --------------------------------------------------------*/
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern UART_HandleTypeDef huart1;
/* 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 */

  /* 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).                    */
/******************************************************************************/

/**
  * @brief This function handles TIM2 global interrupt.
  */
void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
	OSIntEnter(); /* 进入中断 */
  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */
  
  	__HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_UPDATE);
	TIM2->CNT = 0;
  	PulseNum ++;  /* 脉冲段数增加 */
	

	if (Options.RunMod == 0) /* 相对模式 */
	{
		/* 如果之后还有脉冲 */
		if(Options.AllPulse - PulseNum > 0)
		{
			PrePulse = NowPulse; /* 保存之前的脉冲段 */
			if (PulseOutput[NowPulse].NextPulse == 0)
			{
				NowPulse = NowPulse + 1; /* 进入下一段脉冲 */
			}
			else
			{
				NowPulse = PulseOutput[NowPulse].NextPulse - 1; /* 进入下一段脉冲 */
			}
			/* 准备加减速 */
			if(PulseOutput[PrePulse].Frequency <= PulseOutput[NowPulse].Frequency)
			{
				Acc = GetAcc(PulseOutput[PrePulse].Frequency,
					PulseOutput[NowPulse].Frequency, Options.AccUpTime); /* 计算加速度 */	
			}
			else
			{
				Acc = GetAcc(PulseOutput[PrePulse].Frequency,
					PulseOutput[NowPulse].Frequency, Options.AccDownTime); /* 计算加速度 */	
			}				
			ArrFlag = 1;	
			SetNextPulse();/* 设置下一次进入中断的脉冲数 */
			NowFrequency = PulseOutput[NowPulse].Frequency; /* 保存当前脉冲的频率 */
			HAL_TIM_Base_Start_IT(&htim3);  /* 启动定时器3和中断 */
			
		}
		
		/* 如果之后没有脉冲 */
		else
		{
	    	PulseCount = 0;
			ArrFlag = 0;
        		HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
		}
	}
	
	
	if (Options.RunMod == 1) /* 绝对模式 */
	{
		/* 如果之后还有脉冲 */
		if (Options.AllPulse - PulseNum > 0)
		{
			if (TIM2->ARR < PulseOutput[PulseOutput[NowPulse].NextPulse].PulseCount) /* 如果已经达到了设定的最大脉冲数 */
			{
				PulseCount = 0;
        			HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
			}
			else
			{
				PrePulse = NowPulse; /* 保存之前的脉冲段 */
				if (PulseOutput[NowPulse].NextPulse == 0)
				{
					NowPulse = NowPulse + 1; /* 进入下一段脉冲 */
				}
				else
				{
					NowPulse = PulseOutput[NowPulse].NextPulse - 1; /* 进入下一段脉冲 */
				}
				/* 准备加减速 */
				if (PulseOutput[PrePulse].Frequency <= PulseOutput[NowPulse].Frequency)
				{
					Acc = GetAcc(PulseOutput[PrePulse].Frequency,
						PulseOutput[NowPulse].Frequency, Options.AccUpTime); /* 计算加速度 */	
				}
				else
				{
					Acc = GetAcc(PulseOutput[PrePulse].Frequency,
						PulseOutput[NowPulse].Frequency, Options.AccDownTime); /* 计算加速度 */	
				}				
				ArrFlag = 1;	
				SetNextPulse();/* 设置下一次进入中断的脉冲数 */
				NowFrequency = PulseOutput[NowPulse].Frequency; /* 保存当前脉冲的频率 */
				HAL_TIM_Base_Start_IT(&htim3);  /* 启动定时器3和中断 */						
			}

		}
		/* 如果之后没有脉冲 */
		else
		{
	    	PulseCount = 0;
			ArrFlag = 0;
        		HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
		}
	}
	OSIntExit(); /* 退出中断 */
  /* USER CODE END TIM2_IRQn 1 */
}

/**
  * @brief This function handles TIM3 global interrupt.
  */
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */

  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */

  /* USER CODE END TIM3_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
	
 	 uint32_t temp;
	// 空闲中断处理
    if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE))
    {
        __HAL_UART_CLEAR_IDLEFLAG(&huart1);  
		temp = huart1.Instance->SR;
		temp = huart1.Instance->DR;
		HAL_UART_DMAStop(&huart1);
        // 计算接收到的数据长度
        uint16_t len = RX_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
        
        // 处理数据
        if(len > 0)
        {
		 	ProcessModbusFrame(RX1Data, len);  /* 处理完整帧 */	
//         	HAL_UART_Transmit(&huart1, RX1Data, len, 0xffff);             // 将接收到的数据再发出
		 	len = 0;
		 	memset(RX1Data, 0, RX_BUFFER_SIZE);
		 	HAL_UART_Receive_DMA(&huart1, RX1Data, RX_BUFFER_SIZE); /* 继续接收 */
        }
	}
	
  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream2 global interrupt.
  */
void DMA2_Stream2_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream2_IRQn 0 */

  /* USER CODE END DMA2_Stream2_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart1_rx);
  /* USER CODE BEGIN DMA2_Stream2_IRQn 1 */

  /* USER CODE END DMA2_Stream2_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream7 global interrupt.
  */
void DMA2_Stream7_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream7_IRQn 0 */

  /* USER CODE END DMA2_Stream7_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart1_tx);
  /* USER CODE BEGIN DMA2_Stream7_IRQn 1 */

  /* USER CODE END DMA2_Stream7_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
