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