/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f7xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  *
  * COPYRIGHT(c) 2019 STMicroelectronics
  *
  * 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 STMicroelectronics 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.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "Data_deal.h"
#include "stm32f7xx_it.h"
#include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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 */
#include "usart.h"
#include "tim.h"
#include "KeySlave485.h"
#include "Dwin.h"
#include "Elec_Seal.h"
#define Screen_SleepTime  1800  //3600// 60分钟 3600S/60=10min
#define RE_Sealloading_Time 60//自动施封时间，单位秒
uint16_t Sleep_Count=0;
uint16_t Count_LED5=0;
uint16_t Read_Collect485_Count;
uint16_t Auto_unload_clear_cnt=0;
uint8_t  Auto_Seal_Exti_cnt = 0;
uint32_t overflow_cnt=0;//静电控制器脉冲输入溢出次数
uint32_t Pro_overflow_cnt =0;
uint16_t Auto_Seal_cnt_Time = 0;
extern volatile uint8_t screen_delay;

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim6;
extern DMA_HandleTypeDef hdma_uart4_rx;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart2_rx;
extern DMA_HandleTypeDef hdma_usart3_rx;
extern UART_HandleTypeDef huart4;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;
extern TIM_HandleTypeDef htim1;

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M7 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 */

  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */
	if (CoreDebug->DHCSR & 1) {  //check C_DEBUGEN == 1 -> Debugger Connected  
      __breakpoint(0);  // halt program execution here         
  }  
  /* 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 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 */
}

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

/**
  * @brief This function handles DMA1 stream1 global interrupt.
  */
void DMA1_Stream1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream1_IRQn 0 */

  /* USER CODE END DMA1_Stream1_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart3_rx);
  /* USER CODE BEGIN DMA1_Stream1_IRQn 1 */

  /* USER CODE END DMA1_Stream1_IRQn 1 */
}

/**
  * @brief This function handles DMA1 stream2 global interrupt.
  */
void DMA1_Stream2_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream2_IRQn 0 */

  /* USER CODE END DMA1_Stream2_IRQn 0 */
;//  HAL_DMA_IRQHandler(&hdma_uart4_rx);
  /* USER CODE BEGIN DMA1_Stream2_IRQn 1 */

  /* USER CODE END DMA1_Stream2_IRQn 1 */
}

/**
  * @brief This function handles DMA1 stream5 global interrupt.
  */
void DMA1_Stream5_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream5_IRQn 0 */

  /* USER CODE END DMA1_Stream5_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_usart2_rx);
  /* USER CODE BEGIN DMA1_Stream5_IRQn 1 */

  /* USER CODE END DMA1_Stream5_IRQn 1 */
}

/**
  * @brief This function handles TIM1 update interrupt and TIM10 global interrupt.
  */
int usart_rx_len;
int delay_ti;
int delay_usart1;
int rs485_rts_delay;
int rs455_1rts_delay;
void TIM1_UP_TIM10_IRQHandler(void)
{
  /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */

  /* USER CODE END TIM1_UP_TIM10_IRQn 0 */
  HAL_TIM_IRQHandler(&htim1);
  if(screen_delay)
  {
    --screen_delay;
  }
	if(delay_ti)
	{
		--delay_ti;
		if(delay_ti==0)
		{
			usart2_rx_flag = 1;
			rx2_len = usart_rx_len;
			usart_rx_len=0;
		}
	}
	
	if(delay_usart1)
	{
		--delay_usart1;
		if(delay_usart1==0)
		{
			if(rx1_len)
        usart1_rx_flag = 1; 
		}
	}
#if 0  
	if(rs485_rts_delay)
	{
		--rs485_rts_delay;
		if(rs485_rts_delay==0)
			TerminalSlave485_TX_Set(0);
	}
	
	if(rs455_1rts_delay)
	{
		--rs455_1rts_delay;
		if(rs455_1rts_delay==0)
			CollectMaster485_TX_Set(0);			//设置为接收模式	
	}
#endif	
	
  ++circle_send;
  /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */

  /* USER CODE END TIM1_UP_TIM10_IRQn 1 */
}

/**
  * @brief This function handles TIM2 global interrupt.
  */
