
/******************************************************************
*  Copyright (C) 2023 - 2023 OneChip, Inc. All rights reserved.
* File name: van_bsp_uart_fun.c
* Author: fanyidong 	 Date:2020.11.21
* Description:		STM32串口接收函数
* Others:
* History:
        1. Date:
                 Author:
                 Modification:

*****************************************************************/

#include "van_bsp_debug_uart.h"
#include <string.h>

/* USER CODE BEGIN Private defines */

#define RECEIVELEN 1024
#define USART_DMA_SENDING 1   //发送未完成
#define USART_DMA_SENDOVER 0  //发送完成
#define USART_DMA_COPY 1      //拷贝中
#define USART_DMA_COPYOVER 0  //拷贝完成
typedef struct {
  unsigned int receive_flag;      //空闲接收标记
  unsigned int dmaSend_flag;      //发送完成标记
  unsigned int dmaCopy_flag;      //发送拷贝标记
  osMutexId_t mutex_dmaCopy;  
  unsigned int rx_head;           //接收头
  unsigned int tx_head;           //发送头
  unsigned int rx_last;           //接收尾
  unsigned int tx_last;           //发送尾
  unsigned int rx_max_len;        //接收缓冲区长度
  unsigned int tx_max_len;        //发送缓冲区长度
  unsigned int rx_DMA_len;        //接收长度
  unsigned int tx_DMA_len;        //接收长度
  unsigned char *usartDMA_RxBuf;  // DMA接收缓存
  unsigned char *usart_RxBuf;     // 接收缓冲区
  unsigned char *usartDMA_TxBuf;  // DMA发送缓存
  unsigned char *usart_TxBuf;     // 发送缓冲区
  UART_HandleTypeDef *huart;
  
  unsigned int uartError;      //串口错误
} USART_COM_RECEIVETYPE, *pUSART_COM_RECEIVETYPE;

#define SYS_CTR_DMA_LEN 256
#define SYS_CTR_BUF_LEN 1024
USART_COM_RECEIVETYPE sys_ctr_uart;
unsigned char sys_ctr_tDMA_RxBuf[SYS_CTR_DMA_LEN + 4] = {0};
unsigned char sys_ctr_tDMA_TxBuf[SYS_CTR_DMA_LEN + 4] = {0};
unsigned char sys_ctr_RxBuf[SYS_CTR_BUF_LEN];
unsigned char sys_ctr_TxBuf[SYS_CTR_BUF_LEN];
CmdMessage sys_ctr_Message;
unsigned char sys_ctr_data[SYS_CTR_BUF_LEN];


#define THD_NTCSAMP_DMA_LEN 32
#define THD_NTCSAMP_BUF_LEN 64
USART_COM_RECEIVETYPE THD_NTCSAMP_uart;
unsigned char THD_NTCSAMP_tDMA_RxBuf[THD_NTCSAMP_DMA_LEN + 4] = {0};
unsigned char THD_NTCSAMP_tDMA_TxBuf[THD_NTCSAMP_DMA_LEN + 4] = {0};
unsigned char THD_NTCSAMP_RxBuf[THD_NTCSAMP_BUF_LEN];
unsigned char THD_NTCSAMP_TxBuf[THD_NTCSAMP_BUF_LEN];
unsigned char THD_NTCSAMP_Message[32];
unsigned char THD_NTCSAMP_data[SYS_CTR_BUF_LEN];

unsigned int p_rec_flag = 0;
unsigned int p_rec_data_flag = 0;
unsigned int p_DecodeNew = 0;
unsigned int p_Rep_flag = 0;
unsigned int p_Rep_data_flag = 0;
unsigned int p_Ans_flag = 0;
unsigned int p_Ans_data_flag = 0;
unsigned int p_Ans_flag_1 = 0;
unsigned int p_Ans_data_flag_1 = 0;
unsigned char sedbuf[SYS_CTR_BUF_LEN];
unsigned int sedbuf_len;


extern DMA_HandleTypeDef hdma_uart7_rx;

extern UART_HandleTypeDef huart7;
unsigned int resetSysUart ;
typedef struct
{
  __IO uint32_t ISR;   /*!< DMA interrupt status register */
  __IO uint32_t Reserved0;
  __IO uint32_t IFCR;  /*!< DMA interrupt flag clear register */
} DMA_Base_Registers;

 #define osMutexPrioInherit    0x00000002U    // 继承锁  继承等待线程中更高的优先级
const osMutexAttr_t Mutex_attr1 = {
  "myThreadMutex",     // human readable mutex name
  osMutexPrioInherit,  // attr_bits
  NULL,                // memory for control block   
  0U                   // size for control block
};

