usart(2025).c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 g_usart3_rx_sta = 0; // 接收完成标志 0x33:接收完成
  25. uint8_t g_usart3_rx_len = 0;
  26. char g_usart2_rx_buf[USART2_REC_LEN];
  27. char g_usart2_rx_sta = 0;
  28. uint8_t g_usart2_rx_len = 0;
  29. /* USER CODE END 0 */
  30. UART_HandleTypeDef huart1;
  31. UART_HandleTypeDef huart2;
  32. UART_HandleTypeDef huart3;
  33. DMA_HandleTypeDef hdma_usart2_rx;
  34. DMA_HandleTypeDef hdma_usart3_tx;
  35. DMA_HandleTypeDef hdma_usart3_rx;
  36. /* USART1 init function */
  37. void MX_USART1_UART_Init(void)
  38. {
  39. /* USER CODE BEGIN USART1_Init 0 */
  40. /* USER CODE END USART1_Init 0 */
  41. /* USER CODE BEGIN USART1_Init 1 */
  42. /* USER CODE END USART1_Init 1 */
  43. huart1.Instance = USART1;
  44. huart1.Init.BaudRate = 115200;
  45. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  46. huart1.Init.StopBits = UART_STOPBITS_1;
  47. huart1.Init.Parity = UART_PARITY_NONE;
  48. huart1.Init.Mode = UART_MODE_TX_RX;
  49. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  50. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  51. if (HAL_UART_Init(&huart1) != HAL_OK)
  52. {
  53. Error_Handler();
  54. }
  55. /* USER CODE BEGIN USART1_Init 2 */
  56. /* USER CODE END USART1_Init 2 */
  57. }
  58. /* USART2 init function */
  59. void MX_USART2_UART_Init(void)
  60. {
  61. /* USER CODE BEGIN USART2_Init 0 */
  62. /* USER CODE END USART2_Init 0 */
  63. /* USER CODE BEGIN USART2_Init 1 */
  64. /* USER CODE END USART2_Init 1 */
  65. huart2.Instance = USART2;
  66. huart2.Init.BaudRate = 115200;
  67. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  68. huart2.Init.StopBits = UART_STOPBITS_1;
  69. huart2.Init.Parity = UART_PARITY_NONE;
  70. huart2.Init.Mode = UART_MODE_TX_RX;
  71. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  72. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  73. if (HAL_UART_Init(&huart2) != HAL_OK)
  74. {
  75. Error_Handler();
  76. }
  77. /* USER CODE BEGIN USART2_Init 2 */
  78. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
  79. /* USER CODE END USART2_Init 2 */
  80. }
  81. /* USART3 init function */
  82. void MX_USART3_UART_Init(void)
  83. {
  84. /* USER CODE BEGIN USART3_Init 0 */
  85. /* USER CODE END USART3_Init 0 */
  86. /* USER CODE BEGIN USART3_Init 1 */
  87. /* USER CODE END USART3_Init 1 */
  88. huart3.Instance = USART3;
  89. huart3.Init.BaudRate = 115200;
  90. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  91. huart3.Init.StopBits = UART_STOPBITS_1;
  92. huart3.Init.Parity = UART_PARITY_NONE;
  93. huart3.Init.Mode = UART_MODE_TX_RX;
  94. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  95. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  96. if (HAL_UART_Init(&huart3) != HAL_OK)
  97. {
  98. Error_Handler();
  99. }
  100. /* USER CODE BEGIN USART3_Init 2 */
  101. // __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
  102. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  103. // __HAL_UART_ENABLE_IT(&huart3, UART_IT_TXE);
  104. /* USER CODE END USART3_Init 2 */
  105. }
  106. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  107. {
  108. GPIO_InitTypeDef GPIO_InitStruct = {0};
  109. if(uartHandle->Instance==USART1)
  110. {
  111. /* USER CODE BEGIN USART1_MspInit 0 */
  112. /* USER CODE END USART1_MspInit 0 */
  113. /* USART1 clock enable */
  114. __HAL_RCC_USART1_CLK_ENABLE();
  115. __HAL_RCC_GPIOA_CLK_ENABLE();
  116. /**USART1 GPIO Configuration
  117. PA9 ------> USART1_TX
  118. PA10 ------> USART1_RX
  119. */
  120. GPIO_InitStruct.Pin = GPIO_PIN_9;
  121. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  122. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  123. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  124. GPIO_InitStruct.Pin = GPIO_PIN_10;
  125. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  126. GPIO_InitStruct.Pull = GPIO_NOPULL;
  127. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  128. /* USART1 interrupt Init */
  129. HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
  130. HAL_NVIC_EnableIRQ(USART1_IRQn);
  131. /* USER CODE BEGIN USART1_MspInit 1 */
  132. /* USER CODE END USART1_MspInit 1 */
  133. }
  134. else if(uartHandle->Instance==USART2)
  135. {
  136. /* USER CODE BEGIN USART2_MspInit 0 */
  137. /* USER CODE END USART2_MspInit 0 */
  138. /* USART2 clock enable */
  139. __HAL_RCC_USART2_CLK_ENABLE();
  140. __HAL_RCC_GPIOA_CLK_ENABLE();
  141. /**USART2 GPIO Configuration
  142. PA2 ------> USART2_TX
  143. PA3 ------> USART2_RX
  144. */
  145. GPIO_InitStruct.Pin = GPIO_PIN_2;
  146. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  147. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  148. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  149. GPIO_InitStruct.Pin = GPIO_PIN_3;
  150. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  151. GPIO_InitStruct.Pull = GPIO_NOPULL;
  152. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  153. /* USART2 DMA Init */
  154. /* USART2_RX Init */
  155. hdma_usart2_rx.Instance = DMA1_Channel6;
  156. hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  157. hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  158. hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
  159. hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  160. hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  161. hdma_usart2_rx.Init.Mode = DMA_NORMAL;
  162. hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
  163. if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
  164. {
  165. Error_Handler();
  166. }
  167. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);
  168. /* USART2 interrupt Init */
  169. HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
  170. HAL_NVIC_EnableIRQ(USART2_IRQn);
  171. /* USER CODE BEGIN USART2_MspInit 1 */
  172. /* USER CODE END USART2_MspInit 1 */
  173. }
  174. else if(uartHandle->Instance==USART3)
  175. {
  176. /* USER CODE BEGIN USART3_MspInit 0 */
  177. /* USER CODE END USART3_MspInit 0 */
  178. /* USART3 clock enable */
  179. __HAL_RCC_USART3_CLK_ENABLE();
  180. __HAL_RCC_GPIOD_CLK_ENABLE();
  181. /**USART3 GPIO Configuration
  182. PD8 ------> USART3_TX
  183. PD9 ------> USART3_RX
  184. */
  185. GPIO_InitStruct.Pin = GPIO_PIN_8;
  186. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  187. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  188. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  189. GPIO_InitStruct.Pin = GPIO_PIN_9;
  190. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  191. GPIO_InitStruct.Pull = GPIO_NOPULL;
  192. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  193. __HAL_AFIO_REMAP_USART3_ENABLE();
  194. /* USART3 DMA Init */
  195. /* USART3_TX Init */
  196. hdma_usart3_tx.Instance = DMA1_Channel2;
  197. hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  198. hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  199. hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
  200. hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  201. hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  202. hdma_usart3_tx.Init.Mode = DMA_NORMAL;
  203. hdma_usart3_tx.Init.Priority = DMA_PRIORITY_HIGH;
  204. if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
  205. {
  206. Error_Handler();
  207. }
  208. __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
  209. /* USART3_RX Init */
  210. hdma_usart3_rx.Instance = DMA1_Channel3;
  211. hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  212. hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  213. hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
  214. hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  215. hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  216. hdma_usart3_rx.Init.Mode = DMA_NORMAL;
  217. hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
  218. if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
  219. {
  220. Error_Handler();
  221. }
  222. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
  223. /* USART3 interrupt Init */
  224. HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
  225. HAL_NVIC_EnableIRQ(USART3_IRQn);
  226. /* USER CODE BEGIN USART3_MspInit 1 */
  227. /* USER CODE END USART3_MspInit 1 */
  228. }
  229. }
  230. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  231. {
  232. if(uartHandle->Instance==USART1)
  233. {
  234. /* USER CODE BEGIN USART1_MspDeInit 0 */
  235. /* USER CODE END USART1_MspDeInit 0 */
  236. /* Peripheral clock disable */
  237. __HAL_RCC_USART1_CLK_DISABLE();
  238. /**USART1 GPIO Configuration
  239. PA9 ------> USART1_TX
  240. PA10 ------> USART1_RX
  241. */
  242. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  243. /* USART1 interrupt Deinit */
  244. HAL_NVIC_DisableIRQ(USART1_IRQn);
  245. /* USER CODE BEGIN USART1_MspDeInit 1 */
  246. /* USER CODE END USART1_MspDeInit 1 */
  247. }
  248. else if(uartHandle->Instance==USART2)
  249. {
  250. /* USER CODE BEGIN USART2_MspDeInit 0 */
  251. /* USER CODE END USART2_MspDeInit 0 */
  252. /* Peripheral clock disable */
  253. __HAL_RCC_USART2_CLK_DISABLE();
  254. /**USART2 GPIO Configuration
  255. PA2 ------> USART2_TX
  256. PA3 ------> USART2_RX
  257. */
  258. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
  259. /* USART2 DMA DeInit */
  260. HAL_DMA_DeInit(uartHandle->hdmarx);
  261. /* USART2 interrupt Deinit */
  262. HAL_NVIC_DisableIRQ(USART2_IRQn);
  263. /* USER CODE BEGIN USART2_MspDeInit 1 */
  264. /* USER CODE END USART2_MspDeInit 1 */
  265. }
  266. else if(uartHandle->Instance==USART3)
  267. {
  268. /* USER CODE BEGIN USART3_MspDeInit 0 */
  269. /* USER CODE END USART3_MspDeInit 0 */
  270. /* Peripheral clock disable */
  271. __HAL_RCC_USART3_CLK_DISABLE();
  272. /**USART3 GPIO Configuration
  273. PD8 ------> USART3_TX
  274. PD9 ------> USART3_RX
  275. */
  276. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8|GPIO_PIN_9);
  277. /* USART3 DMA DeInit */
  278. HAL_DMA_DeInit(uartHandle->hdmatx);
  279. HAL_DMA_DeInit(uartHandle->hdmarx);
  280. /* USART3 interrupt Deinit */
  281. HAL_NVIC_DisableIRQ(USART3_IRQn);
  282. /* USER CODE BEGIN USART3_MspDeInit 1 */
  283. /* USER CODE END USART3_MspDeInit 1 */
  284. }
  285. }
  286. /* USER CODE BEGIN 1 */
  287. //void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  288. //{
  289. // HAL_StatusTypeDef flag = 0;
  290. // if(huart->Instance == USART3){
  291. // HAL_UARTEx_ReceiveToIdle_IT(huart,(uint8_t *)g_usart3_rx_buf,USART3_REC_LEN);
  292. // if(flag == HAL_OK){
  293. // g_usart3_rx_sta = 0x33;
  294. // }
  295. //
  296. // }
  297. //
  298. //}
  299. //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  300. //{
  301. // if(huart->Instance == USART3)
  302. // {
  303. //
  304. // }
  305. //}
  306. //void HAL_UART_IdleCpltCallback(UART_HandleTypeDef *huart)
  307. //{
  308. // if(huart->Instance == USART3)
  309. // {
  310. // RxFlag = 1;//置位接收完成标志
  311. //
  312. // }
  313. //}
  314. /* USER CODE END 1 */