usart(6149).c 11 KB

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