/****************************************************************
* 函数名:    Van_UART_Fun_TxCpltCallback
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明： DMA 发送中断
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Fun_TxCpltCallback_Base(pUSART_COM_RECEIVETYPE uartinf) {
  UART_HandleTypeDef *huart = uartinf->huart;
  __HAL_DMA_DISABLE(huart->hdmatx);
  unsigned int dataleninTxbuf = (uartinf->tx_last + uartinf->tx_max_len - uartinf->tx_head) % uartinf->tx_max_len;
  if (dataleninTxbuf) {
    memset((void *)uartinf->usartDMA_TxBuf, 0, uartinf->tx_DMA_len);
    if (dataleninTxbuf >= uartinf->tx_DMA_len) {
      if (uartinf->tx_max_len >= (uartinf->tx_head + uartinf->tx_DMA_len)) {
        memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), uartinf->tx_DMA_len);
        uartinf->tx_head += uartinf->tx_DMA_len;
        Van_Device_Printf(VAN_LOG_DEBUG, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
      } else {
        memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), (uartinf->tx_max_len - uartinf->tx_head));
        unsigned int secondcopelen = uartinf->tx_DMA_len - (uartinf->tx_max_len - uartinf->tx_head);
        memcpy((void *)(uartinf->usartDMA_TxBuf + uartinf->tx_max_len - uartinf->tx_head), (const void *)uartinf->usart_TxBuf, secondcopelen);
        uartinf->tx_head = secondcopelen;
        Van_Device_Printf(VAN_LOG_DEBUG, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
      }
      Van_Device_Printf(VAN_LOG_DEBUG, "len:%d %s\n\r***", uartinf->tx_DMA_len, uartinf->usartDMA_TxBuf);
      HAL_UART_Transmit_DMA(huart, uartinf->usartDMA_TxBuf, uartinf->tx_DMA_len);
    } else {
      if (uartinf->tx_max_len >= (uartinf->tx_head + dataleninTxbuf)) {
        memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), dataleninTxbuf);
        uartinf->tx_head += dataleninTxbuf;
        Van_Device_Printf(VAN_LOG_DEBUG, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
      } else {
        memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), (uartinf->tx_max_len - uartinf->tx_head));
        unsigned int secondcopelen = dataleninTxbuf - (uartinf->tx_max_len - uartinf->tx_head);
        memcpy((void *)(uartinf->usartDMA_TxBuf + uartinf->tx_max_len - uartinf->tx_head), (const void *)uartinf->usart_TxBuf, secondcopelen);
        uartinf->tx_head = secondcopelen;
        Van_Device_Printf(VAN_LOG_DEBUG, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
      }
      Van_Device_Printf(VAN_LOG_DEBUG, "len:%d %s\n\r***", dataleninTxbuf, uartinf->usartDMA_TxBuf);
      HAL_UART_Transmit_DMA(huart, uartinf->usartDMA_TxBuf, dataleninTxbuf);
    }
    uartinf->dmaSend_flag = USART_DMA_SENDING;
  } else {
    uartinf->dmaSend_flag = USART_DMA_SENDOVER;
  }
}
/****************************************************************
* 函数名:    Van_UART_Fun_Send_DMA
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明： DMA 发送
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Fun_Send_DMA_Base(unsigned char *pdata, unsigned int Length, pUSART_COM_RECEIVETYPE uartinf) {
  UART_HandleTypeDef *huart = uartinf->huart;
  /*超长不拷贝*/

//  Van_Device_Printf(VAN_LOG_NOTICE, "head:%d last:%d Length:%d %s\n\r", uartinf->tx_head, uartinf->tx_last, Length, pdata);
  if (Length > uartinf->tx_max_len) {
    Length = uartinf->tx_max_len;
  }
//  while (uartinf->dmaCopy_flag == USART_DMA_COPY);
//  uartinf->dmaCopy_flag = USART_DMA_COPY;
  __disable_irq(); 
  osMutexAcquire(uartinf->mutex_dmaCopy,0xffffffff);
  if ((Length + uartinf->tx_last) < uartinf->tx_max_len) {
    memcpy((void *)(uartinf->usart_TxBuf + uartinf->tx_last), (const void *)pdata, Length);
    uartinf->tx_last += Length;
//    Van_Device_Printf(VAN_LOG_NOTICE, "head:%d last:%d \n\r", uartinf->tx_head, uartinf->tx_last);
  } else {
    memcpy((void *)(uartinf->usart_TxBuf + uartinf->tx_last), (const void *)pdata, (uartinf->tx_max_len - uartinf->tx_last));

    unsigned int secondcopelen = Length - (uartinf->tx_max_len - uartinf->tx_last);
    memcpy((void *)uartinf->usart_TxBuf, (const void *)(pdata + (uartinf->tx_max_len - uartinf->tx_last)), secondcopelen);
    uartinf->tx_last = secondcopelen;
//    Van_Device_Printf(VAN_LOG_NOTICE, "head:%d last:%d \n\r", uartinf->tx_head, uartinf->tx_last);
  }  
//  osMutexRelease(uartinf->mutex_dmaCopy);
//  uartinf->dmaCopy_flag = USART_DMA_COPYOVER;
  if (uartinf->dmaSend_flag == USART_DMA_SENDOVER) {
    unsigned int dataleninTxbuf = (uartinf->tx_last + uartinf->tx_max_len - uartinf->tx_head) % uartinf->tx_max_len;
    if (dataleninTxbuf) 
    {
      memset((void *)uartinf->usartDMA_TxBuf, 0, uartinf->tx_DMA_len);
      if (dataleninTxbuf >= uartinf->tx_DMA_len) 
      {
        if (uartinf->tx_max_len >= (uartinf->tx_head + uartinf->tx_DMA_len)) 
        {
          memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), uartinf->tx_DMA_len);
          uartinf->tx_head += uartinf->tx_DMA_len;
//          Van_Device_Printf(VAN_LOG_NOTICE, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
        } 
        else 
        {
          memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), (uartinf->tx_max_len - uartinf->tx_head));
          unsigned int secondcopelen = uartinf->tx_DMA_len - (uartinf->tx_max_len - uartinf->tx_head);
          memcpy((void *)(uartinf->usartDMA_TxBuf + uartinf->tx_max_len - uartinf->tx_head), (const void *)uartinf->usart_TxBuf, secondcopelen);
          uartinf->tx_head = secondcopelen;
//          Van_Device_Printf(VAN_LOG_NOTICE, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
        } 

//        Van_Device_Printf(VAN_LOG_NOTICE, "len:%d %s\n\r***", uartinf->tx_DMA_len, uartinf->usartDMA_TxBuf);
        HAL_UART_Transmit_DMA(huart, uartinf->usartDMA_TxBuf, uartinf->tx_DMA_len);
      } 
      else 
      {
        if (uartinf->tx_max_len >= (uartinf->tx_head + dataleninTxbuf)) 
        {
          memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), dataleninTxbuf);
          uartinf->tx_head += dataleninTxbuf;
//          Van_Device_Printf(VAN_LOG_NOTICE, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
        } 
        else 
        {
          memcpy((void *)uartinf->usartDMA_TxBuf, (const void *)(uartinf->usart_TxBuf + uartinf->tx_head), (uartinf->tx_max_len - uartinf->tx_head));
          unsigned int secondcopelen = dataleninTxbuf - (uartinf->tx_max_len - uartinf->tx_head);
          memcpy((void *)(uartinf->usartDMA_TxBuf + uartinf->tx_max_len - uartinf->tx_head), (const void *)uartinf->usart_TxBuf, secondcopelen);
          uartinf->tx_head = secondcopelen;
//          Van_Device_Printf(VAN_LOG_NOTICE, "line:%d head:%d last:%d \n\r", __LINE__, uartinf->tx_head, uartinf->tx_last);
        }
