/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether 
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * Copyright (c) 2019 STMicroelectronics International N.V. 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without 
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution 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 other 
  *    contributors to this software may be used to endorse or promote products 
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this 
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under 
  *    this license is void and will automatically terminate your rights under 
  *    this license. 
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
  * SHALL STMICROELECTRONICS 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.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "usart.h"
#include "Dwin.h"

/* USER CODE BEGIN 0 */
#include <stdio.h>
#include <string.h>
#include "TerminalSlave485.h"
#include "KeySlave485.h"
#include "CollectMaster485.h"
#include "ScreenMaster485.h"



volatile uint8_t usart1_rx_flag;
volatile uint8_t usart2_rx_flag;
volatile uint8_t usart3_rx_flag;
volatile uint8_t usart4_rx_flag;
uint16_t rx1_len;
uint16_t rx2_len;
uint16_t rx3_len;
uint16_t rx3_len;
uint16_t rx4_len;

static uint8_t USART_RX_BUF_U1[Uart1_BUF_SIZE];   /*为UART专门开一个DMA接收存储区 2021-2-23 by Daiyf*/
uint8_t USART_RX_BUF_U2[Uart2_BUF_SIZE];   /*为UART2、3专门开一个DMA接收存储区 2021-4-12 by Daiyf*///static
static uint8_t USART_RX_BUF_U3[Uart3_BUF_SIZE];   

uint8_t USART_RX_BUF[Uart1_BUF_SIZE];
uint8_t USART_TX_BUF[Uart1_BUF_SIZE];
uint8_t USART1_RX_BUF[Uart1_BUF_SIZE];
uint8_t USART1_TX_BUF[Uart1_BUF_SIZE];
uint8_t USART2_RX_BUF[Uart2_BUF_SIZE];
uint8_t USART2_TX_BUF[Uart2_BUF_SIZE];
uint8_t USART3_RX_BUF[Uart3_BUF_SIZE];
uint8_t USART3_TX_BUF[Uart3_BUF_SIZE];
uint8_t UART4_RX_BUF[BUF_SIZE];
uint8_t UART4_TX_BUF[BUF_SIZE];
/* USER CODE END 0 */

UART_HandleTypeDef huart4;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
//DMA_HandleTypeDef hdma_uart4_rx;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart3_rx;

/* UART4 init function */
void MX_UART4_Init(void)
{

  huart4.Instance = UART4;
  huart4.Init.BaudRate = 9600;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    Error_Handler();
  }
	
	
	

}
/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600; ////38400;//
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }

}
/* USART2 init function */

void MX_USART2_UART_Init(void)
{

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;//19200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }

}
/* USART3 init function */

void MX_USART3_UART_Init(void)
{

  huart3.Instance = USART3;
  huart3.Init.BaudRate = 19200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==UART4)
  {
  /* USER CODE BEGIN UART4_MspInit 0 */

  /* USER CODE END UART4_MspInit 0 */
    /* UART4 clock enable */
    __HAL_RCC_UART4_CLK_ENABLE();
  
    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**UART4 GPIO Configuration    
    PC10     ------> UART4_TX
    PC11     ------> UART4_RX 
    */
    GPIO_InitStruct.Pin = Key_TX_Pin|Key_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* UART4 DMA Init */
    /* UART4_RX Init */
		/*     //李伟修改于20210516
    hdma_uart4_rx.Instance = DMA1_Stream2;
    hdma_uart4_rx.Init.Channel = DMA_CHANNEL_4;
    hdma_uart4_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_uart4_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_uart4_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_uart4_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_uart4_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_uart4_rx.Init.Mode = DMA_CIRCULAR;
    hdma_uart4_rx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_uart4_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_uart4_rx) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(uartHandle,hdmarx,hdma_uart4_rx);
		*/

    /* UART4 interrupt Init */
    HAL_NVIC_SetPriority(UART4_IRQn, 0, 4);
    HAL_NVIC_EnableIRQ(UART4_IRQn);
  /* USER CODE BEGIN UART4_MspInit 1 */

  /* USER CODE END UART4_MspInit 1 */
  }
  else if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    GPIO_InitStruct.Pin = Terminal_TX_Pin|Terminal_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#if 0
    /* USART1 DMA Init */
    /* USART1_RX Init */
    hdma_usart1_rx.Instance = DMA2_Stream2;
    hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
    hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
    hdma_usart1_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
    hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);
		
