van_bsp_debug_uart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /******************************************************************
  2. * Copyright (C) 2023 - 2023 OneChip, Inc. All rights reserved.
  3. * File name: van_bsp_debug_uart.c
  4. * Author: fanyidong Date:2020.11.21
  5. * Description: STM32调试串口接口函数
  6. * Others:
  7. * History:
  8. 1. Date:
  9. Author:
  10. Modification:
  11. *****************************************************************/
  12. #include <string.h>
  13. #include "van_bsp_debug_uart.h"
  14. /* USER CODE BEGIN Private defines */
  15. #define RECEIVELEN 1024
  16. #define USART_DMA_SENDING 1 //发送未完成
  17. #define USART_DMA_SENDOVER 0 //发送完成
  18. typedef struct
  19. {
  20. unsigned int receive_flag; //空闲接收标记
  21. unsigned int dmaSend_flag; //发送完成标记
  22. unsigned short rx_len; //接收长度
  23. unsigned short tx_len; //接收长度
  24. unsigned char usartDMA_rxBuf[RECEIVELEN]; // DMA接收缓存
  25. unsigned char usartDMA_TXBuf[RECEIVELEN]; // DMA接收缓存
  26. unsigned char usartDMA_TXtempBuf[RECEIVELEN]; // DMA接收缓存
  27. } USART_RECEIVETYPE;
  28. USART_RECEIVETYPE debug_rec_type1;
  29. UART_HandleTypeDef *debugUart;
  30. // DMA发送完成中断回调函数
  31. void Van_Debug_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  32. {
  33. __HAL_DMA_DISABLE(huart->hdmatx);
  34. if (debug_rec_type1.tx_len)
  35. {
  36. memcpy(debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.usartDMA_TXtempBuf,
  37. debug_rec_type1.tx_len);
  38. debug_rec_type1.dmaSend_flag = USART_DMA_SENDING;
  39. HAL_UART_Transmit_DMA((UART_HandleTypeDef *)debugUart,
  40. debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.tx_len);
  41. debug_rec_type1.tx_len = 0;
  42. }
  43. else
  44. {
  45. debug_rec_type1.dmaSend_flag = USART_DMA_SENDOVER;
  46. }
  47. }
  48. void Van_Debug_UART_Send_DMA(unsigned char *pdata, unsigned int Length)
  49. {
  50. /*超长不拷贝*/
  51. if (debug_rec_type1.tx_len + Length < RECEIVELEN)
  52. {
  53. memcpy(debug_rec_type1.usartDMA_TXtempBuf + debug_rec_type1.tx_len, pdata,
  54. Length);
  55. debug_rec_type1.tx_len += Length;
  56. }
  57. if (debug_rec_type1.dmaSend_flag == USART_DMA_SENDOVER)
  58. {
  59. memcpy(debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.usartDMA_TXtempBuf,
  60. debug_rec_type1.tx_len);
  61. debug_rec_type1.dmaSend_flag = USART_DMA_SENDING;
  62. // HAL_UART_Transmit_DMA(&huart1, debug_rec_type1.usartDMA_TXBuf,
  63. // debug_rec_type1.tx_len);
  64. HAL_UART_Transmit_DMA((UART_HandleTypeDef *)debugUart,
  65. debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.tx_len);
  66. debug_rec_type1.tx_len = 0;
  67. }
  68. }
  69. //串口接收空闲中断
  70. void Van_Debug_UART_Rec_IDLE(UART_HandleTypeDef *huart)
  71. {
  72. uint32_t temp;
  73. if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
  74. {
  75. __HAL_UART_CLEAR_IDLEFLAG(huart);
  76. // UART_EndRxTransfer(huart);
  77. // HAL_UART_DMA_Stop(huart);
  78. // DMA_HandleTypeDef *dma = ;
  79. temp = __HAL_DMA_GET_COUNTER(huart->hdmarx);
  80. debug_rec_type1.rx_len = RECEIVELEN - temp;
  81. debug_rec_type1.receive_flag = 1;
  82. addinputtocmdbuf(debug_rec_type1.usartDMA_rxBuf, debug_rec_type1.rx_len);
  83. HAL_UART_Receive_DMA(huart, debug_rec_type1.usartDMA_rxBuf, RECEIVELEN);
  84. // if (debug_rec_type1.dmaSend_flag == USART_DMA_SENDING)
  85. // {
  86. // debug_rec_type1.dmaSend_flag = USART_DMA_SENDOVER;
  87. // }
  88. }
  89. }
  90. /*串口终端后处理*/
  91. void Van_Debug_UART_ErrorCallback(UART_HandleTypeDef *huart)
  92. {
  93. DMA_HandleTypeDef *pdma;
  94. pdma = huart->hdmatx;
  95. __HAL_UART_CLEAR_FLAG(huart,UART_FLAG_RXNE);
  96. if (pdma->ErrorCode)
  97. {
  98. __HAL_DMA_DISABLE(huart->hdmatx);
  99. if (debug_rec_type1.tx_len)
  100. {
  101. memcpy(debug_rec_type1.usartDMA_TXBuf, debug_rec_type1.usartDMA_TXtempBuf,
  102. debug_rec_type1.tx_len);
  103. debug_rec_type1.dmaSend_flag = USART_DMA_SENDING;
  104. // HAL_UART_Transmit_DMA(&huart1, debug_rec_type1.usartDMA_TXBuf,
  105. //debug_rec_type1.tx_len);
  106. HAL_UART_Transmit_DMA(
  107. (UART_HandleTypeDef *)debugUart,
  108. (unsigned char *)"Van_Debug_UART_ErrorCallback hdmaTx\n\r",
  109. sizeof("Van_Debug_UART_ErrorCallback\n\r"));
  110. // debug_rec_type1.tx_len = 0;
  111. }
  112. else
  113. {
  114. debug_rec_type1.dmaSend_flag = USART_DMA_SENDOVER;
  115. }
  116. }
  117. pdma = huart->hdmarx;
  118. if (pdma->ErrorCode)
  119. {
  120. __HAL_DMA_DISABLE(huart->hdmarx);
  121. HAL_UART_Receive_DMA((UART_HandleTypeDef *)debugUart,
  122. debug_rec_type1.usartDMA_rxBuf, RECEIVELEN);
  123. __HAL_UART_ENABLE_IT((UART_HandleTypeDef *)debugUart,
  124. UART_IT_IDLE);
  125. HAL_UART_Transmit_DMA(
  126. (UART_HandleTypeDef *)debugUart,
  127. (unsigned char *)"Van_Debug_UART_ErrorCallback hdmaRx\n\r",
  128. sizeof("Van_Debug_UART_ErrorCallback\n\r"));
  129. }
  130. }
  131. /****************************************************************
  132. * 函数名: Van_Debug_UART_Init
  133. * 创建时间: 2022/02/18
  134. * 创建人: 范义东
  135. * 函数说明:配置串口,初始化,开中断,在系统初始化里面进行
  136. * 输入参数: 无
  137. * 输出参数:
  138. * 返回值 : void
  139. ****************************************************************/
  140. void Van_Debug_UART_Init(UART_HandleTypeDef * puart)
  141. {
  142. debugUart = puart;
  143. HAL_UART_Receive_DMA(debugUart, debug_rec_type1.usartDMA_rxBuf,
  144. RECEIVELEN);
  145. __HAL_UART_ENABLE_IT(debugUart, UART_IT_IDLE);
  146. }
  147. void Van_Debug_UART1_test(void)
  148. {
  149. if (debug_rec_type1.receive_flag) //如果产生了空闲中断
  150. {
  151. debug_rec_type1.receive_flag = 0; //清零标记
  152. decodedebugfunction();
  153. decodedebugvariable();
  154. }
  155. }
  156. /* USER CODE END Private defines */