//        Van_Device_Printf(VAN_LOG_NOTICE, "len:%d %s\n\r***", dataleninTxbuf, uartinf->usartDMA_TxBuf);
        HAL_UART_Transmit_DMA(huart, uartinf->usartDMA_TxBuf, dataleninTxbuf);
      }
      uartinf->dmaSend_flag = USART_DMA_SENDING;
    }
//    else
//      Van_Device_Printf(VAN_LOG_INFO, "HAL_UART_Transmit_DMA dataleninTxbuf == 0\n");
  }
  osMutexRelease(uartinf->mutex_dmaCopy);
__enable_irq();
}

/****************************************************************
* 函数名:    Van_UART_Fun_RxCpltCallback
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明： DMA接收处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Fun_RxCplt_Base(pUSART_COM_RECEIVETYPE uartinf) {
  UART_HandleTypeDef *huart = uartinf->huart;
  uint32_t temp;

  HAL_UART_DMA_RX_Stop(huart);
  // 停止DMA接收
  DMA_Stream_TypeDef *dma = huart->hdmarx->Instance;
  temp = dma->NDTR;
  unsigned int dataleninRxDMAbuf = uartinf->rx_DMA_len - temp;
  uint32_t isrst = huart->Instance->ISR;
  uint32_t dmacr = dma->CR;
//  Van_Device_Printf(VAN_LOG_NOTICE, "%s %d addr:%x %d-%d=%d\n\r", __func__, __LINE__, (unsigned int)uartinf->usartDMA_RxBuf, uartinf->rx_DMA_len, temp,
//                    dataleninRxDMAbuf);
//  Van_Device_Printf(VAN_LOG_NOTICE, "%x %x\n\r", isrst, dmacr);
  if (dataleninRxDMAbuf) {
		if(dataleninRxDMAbuf > uartinf->rx_max_len){
		   Van_Device_Printf(VAN_LOG_NOTICE, "*******************rec %d  clean*******************\n\r", dataleninRxDMAbuf);
		}else{
    //		 Van_Device_Printf(VAN_LOG_NOTICE, "Cplt len:%d %s\n\r***",dataleninRxDMAbuf,uartinf->usartDMA_RxBuf);
    if ((dataleninRxDMAbuf + uartinf->rx_last) <= uartinf->rx_max_len) {
      memcpy((void *)(uartinf->usart_RxBuf + uartinf->rx_last), (const void *)uartinf->usartDMA_RxBuf, dataleninRxDMAbuf);
      uartinf->rx_last += dataleninRxDMAbuf;
      //Van_Device_Printf(VAN_LOG_NOTICE, "rx_last 1:%d  %d  \n\r", uartinf->rx_last, uartinf->rx_head);
    } else {
      memcpy((void *)(uartinf->usart_RxBuf + uartinf->rx_last), (const void *)uartinf->usartDMA_RxBuf, (uartinf->rx_max_len - uartinf->rx_last));
      unsigned int secondcopelen = dataleninRxDMAbuf - (uartinf->rx_max_len - uartinf->rx_last);
      memcpy((void *)uartinf->usart_RxBuf, (const void *)(uartinf->usartDMA_RxBuf + (uartinf->rx_max_len - uartinf->rx_last)), secondcopelen);
      uartinf->rx_last = secondcopelen;
     // Van_Device_Printf(VAN_LOG_NOTICE, "rx_last 2 :%d  %d \n\r", uartinf->rx_last, uartinf->rx_head,dataleninRxDMAbuf,uartinf->rx_max_len );
    }
    uartinf->receive_flag = 1;
	  sysinf.reccmd_conter = sysinf.ms_conter;		
		}

    memset((void *)uartinf->usartDMA_RxBuf, 0, uartinf->rx_DMA_len);
    // 重启DMA接收

    HAL_UART_Receive_DMA((UART_HandleTypeDef *)huart, uartinf->usartDMA_RxBuf, uartinf->rx_DMA_len);
    dmacr = dma->CR;
//   Van_Device_Printf(VAN_LOG_NOTICE, "rx len %d\n\r", uartinf->rx_DMA_len);
  }
}

/****************************************************************
* 函数名:    Van_UART_Fun_Rec_IDLE
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明： 接收处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Fun_Rec_IDLE_Base(pUSART_COM_RECEIVETYPE uartinf) {
  UART_HandleTypeDef *huart = uartinf->huart;
  if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET)) {
    __HAL_UART_CLEAR_IDLEFLAG(huart);
    Van_UART_Fun_RxCplt_Base(uartinf);
//    Van_Device_Printf(VAN_LOG_NOTICE, "%s %d\n\r", __func__, __LINE__);
  }
}

/****************************************************************
* 函数名:    Van_UART_Fun_ErrorCallback
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：异常后处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Fun_ErrorCallback_Base(pUSART_COM_RECEIVETYPE uartinf) {
  UART_HandleTypeDef *huart = uartinf->huart;

  DMA_HandleTypeDef *pdma;
  
  Van_Device_Printf(VAN_LOG_ERROR, "Van_UART_Fun_ErrorCallback_Base %8x  dmaTX%8x  dmaRX%8x\n\r",huart->ErrorCode, huart->hdmatx->ErrorCode, huart->hdmarx->ErrorCode);
  
  //if (huart->ErrorCode == (HAL_UART_ERROR_NE | HAL_UART_ERROR_ORE)) 
//{
	  uartinf->uartError = True;
    __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
//  }
  
  pdma = huart->hdmatx;
  if (pdma->ErrorCode) {
    __HAL_DMA_DISABLE(huart->hdmatx);
    uartinf->dmaSend_flag = USART_DMA_SENDOVER;
  }
  pdma = huart->hdmarx;
  if ( pdma->ErrorCode) {
    __HAL_DMA_DISABLE(huart->hdmarx);
    HAL_UART_Receive_DMA((UART_HandleTypeDef *)huart, uartinf->usartDMA_RxBuf, RECEIVELEN);
    __HAL_UART_ENABLE_IT((UART_HandleTypeDef *)huart, UART_IT_IDLE);
//	Van_Device_Printf(VAN_LOG_ERROR, "huart rx dma reset\n\r");
  }
}

/****************************************************************
* 函数名:    Van_UART_Fun_Rec_IDLE
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：空闲中断回调处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/

void Van_UART_Fun_TxCpltCallback(UART_HandleTypeDef *huart) {
  UART_HandleTypeDef *pUart = sys_ctr_uart.huart;
  UART_HandleTypeDef *pTHD_NTCSAMPUart = THD_NTCSAMP_uart.huart;
  if (huart->Instance == pUart->Instance) {
    Van_UART_Fun_TxCpltCallback_Base(&sys_ctr_uart);
  }else if (huart->Instance == pTHD_NTCSAMPUart->Instance) {    
     Van_UART_Fun_TxCpltCallback_Base(&THD_NTCSAMP_uart);
  }
  return;
}

/****************************************************************
* 函数名:    Van_UART_Fun_Rec_IDLE
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：空闲中断回调处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/

void Van_UART_Fun_RxCpltCallback(UART_HandleTypeDef *huart) {
  UART_HandleTypeDef *pUart = sys_ctr_uart.huart;
  UART_HandleTypeDef *pTHD_NTCSAMPUart = THD_NTCSAMP_uart.huart;
  if (huart->Instance == pUart->Instance) {
    Van_UART_Fun_RxCplt_Base(&sys_ctr_uart);
//    Van_Device_Printf(VAN_LOG_NOTICE, "%s %d\n\r", __func__, __LINE__);
  }else if (huart->Instance == pTHD_NTCSAMPUart->Instance) {    
    Van_UART_Fun_RxCplt_Base(&THD_NTCSAMP_uart);
  }
  return;
}

/****************************************************************
* 函数名:    Van_UART_Fun_Rec_IDLE
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：空闲中断回调处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/

void Van_UART_Fun_ErrorCallback(UART_HandleTypeDef *huart) {
  UART_HandleTypeDef *pUart = sys_ctr_uart.huart;
  UART_HandleTypeDef *pTHD_NTCSAMPUart = THD_NTCSAMP_uart.huart;
  if (huart->Instance == pUart->Instance) {
    Van_UART_Fun_ErrorCallback_Base(&sys_ctr_uart);
  }else if (huart->Instance == pTHD_NTCSAMPUart->Instance) {    
    Van_UART_Fun_ErrorCallback_Base(&THD_NTCSAMP_uart);
  } 
  return;
}

/****************************************************************
* 函数名:    Van_UART_Fun_Rec_IDLE
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：空闲中断回调处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/

void Van_UART_Fun_Rec_IDLE(UART_HandleTypeDef *huart) {
  UART_HandleTypeDef *pUart = sys_ctr_uart.huart;
  UART_HandleTypeDef *pTHD_NTCSAMPUart = THD_NTCSAMP_uart.huart;
  if (huart->Instance == pUart->Instance) {
    Van_UART_Fun_Rec_IDLE_Base(&sys_ctr_uart);
//    Van_Device_Printf(VAN_LOG_NOTICE, "%s %d\n\r", __func__, __LINE__);
  }else if (huart->Instance == pTHD_NTCSAMPUart->Instance) {
    Van_UART_Fun_Rec_IDLE_Base(&THD_NTCSAMP_uart);
//    Van_Device_Printf(VAN_LOG_NOTICE, "%s %d\n\r", __func__, __LINE__);
  }else{
	if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET)) {
	  char	Name[16] = {0};
	  __HAL_UART_CLEAR_IDLEFLAG(huart);
	  find_stm32_Peripherals(huart,Name);
	   Van_Device_Printf(VAN_LOG_NOTICE, "%s %s %d\n\r", Name,__func__);
	}
  }
  return;
}

/****************************************************************
* 函数名:    Van_UART_Fun_Send_DMA
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明： DMA 发送
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Fun_Send_DMA(unsigned char *pdata, unsigned int Length, UART_HandleTypeDef *huart) {
  UART_HandleTypeDef *pUart = sys_ctr_uart.huart;
  UART_HandleTypeDef *pTHD_NTCSAMPUart = THD_NTCSAMP_uart.huart;
  if (huart->Instance == pUart->Instance) {
    
    Van_UART_Fun_Send_DMA_Base(pdata, Length, &sys_ctr_uart);
  }else if (huart->Instance == pTHD_NTCSAMPUart->Instance) {    
    Van_UART_Fun_Send_DMA_Base(pdata, Length, &THD_NTCSAMP_uart);
  } 
  return;
}

/****************************************************************
* 函数名:    Van_UART_Sys_Ctr_Send_type
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：系统命令发送
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Sys_Ctr_Send_type(unsigned char type, unsigned char *pdata, unsigned int Length) {
  struct CmdMessage msg = {0};
  unsigned char sedbuf[SYS_CTR_BUF_LEN];
  unsigned int checksum = 0;
  combine_cmdMessage(msg, VAN_COM_HEADER_FALG, type, VAN_CFG_NONE, VAN_COM_TAIL_FALG);
  msg.len = Length;
  *(unsigned short *)sedbuf = msg.head;
  sedbuf[2] = msg.type;
  sedbuf[3] = msg.cmd;
  *(unsigned short *)(sedbuf + 4) = msg.len;
  for (unsigned int i = 0; i < msg.len; i++) {
    sedbuf[6 + i] = pdata[i];
  }
  checksum = checkSum(&sedbuf[2], msg.len + 4);
  sedbuf[6 + msg.len] = checksum & 0xff; /*结尾标记高位*/
  sedbuf[7 + msg.len] = checksum >> 8;   /*结尾标记低位*/
  if(p_Rep_flag){
  	  unsigned int alllen = msg.len + 8;
	  Van_Device_Printf(VAN_LOG_NOTICE, "ans: len%:d \n\r",alllen);
	  if(p_Rep_data_flag){
		  for(int i=0; i <alllen; i++){
			  Van_Device_Printf(VAN_LOG_NOTICE, "%2x ", sedbuf[i]);
			  if(i%32==31){
				 Van_Device_Printf(VAN_LOG_NOTICE, "\n\r"); 
			  }
		  }
		  Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
	  }
  }
  Van_UART_Fun_Send_DMA(sedbuf, Length + 8, sys_ctr_uart.huart);
}

