usart(6628).c 9.5 KB

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