extern SDateTime m_datetime;
void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
	Sleep_Count++;
	Read_Collect485_Count++;
	
	++m_datetime.msec;
	if(m_datetime.msec>=1000)
	{
		m_datetime.msec=0;
		++m_datetime.sec;
		if(m_datetime.sec>=60)
		{
			m_datetime.sec=0;
			++m_datetime.min;
			if(m_datetime.min>=60)
			{
				m_datetime.min=0;
				++m_datetime.hour;
				if(m_datetime.hour>=24)
				{
					m_datetime.hour=0;
					++m_datetime.day;
				}
			}
		}
	}
	
	//打印机定时器
	if(tmrForPrinter>0)
	tmrForPrinter--;
	
	//任何按键按下定时器
	if(tmrForKeyPad>0)
	tmrForKeyPad--;
	
		//远程标定模式定时器
	if(tmrForRemoteCali>0)
	tmrForRemoteCali--;
	
	
	//手工输入温度之后的定时器
	if(tmrForTemp_ManualInput>0)
	tmrForTemp_ManualInput--;
	
	
	if(Sleep_Count>=1000)
	{
		Sleep_Count=0;
		Sleep_Timer_cnt3++;
		Auto_Seal_Exti_cnt++;
    StartUpdate_Screen_Flag=True;
    if(usart_timeout)
		{
			++usart_timesec;
		}
		if(Sleep_Timer_cnt3>=Screen_SleepTime)
		{
			if(Lcd_sleep_flag!=1)
				Lcd_sleep_flag=1;
			Sleep_Timer_cnt3=0;
		}
		
		
		if(Auto_Seal_Exti_cnt>=5)//5S
		{
			Auto_Seal_Exti_cnt = 0;
			if(Pro_overflow_cnt!=overflow_cnt)
			{
				Auto_Seal_cnt_Time =0;
				Pro_overflow_cnt = overflow_cnt;//防溢流静电控制器连接中
				Full_Oil_allinfo.Start_Full_flag =1;//静电控制器插入，开始装油
				Full_Oil_allinfo.Start_Decrease_flag =0;
				Full_Oil_allinfo.Auto_full_oil_mode =1;//自动模式
			}
			else
			{
				overflow_cnt = 0;//防溢流静电控制器断开中
				Pro_overflow_cnt = 0;
			}
		}
		if((Config_info_all.Config_fun_id&0x01)==0x01)//开启自动施封功能
		{		
			if((overflow_cnt == 0)&&(Full_Oil_allinfo.Start_Full_flag ==1))
			{
				Auto_Seal_cnt_Time++;
				if(Auto_Seal_cnt_Time>Config_info_all.Auto_Seal_Time)//自动施封
				{
					Auto_Seal_cnt_Time =0;
					Full_Oil_allinfo.Start_Seal_flag =1;//开始施封
				}
			}
		}
	}
	if(Read_Collect485_Count>=READ_COLLECT_INTERVAL)//500
	{
		Read_Collect485_Count=0;
		Read_Collect485_Flag=True;
	}
	
	
	if(Auto_unload_clear_info_flag == 1)//计数10S
	{
		Auto_unload_clear_cnt++;
		if(Auto_unload_clear_cnt>=10000)//5000  =5s
		{
			Auto_unload_clear_info_flag = 0;
			Auto_unload_clear_cnt = 0;
			Auto_unload_clear_start_flag = 1;
		}
	}
		
  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */

  /* USER CODE END TIM2_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
	//HAL_UART_IRQHandler(&huart1);
	uint32_t isrflags   = huart1.Instance->ISR;
  uint32_t cr1its     = READ_REG(huart1.Instance->CR1);
	
	if(__HAL_UART_GET_IT(&huart1,UART_IT_RXNE) && __HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE))
	{
		__HAL_UART_CLEAR_IT(&huart1,UART_FLAG_RXNE);
      rx1_len %= 1024;
		  USART1_RX_BUF[rx1_len] = huart1.Instance->RDR;
		  rx1_len++;
		  delay_usart1=3;
	}
	
  if (((cr1its & USART_CR1_TXEIE) != RESET)  &&  __HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE))
  {
		__HAL_UART_CLEAR_IT(&huart1,UART_FLAG_TXE);
    if (huart1.pTxBuffPtr)
    {
			huart1.Instance->TDR = (huart1.pTxBuffPtr[huart1.TxXferCount++] & (uint8_t)0xFFU);
      if (huart1.TxXferCount >= huart1.TxXferSize)
      {
         /* Disable the UART Transmit Data Register Empty Interrupt */
        CLEAR_BIT(huart1.Instance->CR1, USART_CR1_TXEIE);
				//__HAL_UART_DISABLE_IT(&huart1,UART_IT_TXE);

        /* Enable the UART Transmit Complete Interrupt */
        SET_BIT(huart1.Instance->CR1, USART_CR1_TCIE);
				//__HAL_UART_ENABLE_IT(&huart1,UART_IT_TC);	
      }
			//else
        
    }
  }
  else if(((isrflags & UART_FLAG_TC)) && ((cr1its & USART_CR1_TCIE) != RESET))
  {
    TerminalSlave485_TX_Set(0);
    __HAL_UART_DISABLE_IT(&huart1,UART_IT_TC);
    huart1.gState = HAL_UART_STATE_READY;
  }

