usart(4075).c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file usart.c
  5. * @brief This file provides code for the configuration
  6. * of the USART instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usart.h"
  22. /* USER CODE BEGIN 0 */
  23. char g_usart3_rx_buf[USART3_REC_LEN];
  24. char usart3_rx_buf2[USART3_REC_LEN];
  25. char g_usart3_rx_sta = 0; // 接收完成标志 0x33:接收完成
  26. uint8_t g_usart3_rx_len = 0;
  27. /* USER CODE END 0 */
  28. UART_HandleTypeDef huart1;
  29. UART_HandleTypeDef huart3;
  30. DMA_HandleTypeDef hdma_usart3_tx;
  31. DMA_HandleTypeDef hdma_usart3_rx;
  32. /* USART1 init function */
  33. void MX_USART1_UART_Init(void)
  34. {
  35. /* USER CODE BEGIN USART1_Init 0 */
  36. /* USER CODE END USART1_Init 0 */
  37. /* USER CODE BEGIN USART1_Init 1 */
  38. /* USER CODE END USART1_Init 1 */
  39. huart1.Instance = USART1;
  40. huart1.Init.BaudRate = 115200;
  41. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  42. huart1.Init.StopBits = UART_STOPBITS_1;
  43. huart1.Init.Parity = UART_PARITY_NONE;
  44. huart1.Init.Mode = UART_MODE_TX_RX;
  45. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  46. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  47. if (HAL_UART_Init(&huart1) != HAL_OK)
  48. {
  49. Error_Handler();
  50. }
  51. /* USER CODE BEGIN USART1_Init 2 */
  52. /* USER CODE END USART1_Init 2 */
  53. }
  54. /* USART3 init function */
  55. void MX_USART3_UART_Init(void)
  56. {
  57. /* USER CODE BEGIN USART3_Init 0 */
  58. /* USER CODE END USART3_Init 0 */
  59. /* USER CODE BEGIN USART3_Init 1 */
  60. /* USER CODE END USART3_Init 1 */
  61. huart3.Instance = USART3;
  62. huart3.Init.BaudRate = 115200;
  63. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  64. huart3.Init.StopBits = UART_STOPBITS_1;
  65. huart3.Init.Parity = UART_PARITY_NONE;
  66. huart3.Init.Mode = UART_MODE_TX_RX;
  67. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  68. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  69. if (HAL_UART_Init(&huart3) != HAL_OK)
  70. {
  71. Error_Handler();
  72. }
  73. /* USER CODE BEGIN USART3_Init 2 */
  74. // __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
  75. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  76. // __HAL_UART_ENABLE_IT(&huart3, UART_IT_TXE);
  77. /* USER CODE END USART3_Init 2 */
  78. }
  79. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  80. {
  81. GPIO_InitTypeDef GPIO_InitStruct = {0};
  82. if(uartHandle->Instance==USART1)
  83. {
  84. /* USER CODE BEGIN USART1_MspInit 0 */
  85. /* USER CODE END USART1_MspInit 0 */
  86. /* USART1 clock enable */
  87. __HAL_RCC_USART1_CLK_ENABLE();
  88. __HAL_RCC_GPIOA_CLK_ENABLE();
  89. /**USART1 GPIO Configuration
  90. PA9 ------> USART1_TX
  91. PA10 ------> USART1_RX
  92. */
  93. GPIO_InitStruct.Pin = GPIO_PIN_9;
  94. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  95. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  96. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  97. GPIO_InitStruct.Pin = GPIO_PIN_10;
  98. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  99. GPIO_InitStruct.Pull = GPIO_NOPULL;
  100. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  101. /* USART1 interrupt Init */
  102. HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
  103. HAL_NVIC_EnableIRQ(USART1_IRQn);
  104. /* USER CODE BEGIN USART1_MspInit 1 */
  105. /* USER CODE END USART1_MspInit 1 */
  106. }
  107. else if(uartHandle->Instance==USART3)
  108. {
  109. /* USER CODE BEGIN USART3_MspInit 0 */
  110. /* USER CODE END USART3_MspInit 0 */
  111. /* USART3 clock enable */
  112. __HAL_RCC_USART3_CLK_ENABLE();
  113. __HAL_RCC_GPIOD_CLK_ENABLE();
  114. /**USART3 GPIO Configuration
  115. PD8 ------> USART3_TX
  116. PD9 ------> USART3_RX
  117. */
  118. GPIO_InitStruct.Pin = GPIO_PIN_8;
  119. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  120. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  121. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  122. GPIO_InitStruct.Pin = GPIO_PIN_9;
  123. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  124. GPIO_InitStruct.Pull = GPIO_NOPULL;
  125. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  126. __HAL_AFIO_REMAP_USART3_ENABLE();
  127. /* USART3 DMA Init */
  128. /* USART3_TX Init */
  129. hdma_usart3_tx.Instance = DMA1_Channel2;
  130. hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  131. hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  132. hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
  133. hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  134. hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  135. hdma_usart3_tx.Init.Mode = DMA_NORMAL;
  136. hdma_usart3_tx.Init.Priority = DMA_PRIORITY_HIGH;
  137. if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141. __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
  142. /* USART3_RX Init */
  143. hdma_usart3_rx.Instance = DMA1_Channel3;
  144. hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  145. hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  146. hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
  147. hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  148. hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  149. hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;
  150. hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
  151. if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
  152. {
  153. Error_Handler();
  154. }
  155. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
  156. /* USART3 interrupt Init */
  157. HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
  158. HAL_NVIC_EnableIRQ(USART3_IRQn);
  159. /* USER CODE BEGIN USART3_MspInit 1 */
  160. /* USER CODE END USART3_MspInit 1 */
  161. }
  162. }
  163. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  164. {
  165. if(uartHandle->Instance==USART1)
  166. {
  167. /* USER CODE BEGIN USART1_MspDeInit 0 */
  168. /* USER CODE END USART1_MspDeInit 0 */
  169. /* Peripheral clock disable */
  170. __HAL_RCC_USART1_CLK_DISABLE();
  171. /**USART1 GPIO Configuration
  172. PA9 ------> USART1_TX
  173. PA10 ------> USART1_RX
  174. */
  175. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  176. /* USART1 interrupt Deinit */
  177. HAL_NVIC_DisableIRQ(USART1_IRQn);
  178. /* USER CODE BEGIN USART1_MspDeInit 1 */
  179. /* USER CODE END USART1_MspDeInit 1 */
  180. }
  181. else if(uartHandle->Instance==USART3)
  182. {
  183. /* USER CODE BEGIN USART3_MspDeInit 0 */
  184. /* USER CODE END USART3_MspDeInit 0 */
  185. /* Peripheral clock disable */
  186. __HAL_RCC_USART3_CLK_DISABLE();
  187. /**USART3 GPIO Configuration
  188. PD8 ------> USART3_TX
  189. PD9 ------> USART3_RX
  190. */
  191. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8|GPIO_PIN_9);
  192. /* USART3 DMA DeInit */
  193. HAL_DMA_DeInit(uartHandle->hdmatx);
  194. HAL_DMA_DeInit(uartHandle->hdmarx);
  195. /* USART3 interrupt Deinit */
  196. HAL_NVIC_DisableIRQ(USART3_IRQn);
  197. /* USER CODE BEGIN USART3_MspDeInit 1 */
  198. /* USER CODE END USART3_MspDeInit 1 */
  199. }
  200. }
  201. /* USER CODE BEGIN 1 */
  202. //void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  203. //{
  204. // HAL_StatusTypeDef flag = 0;
  205. // if(huart->Instance == USART3){
  206. // HAL_UARTEx_ReceiveToIdle_IT(huart,(uint8_t *)g_usart3_rx_buf,USART3_REC_LEN);
  207. // if(flag == HAL_OK){
  208. // g_usart3_rx_sta = 0x33;
  209. // }
  210. //
  211. // }
  212. //
  213. //}
  214. //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  215. //{
  216. // if(huart->Instance == USART3)
  217. // {
  218. //
  219. // }
  220. //}
  221. //void HAL_UART_IdleCpltCallback(UART_HandleTypeDef *huart)
  222. //{
  223. // if(huart->Instance == USART3)
  224. // {
  225. // RxFlag = 1;//置位接收完成标志
  226. //
  227. // }
  228. //}
  229. /* USER CODE END 1 */