/****************************************************************
* 函数名:    Van_UART_Sys_Ctr_Send
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：系统命令发送
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Sys_Ctr_Send(unsigned char *pdata, unsigned int Length) {
  struct CmdMessage msg = {0};
  unsigned int checksum = 0;
  combine_cmdMessage(msg, VAN_COM_HEADER_FALG, MSG_SYS_CTR, VAN_CFG_NONE, VAN_COM_TAIL_FALG);
  sedbuf_len = Length + 8;
  msg.len = Length;
  *(unsigned short *)sedbuf = msg.head;
  sedbuf[2] = msg.type;
  sedbuf[3] = msg.cmd;
  *(unsigned short *)(sedbuf + 4) = msg.len;
  for (unsigned int i = 0; i < msg.len; i++) {
    sedbuf[6 + i] = pdata[i];
  }
  checksum = checkSum(&sedbuf[2], msg.len + 4);
  sedbuf[6 + msg.len] = checksum & 0xff; /*结尾标记高位*/
  sedbuf[7 + msg.len] = checksum >> 8;   /*结尾标记低位*/
  if(p_Ans_flag){
  	  unsigned int alllen = msg.len + 8;
	  Van_Device_Printf(VAN_LOG_NOTICE, "ans len: %d \n\r",alllen);
	  if(p_Ans_data_flag){
		  for(int i=0; i <alllen; i++){
			  Van_Device_Printf(VAN_LOG_NOTICE, "%2x ", sedbuf[i]);
			  if(i%32==31){
				 Van_Device_Printf(VAN_LOG_NOTICE, "\n\r"); 
			  }
		  }
		  Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
	  }
  }
  Van_UART_Fun_Send_DMA(sedbuf, sedbuf_len, sys_ctr_uart.huart);
}