/*	if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE))
  {
		UsartReceive_IDLE(&huart1);
	}*/
  /* USER CODE END USART1_IRQn 0 */
 // HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
  if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_ORE))
  {
        __HAL_UART_CLEAR_OREFLAG(&huart1);
				//taskENTER_CRITICAL();
       // HAL_UART_MspDeInit(&huart1);
				//HAL_UART_MspInit(&huart1);
				//taskEXIT_CRITICAL();
		}
   
		__HAL_UART_CLEAR_IT(&huart1,isrflags);
  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt.
  */

void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
	uint32_t isrflags   = huart2.Instance->ISR;
	uint32_t cr1its     = READ_REG(huart2.Instance->CR1);
/*	HAL_UART_IRQHandler(&huart2);
	if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_IDLE))
  {
		UsartReceive_IDLE(&huart2);
	}*/
	if(__HAL_UART_GET_IT(&huart2,UART_IT_RXNE) && __HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE))
	{
		__HAL_UART_CLEAR_IT(&huart2,UART_FLAG_RXNE);
		usart_rx_len %= Uart2_BUF_SIZE;
		USART2_RX_BUF[usart_rx_len] = huart2.Instance->RDR;
		usart_rx_len++;
		delay_ti=3;
	}

  /*
    中断发送
    zhuzl 20211215 add 
  */
  if (((cr1its & USART_CR1_TXEIE) != RESET) &&  __HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE))
  {
		__HAL_UART_CLEAR_IT(&huart2,UART_FLAG_TXE);
    if (huart2.pTxBuffPtr)
    {
      huart2.Instance->TDR = (huart2.pTxBuffPtr[huart2.TxXferCount++] & (uint8_t)0xFFU);
      if (huart2.TxXferCount >= huart2.TxXferSize)
      {
        /* Disable the UART Transmit Data Register Empty Interrupt */
        CLEAR_BIT(huart2.Instance->CR1, USART_CR1_TXEIE);

        /* Enable the UART Transmit Complete Interrupt */
        SET_BIT(huart2.Instance->CR1, USART_CR1_TCIE);
		
      }        
    }
  }
  else if(((isrflags & UART_FLAG_TC)) && ((cr1its & USART_CR1_TCIE) != RESET))
  {
    CollectMaster485_TX_Set(0); //设置为接收模式	
    huart2.gState = HAL_UART_STATE_READY;
    __HAL_UART_DISABLE_IT(&huart2,UART_IT_TC);
  } 
	
	//20210107李伟添加 
	//if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_ORE))
  {
  //      __HAL_UART_CLEAR_OREFLAG(&huart2);
	}

  if((__HAL_UART_GET_FLAG(&huart2, UART_FLAG_ORE) != RESET))
				{
					//i = UART2->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
					//i = UART2->RDR; //读取数据寄存器中的数据
					//i = hdma_uart4_rx.Instance->NDTR;
					__HAL_UART_CLEAR_OREFLAG(&huart2);
					//HAL_DMA_DeInit(&hdma_uart4_rx);
				//	HAL_UART_MspInit(&huart2);
					//HAL_UART_Receive_DMA(&huart4, USART_RX_BUF,BUF_SIZE);

				}
	__HAL_UART_CLEAR_IT(&huart2,isrflags);
  /* USER CODE END USART2_IRQn 0 */
	//	HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */
	
	

  /* USER CODE END USART2_IRQn 1 */
}

/**
  * @brief This function handles USART3 global interrupt.
  */
 int usart3_rx_len;
