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

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

/* USER CODE BEGIN Private defines */

#define RECEIVELEN		   1024
#define USART_DMA_SENDING  1 //发送未完成
#define USART_DMA_SENDOVER 0 //发送完成
typedef struct
{
	unsigned int	 receive_flag;				 //空闲接收标记
	unsigned int	 dmaSend_flag;				 //发送完成标记
	unsigned short rx_len;						 //接收长度
	unsigned short tx_len;						 //接收长度
	unsigned char	 usartDMA_rxBuf[RECEIVELEN];	 // DMA接收缓存
	unsigned char	 usartDMA_TXBuf[RECEIVELEN];	 // DMA接收缓存
	unsigned char	 usartDMA_TXtempBuf[RECEIVELEN]; // DMA接收缓存
} USART_RECEIVETYPE;

USART_RECEIVETYPE debug_rec_type1;

UART_HandleTypeDef *debugUart;

// DMA发送完成中断回调函数
void Van_Debug_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	__HAL_DMA_DISABLE(huart->hdmatx);
	if (debug_rec_type1.tx_len)
	{
		memcpy(debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.usartDMA_TXtempBuf,
			   debug_rec_type1.tx_len);
		debug_rec_type1.dmaSend_flag = USART_DMA_SENDING;
		HAL_UART_Transmit_DMA((UART_HandleTypeDef *)debugUart,
							  debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.tx_len);

		debug_rec_type1.tx_len = 0;
	}
	else
	{
		debug_rec_type1.dmaSend_flag = USART_DMA_SENDOVER;
	}
}
void Van_Debug_UART_Send_DMA(unsigned char *pdata, unsigned int Length)
{
	/*超长不拷贝*/
	if (debug_rec_type1.tx_len + Length < RECEIVELEN)
	{
		memcpy(debug_rec_type1.usartDMA_TXtempBuf + debug_rec_type1.tx_len, pdata,
			   Length);
		debug_rec_type1.tx_len += Length;
	}
	if (debug_rec_type1.dmaSend_flag == USART_DMA_SENDOVER)
	{
		memcpy(debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.usartDMA_TXtempBuf,
			   debug_rec_type1.tx_len);
		debug_rec_type1.dmaSend_flag = USART_DMA_SENDING;
		//  	HAL_UART_Transmit_DMA(&huart1, debug_rec_type1.usartDMA_TXBuf,
		//  debug_rec_type1.tx_len);
		HAL_UART_Transmit_DMA((UART_HandleTypeDef *)debugUart,
							  debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.tx_len);
		debug_rec_type1.tx_len = 0;
	}
}

//串口接收空闲中断
void Van_Debug_UART_Rec_IDLE(UART_HandleTypeDef *huart)
{
	uint32_t temp;
	if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
	{
		__HAL_UART_CLEAR_IDLEFLAG(huart);
//		UART_EndRxTransfer(huart);
//		HAL_UART_DMA_Stop(huart);
//		DMA_HandleTypeDef *dma = ;
		temp					= __HAL_DMA_GET_COUNTER(huart->hdmarx);
		debug_rec_type1.rx_len		= RECEIVELEN - temp;
		debug_rec_type1.receive_flag = 1;
		addinputtocmdbuf(debug_rec_type1.usartDMA_rxBuf, debug_rec_type1.rx_len);
		HAL_UART_Receive_DMA(huart, debug_rec_type1.usartDMA_rxBuf, RECEIVELEN);
//		if (debug_rec_type1.dmaSend_flag == USART_DMA_SENDING)
//		{
//			debug_rec_type1.dmaSend_flag = USART_DMA_SENDOVER;
//		}
	}
}
/*串口终端后处理*/
void Van_Debug_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
	DMA_HandleTypeDef *pdma;
	pdma = huart->hdmatx;
	__HAL_UART_CLEAR_FLAG(huart,UART_FLAG_RXNE);
	if (pdma->ErrorCode)
	{
		__HAL_DMA_DISABLE(huart->hdmatx);
		if (debug_rec_type1.tx_len)
		{
			memcpy(debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.usartDMA_TXtempBuf,
				   debug_rec_type1.tx_len);
			debug_rec_type1.dmaSend_flag = USART_DMA_SENDING;
			//	   HAL_UART_Transmit_DMA(&huart1, debug_rec_type1.usartDMA_TXBuf,
			//debug_rec_type1.tx_len);
			HAL_UART_Transmit_DMA(
				(UART_HandleTypeDef *)debugUart,
				(unsigned char *)"Van_Debug_UART_ErrorCallback hdmaTx\n\r",
				sizeof("Van_Debug_UART_ErrorCallback\n\r"));
			//			debug_rec_type1.tx_len = 0;
		}
		else
		{
			debug_rec_type1.dmaSend_flag = USART_DMA_SENDOVER;
		}
	}

	pdma = huart->hdmarx;
	if (pdma->ErrorCode)
	{
		__HAL_DMA_DISABLE(huart->hdmarx);
		HAL_UART_Receive_DMA((UART_HandleTypeDef *)debugUart,
							 debug_rec_type1.usartDMA_rxBuf, RECEIVELEN);
		__HAL_UART_ENABLE_IT((UART_HandleTypeDef *)debugUart,
							 UART_IT_IDLE);

		HAL_UART_Transmit_DMA(
			(UART_HandleTypeDef *)debugUart,
			(unsigned char *)"Van_Debug_UART_ErrorCallback hdmaRx\n\r",
			sizeof("Van_Debug_UART_ErrorCallback\n\r"));
	}
}

/****************************************************************
* 函数名:    Van_Debug_UART_Init
* 创建时间:  2022/02/18
* 创建人:    范义东
* 函数说明：配置串口，初始化，开中断，在系统初始化里面进行
* 输入参数: 无

* 输出参数:
* 返回值 :   void
****************************************************************/
void Van_Debug_UART_Init(UART_HandleTypeDef * puart)
{
	debugUart = puart;
	HAL_UART_Receive_DMA(debugUart, debug_rec_type1.usartDMA_rxBuf,
								 RECEIVELEN);
	__HAL_UART_ENABLE_IT(debugUart, UART_IT_IDLE);
}

void Van_Debug_UART1_test(void)
{
	if (debug_rec_type1.receive_flag) //如果产生了空闲中断
	{
		debug_rec_type1.receive_flag = 0; //清零标记
		decodedebugfunction();
		decodedebugvariable();
	}
}

/* USER CODE END Private defines */