/****************************************************************
* 函数名:    Van_UART_Sys_Ctr_SendNoBuf
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：主动上报，不缓存
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Sys_Ctr_SendNoBuf(unsigned char *pdata, unsigned int Length) {
  struct CmdMessage msg = {0};
  unsigned int checksum = 0;
  combine_cmdMessage(msg, VAN_COM_HEADER_FALG, MSG_SYS_CTR, VAN_CFG_NONE, VAN_COM_TAIL_FALG);  
  unsigned char sedbuf[SYS_CTR_BUF_LEN];
  unsigned int sedbuf_len;
  sedbuf_len = Length + 8;
  msg.len = Length;
  *(unsigned short *)sedbuf = msg.head;
  sedbuf[2] = msg.type;
  sedbuf[3] = msg.cmd;
  *(unsigned short *)(sedbuf + 4) = msg.len;
  for (unsigned int i = 0; i < msg.len; i++) {
    sedbuf[6 + i] = pdata[i];
  }
  checksum = checkSum(&sedbuf[2], msg.len + 4);
  sedbuf[6 + msg.len] = checksum & 0xff; /*结尾标记高位*/
  sedbuf[7 + msg.len] = checksum >> 8;   /*结尾标记低位*/
  if(p_Ans_flag_1){
  	  unsigned int alllen = msg.len + 8;
	  Van_Device_Printf(VAN_LOG_NOTICE, "ans len: %d \n\r",alllen);
	  if(p_Ans_data_flag_1){
		  for(int i=0; i <alllen; i++){
			  Van_Device_Printf(VAN_LOG_NOTICE, "%2x ", sedbuf[i]);
			  if(i%32==31){
				 Van_Device_Printf(VAN_LOG_NOTICE, "\n\r"); 
			  }
		  }
		  Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
	  }
  }
  Van_UART_Fun_Send_DMA(sedbuf, sedbuf_len, sys_ctr_uart.huart);
}