void USART3_IRQHandler(void)
{
	uint32_t isrflags   = huart3.Instance->ISR;
  uint32_t cr1its     = READ_REG(huart3.Instance->CR1);
  /* USER CODE BEGIN USART3_IRQn 0 */
	 //HAL_UART_IRQHandler(&huart3);
/*	if(__HAL_UART_GET_FLAG(&huart3,UART_FLAG_IDLE))
  {
		UsartReceive_IDLE(&huart3);
	}*/
  if(__HAL_UART_GET_IT(&huart3,UART_IT_RXNE) && __HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE))
	{
		__HAL_UART_CLEAR_IT(&huart3,UART_FLAG_RXNE);
		usart3_rx_len %= 128;
		USART3_RX_BUF[usart3_rx_len] = huart3.Instance->RDR;
		usart3_rx_len++;
	//	delay_ti=5;
	}
  /*
    中断发送
    zhuzl 20211215 add 
  */
  if (((cr1its & USART_CR1_TXEIE) != RESET) &&  __HAL_UART_GET_FLAG(&huart3, UART_FLAG_TXE))
  {
    if (huart3.pTxBuffPtr)
    {
      huart3.Instance->TDR = (huart3.pTxBuffPtr[huart3.TxXferCount++] & (uint8_t)0xFFU);
      if (huart3.TxXferCount >= huart3.TxXferSize)
      {
				//__HAL_UART_CLEAR_IT(&huart3,UART_FLAG_TXE);
        CLEAR_BIT(huart3.Instance->CR1, USART_CR1_TXEIE);
				/* Enable the UART Transmit Complete Interrupt */
        SET_BIT(huart3.Instance->CR1, USART_CR1_TCIE);
      }    
    }
  }
  else if(((isrflags & UART_FLAG_TC)) && ((cr1its & USART_CR1_TCIE) != RESET))
  {
    ScreenMaster485_TX_Set(0);
    CLEAR_BIT(huart3.Instance->CR1, USART_CR1_TCIE);
    huart3.gState = HAL_UART_STATE_READY;
    screen_delay=40;
  }

    //20210107李伟添加 
	if(__HAL_UART_GET_FLAG(&huart3,UART_FLAG_ORE))
  {
        __HAL_UART_CLEAR_OREFLAG(&huart3);
				HAL_UART_MspInit(&huart3);
	}
	__HAL_UART_CLEAR_IT(&huart3,isrflags);
	
  /* USER CODE END USART3_IRQn 0 */
 // HAL_UART_IRQHandler(&huart3);
  /* USER CODE BEGIN USART3_IRQn 1 */

  /* USER CODE END USART3_IRQn 1 */
}

/**
  * @brief This function handles UART4 global interrupt.
  */
void UART4_IRQHandler(void)
{
  /* USER CODE BEGIN UART4_IRQn 0 */
	uint32_t i = 0;
  uint32_t isrflags   = huart4.Instance->ISR;
  uint32_t cr1its     = READ_REG(huart4.Instance->CR1);
	//HAL_UART_IRQHandler(&huart4);
  if (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE))
  {
    // UsartReceive_IDLE(&huart4);
    __HAL_UART_CLEAR_IDLEFLAG(&huart4);
    //__HAL_UART_CLEAR_IDLEFLAG(UART4);
    //						i = huart4->Instance->ISR;
    //            i = huart4->Instance->RDR;
    //            i = hdma_uart4_rx.Instance->NDTR;
    // HAL_UART_DMAStop(huart);
    // HAL_UART_Receive_DMA(&huart4, USART_RX_BUF,BUF_SIZE);
    //此处处理数据，主要是拷贝和置位标志位 //
    // memcpy(UART4_RX_BUF, USART_RX_BUF, (BUF_SIZE - i));
    usart4_rx_flag = 1;
    rx4_len = uart4RxCounter - 0;
    uart4RxCounter = 0;
    //	__nop();
    // rx4_len = BUF_SIZE - i;//实际接收到的有用数据
    // memset(USART_RX_BUF, 0x00, BUF_SIZE);
  }

  if ((__HAL_UART_GET_FLAG(&huart4, UART_FLAG_ORE) != RESET))
  {
    i = UART4->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
    i = UART4->RDR; //读取数据寄存器中的数据
    // i = hdma_uart4_rx.Instance->NDTR;
    __HAL_UART_CLEAR_OREFLAG(&huart4);
    // HAL_DMA_DeInit(&hdma_uart4_rx);
    HAL_UART_MspInit(&huart4);
    // HAL_UART_Receive_DMA(&huart4, USART_RX_BUF,BUF_SIZE);
  }

  if (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_RXNE)) //接收中断
  {
    i = UART4->RDR;

    // sMyUart4.rxBuf[sMyUart4.rxTotal++] = i;
    UART4_RX_BUF[uart4RxCounter++] = i;

    //__nop();

    if (uart4RxCounter >= Uart1_BUF_SIZE - 1)
    {
      uart4RxCounter = 0; //避免溢出错误
    }

    //__HAL_UART_CLEAR_OREFLAG(UART4, UART_IT_RXNE);读取DR自动清零    不需要手动清零
  }

  if (((cr1its & USART_CR1_TXEIE) != RESET) && __HAL_UART_GET_FLAG(&huart4, UART_FLAG_TXE))
  {
    if (huart4.pTxBuffPtr)
    {
      huart4.Instance->TDR = (huart4.pTxBuffPtr[huart4.TxXferCount++] & (uint8_t)0xFFU);
      if (huart4.TxXferCount >= huart4.TxXferSize)
      {
        //__HAL_UART_CLEAR_IT(&huart3,UART_FLAG_TXE);
        CLEAR_BIT(huart4.Instance->CR1, USART_CR1_TXEIE);
        /* Enable the UART Transmit Complete Interrupt */
        SET_BIT(huart4.Instance->CR1, USART_CR1_TCIE);
      }
    }
  }
  else if (((isrflags & UART_FLAG_TC)) && ((cr1its & USART_CR1_TCIE) != RESET))
  {
    KeySlave485_TX_Set(0);
    CLEAR_BIT(huart4.Instance->CR1, USART_CR1_TCIE);
    huart4.gState = HAL_UART_STATE_READY;
  }

  /* USER CODE END UART4_IRQn 0 */