//		__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);          //使能IDLE中断
#endif    
		/* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 2);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */
    /* USART2 clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration    
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX 
    */
    GPIO_InitStruct.Pin = Collect_TX_Pin|Collect_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#if 0
    /* USART2 DMA Init */
    /* USART2_RX Init */
    hdma_usart2_rx.Instance = DMA1_Stream5;
    hdma_usart2_rx.Init.Channel = DMA_CHANNEL_4;
    hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart2_rx.Init.Mode = DMA_CIRCULAR;
    hdma_usart2_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
    hdma_usart2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);
#endif
    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 0, 2);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */
  }
  else if(uartHandle->Instance==USART3)
  {
  /* USER CODE BEGIN USART3_MspInit 0 */

  /* USER CODE END USART3_MspInit 0 */
    /* USART3 clock enable */
    __HAL_RCC_USART3_CLK_ENABLE();
  
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**USART3 GPIO Configuration    
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX 
    */
    GPIO_InitStruct.Pin = Screen_TX_Pin|Screen_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
#if 0
    /* USART3 DMA Init */
    /* USART3_RX Init */
    hdma_usart3_rx.Instance = DMA1_Stream1;
    hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
    hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;
    hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
    hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
    hdma_usart3_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
    hdma_usart3_rx.Init.MemBurst = DMA_MBURST_SINGLE;
    hdma_usart3_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
    if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
#endif
    /* USART3 interrupt Init */
    HAL_NVIC_SetPriority(USART3_IRQn, 0, 3);
    HAL_NVIC_EnableIRQ(USART3_IRQn);
  /* USER CODE BEGIN USART3_MspInit 1 */

  /* USER CODE END USART3_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==UART4)
  {
  /* USER CODE BEGIN UART4_MspDeInit 0 */

  /* USER CODE END UART4_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_UART4_CLK_DISABLE();
  
    /**UART4 GPIO Configuration    
    PC10     ------> UART4_TX
    PC11     ------> UART4_RX 
    */
    HAL_GPIO_DeInit(GPIOC, Key_TX_Pin|Key_RX_Pin);

    /* UART4 DMA DeInit */
    HAL_DMA_DeInit(uartHandle->hdmarx);

    /* UART4 interrupt Deinit */
    HAL_NVIC_DisableIRQ(UART4_IRQn);
  /* USER CODE BEGIN UART4_MspDeInit 1 */

  /* USER CODE END UART4_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();
  
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    HAL_GPIO_DeInit(GPIOA, Terminal_TX_Pin|Terminal_RX_Pin);

    /* USART1 DMA DeInit */
    HAL_DMA_DeInit(uartHandle->hdmarx);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspDeInit 0 */

  /* USER CODE END USART2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART2_CLK_DISABLE();
  
    /**USART2 GPIO Configuration    
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX 
    */
    HAL_GPIO_DeInit(GPIOA, Collect_TX_Pin|Collect_RX_Pin);

    /* USART2 DMA DeInit */
    HAL_DMA_DeInit(uartHandle->hdmarx);

    /* USART2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspDeInit 1 */

  /* USER CODE END USART2_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART3)
  {
  /* USER CODE BEGIN USART3_MspDeInit 0 */

  /* USER CODE END USART3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART3_CLK_DISABLE();
  
    /**USART3 GPIO Configuration    
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX 
    */
    HAL_GPIO_DeInit(GPIOB, Screen_TX_Pin|Screen_RX_Pin);

    /* USART3 DMA DeInit */
    HAL_DMA_DeInit(uartHandle->hdmarx);

    /* USART3 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART3_IRQn);
  /* USER CODE BEGIN USART3_MspDeInit 1 */

  /* USER CODE END USART3_MspDeInit 1 */
  }
} 
/* USER CODE BEGIN 1 */
uint16_t ErrorLength;
uint32_t iUart1,iUart2,iUart3,iUart4;//siUart_ALL = 0,
void UsartReceive_IDLE(UART_HandleTypeDef *huart)
{
    
    if((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
    {
        if(huart->Instance == USART1)
        {
            __HAL_UART_CLEAR_IDLEFLAG(huart);
					//	i = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能  /*这三行不要。 2021-2-23 by Daiyf*/
          //  i = huart->Instance->RDR; //读取数据寄存器中的数据
          //  i = hdma_usart1_rx.Instance->NDTR;
//						__HAL_UART_DISABLE_IT(&huart1, UART_IT_RXNE);
            HAL_UART_DMAStop(huart);
						iUart1 =  __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);// 获取DMA中未传输的数据个数 
//						HAL_UART_Receive_DMA(&huart1, USART_RX_BUF,Uart1_BUF_SIZE);//重新打开DMA接收
            /* 此处处理数据，主要是拷贝和置位标志位 */
            memcpy(USART1_RX_BUF, USART_RX_BUF_U1, (Uart1_BUF_SIZE - iUart1));  /*为UART专门开一个DMA接收存储区 2021-2-23 by Daiyf*/
            usart1_rx_flag = 1; 
            rx1_len = Uart1_BUF_SIZE - iUart1;//-2;//实际接收到的有用数据,总计数减去未传输的数据个数，得到已经接收的数据个数(0x0D,0x0A)
            /* 清空缓存，重新接收 */
						ErrorLength=Uart1_BUF_SIZE;//DMA缺陷处理不定长数据时，连续数据太快导致长度异常，处理方法不用RTOS，或者不用DMA.
//						printf("%d:%s\r\n",rx1_len,USART_RX_BUF);
//						KeySlave485_Send_Data(USART_RX_BUF,rx1_len);
//						SendChar4(rx1_len);
            //memset(USART_RX_BUF_U1, 0x00, Uart1_BUF_SIZE);                                        /*这二行不要。 2021-2-23 by Daiyf*/
						//__HAL_DMA_SET_COUNTER(&hdma_usart1_rx, 0); //清除DMA 计数

						HAL_UART_Receive_DMA(&huart1, USART_RX_BUF_U1,Uart1_BUF_SIZE);//重新打开DMA接收
						if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) != RESET))
						{
							iUart1 = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
							iUart1 = huart->Instance->RDR; //读取数据寄存器中的数据
							iUart1 = hdma_usart1_rx.Instance->NDTR;
							__HAL_UART_CLEAR_OREFLAG(&huart1);
							HAL_DMA_DeInit(&hdma_usart1_rx);
						}
			}
        else if(huart->Instance == USART2)
        {
            __HAL_UART_CLEAR_IDLEFLAG(huart);
						HAL_UART_DMAStop(huart);
						iUart2 = huart->Instance->ISR;
            iUart2 = huart->Instance->RDR;
            iUart2 =__HAL_DMA_GET_COUNTER(&hdma_usart2_rx);// 获取DMA中未传输的数据个数 
					  iUart2 =hdma_usart2_rx.Instance->NDTR;
					
								
						//memcpy(USART2_RX_BUF, USART_RX_BUF, rx2_len);
						memcpy(USART2_RX_BUF, USART_RX_BUF_U2, Uart2_BUF_SIZE - iUart2);  //这里原来可能有点问题，改了一下与其他串口方式相同处理
						__nop();
						//memcpy(USART2_RX_BUF, USART_RX_BUF_U2, 64);
						__nop();

						usart2_rx_flag = 1;
						rx2_len = Uart2_BUF_SIZE - iUart2;//实际接收到的有用数据
						// memset(USART_RX_BUF_U2, 0x00, Uart2_BUF_SIZE);          /*这二行不要。 2021-4-12 by Daiyf*/

					

						HAL_UART_Receive_DMA(&huart2, USART_RX_BUF_U2,Uart2_BUF_SIZE); /*为UART2专门开一个DMA接收存储区 2021-4-12 by Daiyf*/
            /* 此处处理数据，主要是拷贝和置位标志位 */
			
			

        }
        else if(huart->Instance == USART3)
        {
            __HAL_UART_CLEAR_IDLEFLAG(huart);
					#if 1
						HAL_UART_DMAStop(huart);
						iUart3 =  __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);// 获取DMA中未传输的数据个数 
						memcpy(USART3_RX_BUF, USART_RX_BUF_U3, (Uart3_BUF_SIZE - iUart3));
						usart3_rx_flag = 1; 
						rx3_len = Uart3_BUF_SIZE - iUart3;
						ErrorLength=Uart3_BUF_SIZE;
						
					  HAL_UART_Receive_DMA(&huart3, USART_RX_BUF_U3,Uart3_BUF_SIZE);//重新打开DMA接收
						if((__HAL_UART_GET_FLAG(&huart3, UART_FLAG_ORE) != RESET))
						{
							iUart3 = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
							iUart3 = huart->Instance->RDR; //读取数据寄存器中的数据
							iUart3 = hdma_usart3_rx.Instance->NDTR;
							__HAL_UART_CLEAR_OREFLAG(&huart3);
							HAL_DMA_DeInit(&hdma_usart3_rx);
						}
						
					#else
						i = huart->Instance->ISR;
			            i = huart->Instance->RDR;
			            i = hdma_usart3_rx.Instance->NDTR;
			            HAL_UART_DMAStop(huart);
									HAL_UART_Receive_DMA(&huart3, USART_RX_BUF_U3,Uart3_BUF_SIZE);  /*为UART3专门开一个DMA接收存储区 2021-4-12 by Daiyf*/
     
								 /* 此处处理数据，主要是拷贝和置位标志位 */
			            memcpy(USART3_RX_BUF, USART_RX_BUF_U3, (Uart3_BUF_SIZE - i));
			            usart3_rx_flag = 1;
			            rx3_len = Uart3_BUF_SIZE - i;//实际接收到的有用数据
					 #endif  

					
					
					
					
					
						DateDwin[0]=USART3_RX_BUF[7];//年月日
						DateDwin[1]=USART3_RX_BUF[8];
						DateDwin[2]=USART3_RX_BUF[9];
						DateDwin[3]=USART3_RX_BUF[10];//星期
						DateDwin[4]=USART3_RX_BUF[11];//时分秒
						DateDwin[5]=USART3_RX_BUF[12];
						DateDwin[6]=USART3_RX_BUF[13];
					
            memset(USART_RX_BUF, 0x00, BUF_SIZE);
        }
				
				/*
				else if(huart->Instance == UART4)
        {
            __HAL_UART_CLEAR_IDLEFLAG(huart);
						i = huart->Instance->ISR;
            i = huart->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 = BUF_SIZE - i;//实际接收到的有用数据
            memset(USART_RX_BUF, 0x00, BUF_SIZE);
					
						if((__HAL_UART_GET_FLAG(&huart4, UART_FLAG_ORE) != RESET))
						{
							i = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
							i = huart->Instance->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);
						}
						
        }
				
				*/
    }