/****************************************************************
* 函数名:    Van_UART_Sys_Ctr_Send_Again
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：应答消息重新发送
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Sys_Ctr_Send_Again(void) {
  Van_UART_Fun_Send_DMA(sedbuf, sedbuf_len, sys_ctr_uart.huart);
}

/****************************************************************
* 函数名:    Van_UART_Sys_Ttr_Init
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：系统命令串口挂载
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Sys_Ctr_Init(UART_HandleTypeDef *puart) {
  sys_ctr_uart.huart = puart;
  sys_ctr_uart.usartDMA_RxBuf = sys_ctr_tDMA_RxBuf;
  sys_ctr_uart.usartDMA_TxBuf = sys_ctr_tDMA_TxBuf;
  sys_ctr_uart.usart_RxBuf = sys_ctr_RxBuf;
  sys_ctr_uart.usart_TxBuf = sys_ctr_TxBuf;
  sys_ctr_uart.tx_DMA_len = SYS_CTR_DMA_LEN;
  sys_ctr_uart.rx_DMA_len = SYS_CTR_DMA_LEN;
  sys_ctr_uart.rx_max_len = SYS_CTR_BUF_LEN;
  sys_ctr_uart.tx_max_len = SYS_CTR_BUF_LEN;
  sys_ctr_uart.rx_last = 0;
  sys_ctr_uart.rx_head = 0;
  sys_ctr_uart.tx_last = 0;
  sys_ctr_uart.tx_head = 0;
  sys_ctr_uart.uartError = False;
  memset((void *)sys_ctr_uart.usartDMA_RxBuf, 0, sys_ctr_uart.rx_DMA_len + 4);
  HAL_UART_Receive_DMA(puart, sys_ctr_uart.usartDMA_RxBuf, SYS_CTR_DMA_LEN);
  __HAL_UART_CLEAR_IDLEFLAG(puart);
  __HAL_UART_CLEAR_FLAG(puart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  //	__HAL_UART_CLEAR_OREFLAG(puart);
  __HAL_UART_ENABLE_IT(puart, UART_IT_IDLE);
  __HAL_UART_ENABLE_IT(puart, UART_IT_ERR);
  
  sys_ctr_uart.mutex_dmaCopy = osMutexNew(&Mutex_attr1);
}



/****************************************************************
* 函数名:    Van_UART_Fun_RawdataDeal
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：串口接收元数据处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_RawdataDeal_dispatch(void* pram)
{
	unsigned int anslen = 0;
	P_RawdataMessage pcmd = (P_RawdataMessage)pram;
	RawdataMessage Ans ;
	Ans.fuid = pcmd->fuid;
	Ans.cmd = 0;
	anslen = sizeof(Ans) - sizeof(Ans.data);
	switch(pcmd->cmd){
		case RAW_V1_SET_PID_SEC_OFFSET:
			Ans.cmd = PidSetSectionOffsetDirect((void*)pcmd->data,(void*)Ans.data);
			anslen = 8+sizeof(pidSectionOffset_ST_Def);
			break;
		case RAW_V1_GET_PID_SEC_OFFSET:
			Ans.cmd = PidGetSectionOffsetDirect((void*)pcmd->data,(void*)Ans.data);
			if(Ans.cmd){
				P_pidSectionOffset_ST_Def poffset = (P_pidSectionOffset_ST_Def)Ans.data;
				anslen = 8+sizeof(pidSectionOffset_ST_Def);
			    Van_Device_Printf(VAN_LOG_NOTICE, "mPidNum:%d sectionNum:%d\n\r***", poffset->mPidNum ,poffset->sectionNum );
			}
			break;	
		case RAW_V1_SET_PID_SINGEPIDOFFSET:
			Ans.cmd = PidSetSinglPIDDirect((void*)pcmd->data,(void*)Ans.data);
			break;
		case RAW_V1_GET_PID_SINGEPIDOFFSET:
			Ans.cmd = PidGetSinglPIDDirect((void*)pcmd->data,(void*)Ans.data);
			if(Ans.cmd){
				P_pidSectionOffset_ST_Def poffset = (P_pidSectionOffset_ST_Def)Ans.data;
				anslen += sizeof(P_pidSignlePidPram_ST_Def);
			    Van_Device_Printf(VAN_LOG_NOTICE, "mPidNum:%d sectionNum:%d\n\r***", poffset->mPidNum ,poffset->sectionNum );
			}
			break;	
		case RAW_V1_SET_QUADRATICFIT:
			Ans.cmd = PidSetQuadraticfitDirect((void*)pcmd->data,(void*)Ans.data);
			break;
		case RAW_V1_GET_QUADRATICFIT:
			Ans.cmd = PidGetQuadraticfitDirect((void*)pcmd->data,(void*)Ans.data);
			if(Ans.cmd){
				P_quadraticfit_ST_Def poffset = (P_quadraticfit_ST_Def)Ans.data;
				anslen += sizeof(quadraticfit_ST_Def);
			}
			break;	

			
		case RAW_V1_GET_PID_MULT_PRAM:{
			Ans.cmd = PidMultSectionPramGetDirect((void*)pcmd->data,(void*)Ans.data);
			if(Ans.cmd){
				anslen += 64;
			}
			
		}
			break;
		case RAW_V1_SET_PID_MULT_PRAM:{
			Ans.cmd = PidMultSectionPramSetDirect((void*)pcmd->data,(void*)Ans.data);
			if(Ans.cmd){
				anslen += 64;
			}
			
		}
			break;
	}
	Van_UART_Sys_Ctr_Send_type(MSG_RAW_DATA_1,(void *)&Ans,anslen);
}


/****************************************************************
* 函数名:    Van_UART_Fun_RawdataDeal
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：串口接收元数据处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Fun_RawdataDeal(void) {
static int conter10ms = 0;
  if (sys_ctr_uart.receive_flag)  //如果产生了空闲中断
  {
    sys_ctr_uart.receive_flag = 0;  //清零标记
    unsigned int dataleninRxbuf = (sys_ctr_uart.rx_last + sys_ctr_uart.rx_max_len - sys_ctr_uart.rx_head) % sys_ctr_uart.rx_max_len;
	if(dataleninRxbuf > 64){
		Van_Device_Printf(VAN_LOG_NOTICE, "buffer have:%d\n\r***", dataleninRxbuf);
	}
    if(p_rec_flag){
		Van_Device_Printf(VAN_LOG_NOTICE, "last:%d head:%d buflen:%d  have:%d\n\r***", sys_ctr_uart.rx_last,sys_ctr_uart.rx_head,sys_ctr_uart.rx_max_len,dataleninRxbuf);
		if(p_rec_data_flag){
			for(int i=0; i <dataleninRxbuf; i++){
				Van_Device_Printf(VAN_LOG_NOTICE, "%2x ", sys_ctr_uart.usart_RxBuf[(i+sys_ctr_uart.rx_head)%sys_ctr_uart.rx_max_len]);
				if(i%32==31){
					Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
				}
			}
			Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
		}

	}
    if (dataleninRxbuf > (sizeof(CmdMessage)-4)) {
      pbRawdef raw = {0};
      if (DecodeNew(&sys_ctr_Message, raw.data, sys_ctr_uart.usart_RxBuf, &sys_ctr_uart.rx_head, sys_ctr_uart.rx_last, sys_ctr_uart.rx_max_len)) {
        raw.datalen = sys_ctr_Message.len;     
		if(p_DecodeNew){
			Van_Device_Printf(VAN_LOG_NOTICE, "DecodeNew is ok\n\r");
		}
		if(sys_ctr_Message.type == MSG_RAW_DATA_1){		
			Van_RawdataDeal_dispatch(raw.data);
			
		}else{	
	    	pb_CtrlProto_v1_decode(raw.data, raw.datalen);
		}
      }
    }
	dataleninRxbuf = (sys_ctr_uart.rx_last + sys_ctr_uart.rx_max_len - sys_ctr_uart.rx_head) % sys_ctr_uart.rx_max_len;
	if(dataleninRxbuf > (sizeof(CmdMessage)-4)) {
		sys_ctr_uart.receive_flag = 1;  //缓冲区存在数据，接续解析
	}
    
  }
  if(resetSysUart){
	resetSysUart = 0;
	sys_ctr_uart.uartError = True;
  }
  if( sys_ctr_uart.uartError){
	Van_Device_Printf(VAN_LOG_NOTICE, "reinit sysUart\n\r");
  	HAL_UART_DMAStop(sys_ctr_uart.huart);
	sys_ctr_uart.uartError = False;		
    osDelay(100U);
    if(sysinf.devdesc.deveice_type == deviceType_deviceETA_004){	
		MX_UART4_Init();
	}else{	
 		MX_UART7_Init();
	}	
	memset((void *)sys_ctr_uart.usartDMA_RxBuf, 0, sys_ctr_uart.rx_DMA_len + 4);
	HAL_UART_Receive_DMA(sys_ctr_uart.huart, sys_ctr_uart.usartDMA_RxBuf, SYS_CTR_DMA_LEN);
	__HAL_UART_CLEAR_IDLEFLAG(sys_ctr_uart.huart);
	__HAL_UART_ENABLE_IT(sys_ctr_uart.huart, UART_IT_IDLE);
	__HAL_UART_ENABLE_IT(sys_ctr_uart.huart, UART_IT_ERR);
  }
  conter10ms++;
  if(conter10ms==200){
	  if ((__HAL_UART_GET_FLAG(sys_ctr_uart.huart, UART_FLAG_ORE) != RESET)||
		(__HAL_UART_GET_FLAG(sys_ctr_uart.huart, UART_FLAG_NE) != RESET)||
		(__HAL_UART_GET_FLAG(sys_ctr_uart.huart, UART_FLAG_FE) != RESET)||
		(__HAL_UART_GET_FLAG(sys_ctr_uart.huart, UART_FLAG_PE) != RESET)){
				Van_Device_Printf(VAN_LOG_NOTICE, "sysUart error\n\r");
				Van_Device_Printf(VAN_LOG_NOTICE, "ISR:%8x \n\r",sys_ctr_uart.huart->Instance->ISR); 
			    if(sysinf.devdesc.deveice_type == deviceType_deviceETA_004){	
					MX_UART4_Init();
				}else{	
			 		MX_UART7_Init();
				}	 
  				HAL_UART_DMAStop(sys_ctr_uart.huart);
				memset((void *)sys_ctr_uart.usartDMA_RxBuf, 0, sys_ctr_uart.rx_DMA_len + 4);
				HAL_UART_Receive_DMA(sys_ctr_uart.huart, sys_ctr_uart.usartDMA_RxBuf, SYS_CTR_DMA_LEN);
				__HAL_UART_CLEAR_IDLEFLAG(sys_ctr_uart.huart);
				__HAL_UART_ENABLE_IT(sys_ctr_uart.huart, UART_IT_IDLE);
				__HAL_UART_ENABLE_IT(sys_ctr_uart.huart, UART_IT_ERR);				  
		}

  }

}

/****************************************************************
* 函数名:    Van_UART_Fun_RawdataDeal
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：串口接收元数据处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_UART_Sys_Ctr_SendTest(void) {
  unsigned char sendtestbuf[128] = {0};
  for (int i = 0; i < 100; i++) {
    sendtestbuf[i] = '0' + i % 10;
  }

  Van_UART_Sys_Ctr_Send(sendtestbuf, 100);
  Van_UART_Sys_Ctr_Send((unsigned char*)"\n\r", 2);
}

/****************************************************************
* 函数名:    Van_UART_Sys_Get_Debuging  
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：获取调试信息
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/

void Van_UART_Sys_Get_Debuging(void) {
  DMA_Base_Registers  *regs_dma  = (DMA_Base_Registers *)hdma_uart7_rx.StreamBaseAddress;
  BDMA_Channel_TypeDef *dma_instance =     (BDMA_Channel_TypeDef *)hdma_uart7_rx.Instance;
  Van_Device_Printf(VAN_LOG_NOTICE, "ISR:%8x  IFCR:%8x \n\r",regs_dma->ISR,regs_dma->IFCR);
  Van_Device_Printf(VAN_LOG_NOTICE, "CCR:%8x  CNDTR:%8x  CPAR:%8x  CM0AR:%8x  CM1AR:%8x \n\r",
  		dma_instance->CCR,dma_instance->CNDTR,dma_instance->CPAR,dma_instance->CM0AR,dma_instance->CM1AR);  
  Van_Device_Printf(VAN_LOG_NOTICE, "huart7 ISR:%8x \n\r",huart7.Instance->ISR);   
}

#if defined(DEVICETYPE_IS_ATCG)

/****************************************************************
* 函数名:    Van_THD_NTCSAMP_Fun_RawdataDeal
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：串口接收元数据处理
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_THD_NTCSAMP_Fun_RawdataDeal(void) {
  if (THD_NTCSAMP_uart.receive_flag)  //如果产生了空闲中断
  {
    THD_NTCSAMP_uart.receive_flag = 0;  //清零标记
    unsigned int dataleninRxbuf = (THD_NTCSAMP_uart.rx_last + THD_NTCSAMP_uart.rx_max_len - THD_NTCSAMP_uart.rx_head) % THD_NTCSAMP_uart.rx_max_len;
    if(p_rec_flag){
		Van_Device_Printf(VAN_LOG_NOTICE, "last:%d head:%d buflen:%d  have:%d\n\r***", THD_NTCSAMP_uart.rx_last,THD_NTCSAMP_uart.rx_head,THD_NTCSAMP_uart.rx_max_len,dataleninRxbuf);
		if(p_rec_data_flag){
			for(int i=0; i <dataleninRxbuf; i++){
				Van_Device_Printf(VAN_LOG_NOTICE, "%2x ", THD_NTCSAMP_uart.usart_RxBuf[(i+THD_NTCSAMP_uart.rx_head)%THD_NTCSAMP_uart.rx_max_len]);
				if(i%32==31){
					Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
				}
			}
			Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
		}

	}
    if (dataleninRxbuf >= (sizeof(RTUMessage))) {
			
		unsigned int msgLen = DecodeModbud_RTU(THD_NTCSAMP_Message, THD_NTCSAMP_uart.usart_RxBuf, &THD_NTCSAMP_uart.rx_head, THD_NTCSAMP_uart.rx_last, THD_NTCSAMP_uart.rx_max_len);
      if (msgLen) {	  	
//	  		for(int i=0;i<msgLen;i++){
//				Van_Device_Printf(VAN_LOG_NOTICE, "%2x ",THD_NTCSAMP_Message[i]);
//				if( i%32 == 31){
//					Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
//				}	
//			}
//			Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
		
      }
    }
	dataleninRxbuf = (THD_NTCSAMP_uart.rx_last + THD_NTCSAMP_uart.rx_max_len - THD_NTCSAMP_uart.rx_head) % THD_NTCSAMP_uart.rx_max_len;
	if(dataleninRxbuf >= (sizeof(RTUMessage))) {
		THD_NTCSAMP_uart.receive_flag = 1;  //缓冲区存在数据，接续解析
	}
    
  }
  if( THD_NTCSAMP_uart.uartError){
	THD_NTCSAMP_uart.uartError = False;		
    osDelay(100U);
  	MX_UART5_Init();
   Van_THD_NTCSAMP_Init(THD_NTCSAMP_uart.huart);
	 Van_Device_Printf(VAN_LOG_ERROR, "sys_ctr_uart reset\n\r");	
    __HAL_DMA_DISABLE(THD_NTCSAMP_uart.huart->hdmarx);
    HAL_UART_Receive_DMA((UART_HandleTypeDef *)&huart5, THD_NTCSAMP_uart.usartDMA_RxBuf, RECEIVELEN);
    __HAL_UART_CLEAR_IDLEFLAG((UART_HandleTypeDef *)&huart5);
  	__HAL_UART_CLEAR_OREFLAG((UART_HandleTypeDef *)&huart5);
		__HAL_UART_ENABLE_IT((UART_HandleTypeDef *)&huart5, UART_IT_IDLE);
		__HAL_UART_ENABLE_IT((UART_HandleTypeDef *)&huart5, UART_IT_ERR);
  }
}


/****************************************************************
* 函数名:    Van_THD_NTCSAMP_Ctr_Init
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：系统命令串口挂载
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_THD_NTCSAMP_Init(UART_HandleTypeDef *puart) {
  THD_NTCSAMP_uart.huart = puart;
  THD_NTCSAMP_uart.usartDMA_RxBuf = THD_NTCSAMP_tDMA_RxBuf;
  THD_NTCSAMP_uart.usartDMA_TxBuf = THD_NTCSAMP_tDMA_TxBuf;
  THD_NTCSAMP_uart.usart_RxBuf = THD_NTCSAMP_RxBuf;
  THD_NTCSAMP_uart.usart_TxBuf = THD_NTCSAMP_TxBuf;
  THD_NTCSAMP_uart.tx_DMA_len = THD_NTCSAMP_DMA_LEN;
  THD_NTCSAMP_uart.rx_DMA_len = THD_NTCSAMP_DMA_LEN;
  THD_NTCSAMP_uart.rx_max_len = THD_NTCSAMP_BUF_LEN;
  THD_NTCSAMP_uart.tx_max_len = THD_NTCSAMP_BUF_LEN;
  THD_NTCSAMP_uart.rx_last = 0;
  THD_NTCSAMP_uart.rx_head = 0;
  THD_NTCSAMP_uart.tx_last = 0;
  THD_NTCSAMP_uart.tx_head = 0;
  THD_NTCSAMP_uart.uartError = False;
  memset((void *)THD_NTCSAMP_uart.usartDMA_RxBuf, 0, THD_NTCSAMP_uart.rx_DMA_len + 4);
  HAL_UART_Receive_DMA(puart, THD_NTCSAMP_uart.usartDMA_RxBuf, THD_NTCSAMP_DMA_LEN);
  __HAL_UART_CLEAR_IDLEFLAG(puart);
  //	__HAL_UART_CLEAR_OREFLAG(puart);
  __HAL_UART_ENABLE_IT(puart, UART_IT_IDLE);
  __HAL_UART_ENABLE_IT(puart, UART_IT_ERR);
  
  THD_NTCSAMP_uart.mutex_dmaCopy = osMutexNew(&Mutex_attr1);
}

/****************************************************************
* 函数名:    Van_UART_Sys_Ctr_Send
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：系统命令发送
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_THD_NTCSAMP_Send(const unsigned char *pdata, unsigned int Length) {
	unsigned char sedbuf[SYS_CTR_BUF_LEN];
	unsigned int i = 0;
	for (; i < Length; i++) {
	  sedbuf[i] = pdata[i];
	}
	if( THD_NTCSAMP_uart.uartError) return;
//	sedbuf[i++] = checksum & 0xff; /*结尾标记高位*/
//	sedbuf[i++] = checksum >> 8;   /*结尾标记低位*/
//	if(1){
//		unsigned int alllen = msg.len + 8;
//		Van_Device_Printf(VAN_LOG_NOTICE, "NTC: len%:d \n\r",alllen);
//		if(1){
//			for(int i=0; i <alllen; i++){
//				Van_Device_Printf(VAN_LOG_NOTICE, "%2x ", sedbuf[i]);
//				if(i%32==31){
//				   Van_Device_Printf(VAN_LOG_NOTICE, "\n\r"); 
//				}
//			}
//			Van_Device_Printf(VAN_LOG_NOTICE, "\n\r");
//		}
//	}
	Van_UART_Fun_Send_DMA(sedbuf, Length, THD_NTCSAMP_uart.huart);

} 
#endif


/* USER CODE END Private defines */