//  HAL_UART_IRQHandler(&huart4);
  /* USER CODE BEGIN UART4_IRQn 1 */
	
//	
//	__HAL_UART_CLEAR_PEFLAG(&huart4);
//	
//	__HAL_UART_CLEAR_FEFLAG(&huart4);
//	
//	__HAL_UART_CLEAR_OREFLAG(&huart4);
	
	//20210107李伟添加 
	/*if(__HAL_UART_GET_FLAG(&huart4,UART_FLAG_ORE))
  {
        __HAL_UART_CLEAR_OREFLAG(&huart4);
	}*/
	__HAL_UART_CLEAR_IT(&huart4,isrflags);
 
	

  /* USER CODE END UART4_IRQn 1 */
}

/**
  * @brief This function handles TIM6 global interrupt, DAC1 and DAC2 underrun error interrupts.
  */
void TIM6_DAC_IRQHandler(void)
{
  /* USER CODE BEGIN TIM6_DAC_IRQn 0 */
//	Count_LED4++;
//	if(Count_LED4>=500)
//	{
//		Count_LED4=0;
//		HAL_GPIO_TogglePin(GPIOE,LED4_Pin);
//	}
  /* USER CODE END TIM6_DAC_IRQn 0 */
  HAL_TIM_IRQHandler(&htim6);
  /* USER CODE BEGIN TIM6_DAC_IRQn 1 */
	
  /* USER CODE END TIM6_DAC_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 */
}

/* USER CODE BEGIN 1 */
/**
  * @brief This function handles EXTI line0 interrupt.
  */
void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */

  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */

  /* USER CODE END EXTI0_IRQn 1 */
}

/**
  * @brief This function handles EXTI line[15:10] interrupts.
  */
//void EXTI15_10_IRQHandler(void)
//{
//  /* USER CODE BEGIN EXTI15_10_IRQn 0 */

//  /* USER CODE END EXTI15_10_IRQn 0 */
//  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10);
//  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11);
//  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12);
//  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
//  /* USER CODE BEGIN EXTI15_10_IRQn 1 */

//  /* USER CODE END EXTI15_10_IRQn 1 */
//}
//void EXTI15_10_IRQHandler(void)
//{
//    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);  //调用中断处理公用函数
//}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)  
{  
  /* NOTE: This function Should not be modified, when the callback is needed, 
           the HAL_GPIO_EXTI_Callback could be implemented in the user file 
   */  
    switch(GPIO_Pin)  
    {  
        case GPIO_PIN_0: overflow_cnt++;break;  
//        case GPIO_PIN_10:LED1_Toggle();break;  
//        case GPIO_PIN_11:LED2_Toggle();break;  
//        case GPIO_PIN_12:LED3_Toggle();break; 
//				case GPIO_PIN_13:LED3_Toggle();break;
        default:break;  
    }  
}       
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