//		HAL_DMA_DeInit(&hdma_usart1_rx);
}
void Uart_Mode_Init(void)
{
	//__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
//	__HAL_UART_ENABLE_IT(&huart1, UART_IT_ORE);
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
  //__HAL_UART_ENABLE_IT(&huart1, UART_IT_ERR);
  
	//__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
	//__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
	__HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
	__HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
	__HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);
	KeySlave485_TX_Set(0);			//设置为接收模式	
	TerminalSlave485_TX_Set(0);			//设置为接收模式
	CollectMaster485_TX_Set(0);			//设置为接收模式	
	ScreenMaster485_TX_Set(0);			//设置为接收模式
}


/*****************************************************************
* 串口2发送函数KeySlave485_Send_Data(uint8_t *buf,uint8_t len)
******************************************************************/
void SendChar2 (uint8_t ch)
{
    HAL_UART_Transmit(&huart2, &ch, 1, 0xffff);
}
void SendChar4 (uint8_t ch)
{
		KeySlave485_TX_Set(1);
		HAL_UART_Transmit(&huart4, &ch, 1, 1000);
		KeySlave485_TX_Set(0);
}
/*****************************************************************
* 串口发送数组函数
******************************************************************/
void UartWrite(UART_HandleTypeDef *uartHandle, uint8_t *buf , uint8_t len)
{
    uint8_t i;
    for(i = 0; i < len; i++)
    {
        HAL_UART_Transmit(uartHandle, &buf[i], 1, 0xFFFF);
    }
}
// 重定向函数1
int fputc(int ch,FILE *f)
{
#if 0
    uint8_t temp[1]={ch};
    HAL_UART_Transmit(&huart4,temp,1,2);        //
#endif
		return 0;
}
/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
